top

感謝水龍同學貢獻此文,我就不客氣,收藏了。
top指令輸出的內容比較多,首先說一下偶認爲比較有用的過濾選項:

-Uxxx:只顯示用戶名爲xxx的進程
-S:切換是否顯示系統進程
-I:切換是否顯示空閒的進程
-b:進入非交互模式
-s:設置刷新時間
-m:設置顯示的數據,有cpu以及io兩種

top的輸出分爲2個部分,第一部分爲系統信息,比較重要的是CPU以及MEM兩項:
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
其中的nice表示被nice命令改變優先級的任務佔用的CPU時間百分比

Mem: 2210M Active, 629M Inact, 235M Wired, 118M Cache, 112M Buf, 63M Free
獲取方式爲:
GETSYSCTL("vfs.bufspace", bufspace);
GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);        /* number of pages active */
GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);        /* number of pages inactive */
GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]);            /* number of pages active */    
GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]);        /* number of pages on buffer cache queue */
GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);            /* number of pages free */    
GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);                /* swap pager pages paged in */
GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);            /* swap pager pages paged out */    

具體各種內存的含義
http://www.hudong.com/wiki/FreeBSD%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D

active是當前系統中在一定的時間內被持續使用的頁面,系統中有一個後臺核心進程專門掃描active隊列,當有發現好久沒有使用某個頁面時,內核就會把該頁面移動到inactive隊列。

inactive隊列,個人推測,即使使用其的進程已經退出,也不會把進程對應的Inact的內容放到Free裏面。直到 Free 不夠用了,纔會動用Inact裏面的內存。
當Inact裏面的值很大時,可以通過下面的小程序,不停分配內存,同時看到Inact的內容慢慢減少。
----------------
#include <stdio.h>
#include <unistd.h>

int main()
{
        while (1)
        {
                void *p = malloc(1024*1024*50);
                memset(p, 0, 1024*1024*50);
                usleep(1000*1000*1);
                printf("vvv\n");
        }

        return 0;
}
----------------

cache 隊列中的頁面是有些子系統釋放某些頁面,但是頁面中的數據依然有效,將來還有可能再次使用其中的數據的頁面,只是這些頁面已經是“乾淨的”,不用再存盤, 典型的是文件系統使用這種技術,用戶關閉文件,但是文件系統有自己的cache管理,不一定就立即關閉文件,而將數據留在內存中,以便下次使用時能迅速獲 得數據而不需要讀盤。

free的意思很明瞭,其中不包含數據也沒有誰再與其有聯繫。無論上述是哪種頁面,內核都可以隨時那來作其他用途, 只是使用的優先級別不一樣,在內核需要一個新頁時,free的頁面被最先考慮,但是當數值小於一定量時,就會從cache類型的隊列中找,cache中的 頁面被取出時,需要與原來的vm_object交互,或許要脫鉤,當然沒有申請free的頁面來得快,但使用cache還是很快的,如果cache隊列中 沒有了頁面,就會去找inactive隊列中的頁面, inactive中的頁面有可能是“Dirty”,在拿做其他用途時,不但要與原來的vm_object脫鉤,而且還要因爲是“Dirty”而要引起寫 盤,萬不得已時active隊列中的頁面就會被拿作他用,這是系統會非常繁忙了。

wired頁面, 意思是被掛起的,這些頁面是不在上面講的隊列中的,因此它不能被交換到硬盤,這些頁面多了會導致系統使用內存緊張,需要交換時可以活動的餘地就會減少,導 致系統性能下降。通常內核中的數據結構都使用wired頁面,避免在內核使用自己的數據時發生缺頁而導致不可預料的結果,通常是無法恢復故障而崩潰,負載 較重的系統,wired頁面會多一些。而除了內核使用的內存是wired以外,用戶進程則幾 乎全部使用上述四種隊列種的頁面,全是可以被丟棄而再重新恢復的那種類型。wired頁面還可以由用戶進程調用mlock系統調用鎖定頁面而引起的。


top輸出的第二部分爲各個進程的信息:
  PID    USERNAME    THR PRI NICE   SIZE       RES       STATE  C   TIME   WCPU  COMMAND
10368 mysql                12       20    0      56868K 25940K   kserel    0    3:25     0.00%  mysqld

PRI,爲進程優先級
SIZE,爲虛存大小
RES,爲實存大小
C,爲該進程運行的cpu編號
CPU,CPU佔用時間
WCPU,權重CPU佔用時間

CPU與WCPU的關係爲:
#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
        ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
其中pct爲CPU/100, (pp)->ki_swtime 爲當前進程swap in/out的時間,lopcpu爲根據kern.ccpu計算的一個常數
詳細的解釋還可參考:http://lopsa.org/node/1495
按方向鍵“右”可以切換CPU/WCPU,不過按上面文章說,其實WCPU是爲早期的bsd設計的,現代操作系統調度算法已經很先進了,WCPU其實反倒沒啥意義。是否如此,有待考證。



STATE爲進程當前的狀態
http://www.webhostingtalk.com/showthread.php?t=583672

select
Process is blocked in the select(2) syscall. Usually means it's waiting for external input (network, terminal, file updated, things like that). select on

ly supports waiting for something related to a file descriptor (e.g., socket, tty, vnode).

nanslp
Process is blocked in the nanosleep(2) syscall, which is an explicit request to pause for a certain amount of time. Hard to generalize this one. It should be infrequent, or it might be used by something implementing its own polling.

kserel
Process is blocked waiting for an event to trigger a KSE upcall. This isn't easy to explain in a few words, but you can read kse(2) for most of the relevant details. Usually means the process is waiting for external input (similar to select).
kse 對應到程序就是 pthread 啦。

RUN
Process is ready to run but has been suspended while another process is running.

pause
Process is blocked in the pause(2) or sigsuspend(2) syscall. It might be waiting for a signal, or you might see this if another thread is running and the main thread is waiting for all other threads to end.

lockf
Process is blocked waiting for a file lock to be released. Could be an flock(2) lock or an fcntl(2)/F_SETLK record lock.

kqread
Process is blocked in the kqueue(2) syscall. This is similar to select(2)--waiting for external input. kqueue is more efficient than select and supports several other events that can be waited on.

sbwait
Process is waiting for a socket buffer to be filled or emptied.
比如 read 一個 block-fd,此時沒有數據,就 block 住啦。
 
pipered
Process is waiting for data to arrive on a pipe ("rd" is short for read).
 

轉載:

http://kasicass.blog.163.com/blog/static/39561920099231255314/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章