天天看點

Linux Top 指令解析 比較詳細

TOP是一個動态顯示過程,即可以通過使用者按鍵來不斷重新整理目前狀态.如果在前台執行該指令,它将獨占前台,直到使用者終止該程式為止.比較準确的說,top指令提供了實時的對系統處理器的狀态監視.它将顯示系統中CPU最“敏感”的任務清單.該指令可以按CPU使用.記憶體使用和執行時間對任務進行排序;而且該指令的很多特性都可以通過互動式指令或者在個人定制檔案中進行設定.

top - 12:38:33 up 50 days, 23:15,  7 users,  load average: 60.58, 61.14, 61.22

Tasks: 203 total,  60 running, 139 sleeping,   4 stopped,   0 zombie

Cpu(s)  : 27.0%us, 73.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   1939780k total,  1375280k used,   564500k free,   109680k buffers

Swap:  4401800k total,   497456k used,  3904344k free,   848712k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                

 4338 oracle    25   0  627m 209m 207m R    0 11.0 297:14.76 oracle                                

 4267 oracle    25   0  626m 144m 143m R    6  7.6  89:16.62 oracle                                

 3458 oracle    25   0  672m 133m 124m R    0  7.1   1283:08 oracle                                

 3478 oracle    25   0  672m 124m 115m R    0  6.6   1272:30 oracle                                

 3395 oracle    25   0  672m 122m 113m R    0  6.5   1270:03 oracle                                 

 3480 oracle    25   0  672m 122m 109m R    8  6.4   1274:13 oracle                                

 3399 oracle    25   0  672m 121m 110m R    0  6.4   1279:37 oracle                                

 4261 oracle    25   0  634m 100m  99m R    0  5.3  86:13.90 oracle                                

25737 oracle    25   0  632m  81m  74m R    0  4.3 272:35.42 oracle                                

 7072 oracle    25   0  626m  72m  71m R    0  3.8   6:35.68 oracle                                

16073 oracle    25   0  630m  68m  63m R    8  3.6 175:20.36 oracle                                

16140 oracle    25   0  630m  66m  60m R    0  3.5 175:13.42 oracle                                

16122 oracle    25   0  630m  66m  60m R    0  3.5 176:47.73 oracle                                

  786 oracle    25   0  627m  63m  63m R    0  3.4   1:54.93 oracle                                

 4271 oracle    25   0  627m  59m  58m R    8  3.1  86:09.64 oracle                                

 4273 oracle    25   0  627m  57m  56m R    8  3.0  84:38.20 oracle                                

22670 oracle    25   0  626m  50m  49m R    0  2.7  84:55.82 oracle     

一.  TOP前五行統計資訊

統計資訊區前五行是系統整體的統計資訊。

1. 第一行是任務隊列資訊

同 uptime  指令的執行結果:

[[email protected] ~]# uptime

 13:22:30 up 8 min,  4 users,  load average: 0.14, 0.38, 0.25

其内容如下:

12:38:33 目前時間
up 50days 系統運作時間,格式為時:分
1 user 目前登入使用者數
load average: 0.06, 0.60, 0.48 系統負載,即任務隊列的平均長度。 三個數值分别為  1分鐘、5分鐘、15分鐘前到現在的平均值。

2. 第二、三行為程序和CPU的資訊

當有多個CPU時,這些内容可能會超過兩行。内容如下:

Tasks: 29 total 程序總數
1 running 正在運作的程序數
28 sleeping 睡眠的程序數
0 stopped 停止的程序數
0 zombie 僵屍程序數
Cpu(s): 0.3% us 使用者空間占用CPU百分比
1.0% sy 核心空間占用CPU百分比
0.0% ni 使用者程序空間内改變過優先級的程序占用CPU百分比
98.7% id 空閑CPU百分比
0.0% wa 等待輸入輸出的CPU時間百分比
0.0% hi
0.0% si

3. 第四五行為記憶體資訊。

内容如下:

