目录

  • 参考资料
  • top命令详解
  • 使用/proc/self/status
  • 注入代码监控某段代码执行前后进程内存状态


top命令详解

top

PID        USER    PR  NI  VIRT  RES      SHR  S  %CPU  %MEM     TIME+ COMMAND

PID:进程id
USER:进程所有者
PR:进程的优先级,越小优先级越高
NI:nice值,负值表示高优先级,正值表示低优先级
VIRT:进程使用的虚拟内存,单位是kb
RES:进程使用的物理内存,单位kb
SHR:进程使用的共享内存,单位kb
S:进程状态(S表示休眠,R表示正在运行, Z表示僵死状态,N表示该进程优先值为负数,I表示空闲状态)
%CPU:进程占用的CPU使用率
%MEM:进程使用的物理内存和总内存的百分比
TIME+:进程使用的CPU时间总计,单位1/100秒
COMMAND:命令行

VIRT/RES/SHR含义
VIRT 意味着进程虚拟空间的大小, 是真实使用的内存,加上映射进程自己使用的内存(如, X server使用显卡内存), 加上映射磁盘文件使用的内存(主要是加载共享程序库文件), 加上与其他进程共享的内存. VIRT代表进程当前时刻有多少内存可以访问.
RES 意味驻留内存大小, 是当前进程真正占用物理内存的精确反映. (直接与%MEM列相对应.) RES始终要比VIRT小, 因为多数程序依赖C库文件.
SHR 表示VIRT里有多少其实是共享部分(库文件使用的内存). 关系到库文件里, 并不是整个的库文件会驻留. 如, 如果程序仅用到了库文件里的少数函数, 整个库文件会映射并被计算到VIRT和SHR里, 但只有库文件包含用到的函数那部分真正加载到内存并被计算到RES里.

top | grep your_process
PID        USER    PR  NI  VIRT  RES      SHR  S  %CPU  %MEM     TIME+ COMMAND 
1234567 xxx       20   0 5430600 824588  43876 S  60.7   0.4   0:20.11 your_process                                                                                                                                                                    
1234567 xxx       20   0 5496136 895504  43876 S 104.7   0.5   0:23.25 your_process                                                                                                                                                                    
1234567 xxx       20   0 5561672 967576  43876 S 104.7   0.5   0:26.40 your_process                                                                                                                                                                    
1234567 xxx       20   0 5627208   1.0g  43876 S 104.7   0.5   0:29.54 your_process                                                                                                                                                                    
1234567 xxx       20   0 5758280   1.1g  43876 S 104.7   0.6   0:32.69 your_process                                                                                                                                                                    
1234567 xxx       20   0 5823816   1.1g  43876 S 105.0   0.6   0:35.84 your_process                                                                                                                                                                    
1234567 xxx       20   0 5889352   1.2g  43876 S 104.7   0.6   0:38.98 your_process                                                                                                                                                                    
1234567 xxx       20   0 5954888   1.3g  43876 S 104.3   0.7   0:42.12 your_process                                                                                                                                                                    
1234567 xxx       20   0 6020424   1.3g  43876 S 105.0   0.7   0:45.27 your_process                                                                                                                                                                    
1234567 xxx       20   0 6085960   1.4g  43876 S 104.3   0.8   0:48.40 your_process                                                                                                                                                                    
1234567 xxx       20   0 6151496   1.5g  43876 S 104.3   0.8   0:51.54 your_process                                                                                                                                                                    
1234567 xxx       20   0 6217032   1.5g  43876 S 105.0   0.8   0:54.69 your_process                                                                                                                                                                    
1234567 xxx       20   0 6282280   1.6g  43876 S 104.3   0.9   0:57.82 your_process                                                                                                                                                                    
1234567 xxx       20   0 6413352   1.7g  43876 S 103.7   0.9   1:00.94 your_process                                                                                                                                                                    
1234567 xxx       20   0 6478888   1.8g  43876 S 104.3   0.9   1:04.07 your_process                                                                                                                                                                    
1234567 xxx       20   0 6544424   1.8g  43876 S 104.0   1.0   1:07.19 your_process                                                                                                                                                                    
1234567 xxx       20   0 6609960   1.9g  43876 S 103.7   1.0   1:10.31 your_process                                                                                                                                                                    
1234567 xxx       20   0 6675496   2.0g  43876 S 104.3   1.0   1:13.44 your_process                                                                                                                                                                    
1234567 xxx       20   0 6741032   2.0g  43876 S 104.3   1.1   1:16.58 your_process                                                                                                                                                                    
1234567 xxx       20   0 6806568   2.1g  43876 S 104.3   1.1   1:19.71 your_process                                                                                                                                                                    
1234567 xxx       20   0 6872104   2.2g  43876 S 104.7   1.2   1:22.85 your_process                                                                                                                                                                    
1234567 xxx       20   0 6937640   2.2g  43876 S 104.0   1.2   1:25.98 your_process                                                                                                                                                                    
1234567 xxx       20   0 7068712   2.3g  43876 S 104.0   1.2   1:29.10 your_process                                                                                                                                                                    
1234567 xxx       20   0 7446828   2.4g  43940 S  43.5   1.3   1:30.41 your_process

