| 1 | #!/usr/bin/env python2
 | 
| 2 | """
 | 
| 3 | cachegrind_to_tsv.py
 | 
| 4 | 
 | 
| 5 | Turn a set of stdout of valgrind --tool=cachegrind into a TSV file.
 | 
| 6 | """
 | 
| 7 | from __future__ import print_function
 | 
| 8 | 
 | 
| 9 | import collections
 | 
| 10 | import os
 | 
| 11 | import re
 | 
| 12 | import sys
 | 
| 13 | 
 | 
| 14 | EXTRACT_RE = re.compile(r'I[ ]*refs:[ ]*([\d,]+)')
 | 
| 15 | 
 | 
| 16 | 
 | 
| 17 | def main(argv):
 | 
| 18 |     header = None
 | 
| 19 | 
 | 
| 20 |     for path in argv[1:]:
 | 
| 21 |         filename = os.path.basename(path)
 | 
| 22 |         join_id, _ = os.path.splitext(filename)
 | 
| 23 | 
 | 
| 24 |         d = collections.OrderedDict()
 | 
| 25 | 
 | 
| 26 |         d['join_id'] = join_id
 | 
| 27 | 
 | 
| 28 |         with open(path) as f:
 | 
| 29 |             contents = f.read()
 | 
| 30 |         m = EXTRACT_RE.search(contents)
 | 
| 31 |         if not m:
 | 
| 32 |             raise RuntimeError('Expected I refs')
 | 
| 33 | 
 | 
| 34 |         irefs = m.group(1).replace(',', '')
 | 
| 35 | 
 | 
| 36 |         d['irefs'] = irefs
 | 
| 37 | 
 | 
| 38 |         if header is None:
 | 
| 39 |             header = d.keys()
 | 
| 40 |             print("\t".join(header))
 | 
| 41 |         else:
 | 
| 42 |             # Ensure the order
 | 
| 43 |             assert d.keys() == header
 | 
| 44 | 
 | 
| 45 |         row = d.values()
 | 
| 46 |         print("\t".join(row))
 | 
| 47 | 
 | 
| 48 | 
 | 
| 49 | if __name__ == '__main__':
 | 
| 50 |     try:
 | 
| 51 |         main(sys.argv)
 | 
| 52 |     except RuntimeError as e:
 | 
| 53 |         print('FATAL: %s' % e, file=sys.stderr)
 | 
| 54 |         sys.exit(1)
 |