日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Linux 命令 top 学习总结

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 命令 top 学习总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文簡介
??? 概要: 學習總結 Linux 下的 top 命令
??? 版本: Debian 5(Lenny), top: procps version 3.2.7
??? 日期: 2010-11-17
??? 永久鏈接: http://sleepycat.org/linux/linuxcommand/top.html


I. 概述

學習總結 top 命令。主要學習自 man 手冊。

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

以下是詳細解釋。


II. 第一行, 系統信息.
top - 01:33:08 up 169 days,? 7:03, 16 users,? load average: 0.07, 0.02, 0.00

- 01:33:08
??? 當前時間 01:33:08

up 169 days,? 7:03
??? 系統總運行時間: 169天7小時3分鐘

16 users
??? 當前登陸的用戶數: 16

load average
??? 系統平均負載.
??? 分別說明上一分鐘、最后五分鐘以及最后十五分鐘的系統負載均值

??? 在多處理器系統中,負載均值是基于內核的數量決定的。
??? 以 100% 負載計算,1.00 表示單個處理器,而 2.00 則說明有兩個雙處理器,那么 4.00 就說明主機具有
四個處理器。

??? 也就是說,對雙核處理器,值為2.00則表示100%負載。

??? 關于 load average 的詳細解釋的英文出處及中文翻譯:
??? 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

點數字鍵1可以展開或合上多個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

各個參數值顯示的都是百分比。

us? -- User CPU time
??? CPU花在未調整優先級的(not niced)用戶進程的時間。
??? The? time? the? CPU? has spent running users’ processes that are not niced.

??? 注: nice 命令: 調整程序運行的優先級(范圍 -20 到 19,越大,優先級越高)

sy? --? System CPU time
??? CPU花在內核及其進程上的時間。
??? The time the CPU has spent running the kernel and its processes.

ni? --? Nice CPU time??? CPU花在調整過優先級的(niced)用戶進程的時間。
??? The time the CPU has spent running users’ proccess? that? have? been niced.

wa? --? iowait
??? 等待I/O結束的CPU時間。
??? Amount of time the CPU has been waiting for I/O to complete.

hi? --? Hardware IRQ
??? CPU花在硬件中斷的時間。
??? The amount of time the CPU has been servicing hardware interrupts.

si? --? Software Interrupts
??? CPU花在軟件中斷的時間。
??? 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. 內存信息
Mem:??? 126396k total,?? 123312k used,???? 3084k free,??? 40908k buffers
Swap:?? 369452k total,???? 5912k used,?? 363540k free,??? 18244k cached

顯示物理內存與交換內存使用情況。

先詳細了解下 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


系統內存信息(單位為Mb)詳解:


Mem(表示物理內存使用概況):

??????? Mem: total = 60, 表示總共物理內存為 60M (其實是 64M 的內存, 單位轉換造成誤差) (total = used + free, 即: 60 = 56 + 3; 因為省略了小數位, 可能存在誤差)

??????? Mem: used = 56, 表示總共已經使用了 56M (這個數值是從內核的角度來看的. 也就是說, 所有 buffers 和 cached 所占用的內存也都被當作已經使用. 其實這些部分還是可以分給應用程序的)

??????? Mem: free = 3, 表示目前空閑 3M. (同樣, 這個數值是從內核的角度來看的. 也就是說, 所有 buffers 和 cached 所占用的內存也都被當作已經使用)

??????? Mem: shared = 0, 表示應用程序的共享內存(這一塊還不是很理解)

??????? Mem: buffers = 2, 表示 buffers 緩沖使用了 2M (塊設備的讀寫緩沖區)

??????? Mem: cached = 35, 表示 cache 緩沖使用了 35M (文件系統的cache)


-/+ buffers/cache(表示如果將 buffers 和 cached 的內存看作可用的話, 內存使用情況):

??????? -/+ buffers/cache: used = 17, 表示不考慮 buffers 和 cached 的內存, 系統實際使用了 17 M. (56 - 3 - 35, 誤差來源于省略掉的小數位)

??????? -/+ buffers/cache: free = 42, 表示不考慮 buffers 和 cached 的內存, 系統實際空閑了 42 M. (3 + 3 + 35, 誤差來源于省略掉的小數位) (注: 這一組值很重要, 可以作為系統空閑情況的重要參考)


Swap(表示交換內存使用概況):

??????? "total/used/free" 分別表示 "總共/已使用/空閑" 狀況.


而 top 命令中的內存信息, 只是將 -/+ buffers/cache 省掉(其實我們可以自己算), 并將 cached 挪到了下一行而已.


V. 各列名稱解釋

PID
??? 進程ID(Process Id)

USER
??? 任務所有者(The effective user name of the task’s owner)

PR
??? 優先級(Priority)