Mem: 191272k total 實體記憶體總量
173656k used 使用的實體記憶體總量
17616k free 空閑記憶體總量
22052k buffers 用作核心緩存的記憶體量
Swap: 192772k total 交換區總量
0k used 使用的交換區總量
192772k free 空閑交換區總量
123988k cached 緩沖的交換區總量。 記憶體中的内容被換出到交換區,而後又被換入到記憶體,但使用過的交換區尚未被覆寫, 該數值即為這些内容已存在于記憶體中的交換區的大小。相應的記憶體再次被換出時可不必再對交換區寫入。

二.  程序資訊

列名 含義
PID 程序id
PPID 父程序id
RUSER Real user name
UID 程序所有者的使用者id
USER 程序所有者的使用者名
GROUP 程序所有者的組名
TTY 啟動程序的終端名。不是從終端啟動的程序則顯示為 ?
PR 優先級
NI nice值。負值表示高優先級,正值表示低優先級
P 最後使用的CPU,僅在多CPU環境下有意義
%CPU 上次更新到現在的CPU時間占用百分比
TIME 程序使用的CPU時間總計,機關秒
TIME+ 程序使用的CPU時間總計,機關1/100秒
%MEM 程序使用的實體記憶體百分比
VIRT 程序使用的虛拟記憶體總量,機關kb。VIRT=SWAP+RES
SWAP 程序使用的虛拟記憶體中,被換出的大小,機關kb。
RES 程序使用的、未被換出的實體記憶體大小,機關kb。RES=CODE+DATA
CODE 可執行代碼占用的實體記憶體大小,機關kb
DATA 可執行代碼以外的部分(資料段+棧)占用的實體記憶體大小,機關kb
SHR 共享記憶體大小,機關kb
nFLT 頁面錯誤次數
nDRT 最後一次寫入到現在,被修改過的頁面數。
S

程序狀态。

            D=不可中斷的睡眠狀态

            R=運作

            S=睡眠

            T=跟蹤/停止

            Z=僵屍程序

COMMAND 指令名/指令行
WCHAN 若該程序在睡眠,則顯示睡眠中的系統函數名
Flags 任務标志,參考 sched.h

top 的man 指令解釋如下:

    Listed below are top's available fields.  They are always associated with the  letter  shown,  regardless  of the position you may have established for them with the 'o' (Order fields) interactive command.Any field is selectable as the sort field, and you control whether they are  sorted high-to-low  or  low-to-high.   For  additional  information on sort provisions see  topic 3c. TASK Area Commands.

a: PID  --  Process Id

       The task's unique process ID, which periodically wraps, though never  restarting at zero.

b: PPID  --  Parent Process Pid

       The process ID of a task's parent.

c: RUSER  --  Real User Name

       The real user name of the task's owner.

d: UID  --  User Id

       The effective user ID of the task's owner.

e: USER  --  User Name

       The effective user name of the task's owner.

f: GROUP  --  Group Name

       The effective group name of the task's owner.

g: TTY  --  Controlling Tty

       The  name of the controlling terminal.  This is usually the device (serial port, pty, etc.) from which the process was started, and which it uses  for  input  oroutput.   However,  a task need not be associated with a terminal, in which case you'll see '?' displayed.

h: PR  --  Priority

       The priority of the task.

i: NI  --  Nice value

       The nice value of the task.   A  negative  nice  value  means  higher  priority, whereas  a  positive nice value means lower priority.  Zero in this field simply means priority will not be adjusted in determining a task's dispatchability.

j: P  --  Last used CPU (SMP)

       A number representing the last used processor.  In a true SMP  environment  this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity  and  cause  more processes  to change CPUs more often (because of the extra demand for cpu time).

k: %CPU  --  CPU usage

       The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.  In a true SMP environment, if 'Irix mode' is Off, top will operate in 'Solaris mode' where a task's cpu usage will be divided by  the  total  number  of  CPUs.   You toggle 'Irix/Solaris' modes with the 'I' interactive command.

l: TIME  --  CPU Time

       Total CPU time the task has used since it started.  When  'Cumulative  mode'  is On,  each  process is listed with the cpu time that it and its dead children has used.  You toggle 'Cumulative mode' with 'S', which is a command-line option and an interactive command.  See the 'S' interactive command for additional information regarding this mode.

