Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
391 lines (339 loc) · 12.7 KB
/
Copy pathbenchmark.py
File metadata and controls
391 lines (339 loc) · 12.7 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
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
382
383
384
385
386
387
388
389
390
391
__author__ = "Manuel Holtgrewe"
__copyright__ = "Copyright 2017, Manuel Holtgrewe"
__email__ = "manuel.holtgrewe@bihealth.de"
__license__ = "MIT"
import contextlib
import datetime
from itertools import chain
import os
import sys
import time
import threading
import psutil
import GPUtil
from snakemake.exceptions import WorkflowError
#: Interval (in seconds) between measuring resource usage
BENCHMARK_INTERVAL = 30
#: Interval (in seconds) between measuring resource usage before
#: BENCHMARK_INTERVAL
BENCHMARK_INTERVAL_SHORT = 0.5
class BenchmarkRecord:
"""Record type for benchmark times"""
@classmethod
def get_header(klass):
return "\t".join(
(
"s",
"h:m:s",
"max_rss",
"max_vms",
"max_uss",
"max_pss",
"io_in",
"io_out",
"mean_load",
"max_gpu_load",
"max_gpu_mem",
)
)
def __init__(
self,
running_time=None,
max_rss=None,
max_vms=None,
max_uss=None,
max_pss=None,
io_in=None,
io_out=None,
cpu_seconds=None,
max_gpu_load=None,
max_gpu_mem=None,
rss=None,
vms=None,
uss=None,
pss=None,
gpu_load=None,
gpu_mem=None,
):
#: Running time in seconds
self.running_time = running_time or 0
#: Maximal RSS in MB
self.max_rss = max_rss
self.rss = rss
#: Maximal VMS in MB
self.max_vms = max_vms
self.vms = vms
#: Maximal USS in MB
self.max_uss = max_uss
self.uss = uss
#: Maximal PSS in MB
self.max_pss = max_pss
self.pss = pss
#: I/O read in bytes
self.io_in = io_in
#: I/O written in bytes
self.io_out = io_out
#: Count of CPU seconds, divide by running time to get mean load estimate
self.cpu_seconds = cpu_seconds or 0
#: First time when we measured CPU load, for estimating total running time
self.first_time = None
#: Previous point when measured CPU load, for estimating total running time
self.prev_time = None
self.max_gpu_load = max_gpu_load
self.gpu_load = gpu_load
self.max_gpu_mem = max_gpu_mem
self.gpu_mem = gpu_mem
def to_tsv(self, rt=False):
"""Return ``str`` with the TSV representation of this record"""
def to_tsv_str(x):
"""Conversion of value to str for TSV (None becomes "-")"""
if x is None:
return "-"
elif isinstance(x, float):
return "{:.2f}".format(x)
elif isinstance(x, list):
return ",".join(["{:.2f}".format(f) for f in x])
else:
return str(x)
def timedelta_to_str(x):
"""Conversion of timedelta to str without fractions of seconds"""
mm, ss = divmod(x.seconds, 60)
hh, mm = divmod(mm, 60)
s = "%d:%02d:%02d" % (hh, mm, ss)
if x.days:
def plural(n):
return n, abs(n) != 1 and "s" or ""
s = ("%d day%s, " % plural(x.days)) + s
return s
if rt:
return "\t".join(
map(
to_tsv_str,
(
"{:.4f}".format(self.running_time),
timedelta_to_str(datetime.timedelta(seconds=self.running_time)),
self.rss,
self.vms,
self.uss,
self.pss,
self.io_in,
self.io_out,
100.0 * self.cpu_seconds / self.running_time,
self.gpu_load,
self.gpu_mem,
),
)
)
else:
return "\t".join(
map(
to_tsv_str,
(
"{:.4f}".format(self.running_time),
timedelta_to_str(datetime.timedelta(seconds=self.running_time)),
self.max_rss,
self.max_vms,
self.max_uss,
self.max_pss,
self.io_in,
self.io_out,
100.0 * self.cpu_seconds / self.running_time,
self.max_gpu_load,
self.max_gpu_mem,
),
)
)
class DaemonTimer(threading.Thread):
"""Variant of threading.Timer that is deaemonized"""
def __init__(self, interval, function, args=None, kwargs=None):
threading.Thread.__init__(self, daemon=True)
self.interval = interval
self.function = function
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
self.finished = threading.Event()
def cancel(self):
"""Stop the timer if it hasn't finished yet."""
self.finished.set()
def run(self):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.set()
class ScheduledPeriodicTimer:
"""Scheduling of periodic events
Up to self._interval, schedule actions per second, above schedule events
in self._interval second gaps.
"""
def __init__(self, interval):
self._times_called = 0
self._interval = interval
self._timer = None
self._stopped = True
self._gpu = False
self._rtpath = None
self.start_time = None
def start(self):
"""Start the intervalic timer"""
self.start_time = time.time()
self.work()
self._times_called += 1
self._stopped = False
if self._times_called > self._interval:
self._timer = DaemonTimer(self._interval, self._action)
else:
self._timer = DaemonTimer(BENCHMARK_INTERVAL_SHORT, self._action)
self._timer.start()
def _action(self):
"""Internally, called by timer"""
self.work()
self._times_called += 1
if self._times_called > self._interval:
self._timer = DaemonTimer(self._interval, self._action)
else:
self._timer = DaemonTimer(BENCHMARK_INTERVAL_SHORT, self._action)
self._timer.start()
def work(self):
"""Override to perform the action"""
raise NotImplementedError("Override me!")
def cancel(self):
"""Call to cancel any events"""
self._timer.cancel()
self._stopped = True
class BenchmarkTimer(ScheduledPeriodicTimer):
"""Allows easy observation of a given PID for resource usage"""
def __init__(self, pid, bench_record, interval=BENCHMARK_INTERVAL, gpus=None, rt_path=None):
ScheduledPeriodicTimer.__init__(self, interval)
#: PID of observed process
self.pid = pid
self.main = psutil.Process(self.pid)
#: ``BenchmarkRecord`` to write results to
self.bench_record = bench_record
#: Cache of processes to keep track of cpu percent
self.procs = {}
if gpus:
self._gpu = True
self.gpus = gpus
self.bench_record.max_gpu_load = [-1] * len(gpus)
self.bench_record.max_gpu_mem = [-1] * len(gpus)
self.bench_record.gpu_load = [-1] * len(gpus)
self.bench_record.gpu_mem = [-1] * len(gpus)
if rt_path:
self._rtpath = rt_path
write_benchmark_records([], self._rtpath, head=True)
def work(self):
"""Write statistics"""
try:
self._update_record()
except psutil.NoSuchProcess:
pass # skip, process died in flight
except AttributeError:
pass # skip, process died in flight
if self._rtpath:
write_benchmark_records([self.bench_record], self._rtpath, head=False, mode='a', rt=True)
def _update_record(self):
"""Perform the actual measurement"""
# Memory measurements
rss, vms, uss, pss = 0, 0, 0, 0
# I/O measurements
io_in, io_out = 0, 0
check_io = True
# CPU seconds
cpu_seconds = 0
if self._gpu:
# GPU measurements
gpu_load, gpu_mem = 0, 0
# Iterate over process and all children
try:
this_time = time.time()
self.bench_record.running_time = this_time - self.start_time
for proc in chain((self.main,), self.main.children(recursive=True)):
proc = self.procs.setdefault(proc.pid, proc)
with proc.oneshot():
if self.bench_record.prev_time:
cpu_seconds += (
proc.cpu_percent()
/ 100
* (this_time - self.bench_record.prev_time)
)
meminfo = proc.memory_full_info()
rss += meminfo.rss
vms += meminfo.vms
uss += meminfo.uss
pss += meminfo.pss
if check_io:
try:
ioinfo = proc.io_counters()
io_in += ioinfo.read_bytes
io_out += ioinfo.write_bytes
except NotImplementedError as nie:
# OS doesn't track IO
check_io = False
self.bench_record.prev_time = this_time
if not self.bench_record.first_time:
self.bench_record.prev_time = this_time
rss /= 1024 * 1024
vms /= 1024 * 1024
uss /= 1024 * 1024
pss /= 1024 * 1024
if check_io:
io_in /= 1024 * 1024
io_out /= 1024 * 1024
else:
io_in = None
io_out = None
except psutil.Error as e:
return
if self._gpu:
for gpu_i, gpu in enumerate(self.gpus):
gpu_load = GPUtil.getGPUs()[gpu].load*100
gpu_mem = GPUtil.getGPUs()[gpu].memoryUsed # MB
self.bench_record.max_gpu_load[gpu_i] = max(self.bench_record.max_gpu_load[gpu_i] or 0, gpu_load)
self.bench_record.max_gpu_mem[gpu_i] = max(self.bench_record.max_gpu_mem[gpu_i] or 0, gpu_mem)
self.bench_record.gpu_load[gpu_i] = gpu_load
self.bench_record.gpu_mem[gpu_i] = gpu_mem
# Update benchmark record's RSS and VMS
self.bench_record.max_rss = max(self.bench_record.max_rss or 0, rss)
self.bench_record.max_vms = max(self.bench_record.max_vms or 0, vms)
self.bench_record.max_uss = max(self.bench_record.max_uss or 0, uss)
self.bench_record.max_pss = max(self.bench_record.max_pss or 0, pss)
self.bench_record.rss = rss
self.bench_record.vms = vms
self.bench_record.uss = uss
self.bench_record.pss = pss
self.bench_record.io_in = io_in
self.bench_record.io_out = io_out
self.bench_record.cpu_seconds += cpu_seconds
@contextlib.contextmanager
def benchmarked(pid=None, benchmark_record=None, interval=BENCHMARK_INTERVAL, gpus=None, rt_path=None):
"""Measure benchmark parameters while within the context manager
Yields a ``BenchmarkRecord`` with the results (values are set after
leaving context).
If ``pid`` is ``None`` then the PID of the current process will be used.
If ``benchmark_record`` is ``None`` then a new ``BenchmarkRecord`` is
created and returned, otherwise, the object passed as this parameter is
returned.
Usage::
with benchmarked() as bench_result:
pass
"""
result = benchmark_record or BenchmarkRecord()
if pid is False:
yield result
else:
start_time = time.time()
bench_thread = BenchmarkTimer(int(pid or os.getpid()), result, interval, gpus=gpus, rt_path=rt_path)
bench_thread.start()
yield result
bench_thread.cancel()
result.running_time = time.time() - start_time
def print_benchmark_records(records, file_, head=True, rt=False):
"""Write benchmark records to file-like object"""
if head:
print(BenchmarkRecord.get_header(), file=file_)
for r in records:
print(r.to_tsv(rt=rt), file=file_)
def write_benchmark_records(records, path, head=True, mode="w", rt=False):
"""Write benchmark records to file at path"""
with open(path, mode) as f:
print_benchmark_records(records, f, head=head, rt=rt)