目录
前言
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、进程和线程等。
命令:
#运行top top #显示进程的完整命令 top-c
推荐使用下面的htop命令来代替top命令。
htop命令
htop命令用来监控系统的资源使用情况,包括cpu、内存、swap、进程和线程等。相比top命令,htop命令更能更强大,显示也更加直观。
命令:
#运行htop htop
概要信息说明:
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:正在运行的进程数;
系统平均负载统计:load average:前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:进程使用的虚拟内存(virtual memory);
res:进程使用的物理内存(physical memory);
shr:进程使用的共享内存(shared memory);
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 #获取指定进程的pid ps-ef|grepjava|grepjenkins|grep-vgrep|awk'{print$2}' #根据进程的pid中止进程 ps-ef|grepjava|grepjenkins|grep-vgrep|awk'{print$2}'|xargskill-9 #查看进程堆栈 #p(path)显示完整路径 pmap-p #查看9091端口是哪个容器进程 dockerps|grep9091
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: the amount of ram installed in your system.
used: equal tototal-(free+buffers+cache).
free: the amount of memory completely unused by anything.
shared: memory taken by thetmpfsfile systems.
buffer: the data structures that are maintained to provide an index for everything stored incache.
cache: data read from the hard drive, modified data waiting to be written back to the hard drive, and other computed values.
available: what’s really free. an estimate of the memory infree,buffer, andcachethat could be used to satisfy a memory request.
简单来说,total是系统总内存,used就是系统已用内存,total - used 就是系统可用内存。
参考:
https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/
vmstat命令
vmstat命令用来统计进程、内存、swap、io和cpu等信息。
命令:
#运行vmstat vmstat #每5秒统计1次 vmstat5 #每5秒统计1次,共统计4次 vmstat54 #以mb格式输出内存使用情况 vmstat54-sm #显示统计概要信息 vmstat-s
说明:
proc
r: the number of runnable processes. these are processes that have been launched and are either running or are waiting for their next time-sliced burst of cpu cycles.
b: the number of processes in uninterruptible sleep. the process isn’t sleeping, it is performing a blocking system call, and it cannot be interrupted until it has completed its current action. typically the process is a device driver waiting for some resource to come free. any queued interrupts for that process are handled when the process resumes its usual activity.
memory
swpd: the amount of virtual memory used. in other words, how much memory has been swapped out.,
free: the amount of idle (currently unused) memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
swap
si: amount of virtual memory swappedinfrom swap space.
so: amount of virtual memory swappedoutto swap space.
io
bi: blocks received from a block device. the number of data blocks used to swap virtual memory back into ram.
bo: blocks sent to a block device. the number of data blocks used to swap virtual memory out of ram and into swap space.
system
in: the number of interrupts per second, including the clock.
cs: the number of context switches per second. a context switch is when the kernel swaps from system mode processing into user mode processing.
cpu
these values are all percentages of the total cpu time.
us: time spent running non-kernel code. that is, how much time is spent in user time processing and in nice time processing.
sy: time spent running kernel code.
id: time spent idle.
wa: time spent waiting for input or output.
st: time stolen from a virtual machine. this is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and attend to this virtual machine.
参考:
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等信息。
命令:
#运行iostat iostat #以mb格式输出 iostat-m #查看指定磁盘(分区) iostat-m-pvda #每2秒统计一次,共统计3次 iostat-m23
说明:
cpu信息:
%user :it shows the percentage of cpu being utilization that while executing at the user level.
%nice :it shows the percentage of cpu utilization that occurred while executing at the user level with a nice priority.
%system :it shows the percentage of cpu utilization that occurred while executing at the system (kernel) level.
%iowait :it shows the percentage of the time that the cpu or cpus were idle during which the system had an outstanding disk i/o request.
%steal :it shows the percentage of time being spent in involuntary wait by the virtual cpu or cpus while the hypervisor was servicing by another virtual processor.
%idle :it shows the percentage of time that the cpu or cpus were idle and the system did not have an outstanding disk i/o request.
磁盘信息:
device :the device/partition name is listed in/devdirectory.
tps :the number of transfers per second that were issued to the device. higher tps means the processor is busier.
blk_read/s :it shows the amount of data read from the device expressed in a number of blocks (kilobytes, megabytes) per second.
blk_wrtn/s :the amount of data written to the device expressed in a number of blocks (kilobytes, megabytes) per second.
blk_read :it shows the total number of blocks read.
blk_wrtn :it shows the total number of blocks written.
参考:
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
原文标题:9个必须掌握的linux性能调优命令和工具
文章出处:【微信公众号:嵌入式arm】欢迎添加关注!文章转载请注明出处。
光纤网络和普通网络的区别
嵌入式系统 Boot Loader技术内幕
半导体材料迎来重大机遇
华为已下单14nm中芯国际制造工艺订单
路由器限制人数怎么设置_无线路由器怎么限制连接人数
Linux查看资源使用情况和性能调优常用的命令
欧盟RoHS指令新增豁免条款六价铬
全球PC市场逐渐回暖苹果Mac销量却出现下滑
博通集成成为2019年首批过会企业
采用神经网络和DSP模块实现锡炉在线实时补偿加热控制系统的设计
蓝牙模块特征详细解析
SD130T带充电平衡,双节锂电池充电控制芯片
微处理器和片上系统的全速实时调试
业界联动共推福建人工智能产业发展
AR光波导显示技术引领者灵犀微光将登陆2021 ICDT
解析磷酸铁锂电池电压为什么是3.2v
松下伺服电机调整电机负载如何设置?
车用半导体市场将年增9%达到320亿美元
高通/联发科/华为海思/紫光展锐四家的芯片专家聚在一起会发生什么?
Hitless Protection Switching w