Debian Codegraph @

contrib/catapipe.py

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright 2018 Google LLC.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 """Tool read primitive events from a pipe to produce a catapult trace.
8
9 Usage:
10 Terminal 1: $ catapipe.py /tmp/mypipe /tmp/trace.json
11 Terminal 2: $ HGCATAPULTSERVERPIPE=/tmp/mypipe hg root
12 <ctrl-c catapipe.py in Terminal 1>
13 $ catapult/tracing/bin/trace2html /tmp/trace.json # produce /tmp/trace.html
14 <open trace.html in your browser of choice; the WASD keys are very useful>
15 (catapult is located at https://github.com/catapult-project/catapult)
16
17 For now the event stream supports
18
19 START $SESSIONID ...
20
21 and
22
23 END $SESSIONID ...
24
25 events. Everything after the SESSIONID (which must not contain spaces)
26 is used as a label for the event. Events are timestamped as of when
27 they arrive in this process and are then used to produce catapult
28 traces that can be loaded in Chrome's about:tracing utility. It's
29 important that the event stream *into* this process stay simple,
30 because we have to emit it from the shell scripts produced by
31 run-tests.py.
32
33 Typically you'll want to place the path to the named pipe in the
34 HGCATAPULTSERVERPIPE environment variable, which both run-tests and hg
35 understand. To trace *only* run-tests, use HGTESTCATAPULTSERVERPIPE instead.
36 """
37
38 import argparse
39 import json
40 import os
41 import timeit
42
43 _TYPEMAP = {
44 'START': 'B',
45 'END': 'E',
46 'COUNTER': 'C',
47 }
48
49 _threadmap = {}
50
51 # Timeit already contains the whole logic about which timer to use based on
52 # Python version and OS
53 timer = timeit.default_timer
54
55
56 def main():
57 parser = argparse.ArgumentParser()
58 parser.add_argument(
59 'pipe',
60 type=str,
61 nargs=1,
62 help='Path of named pipe to create and listen on.',
63 )
64 parser.add_argument(
65 'output',
66 default='trace.json',
67 type=str,
68 nargs='?',
69 help='Path of json file to create where the traces ' 'will be stored.',
70 )
71 parser.add_argument(
72 '--debug',
73 default=False,
74 action='store_true',
75 help='Print useful debug messages',
76 )
77 args = parser.parse_args()
78 fn = args.pipe[0]
79 os.mkfifo(fn)
80 try:
81 with open(fn) as f, open(args.output, 'w') as out:
82 out.write('[\n')
83 start = timer()
84 while True:
85 ev = f.readline().strip()
86 if not ev:
87 continue
88 now = timer()
89 if args.debug:
90 print(ev)
91 verb, session, label = ev.split(' ', 2)
92 if session not in _threadmap:
93 _threadmap[session] = len(_threadmap)
94 if verb == 'COUNTER':
95 amount, label = label.split(' ', 1)
96 payload_args = {'value': int(amount)}
97 else:
98 payload_args = {}
99 pid = _threadmap[session]
100 ts_micros = (now - start) * 1000000
101 out.write(
102 json.dumps(
103 {
104 "name": label,
105 "cat": "misc",
106 "ph": _TYPEMAP[verb],
107 "ts": ts_micros,
108 "pid": pid,
109 "tid": 1,
110 "args": payload_args,
111 }
112 )
113 )
114 out.write(',\n')
115 finally:
116 os.unlink(fn)
117
118
119 if __name__ == '__main__':
120 main()