一句話總結(jié)
通過(guò)strace命令跟蹤可知,經(jīng)常使用的ps、top、free等命令都是通過(guò)讀/proc目錄下的文件獲得數(shù)據(jù)然后加以處理和展示。/proc目錄是procfs文件系統(tǒng)的掛載點(diǎn),procfs作為內(nèi)核中數(shù)據(jù)結(jié)構(gòu)的訪問(wèn)接口,提供系統(tǒng)的運(yùn)行時(shí)信息。
strace跟蹤
free -m命令會(huì)讀文件/proc/meminfo,示例如下:
# strace free -m 2>&1 | grep "/proc/meminfo"
open("/proc/meminfo", O_RDONLY) = 4
lscpu命令會(huì)讀文件/proc/cpuinfo和/sys/devices/system/cpu/目錄下的文件。
ps和top命令會(huì)讀/proc/sys/kernel/pid_max和/proc/$pid/{stat, status, cmdline}等文件。
還有很多其它的命令在此不再贅述,感興趣的話用strace跟蹤一下命令的執(zhí)行即可。
/proc介紹
在此引用相關(guān)文檔的解釋和說(shuō)明,感興趣的同學(xué)可根據(jù)對(duì)應(yīng)文檔深入了解。
https://github.com/torvalds/linux/blob/master/Documentation/filesystems/proc.txt
The proc file system acts as an interface to internal data structures in the
kernel. It can be used to obtain information about the system and to change
certain kernel parameters at runtime (sysctl).
http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
You might wonder how you can see details of a process that has a file size of 0. It makes more sense if you think of it as a window into the kernel. The file doesn't actually contain any data; it just acts as a pointer to where the actual process information resides.
man proc
The proc file system is a pseudo-file system which is used as an interface to kernel data structures. It is commonly mounted at /proc. Most of it is read-only, but some files allow kernel variables to be changed.