valgrind massif.out分析器
msparser的Python项目详细描述
Valgrind massif.out文件的分析器
msparser模块提供了一个简单的接口来解析valgrind massif.out 文件格式,即valgrind heap profiler生成的数据文件,应该是 与Python2.5及更高版本兼容(包括3.x和pypy)
我怎么用?
导入模块
像往常一样,导入模块:
>>> import msparser
分析massif.out文件
要从massif.out文件中提取数据,只需将其路径指定给 parse_file函数:
>>> data = msparser.parse_file('massif.out')
您还可以将msparser.parse函数直接用于文件 描述符
了解数据
解析后的数据作为紧跟massif.out的字典返回。 格式。看起来是这样的:
>>> from pprint import pprint >>> pprint(data, depth=1) {'cmd': './a.out', 'desc': '--time-unit=ms', 'detailed_snapshots_index': [...], 'peak_snapshot_index': 16, 'snapshots': [...], 'time_unit': 'ms'}
detailed_snapshots_index和peak_snapshot_index字段允许 ^{tt5}中详细快照和峰值快照的高效本地化$ 列表。例如,要从snapshots列表中检索峰值快照, 我们可以做到:
>>> peak_index = data['peak_snapshot_index'] >>> peak_snapshot = data['snapshots'][peak_index]
snapshots列表存储表示每个快照数据的字典:
>>> second_snapshot = data['snapshots'][1] >>> pprint(second_snapshot) {'heap_tree': None, 'id': 1, 'mem_heap': 1000, 'mem_heap_extra': 8, 'mem_stack': 0, 'time': 183}
如果快照是详细的,heap_tree字段而不是无字段, 将存储堆树:
>>> peak_heap_tree = peak_snapshot['heap_tree'] >>> pprint(peak_heap_tree, depth=3) {'children': [{'children': [...], 'details': {...}, 'nbytes': 12000}, {'children': [], 'details': {...}, 'nbytes': 10000}, {'children': [...], 'details': {...}, 'nbytes': 8000}, {'children': [...], 'details': {...}, 'nbytes': 2000}], 'details': None, 'nbytes': 32000}
在根节点上,details字段始终为none,但在子节点上 nodes这是一本字典,看起来像这样:
>>> first_child = peak_snapshot['heap_tree']['children'][0] >>> pprint(first_child['details'], width=1) {'address': '0x8048404', 'file': 'prog.c', 'function': 'h', 'line': 4}
显然,如果节点低于massif阈值,那么details字段 不会的。
总而言之
从这个数据结构中,很容易编写一个过程来生成 准备好供Gnuplot使用的数据表:
print("# valgrind --tool=massif", data['desc'], data['cmd']) print("# id", "time", "heap", "extra", "total", "stack", sep='\t') for snapshot in data['snapshots']: id = snapshot['id'] time = snapshot['time'] heap = snapshot['mem_heap'] extra = snapshot['mem_heap_extra'] total = heap + extra stack = snapshot['mem_stack'] print(' '+str(id), time, heap, extra, total, stack, sep='\t')
输出应该如下:
# valgrind --tool=massif --time-unit=ms ./a.out # id time heap extra total stack 0 0 0 0 0 0 1 183 1000 8 1008 0 2 184 2000 16 2016 0 3 184 3000 24 3024 0 4 184 4000 32 4032 0 5 184 5000 40 5040 0 6 184 6000 48 6048 0 7 184 7000 56 7056 0 8 184 8000 64 8064 0 9 184 9000 72 9072 0
测试
运行msparser的测试套件:
$ python msparser_test.py --verbose
travis上的当前生成状态:http://travis-ci.org/#!/MathieuTurcotte/msparser
许可证
此代码可以根据MIT license的条款自由使用。