1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """
|
4 | virtual_memory.py
|
5 | """
|
6 |
|
7 | import os
|
8 | import sys
|
9 | import re
|
10 |
|
11 | # VmSize, VmData might be interesting too.
|
12 | METRIC_RE = re.compile('^(VmPeak|VmRSS):\s*(\d+)')
|
13 |
|
14 |
|
15 | class TsvWriter(object):
|
16 |
|
17 | def __init__(self, f):
|
18 | self.f = f
|
19 |
|
20 | def writerow(self, row):
|
21 | self.f.write('\t'.join(row) + '\n')
|
22 |
|
23 |
|
24 | def main(argv):
|
25 | action = argv[1]
|
26 |
|
27 | if action == 'baseline':
|
28 | input_dirs = argv[2:]
|
29 |
|
30 | out = TsvWriter(sys.stdout)
|
31 | out.writerow(('host', 'shell_name', 'shell_hash', 'metric_name',
|
32 | 'metric_value'))
|
33 |
|
34 | # Dir name looks like "$host.$job_id"
|
35 | for input_dir in input_dirs:
|
36 | d = os.path.basename(input_dir)
|
37 | host, job_id = d.split('.')
|
38 | for name in os.listdir(input_dir):
|
39 | n, _ = os.path.splitext(name)
|
40 | shell_name, shell_hash = n.split('-')
|
41 | path = os.path.join(input_dir, name)
|
42 | with open(path) as f:
|
43 | for line in f:
|
44 | m = METRIC_RE.match(line)
|
45 | if m:
|
46 | name, value = m.groups()
|
47 | row = (host, shell_name, shell_hash, name, value)
|
48 | out.writerow(row)
|
49 |
|
50 | else:
|
51 | raise RuntimeError('Invalid action %r' % action)
|
52 |
|
53 |
|
54 | if __name__ == '__main__':
|
55 | try:
|
56 | main(sys.argv)
|
57 | except RuntimeError as e:
|
58 | print('FATAL: %s' % e, file=sys.stderr)
|
59 | sys.exit(1)
|