linux系統top命令詳解

lurou發表於2012-06-11

DBA必須掌握的工具之一,linux系統環境下的監控命令 top

[@more@]

top命令是Linux下常用的效能分析工具,能夠實時顯示系統中各個程式的資源佔用狀況,類似於Windows的工作管理員。下面詳細介紹它的使用方法。

top - 01:06:48 up 1:22, 1 user, load average: 0.06, 0.60, 0.48
Tasks: 29 total, 1 running, 28 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3% us, 1.0% sy, 0.0% ni, 98.7% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 191272k total, 173656k used, 17616k free, 22052k buffers
Swap: 192772k total, 0k used, 192772k free, 123988k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1379 root 16 0 7976 2456 1980 S 0.7 1.3 0:11.03 sshd
14704 root 16 0 2128 980 796 R 0.7 0.5 0:02.72 top
1 root 16 0 1992 632 544 S 0.0 0.3 0:00.90 init
2 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0

統計資訊區前五行是系統整體的統計資訊。第一行是任務佇列資訊,同 uptime 命令的執行結果。其內容如下:

01:06:48 當前時間
up 1:22
系統執行時間,格式為時:
1 user
當前登入使用者數
load average: 0.06, 0.60, 0.48
系統負載,即任務佇列的平均長度。三個數值分別為 1分鐘、5分鐘、15分鐘前到現在的平均值。

第二、三行為程式和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

最後兩行為記憶體資訊。內容如下:

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

程式資訊區統計資訊區域的下方顯示了各個程式的詳細資訊。首先來認識一下各列的含義。

序號 列名 含義
a PID
程式id
b PPID
父程式id
c RUSER Real user name
d UID
程式所有者的使用者id
e USER
程式所有者的使用者名稱
f GROUP
程式所有者的組名
g TTY
啟動程式的終端名。不是從終端啟動的程式則顯示為 ?
h PR
優先順序
i NI nice
值。負值表示高優先順序,正值表示低優先順序
j P
最後使用的CPU,僅在多CPU環境下有意義
k %CPU
上次更新到現在的CPU時間佔用百分比
l TIME
程式使用的CPU時間總計,單位秒
m TIME+
程式使用的CPU時間總計,單位1/100
n %MEM
程式使用的實體記憶體百分比
o VIRT
程式使用的虛擬記憶體總量,單位kbVIRT=SWAP+RES
p SWAP
程式使用的虛擬記憶體中,被換出的大小,單位kb
q RES
程式使用的、未被換出的實體記憶體大小,單位kbRES=CODE+DATA
r CODE
可執行程式碼佔用的實體記憶體大小,單位kb
s DATA
可執行程式碼以外的部分(資料段+)佔用的實體記憶體大小,單位kb
t SHR
共享記憶體大小,單位kb
u nFLT
頁面錯誤次數
v nDRT
最後一次寫入到現在,被修改過的頁面數。
w S
程式狀態。
D=
不可中斷的睡眠狀態
R=
執行
S=
睡眠
T=
跟蹤/停止
Z=
殭屍程式
x COMMAND
命令名/命令列
y WCHAN
若該程式在睡眠,則顯示睡眠中的系統函式名
z Flags
任務標誌,參考 sched.h

預設情況下僅顯示比較重要的 PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 列。可以透過下面的快捷鍵來更改顯示內容。

更改顯示內容透過 f 鍵可以選擇顯示的內容。按 f 鍵之後會顯示列的列表,按 a-z 即可顯示或隱藏對應的列,最後按Enter鍵確定。

o 鍵可以改變列的顯示順序。按小寫的 a-z 可以將相應的列向右移動,而大寫的 A-Z 可以將相應的列向左移動。最後按Enter鍵確定。

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

命令使用

1 工具(命令)名稱
top
2
.工具(命令)作用顯示系統當前的程式和其他狀況; top是一個動態顯示過程,即可以透過使用者按鍵來不斷重新整理當前狀態.如果在前臺執行該命令,它將獨佔前臺,直到使用者終止該程式為止. 比較準確的說,top命令提供了實時的對系統處理器的狀態監視.它將顯示系統中CPU敏感的任務列表.該命令可以按CPU使用.記憶體使用和執行時間對任務進行排序;而且該命令的很多特性都可以透過互動式命令或者在個人定製檔案中進行設定.
3
.環境設定Linux下使用。
4
.使用方法
4
1使用格式
top [-] [d] [p] [q] [c] [C] [S] [s] [n]
4
2引數說明
d
指定每兩次螢幕資訊重新整理之間的時間間隔。當然使用者可以使用s互動命令來改變之。
p
透過指定監控程式ID來僅僅監控某個程式的狀態。
q
該選項將使top沒有任何延遲的進行重新整理。如果呼叫程式有超級使用者許可權,那麼top將以儘可能高的優先順序執行。
S
指定累計模式
s
使top命令在安全模式中執行。這將去除互動命令所帶來的潛在危險。
i
使top不顯示任何閒置或者僵死程式。
c
顯示整個命令列而不只是顯示命令名
4.3
其他  下面介紹在top命令執行過程中可以使用的一些互動命令。從使用角度來看,熟練的掌握這些命令比掌握選項還重要一些。這些命令都是單字母的,如果在命令列選項中使用了s選項,則可能其中一些命令會被遮蔽掉。  Ctrl+L 擦除並且重寫螢幕。  h或者? 顯示幫助畫面,給出一些簡短的命令總結說明。  k 終止一個程式。系統將提示使用者輸入需要終止的程式PID,以及需要傳送給該程式什麼樣的訊號。一般的終止程式可以使用15訊號;如果不能正常結束那就使用訊號9強制結束該程式。預設值是訊號15。在安全模式中此命令被遮蔽。  i 忽略閒置和僵死程式。這是一個開關式命令。  q 退出程式。  r 重新安排一個程式的優先順序別。系統提示使用者輸入需要改變的程式PID以及需要設定的程式優先順序值。輸入一個正值將使優先順序降低,反之則可以使該程式擁有更高的優先權。預設值是10  S 切換到累計模式。  s 改變兩次重新整理之間的延遲時間。系統將提示使用者輸入新的時間,單位為s。如果有小數,就換算成m s。輸入0值則系統將不斷重新整理,預設值是5 s。需要注意的是如果設定太小的時間,很可能會引起不斷重新整理,從而根本來不及看清顯示的情況,而且系統負載也會大大增加。  f或者F 從當前顯示中新增或者刪除專案。  o或者O 改變顯示專案的順序。  l 切換顯示平均負載和啟動時間資訊。  m 切換顯示記憶體資訊。  t 切換顯示程式和CPU狀態資訊。  c 切換顯示命令名稱和完整命令列。  M 根據駐留記憶體大小進行排序。  P 根據CPU使用百分比大小進行排序。  T 根據時間/累計時間進行排序。
W
將當前設定寫入~/.toprc檔案中。這是寫top配置檔案的推薦方法。

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 . Less formal documentation can also be found on the 'Fields select' and 'Order fields' screens.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26006637/viewspace-1058478/,如需轉載,請註明出處,否則將追究法律責任。

相關文章