m: TIME+  --  CPU Time, hundredths

       The same as 'TIME', but reflecting more granularity through hundredths of a sec          ond.

n: %MEM  --  Memory usage (RES)

       A task's currently used share of available physical memory.

o: VIRT  --  Virtual Image (kb)

       The total amount of virtual memory used by the task.  It includes all code, data and shared libraries plus pages that have  been  swapped  out.  (Note:  you  can define  the STATSIZE=1 environment variable and the VIRT will be calculated from the /proc/#/state VmSize field.)

       VIRT = SWAP + RES.

p: SWAP  --  Swapped size (kb)

       The swapped out portion of a task's total virtual memory image.

q: RES  --  Resident size (kb)

       The non-swapped physical memory a task has used.

       RES = CODE + DATA.

r: CODE  --  Code size (kb)

       The amount of physical memory devoted to executable  code,  also  known  as  the'text resident set' size or TRS.

s: DATA  --  Data+Stack size (kb)

       The  amount of physical memory devoted to other than executable code, also known the 'data resident set' size or DRS.

t: SHR  --  Shared Mem size (kb)

       The amount of shared memory used by a task.   It  simply  reflects  memory  that could be potentially shared with other processes.

u: nFLT  --  Page Fault count

       The  number  of  major  page faults that have occurred for a task.  A page fault occurs when a process attempts to read from or write to a virtual page  that  is not  currently  present  in  its address space.  A major page fault is when disk access is involved in making that page available.

v: nDRT  --  Dirty Pages count

       The number of pages that have been modified since  they  were  last  written  to disk.   Dirty  pages  must  be written to disk before the corresponding physical memory location can be used for some other virtual page.

w: S  --  Process Status

       The status of the task which can be one of:

             'D' = uninterruptible sleep

             'R' = running

             'S' = sleeping

             'T' = traced or stopped

             'Z' = zombie

       Tasks shown as running should be more properly thought of as 'ready to run'  --their  task_struct is simply represented on the Linux run-queue.  Even without a true SMP machine, you may see numerous tasks in this state  depending  on  top's delay interval and nice value.

x: Command  --  Command line or Program name

       Display the command line used to start a task or the name of the associated program.  You toggle between command line and name with 'c', which is both  a  command-line option and an interactive command. When  you've  chosen  to display command lines, processes without a command line (like kernel threads) will be shown with only the program name  in  parentheses, as in this example:                ( mdrecoveryd ) Either  form  of  display is subject to potential truncation if it's too long to fit in this field's  current  width.   That  width  depends  upon  other  fields  selected, their order and the current screen width.

       Note: The 'Command' field/column is unique, in that it is not fixed-width.  When displayed, this column will be allocated all remaining screen width (up  to  the maximum  512  characters)  to  provide for the potential growth of program names into command lines.

y: WCHAN  --  Sleeping in Function

       Depending on the availability of the kernel link map ('System.map'), this  field will  show  the  name or the address of the kernel function in which the task is currently sleeping.  Running tasks will display a dash ('-') in this column.

       Note: By displaying this field, top's own working set will be increased by  over 700Kb.   Your  only  means of reducing that overhead will be to stop and restart          top.

z: Flags  --  Task Flags

       This column represents the task's current scheduling flags which  are  expressed in  hexadecimal  notation and with zeros suppressed.  These flags are officially documented in <linux/sched.h>.  Less formal documentation can also be  found  on the 'Fields select' and 'Order fields' screens.

       預設情況下僅顯示比較重要的  PID、USER、PR、NI、VIRT、RES、SHR、S、%CPU、%MEM、TIME+、COMMAND  列。

2.1 用快捷鍵更改顯示内容。

(1)更改顯示内容通過 f鍵可以選擇顯示的内容。

       按 f 鍵之後會顯示列的清單,按 a-z  即可顯示或隐藏對應的列,最後按Enter鍵确定。

(2)按o鍵可以改變列的顯示順序。

       按小寫的 a-z 可以将相應的列向右移動,而大寫的 A-Z  可以将相應的列向左移動。最後按Enter鍵确定。

       按大寫的 F 或 O 鍵,然後按 a-z 可以将程序按照相應的列進行排序。而大寫的  R 鍵可以将目前的排序倒轉。

       設定完按回車傳回界面。

三.  指令使用

詳細内容可以參考MAN 幫助文檔。這裡列舉部分内容:

指令格式:

top [-] [d] [p] [q] [c] [C] [S]    [n]

參數說明:

d:  指定每兩次螢幕資訊重新整理之間的時間間隔。當然使用者可以使用s互動指令來改變之。

p:  通過指定監控程序ID來僅僅監控某個程序的狀态。

q:該選項将使top沒有任何延遲的進行重新整理。如果調用程式有超級使用者權限,那麼top将以盡可能高的優先級運作。

S: 指定累計模式

s : 使top指令在安全模式中運作。這将去除互動指令所帶來的潛在危險。

i:  使top不顯示任何閑置或者僵死程序。

c:  顯示整個指令行而不隻是顯示指令名

在top指令的顯示視窗,我們還可以輸入以下字母,進行一些互動:

幫助文檔如下:

Help for Interactive Commands - procps version 3.2.7

Window 1:Def: Cumulative mode Off.  System: Delay 4.0 secs; Secure mode Off.

  Z,B       Global: 'Z' change color mappings; 'B' disable/enable bold

  l,t,m     Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info

  1,I       Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode

  f,o     . Fields/Columns: 'f' add or remove; 'o' change display order

  F or O  . Select sort field

  <,>     . Move sort field: '<' next col left; '>' next col right

  R,H     . Toggle: 'R' normal/reverse sort; 'H' show threads

  c,i,S   . Toggle: 'c' cmd name/line; 'i' idle tasks; 'S' cumulative time

  x,y     . Toggle highlights: 'x' sort field; 'y' running tasks

  z,b     . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y')

  u       . Show specific user only

  n or #  . Set maximum tasks displayed

  k,r       Manipulate tasks: 'k' kill; 'r' renice

  d or s    Set update interval

  W         Write configuration file

  q         Quit

          ( commands shown with '.' require a visible task display window )

Press 'h' or '?' for help with Windows,

h或者?  : 顯示幫助畫面,給出一些簡短的指令總結說明。

k  :終止一個程序。系統将提示使用者輸入需要終止的程序PID,以及需要發送給該程序什麼樣的信号。一般的終止程序可以使用15信号;如果不能正常結束那就使用信号9強制結束該程序。預設值是信号15。在安全模式中此指令被屏蔽。

i:忽略閑置和僵死程序。這是一個開關式指令。

q:  退出程式。

r:  重新安排一個程序的優先級别。系統提示使用者輸入需要改變的程序PID以及需要設定的程序優先級值。輸入一個正值将使優先級降低,反之則可以使該程序擁有更高的優先權。預設值是10。

S:切換到累計模式。

s :  改變兩次重新整理之間的延遲時間。系統将提示使用者輸入新的時間,機關為s。如果有小數,就換算成ms。輸入0值則系統将不斷重新整理,預設值是5 s。需要注意的是如果設定太小的時間,很可能會引起不斷重新整理,進而根本來不及看清顯示的情況,而且系統負載也會大大增加。

f或者F :從目前顯示中添加或者删除項目。

o或者O  :改變顯示項目的順序。

l: 切換顯示平均負載和啟動時間資訊。即顯示影藏第一行

m: 切換顯示記憶體資訊。即顯示影藏記憶體行

t : 切換顯示程序和CPU狀态資訊。即顯示影藏CPU行

c:  切換顯示指令名稱和完整指令行。 顯示完整的指令。 這個功能很有用。

M : 根據駐留記憶體大小進行排序。

P:根據CPU使用百分比大小進行排序。

T: 根據時間/累計時間進行排序。

W:  将目前設定寫入~/.toprc檔案中。這是寫top配置檔案的推薦方法。

繼續閱讀