分析cpu使用情況的方法
分析cpu使用情況的方法
大家都知道怎么使用電腦,但是你知道怎么分析cpu使用情況嗎?不知道的話跟著學習啦小編一起來學習了解怎么分析cpu使用情況。
怎么分析cpu使用情況
要分析系統(tǒng)的CPU資源是否夠的前提誰占用了CPU資源,占用了多少,時間多長。 下面是一些衡量CPU閑忙程度的經(jīng)用指標:
1)用戶使用CPU的情況
CPU運行常規(guī)用戶進程
CPU運行niced process
CPU運行實時進程
2)系統(tǒng)使用CPU的情況
用于系統(tǒng)調(diào)用
用于I/O管理:中斷和驅(qū)動
用于內(nèi)存管理:paging and swapping
用于進程管理:context switch and process start
3)WIO:由于進程等待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
運行進程隊列的長度,即處于可運行狀態(tài)的進程個數(shù)的大小,不過我們關心的是這些在等待CPU調(diào)度執(zhí)行時所花的時間;
9)平均負載(load average)
CPU資源成為系統(tǒng)性能的瓶頸的征兆
CPU就像人的大腦,完成各種交給它的任務。如果任務太多,CPU就要忙不過來,它的運行效率就要下降。就像人生病會有一典型癥狀一樣,當CPU資源 成為系統(tǒng)性能的瓶頸時,它也有一些典型的癥狀:
很慢的響應時間(slow response time)
CPU空閑時間為零(zero percent idle CPU)
過高的用戶占用CPU時間(high percent user CPU)
過高的系統(tǒng)占用CPU時間(high percent system CPU)
長時間的有很長的運行進程隊列(large run queue size sustained over time)
processes blocked on prority
必須注意的是,如果系統(tǒng)出現(xiàn)上面的這些癥狀并不能說一定是由于CPU資源不夠,事實,有些癥狀 的出現(xiàn)很可能是由于其他資源的不足而引起,如內(nèi)存不夠時,CPU會忙內(nèi)存管理的事,這時從表面上, CPU的利用是100%,甚至顯得不夠,如果據(jù)此就簡單地認為增加CPU就可以解決問題是大錯特錯了。
因此,還是那句話,必須用不同的工具、從不同的方面對系統(tǒng)進行分析后,才能做出結論,即使這樣,經(jīng)驗將 起到不可替代的作用。
哪些進程是占用CPU資源的大戶?
在操作系統(tǒng)中,并不是所有的進程都以同樣的方式使用CPU資源。通常情況下,有些進程需要比其他進程更多的CPU時間片才能順利地完成任務。 下面是一些典型的占用CPU資源的大戶:
進程創(chuàng)建(process creation)
終端字符進程(teminal character processes(MUX- and LAN-based)
計算密集型進程和實時進程
X-終端和X-服務器進程(X-terminals and X-servers)
利用SAR工具分析CPU的利用率
利用SAR進行CPU的利用率分析的命令形式:
#sar -u,這時數(shù)據(jù)是通過sa1在后臺定時生成;
#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;
對結果的分析
首先,我們看%idle列的值,如果為接近零,則再看對應%wio列的值,如果這列的大于7,則表明系統(tǒng)的磁盤或其他I/O可能有問題,需要進一步的分析:
用iostat命令分析各個磁盤的傳輸閑忙狀況,如#iostat -t 5 2,每隔5秒取樣一次,共取2次;
用sar -d命令分析各塊設備(磁盤、磁帶)活動情況;
用sar -b命令分析系統(tǒng)的緩存的活動情況;
用sar -w命令分析進程的deactivation/reactivation and switching activities of the system;
如果%idle列很小,而對應的%wio列的值也很小,這時,我們查看%usr列和%sys列的值。如果%usr列的值很大,說明有用戶進程占用很多CPU時間;如果%sys列的值很大,則說明系統(tǒng)管理方面花了很多時間。需要進一步的分析:
用GlancePlus對占用CPU時間最大的進程進行單獨分析,為什么它會占用如此多的CPU時間。
如果%sys列的值很大,可以用SAR -C命令對系統(tǒng)調(diào)用進行進一步分解,看這些系統(tǒng)調(diào)用主要是做些什么。同時,還必須分析是否有其他瓶頸,如paging也會引起%sys的值很大,這時,可以用sar -q查看系統(tǒng)的運行進程隊列長度,也可以用GlancePlus和vmstat查看內(nèi)存的使用情況;
利用SAR工具分析運行進程隊列長度
利用SAR進行運行進程隊列長度分析的命令形式:
#sar -q,這時數(shù)據(jù)是通過sa1在后臺定時生成;
#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.
對結果的分析:
這些數(shù)據(jù)越小越好。
如果runq-sz大于4,或者%swapocc大于5時,則表明系統(tǒng)的CPU或內(nèi)存可能有問題,需要進一步的分析:
用sar -u命令分析CPU的使用情況;
用sar -w命令分析進程的deactivation/reactivation and switching activities of the system;
也可以用GlancePlus;
利用SAR工具分析系統(tǒng)調(diào)用
利用SAR進行系統(tǒng)調(diào)用分析的命令形式:
#sar -c,這時數(shù)據(jù)是通過sa1在后臺定時生成;
#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.
對結果的分析:
如果scall/s列的值很大,那么這么多的系統(tǒng)調(diào)用的原因就必須仔細分析了。
我們可以查看fork/s和exec/s列的值,看看系統(tǒng)是否在創(chuàng)建大量新的進程。
利用time命令測試某個命令和程序的執(zhí)行效率
我們可以利用time命令來測試一個命令的執(zhí)行效率,語法為:
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.
當我們覺得某個進程的性能不好時,最簡單的方法就是利用time命令來查看一下進程執(zhí)行時它的 時間分布情況,然后再用其他工具進一步分析。
利用top命令查看最耗CPU資源的進程
我們可以利用top命令來查看最耗CPU資源的進程。top命令還會根據(jù)進程占用CPU資源的多少而動態(tài)改變。
它的語法為:
top [-s time] [-d count] [-q] [-u] [-h] [-n number]
其中各選項的含義為:
-s time: 屏幕刷新的時間間隔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命令運行時,我們可用以下幾個快捷鍵來翻屏:
j: 向前翻;
k: 向后翻;
t: 回到第一頁;
對結果的分析:
通過top命令,我們可以快速了解到目前系統(tǒng)的CPU資源使用情況,尤其是占用CPU資源最多的進程是我們必須關注的對象。
我們通過RES(the current size of the process resident in memory)列可以知道每個進程占用內(nèi)存的數(shù)量。
我們通過NICE列可以知道系統(tǒng)是否使用NICE值來調(diào)節(jié)該進程的工作負載平衡。
利用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.
它的語法為:
uptime [-hlsuw] [user]
w [-hlsuw] [user]
其中各選項的含義為:
-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使用情況的方法相關文章: