Linux 命令 top 学习总结
本文簡(jiǎn)介
??? 概要: 學(xué)習(xí)總結(jié) Linux 下的 top 命令
??? 版本: Debian 5(Lenny), top: procps version 3.2.7
??? 日期: 2010-11-17
??? 永久鏈接: http://sleepycat.org/linux/linuxcommand/top.html
I. 概述
學(xué)習(xí)總結(jié) top 命令。主要學(xué)習(xí)自 man 手冊(cè)。
Linux 下 top 命令:
# top
top - 00:34:31 up 169 days,? 6:05, 16 users,? load average: 0.01, 0.01, 0.00
Tasks:? 97 total,?? 1 running,? 96 sleeping,?? 0 stopped,?? 0 zombie
Cpu(s):? 0.0%us,? 0.2%sy,? 0.0%ni, 99.8%id,? 0.0%wa,? 0.0%hi,? 0.0%si,? 0.0%st
Mem:??? 126396k total,?? 123312k used,???? 3084k free,??? 40908k buffers
Swap:?? 369452k total,???? 5912k used,?? 363540k free,??? 18244k cached
?PID USER????? PR? NI? VIRT? RES? SHR S %CPU %MEM??? TIME+? COMMAND
1993 root????? 20?? 0? 8668 2584 2468 S??? 1? 2.0 131:31.81 nmbd
15774 hw??? 20?? 0? 2388 1116? 876 R??? 1? 0.9?? 0:00.08 top
1 root????? 20?? 0? 1980? 604? 520 S??? 0? 0.5?? 5:49.12 init
2 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:00.00 kthreadd
3 root????? RT? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:12.78 migration/0
4 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 1:57.97 ksoftirqd/0
5 root????? RT? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:00.00 watchdog/0
6 root????? RT? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:04.78 migration/1
7 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 2:29.45 ksoftirqd/1
8 root????? RT? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:00.10 watchdog/1
9 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0? 11:29.26 events/0
10 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0? 11:50.89 events/1
11 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 0:00.02 khelper
44 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 2:37.22 kblockd/0
45 root????? 15? -5???? 0??? 0??? 0 S??? 0? 0.0?? 1:51.64 kblockd/1
以下是詳細(xì)解釋。
II. 第一行, 系統(tǒng)信息.
top - 01:33:08 up 169 days,? 7:03, 16 users,? load average: 0.07, 0.02, 0.00
- 01:33:08
??? 當(dāng)前時(shí)間 01:33:08
up 169 days,? 7:03
??? 系統(tǒng)總運(yùn)行時(shí)間: 169天7小時(shí)3分鐘
16 users
??? 當(dāng)前登陸的用戶數(shù): 16
load average
??? 系統(tǒng)平均負(fù)載.
??? 分別說(shuō)明上一分鐘、最后五分鐘以及最后十五分鐘的系統(tǒng)負(fù)載均值
??? 在多處理器系統(tǒng)中,負(fù)載均值是基于內(nèi)核的數(shù)量決定的。
??? 以 100% 負(fù)載計(jì)算,1.00 表示單個(gè)處理器,而 2.00 則說(shuō)明有兩個(gè)雙處理器,那么 4.00 就說(shuō)明主機(jī)具有
四個(gè)處理器。
??? 也就是說(shuō),對(duì)雙核處理器,值為2.00則表示100%負(fù)載。
??? 關(guān)于 load average 的詳細(xì)解釋的英文出處及中文翻譯:
??? http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
??? http://doc.chinaunix.net/linux/201004/507455.shtml
III. 第二行, CPU信息.
Cpu(s):? 0.0%us,? 0.2%sy,? 0.0%ni, 99.8%id,? 0.0%wa,? 0.0%hi,? 0.0%si,? 0.0%st
點(diǎn)數(shù)字鍵1可以展開(kāi)或合上多個(gè)CPU, 如下:
??? Cpu0? :? 0.0%us,? 0.0%sy,? 0.0%ni,100.0%id,? 0.0%wa,? 0.0%hi,? 0.0%si,? 0.0%st
??? Cpu1? :? 0.0%us,? 0.3%sy,? 0.0%ni, 99.7%id,? 0.0%wa,? 0.0%hi,? 0.0%si,? 0.0%st
各個(gè)參數(shù)值顯示的都是百分比。
us? -- User CPU time
??? CPU花在未調(diào)整優(yōu)先級(jí)的(not niced)用戶進(jìn)程的時(shí)間。
??? The? time? the? CPU? has spent running users’ processes that are not niced.
??? 注: nice 命令: 調(diào)整程序運(yùn)行的優(yōu)先級(jí)(范圍 -20 到 19,越大,優(yōu)先級(jí)越高)
sy? --? System CPU time
??? CPU花在內(nèi)核及其進(jìn)程上的時(shí)間。
??? The time the CPU has spent running the kernel and its processes.
ni? --? Nice CPU time??? CPU花在調(diào)整過(guò)優(yōu)先級(jí)的(niced)用戶進(jìn)程的時(shí)間。
??? The time the CPU has spent running users’ proccess? that? have? been niced.
wa? --? iowait
??? 等待I/O結(jié)束的CPU時(shí)間。
??? Amount of time the CPU has been waiting for I/O to complete.
hi? --? Hardware IRQ
??? CPU花在硬件中斷的時(shí)間。
??? The amount of time the CPU has been servicing hardware interrupts.
si? --? Software Interrupts
??? CPU花在軟件中斷的時(shí)間。
??? The amount of time the CPU has been servicing software interrupts.
st? --? Steal Time
??? The amount of CPU ’stolen’ from this virtual machine by the hypervisor for other tasks (such
?as running another virtual machine).
IV. 內(nèi)存信息
Mem:??? 126396k total,?? 123312k used,???? 3084k free,??? 40908k buffers
Swap:?? 369452k total,???? 5912k used,?? 363540k free,??? 18244k cached
顯示物理內(nèi)存與交換內(nèi)存使用情況。
先詳細(xì)了解下 free 命令:
tester@debian01:~$ free -m
???????????? total?????? used?????? free???? shared??? buffers???? cached
Mem:??????????? 60???????? 56????????? 3????????? 0????????? 3???????? 35
-/+ buffers/cache:???????? 17???????? 42
Swap:????????? 172????????? 0??????? 171
系統(tǒng)內(nèi)存信息(單位為Mb)詳解:
Mem(表示物理內(nèi)存使用概況):
??????? Mem: total = 60, 表示總共物理內(nèi)存為 60M (其實(shí)是 64M 的內(nèi)存, 單位轉(zhuǎn)換造成誤差) (total = used + free, 即: 60 = 56 + 3; 因?yàn)槭÷粤诵?shù)位, 可能存在誤差)
??????? Mem: used = 56, 表示總共已經(jīng)使用了 56M (這個(gè)數(shù)值是從內(nèi)核的角度來(lái)看的. 也就是說(shuō), 所有 buffers 和 cached 所占用的內(nèi)存也都被當(dāng)作已經(jīng)使用. 其實(shí)這些部分還是可以分給應(yīng)用程序的)
??????? Mem: free = 3, 表示目前空閑 3M. (同樣, 這個(gè)數(shù)值是從內(nèi)核的角度來(lái)看的. 也就是說(shuō), 所有 buffers 和 cached 所占用的內(nèi)存也都被當(dāng)作已經(jīng)使用)
??????? Mem: shared = 0, 表示應(yīng)用程序的共享內(nèi)存(這一塊還不是很理解)
??????? Mem: buffers = 2, 表示 buffers 緩沖使用了 2M (塊設(shè)備的讀寫(xiě)緩沖區(qū))
??????? Mem: cached = 35, 表示 cache 緩沖使用了 35M (文件系統(tǒng)的cache)
-/+ buffers/cache(表示如果將 buffers 和 cached 的內(nèi)存看作可用的話, 內(nèi)存使用情況):
??????? -/+ buffers/cache: used = 17, 表示不考慮 buffers 和 cached 的內(nèi)存, 系統(tǒng)實(shí)際使用了 17 M. (56 - 3 - 35, 誤差來(lái)源于省略掉的小數(shù)位)
??????? -/+ buffers/cache: free = 42, 表示不考慮 buffers 和 cached 的內(nèi)存, 系統(tǒng)實(shí)際空閑了 42 M. (3 + 3 + 35, 誤差來(lái)源于省略掉的小數(shù)位) (注: 這一組值很重要, 可以作為系統(tǒng)空閑情況的重要參考)
Swap(表示交換內(nèi)存使用概況):
??????? "total/used/free" 分別表示 "總共/已使用/空閑" 狀況.
而 top 命令中的內(nèi)存信息, 只是將 -/+ buffers/cache 省掉(其實(shí)我們可以自己算), 并將 cached 挪到了下一行而已.
V. 各列名稱解釋
PID
??? 進(jìn)程ID(Process Id)
USER
??? 任務(wù)所有者(The effective user name of the task’s owner)
PR
??? 優(yōu)先級(jí)(Priority)
NI
??? 優(yōu)先值(Nice value)
??? 任務(wù)的優(yōu)先值。正值表示高優(yōu)先級(jí),負(fù)值表示低優(yōu)先級(jí)(范圍 -20 到 19)。值為零則表示不會(huì)調(diào)整任務(wù)分配
的優(yōu)先級(jí)。
??? The nice value of the task.? A negative nice value means higher pri‐ority, 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.
VIRT
??? 虛擬內(nèi)存, 單位: Kb. [Virtual Image (kb)]
??? 任務(wù)所占用的虛擬內(nèi)存。包含了所有的代碼、數(shù)據(jù)、共享庫(kù)及交換頁(yè)面。
??? 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.
RES
??? 物理內(nèi)存, 單位: Kb. [Resident size (kb)]
??? 任務(wù)所占用的非交換物理內(nèi)存。
??? The non-swapped physical memory a task has used.
??? 注: 這一組內(nèi)存信息最重要, %MEM 所顯示的百分比就是由此列的值得出.
SHR
??? 共享內(nèi)存, 單位: Kb. [Shared Mem size (kb)]
??? 任務(wù)所占用的共享內(nèi)存。它反映了可以與其他進(jìn)程共享的內(nèi)存的大小。
??? The amount of shared memory used by a task.?
??? It simply reflects memory that could be potentially shared with other processes.
-------- 關(guān)于 VIRT/RES/SHR 的詳解區(qū)別, 請(qǐng)參考如下地址 -------------------
http://blog.csdn.net/dingpeng1978/article/details/6559232
VIRT:virtual memory usage
??? 1、進(jìn)程“需要的”虛擬內(nèi)存大小,包括進(jìn)程使用的庫(kù)、代碼、數(shù)據(jù)等
??? 2、假如進(jìn)程申請(qǐng)100m的內(nèi)存,但實(shí)際只使用了10m,那么它會(huì)增長(zhǎng)100m,而不是實(shí)際的使用量
RES:resident memory usage 常駐內(nèi)存
??? 1、進(jìn)程當(dāng)前使用的內(nèi)存大小,但不包括swap out
??? 2、包含其他進(jìn)程的共享
??? 3、如果申請(qǐng)100m的內(nèi)存,實(shí)際使用10m,它只增長(zhǎng)10m,與VIRT相反
??? 4、關(guān)于庫(kù)占用內(nèi)存的情況,它只統(tǒng)計(jì)加載的庫(kù)文件所占內(nèi)存大小
SHR:shared memory
??? 1、除了自身進(jìn)程的共享內(nèi)存,也包括其他進(jìn)程的共享內(nèi)存
??? 2、雖然進(jìn)程只使用了幾個(gè)共享庫(kù)的函數(shù),但它包含了整個(gè)共享庫(kù)的大小
??? 3、計(jì)算某個(gè)進(jìn)程所占的物理內(nèi)存大小公式:RES – SHR
??? 4、swap out后,它將會(huì)降下來(lái)
DATA
??? 1、數(shù)據(jù)占用的內(nèi)存。如果top沒(méi)有顯示,按f鍵可以顯示出來(lái)。
??? 2、真正的該程序要求的數(shù)據(jù)空間,是真正在運(yùn)行中要使用的。
-----------------------------------------------------------------------
S
??? 進(jìn)程狀態(tài) [Process Status]
??? 狀態(tài)值所對(duì)應(yīng)的意義如下:
??????? D = 不可中斷的休眠 [uninterruptible sleep]
??????? R = 運(yùn)行 [running]
??????? S = 休眠 [sleeping]
??????? T = 回溯或停止 [traced or stopped]
??????? Z = 僵尸 [zombie]
%CPU
??? CPU 使用率 [CPU usage]
%MEM
??? 物理內(nèi)存使用率 [Memory usage (RES)], 以 RES 列的值為標(biāo)準(zhǔn).
TIME+
??? CPU Time, hundredths (精確到1/100秒,如11:29.46=11分鐘, 29.46秒)
??? 任務(wù)開(kāi)啟后所占用的總的CPU時(shí)間。
COMMAND
??? 任務(wù)名稱
VI. 常用參數(shù):
top -d 1
??? 每隔一秒刷新一次。(默認(rèn)為3秒)
??? 在 top 運(yùn)行過(guò)程中, 也可以通過(guò)輸入 d? 或 s 重新設(shè)置刷新值。
??? Delay time interval as:? -d ss.tt (seconds.tenths)
top -c
??? 對(duì)于COMMAND列,顯示包含路徑的命令行,而不只是程序名稱。
??? 如: 不使用 -c 顯示為 smbd, 加 -c 則為 /usr/sbin/smbd
???
??? 在 top 運(yùn)行過(guò)程中可以通過(guò)輸入 c 切換此狀態(tài)。
??? Command line/Program name toggle
??? Starts top with the last remembered ’c’ state reversed.?
??? Thus,? if top was displaying command lines, now that field will show program names, and
visa versa.?
??? See the ’c’ interactive command for? additional information.
top -S
??? 累計(jì)模式
??? 累計(jì)模式開(kāi)啟,則每個(gè)進(jìn)程的CPU時(shí)間(TIME+),表示它及它的關(guān)閉的子進(jìn)程所用的時(shí)間。
??? 在top命令運(yùn)行時(shí),可以通過(guò)輸入 S 切換累計(jì)模式狀態(tài)。
??? Cumulative time mode toggle
??? Starts? top? with? the? last? remembered ’S’ state reversed.?
??? When’Cumulative mode’ is On, each process is listed with the cpu? time that? it and its dead
?children have used.?
??? See the ’S’ interactive command for additional information regarding this mode.
VII. 交互命令
top 運(yùn)行時(shí)的交互命令:
??? M
??????? 按 %MEM 由高到低排列
??? N
??????? 按 PID 由高到低排列
??? P
??????? 按 %CPU 由高到低排列
??? T
??????? 按 TIME+ 由高到低排列
??? s
??????? 更改刷新頻率,輸入秒數(shù)。
??? d
??????? 同 s 一樣。
??? S
??????? 開(kāi)/關(guān)累計(jì)模式
??? c
??????? 顯示/不顯示程序全路徑 [參看 top -c]
??
??? i
??????? 忽略閑置、僵死的進(jìn)程
??? u
??????? 輸入用戶名,只顯示此用戶的進(jìn)程
??? H
??????? 顯示/不顯示線程
??
??? >
??????? 向后翻頁(yè)
??? <
??????? 向前翻頁(yè)
??? q
??????? 退出
??
??? f
??????? 選擇要顯示的列。直接輸入小寫(xiě)索引字母即可。點(diǎn)擊任意其他鍵返回(如回車(chē))。
??????? 選中的會(huì)改為大寫(xiě)并加*號(hào)。如下:
??????????? * A: PID??????? = Process Id
??????? 未選中的(即不顯示的), 狀態(tài)為小寫(xiě),如下:
??????????? a: PID??????? = Process Id
??? o
??????? 改變顯示列的順序,用法與上相同。
??????????? 輸入大寫(xiě)索引字母,則會(huì)往前移動(dòng)。
??????????? 輸入小寫(xiě)索引字母,則會(huì)往后移動(dòng)。
??? F
??????? 選擇排序的列。
??????? top 以此處選擇的列為準(zhǔn)排序。
??? O
??????? 與 F 功能完全相同。
?
VIII. 應(yīng)用
1. 如何用 top 只監(jiān)視某個(gè)進(jìn)程的狀態(tài)?
??? 目前只發(fā)現(xiàn)用 top -p PID 的方式, 監(jiān)控某個(gè)PID的進(jìn)程。
??? 所以,先用pstree -p 取得進(jìn)程PID, 然后再監(jiān)控。如:
??? # pstree -p | grep apache2
??? # top -p 2038,13734,13735
2. 如何通過(guò) shell 腳本監(jiān)控 top 命令并抓取某幾個(gè)進(jìn)程的信息?
???
??? 在 shell 腳本中用如下一行即可.
???
??? top -b -n 2 | grep -E "mysql| apache2" >> log
??? 參數(shù) -b 用于輸出 top 的信息, -n 2 表示兩次刷新. 可以加 -d 10 更改刷新間隔時(shí)間. 也可以去掉 -n 2, 表示一直不停取結(jié)果.
???
?
總結(jié)
以上是生活随笔為你收集整理的Linux 命令 top 学习总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “虽异匏瓜难不食”下一句是什么
- 下一篇: F5负载均衡会话保持技术及原理技术白皮书