aboutsummaryrefslogtreecommitdiffstats
path: root/sched_trace/draw.py
blob: 4a0386ee2861be2bd692edc022961c777a073bbb (plain) (blame)
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
from __future__ import division

import sys
import cairo

from math import ceil

from .format import EVENTS

from . import event_time, event_pid, event_cpu, event_job_id

ARROW_LINE_WIDTH = 2 # (pts)
ARROW_WIDTH      = 12 # (pts)
ARROW_HEIGHT     = 0.8 # (relative)
ALLOC_HEIGHT     = 0.5 # (relative)

COLORS = [
    (0.0000, 0.1667, 1.0000),
    (1.0000, 0.0833, 0.0000),
    (0.0417, 1.0000, 0.0000),
    (0.9583, 0.0000, 1.0000),
    (0.9792, 1.0000, 0.0000),
    (0.0000, 1.0000, 0.8958),
    (0.3958, 0.0000, 1.0000),
    (1.0000, 0.0000, 0.4792),
    (0.6042, 1.0000, 0.0000),
    (0.0000, 0.7292, 1.0000),
    (0.0000, 1.0000, 0.3333),
    (1.0000, 0.4583, 0.0000),
    (0.0208, 0.0000, 1.0000),
    (1.0000, 0.8333, 0.0000),
    (0.2083, 0.0000, 1.0000),
    (0.7917, 1.0000, 0.0000),
    (0.5833, 0.0000, 1.0000),
    (0.4167, 1.0000, 0.0000),
    (0.7708, 0.0000, 1.0000),
    (0.2292, 1.0000, 0.0000),
    (1.0000, 0.0000, 0.8542),
    (0.0000, 1.0000, 0.1458),
    (1.0000, 0.0000, 0.6667),
    (0.0000, 1.0000, 0.5208),
    (1.0000, 0.0000, 0.2917),
    (0.0000, 1.0000, 0.7083),
    (1.0000, 0.0000, 0.1042),
    (0.0000, 0.9167, 1.0000),
    (1.0000, 0.2708, 0.0000),
    (0.0000, 0.5417, 1.0000),
    (1.0000, 0.6458, 0.0000),
    (0.0000, 0.3542, 1.0000)
]

def cpu_color(cpu):
    return COLORS[cpu % len(COLORS)]

MAJOR_TICK_FONT_SIZE = 24

TASK_LABEL_FONT_SIZE = 18
TASK_LABEL_COLOR = (0, 0, 0)

TAG_FONT_SIZE = 12
TAG_COLOR = (0, 0, 0)

JOB_ID_FONT_SIZE = 10

GRID_WIDTH       = 2
MAJOR_TICK_WIDTH = 2
MINOR_TICK_WIDTH = 1

MAJOR_TICK_COLOR = (0.7, 0.7, 0.7)
MINOR_TICK_COLOR = (0.8, 0.8, 0.8)

GRID_COLOR = (0.7, 0.7, 0.7)

JOB_EVENT_COLOR = (0, 0, 0)

YRES = 100



XLABEL_MARGIN = 72
YLABEL_MARGIN = 200

def center_text(c, x, y, msg):
    x_bear, y_bear, width, height, x_adv, y_adv = c.text_extents(msg)
    c.move_to(x, y)
    c.rel_move_to(-width / 2, height)
    c.show_text(msg)

def center_text_above(c, x, y, msg):
    x_bear, y_bear, width, height, x_adv, y_adv = c.text_extents(msg)
    c.move_to(x, y)
    c.rel_move_to(-width / 2, 0)
    c.show_text(msg)

def text_left_align_below(c, x, y, msg):
    x_bear, y_bear, width, height, x_adv, y_adv = c.text_extents(msg)
    c.move_to(x, y)
    c.rel_move_to(0, height)
    c.show_text(msg)

def text_left_align_above(c, x, y, msg):
    c.move_to(x, y)
    c.show_text(msg)

def vcenter_right_align_text(c, x, y, msg):
    x_bear, y_bear, width, height, x_adv, y_adv = c.text_extents(msg)
    c.move_to(x, y)
    c.rel_move_to(-width, height/2)
    c.show_text(msg)
    return y_adv

