目录
前言
top命令
htop命令
ps命令
free命令
vmstat命令
iosat命令
netstat命令
df命令
du命令
前言
一般来说,Linux性能调优涉及CPU、内存和IO,包括进程、线程、程序堆栈、内存、Swap、CPU调度、内存调度、网络连接和IO读写等。
本文介绍了Linux查看资源使用情况和性能调优常用的命令,包括top、htop、ps、free、vmstat、iostat、netstat、df和du命令。
top命令
top命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。
命令:
#运行toptop#显示进程的完整命令top-c
推荐使用下面的htop命令来代替top命令。
htop命令
htop命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。相比top命令,htop命令更能更强大,显示也更加直观。
命令:
#运行htophtop
概要信息说明:
CPU使用率:数字1、2、...表示CPU有几个核,后面的进度条用不同颜色来表示不同维度的CPU使用率,最后是CPU使用率;
内存使用率:Mem后面的进度条用不同颜色来表示不同维度的内存使用率(绿色:used,蓝色:buffers,橙色:cache),最后是内存使用率(used/total);
Swap使用率:Swap后面的进度条显示已用的Swap,最后是Swap使用率(used/total),如果系统关闭了Swap,则进度条为空,Swap使用率为0K/0K;
进程和线程统计:Tasks:进程数,thr:线程数,running:正在运行的进程数;
系统平均负载统计:Loadaverage:前1分钟平均负载、前5分钟平均负载和前15分钟平均负载;负载越低说明CPU越空闲,负载越高说明CPU越忙:
负载低于0.7*CPU核数:CPU使用率正常;(道路通畅)
负载高于0.7*CPU核数:CPU使用率较高,需要关注;(道路堵车,需要交警到场疏导交通)
负载高于1*CPU核数:CPU使用率过高,需要关注;(道路严重堵车,可能发生了交通事故,需要交警马上到现场处理)
负载高于5*CPU核数:系统超负荷运转,无法正常响应;(交通瘫痪,可能道路已经被水淹无法通行,需要投入抢险力量恢复交通)
系统已连续运行时间:Uptime后面为系统从上次启动后,已连续运行时间,可以倒推系统上次启动时间;
详细信息说明:
PID:进程ID;
USER:进程所有者;
PRI:进程优先级;
NI:nice值,负值表示高优先级,正值表示低优先级;
VIRT:进程使用的虚拟内存(virtualmemory);
RES:进程使用的物理内存(physicalmemory);
SHR:进程使用的共享内存(sharedmemory);
S:进程状态,R(Running)正在运行的进程、S(Sleeping)休眠的进程、T/S(Traced/Stopped)已停止或中止的进程或Z(Zommbie)僵尸进程;
CPU%:进程使用的CPU时间百分比;
MEM%:进程使用的内存百分比;
TIME+:进程已连续运行时间;
Command:进程的执行命令;
常用快捷键:
上下箭头:上下滚动查看进程;
u:显示指定用户的进程;
P:按照进程使用的CPU时间百分比排序;
M:按照进程使用的内存百分比排序;
T:按照进程已连续运行时间排序;
参考:
https://linuxtogether.org/htop-command-explanation/
https://www.softprayog.in/tutorials/htop-command-in-linux
https://scoutapm.com/blog/understanding-load-averages
htop官网
ps命令
#查看java进程#e(every)所有进程#f(fullformat)完整输出格式ps-ef|grepnginx#不显示grep本身ps-ef|grepnginx|grep-vgrep#获取指定进程的PIDps-ef|grepjava|grepjenkins|grep-vgrep|awk'{print$2}'#根据进程的PID中止进程ps-ef|grepjava|grepjenkins|grep-vgrep|awk'{print$2}'|xargskill-9#查看进程堆栈#p(path)显示完整路径pmap
ps-ef格式:
UID:进程的UID(用户)
PID:进程ID
PPID:父进程ID
C:CPU使用时间百分比
STIME
TTY
TIME
CMD:启动进程的命令
free命令
free命令用来查看内存和Swap的使用情况。
命令:
#以适合阅读的形式输出(h:human)free-h#以MB格式输出(m:MB)free-m#合计Mem和Swap(t:total)free-h-t#统计3次,每秒1次(c:count)free-h-c3#统计5次,每2秒统计1次(s:second)free-h-t-c5-s2
说明:
Total:TheamountofRAMinstalledinyoursystem.
Used:EqualtoTotal-(Free+Buffers+Cache).
Free:Theamountofmemorycompletelyunusedbyanything.
Shared:Memorytakenbythetmpfsfilesystems.
Buffer:ThedatastructuresthataremaintainedtoprovideanindexforeverythingstoredinCache.
Cache:Datareadfromtheharddrive,modifieddatawaitingtobewrittenbacktotheharddrive,andothercomputedvalues.
Available:What’sreallyfree.AnestimateofthememoryinFree,Buffer,andCachethatcouldbeusedtosatisfyamemoryrequest.
简单来说,total是系统总内存,used就是系统已用内存,total-used就是系统可用内存。
参考:
https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/
vmstat命令
vmstat命令用来统计进程、内存、Swap、IO和CPU等信息。
命令:
#运行vmstatvmstat#每5秒统计1次vmstat5#每5秒统计1次,共统计4次vmstat54#以MB格式输出内存使用情况vmstat54-SM#显示统计概要信息vmstat-s
说明:
Proc
r:Thenumberofrunnableprocesses.Theseareprocessesthathavebeenlaunchedandareeitherrunningorarewaitingfortheirnexttime-slicedburstofCPUcycles.
b:Thenumberofprocessesinuninterruptiblesleep.Theprocessisn’tsleeping,itisperformingablockingsystemcall,anditcannotbeinterrupteduntilithascompleteditscurrentaction.Typicallytheprocessisadevicedriverwaitingforsomeresourcetocomefree.Anyqueuedinterruptsforthatprocessarehandledwhentheprocessresumesitsusualactivity.
Memory
swpd:theamountofvirtualmemoryused.Inotherwords,howmuchmemoryhasbeenswappedout.,
free:theamountofidle(currentlyunused)memory.
buff:theamountofmemoryusedasbuffers.
cache:theamountofmemoryusedascache.
Swap
si:Amountofvirtualmemoryswappedinfromswapspace.
so:Amountofvirtualmemoryswappedouttoswapspace.
IO
bi:Blocksreceivedfromablockdevice.ThenumberofdatablocksusedtoswapvirtualmemorybackintoRAM.
bo:Blockssenttoablockdevice.ThenumberofdatablocksusedtoswapvirtualmemoryoutofRAMandintoswapspace.
System
in:Thenumberofinterruptspersecond,includingtheclock.
cs:Thenumberofcontextswitchespersecond.Acontextswitchiswhenthekernelswapsfromsystemmodeprocessingintousermodeprocessing.
CPU
ThesevaluesareallpercentagesofthetotalCPUtime.
us:Timespentrunningnon-kernelcode.Thatis,howmuchtimeisspentinusertimeprocessingandinnicetimeprocessing.
sy:Timespentrunningkernelcode.
id:Timespentidle.
wa:Timespentwaitingforinputoroutput.
st:Timestolenfromavirtualmachine.Thisisthetimeavirtualmachinehastowaitforthehypervisortofinishservicingothervirtualmachinesbeforeitcancomebackandattendtothisvirtualmachine.
参考:
https://www.howtogeek.com/424334/how-to-use-the-vmstat-command-on-linux/
https://www.tecmint.com/linux-performance-monitoring-with-vmstat-and-iostat-commands/
iosat命令
iostat命令用来统计磁盘IO和CPU等信息。
命令:
#运行iostatiostat#以MB格式输出iostat-m#查看指定磁盘(分区)iostat-m-pvda#每2秒统计一次,共统计3次iostat-m23
说明:
CPU信息:
%user:ItshowsthepercentageofCPUbeingutilizationthatwhileexecutingattheuserlevel.
%nice:ItshowsthepercentageofCPUutilizationthatoccurredwhileexecutingattheuserlevelwithanicepriority.
%system:ItshowsthepercentageofCPUutilizationthatoccurredwhileexecutingatthesystem(kernel)level.
%steal:ItshowsthepercentageoftimebeingspentininvoluntarywaitbythevirtualCPUorCPUswhilethehypervisorwasservicingbyanothervirtualprocessor.
%idle:ItshowsthepercentageoftimethattheCPUorCPUswereidleandthesystemdidnothaveanoutstandingdiskI/Orequest.
磁盘信息:
Device:Thedevice/partitionnameislistedin/devdirectory.
tps:Thenumberoftransferspersecondthatwereissuedtothedevice.Highertpsmeanstheprocessorisbusier.
Blk_read/s:Itshowstheamountofdatareadfromthedeviceexpressedinanumberofblocks(kilobytes,megabytes)persecond.
Blk_wrtn/s:Theamountofdatawrittentothedeviceexpressedinanumberofblocks(kilobytes,megabytes)persecond.
Blk_read:Itshowsthetotalnumberofblocksread.
Blk_wrtn:Itshowsthetotalnumberofblockswritten.
参考:
https://www.geeksforgeeks.org/iostat-command-in-linux-with-examples/
https://www.techrepublic.com/article/how-to-use-the-linux-iostat-command-to-check-on-your-storage-subsystem/
netstat命令
netstat命令用来统计网络连接和端口等信息。
命令:
#显示在侦听的TCP/UDP网络连接,包括程序和端口信息#u(UDP)#t(TCP)#n(port)#l(listen)#p(program)netstat-utnlp#只显示在侦听的TCP网络连接,包括程序和端口信息netstat-tnlp#查看某个端口被哪个进程使用#a(all)netstat-anp|grep6379#查看某个进程在哪个端口netstat-anp|grepredis#查看docker启动的进程在哪个端口dockerps|grepjenkins
df命令
df命令用来统计磁盘使用情况。
命令:
#查看全部文件系统的磁盘使用情况df-h#查看指定目录所在的文件系统的磁盘使用情况df-h~#不显示docker文件系统df-h|grep-vdocker
du命令
du命令用来统计指定目录的大小。
命令:
#统计某个目录大小du-sh~#统计目录下各个子目录和文件的大小du-h~#统计目录下各个子目录和文件的大小,并显示合计大小du-ch~#只统计下一级目录大小du-h--max-depth1#只统计下一级目录大小,单位为MB,从大到小排序du-m--max-depth1|sort-rn#只统计下一级目录大小,单位为MB,从大到小排序,返回最大的10个文件(目录)du-m--max-depth1|sort-rn|head-11
责任编辑:xj
原文标题:9个必须掌握的Linux性能调优命令和工具
文章出处:【微信公众号:嵌入式ARM】欢迎添加关注!文章转载请注明出处。
原文标题:9个必须掌握的Linux性能调优命令和工具
文章出处:【微信号:gh_c472c2199c88,微信公众号:嵌入式ARM】欢迎添加关注!文章转载请注明出处。
评论