3 # Cpu task migration overview toy
5 # Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com>
7 # perf trace event handlers have been generated by perf trace -g python
9 # The whole is licensed under the terms of the GNU GPL License version 2
15 raise ImportError, "You need to install the wxpython lib for this script"
20 from collections import defaultdict
21 from UserList import UserList
23 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
24 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
26 from perf_trace_context import *
29 class RootFrame(wx.Frame):
33 EVENT_MARKING_WIDTH = 5
35 def __init__(self, sched_tracer, title, parent = None, id = -1):
36 wx.Frame.__init__(self, parent, id, title)
38 (self.screen_width, self.screen_height) = wx.GetDisplaySize()
39 self.screen_width -= 10
40 self.screen_height -= 10
42 self.scroll_scale = 20
43 self.sched_tracer = sched_tracer
44 self.sched_tracer.set_root_win(self)
45 (self.ts_start, self.ts_end) = sched_tracer.interval()
46 self.update_width_virtual()
47 self.nr_rects = sched_tracer.nr_rectangles() + 1
48 self.height_virtual = RootFrame.Y_OFFSET + (self.nr_rects * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))
51 self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height))
53 # scrollable container
54 self.scroll = wx.ScrolledWindow(self.panel)
55 self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale)
56 self.scroll.EnableScrolling(True, True)
57 self.scroll.SetFocus()
59 # scrollable drawing area
60 self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width - 15, self.screen_height / 2))
61 self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint)
62 self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
63 self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
64 self.scroll.Bind(wx.EVT_PAINT, self.on_paint)
65 self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
66 self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
71 self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, self.height_virtual, wx.SIZE_USE_EXISTING)
77 def us_to_px(self, val):
78 return val / (10 ** 3) * self.zoom
80 def px_to_us(self, val):
81 return (val / self.zoom) * (10 ** 3)
83 def scroll_start(self):
84 (x, y) = self.scroll.GetViewStart()
85 return (x * self.scroll_scale, y * self.scroll_scale)
87 def scroll_start_us(self):
88 (x, y) = self.scroll_start()
89 return self.px_to_us(x)
91 def paint_rectangle_zone(self, nr, color, top_color, start, end):
92 offset_px = self.us_to_px(start - self.ts_start)
93 width_px = self.us_to_px(end - self.ts_start)
95 offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))
96 width_py = RootFrame.RECT_HEIGHT
100 if top_color is not None:
101 (r, g, b) = top_color
102 top_color = wx.Colour(r, g, b)
103 brush = wx.Brush(top_color, wx.SOLID)
105 dc.DrawRectangle(offset_px, offset_py, width_px, RootFrame.EVENT_MARKING_WIDTH)
106 width_py -= RootFrame.EVENT_MARKING_WIDTH
107 offset_py += RootFrame.EVENT_MARKING_WIDTH
110 color = wx.Colour(r, g, b)
111 brush = wx.Brush(color, wx.SOLID)
113 dc.DrawRectangle(offset_px, offset_py, width_px, width_py)
115 def update_rectangles(self, dc, start, end):
116 start += self.ts_start
118 self.sched_tracer.fill_zone(start, end)
120 def on_paint(self, event):
121 dc = wx.PaintDC(self.scroll_panel)
124 width = min(self.width_virtual, self.screen_width)
125 (x, y) = self.scroll_start()
126 start = self.px_to_us(x)
127 end = self.px_to_us(x + width)
128 self.update_rectangles(dc, start, end)
130 def rect_from_ypixel(self, y):
131 y -= RootFrame.Y_OFFSET
132 rect = y / (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)
133 height = y % (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)
135 if rect < 0 or rect > self.nr_rects - 1 or height > RootFrame.RECT_HEIGHT:
140 def update_summary(self, txt):
143 self.txt = wx.StaticText(self.panel, -1, txt, (0, (self.screen_height / 2) + 50))
146 def on_mouse_down(self, event):
147 (x, y) = event.GetPositionTuple()
148 rect = self.rect_from_ypixel(y)
152 t = self.px_to_us(x) + self.ts_start
154 self.sched_tracer.mouse_down(rect, t)
157 def update_width_virtual(self):
158 self.width_virtual = self.us_to_px(self.ts_end - self.ts_start)
161 self.update_width_virtual()
162 (xpos, ypos) = self.scroll.GetViewStart()
163 xpos = self.us_to_px(x) / self.scroll_scale
164 self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale, xpos, ypos)
168 x = self.scroll_start_us()
173 x = self.scroll_start_us()
178 def on_key_press(self, event):
179 key = event.GetRawKeyCode()
187 key = event.GetKeyCode()
188 (x, y) = self.scroll.GetViewStart()
189 if key == wx.WXK_RIGHT:
190 self.scroll.Scroll(x + 1, y)
191 elif key == wx.WXK_LEFT:
192 self.scroll.Scroll(x - 1, y)
193 elif key == wx.WXK_DOWN:
194 self.scroll.Scroll(x, y + 1)
195 elif key == wx.WXK_UP:
196 self.scroll.Scroll(x, y - 1)
199 threads = { 0 : "idle"}
201 def thread_name(pid):
202 return "%s:%d" % (threads[pid], pid)
205 def __init__(self, common_cpu, common_secs, common_nsecs,
206 common_pid, common_comm):
207 self.cpu = common_cpu
208 self.secs = common_secs
209 self.nsecs = common_nsecs
210 self.pid = common_pid
211 self.comm = common_comm
214 return (self.secs * (10 ** 9)) + self.nsecs
217 return "%d.%d" % (self.secs, int(self.nsecs / 1000))
220 def taskState(state):
228 if state not in states:
234 class RunqueueEventUnknown:
242 class RunqueueEventSleep:
247 def __init__(self, sleeper):
248 self.sleeper = sleeper
251 return "%s gone to sleep" % thread_name(self.sleeper)
253 class RunqueueEventWakeup:
256 return (0xff, 0xff, 0)
258 def __init__(self, wakee):
262 return "%s woke up" % thread_name(self.wakee)
264 class RunqueueEventFork:
269 def __init__(self, child):
273 return "new forked task %s" % thread_name(self.child)
275 class RunqueueMigrateIn:
278 return (0, 0xf0, 0xff)
280 def __init__(self, new):
284 return "task migrated in %s" % thread_name(self.new)
286 class RunqueueMigrateOut:
289 return (0xff, 0, 0xff)
291 def __init__(self, old):
295 return "task migrated out %s" % thread_name(self.old)
297 class RunqueueSnapshot:
298 def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
299 self.tasks = tuple(tasks)
302 def sched_switch(self, prev, prev_state, next):
303 event = RunqueueEventUnknown()
305 if taskState(prev_state) == "R" and next in self.tasks \
306 and prev in self.tasks:
309 if taskState(prev_state) != "R":
310 event = RunqueueEventSleep(prev)
312 next_tasks = list(self.tasks[:])
313 if prev in self.tasks:
314 if taskState(prev_state) != "R":
315 next_tasks.remove(prev)
316 elif taskState(prev_state) == "R":
317 next_tasks.append(prev)
319 if next not in next_tasks:
320 next_tasks.append(next)
322 return RunqueueSnapshot(next_tasks, event)
324 def migrate_out(self, old):
325 if old not in self.tasks:
327 next_tasks = [task for task in self.tasks if task != old]
329 return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
331 def __migrate_in(self, new, event):
332 if new in self.tasks:
335 next_tasks = self.tasks[:] + tuple([new])
337 return RunqueueSnapshot(next_tasks, event)
339 def migrate_in(self, new):
340 return self.__migrate_in(new, RunqueueMigrateIn(new))
342 def wake_up(self, new):
343 return self.__migrate_in(new, RunqueueEventWakeup(new))
345 def wake_up_new(self, new):
346 return self.__migrate_in(new, RunqueueEventFork(new))
349 """ Provide the number of tasks on the runqueue.
351 return len(self.tasks) - 1
354 ret = self.tasks.__repr__()
355 ret += self.origin_tostring()
360 def __init__(self, start, prev):
364 # cpus that triggered the event
367 self.total_load = prev.total_load
368 self.rqs = prev.rqs.copy()
370 self.rqs = defaultdict(RunqueueSnapshot)
373 def __update_total_load(self, old_rq, new_rq):
374 diff = new_rq.load() - old_rq.load()
375 self.total_load += diff
377 def sched_switch(self, ts_list, prev, prev_state, next, cpu):
378 old_rq = self.prev.rqs[cpu]
379 new_rq = old_rq.sched_switch(prev, prev_state, next)
384 self.rqs[cpu] = new_rq
385 self.__update_total_load(old_rq, new_rq)
387 self.event_cpus = [cpu]
389 def migrate(self, ts_list, new, old_cpu, new_cpu):
390 if old_cpu == new_cpu:
392 old_rq = self.prev.rqs[old_cpu]
393 out_rq = old_rq.migrate_out(new)
394 self.rqs[old_cpu] = out_rq
395 self.__update_total_load(old_rq, out_rq)
397 new_rq = self.prev.rqs[new_cpu]
398 in_rq = new_rq.migrate_in(new)
399 self.rqs[new_cpu] = in_rq
400 self.__update_total_load(new_rq, in_rq)
404 if old_rq is not out_rq:
405 self.event_cpus.append(old_cpu)
406 self.event_cpus.append(new_cpu)
408 def wake_up(self, ts_list, pid, cpu, fork):
409 old_rq = self.prev.rqs[cpu]
411 new_rq = old_rq.wake_up_new(pid)
413 new_rq = old_rq.wake_up(pid)
417 self.rqs[cpu] = new_rq
418 self.__update_total_load(old_rq, new_rq)
420 self.event_cpus = [cpu]
424 return TimeSlice(t, self)
426 class TimeSliceList(UserList):
427 def __init__(self, arg = []):
430 def get_time_slice(self, ts):
431 if len(self.data) == 0:
432 slice = TimeSlice(ts, TimeSlice(-1, None))
434 slice = self.data[-1].next(ts)
437 def find_time_slice(self, ts):
443 if start == end or start == end - 1:
446 i = (end + start) / 2
447 if self.data[i].start <= ts and self.data[i].end >= ts:
452 if self.data[i].end < ts:
455 elif self.data[i].start > ts:
460 def set_root_win(self, win):
463 def mouse_down(self, cpu, t):
464 idx = self.find_time_slice(t)
470 raw = "CPU: %d\n" % cpu
471 raw += "Last event : %s\n" % rq.event.__repr__()
472 raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
473 raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
474 raw += "Load = %d\n" % rq.load()
476 raw += "%s \n" % thread_name(t)
478 self.root_win.update_summary(raw)
480 def update_rectangle_cpu(self, slice, cpu):
483 if slice.total_load != 0:
484 load_rate = rq.load() / float(slice.total_load)
488 red_power = int(0xff - (0xff * load_rate))
489 color = (0xff, red_power, red_power)
493 if cpu in slice.event_cpus:
494 top_color = rq.event.color()
496 self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
498 def fill_zone(self, start, end):
499 i = self.find_time_slice(start)
503 for i in xrange(i, len(self.data)):
504 timeslice = self.data[i]
505 if timeslice.start > end:
508 for cpu in timeslice.rqs:
509 self.update_rectangle_cpu(timeslice, cpu)
512 if len(self.data) == 0:
515 return (self.data[0].start, self.data[-1].end)
517 def nr_rectangles(self):
518 last_ts = self.data[-1]
520 for cpu in last_ts.rqs:
526 class SchedEventProxy:
528 self.current_tsk = defaultdict(lambda : -1)
529 self.timeslices = TimeSliceList()
531 def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
532 next_comm, next_pid, next_prio):
533 """ Ensure the task we sched out this cpu is really the one
534 we logged. Otherwise we may have missed traces """
536 on_cpu_task = self.current_tsk[headers.cpu]
538 if on_cpu_task != -1 and on_cpu_task != prev_pid:
539 print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
540 (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
542 threads[prev_pid] = prev_comm
543 threads[next_pid] = next_comm
544 self.current_tsk[headers.cpu] = next_pid
546 ts = self.timeslices.get_time_slice(headers.ts())
547 ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
549 def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
550 ts = self.timeslices.get_time_slice(headers.ts())
551 ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
553 def wake_up(self, headers, comm, pid, success, target_cpu, fork):
556 ts = self.timeslices.get_time_slice(headers.ts())
557 ts.wake_up(self.timeslices, pid, target_cpu, fork)
562 parser = SchedEventProxy()
566 timeslices = parser.timeslices
567 frame = RootFrame(timeslices, "Migration")
570 def sched__sched_stat_runtime(event_name, context, common_cpu,
571 common_secs, common_nsecs, common_pid, common_comm,
572 comm, pid, runtime, vruntime):
575 def sched__sched_stat_iowait(event_name, context, common_cpu,
576 common_secs, common_nsecs, common_pid, common_comm,
580 def sched__sched_stat_sleep(event_name, context, common_cpu,
581 common_secs, common_nsecs, common_pid, common_comm,
585 def sched__sched_stat_wait(event_name, context, common_cpu,
586 common_secs, common_nsecs, common_pid, common_comm,
590 def sched__sched_process_fork(event_name, context, common_cpu,
591 common_secs, common_nsecs, common_pid, common_comm,
592 parent_comm, parent_pid, child_comm, child_pid):
595 def sched__sched_process_wait(event_name, context, common_cpu,
596 common_secs, common_nsecs, common_pid, common_comm,
600 def sched__sched_process_exit(event_name, context, common_cpu,
601 common_secs, common_nsecs, common_pid, common_comm,
605 def sched__sched_process_free(event_name, context, common_cpu,
606 common_secs, common_nsecs, common_pid, common_comm,
610 def sched__sched_migrate_task(event_name, context, common_cpu,
611 common_secs, common_nsecs, common_pid, common_comm,
612 comm, pid, prio, orig_cpu,
614 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
615 common_pid, common_comm)
616 parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
618 def sched__sched_switch(event_name, context, common_cpu,
619 common_secs, common_nsecs, common_pid, common_comm,
620 prev_comm, prev_pid, prev_prio, prev_state,
621 next_comm, next_pid, next_prio):
623 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
624 common_pid, common_comm)
625 parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
626 next_comm, next_pid, next_prio)
628 def sched__sched_wakeup_new(event_name, context, common_cpu,
629 common_secs, common_nsecs, common_pid, common_comm,
630 comm, pid, prio, success,
632 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
633 common_pid, common_comm)
634 parser.wake_up(headers, comm, pid, success, target_cpu, 1)
636 def sched__sched_wakeup(event_name, context, common_cpu,
637 common_secs, common_nsecs, common_pid, common_comm,
638 comm, pid, prio, success,
640 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
641 common_pid, common_comm)
642 parser.wake_up(headers, comm, pid, success, target_cpu, 0)
644 def sched__sched_wait_task(event_name, context, common_cpu,
645 common_secs, common_nsecs, common_pid, common_comm,
649 def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
650 common_secs, common_nsecs, common_pid, common_comm,
654 def sched__sched_kthread_stop(event_name, context, common_cpu,
655 common_secs, common_nsecs, common_pid, common_comm,
659 def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
660 common_pid, common_comm):