-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutiliviz.py
370 lines (298 loc) · 11 KB
/
utiliviz.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
"""
UtiliViz
System utilization visualizer to profile compute and memory utilization
of code blocks.
"""
from __future__ import print_function, absolute_import
import multiprocessing as mp
from queue import Empty
from collections import OrderedDict, defaultdict
from contextlib import contextmanager
from timeit import default_timer as timer
import psutil
# The interval for the profiler to sample the system.
DEFAULT_INTERVAL = 0.05 # unit: seconds
@contextmanager
def record(mons):
"""A context-manager for containing a profiled code-block.
Example
-------
with utiliviz.record([CpuMon, CudaGpuMon]) as rec:
code_being_profiled()
rec.make_plot() # to make Bokeh plot
rawdata = rec.get_data() # to access raw data.
"""
r = Record(mons)
r.start()
try:
yield r.get_result_obj()
finally:
r.stop()
def register_magic(setup_bokeh=False):
"""Use this in Jupyter/IPython notebooks to install the magic command.
"""
from IPython.core.magic import Magics, magics_class, cell_magic
from IPython.core.magic_arguments import (magic_arguments, argument,
parse_argstring)
from bokeh.io import show, output_notebook
if setup_bokeh:
output_notebook()
@magics_class
class MyMagic(Magics):
@cell_magic
@magic_arguments()
@argument('--nocpu', action='store_true', default=False,
help='do not record CPU usage')
@argument('--cpuoverall', action='store_true', default=False,
help='record CPU overall usage')
@argument('--cuda', action='store_true', default=False,
help='record gpu usage')
def utiliviz(self, line, cell):
args = parse_argstring(self.utiliviz, line)
mons = []
if not args.nocpu:
if args.cpuoverall:
mons.append(CpuOverallMon)
else:
mons.append(CpuMon)
if args.cuda:
mons.append(CudaGpuMon)
raw_code = cell
r = Record(mons)
r.start()
self.shell.run_cell(raw_code, store_history=False)
r.stop()
results = r.get_result_obj()
# It needs more than 2 samples for a good plot
if len(results) > 2:
for p in results.make_plot():
show(p)
else:
print("utiliviz: insufficient sample data for plotting")
ip = get_ipython()
ip.register_magics(MyMagic)
def _proc_record(qread, qwrite, monclses, interval):
mons = [cls() for cls in monclses]
samples = Samples(mons)
qwrite.put("STARTED")
while True:
try:
signal = qread.get(True, interval)
except Empty:
samples.sample()
else:
te = timer()
assert signal == 'STOP'
break
# Get actual start/stop time
qwrite.put("STOPPED")
(ts, te) = qread.get()
sampledresult = samples.finalize(ts, te)
qwrite.put(sampledresult)
qwrite.close()
qread.close()
class Record(object):
def __init__(self, mons):
ctx = mp.get_context('spawn')
self._qwrite = ctx.Queue(1)
self._qread = ctx.Queue(1)
interval = DEFAULT_INTERVAL
proc = ctx.Process(target=_proc_record,
args=(self._qwrite,
self._qread,
mons,
interval))
self._proc = proc
def get_result_obj(self):
return RecordedResult(self)
def start(self):
self._proc.start()
got = self._qread.get()
assert got == 'STARTED'
self._start_time = timer()
def stop(self):
self._stop_time = timer()
self._qwrite.put('STOP')
got = self._qread.get()
assert got == 'STOPPED'
self._qwrite.put((self._start_time, self._stop_time))
self.samples = self._qread.get()
self._qwrite.close()
self._qread.close()
self._proc.join()
class RecordedResult(object):
def __init__(self, record):
self._record = record
def __len__(self):
return len(self._record.samples)
def get_data(self):
return self._record.samples.get()
def make_plot(self):
"""Make a plot of the sample data
"""
from bokeh.plotting import figure
from bokeh.models import Legend
from bokeh import palettes
def plot_data(name, samples):
ts, data = zip(*samples)
ts = [x - ts[0] for x in ts]
memdata = [d['mem'] for d in data]
memusedata = defaultdict(list)
for rec in memdata:
for k, v in rec.items():
memusedata[k].append(v[0] / v[1] * 100)
compdata = [d['compute'] for d in data]
computildata = defaultdict(list)
for rec in compdata:
for k, v in rec.items():
computildata[k].append(v * 100)
# Make plot
plot = figure(plot_height=200, title=name,
x_range=(0, max(ts)), y_range=(0, 105),
background_fill_color='#EEEEEE',
toolbar_location=None)
plot.xaxis.axis_label = 'seconds'
plot.yaxis.axis_label = 'util %'
plot.xaxis.axis_label_text_font_size = '8pt'
plot.yaxis.axis_label_text_font_size = '8pt'
plot.xgrid.grid_line_color = None
plot.ygrid.grid_line_color = None
legend_items = []
widths = [a - b for a, b in zip(ts[1:], ts[:-1])]
widths.append(widths[-1])
cmap = list(palettes.Category10[10])
def cmap_picker():
while True:
for c in cmap:
yield c
picker = cmap_picker()
colors = defaultdict(lambda: next(picker))
for k, vs in memusedata.items():
ln = plot.rect(x=[t + w / 2 for t, w in zip(ts, widths)],
y=[v / 2 for v in vs],
width=widths, height=vs, alpha=0.5,
line_alpha=0, fill_color=colors[k])
legend_items.append(('MEM-{}'.format(k), [ln]))
for k, vs in computildata.items():
styles = dict(line_color=colors[k], line_width=1,
line_alpha=0.7)
if '*' in k:
styles.update(line_width=4, line_color='#333333')
ln = plot.line(ts, vs, line_dash='dashed', **styles)
# markers = plot.triangle(ts, vs, size=7, alpha=0.9,
# fill_color=styles['line_color'],
# line_color=styles['line_color'])
legend_items.append(('{}'.format(k), [ln]))
else:
ln = plot.line(ts, vs, **styles)
cir = plot.circle(ts, vs, size=styles['line_width'] + 1,
fill_color=colors[k], **styles)
legend_items.append(('{}'.format(k), [ln, cir]))
legend = Legend(items=legend_items,
label_text_font_size='6pt',
location=(0, 0),
orientation='horizontal')
plot.add_layout(legend, 'below')
return plot
return [plot_data(name, sample_data)
for name, sample_data in self.get_data().items()]
class Samples(object):
def __init__(self, monitors):
self._samples = OrderedDict()
self._mons = monitors
for m in self._mons:
self._samples[m.name] = []
self._ct = 0
def sample(self):
ts = timer()
for mon in self._mons:
self._samples[mon.name].append((ts, mon.sample()))
self._ct += 1
def finalize(self, start_time, stop_time):
for k in self._samples:
self._samples[k] = [x for x in self._samples[k]
if start_time < x[0] < stop_time]
return SampledResult(self._ct, self._samples)
class SampledResult(object):
def __init__(self, nsample, samples):
self._ct = nsample
self._samples = samples
def __len__(self):
return self._ct
def get(self):
return self._samples
class Mon(object):
def get_memory_info(self):
"""Returns `{"proc_name": (used, total)}`.
Memory usage is reported in bytes.
"""
raise NotImplementedError
def get_processor_util(self):
"""Returns `{"proc_name": util_percent }`.
Utilization is reported as floating point as percentage.
"""
raise NotImplementedError
def sample(self):
return {'mem': self.get_memory_info(),
'compute': self.get_processor_util()}
class CpuMon(Mon):
"""Uses psutil
"""
_primed = False
_percpu = True
name = 'cpu'
def __init__(self):
if not self._primed:
# trigger psutil.cpu_percent since the first value is meaningless
psutil.cpu_percent()
self._primed = True
def get_memory_info(self):
out = OrderedDict()
vm = psutil.virtual_memory()
out['sys'] = (vm.used, vm.total)
return out
def get_processor_util(self):
out = OrderedDict()
if self._percpu:
values = []
for i, v in enumerate(psutil.cpu_percent(percpu=self._percpu)):
v /= 100
values.append(v)
out['cpu{}'.format(i)] = v
n = len(out)
out['cpu-overall*'] = sum(values) / n
else:
out['cpu'] = psutil.cpu_percent(percpu=self._percpu) / 100
return out
class CpuOverallMon(CpuMon):
_percpu = False
name = 'cpu-overall'
class CudaGpuMon(Mon):
"""Uses py3nvml
"""
name = 'cuda'
def __init__(self):
import py3nvml.py3nvml as nvml
self._nvml = nvml
self._gpus = OrderedDict()
self._init()
def _init(self):
self._nvml.nvmlInit()
ngpus = self._nvml.nvmlDeviceGetCount()
for i in range(ngpus):
handle = self._nvml.nvmlDeviceGetHandleByIndex(i)
device_name = self._nvml.nvmlDeviceGetName(handle)
name = "CUDA {}: {}".format(i, device_name)
self._gpus[name] = handle
def get_memory_info(self):
out = OrderedDict()
for k, hdl in self._gpus.items():
info = self._nvml.nvmlDeviceGetMemoryInfo(hdl)
out[k] = (info.used, info.total)
return out
def get_processor_util(self):
out = OrderedDict()
for k, hdl in self._gpus.items():
util = self._nvml.nvmlDeviceGetUtilizationRates(hdl)
out[k] = util.gpu / 100
return out