页面加载中,请稍候
来源:开源Linux发布时间:2023-03-06371浏览
询问 AI
来源:网络技术联盟站
链接:https://www.wljslmz.cn/19901.html
cat/proc/cpuinfo
#物理CPU个数
cat/proc/cpuinfo|grep'physicalid'|sort|uniq|wc-l
#每个CPU核心数
cat/proc/cpuinfo|grep'coreid'|sort|uniq|wc-l
#逻辑CPU
cat/proc/cpuinfo|grep'processor'|sort|uniq|wc-l
#mpstat
mpstat
mpstat210
cat/proc/meminfo
free-gt
df-hT
du-csh./*
操作系统 IPC 共享内存/队列:
ipcs#(shmems,queues,semaphores)
平时我们经常需要监控内存的使用状态,常用的命令有free、vmstat、top、dstat -m等。
>free-h
totalusedfreesharedbufferscached
Mem:7.7G6.2G1.5G17M33M184M
-/+buffers/cache:6.0G1.7G
Swap:24G581M23G
第一行Mem:
其中有:
total=used+free
第二行-/+ buffers/cache,代表应用程序实际使用的内存:
可以看到,这两个值加起来也是total
第三行swap,代表交换分区的使用情况:总量、使用的和未使用的
cache代表缓存,当系统读取文件时,会先把数据从硬盘读到内存里,因为硬盘比内存慢很多,所以这个过程会很耗时。
为了提高效率,Linux 会把读进来的文件在内存中缓存下来(局部性原理),即使程序结束,cache 也不会被自动释放。因此,当有程序进行大量的读文件操作时,就会发现内存使用率升高了。
当其他程序需要使用内存时,Linux 会根据自己的缓存策略(例如 LRU)将这些没人使用的 cache 释放掉,给其他程序使用,当然也可以手动释放缓存:
echo1>/proc/sys/vm/drop_caches
考虑内存写文件到硬盘的场景,因为硬盘太慢了,如果内存要等待数据写完了之后才继续后面的操作,效率会非常低,也会影响程序的运行速度,所以就有了缓冲区buffer。
当内存需要写数据到硬盘中时会先放到 buffer 里面,内存很快把数据写到 buffer 中,可以继续其他工作,而硬盘可以在后台慢慢读出 buffer 中的数据并保存起来,这样就提高了读写的效率。
例如把电脑中的文件拷贝到 U 盘时,如果文件特别大,有时会出现这样的情况:明明看到文件已经拷贝完,但系统还是会提示 U 盘正在使用中。这就是 buffer 的原因:拷贝程序虽然已经把数据放到 buffer 中,但是还没有全部写入到 U 盘中
同样的,可以使用sync命令来手动flush buffer中的内容:
>sync--help
Usage:sync[OPTION][FILE]...
Synchronizecachedwritestopersistentstorage
Ifoneormorefilesarespecified,synconlythem,
ortheircontainingfilesystems.
-d,--datasynconlyfiledata,nounneededmetadata
-f,--file-systemsyncthefilesystemsthatcontainthefiles
--helpdisplaythishelpandexit
--versionoutputversioninformationandexit
GNUcoreutilsonlinehelp:<http://www.gnu.org/software/coreutils/>
Fulldocumentationat:<http://www.gnu.org/software/coreutils/sync>
oravailablelocallyvia:info'(coreutils)syncinvocation'
交换分区swap是实现虚拟内存的重要概念。swap就是把硬盘上的一部分空间当作内存来使用,正在运行的程序会使用物理内存,把未使用的内存放到硬盘,叫做swap out。而把硬盘交换分区中的内存重新放到物理内存中,叫做swap in。
交换分区可以在逻辑上扩大内存空间,但是也会拖慢系统速度,因为硬盘的读写速度很慢。Linux 系统会将不经常使用的内存放到交换分区中。
简单来说:page cache用来缓存文件数据,buffer cache用来缓存磁盘数据。在有文件系统的情况下,对文件操作,那么数据会缓存到page cache中。如果直接采用dd等工具对磁盘进行读写,那么数据会缓存到buffer cache中。
vmstat (Virtual Memory Statics,虚拟内存统计)是对系统的整体情况进行统计,包括内核进程、虚拟内存、磁盘、中断和CPU 活动的统计信息:
>vmstat--help
Usage:
vmstat[options][delay[count]]
Options:
-a,--activeactive/inactivememory
-f,--forksnumberofforkssinceboot
-m,--slabsslabinfo
-n,--one-headerdonotredisplayheader
-s,--statseventcounterstatistics
-d,--diskdiskstatistics
-D,--disk-sumsummarizediskstatistics
-p,--partition<dev>partitionspecificstatistics
-S,--unit<char>definedisplayunit
-w,--widewideoutput
-t,--timestampshowtimestamp
-h,--helpdisplaythishelpandexit
-V,--versionoutputversioninformationandexit
来源|公众号:网络技术干货圈
Formoredetailsseevmstat(8).
>vmstat-SM1100#1表示刷新间隔(秒),100表示打印次数,单位MB
procs-----------memory-------------swap-------io-----system--------cpu-----
rbswpdfreebuffcachesisobiboincsussyidwast
1004701881154000430009900
00047018811540000112231119800
00047018811540000911760010000
00047018811540000118229109900
00047018811540000781560010000
00047018811540006484186019720
这里设置的bi+bo参考值为1000,如果超过1000,且wa值比较大,则表示系统磁盘 I/O 性能瓶颈
上面这两个值越大,内核消耗的 CPU 时间就越多
ifconfig
iftop
ethtool
#端口
netstat-ntlp#TCP
netstat-nulp#UDP
netstat-nxlp#UNIX
netstat-nalp#不仅展示监听端口,还展示其他阶段的连接
lsof-p<PID>-P
lsof-i:5900
sar-nDEV1#网络流量
ss
ss-s
sudotcpdump-ianyudpport20112andip[0x1f:02]=0x4e91-XNnvvv
sudotcpdump-iany-XNnvvv
sudotcpdump-ianyudp-XNnvvv
sudotcpdump-ianyudpport20112-XNnvvv
sudotcpdump-ianyudpport20112andip[0x1f:02]=0x4e91-XNnvvv
监控各进程的网络流量
nethogs
iotop
iostat
iostat-kx2
vmstat-SM
vmstat210
dstat
dstat--top-io--top-bio
top
top-H
htop
psauxf
ps-eLf#展示线程
ls/proc/<PID>/task
例如最常用的top命令:
HelpforInteractiveCommands-procpsversion3.2.8
Window1:Def:CumulativemodeOff.System:Delay3.0secs;SecuremodeOff.
Z,BGlobal:'Z'changecolormappings;'B'disable/enablebold
l,t,mToggleSummaries:'l'loadavg;'t'task/cpustats;'m'meminfo
1,IToggleSMPview:'1'single/separatestates;'I'Irix/Solarismode
f,o.Fields/Columns:'f'addorremove;'o'changedisplayorder
ForO.Selectsortfield
<,>.Movesortfield:'<'nextcolleft;'>'nextcolright
R,H.Toggle:'R'normal/reversesort;'H'showthreads
c,i,S.Toggle:'c'cmdname/line;'i'idletasks;'S'cumulativetime
x,y.Togglehighlights:'x'sortfield;'y'runningtasks
z,b.Toggle:'z'color/mono;'b'bold/reverse(onlyif'x'or'y')
u.Showspecificuseronly
nor#.Setmaximumtasksdisplayed
k,rManipulatetasks:'k'kill;'r'renice
dorsSetupdateinterval
WWriteconfigurationfile
qQuit
(commandsshownwith'.'requireavisibletaskdisplaywindow)
Press'h'or'?'forhelpwithWindows,
anyotherkeytocontinue
us-Timespentinuserspace
sy-Timespentinkernelspace
ni-Timespentrunningniceduserprocesses(Userdefinedpriority)
id-Timespentinidleoperations
wa-TimespentonwaitingonIOperipherals(eg.disk)
hi-Timespenthandlinghardwareinterruptroutines.(WheneveraperipheralunitwantattentionformtheCPU,itliterallypullsaline,tosignaltheCPUtoserviceit)
来源|公众号:网络技术干货圈
si-Timespenthandlingsoftwareinterruptroutines.(apieceofcode,callsaninterruptroutine...)
st-Timespentoninvoluntarywaitsbyvirtualcpuwhilehypervisorisservicinganotherprocessor(stolenfromavirtualmachine)
lsof-P-p123
stress--cpu8\
--io4\
--vm2\
--vm-bytes128M\
--timeout60s
time命令
w
whoami
uptime
htop
vmstat
mpstat
dstat
lspci
lscpu
lsblk
lsblk-fm#显示文件系统、权限
lshw-cdisplay
dmidecode
#挂载
mount
umount
cat/etc/fstab
#LVM
pvdisplay
pvs
lvdisplay
lvs
vgdisplay
vgs
df-hT
lsof
cat/proc/modules
sysctl-a|grep...
cat/proc/interrupts
dmesg
less/var/log/messages
less/var/log/secure
less/var/log/auth
crontab-l
crontab-l-unobody
#查看所有用户的cron
sudofind/var/spool/cron/|sudoxargscat
strace命令用于打印系统调用、信号:
strace-p
strace-p5191-f
strace-etrace=signal-p5191
-etrace=open
-etrace=file
-etrace=process
-etrace=network
-etrace=signal
-etrace=ipc
-etrace=desc
-etrace=memory
ltrace命令用于打印动态链接库访问:
ltrace-p<PID>
ltrace-S#syscall
w#显示当前登录的用户、登录IP、正在执行的进程等
last#看看最近谁登录了服务器、服务器重启时间
uptime#开机时间、登录用户、平均负载
history#查看历史命令
cat/proc/...
cgroups
cmdline
cpuinfo
crypto
devices
diskstats
filesystems
iomem
ioports
kallsyms
meminfo
modules
partitions
uptime
version
vmstat
nohup<command>>[some.log]
#综合
top
htop
glances
dstatsar
mpstat
#性能分析
perf
#进程
ps
pstree-p
pgrep
pkill
pidof
Ctrl+zjobsfg
#网络
ip
ifconfig
dig
ping
traceroute
iftop
pingtop
nload
netstat
vnstat
slurm
scp
tcpdump
#磁盘I/O
iotop
iostat
#虚拟机
virt-top
#用户
w
whoami
#运行时间
uptime
#磁盘
du
df
lsblk
#权限
chown
chmod
#服务
systemctllist-unit-files
#定位
find
locate
#性能测试
time

10T 技术资源大放送!包括但不限于:Linux、虚拟化、容器、云计算、网络、Python、Go 等。在开源Linux公众号内回复10T,即可免费获取!
新闻来源:开源Linux,文中所述为作者独立观点,不代表icspec立场。更多精彩资讯请下载icspec App。如对本稿件有异议,请联系微信客服specltkj。
暂无评论哦,快来评论一下吧!

2026-07-14

2026-07-07

2026-07-01