分析cpu使用情況的方法
分析cpu使用情況的方法
大家都知道怎么使用電腦,但是你知道怎么分析cpu使用情況嗎?不知道的話跟著學(xué)習(xí)啦小編一起來學(xué)習(xí)了解怎么分析cpu使用情況。
怎么分析cpu使用情況
要分析系統(tǒng)的CPU資源是否夠的前提誰占用了CPU資源,占用了多少,時(shí)間多長(zhǎng)。 下面是一些衡量CPU閑忙程度的經(jīng)用指標(biāo):
1)用戶使用CPU的情況
CPU運(yùn)行常規(guī)用戶進(jìn)程
CPU運(yùn)行niced process
CPU運(yùn)行實(shí)時(shí)進(jìn)程
2)系統(tǒng)使用CPU的情況
用于系統(tǒng)調(diào)用
用于I/O管理:中斷和驅(qū)動(dòng)
用于內(nèi)存管理:paging and swapping
用于進(jìn)程管理:context switch and process start
3)WIO:由于進(jìn)程等待I/O而使CPU處于空閑狀態(tài)的比率,這些I/O主要指block I/O,raw I/O,VM paging/swapins;
4)CPU的空閑率,即除了上面的WIO以外的空閑情況;
5)CPU用于上下文交換的比率(Context Switch CPU utilization)
6)nice
7)real-time
運(yùn)行進(jìn)程隊(duì)列的長(zhǎng)度,即處于可運(yùn)行狀態(tài)的進(jìn)程個(gè)數(shù)的大小,不過我們關(guān)心的是這些在等待CPU調(diào)度執(zhí)行時(shí)所花的時(shí)間;
9)平均負(fù)載(load average)
CPU資源成為系統(tǒng)性能的瓶頸的征兆
CPU就像人的大腦,完成各種交給它的任務(wù)。如果任務(wù)太多,CPU就要忙不過來,它的運(yùn)行效率就要下降。就像人生病會(huì)有一典型癥狀一樣,當(dāng)CPU資源 成為系統(tǒng)性能的瓶頸時(shí),它也有一些典型的癥狀:
很慢的響應(yīng)時(shí)間(slow response time)
CPU空閑時(shí)間為零(zero percent idle CPU)
過高的用戶占用CPU時(shí)間(high percent user CPU)
過高的系統(tǒng)占用CPU時(shí)間(high percent system CPU)
長(zhǎng)時(shí)間的有很長(zhǎng)的運(yùn)行進(jìn)程隊(duì)列(large run queue size sustained over time)
processes blocked on prority
必須注意的是,如果系統(tǒng)出現(xiàn)上面的這些癥狀并不能說一定是由于CPU資源不夠,事實(shí),有些癥狀 的出現(xiàn)很可能是由于其他資源的不足而引起,如內(nèi)存不夠時(shí),CPU會(huì)忙內(nèi)存管理的事,這時(shí)從表面上, CPU的利用是100%,甚至顯得不夠,如果據(jù)此就簡(jiǎn)單地認(rèn)為增加CPU就可以解決問題是大錯(cuò)特錯(cuò)了。
因此,還是那句話,必須用不同的工具、從不同的方面對(duì)系統(tǒng)進(jìn)行分析后,才能做出結(jié)論,即使這樣,經(jīng)驗(yàn)將 起到不可替代的作用。
哪些進(jìn)程是占用CPU資源的大戶?
在操作系統(tǒng)中,并不是所有的進(jìn)程都以同樣的方式使用CPU資源。通常情況下,有些進(jìn)程需要比其他進(jìn)程更多的CPU時(shí)間片才能順利地完成任務(wù)。 下面是一些典型的占用CPU資源的大戶:
進(jìn)程創(chuàng)建(process creation)
終端字符進(jìn)程(teminal character processes(MUX- and LAN-based)
計(jì)算密集型進(jìn)程和實(shí)時(shí)進(jìn)程
X-終端和X-服務(wù)器進(jìn)程(X-terminals and X-servers)
利用SAR工具分析CPU的利用率
利用SAR進(jìn)行CPU的利用率分析的命令形式:
#sar -u,這時(shí)數(shù)據(jù)是通過sa1在后臺(tái)定時(shí)生成;
#sar -u 5 100,每隔5秒取樣一次,共取100次;
SAR -u:Report CPU utilization (the default); portion of time running in one of several modes. On a multi-processor system, if the -M option is used together with the -u option, per-CPU utilization as well as the average CPU utilization of all the processors are reported. If the -M option is not used, only the average CPU utilization of all the processors is reported:
cpu: cpu number (only on a multi-processor system with the -M option);
%usr: user mode;
%sys: system mode;
%wio: idle with some process waiting for I/O (only block I/O, raw I/O, or VM pageins/swapins indicated);
%idle: otherwise idle;
對(duì)結(jié)果的分析
首先,我們看%idle列的值,如果為接近零,則再看對(duì)應(yīng)%wio列的值,如果這列的大于7,則表明系統(tǒng)的磁盤或其他I/O可能有問題,需要進(jìn)一步的分析:
用iostat命令分析各個(gè)磁盤的傳輸閑忙狀況,如#iostat -t 5 2,每隔5秒取樣一次,共取2次;
用sar -d命令分析各塊設(shè)備(磁盤、磁帶)活動(dòng)情況;
用sar -b命令分析系統(tǒng)的緩存的活動(dòng)情況;
用sar -w命令分析進(jìn)程的deactivation/reactivation and switching activities of the system;
如果%idle列很小,而對(duì)應(yīng)的%wio列的值也很小,這時(shí),我們查看%usr列和%sys列的值。如果%usr列的值很大,說明有用戶進(jìn)程占用很多CPU時(shí)間;如果%sys列的值很大,則說明系統(tǒng)管理方面花了很多時(shí)間。需要進(jìn)一步的分析:
用GlancePlus對(duì)占用CPU時(shí)間最大的進(jìn)程進(jìn)行單獨(dú)分析,為什么它會(huì)占用如此多的CPU時(shí)間。
如果%sys列的值很大,可以用SAR -C命令對(duì)系統(tǒng)調(diào)用進(jìn)行進(jìn)一步分解,看這些系統(tǒng)調(diào)用主要是做些什么。同時(shí),還必須分析是否有其他瓶頸,如paging也會(huì)引起%sys的值很大,這時(shí),可以用sar -q查看系統(tǒng)的運(yùn)行進(jìn)程隊(duì)列長(zhǎng)度,也可以用GlancePlus和vmstat查看內(nèi)存的使用情況;
利用SAR工具分析運(yùn)行進(jìn)程隊(duì)列長(zhǎng)度
利用SAR進(jìn)行運(yùn)行進(jìn)程隊(duì)列長(zhǎng)度分析的命令形式:
#sar -q,這時(shí)數(shù)據(jù)是通過sa1在后臺(tái)定時(shí)生成;
#sar -q 5 100,每隔5秒取樣一次,共取100次;
SAR -q: Report average queue length while occupied, and percent of time occupied. On a multi-processor machine, if the -M option is used together with the -q option, the per-CPU run queue as well as the average run queue of all the processors are reported. If the -M option is not used, only the average run queue information of all the processors is reported:
cpu: cpu number (only on a multi-processor system with the -M option);
runq-sz: Average length of the run queue(s) of processes (in memory and runnable);
%runocc: The percentage of time the run queue(s) were occupied by processes (in memory and runnable);
swpq-sz: Average length of the swap queue of runnable processes (processes swapped out but ready to run);
%swpocc: The percentage of time the swap queue of runnable processes (processes swapped out but ready to run) was occupied.
對(duì)結(jié)果的分析:
這些數(shù)據(jù)越小越好。
如果runq-sz大于4,或者%swapocc大于5時(shí),則表明系統(tǒng)的CPU或內(nèi)存可能有問題,需要進(jìn)一步的分析:
用sar -u命令分析CPU的使用情況;
用sar -w命令分析進(jìn)程的deactivation/reactivation and switching activities of the system;
也可以用GlancePlus;
利用SAR工具分析系統(tǒng)調(diào)用
利用SAR進(jìn)行系統(tǒng)調(diào)用分析的命令形式:
#sar -c,這時(shí)數(shù)據(jù)是通過sa1在后臺(tái)定時(shí)生成;
#sar -c 5 100,每隔5秒取樣一次,共取100次;
SAR -c: Report system calls:
scall/s: Number of system calls of all types per second;
sread/s: Number of read() and/or readv() system calls per second;
swrit/s: Number of write() and/or writev() system calls per second;
swpq-sz: Average length of the swap queue of runnable processes (processes swapped out but ready to run);
fork/s: Number of fork() and/or vfork() system calls per second;
exec/s: Number of exec() system calls per second;
rchar/s: Number of characters transferred by read system calls block devices only) per second;
wchar/s: Number of characters transferred by write system calls (block devices only) per second.
對(duì)結(jié)果的分析:
如果scall/s列的值很大,那么這么多的系統(tǒng)調(diào)用的原因就必須仔細(xì)分析了。
我們可以查看fork/s和exec/s列的值,看看系統(tǒng)是否在創(chuàng)建大量新的進(jìn)程。
利用time命令測(cè)試某個(gè)命令和程序的執(zhí)行效率
我們可以利用time命令來測(cè)試一個(gè)命令的執(zhí)行效率,語(yǔ)法為:
time command
command is executed. Upon completion, time prints the elapsed time during the command, the time spent in the system, and the time spent executing the command. Times are reported in seconds.
Execution time can depend on the performance of the memory in which the program is running.
當(dāng)我們覺得某個(gè)進(jìn)程的性能不好時(shí),最簡(jiǎn)單的方法就是利用time命令來查看一下進(jìn)程執(zhí)行時(shí)它的 時(shí)間分布情況,然后再用其他工具進(jìn)一步分析。
利用top命令查看最耗CPU資源的進(jìn)程
我們可以利用top命令來查看最耗CPU資源的進(jìn)程。top命令還會(huì)根據(jù)進(jìn)程占用CPU資源的多少而動(dòng)態(tài)改變。
它的語(yǔ)法為:
top [-s time] [-d count] [-q] [-u] [-h] [-n number]
其中各選項(xiàng)的含義為:
-s time: 屏幕刷新的時(shí)間間隔time,缺省為5秒;
-d count: 屏幕刷新count次后,top命令自己也退出;
-q: This option runs the top program at the same priority as if it is executed via a nice -20 command so that it will execute faster (see nice(1)). This can be very useful in discovering any system problem when the system is very sluggish. This option is accessibly only to users who have appropriate privileges.
-u: User ID (uid) numbers are displayed instead of usernames. This improves execution speed by eliminating the additional time required to map uid numbers to user names.
-h: Hides the individual CPU state information for systems having multiple processors. Only the average CPU status will be displayed.
-n number: Show only number processes per screen. Note that this option is ignored if number is greater than the maximum number of processes that can be displayed per screen.
在top命令運(yùn)行時(shí),我們可用以下幾個(gè)快捷鍵來翻屏:
j: 向前翻;
k: 向后翻;
t: 回到第一頁(yè);
對(duì)結(jié)果的分析:
通過top命令,我們可以快速了解到目前系統(tǒng)的CPU資源使用情況,尤其是占用CPU資源最多的進(jìn)程是我們必須關(guān)注的對(duì)象。
我們通過RES(the current size of the process resident in memory)列可以知道每個(gè)進(jìn)程占用內(nèi)存的數(shù)量。
我們通過NICE列可以知道系統(tǒng)是否使用NICE值來調(diào)節(jié)該進(jìn)程的工作負(fù)載平衡。
利用uptime命令查看系統(tǒng)整體情況
uptime prints the current time, the length of time the system has been up, the number of users logged on to the system, and the average number of jobs in the run queue over the last 1, 5, and 15 minutes.
w is linked to uptime and prints the same output as uptime -w, displaying a summary of the current activity on the system.
它的語(yǔ)法為:
uptime [-hlsuw] [user]
w [-hlsuw] [user]
其中各選項(xiàng)的含義為:
-h: Suppress the first line and the heading line. This option should not be used with the -u option. This option assumes the use of the -w option to uptime.
-l: Use long output. This option assumes the use of the -w option to uptime.
-s: Use the short form. of output for displaying terminal information. The terminal name is abbreviated; the login time and CPU times are suppressed.
-u: Print only the first line describing the overall state of the system. This is the default for the uptime command.ormation for systems having multiple processors. Only the average CPU status will be displayed.
-w: Print a summary of the current activity on the system for each user. This is the default for the w command.
分析cpu使用情況的方法相關(guān)文章: