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的内容都是相似的。