def render(opts, trace):
    if opts.verbose:
        print '[II] Identifying relevant tasks and CPUs...',
        sys.stdout.flush()
    tasks, cores = trace.active_in_interval(opts.start, opts.end)
    if opts.verbose:
        print '%d tasks, %d cores.' % (len(tasks), len(cores))

    # scale is given in points/ms, we need points/ns
    xscale = opts.xscale/1E6
    yscale = opts.yscale/YRES
    width  = ceil(opts.margin * 2 + (opts.end - opts.start) * xscale + YLABEL_MARGIN)

    height = ceil(opts.margin * 2 + (len(tasks) * YRES)  * yscale + XLABEL_MARGIN)

    if opts.verbose:
        print '[II] Canvas size: %dpt x %dpt' % (width, height)

    pdf = cairo.PDFSurface(opts.output, width, height)

    task_idx = {}
    for i, t in enumerate(sorted(tasks)):
        task_idx[t] = i


    c = cairo.Context(pdf)
    c.translate(opts.margin + YLABEL_MARGIN, opts.margin)

    # draw box
#     c.rectangle(0, 0, width - opts.margin * 2, height - opts.margin * 2)
#     c.stroke()

    def xpos(x):
        return (x - opts.start) * xscale

    def ypos(y):
        return (y * YRES) * yscale

#    c.scale(xscale, yscale)

    if opts.verbose:
        print '[II] Drawing grid...',
        sys.stdout.flush()

    # draw minor tick lines
    if opts.minor_ticks:
        c.set_source_rgb(*MINOR_TICK_COLOR)
        c.set_line_width(MINOR_TICK_WIDTH)
        time = opts.start
        while time <= opts.end:
            x = xpos(time)
            y = ypos(len(tasks))
            c.new_path()
            c.move_to(x, y)
            c.line_to(x, 0)
            c.stroke()
            time += opts.minor_ticks * 1E6


    # draw major tick lines
    if opts.major_ticks:
        c.set_source_rgb(*MAJOR_TICK_COLOR)
        c.set_line_width(MAJOR_TICK_WIDTH)
        c.set_font_size(MAJOR_TICK_FONT_SIZE)
        time = opts.start
        while time <= opts.end:
            x = xpos(time)
            y = ypos(len(tasks) + 0.2)
            c.new_path()
            c.move_to(x, y)
            c.line_to(x, 0)
            c.stroke()
            y = ypos(len(tasks) + 0.3)
            center_text(c, x, y, "%dms" % ((time - opts.start) / 1E6))
            time += opts.major_ticks * 1E6

    # draw task labels
    c.set_font_size(TASK_LABEL_FONT_SIZE)
    c.set_source_rgb(*TASK_LABEL_COLOR)
    for pid in tasks:
        x = -24
        y = ypos(task_idx[pid] + 0.25)
        vcenter_right_align_text(c, x, y, "%s/%d" % (trace.task_names[pid], pid))
        y = ypos(task_idx[pid] + 0.75)
        vcenter_right_align_text(c, x, y,
            "(%.2fms, %.2fms)" % (trace.task_wcets[pid] / 1E6,
                                  trace.task_periods[pid] / 1E6))

    if opts.verbose:
        print 'done.'
        print '[II] Drawing CPU allocations...',
        sys.stdout.flush()


    # raw allocations
    box_height = ALLOC_HEIGHT * YRES * yscale
    c.set_font_size(TAG_FONT_SIZE)
    for (to, away) in trace.scheduling_intervals_in_range(opts.start, opts.end):
        delta = (event_time(away) - event_time(to)) * xscale
        pid = event_pid(to)
        y = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT))
        x = xpos(event_time(to))
        c.new_path()
        c.rectangle(x, y, delta, box_height)
        c.set_source_rgb(*cpu_color(event_cpu(to)))
        c.fill()

        c.set_source_rgb(*TAG_COLOR)
        text_left_align_below(c, x + 2, y + 2, '%d' % event_cpu(to))

    # draw task base lines
    c.set_source_rgb(*GRID_COLOR)
    c.set_line_width(GRID_WIDTH)
    for t in tasks:
        c.new_path()
        y = ypos(task_idx[t] + 1)
        x = xpos(opts.start)
        c.move_to(x, y)
        x = xpos(opts.end)
        c.line_to(x, y)
        c.stroke()

    if opts.verbose:
        print 'done.'
        print '[II] Drawing releases and deadlines...',
        sys.stdout.flush()

    # draw releases and deadlines
    c.set_source_rgb(*JOB_EVENT_COLOR)
    c.set_line_width(ARROW_LINE_WIDTH)
