summaryrefslogtreecommitdiffstats
path: root/unit_trace/viz/schedule.py
blob: f1345623e2a83adb688329bee2e7cacbee7b5197 (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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env python

"""The data structures to store a schedule (task system), along with all
the job releases and other events that have occurred for each task. This gives
a high-level representation of a schedule that can be converted to, say, a
graphic."""

from draw import *
import util

import copy

EVENT_LIST = None
SPAN_EVENTS = None

class TimeSlotArray(object):
    """Represents another way of organizing the events. This structure organizes events by
    the (approximate) time at which they occur. Events that occur at approximately the same
    time are assigned the same ``slot'', and each slot organizes its events by task number
    as well as by CPU."""
    
    TASK_LIST = 0
    CPU_LIST = 1
    
    def __init__(self, time_per_maj=None, num_tasks=0, num_cpus=0):
        if time_per_maj is None:
            self.array = None
            return
            
        self.time_per_maj = time_per_maj
        self.list_sizes = { TimeSlotArray.TASK_LIST : num_tasks, TimeSlotArray.CPU_LIST : num_cpus }
        self.array = {}
        
        for type in self.list_sizes:
            num = self.list_sizes[type]
            self.array[type] = []
            for j in range(0, num):
                # for each slot in the array, we need a list of all events under this type
                # (for example, a list of all events that occur in this time slot, indexed
                # by task).
                self.array[type].append(dict(zip(EVENT_LIST, \
                                [{} for j in range(0, len(EVENT_LIST))])))
    
    def get_time_slot(self, time):
        return int(time // self.time_per_maj)
    
    def _put_event_in_slot(self, list_type, no, klass, slot, event):
        if slot not in self.array[list_type][no][klass]:
            self.array[list_type][no][klass][slot] = []
        self.array[list_type][no][klass][slot].append(event)
                
    def add_event_to_time_slot(self, event):
        task_no = event.get_job().get_task().get_task_no()
        cpu = event.get_cpu()
        time_slot = self.get_time_slot(event.get_time())
        
        self._put_event_in_slot(TimeSlotArray.TASK_LIST, task_no, event.__class__, time_slot, event)
        self._put_event_in_slot(TimeSlotArray.CPU_LIST, cpu, event.__class__, time_slot, event)
        
        if event.__class__ in SPAN_END_EVENTS:
            self.fill_span_event_from_end(event)
        
    def fill_span_event_from_end(self, event):
        start_slot = None
        if event.corresp_start_event is None:
            start_slot = self.get_time_slot(event.get_job().get_task().get_schedule().start) - 1
        else:
            start_slot = self.get_time_slot(event.corresp_start_event.get_time())
        end_slot = self.get_time_slot(event.get_time())
        
        for slot in range(start_slot + 1, end_slot):
            task_no = event.get_job().get_task().get_task_no()
            cpu = event.get_cpu()
            
            dummy = SPAN_END_EVENTS[event.__class__](task_no, cpu)
            dummy.corresp_start_event = event.corresp_start_event
            dummy.corresp_end_event = event
                    
            self._put_event_in_slot(TimeSlotArray.TASK_LIST, task_no, dummy.__class__, slot, dummy)
            self._put_event_in_slot(TimeSlotArray.CPU_LIST, cpu, dummy.__class__, slot, dummy)
            
    def fill_span_event_from_start(self, event):
        end_slot = None
        if event.corresp_end_event is None:
            end_slot = self.get_time_slot(event.get_job().get_task().get_schedule().end) + 1
        else:
            end_slot = self.get_time_slot(event.corresp_end_event.get_time())
        start_slot = self.get_time_slot(event.get_time())
        
        for slot in range(start_slot + 1, end_slot):
            task_no = event.get_job().get_task().get_task_no()
            cpu = event.get_cpu()
            
            dummy = SPAN_START_EVENTS[event.__class__](task_no, cpu)
            dummy.corresp_start_event = event
            dummy.corresp_end_event = event.corresp_end_event
            
            self._put_event_in_slot(TimeSlotArray.TASK_LIST, task_no, dummy.__class__, slot, dummy)
            self._put_event_in_slot(TimeSlotArray.CPU_LIST, cpu, dummy.__class__, slot, dummy)
            
    def iter_over_period(self, start, end, start_no, end_no, list_type, event_types):
        if self.array is None:
            return # empty schedule
            
        if start > end:
            raise ValueError('Litmus is not a time machine')
        if start_no > end_no:
            raise ValueError('start no should be less than end no')
        
        start_slot = self.get_time_slot(start)
        end_slot = self.get_time_slot(end) + 2
        
        start_no = max(0, start_no)
        end_no = min(self.list_sizes[list_type] - 1, end_no)
        
        for no in range(start_no, end_no + 1):
            for type in event_types:
                for slot in range(start_slot, end_slot):
                    if slot in self.array[list_type][no][type]:
                        for event in self.array[list_type][no][type][slot]:
                            yield event
    
class Schedule(object):
    """The total schedule (task system), consisting of a certain number of
    tasks."""
    
    def __init__(self, name, num_cpus, task_list=[]):
        self.name = name
        self.tasks = {}
        self.task_list = []
        self.selected = {}
        self.time_slot_array = None
        self.cur_task_no = 0
        self.num_cpus = num_cpus
        for task in task_list:
            self.add_task(task)
    
    def get_selected(self):
        return self.selected
        
    def set_selected(self, new_selected):
        for event in self.selected:
            event.selected = False
        for event in new_selected:
            event.selected = True
        self.selected = new_selected
    
    def get_selected(self):
        return copy.copy(self.selected)
            
    def set_time_params(self, time_per_maj=None):
        self.time_per_maj = time_per_maj
        if self.time_per_maj is None:
            self.time_slot_array = TimeSlotArray()
            return
            
        self.time_slot_array = TimeSlotArray(self.time_per_maj, \
                                                 len(self.task_list), self.num_cpus)
            
    def get_time_slot_array(self):
        return self.time_slot_array
    
    def get_time_bounds(self):
        return (self.start, self.end)
        
    def scan(self, time_per_maj):
        self.start = None
        self.end = None
        
        self.set_time_params(time_per_maj)
        
        # we scan the graph task by task, and job by job
        for task_no, task in enumerate(self.get_task_list()):
            switches = {}
            for event in EVENT_LIST:
                switches[event] = None
            cur_cpu = [Event.NO_CPU]
            for job_no in sorted(task.get_jobs().keys()):
                job = task.get_jobs()[job_no]    
                for event_time in sorted(job.get_events().keys()):
                    # could have multiple events at the same time (unlikely but possible)
                    for event in job.get_events()[event_time]:
                        event.scan(cur_cpu, switches)
                        
            # What if one of the initial "span events" (switch to or inversion starting) never got a
            # corresponding end event? Well, then we assume that the end event was simply outside of
            # the range of whatever we read in. So we need to fill dummies starting from the initial
            # event all the way to the end of the graph, so that the renderer can see the event no matter
            # how far the user scrolls to the right.
            for span_event in SPAN_START_EVENTS:
                event = switches[span_event]
                if event is not None:
                    self.time_slot_array.fill_span_event_from_start(event)
        
    def add_task(self, task):
        if task.name in self.tasks:
            raise ValueError("task already in list!")
        self.tasks[task.name] = task
        self.task_list.append(task)
        task.schedule = self
        task.task_no = self.cur_task_no
        self.cur_task_no += 1
    
    def get_tasks(self):
        return self.tasks
        
    def get_task_list(self):
        return self.task_list
    
    def get_name(self):
        return self.name
    
    def get_num_cpus(self):
        return self.num_cpus
                    
class Task(object):
    """Represents a task, including the set of jobs that were run under
    this task."""
    
    def __init__(self, name, job_list=[]):
        self.name = name
        self.jobs = {}
        self.task_no = None
        self.schedule = None
        for job in job_list:
            self.add_job(job)
        
    def add_job(self, job):
        if job.job_no in self.jobs:
            raise ScheduleError("a job is already being released at this time for this task")
        self.jobs[job.job_no] = job
        job.task = self
    
    def get_schedule(self):
        return self.schedule
        
    def get_jobs(self):
        return self.jobs
    
    def get_task_no(self):
        return self.task_no
        
    def get_name(self):
        return self.name
            
class Job(object):
    """Represents a job, including everything that happens related to the job"""
    def __init__(self, job_no, event_list=[]):
        self.job_no = job_no
        self.events = {}
        self.task = None
        for event in event_list:
            self.add_event(event)
        
    def add_event(self, event):
        if event.time not in self.events:
            self.events[event.time] = []
        self.events[event.time].append(event)
        event.job = self
    
    def get_events(self):
        return self.events
    
    def get_task(self):
        return self.task
    
    def get_job_no(self):
        return self.job_no

class DummyEvent(object):
    """Represents some event that occurs, but might not actually be
    a full-fledged ``event'' in the schedule. It might instead be a dummy
    event added by the application to speed things up or keep track of
    something. Such an event won't be added to the schedule tree, but
    might appear in the time slot array."""

    def __init__(self, time, cpu):
        self.time = time
        self.cpu = cpu
        self.job = None
        self.layer = None
    
    def __str__(self):
        return '[Dummy Event]'
            
    def get_time(self):
        return self.time
        
    def get_cpu(self):
        return self.cpu
        
    def get_job(self):
        return self.job
    
    def get_layer(self):
        return self.layer
        
    def render(self, graph, layer, prev_events, selectable=False):
        """Method that the visualizer calls to tell the event to render itself
        Obviously only implemented by subclasses (actual event types)
        
        ``Rendering'' can mean either actually drawing the event or just
        adding it as a selectable region. This is controlled by the
        ``selectable'' parameter"""
        raise NotImplementdError
        
class Event(DummyEvent):
    """Represents an event that occurs while a job is running (e.g. get scheduled
    on a CPU, block, ...)"""
    NO_CPU = -1
    NUM_DEC_PLACES = 2
    
    def __init__(self, time, cpu):
        super(Event, self).__init__(time, cpu)
        self.erroneous = False
        self.selected = False
    
    def __str__(self):
        return '[Event]'
    
    def _common_str(self):
        job = self.get_job()
        task = job.get_task()
        return ' for task ' + str(task.get_name()) + ': (TASK, JOB)=' + str((task.get_task_no(), \
            job.get_job_no())) + ', CPU=' + str(self.get_cpu())
            
    def is_erroneous(self):
        """An erroneous event is where something with the event is not quite right,
        something significantly wrong that we don't have logical information telling
        us how we should render the event."""
        return self.erroneous
    
    def is_selected(self):
        """Returns whether the event has been selected by the user. (needed for rendering)"""
        return self.selected
            
    def scan(self, cur_cpu, switches):
        """Part of the procedure that walks through all the events and sets
        some parameters that are unknown at first. For instance, a SwitchAwayEvent
        should know when the previous corresponding SwitchToEvent occurred, but
        the data does not tell us this, so we have to figure that out on our own
        by scanning through the events. ``cur_cpu'' gives the current CPU at this
        time in the scan, and ``switches'' gives the last time a certain switch
        (e.g. SwitchToEvent, InversionStartEvent) occurred"""
        time = self.get_time()
        sched = self.get_job().get_task().get_schedule()
        if sched.start is None or time < sched.start:
            sched.start = time
        if sched.end is None or time > sched.end:
            sched.end = time
        
        sched.get_time_slot_array().add_event_to_time_slot(self)

class ErrorEvent(Event):
    pass
                
class SuspendEvent(Event):
    def __init__(self, time, cpu):
        super(SuspendEvent, self).__init__(time, cpu)
        self.layer = Canvas.MIDDLE_LAYER
    
    def __str__(self):
        return 'Suspend' + self._common_str() + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
            
    def scan(self, cur_cpu, switches):
        if self.get_cpu() != cur_cpu[0]:
            self.erroneous = True
            #fprint "suspending on a CPU different from the CPU we are on!"
        super(SuspendEvent, self).scan(cur_cpu, switches)
            
    def render(self, graph, layer, prev_events, selectable=False):
        if layer == self.layer:
            prev_events[self] = None
            if selectable:
                graph.add_sel_suspend_triangle_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                                self.get_cpu(), self)
            else:
                graph.draw_suspend_triangle_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                                self.get_cpu(), self.is_selected())
            
        
