使用valgrind跟踪管道命令的内存使用情况

时间:2012-03-22 01:13:51

标签: memory valgrind fork massif top-command

我有几个进程正在运行我编写的工具,这些工具通过管道连接,我想用valgrind来衡量他们收集的内存使用情况。到目前为止,我尝试过类似的事情:

$ valgrind tool=massif trace-children=yes --peak-inaccuracy=0.5 --pages-as-heap=yes --massif-out-file=myProcesses".%p" myProcesses.script

myProcesses.script运行相当于我的工具foo两次,例如

foo | foo > /dev/null

Valgrind似乎没有像我期望的那样捕获收集的内存使用情况。如果我使用top来跟踪这个问题,我会(为了争论)在第一个foo上获得10%的内存使用量,然后在foo之前收集另外10%的内存使用率{ {1}}完成。这是我想要衡量的事情:两个过程的使用。 Valgrind会返回以下错误:

myProcesses.script

有没有办法收集我正在以管道方式使用的命令的内存使用数据(使用Massif: ms_main.c:1891 (ms_new_mem_brk): Assertion 'VG_IS_PAGE_ALIGNED(len)' failed. )?还是我可以使用类似的工具来精确自动化这些测量?

valgrind在轮询时返回的数字对我来说似乎是波动的,我正在寻求准确和可重复的测量。如果您有替代工具的建议,我也欢迎这些。

编辑 - 修正了top选项的拼写错误。

编辑2 - 出于某种原因,似乎选项valgrind给我们带来了我们正在测试的二进制文件的麻烦。你的例子运行正常。每次输入非内联函数时都会创建一个新页面(堆栈溢出 - 呵呵)。我们想要计算这些,但它们在我们测试的内存使用规模上相对较小。 (也许--pages-as-heapls中没有函数调用?)删除less有助于重新开始测试。感谢MrGomez的大力帮助。

2 个答案:

答案 0 :(得分:2)

使用勘误表中给出的正确valgrind版本,这似乎只适用于 Valgrind 3.6.1 。我的调用:

<me>@harley:/tmp/test$ /usr/local/bin/valgrind --tool=massif \
  --trace-children=yes --peak-inaccuracy=0.5 --pages-as-heap=yes \
  --massif-out-file=myProcesses".%p" ./testscript.sh
==21067== Massif, a heap profiler
==21067== Copyright (C) 2003-2010, and GNU GPL'd, by Nicholas Nethercote
==21067== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==21067== Command: ./testscript.sh
==21067==
==21068== Massif, a heap profiler
==21068== Copyright (C) 2003-2010, and GNU GPL'd, by Nicholas Nethercote
==21068== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==21068== Command: /bin/ls
==21068==
==21070== Massif, a heap profiler
==21070== Copyright (C) 2003-2010, and GNU GPL'd, by Nicholas Nethercote
==21070== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==21069== Massif, a heap profiler
==21069== Copyright (C) 2003-2010, and GNU GPL'd, by Nicholas Nethercote
==21069== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==21069== Command: /bin/sleep 5
==21069==
==21070== Command: /usr/bin/less
==21070==
==21068==
(END) ==21069==
==21070==
==21067==

我的测试脚本testscript.sh的内容:

ls | sleep 5 | less

稀疏--massif-out-file=myProcesses".%p"myProcesses.21055)生成的其中一个文件中的内容:

desc: --peak-inaccuracy=0.5 --pages-as-heap=yes --massif-out-file=myProcesses.%p
cmd: ./testscript.sh
time_unit: i
#-----------
snapshot=0
#-----------
time=0
mem_heap_B=110592
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=empty
#-----------
snapshot=1
#-----------
time=0
mem_heap_B=118784
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=empty
...
#-----------
snapshot=18
#-----------
time=108269
mem_heap_B=1708032
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=peak
n2: 1708032 (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
 n3: 1474560 0x4015E42: mmap (mmap.S:62)
  n1: 1425408 0x4005CAC: _dl_map_object_from_fd (dl-load.c:1209)
   n2: 1425408 0x4007109: _dl_map_object (dl-load.c:2250)
    n1: 1413120 0x400CEEA: openaux (dl-deps.c:65)
     n1: 1413120 0x400D834: _dl_catch_error (dl-error.c:178)
      n1: 1413120 0x400C1E0: _dl_map_object_deps (dl-deps.c:247)
       n1: 1413120 0x4002B59: dl_main (rtld.c:1780)
        n1: 1413120 0x40140C5: _dl_sysdep_start (dl-sysdep.c:243)
         n1: 1413120 0x4000C6B: _dl_start (rtld.c:333)
          n0: 1413120 0x4000855: ??? (in /lib/ld-2.11.1.so)
    n0: 12288 in 1 place, below massif's threshold (01.00%)
  n0: 28672 in 3 places, all below massif's threshold (01.00%)
  n1: 20480 0x4005E0C: _dl_map_object_from_fd (dl-load.c:1260)
   n1: 20480 0x4007109: _dl_map_object (dl-load.c:2250)
    n0: 20480 in 2 places, all below massif's threshold (01.00%)
 n0: 233472 0xFFFFFFFF: ???
#-----------
snapshot=19
#-----------
time=108269
mem_heap_B=1703936
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=empty
#-----------
snapshot=20
#-----------
time=200236
mem_heap_B=1839104
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=empty

Massif继续抱怨我的其余文件中的堆分配。请注意,这与您的错误非常相似。

我认为您的valgrind版本是在调试模式下构建的,导致断言触发。从源代码重建(我使用this,默认值为./configure)将解决此问题。

无论哪种方式,这都是seems to be expected with Massif

答案 1 :(得分:0)

某些程序允许您预加载libmemusage.so库并获取已记录的内存分配报告:

$ LD_PRELOAD=libmemusage.so less /etc/passwd

Memory usage summary: heap total: 36212, heap peak: 35011, stack peak: 15008
         total calls   total memory   failed calls
 malloc|         39           5985              0
realloc|          3             64              0  (nomove:2, dec:0, free:0)
 calloc|        238          30163              0
   free|         51          11546
Histogram for block sizes:
    0-15            128  45% ==================================================
   16-31             13   4% =====
   32-47            105  37% =========================================
   48-63              2  <1% 
   64-79              4   1% =
   80-95              5   1% =
   96-111             3   1% =
  112-127             3   1% =
  160-175             1  <1% 
  192-207             1  <1% 
  208-223             2  <1% 
  256-271             1  <1% 
  432-447             1  <1% 
  560-575             1  <1% 
  656-671             1  <1% 
  768-783             1  <1% 
  944-959             1  <1% 
 1024-1039            2  <1% 
 1328-1343            1  <1% 
 2128-2143            1  <1% 
 3312-3327            1  <1% 
 7952-7967            1  <1% 
 8240-8255            1  <1% 

虽然我必须承认它并不总是有效 - 例如LD_PRELOAD=libmemusage.so ls从不报告任何内容 - 我希望我知道允许它工作或不工作的条件。