#    c.set_line_cap(cairo.LINE_JOIN_ROUND)
    c.set_line_join(cairo.LINE_JOIN_ROUND)
    c.set_font_size(JOB_ID_FONT_SIZE)
    arrow_width  = ARROW_WIDTH / 2
    arrow_height = ARROW_HEIGHT * YRES * yscale
    for rec in trace.events_in_range_of_type(opts.start, opts.end, 'ST_RELEASE'):
        pid = event_pid(rec)
        y   = ypos(task_idx[pid] + 1)

        rel = xpos(event_time(rec))
        c.new_path()
        c.move_to(rel, y - (GRID_WIDTH - 1))
        c.rel_line_to(0, -arrow_height)
        c.rel_line_to(-arrow_width, arrow_width)
        c.rel_move_to(arrow_width, -arrow_width)
        c.rel_line_to(arrow_width, arrow_width)
        c.stroke()

        if opts.show_job_ids:
            text_left_align_above(c, rel + 4, y - arrow_height - 4,
                            "%d" % event_job_id(rec))

        dl  = xpos(rec[-1])
        c.new_path()
        c.move_to(dl, y - arrow_height - (GRID_WIDTH - 1))
        c.rel_line_to(0, arrow_height)
        c.rel_line_to(-arrow_width, -arrow_width)
        c.rel_move_to(arrow_width, arrow_width)
        c.rel_line_to(arrow_width, -arrow_width)
        c.stroke()


    if opts.verbose:
        print 'done.'
        print '[II] Drawing job completions...',
        sys.stdout.flush()

    # draw job completions
    for rec in trace.events_in_range_of_type(opts.start, opts.end, 'ST_COMPLETION'):
        pid = event_pid(rec)
        y   = ypos(task_idx[pid] + 1)
        x   = xpos(event_time(rec))
        c.new_path()
        c.move_to(x, y - (GRID_WIDTH - 1))
        c.rel_line_to(0, -arrow_height)
        c.rel_move_to(-arrow_width, 0)
        c.rel_line_to(2 * arrow_width, 0)
        c.stroke()

        if opts.show_job_ids:
            text_left_align_above(c, x + 4, y - arrow_height - 4,
                            "%d" % event_job_id(rec))

    if opts.verbose:
        print 'done.'
        print '[II] Drawing job suspensions...',
        sys.stdout.flush()

    # draw job suspensions
    c.set_line_width(1)
    for rec in trace.events_in_range_of_type(opts.start, opts.end, 'ST_BLOCK'):
        pid = event_pid(rec)
        y  = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.5 * ALLOC_HEIGHT)
        y1 = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.4 * ALLOC_HEIGHT)
        y2 = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.6 * ALLOC_HEIGHT)
        x   = xpos(event_time(rec))
        c.new_path()
        c.move_to(x, y1)
        c.line_to(x, y2)
        c.line_to(x - arrow_width, y)
        c.line_to(x, y1)
        c.close_path()
        c.stroke()


    if opts.verbose:
        print 'done.'
        print '[II] Drawing job wake-ups...',
        sys.stdout.flush()

    # draw job suspensions
    c.set_line_width(1)
    for rec in trace.events_in_range_of_type(opts.start, opts.end, 'ST_RESUME'):
        pid = event_pid(rec)
        y  = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.5 * ALLOC_HEIGHT)
        y1 = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.4 * ALLOC_HEIGHT)
        y2 = ypos(task_idx[pid] + (1 - ALLOC_HEIGHT) + 0.6 * ALLOC_HEIGHT)
        x   = xpos(event_time(rec))
        c.new_path()
        c.move_to(x, y1)
        c.line_to(x, y2)
        c.line_to(x + arrow_width, y)
        c.line_to(x, y1)
        c.close_path()
        c.stroke()

    if opts.verbose:
        print 'done.'
        print '[II] Finishing PDF...',
        sys.stdout.flush()

    pdf.finish()

    if opts.verbose:
        print 'done.'

    if opts.verbose:
        print '[II] Flushing PDF...',
        sys.stdout.flush()

    pdf.flush()

    if opts.verbose:
        print 'done.'

    del pdf