NI
??? 優先值(Nice value)
??? 任務的優先值。正值表示高優先級,負值表示低優先級(范圍 -20 到 19)。值為零則表示不會調整任務分配
的優先級。
??? 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
??? 虛擬內存, 單位: Kb. [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.

RES
??? 物理內存, 單位: Kb. [Resident size (kb)]
??? 任務所占用的非交換物理內存。
??? The non-swapped physical memory a task has used.
??? 注: 這一組內存信息最重要, %MEM 所顯示的百分比就是由此列的值得出.

SHR
??? 共享內存, 單位: Kb. [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.


-------- 關于 VIRT/RES/SHR 的詳解區別, 請參考如下地址 -------------------

http://blog.csdn.net/dingpeng1978/article/details/6559232

VIRT:virtual memory usage

??? 1、進程“需要的”虛擬內存大小,包括進程使用的庫、代碼、數據等
??? 2、假如進程申請100m的內存,但實際只使用了10m,那么它會增長100m,而不是實際的使用量

RES:resident memory usage 常駐內存

??? 1、進程當前使用的內存大小,但不包括swap out
??? 2、包含其他進程的共享
??? 3、如果申請100m的內存,實際使用10m,它只增長10m,與VIRT相反
??? 4、關于庫占用內存的情況,它只統計加載的庫文件所占內存大小

SHR:shared memory

??? 1、除了自身進程的共享內存,也包括其他進程的共享內存
??? 2、雖然進程只使用了幾個共享庫的函數,但它包含了整個共享庫的大小
??? 3、計算某個進程所占的物理內存大小公式:RES – SHR
??? 4、swap out后,它將會降下來

DATA

??? 1、數據占用的內存。如果top沒有顯示,按f鍵可以顯示出來。
??? 2、真正的該程序要求的數據空間,是真正在運行中要使用的。

-----------------------------------------------------------------------


S
??? 進程狀態 [Process Status]
??? 狀態值所對應的意義如下:
??????? D = 不可中斷的休眠 [uninterruptible sleep]
??????? R = 運行 [running]
??????? S = 休眠 [sleeping]
??????? T = 回溯或停止 [traced or stopped]
??????? Z = 僵尸 [zombie]

%CPU
??? CPU 使用率 [CPU usage]

%MEM
??? 物理內存使用率 [Memory usage (RES)], 以 RES 列的值為標準.

TIME+
??? CPU Time, hundredths (精確到1/100秒,如11:29.46=11分鐘, 29.46秒)
??? 任務開啟后所占用的總的CPU時間。

COMMAND
??? 任務名稱


VI. 常用參數:

top -d 1
??? 每隔一秒刷新一次。(默認為3秒)
??? 在 top 運行過程中, 也可以通過輸入 d? 或 s 重新設置刷新值。
??? Delay time interval as:? -d ss.tt (seconds.tenths)


top -c
??? 對于COMMAND列,顯示包含路徑的命令行,而不只是程序名稱。
??? 如: 不使用 -c 顯示為 smbd, 加 -c 則為 /usr/sbin/smbd
???
??? 在 top 運行過程中可以通過輸入 c 切換此狀態。
??? 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
??? 累計模式
??? 累計模式開啟,則每個進程的CPU時間(TIME+),表示它及它的關閉的子進程所用的時間。
??? 在top命令運行時,可以通過輸入 S 切換累計模式狀態。
??? 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 運行時的交互命令:

??? M
??????? 按 %MEM 由高到低排列

??? N
??????? 按 PID 由高到低排列

??? P
??????? 按 %CPU 由高到低排列

??? T
??????? 按 TIME+ 由高到低排列


??? s
??????? 更改刷新頻率,輸入秒數。

??? d
??????? 同 s 一樣。

??? S
??????? 開/關累計模式

??? c
??????? 顯示/不顯示程序全路徑 [參看 top -c]
??

??? i
??????? 忽略閑置、僵死的進程

??? u
??????? 輸入用戶名,只顯示此用戶的進程

??? H
??????? 顯示/不顯示線程
??

??? >
??????? 向后翻頁

??? <
??????? 向前翻頁

??? q
??????? 退出
??

??? f
??????? 選擇要顯示的列。直接輸入小寫索引字母即可。點擊任意其他鍵返回(如回車)。

??????? 選中的會改為大寫并加*號。如下:
??????????? * A: PID??????? = Process Id

??????? 未選中的(即不顯示的), 狀態為小寫,如下:
??????????? a: PID??????? = Process Id


??? o
??????? 改變顯示列的順序,用法與上相同。

??????????? 輸入大寫索引字母,則會往前移動。
??????????? 輸入小寫索引字母,則會往后移動。

??? F
??????? 選擇排序的列。
??????? top 以此處選擇的列為準排序。

??? O
??????? 與 F 功能完全相同。

?

VIII. 應用

1. 如何用 top 只監視某個進程的狀態?

??? 目前只發現用 top -p PID 的方式, 監控某個PID的進程。

??? 所以,先用pstree -p 取得進程PID, 然后再監控。如:

??? # pstree -p | grep apache2
??? # top -p 2038,13734,13735

2. 如何通過 shell 腳本監控 top 命令并抓取某幾個進程的信息?
???
??? 在 shell 腳本中用如下一行即可.
???
??? top -b -n 2 | grep -E "mysql| apache2" >> log

??? 參數 -b 用于輸出 top 的信息, -n 2 表示兩次刷新. 可以加 -d 10 更改刷新間隔時間. 也可以去掉 -n 2, 表示一直不停取結果.
???

?

總結

以上是生活随笔為你收集整理的Linux 命令 top 学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。