class ResumeEvent(Event):
    def __init__(self, time, cpu):
        super(ResumeEvent, self).__init__(time, cpu)
        self.layer = Canvas.MIDDLE_LAYER
    
    def __str__(self):
        return 'Resume' + self._common_str() + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
            
    def scan(self, cur_cpu, switches):
        if cur_cpu[0] != Event.NO_CPU and cur_cpu[0] != self.get_cpu():
            self.erroneous = True
            #print "Resuming when currently scheduled on a CPU, but on a different CPU from the current CPU!"
        super(ResumeEvent, self).scan(cur_cpu, switches)
        
    def render(self, graph, layer, prev_events, selectable=False):
        if layer == self.layer:
            prev_events[self] = None
            if selectable:
                graph.add_sel_resume_triangle_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                                self.get_cpu(), self)
            else:
                graph.draw_resume_triangle_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                               self.get_cpu(), self.is_selected())
                
        
class CompleteEvent(Event):
    def __init__(self, time, cpu):
        super(CompleteEvent, self).__init__(time, cpu)
        self.layer = Canvas.TOP_LAYER
    
    def __str__(self):
        return 'Complete' + self._common_str() + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
            
    def scan(self, cur_cpu, switches):
        super(CompleteEvent, self).scan(cur_cpu, switches)
    
    def render(self, graph, layer, prev_events, selectable=False):
        if layer == Canvas.TOP_LAYER:
            prev_events[self] = None
            if selectable:
                graph.add_sel_completion_marker_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                                self.get_cpu(), self)
            else:
                graph.draw_completion_marker_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                                 self.get_cpu(), self.is_selected())

