Android系統基本都會內置ps命令,通過ps命令我們能簡單的瞭解相應進程的stat。
源碼在system/core/toolbox/ps.c
選項 | 解釋 |
---|---|
-t | 顯示所有線程的信息 |
-n | 顯示數字UID |
-Z | 顯示mac label,此選擇是和其他選項對立的 |
-P | 顯示進程策略,存在三種策略fg、bg、空白 |
-p | 對應的是prio, nice, rtprio, sched四種信息 |
-c | 顯示CPU佔用率 |
–abi | 顯示進程位寬 |
數字 | 篩選指定數字pid |
字符 | 篩選指定字符pid |
ps通過解析以下這些文件獲取相應的信息
"/proc/%d", pid
通過此獲得uid,默認顯示字符形式的UID、可以通過-n
顯示數字形式的UID
"/proc/%d/stat", pid
通過此讀取pid的stat,最終篩選出PPID、VSIZE、RSS、WCHAN、PC、NAME。
讀取出來的信息類似這樣的:
18017 (du.map.location) S 17394 17394 0 0 -1 4194624 2970 0 0 0 6 12 0 0 20 0 10 0 42960889 438394880 9400 4294967295 1073958912 1073963167 3204012848 3204011168 1074637424 0 4612 0 38120 4294967295 0 0 17 0 0 0 0 0 0
以下是ps對pid的stat的解析過程,statline是讀取出的全部信息,nexttok是讀取下一個值。
ptr = statline;
nexttok(&ptr); // skip pid
ptr++; // skip "("
name = ptr;
ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',
*ptr++ = '\0'; // and null-terminate name.
ptr++; // skip " "
state = nexttok(&ptr);
ppid = atoi(nexttok(&ptr));
nexttok(&ptr); // pgrp
nexttok(&ptr); // sid
nexttok(&ptr); // tty
nexttok(&ptr); // tpgid
nexttok(&ptr); // flags
nexttok(&ptr); // minflt
nexttok(&ptr); // cminflt
nexttok(&ptr); // majflt
nexttok(&ptr); // cmajflt
utime = atoi(nexttok(&ptr));
stime = atoi(nexttok(&ptr));
nexttok(&ptr); // cutime
nexttok(&ptr); // cstime
prio = atoi(nexttok(&ptr));
nice = atoi(nexttok(&ptr));
nexttok(&ptr); // threads
nexttok(&ptr); // itrealvalue
nexttok(&ptr); // starttime
vss = strtoul(nexttok(&ptr), 0, 10); // vsize
rss = strtoul(nexttok(&ptr), 0, 10); // rss
nexttok(&ptr); // rlim
nexttok(&ptr); // startcode
nexttok(&ptr); // endcode
nexttok(&ptr); // startstack
nexttok(&ptr); // kstkesp
eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip
nexttok(&ptr); // signal
nexttok(&ptr); // blocked
nexttok(&ptr); // sigignore
nexttok(&ptr); // sigcatch
wchan = strtoul(nexttok(&ptr), 0, 10); // wchan
nexttok(&ptr); // nswap
nexttok(&ptr); // cnswap
nexttok(&ptr); // exit signal
psr = atoi(nexttok(&ptr)); // processor
rtprio = atoi(nexttok(&ptr)); // rt_priority
sched = atoi(nexttok(&ptr)); // scheduling policy
nexttok(&ptr); // tty
"/proc/%d/attr/current", pid
MACLABEL的信息就是通過讀取此pid下的這個文件獲取到的,如果沒有內容默認賦值爲-
,此內容可以通過選項-Z
來顯示,此選項跟其他選項是對立存在的。
"/proc/%d/cmdline", pid
在顯示MACLABEL的時候,如果對應的pid下cmdline下有內容,那麼就用於替換NAME顯示輸出。
"/proc/%d/task/%d/stat", pid, tid
通過讀取pid下的task來顯示tid的信息,每個tid下的內容和pid的內容都是相似的。