-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtask_timing.py
381 lines (296 loc) · 10.1 KB
/
task_timing.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Package: utils
from __future__ import annotations
import asyncio
import contextlib
import inspect
import os
import sys
import time
from collections.abc import Iterator
from types import FrameType
from typing import Any
# This is a development utility that instruments tasks (coroutines) and records
# wall-clock time they spend in various functions. Since it relies on
# setprofile(), it cannot be combined with other profilers.
# to enable this instrumentation, set one of the environment variables:
# CHIA_INSTRUMENT_NODE=1
# CHIA_INSTRUMENT_WALLET=1
# Before starting the daemon.
# When exiting, profiles will be written to the `task-profile-<pid>` directory.
# To generate call trees, run:
# python chia/util/task_timing.py task-profile-<pid>
class FrameInfo:
call_timestamp: float
stack_pos: int
def __init__(self) -> None:
self.call_timestamp = 0.0
self.stack_pos = 0
class CallInfo:
duration: float
calls: int
def add(self, duration: float) -> None:
self.duration += duration
self.calls += 1
def __init__(self, duration: float) -> None:
self.duration = duration
self.calls = 1
class TaskInfo:
stack: dict[FrameType, FrameInfo]
stack_pos: int
def __init__(self) -> None:
self.stack = {}
self.stack_pos = 0
g_next_id: int = 0
class FunctionInfo:
name: str
file: str
num_calls: int
duration: float
callers: dict[str, CallInfo]
fun_id: int
def __init__(self, name: str, file: str) -> None:
global g_next_id
self.name = name
self.file = file
self.num_calls = 0
self.duration = 0.0
self.callers = {}
self.fun_id = g_next_id
g_next_id += 1
# maps tasks to call-treea
g_function_infos: dict[str, dict[str, FunctionInfo]] = {}
g_tasks: dict[asyncio.Task[Any], TaskInfo] = {}
g_cwd = os.getcwd() + "/"
# the frame object has the following members:
# clear
# f_back
# f_builtins
# f_code (type: "code")
# f_globals
# f_lasti
# f_lineno
# f_locals
# f_trace
# f_trace_lines
# f_trace_opcodes
# the code class has the following members:
# co_argcount
# co_cellvars
# co_code
# co_consts
# co_filename
# co_firstlineno
# co_flags
# co_freevars
# co_kwonlyargcount
# co_lnotab
# co_name
# co_names
# co_nlocals
# co_posonlyargcount
# co_stacksize
# co_varnames
# replace
# documented here: https://docs.python.org/3/library/inspect.html
def get_stack(frame: FrameType) -> str:
ret = ""
code = frame.f_code
while code.co_flags & inspect.CO_COROUTINE:
ret = f"/{code.co_name}{ret}"
if frame.f_back is None:
break
frame = frame.f_back
code = frame.f_code
return ret
def strip_filename(name: str) -> str:
if "/site-packages/" in name:
return name.split("/site-packages/", 1)[1]
if "/lib/" in name:
return name.split("/lib/", 1)[1]
if name.startswith(g_cwd):
return name[len(g_cwd) :]
return name
def get_fun(frame: FrameType) -> str:
code = frame.f_code
return f"{code.co_name}"
def get_file(frame: FrameType) -> str:
code = frame.f_code
return f"{strip_filename(code.co_filename)}:{code.co_firstlineno}"
def trace_fun(frame: FrameType, event: str, arg: Any) -> None:
if event in ["c_call", "c_return", "c_exception"]:
return
# we only care about instrumenting co-routines
if (frame.f_code.co_flags & inspect.CO_COROUTINE) == 0:
# with open("instrumentation.log", "a") as f:
# f.write(f"[1] {event} {get_fun(frame)}\n")
return
task = asyncio.current_task()
if task is None:
return
global g_tasks
global g_function_infos
ti = g_tasks.get(task)
if ti is None:
ti = TaskInfo()
g_tasks[task] = ti
# t = f"{task.get_name()}"
if event == "call":
fi = ti.stack.get(frame)
if fi is not None:
ti.stack_pos = fi.stack_pos
# with open("instrumentation.log", "a") as f:
# indent = " " * ti.stack_pos
# f.write(f"{indent}RESUME {t} {get_stack(frame)}\n")
else:
fi = FrameInfo()
fi.stack_pos = ti.stack_pos
fi.call_timestamp = time.perf_counter()
ti.stack[frame] = fi
ti.stack_pos += 1
# indent = " " * ti.stack_pos
# with open("instrumentation.log", "a") as f:
# f.write(f"{indent}CALL {t} {get_stack(frame)}\n")
elif event == "return":
fi = ti.stack.get(frame)
assert fi is not None
# indent = " " * (fi.stack_pos)
if asyncio.isfuture(arg):
# this means the function was suspended
# don't pop it from the stack
pass
# with open("instrumentation.log", "a") as f:
# f.write(f"{indent}SUSPEND {t} {get_stack(frame)}\n")
else:
# with open("instrumentation.log", "a") as f:
# f.write(f"{indent}RETURN {t} {get_stack(frame)}\n")
now = time.perf_counter()
duration = now - fi.call_timestamp
task_name = task.get_name()
fun_name = get_fun(frame)
fun_file = get_file(frame)
task_tree = g_function_infos.get(task_name)
if task_tree is None:
task_tree = {}
g_function_infos[task_name] = task_tree
fun_info = task_tree.get(fun_file)
if fun_info is None:
fun_info = FunctionInfo(fun_name, fun_file)
task_tree[fun_file] = fun_info
if frame.f_back is not None:
n = get_file(frame.f_back)
if n in fun_info.callers:
fun_info.callers[n].add(duration)
else:
fun_info.callers[n] = CallInfo(duration)
fun_info.num_calls += 1
fun_info.duration += duration
del ti.stack[frame]
ti.stack_pos = fi.stack_pos - 1
def start_task_instrumentation() -> None:
sys.setprofile(trace_fun)
def color(pct: float) -> str:
assert pct >= 0 and pct <= 100
return f"{int((100. - pct) // 10) + 1}"
def fontcolor(pct: float) -> str:
if pct > 80 or pct < 20:
return "white"
else:
return "black"
def stop_task_instrumentation(target_dir: str = f"task-profile-{os.getpid()}") -> None:
sys.setprofile(None)
global g_function_infos
try:
os.mkdir(target_dir)
except Exception:
pass
for task, call_tree in g_function_infos.items():
dot_file_name = f"{target_dir}/" + task + ".dot"
total_duration = 0.0
for name, fun_info in call_tree.items():
total_duration = max(total_duration, fun_info.duration)
if total_duration < 0.001:
continue
# ignore trivial call trees
if len(call_tree) <= 2:
continue
filter_frames = set()
with open(dot_file_name, "w") as f:
f.write(
"digraph {\n"
'node [fontsize=11, colorscheme=rdylgn10, style=filled, fontname="Arial"]\n'
'edge [fontsize=11, colorscheme=rdylgn10, fontname="Arial"]\n'
)
# print all nodes (functions)
for name, fun_info in call_tree.items():
# frames that are less than 0.1% of the total wall-clock time are
# filtered
if fun_info.duration / total_duration < 0.001:
filter_frames.add(name)
continue
percent = fun_info.duration * 100 / total_duration
f.write(
f'frame_{fun_info.fun_id} [shape=box, label="{fun_info.name}()\\l'
f"{fun_info.file}\\l"
f"{percent:0.2f}%\\n"
f"{fun_info.duration * 1000:0.2f}ms\\n"
f'{fun_info.num_calls}x\\n",'
f"fillcolor={color(percent)}, "
f"color={color(percent)}, "
f"fontcolor={fontcolor(percent)}]\n"
)
# print all edges (calls)
for name, fun_info in call_tree.items():
if name in filter_frames:
continue
for caller, ci in fun_info.callers.items():
caller_info = call_tree.get(caller)
if caller_info is None:
continue
if caller_info.file in filter_frames:
continue
percent = ci.duration * 100 / total_duration
# filter edges that are too insignificant
if percent < 0.01:
continue
f.write(
f"frame_{caller_info.fun_id} -> frame_{fun_info.fun_id} "
f'[label="{percent:0.2f}%\\n{ci.calls}x",'
f"penwidth={0.3 + (ci.duration * 6 / total_duration):0.2f},"
f"color={color(percent)}]\n"
)
f.write("}\n")
@contextlib.contextmanager
def manage_task_instrumentation() -> Iterator[None]:
start_task_instrumentation()
try:
yield
finally:
stop_task_instrumentation()
@contextlib.contextmanager
def maybe_manage_task_instrumentation(enable: bool) -> Iterator[None]:
if enable:
with manage_task_instrumentation():
yield
else:
yield
def main(args: list[str]) -> int:
import glob
import pathlib
import subprocess
profile_dir = pathlib.Path(args[0])
queue: list[subprocess.Popen[bytes]] = []
for file in glob.glob(str(profile_dir / "*.dot")):
print(file)
if os.path.exists(file + ".png"):
continue
if len(queue) > 15:
oldest = queue.pop(0)
oldest.wait()
with open(file + ".png", "w+") as f:
queue.append(subprocess.Popen(["dot", "-Tpng", file], stdout=f))
while len(queue) > 0:
oldest = queue.pop(0)
oldest.wait()
return 0
if __name__ == "__main__":
sys.exit(main(args=sys.argv[1:]))