-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_jobs.py
547 lines (435 loc) · 18.3 KB
/
profile_jobs.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import itertools
import numpy as np
import re
import sys
import time
import os
from subprocess import call
from lxml.html import builder as E
from lxml.html.builder import TR, TD, TH
import lxml
from collections import namedtuple
from numpy.lib.recfunctions import append_fields
seconds_per_hour = 60.0 * 60.0
output_dir = 'rum_profile'
Event = namedtuple('Event', 'timestamp type step job source')
Proc = namedtuple('Proc', 'step start stop job')
step_mapping = {
'Run bowtie on genome' : 'Run Bowtie on genome',
'Parse genome Bowtie output' : 'Run Bowtie on genome',
'Run bowtie on transcriptome' : 'Run Bowtie on transcriptome',
'Parse transcriptome Bowtie output' : 'Run Bowtie on transcriptome',
'Run blat on unmapped reads' : 'Run BLAT',
'Parse blat output' : 'Run BLAT',
'Run mdust on unmapped reads' : 'Run BLAT',
'Sort junctions (all, bed) by location' : 'Sort junctions',
'Sort junctions (all, rum) by location' : 'Sort junctions',
'Sort junctions (high-quality, bed) by location' : 'Sort junctions',
'Finish mapping stats' : 'Other post-processing',
'Merge SAM headers' : 'Other post-processing',
'Sort junctions' : 'Other post-processing',
'Get inferred internal exons' : 'Other post-processing',
'Merge quants' : 'Other post-processing',
'Sort junctions' : 'Other post-processing',
'Remove duplicates from NU' : 'Other processing',
'Make unmapped reads file for blat' : 'Other processing',
}
def parse_log_file(filename, job_name):
"""Parse the specified log file and return an iterator of Events
from the file."""
time_re = "(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})"
time_pat = re.compile(time_re)
pat = re.compile(time_re + ".*RUM\.Workflow.*(START|FINISH)\s+(.*)")
time_fmt = "%Y/%m/%d %H:%M:%S"
first_time = None
with open(filename) as f:
for line in f:
if first_time is None:
m = time_pat.match(line)
if m is None:
raise Exception("Couldn't parse time from " + line)
tm = m.group(1)
print "TM is " + str(tm)
first_time = time.strptime(tm, time_fmt)
print "First time is " + str(first_time)
yield Event(first_time, 'START', 'log', job_name, filename)
m = pat.match(line)
if (m is not None):
(tm, type, step) = m.groups()
t = time.strptime(tm, time_fmt)
e = Event(t, type, step, job_name, filename)
yield e
def build_timings(events):
"""Given a list of events, match each START events to its
corresponding FINISH event and return an iterator over the resulting
Proc objects."""
stack = []
timings = []
for e in events:
if e.type == 'START':
stack.append(e)
elif e.type == 'FINISH':
prev = stack.pop()
if prev.step != e.step:
raise Exception(
"""I have a FINISH event for the START event of a
different step""")
yield Proc(e.step, prev.timestamp, e.timestamp, e.job)
def append_filenames(dest, dirname, filenames):
pat = re.compile("(rum_(postproc|\d+).*log)|(rum.log)$")
for filename in filenames:
if pat.match(filename):
dest.append(dirname + "/" + filename)
def load_events_for_job(path, job_name):
log_files = []
os.path.walk(path, append_filenames, log_files)
event_iters = [parse_log_file(f, job_name) for f in log_files]
return itertools.chain(*event_iters)
def group_times_by_step(procs):
times_for_step = {}
for p in procs:
if p.step not in times_for_step:
times_for_step[p.step] = []
start = time.mktime(p.start)
stop = time.mktime(p.stop)
times_for_step[p.step].append(stop - start)
return times_for_step
def new_step_name(step):
if step in step_mapping:
return step_mapping[step]
return step
def procs_to_array(procs):
steps = []
seen_steps = set()
for p in procs:
step = new_step_name(p.step)
if step not in seen_steps:
seen_steps.add(step)
steps.append(step)
times_for_step = group_times_by_step(procs)
times_for_step = rename_steps(times_for_step)
table = []
for s in steps:
times = np.array(times_for_step[s])
table.append((s, len(times), sum(times), max(times)))
return np.array(table, dtype=[
('step', 'S100'),
('chunks', int),
('total', float),
('median', float)])
def rename_steps(step_to_times):
result = {}
for step in step_to_times:
times = np.array(step_to_times[step])
new_step = step_mapping[step] if step in step_mapping else step
if new_step in result:
result[new_step] += times
else:
result[new_step] = times
return result
def infer_preproc_from_events(events):
log_starts = [e for e in events if e.step == 'log']
log_starts = sorted(log_starts, key=lambda x: x.timestamp)
(start, stop) = log_starts[0:2]
return Proc('Pre-processing', start.timestamp, stop.timestamp, start.job)
def main():
jobs = []
for arg in sys.argv[1:]:
(job_name, dir) = arg.split("=")
jobs.append((job_name, dir))
stats = {}
for (job_name, dir) in jobs:
events = list(load_events_for_job(dir, job_name))
preproc = infer_preproc_from_events(events)
procs = list(build_timings(events))
procs = [preproc] + procs
job_stats = procs_to_array(procs)
if job_name not in stats:
stats[job_name] = []
stats[job_name].append(job_stats)
tables = []
seen_job_name = set()
for (job_name, path) in jobs:
if job_name not in seen_job_name:
seen_job_name.add(job_name)
copies = stats[job_name]
merged = merge_copies(stats[job_name])
tables.append((job_name, merged))
table = make_final_table(tables)
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
call(['unzip', 'bootstrap.zip', '-d', output_dir])
call(['cp', 'profile.css', output_dir])
metrics = ['cpu', 'wc']
for metric in metrics:
filename = 'rum_profile/%s.html' % metric
print_table(filename, table, [x[0] for x in tables], metric)
print_help_page()
def print_help_page():
with open('rum_profile/help.html', 'w') as f:
contents = E.DIV(
E.P("""
This shows the amount of time spent on each step for one or more RUM
jobs. You should be able to use the output to gain some insight into
the performance of a single job, or to compare the running times for
two or more jobs in order to understand the effects of changes made to
the RUM code, to the input data, or to the system on which RUM was
run."""),
E.P("""
The times for a single job are are compiled by parsing the log files
after the job is finished. For each step, we consider the duration to
be from the time the script was actually invoked to the time the
script exited. This means that any latency caused by the cluster's
scheduling system will not be reflected in the times."""),
E.P("""
"CPU time" for a step measures the total time for the step across all
chunks. If you have grouped multiple jobs together, the CPU time
reported here is the median value for all the jobs. This is intended
to show the total amount of computing time the job would take for a
typical invocation. The total CPU time for the job is the sum of all
these median values.
"""),
E.P("""
"Wallclock time" for a step is the time of the chunk that took the
longest time to complete the step. We use the maximum value in order
model the worst-case scenario, where one of the chunks takes much
longer than the other chunks, and becomes the limiting factor for the
running time of the whole job. If you have grouped jobs together,
wallclock time is the median value for all jobs. This is intended to
show the maximum duration of a typical invocation of the job."""),
E.P("""
The times for all steps are highlighted in varying shades of yelloq in
order to indicate each step's impact on the total running time,
relative to the other steps. The step with the longest time is pure
yellow, the step with the shortest time is pure white, and the colors
for the other steps are scaled linearly according to the running
time. This is intended to allow you to quickly identify hot spots, or
steps that took a very long time compared to other steps, and which
might benefit from optimization or parameter tuning.
"""),
E.P("""
If you have run two or more job groups, we use the first group as a
baseline and compare the running time of all the other jobs to the
baseline. The "improvement" for a step shows the number of hours saved
relative to the baseline. The percent improvement is the improvement
divided by the total running time for the baseline job. This is
intended to show the impact that improving the running time of one
step has on the running time of the entire job. For example suppose
the baseline job took 100 hours, 30 of which were spent on the "Run
BLAT" step, and that the "Run BLAT" step in the comparison job took
only 20 hours. The improvement is 30 - 20 = 10 hours, and the percent
improvement is (30 - 20) / 100 = 10%. If a step in the new job is
slower, the improvement and percent improvement will be negative."""),
E.P("""
The improvement for each step is colored green or red according to the
degree to which that step improved or degraded the performance
compared to the baseline."""),
E.P(E.STRONG("Note:"),
"""
These numbers do not include the "preprocessing" phase at all. Prior
to RUM 2.0.3, it is difficult to determine from the log files exactly
when pre-processing begins and ends. RUM 2.0.3 and greater will
clearly include these times in the log file, so future versions of the
profiling program will be able to incorporate times for
preprocessing.""")
)
help_page = template('help',
contents)
f.write(lxml.html.tostring(help_page))
def merge_copies(copies):
result = []
steps = copies[0]['step']
print "--- Merging %d copies ---" % len(copies)
for step in range(len(copies[0])):
steps = set([c[step]['step'] for c in copies])
chunks = set([c[step]['chunks'] for c in copies])
totals = [c[step]['total'] for c in copies]
medians = [c[step]['median'] for c in copies]
if (len(steps) != 1):
raise Exception("Different steps for copies of same job")
if (len(chunks) != 1):
raise Exception("Different number of chunks for copies of same job")
row = (list(steps)[0],
list(chunks)[0],
np.median(totals),
np.median(medians))
result.append(row)
return np.array(result, dtype=copies[0].dtype)
def make_final_table(jobs):
stats = [j[1] for j in jobs]
columns = []
did_step = False
steps = [(step,) for step in jobs[0][1]['step']]
result = np.array(steps, dtype=[('step', 'S100')])
for (name, table) in jobs:
result = append_fields(
result,
['%s_chunks' % name,
'%s_cpu' % name,
'%s_wc' % name],
[table['chunks'],
table['total'],
table['median']
])
for (name, table) in jobs:
cpu_times = result['%s_cpu' % name]
wc_times = result['%s_wc' % name]
cpu_intensity = (cpu_times - min(cpu_times)) / (max(cpu_times) - min(cpu_times))
wc_intensity = (wc_times - min(wc_times)) / (max(wc_times) - min(wc_times))
cpu_intensity = (255 - (cpu_intensity * 255))
wc_intensity = (255 - (wc_intensity * 255))
result = append_fields(
result,
['%s_cpu_pct' % name,
'%s_wc_pct' % name,
'%s_cpu_intensity' % name,
'%s_wc_intensity' % name,
],
[100 * cpu_times / sum(cpu_times),
100 * wc_times / sum(wc_times),
cpu_intensity,
wc_intensity
])
return result
# return np.array(list(result) + [footer], dtype=result.dtype)
def calc_gain(table, job_names):
baseline_cpu_secs = table['%s_cpu' % job_names[0]]
baseline_wc_secs = table['%s_wc' % job_names[0]]
for name in job_names[1:]:
cpu_gain = baseline_cpu_secs - table['%s_cpu' % name]
median_gain = baseline_wc_secs - table['%s_wc' % name]
cpu_pct_gain = cpu_gain / sum(baseline_cpu_secs)
wc_pct_gain = median_gain / sum(baseline_wc_secs)
table = append_fields(table,
['%s_cpu_gain' % name,
'%s_cpu_pct_gain' % name,
'%s_wc_gain' % name,
'%s_wc_pct_gain' % name,
],
[cpu_gain, 100. * cpu_pct_gain,
median_gain, 100. * wc_pct_gain]
)
return table
def td_hours(seconds, bgcolor=None):
if bgcolor is None:
return TD('%.2f' % (seconds / seconds_per_hour),
CLASS='numeric')
else:
return TD('%.2f' % (seconds / seconds_per_hour),
CLASS='numeric',
bgcolor=bgcolor)
def td_percent(pct, bgcolor=None):
if bgcolor is None:
return TD('%.2f%%' % pct,
CLASS='numeric')
else:
return TD('%.2f%%' % pct, bgcolor=bgcolor,
CLASS='numeric')
metric_names = {
'wc' : 'Wallclock',
'cpu' : 'CPU'
}
def gain_pct_to_bgcolor(pct):
intensity = 255 * (pct / 100.)
if (intensity < 0):
intensity = 255 + intensity
return '#ff%02x%02x' % (intensity, intensity)
else:
intensity = 255 - intensity
return '#%02xff%02x' % (intensity, intensity)
def template(name, contents):
cpu_class = 'active' if name == 'cpu' else ''
wc_class = 'active' if name == 'wc' else ''
help_class = 'active' if name == 'help' else ''
return E.HTML(
E.HEAD(
E.LINK(rel='stylesheet', type='text/css', href='bootstrap/css/bootstrap.css'),
E.LINK(rel='stylesheet', type='text/css', href='profile.css'),
E.SCRIPT(src='bootstrap/js/bootstrap.min.js'),
E.TITLE('RUM Job Profile')),
E.BODY(
E.DIV(
E.DIV(
E.DIV(
E.A(E.SPAN(CLASS='icon-bar'),
E.SPAN(CLASS='icon-bar'),
E.SPAN(CLASS='icon-bar'),
CLASS='btn btn-navbar'),
E.A('RUM Profile', CLASS='brand', href='#'),
E.DIV(
E.UL(
E.LI(
E.A('CPU time', href='cpu.html'),
CLASS=cpu_class),
E.LI(
E.A('Wallclock time', href='wc.html'),
CLASS=wc_class),
E.LI(
E.A('Help', href='help.html'),
CLASS=help_class),
CLASS='nav'),
CLASS='nav-collapse collapse'),
CLASS='container'),
CLASS='navbar-inner'),
CLASS='navbar navbar-inverse navbar-fixed-top'),
E.BR(),
E.BR(),
E.BR(),
E.DIV(contents,
CLASS='container')))
def print_table(filename, table, job_names, metric):
table = calc_gain(table, job_names)
baseline = job_names[0]
with open(filename, 'w') as out:
top_headers = TR(TH(''))
headers = TR(TH('Steps'))
# Build the header row
for j in job_names:
colspan = 1
headers.append(TH('chunks'))
colspan += 2
headers.append(TH('hours', colspan='2'))
# All jobs except the baseline get "hours gained" and
# "percent hours gained" columns
if j != baseline:
colspan += 2
headers.append(TH('improvement', colspan='2'))
top_headers.append(TH(j, colspan=str(colspan)))
data_rows = []
for row in table:
tr = TR(TD(str(row['step'])))
for j in job_names:
tr.append(TD(str(row['%s_chunks' % j]),
CLASS='numeric'))
intensity = row['%s_%s_intensity' % (j, metric)]
bgcolor = '#ffff%02x' % intensity
tr.append(td_hours(row['%s_%s' % (j, metric)], bgcolor))
tr.append(td_percent(row['%s_%s_pct' % (j, metric)], bgcolor))
if j != baseline:
hours = row['%s_%s_gain' % (j, metric)]
pct = row['%s_%s_pct_gain' % (j, metric)]
intensity = 255 * (pct / 100.)
bgcolor = gain_pct_to_bgcolor(pct)
tr.append(td_hours(hours, bgcolor=bgcolor))
tr.append(td_percent(pct, bgcolor=bgcolor))
data_rows.append(tr)
summary = [TD('Totals')]
for j in job_names:
summary.append(TD(''))
summary.extend([
td_hours(sum(table['%s_%s' % (j, metric)])),
td_percent(sum(table['%s_%s_pct' % (j, metric)]))])
if j != baseline:
hours = sum(table['%s_%s_gain' % (j, metric)])
pct = sum(table['%s_%s_pct_gain' % (j, metric)])
bgcolor = gain_pct_to_bgcolor(pct)
summary.extend([
td_hours(hours, bgcolor),
td_percent(pct, bgcolor)])
rows = [top_headers, headers]
rows.extend(data_rows)
rows.append(TR(*summary))
html = template(metric, E.DIV(
E.TABLE(*rows)))
out.write(lxml.html.tostring(html))
main()