stackcollapse-chrome-tracing.py (3910B)
1 #!/usr/bin/python 2 # 3 # stackcolllapse-chrome-tracing.py collapse Trace Event Format [1] 4 # callstack events into single lines. 5 # 6 # [1] https://github.com/catapult-project/catapult/wiki/Trace-Event-Format 7 # 8 # USAGE: ./stackcollapse-chrome-tracing.py input_json [input_json...] > outfile 9 # 10 # Example input: 11 # 12 # {"traceEvents":[ 13 # {"pid":1,"tid":2,"ts":0,"ph":"X","name":"Foo","dur":50}, 14 # {"pid":1,"tid":2,"ts":10,"ph":"X","name":"Bar","dur":30} 15 # ]} 16 # 17 # Example output: 18 # 19 # Foo 20.0 20 # Foo;Bar 30.0 21 # 22 # Input may contain many stack trace events from many processes/threads. 23 # 24 # CDDL HEADER START 25 # 26 # The contents of this file are subject to the terms of the 27 # Common Development and Distribution License (the "License"). 28 # You may not use this file except in compliance with the License. 29 # 30 # You can obtain a copy of the license at docs/cddl1.txt or 31 # http://opensource.org/licenses/CDDL-1.0. 32 # See the License for the specific language governing permissions 33 # and limitations under the License. 34 # 35 # When distributing Covered Code, include this CDDL HEADER in each 36 # file and include the License file at docs/cddl1.txt. 37 # If applicable, add the following below this CDDL HEADER, with the 38 # fields enclosed by brackets "[]" replaced with your own identifying 39 # information: Portions Copyright [yyyy] [name of copyright owner] 40 # 41 # CDDL HEADER END 42 # 43 # 4-Jan-2018 Marcin Kolny Created this. 44 import argparse 45 import json 46 47 stack_identifiers = {} 48 49 50 class Event: 51 def __init__(self, label, timestamp, dur): 52 self.label = label 53 self.timestamp = timestamp 54 self.duration = dur 55 self.total_duration = dur 56 57 def get_stop_timestamp(self): 58 return self.timestamp + self.duration 59 60 61 def cantor_pairing(a, b): 62 s = a + b 63 return s * (s + 1) / 2 + b 64 65 66 def get_trace_events(trace_file, events_dict): 67 json_data = json.load(trace_file) 68 69 for entry in json_data['traceEvents']: 70 if entry['ph'] == 'X': 71 cantor_val = cantor_pairing(int(entry['tid']), int(entry['pid'])) 72 if 'dur' not in entry: 73 continue 74 if cantor_val not in events_dict: 75 events_dict[cantor_val] = [] 76 events_dict[cantor_val].append(Event(entry['name'], float(entry['ts']), float(entry['dur']))) 77 78 79 def load_events(trace_files): 80 events = {} 81 82 for trace_file in trace_files: 83 get_trace_events(trace_file, events) 84 85 for key in events: 86 events[key].sort(key=lambda x: x.timestamp) 87 88 return events 89 90 91 def save_stack(stack): 92 first = True 93 event = None 94 identifier = '' 95 96 for event in stack: 97 if first: 98 first = False 99 else: 100 identifier += ';' 101 identifier += event.label 102 103 if not event: 104 return 105 106 if identifier in stack_identifiers: 107 stack_identifiers[identifier] += event.total_duration 108 else: 109 stack_identifiers[identifier] = event.total_duration 110 111 112 def load_stack_identifiers(events): 113 event_stack = [] 114 115 for e in events: 116 if not event_stack: 117 event_stack.append(e) 118 else: 119 while event_stack and event_stack[-1].get_stop_timestamp() <= e.timestamp: 120 save_stack(event_stack) 121 event_stack.pop() 122 123 if event_stack: 124 event_stack[-1].total_duration -= e.duration 125 126 event_stack.append(e) 127 128 while event_stack: 129 save_stack(event_stack) 130 event_stack.pop() 131 132 133 parser = argparse.ArgumentParser() 134 parser.add_argument('input_file', nargs='+', 135 type=argparse.FileType('r'), 136 help='Chrome Tracing input files') 137 args = parser.parse_args() 138 139 all_events = load_events(args.input_file) 140 for tid_pid in all_events: 141 load_stack_identifiers(all_events[tid_pid]) 142 143 for identifiers, duration in stack_identifiers.items(): 144 print(identifiers + ' ' + str(duration))