-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathexample_trace_dump.py
More file actions
191 lines (158 loc) · 3.73 KB
/
example_trace_dump.py
File metadata and controls
191 lines (158 loc) · 3.73 KB
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
# This script is best run with pystats enabled to help visualize the shape of the traces.
# ./configure --enable-experimental-jit=interpreter -C --with-pydebug --enable-pystats
# The resulting images can be visualized by on linux as follows:
# $ cd folder_with_gv_files
# $ dot -Tsvg -Osvg *.gv
# $ firefox *.gv.svg
# type: ignore
import sys
import os.path
from types import FunctionType
# All functions declared in this module will be run to generate
# a .gv file of the executors, unless the name starts with an underscore.
def _gen(n):
for _ in range(n):
yield n
def gen_in_loop(n):
t = 0
for n in _gen(n):
t += n
return n
def short_loop(n):
t = 0
for _ in range(n):
t += 1
t += 1
t += 1
t += 1
t += 1
return t
exec(
"\n".join(
["def mid_loop(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" t += 1"] * 20
+ [" return t"]
),
globals(),
)
exec(
"\n".join(
["def long_loop(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" t += 1"] * 100
+ [" return t"]
),
globals(),
)
def _add(a, b):
return a + b
def short_loop_with_calls(n):
t = 0
for _ in range(n):
t = _add(t, 1)
t = _add(t, 1)
t = _add(t, 1)
t = _add(t, 1)
t = _add(t, 1)
return t
exec(
"\n".join(
["def mid_loop_with_calls(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" t = _add(t, 1)"] * 20
+ [" return t"]
),
globals(),
)
exec(
"\n".join(
["def long_loop_with_calls(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" t = _add(t, 1)"] * 100
+ [" return t"]
),
globals(),
)
def short_loop_with_side_exits(n):
t = 0
for i in range(n):
if t < 0:
break
t += 1
if t < 0:
break
t += 1
if t < 0:
break
t += 1
if t < 0:
break
t += 1
if t < 0:
break
t += 1
return t
exec(
"\n".join(
["def mid_loop_with_side_exits(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" if t < 0:", " break", " t += 1"] * 20
+ [" return t"]
),
globals(),
)
exec(
"\n".join(
["def long_loop_with_side_exits(n):"]
+ [" t = 0"]
+ [" for _ in range(n):"]
+ [" if t < 0:", " break", " t += 1"] * 100
+ [" return t"]
),
globals(),
)
def short_branchy_loop(n):
# Branches are correlated and exit 1 time in 4.
t = 0
for i in range(n):
# Start with a few operations to form a viable trace
t += 1
t += 1
t += 1
if not t & 6:
continue
t += 1
if not t & 12:
continue
t += 1
if not t & 24:
continue
t += 1
if not t & 48:
continue
t += 1
return t
def _run_and_dump(func, n, outdir):
sys._clear_internal_caches()
func(n)
sys._dump_tracelets(os.path.join(outdir, f"{func.__name__}.gv"))
def _main():
if len(sys.argv) < 2 or len(sys.argv) > 3:
print(f"Usage: {sys.argv[0] if sys.argv else " "} OUTDIR [loops]")
outdir = sys.argv[1]
n = int(sys.argv[2]) if len(sys.argv) > 2 else 5000
functions = [
func
for func in globals().values()
if isinstance(func, FunctionType) and not func.__name__.startswith("_")
]
for func in functions:
_run_and_dump(func, n, outdir)
if __name__ == "__main__":
_main()