当前位置:   article > 正文

在Linux上运行procmem和procrank_proc_mem

proc_mem

Android系统中提供了两个命令行工具procrank、procmem用于查看系统中的内存使用情况。procrank可以查看系统中所有进程的整体内存占用情况,并按照规则排序。而procmem可以针对某个特定的进程分析其堆、栈、共享库等内存占用情况。这两个工具对于我们分析内存相关问题非常有效。由于Android系统使用的是Linux内核,理论上这样的工具可以在Linux上运行。

编译

参考Android系统中procrank、procmem的Android.mk发现他们依赖于libpagemap库,需要将其一并移植到Linux系统中。
Makefile可以通过CMake自动生成,当然前提是需要安装cmake工具。

sudo apt-get install cmake
  • 1

参考Android.mk,可以写出如下的CMakeLists.txt.

cmake_minimum_required(VERSION 2.8)
PROJECT (mem_proc)
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/libpagemap/*.c")
add_definitions (
    -D_LARGEFILE64_SOURCE
    )
include_directories (${PROJECT_SOURCE_DIR}/libpagemap/include)
add_library(pagemap ${SOURCES})

add_executable(procmem  ${PROJECT_SOURCE_DIR}/procmem/procmem.c)
target_link_libraries(procmem pagemap)
add_executable(procrank  ${PROJECT_SOURCE_DIR}/procrank/procrank.c)
target_link_libraries(procrank pagemap)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

编译过程如下:
-从GitHub下载源码

david-virtual-machine% git clone https://github.com/sunao2002002/mem_proc.git
正克隆到 'mem_proc'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 22 (delta 4), reused 22 (delta 4), pack-reused 0
展开对象中: 100% (22/22), 完成.
检查连接... 完成。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

-运行cmake生成Makefile

david-virtual-machine% cd mem_proc
david-virtual-machine% ls
build.sh  CMakeLists.txt  libpagemap  procmem  procrank
david-virtual-machine% mkdir out
david-virtual-machine% cd out
david-virtual-machine% cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/david/github/mem_proc/out
david-virtual-machine% ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile
david-virtual-machine%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

-make生成可执行文件

david-virtual-machine% make
Scanning dependencies of target pagemap
[ 11%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_memusage.c.o
[ 22%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_process.c.o
[ 33%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_map.c.o
[ 44%] Building C object CMakeFiles/pagemap.dir/libpagemap/pm_kernel.c.o
[ 55%] Linking C static library libpagemap.a
[ 55%] Built target pagemap
Scanning dependencies of target procmem
[ 66%] Building C object CMakeFiles/procmem.dir/procmem/procmem.c.o
[ 77%] Linking C executable procmem
[ 77%] Built target procmem
Scanning dependencies of target procrank
[ 88%] Building C object CMakeFiles/procrank.dir/procrank/procrank.c.o
/home/david/github/mem_proc/procrank/procrank.c: In function ‘getprocname’:
/home/david/github/mem_proc/procrank/procrank.c:381:9: warning: implicit declaration of function ‘asprintf’ [-Wimplicit-function-declaration]
     if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
         ^
/home/david/github/mem_proc/procrank/procrank.c:408:56: warning: comparison between pointer and integer
         if (strncpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
                                                        ^
[100%] Linking C executable procrank
[100%] Built target procrank
david-virtual-machine% file procrank procmem
procrank: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=19cdcb1d4674c66a3ea62e3bc026f01773a43ce0, not stripped
procmem:  ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=20e20cce4fd40bc859eca4d91d298f4d5165c8ff, not stripped
david-virtual-machine%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

-也可以直接运行build.sh完成整个流程。

运行

procrank

procrank是按照内存占用情况对进程进行排序。因为它需要遍历/proc下的所有进程获取内存占用情况,所以在运行时候需要有root权限。可用排序的有VSS、RSS、PSS、USS。
VSS:Virtual Set Size,虚拟内存耗用内存,包括共享库的内存
RSS:Resident Set Size,实际使用物理内存,包括共享库
PSS:Proportional Set Size,实际使用的物理内存,共享库按比例分配
USS:Unique Set Size,进程独占的物理内存,不计算共享库,也可以理解为将进程杀死能释放出的内存

一般VSS >= RSS >= PSS >= USS

procrank默认是按照PSS降序排列

david-virtual-machine% sudo ./procrank -h
Usage: ./procrank [ -W ] [ -v | -r | -p | -u | -s | -h ]
    -v  Sort by VSS.
    -r  Sort by RSS.
    -p  Sort by PSS.
    -u  Sort by USS.
    -s  Sort by swap.
        (Default sort order is PSS.)
    -R  Reverse sort order (default is descending).
    -c  Only show cached (storage backed) pages
    -C  Only show non-cached (ram/swap backed) pages
    -k  Only show pages collapsed by KSM
    -w  Display statistics for working set only.
    -W  Reset working set of all processes.
    -h  Display this help screen.
david-virtual-machine%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

运行如下截图

david-virtual-machine% sudo ./procrank
  PID       Vss      Rss      Pss      Uss  cmdline
 1078   283472K   40276K   33492K   32176K  /usr/lib/xorg/Xorg
 1746   637444K   50136K   21732K   16172K  /usr/bin/python3
 1724   237872K   28724K   17579K   14408K  /usr/bin/python3
             ......
 1793     4504K    1688K     261K     244K  /bin/sh
  749     4396K    1760K     240K     220K  /usr/sbin/acpid
 3158    14868K    1788K     188K     164K  gnome-pty-helper
                           ------   ------  ------
                          340421K  270184K  TOTAL

RAM: 8157592K total, 6676160K free, 116188K buffers, 834428K cached, 12048K shmem, 148268K slab
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

procmem

procrank 给出了系统整体的内存占用情况,针对每一个进程具体分析就要使用procmem工具了。同样运行procmem需要root权限。
以上图1793号进程 /bin/sh为例,分析内存占用情况:

david-virtual-machine% sudo ./procmem 1793
    Vss      Rss      Pss      Uss     ShCl     ShDi     PrCl     PrDi  Name
-------  -------  -------  -------  -------  -------  -------  -------
   144K     144K     144K     144K       0K       0K     144K       0K  /bin/dash
     8K       8K       8K       8K       0K       0K       8K       0K  /bin/dash
     4K       4K       4K       4K       0K       0K       4K       0K  /bin/dash
     8K       4K       4K       4K       0K       0K       4K       0K
   132K       8K       8K       8K       0K       0K       8K       0K  [heap]
  1788K    1288K      16K       0K    1288K       0K       0K       0K  /lib/x86_64-linux-gnu/libc-2.23.so
  2048K       0K       0K       0K       0K       0K       0K       0K  /lib/x86_64-linux-gnu/libc-2.23.so
    16K      16K      16K      16K       0K       0K      16K       0K  /lib/x86_64-linux-gnu/libc-2.23.so
     8K       8K       8K       8K       0K       0K       8K       0K  /lib/x86_64-linux-gnu/libc-2.23.so
    16K       8K       8K       8K       0K       0K       8K       0K
   152K     152K       1K       0K     152K       0K       0K       0K  /lib/x86_64-linux-gnu/ld-2.23.so
    12K      12K      12K      12K       0K       0K      12K       0K
     8K       8K       8K       8K       0K       0K       8K       0K
     4K       4K       4K       4K       0K       0K       4K       0K  /lib/x86_64-linux-gnu/ld-2.23.so
     4K       4K       4K       4K       0K       0K       4K       0K  /lib/x86_64-linux-gnu/ld-2.23.so
     4K       4K       4K       4K       0K       0K       4K       0K
   132K      12K      12K      12K       0K       0K      12K       0K  [stack]
     8K       0K       0K       0K       0K       0K       0K       0K  [vvar]
     8K       4K       0K       0K       4K       0K       0K       0K  [vdso]
     0K       0K       0K       0K       0K       0K       0K       0K  [vsyscall]
-------  -------  -------  -------  -------  -------  -------  -------
  4504K    1688K     261K     244K    1444K       0K     244K       0K  TOTAL
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

procmem 给出了procrank中VSS、RSS、PSS、USS的每一部分组成,包括进程可执行程序本身、共享库、堆、栈的内存占用。这里的信息应该是从/proc/pid/maps文件中获取的.

david-virtual-machine% sudo cat /proc/1793/maps
559358022000-559358046000 r-xp 00000000 08:01 1835041                    /bin/dash
559358245000-559358247000 r--p 00023000 08:01 1835041                    /bin/dash
559358247000-559358248000 rw-p 00025000 08:01 1835041                    /bin/dash
559358248000-55935824a000 rw-p 00000000 00:00 0
559358920000-559358941000 rw-p 00000000 00:00 0                          [heap]
7fab1342c000-7fab135eb000 r-xp 00000000 08:01 7606546                    /lib/x86_64-linux-gnu/libc-2.23.so
7fab135eb000-7fab137eb000 ---p 001bf000 08:01 7606546                    /lib/x86_64-linux-gnu/libc-2.23.so
7fab137eb000-7fab137ef000 r--p 001bf000 08:01 7606546                    /lib/x86_64-linux-gnu/libc-2.23.so
7fab137ef000-7fab137f1000 rw-p 001c3000 08:01 7606546                    /lib/x86_64-linux-gnu/libc-2.23.so
7fab137f1000-7fab137f5000 rw-p 00000000 00:00 0
7fab137f5000-7fab1381b000 r-xp 00000000 08:01 7602273                    /lib/x86_64-linux-gnu/ld-2.23.so
7fab139fa000-7fab139fd000 rw-p 00000000 00:00 0
7fab13a18000-7fab13a1a000 rw-p 00000000 00:00 0
7fab13a1a000-7fab13a1b000 r--p 00025000 08:01 7602273                    /lib/x86_64-linux-gnu/ld-2.23.so
7fab13a1b000-7fab13a1c000 rw-p 00026000 08:01 7602273                    /lib/x86_64-linux-gnu/ld-2.23.so
7fab13a1c000-7fab13a1d000 rw-p 00000000 00:00 0
7ffe0a1f9000-7ffe0a21a000 rw-p 00000000 00:00 0                          [stack]
7ffe0a273000-7ffe0a275000 r--p 00000000 00:00 0                          [vvar]
7ffe0a275000-7ffe0a277000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号