class SwitchToEvent(Event):
    def __init__(self, time, cpu):
        super(SwitchToEvent, self).__init__(time, cpu)
        self.layer = Canvas.BOTTOM_LAYER
        self.corresp_end_event = None
        
    def __str__(self):
        if self.corresp_end_event is None:
            return 'Switch To (w/o Switch Away)' + self._common_str() + ', TIME=' \
                   + str(self.get_time())
        return 'Scheduled' + self._common_str() + ', START=' \
               + util.format_float(self.get_time(), Event.NUM_DEC_PLACES) \
               + ', END=' + util.format_float(self.corresp_end_event.get_time(), Event.NUM_DEC_PLACES)
        
    def scan(self, cur_cpu, switches):
        old_cur_cpu = cur_cpu[0]
        cur_cpu[0] = self.get_cpu()
        switches[SwitchToEvent] = self
        self.corresp_end_event = None
        
        if old_cur_cpu != Event.NO_CPU:
            self.erroneous = True
            #print "currently scheduled somewhere, can't switch to a CPU"
                            
        super(SwitchToEvent, self).scan(cur_cpu, switches)
    
    def render(self, graph, layer, prev_events, selectable=False):
        if layer == self.layer:
            end_time = None
            clip = None
            if self.corresp_end_event is None:
                end_time = self.get_job().get_task().get_schedule().end
                clip = AlignMode.RIGHT
            else:
                end_time = self.corresp_end_event.get_time()
            
            prev_events[self] = None
            cpu = self.get_cpu()
            task_no = self.get_job().get_task().get_task_no()
            if selectable:
                graph.add_sel_bar_at_time(self.get_time(), end_time,
                                     task_no, cpu, self)
            else:
                graph.draw_bar_at_time(self.get_time(), end_time,
                                   task_no, cpu, self.get_job().get_job_no(),
                                   clip, self.is_selected())
                                           
