CLion:2019.3.6
源码管理平台:Macbook Pro 10.12.6
C语言源码程序:Makefile格式的linux系统的top命令源码。
top所在的系统:ubuntu 14.04
一、源码导入
linux的top命令采用的是procps-ng项目,可以通过Clion 导入Makefile格式的C语言源程序:procps项目 这篇博客将源码进行下载和导入。
二、RES 指标的源码分析
1、输入top命令后显示如下:
对于RES指标, 代表PID进程占用的物理内存,其中包括共享库内存,RES的数据是怎么计算得来的呢,接下来我们将通过查看源码来了解这个数据的源头。
2、top的源码分析
top的源码位于top.c文件中:
top.c通过task_show如下进行获取res的数值,
/*
* Build the information for a single task row and
* display the results or return them to the caller. */
static const char *task_show (const WIN_t *q, const int idx) {
... 省略....
#define pages2K(n) (unsigned long)( (n) << Pg2K_shft )
... 省略....
// we must begin a row with a possible window number in mind...
*(rp = rbuf) = '\0';
if (Rc.mode_altscr) rp = scat(rp, " ");
... 省略....
for (x = 0; x < q->maxpflgs; x++) {
const char *cp = NULL;
FLG_t i = q->procflgs[x];
... 省略....
switch (i) {
... 省略....
case EU_MEM:
cp = scale_pcnt((float)pages2K(p->resident) * 100 / kb_main_total, W, Jn);
... 省略....
case EU_NMA:
cp