tools/client/read_profile.py (2681B)
1 """Summarize a gameplay profile and export its detailed Chrome Trace timeline.""" 2 import collections 3 import json 4 import pathlib 5 import sys 6 7 path = pathlib.Path(sys.argv[1]) 8 events = [] 9 metrics = {} 10 functions = collections.Counter() 11 thread_functions = collections.defaultdict(collections.Counter) 12 phases = collections.Counter() 13 frames = 0 14 frame_ms = 0 15 stalls = 0 16 dropped = 0 17 builds = set() 18 19 # Summaries are weighted by their actual frame count, including loading frames. 20 with path.open() as source: 21 for line in source: 22 batch = json.loads(line) 23 builds.add(batch['build']) 24 frames += batch['frames']['count'] 25 frame_ms += batch['frames']['count'] * batch['frames']['mean'] 26 stalls += batch['frames']['over100'] 27 dropped += batch['dropped'] 28 for name, value in batch['metrics'].items(): 29 metric = metrics.setdefault(name, {'count': 0, 'ms': 0, 'max': 0}) 30 metric['count'] += value['count'] 31 metric['ms'] += value['ms'] 32 metric['max'] = max(metric['max'], value['max']) 33 for event in batch['traceEvents']: 34 if event['name'] == 'guest sample': 35 phase = event['args']['phase'] 36 phases[phase] += 1 37 address = event['args']['function'] 38 event['name'] = f'sub_{address:08X} (wall sample, phase {phase})' 39 # Exclude explicit pacing and I/O; other unmarked host waits can remain. 40 if phase in (0, 2): 41 functions[address] += 1 42 thread_functions[event['tid']][address] += 1 43 events.append(event) 44 45 print('Builds:', ', '.join(str(build) for build in builds)) 46 print(f'Frames: {frames}; mean interval: {frame_ms / frames if frames else 0:.2f} ms; ' 47 f'over 100 ms: {stalls}; dropped observations: {dropped}') 48 for name, metric in sorted(metrics.items(), key=lambda item: -item[1]['ms']): 49 print(f"{name}: {metric['count']} calls, {metric['ms']:.1f} ms total, {metric['max']:.1f} ms maximum") 50 print('Wall-time sample phases (0 guest/host, 1 RAF, 2 render, 3 file, 4 setup, 5 other):', dict(phases)) 51 print('Guest/render residency samples, NOT CPU self-time:') 52 for address, count in functions.most_common(20): 53 print(f' sub_{address:08X}: {count}') 54 for thread, samples in thread_functions.items(): 55 print(f'Thread {thread}, guest/render residency samples:') 56 for address, count in samples.most_common(10): 57 print(f' sub_{address:08X}: {count}') 58 if events: 59 output = path.with_suffix('.trace.json') 60 output.write_text(json.dumps({'traceEvents': events}, separators=(',', ':')) + '\n') 61 print('Timeline:', output)