class SwitchAwayEvent(Event):
    def __init__(self, time, cpu):
        super(SwitchAwayEvent, self).__init__(time, cpu)
        self.layer = Canvas.BOTTOM_LAYER
        self.corresp_start_event = None
    
    def __str__(self):
        if self.corresp_start_event is None:
            return 'Switch Away (w/o Switch To)' + self._common_str() + 'TIME=' \
                   + str(self.get_time())
        return str(self.corresp_start_event)
            
    def scan(self, cur_cpu, switches):
        old_cur_cpu = cur_cpu[0]
        
        self.corresp_start_event = switches[SwitchToEvent]
        
        cur_cpu[0] = Event.NO_CPU
        switches[SwitchToEvent] = None
        
        if self.corresp_start_event is not None:
            self.corresp_start_event.corresp_end_event = self
            
        if self.get_cpu() != old_cur_cpu:
            self.erroneous = True
            #print "switching away from a CPU different from the CPU we are currently on"
        if self.corresp_start_event is None:
            self.erroneous = True
            #print "switch away was not matched by a corresponding switch to"
        elif self.get_time() < self.corresp_start_event.get_time():
            self.erroneous = True
            #print "switching away from a processor before we switched to it?!"
        
        super(SwitchAwayEvent, self).scan(cur_cpu, switches)
            
    def render(self, graph, layer, prev_events, selectable=False):
        if self.corresp_start_event is None:
            # We never found a corresponding start event. In that case, we can assume it lies
            # in some part of the trace that was never read in. So draw a bar starting from
            # the very beginning.
            if layer == self.layer:
                prev_events[self] = None
                cpu = self.get_cpu()
                task_no = self.get_job().get_task().get_task_no()
                start = self.get_job().get_task().get_schedule().start
                if selectable:
                    graph.add_sel_bar_at_time(start, self.get_time(),
                                     task_no, cpu, self)
                else:
                    graph.draw_bar_at_time(start, self.get_time(),
                                   task_no, cpu, self.get_job().get_job_no(),
                                   AlignMode.LEFT, self.is_selected())
        else:
            if self.corresp_start_event in prev_events:
                return # already rendered the bar
            self.corresp_start_event.render(graph, layer, prev_events, selectable)
        
