-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathtracegraph.py
95 lines (67 loc) · 2.24 KB
/
tracegraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python3
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
# Given a trace event file, this tool generates a flame graph based on the event scopes present in the file
# The result of analysis is a .svg file which can be viewed in a browser
import sys
import svg
import json
class Node(svg.Node):
def __init__(self):
svg.Node.__init__(self)
self.caption = ""
self.description = ""
self.ticks = 0
def text(self):
return self.caption
def title(self):
return self.caption
def details(self, root):
return "{} ({:,} usec, {:.1%}); self: {:,} usec".format(self.description, self.width, self.width / root.width, self.ticks)
with open(sys.argv[1]) as f:
dump = f.read()
root = Node()
# Finish the file
if not dump.endswith("]"):
dump += "{}]"
data = json.loads(dump)
stacks = {}
for l in data:
if len(l) == 0:
continue
# Track stack of each thread, but aggregate values together
tid = l["tid"]
if not tid in stacks:
stacks[tid] = []
stack = stacks[tid]
if l["ph"] == 'B':
stack.append(l)
elif l["ph"] == 'E':
node = root
for e in stack:
caption = e["name"]
description = ''
if "args" in e:
for arg in e["args"]:
if len(description) != 0:
description += ", "
description += "{}: {}".format(arg, e["args"][arg])
child = node.child(caption + description)
child.caption = caption
child.description = description
node = child
begin = stack[-1]
ticks = l["ts"] - begin["ts"]
rawticks = ticks
# Flame graph requires ticks without children duration
if "childts" in begin:
ticks -= begin["childts"]
node.ticks += int(ticks)
stack.pop()
if len(stack):
parent = stack[-1]
if "childts" in parent:
parent["childts"] += rawticks
else:
parent["childts"] = rawticks
svg.layout(root, lambda n: n.ticks)
svg.display(root, "Flame Graph", "hot", flip = True)