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
|
#!/usr/bin/env python3
'''
_/_/ _/
_/ _/ _/_/ _/ _/_/ _/_/_/ _/_/_/
_/_/_/_/ _/_/_/_/ _/_/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/ _/
_/ _/ _/_/_/ _/ _/_/_/ _/ _/_/_/
_/
_/_/
Copyright:
This program is free software: you can redistribute it
and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program. If not, see
<https://www.gnu.org/licenses/>.
Commentary:
Aergia is a sampling based profiler based off of SCALENE
by Emery Berger (https://github.com/plasma-umass/scalene).
It is not particularly informative, but unlike SCALENE
or other sampling-based profilers I could find, reports
the wall-time each asyncio await call spends idling.
The goal behind Aergia is to eventually have these features,
or similar, merged into SCALENE.
Code:
'''
from collections import defaultdict
from types import FrameType
from typing import cast, List, Optional, Tuple
import argparse
import asyncio
import selectors
import signal
import sys
import time
import threading
import traceback
orig_thread_join = threading.Thread.join
def thread_join_replacement(
self: threading.Thread, timeout: Optional[float] = None
) -> None:
'''
We replace threading.Thread.join with this method which always
periodically yields.
'''
start_time = time.perf_counter()
interval = sys.getswitchinterval()
while self.is_alive():
orig_thread_join(self, interval)
# If a timeout was specified, check to see if it's expired.
if timeout is not None:
end_time = time.perf_counter()
if end_time - start_time >= timeout:
return None
return None
threading.Thread.join = thread_join_replacement
class ReplacementEpollSelector(selectors.EpollSelector):
'''
Provides a replacement for selectors.PollSelector that
periodically wakes up to accept signals.
'''
def select(
self, timeout=None
) -> List[Tuple[selectors.SelectorKey, int]]:
start_time = time.perf_counter()
if not timeout or timeout < 0:
interval = sys.getswitchinterval()
else:
interval = min(timeout, sys.getswitchinterval())
while True:
selected = super().select(interval)
if selected or timeout == 0 or not timeout:
return selected
end_time = time.perf_counter()
if end_time - start_time >= timeout:
return []
selectors.DefaultSelector = ReplacementEpollSelector
class Aergia(object):
# a key-value pair where keys represent frame metadata (see
# Aergia.frame_to_string) and values represent number of times
# sampled.
samples = defaultdict(lambda: [0, 0])
# number of times samples have been collected
total_samples = 0
# the timestamp recorded last signal
last_signal_time = 0.0
signal_interval = 0
def __init__(self, signal_interval):
Aergia.signal_interval = signal_interval
@staticmethod
def start():
signal.signal(signal.SIGPROF,
Aergia.cpu_signal_handler)
signal.setitimer(signal.ITIMER_PROF,
Aergia.signal_interval,
Aergia.signal_interval)
Aergia.last_signal_time = Aergia.gettime()
def stop():
Aergia.disable_signals()
Aergia.print_samples()
@staticmethod
def print_samples():
'''Pretty-print profiling results.'''
if Aergia.total_samples > 0:
print("FILE\tFUNC\tPERC\tACTUAL+CALCULATED=SECONDS")
for key in Aergia.sort_samples(Aergia.samples):
Aergia.print_sample(key)
else:
print("No samples were gathered. If you are using concurrency, "
"this is likely a bug and you may run the profiler again.")
@staticmethod
def print_sample(key):
'''Pretty-print a single sample.'''
sig_intv = Aergia.signal_interval
value = Aergia.samples[key]
tot = Aergia.sum_sample(value)
print(f"{key} :\t{tot * 100 / Aergia.total_samples:.3f}%"
f"\t({value[0]:.3f} + {value[1]:.3f} ="
f" {tot*sig_intv:.6f} seconds)")
@staticmethod
def disable_signals():
signal.signal(signal.ITIMER_PROF, signal.SIG_IGN)
signal.setitimer(signal.ITIMER_PROF, 0)
@staticmethod
def cpu_signal_handler(sig, frame):
sig_intv = Aergia.signal_interval
elapsed_since_last_signal = Aergia.gettime() - \
Aergia.last_signal_time
c_time_norm = (elapsed_since_last_signal - sig_intv) / \
sig_intv
keys = Aergia.compute_frames_to_record()
for key in keys:
Aergia.samples[Aergia.frame_to_string(key)][0] += 1
Aergia.samples[Aergia.frame_to_string(key)][1] += c_time_norm
Aergia.total_samples += elapsed_since_last_signal / \
sig_intv
Aergia.last_signal_time = Aergia.gettime()
@staticmethod
def compute_frames_to_record():
'''Collects all stack frames that Aergia actually processes.'''
frames = Aergia.get_frames_from_loops(Aergia.get_event_loops())
# Process all the frames to remove ones we aren't going to track.
new_frames = []
for frame in frames:
if frame is None:
continue
fname = frame.f_code.co_filename
# Record samples only for files we care about.
if (len(fname)) == 0:
# 'eval/compile' gives no f_code.co_filename. We have
# to look back into the outer frame in order to check
# the co_filename.
fname = frame.f_back.f_code.co_filename
while not Aergia.should_trace(fname):
# Walk the stack backwards until we hit a frame that
# IS one we should trace (if there is one). i.e., if
# it's in the code being profiled, and it is just
# calling stuff deep in libraries.
if frame:
frame = cast(FrameType, frame.f_back)
else:
break
if frame:
fname = frame.f_code.co_filename
if frame:
new_frames.append(frame)
return new_frames
@staticmethod
def get_event_loops():
loops = []
for t in threading.enumerate():
frame = sys._current_frames().get(t.ident)
if not frame:
continue
# print(f'searching frame {frame}')
loop = Aergia.walk_back_until_loop(frame)
if loop:
# print(f'found loop {loop}')
loops.append(loop)
return loops
@staticmethod
def walk_back_until_loop(frame):
while frame:
loop = Aergia.find_loop_in_locals(frame.f_locals)
if loop:
return loop
frame = frame.f_back
return None
@staticmethod
def find_loop_in_locals(locals_dict):
'''Given a dictionary of local variables for a stack frame,
retrieves the asyncio loop object, if there is one.
This function should work on Windows, but this is dubious because
I don't have a system to test with.'''
for val in locals_dict.values():
if isinstance(val, asyncio.AbstractEventLoop):
return val
return None
@staticmethod
def get_frames_from_loops(loops):
'''Given LOOPS, returns a flat list of tasks.'''
return [
task for loop in loops
for task in Aergia.get_idle_task_frames(loop)
]
@staticmethod
def frame_to_string(frame):
'''Pretty-prints a frame as a function/file name and a line number.
Additionally used as a key for tallying lines.'''
co = frame.f_code
func_name = co.co_name
line_no = frame.f_lineno
filename = co.co_filename
return filename + ':' + str(line_no) + '\t' + func_name
@staticmethod
def should_trace(filename):
'''Returns FALSE if filename is uninteresting to the user.'''
# return True
# FIXME Assume GuixSD. Makes filtering easy
if '/gnu/store' in filename:
return False
if 'site-packages' in filename:
return False
if filename[0] == '<':
return False
if 'aergia' in filename:
return False
return True
@staticmethod
def sort_samples(sample_dict):
'''Returns SAMPLE_DICT in descending order by number of samples.'''
return {k: v for k, v in sorted(sample_dict.items(),
key=lambda item: Aergia.sum_sample(
item[1]),
reverse=True)}
@staticmethod
def get_idle_task_frames(loop):
'''Given an asyncio event loop, returns the list of idle task frames.
A task is considered 'idle' if it is not currently executing.'''
curr_task = asyncio.current_task(loop)
return [
task.get_coro().cr_frame
for task in asyncio.all_tasks(loop)
if task is not curr_task
]
@staticmethod
def sum_sample(sample):
return sample[0] + sample[1]
@staticmethod
def gettime():
'''returns the wallclock time'''
return time.time()
the_globals = {
'__name__': '__main__',
'__doc__': None,
'__package__': None,
'__loader__': globals()['__loader__'],
'__spec__': None,
'__annotations__': {},
'__builtins__': globals()['__builtins__'],
'__file__': None,
'__cached__': None,
}
def parse_arguments():
'''Parse CLI args'''
parser = argparse.ArgumentParser(
usage='%(prog)s [args] script [args]'
)
parser.add_argument('-i', '--interval',
help='The minimum amount of time inbetween \
samples in seconds.',
metavar='',
default=0.01)
parser.add_argument('script', help='A python script to run.')
parser.add_argument('s_args', nargs=argparse.REMAINDER,
help='python script args')
return parser.parse_args()
def main():
args = parse_arguments()
sys.argv = [args.script] + args.s_args
try:
with open(args.script, 'rb') as fp:
code = compile(fp.read(), args.script, "exec")
Aergia(args.interval).start()
exec(code, the_globals)
Aergia.stop()
except Exception:
traceback.print_exc()
if __name__ == "__main__":
main()
|