ps p 22763 -L -o pcpu,pid,tid,time,tname,cmd,pmem,rss --sort rss 按rss排序
ps p 26653 -L -o pcpu,tid |sort -k1 -r -n|less 按第1列pcpu排序
找到上述進程中,CPU利用率比較高的線程號TID(十進制數),此處為3046
命令:
ps p 3036 -L -o pcpu,pid,tid,time,tname,cmd
將獲取的線程號(十進制數)轉換成十六進制,此處為0xb46
命令:
printf "%x\n" 3046
查看進程PID為3036中
nid為0xb46的線程信息。
命令:
jstack -l 3036
ps - 按進程消耗內存多少排序
ps -eo rss,pmem,pcpu,vsize,args | sort -k 1 -r -n | less
解析一下:
ps 都知道,是linux,unix顯示進程信息的, -e 是顯示所有進程, -o是定制顯示信息的格式
rss: resident set size, 表示進程占用RAM(內存)的大小,單位是KB
pmem: %M, 占用內存的百分比
pcpu:%C,占用cpu的百分比
vsize: 表示進程占用的虛擬內存的大小,KB
args:進程名(command)
sort命令對ps結果進行排序
-k 1 :按第一個參數(此處為rss)進行排序
-r:逆序
-n:numeric,按數字來排序
ps是Linux下用來查看進程的各種信息的工具,通過ps命令我們可以非常清楚地掌握各個進程所使用的系統資源,及時發現那些資源消耗異常的進程。保障服務器系統的高可用性及穩定性。
系統環境:Debian GNU/Linux 7.7 (wheezy)
pathname:/bin/ps
package:procps
ps命令選項:
簡單進程選擇:
-A 選擇所有進程
-e 選擇所有進程,等同於-A
通過清單選擇進程(PROCESS SELECTION BY LIST)
-C cmdlist
通過進程的可執行文件的名字來選擇進程
輸出格式控制:
-l —— 長格式,-y選項和這個一起用時很有用
-o —— 自定義輸出格式,后接格式說明符,(格式說明符間用逗號隔開)
-y —— 不顯示flags,顯示rss來代替addr,這個選項只能和-l選項一起用
輸出修改器(OUTPUT MODIFIERS):
--no-headers —— 不打印頭部
--sort spec —— 指定排序次序,默認是按spec(說明符)升序排列(+spec:按spec升序排列;-spec,按spec降序排列)
標准格式說明符:
代碼 | 頭部 | 描述 |
%mem | %MEM | 進程占用物理內存的百分比 |
args | COMMAND | 輸出命令及其全部參數 |
cmd | CMD | alias args |
comm | COMMAND | 僅輸出可執行文件的名字 |
command | COMMAND | alias args |
etime | ELAPSED | 輸出進程的運行時間,時間格式如下:[[dd-]hh:]mm:ss(運行時間沒有超過一天則不顯示dd-,如果運行時間還不到一小時,則顯示格式為mm:ss) |
euser | EUSER | 輸出進程的執行者的用戶名(有時是顯示執行者的uid) |
lstart | STARTED | 輸出進程啟動時刻 |
pid | PID | 輸出進程的進程號 |
psr | PSR | 進程當前被分配的處理器 |
pmem | %MEM | alias %mem |
rss | RSS | 物理內存的使用大小(單位為KB) |
rssize | RSS | alias rss |
rsz | RSZ | alias rss |
s | S | 最小狀態顯示(只顯示一個字符) |
stat | STAT | 多字符狀態顯示(顯示更詳細的進程狀態信息) |
state | S | alias s |
uname | USER | alias euser |
user | USER | alias euser |
進程狀態代碼:
D 不可中斷睡眠
R 正在運行或可運行(或者位於運行隊列中)
S 可中斷睡眠
T 已終止
X 已死(should never be seen)
Z 已故進程,已終止但還未被其父進程回收
< 高優先級(對其它用戶不友好)
N 低優先級(對其它用戶友好)
L 頁面鎖定在內存
s 含有子進程(is a session leader)
l 多線程
+ 位於前台進程組中
root@godontop:~# ps -eo rss,pid,user,etime,lstart,stat,args --no-headers |sort -k 1 -nr
按第一個參數rss(物理內存)的使用情況從大到小排序
按RSS的大小升序排列
# ps -lyC php5-fpm --sort rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 0 18303 1 0 80 0 508 10460 - ? 00:00:03 php5-fpm
S 33 22675 18303 1 80 0 39040 18523 - ? 00:00:04 php5-fpm
S 33 22676 18303 0 80 0 42576 19404 - ? 00:00:03 php5-fpm
S 33 22435 18303 0 80 0 47904 20698 - ? 00:00:19 php5-fpm
備注:當-o選項與--no-headers一起使用時,--no-headers需放在格式說明符的后面
http://godontop.com/linux-ps-command-usage-69/
ps -eLo pid,tid,class,rtprio,ni,pri,psr,pcpu,pmem,stat,wchan:30,comm
-e 顯示所有進程
-L 是現實線程信息
-o 表示使用用戶定義格式打印信息
tid 表示線程id
pcpu 表示cpu使用率
pmem 參照%cpu
%cpu %CPU cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a
percentage. It will not add up to 100% unless you are lucky. (alias pcpu).
pmem 參照%mem
%mem %MEM ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. (alias pmem).
wchan:30 WCHAN name of the kernel function in which the process is sleeping, a "-" if the process is running, or a "*" if the process is multi-threaded and ps is not displaying threads.
程序狀態字段解釋:
D Uninterruptible sleep (usually IO) 不可中斷睡眠
R Running or runnable (on run queue) 正在執行或可執行,表示目前在運行隊列里面
S Interruptible sleep (waiting for an event to complete) 可中斷睡眠
T Stopped, either by a job control signal or because it is being traced.停止
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.僵屍進程
For BSD formats and when the stat keyword is used, additional characters may be displayed:附加字段
< high-priority (not nice to other users) 高優先級
N low-priority (nice to other users) 低優先級
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group
http://blog.chinaunix.net/uid-21706718-id-3341751.html