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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#!/usr/bin/env python3
# build-aux/tent-graph - Take dbg_noncache=True dbg_nstatic=True stack.c on stdin, and produce a tent graph SVG on stdout
#
# Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
import ast
import re
import sys
class Block:
title: str
parent: "Block|None"
children: list["Block"]
nbytes: int
def __init__(self, *, title: str, nbytes: int, parent: "Block|None") -> None:
self.title = title
self.parent = parent
self.children = []
self.nbytes = nbytes
@property
def rows(self) -> int:
if not self.children:
return 1
return sum(c.rows for c in self.children)
@property
def sum_nbytes(self) -> int:
if not self.children:
return self.nbytes
return self.nbytes + max(c.sum_nbytes for c in self.children)
def prune(self) -> None:
tgt = self.sum_nbytes - self.nbytes
self.children = [c for c in self.children if c.sum_nbytes == tgt]
re_line = re.compile(
r"^//dbg-nstatic:(?P<indent>(?: -)*) QName\((?P<func>.*)\)\t(?P<size>[0-9]+)$"
)
def parse() -> list[Block]:
roots: list[Block] = []
stack: list[Block] = []
for line in sys.stdin:
m = re_line.fullmatch(line.strip())
if not m:
continue
depth = len(m.group("indent")) // 2
func = ast.literal_eval(m.group("func"))
size = int(m.group("size"), 10)
stack = stack[:depth]
block = Block(
title=func,
nbytes=size,
parent=stack[-1] if stack else None,
)
if block.parent:
block.parent.children.append(block)
else:
roots.append(block)
stack.append(block)
return roots
def render(roots: list[Block]) -> None:
total_nbytes = max(r.sum_nbytes for r in roots)
total_rows = sum(r.rows for r in roots)
img_w = 1920
img_h = 948
details_h = 16
text_yoff = 12
text_xoff = 3
main_h = img_h - details_h
nbyte_h = main_h / total_nbytes
row_w = img_w / total_rows
print(
f"""<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" width="{img_w}" height="{img_h}" onload="init(evt)" viewBox="0 0 {img_w} {img_h}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style type="text/css">
.func_g:hover {{ stroke:black; stroke-width:0.5; }}
.func_g rect {{ rx: 2px; ry: 2px; }}
rect#background {{ fill: #EEEEEE; }}
text {{ font-size: 12px; font-family: Verdana; fill: rgb(0,0,0); }}
</style>
<script type="text/ecmascript">
<![CDATA[
var details;
function init(evt) {{ details = document.getElementById("details").firstChild; }}
function s(info) {{ details.nodeValue = "Function: " + info; }}
function c() {{ details.nodeValue = ' '; }}
]]>
</script>
<rect id="background" x="0" y="0" width="{img_w}" height="{img_h}" />
<text text-anchor="" x="{text_xoff}" y="{img_h-details_h+text_yoff}" id="details"> </text>"""
)
min_nbytes = roots[0].nbytes
max_nbytes = 0
def visit(b: Block) -> None:
nonlocal min_nbytes
nonlocal max_nbytes
min_nbytes = min(min_nbytes, b.nbytes)
max_nbytes = max(max_nbytes, b.nbytes)
for c in b.children:
visit(c)
for r in roots:
visit(r)
def print_block(block: Block, nbyte: int, row: int) -> None:
nonlocal min_nbytes
nonlocal max_nbytes
if block.nbytes:
hue = 100 - int(
((block.nbytes - min_nbytes) / (max_nbytes - min_nbytes)) * 100
)
x = row * row_w
y = nbyte * nbyte_h
w = max(1, block.rows * row_w - 1)
h = block.nbytes * nbyte_h
title = f"{block.title} = {block.nbytes} / {block.sum_nbytes} bytes"
nonlocal main_h
print(f'<g class="func_g" onmouseover="s(\'{title}\')" onmouseout="c()">')
print(f"\t<title>{title}</title>")
print(
f'\t<rect x="{x}" y="{main_h-y-h}" width="{w}" height="{h}" fill="hsl({hue} 60% 60%)" />'
)
short_title = title.rsplit(":", 1)[-1]
if h > details_h and w > len(short_title) * 10:
print(
f'\t<text x="{x+text_xoff}" y="{main_h-y-h+text_yoff}">{short_title}</text>'
)
print("</g>")
def sort_key(c: Block) -> int:
return c.sum_nbytes
for c in sorted(block.children, key=sort_key, reverse=True):
print_block(c, nbyte + block.nbytes, row)
row += c.rows
row = 0
for r in roots:
print_block(r, 0, row)
row += r.rows
print("</svg>")
def main() -> None:
roots = parse()
# tgt = max(r.sum_nbytes for r in roots)
# roots = [r for r in roots if r.sum_nbytes == tgt]
render(roots)
if __name__ == "__main__":
main()
|