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