class ReleaseEvent(Event):
    def __init__(self, time, cpu):
        super(ReleaseEvent, self).__init__(time, cpu)
        self.layer = Canvas.TOP_LAYER
    
    def __str__(self):
        return 'Release' + self._common_str() + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
        
    def scan(self, cur_cpu, switches):
        super(ReleaseEvent, self).scan(cur_cpu, switches)
        
    def render(self, graph, layer, prev_events, selectable=False):
        prev_events[self] = None
        if layer == Canvas.TOP_LAYER:
            if selectable:
                graph.add_sel_release_arrow_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                            self)
            else:
                graph.draw_release_arrow_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                             self.get_job().get_job_no(), self.is_selected())
            
                                             
class DeadlineEvent(Event):
    def __init__(self, time, cpu):
        super(DeadlineEvent, self).__init__(time, cpu)
        self.layer = Canvas.TOP_LAYER
    
    def __str__(self):
        return 'Deadline' + self._common_str() + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
            
    def scan(self, cur_cpu, switches):
        super(DeadlineEvent, self).scan(cur_cpu, switches)
        
    def render(self, graph, layer, prev_events, selectable=False):
        prev_events[self] = None
        if layer == Canvas.TOP_LAYER:
            if selectable:
                graph.add_sel_deadline_arrow_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                              self)
            else:
                graph.draw_deadline_arrow_at_time(self.get_time(), self.get_job().get_task().get_task_no(),
                                              self.get_job().get_job_no(), self.is_selected())
            

class InversionStartEvent(ErrorEvent):
    def __init__(self, time):
        super(InversionStartEvent, self).__init__(time, Event.NO_CPU)
        self.layer = Canvas.BOTTOM_LAYER
        self.corresp_end_event = None
    
    def __str__(self):
        if self.corresp_end_event is None:
            return 'Inversion Start (w/o Inversion End)' + self._common_str() \
                  + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
        return 'Priority Inversion' + self._common_str() + ', START=' \
               + util.format_float(self.get_time(), Event.NUM_DEC_PLACES) \
               + ', END=' + util.format_float(self.corresp_end_event.get_time(), Event.NUM_DEC_PLACES)
        
    def scan(self, cur_cpu, switches):
        switches[InversionStartEvent] = self
        self.corresp_end_event = None
        
        # the corresp_end_event should already be set
        super(InversionStartEvent, self).scan(cur_cpu, switches)
    
    def render(self, graph, layer, prev_events, selectable=False):
        if layer == self.layer:
            end_time = None
            clip = None
            if self.corresp_end_event is None:
                end_time = self.get_job().get_task().get_schedule().end
                clip = AlignMode.RIGHT
            else:
                end_time = self.corresp_end_event.get_time()
            
            if layer == self.layer:
                prev_events[self] = None
                cpu = self.get_cpu()
                task_no = self.get_job().get_task().get_task_no()
                if selectable:
                    graph.add_sel_mini_bar_at_time(self.get_time(), end_time,
                                     task_no, cpu, self)
                else:
                    graph.draw_mini_bar_at_time(self.get_time(), end_time,
                                     task_no, cpu, self.get_job().get_job_no(),
                                     clip, self.is_selected())
            

