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