使用/proc/self/status

可以通过/proc/self/status获取当前进程的状态信息
这个目录比较特殊。 众所周知可以通过/proc/$pid/来获取指定进程的信息,如果某个进程想要获取本进程的信息,就可以通过进程的pid来访问/proc/$pid/目录。但是这个方法还需要获取进程pid,在fork、daemon等情况下pid还可能发生变化。为了更方便的获取本进程的信息,linux提供了/proc/self/目录,这个目录比较独特,内容等价于/proc/本进程pid/。
如下所示, /proc/self软链到了本进程pid的目录

[]$ ls -l /proc/self
lrwxrwxrwx 1 root root 0 Oct 19 16:18 /proc/self -> 4139459
[]$ ls -l /proc/self
lrwxrwxrwx 1 root root 0 Oct 19 16:18 /proc/self -> 4139536
[]$ ls -l /proc/self
lrwxrwxrwx 1 root root 0 Oct 19 16:18 /proc/self -> 4139537
[]$ ls -l /proc/self
lrwxrwxrwx 1 root root 0 Oct 19 16:18 /proc/self -> 4139538

注入代码监控某段代码执行前后进程内存状态

int parseLine(char *line) {
    // This assumes that a digit will be found and the line ends in " Kb".
    int i = strlen(line);
    const char *p = line;
    while (*p < '0' || *p > '9') p++;
    line[i - 3] = '\0';
    i = atoi(p);
    return i;
}

// 虚拟内存和物理内存,单位为kb
typedef struct {
    uint32_t virtualMem;
    uint32_t physicalMem;
} processMem_t;

processMem_t GetProcessMemory() {
    FILE *file = fopen("/proc/self/status", "r");
    char line[128];
    processMem_t processMem;

    while (fgets(line, 128, file) != NULL) {
        if (strncmp(line, "VmSize:", 7) == 0) {
            processMem.virtualMem = parseLine(line);
            break;
        }

        if (strncmp(line, "VmRSS:", 6) == 0) {
            processMem.physicalMem = parseLine(line);
            break;
        }
    }
    fclose(file);
    return processMem;
}
std::cout << "physicalMem before: "<< GetProcessMemory().physicalMem << std::endl;
  std::cout << "virtualMem before: "<< GetProcessMemory().virtualMem << std::endl;
  // 要监测的部分
  std::cout << "physicalMem before: "<< GetProcessMemory().physicalMem << std::endl;
  std::cout << "virtualMem before: "<< GetProcessMemory().virtualMem << std::endl;

打印日志:

physicalMem before: 3741185024
virtualMem before: 5684040
physicalMem after: 3741184304
virtualMem after: 7322152

top命令结果

top命令里的 RES top命令 virt res_top命令里的 RES

可见virtualMem与top命令的VIRT一致