目錄
前言
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
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
責任編輯:xj
原文標題:9個必須掌握的Linux性能調優命令和工具
-
Linux
+關注
關注
87文章
11345瀏覽量
210385 -
命令
+關注
關注
5文章
696瀏覽量
22107 -
性能
+關注
關注
0文章
271瀏覽量
19038
原文標題:9個必須掌握的Linux性能調優命令和工具
文章出處:【微信號:gh_c472c2199c88,微信公眾號:嵌入式微處理器】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
Linux下如何管理虛擬內存 使用虛擬內存時的常見問題
LMX2592正常使用情況下溫度大概有多少?
TMS320C64x在高性能DSP應用中的高速緩存使用情況
![TMS320C64x在高<b class='flag-5'>性能</b>DSP應用中的高速緩存<b class='flag-5'>使用情況</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
Linux服務器性能查看方法
全志Linux磁盤操作基礎命令
Banana Pi BPI-M7 RK3588開發板在 Linux 上檢查 Rockchip NPU 使用情況
![Banana Pi BPI-M7 RK3588開發板在 <b class='flag-5'>Linux</b> 上檢查 Rockchip NPU <b class='flag-5'>使用情況</b>](https://file1.elecfans.com//web2/M00/FB/29/wKgZomaPnDiAOGuJAALT5qEdEtI669.jpg)
評論