class InversionEndEvent(ErrorEvent):
    def __init__(self, time):
        super(InversionEndEvent, self).__init__(time, Event.NO_CPU)
        self.layer = Canvas.BOTTOM_LAYER
        self.corresp_start_event = None
    
    def __str__(self):
        if self.corresp_start_event is None:
            return 'Inversion End (w/o Inversion Start)' + self._common_str() \
                  + ', TIME=' + util.format_float(self.get_time(), Event.NUM_DEC_PLACES)
        
        return str(self.corresp_start_event)
                      
    def scan(self, cur_cpu, switches):
        self.corresp_start_event = switches[InversionStartEvent]
        
        cur_cpu[0] = Event.NO_CPU
        switches[InversionStartEvent] = None
        
        if self.corresp_start_event is not None:
            self.corresp_start_event.corresp_end_event = self
            
        if self.corresp_start_event is None:
            self.erroneous = True
            print "inversion end was not matched by a corresponding inversion start"
        
        super(InversionEndEvent, self).scan(cur_cpu, switches)
        
    def render(self, graph, layer, prev_events, selectable=False):
        if self.corresp_start_event is None:
            # We never found a corresponding start event. In that case, we can assume it lies
            # in some part of the trace that was never read in. So draw a bar starting from
            # the very beginning.
            if layer == self.layer:
                prev_events[self] = None
                cpu = self.get_cpu()
                task_no = self.get_job().get_task().get_task_no()
                start = self.get_job().get_task().get_schedule().start
                if selectable:
                    graph.add_sel_mini_bar_at_time(start, self.get_time(),
                                     task_no, cpu, self)
                else:
                    graph.draw_mini_bar_at_time(start, self.get_time(),
                                   task_no, cpu, self.get_job().get_job_no(),
                                   AlignMode.LEFT, self.is_selected())
        else:
            if self.corresp_start_event in prev_events:
                return # already rendered the bar
            self.corresp_start_event.render(graph, layer, prev_events, selectable)
        
class InversionDummy(DummyEvent):
    def __init__(self, time, cpu):
        super(InversionDummy, self).__init__(time, Event.NO_CPU)
        self.layer = Canvas.BOTTOM_LAYER
        
    def render(self, graph, layer, prev_events, selectable=False):
        if self.corresp_start_event is None:
            if self.corresp_end_event in prev_events:
                return # we have already been rendered
            self.corresp_end_event.render(graph, layer, prev_events, selectable)
        else:    
            if self.corresp_start_event in prev_events:
                return # we have already been rendered
            self.corresp_start_event.render(graph, layer, prev_events, selectable)
        
class IsRunningDummy(DummyEvent):
    def __init__(self, time, cpu):
        super(IsRunningDummy, self).__init__(time, Event.NO_CPU)
        self.layer = Canvas.BOTTOM_LAYER
        
    def render(self, graph, layer, prev_events, selectable=False):
        if self.corresp_start_event is None:
            if self.corresp_end_event in prev_events:
                return # we have already been rendered
            self.corresp_end_event.render(graph, layer, prev_events, selectable)
        else:
            if self.corresp_start_event in prev_events:
                return # we have already been rendered
            self.corresp_start_event.render(graph, layer, prev_events, selectable)

EVENT_LIST = {SuspendEvent : None, ResumeEvent : None, CompleteEvent : None,
              SwitchAwayEvent : None, SwitchToEvent : None, ReleaseEvent : None,
              DeadlineEvent : None, IsRunningDummy : None,
              InversionStartEvent : None, InversionEndEvent : None,
              InversionDummy : None}

SPAN_START_EVENTS = { SwitchToEvent : IsRunningDummy, InversionStartEvent : InversionDummy }
SPAN_END_EVENTS = { SwitchAwayEvent : IsRunningDummy, InversionEndEvent : InversionDummy}