blob: 8b8fb7c60991b57be2b08c6b295b1de674453dab [file] [log] [blame]
Frederic Weisbecker880d22f2010-07-20 21:55:33 +02001#!/usr/bin/python
2#
3# Cpu task migration overview toy
4#
5# Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com>
6#
7# perf trace event handlers have been generated by perf trace -g python
8#
9# The whole is licensed under the terms of the GNU GPL License version 2
10
11
12try:
13 import wx
14except ImportError:
15 raise ImportError, "You need to install the wxpython lib for this script"
16
17import os
18import sys
19
20from collections import defaultdict
21from UserList import UserList
22
23sys.path.append(os.environ['PERF_EXEC_PATH'] + \
24 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
25
26from perf_trace_context import *
27from Core import *
28
29class RootFrame(wx.Frame):
30 def __init__(self, timeslices, parent = None, id = -1, title = "Migration"):
31 wx.Frame.__init__(self, parent, id, title)
32
33 (self.screen_width, self.screen_height) = wx.GetDisplaySize()
34 self.screen_width -= 10
35 self.screen_height -= 10
36 self.zoom = 0.5
37 self.scroll_scale = 20
38 self.timeslices = timeslices
39 (self.ts_start, self.ts_end) = timeslices.interval()
40 self.update_width_virtual()
41
42 # whole window panel
43 self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height))
44
45 # scrollable container
46 self.scroll = wx.ScrolledWindow(self.panel)
47 self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, 100 / 10)
48 self.scroll.EnableScrolling(True, True)
49 self.scroll.SetFocus()
50
51 # scrollable drawing area
52 self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width, self.screen_height / 2))
53 self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint)
54 self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
55 self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
56 self.scroll.Bind(wx.EVT_PAINT, self.on_paint)
Nikhil Raobe6d947692010-07-21 19:46:11 -070057 self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
58 self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
Frederic Weisbecker880d22f2010-07-20 21:55:33 +020059
60 self.scroll.Fit()
61 self.Fit()
62
63 self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, -1, wx.SIZE_USE_EXISTING)
64
65 self.max_cpu = -1
66 self.txt = None
67
68 self.Show(True)
69
70 def us_to_px(self, val):
71 return val / (10 ** 3) * self.zoom
72
73 def px_to_us(self, val):
74 return (val / self.zoom) * (10 ** 3)
75
76 def scroll_start(self):
77 (x, y) = self.scroll.GetViewStart()
78 return (x * self.scroll_scale, y * self.scroll_scale)
79
80 def scroll_start_us(self):
81 (x, y) = self.scroll_start()
82 return self.px_to_us(x)
83
84 def update_rectangle_cpu(self, dc, slice, cpu, offset_time):
85 rq = slice.rqs[cpu]
86
87 if slice.total_load != 0:
88 load_rate = rq.load() / float(slice.total_load)
89 else:
90 load_rate = 0
91
92
93 offset_px = self.us_to_px(slice.start - offset_time)
94 width_px = self.us_to_px(slice.end - slice.start)
95 (x, y) = self.scroll_start()
96
97 if width_px == 0:
98 return
99
100 offset_py = 100 + (cpu * 150)
101 width_py = 100
102
103 if cpu in slice.event_cpus:
104 rgb = rq.event.color()
105 if rgb is not None:
106 (r, g, b) = rgb
107 color = wx.Colour(r, g, b)
108 brush = wx.Brush(color, wx.SOLID)
109 dc.SetBrush(brush)
110 dc.DrawRectangle(offset_px, offset_py, width_px, 5)
111 width_py -= 5
112 offset_py += 5
113
114 red_power = int(0xff - (0xff * load_rate))
115 color = wx.Colour(0xff, red_power, red_power)
116 brush = wx.Brush(color, wx.SOLID)
117 dc.SetBrush(brush)
118 dc.DrawRectangle(offset_px, offset_py, width_px, width_py)
119
120 def update_rectangles(self, dc, start, end):
121 if len(self.timeslices) == 0:
122 return
123 start += self.timeslices[0].start
124 end += self.timeslices[0].start
125
126 color = wx.Colour(0, 0, 0)
127 brush = wx.Brush(color, wx.SOLID)
128 dc.SetBrush(brush)
129
130 i = self.timeslices.find_time_slice(start)
131 if i == -1:
132 return
133
134 for i in xrange(i, len(self.timeslices)):
135 timeslice = self.timeslices[i]
136 if timeslice.start > end:
137 return
138
139 for cpu in timeslice.rqs:
140 self.update_rectangle_cpu(dc, timeslice, cpu, self.timeslices[0].start)
141 if cpu > self.max_cpu:
142 self.max_cpu = cpu
143
144 def on_paint(self, event):
145 color = wx.Colour(0xff, 0xff, 0xff)
146 brush = wx.Brush(color, wx.SOLID)
147 dc = wx.PaintDC(self.scroll_panel)
148 dc.SetBrush(brush)
149
150 width = min(self.width_virtual, self.screen_width)
151 (x, y) = self.scroll_start()
152 start = self.px_to_us(x)
153 end = self.px_to_us(x + width)
154 self.update_rectangles(dc, start, end)
155
156 def cpu_from_ypixel(self, y):
157 y -= 100
158 cpu = y / 150
159 height = y % 150
160
161 if cpu < 0 or cpu > self.max_cpu or height > 100:
162 return -1
163
164 return cpu
165
166 def update_summary(self, cpu, t):
167 idx = self.timeslices.find_time_slice(t)
168 if idx == -1:
169 return
170
171 ts = self.timeslices[idx]
172 rq = ts.rqs[cpu]
173 raw = "CPU: %d\n" % cpu
174 raw += "Last event : %s\n" % rq.event.__repr__()
175 raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
176 raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
177 raw += "Load = %d\n" % rq.load()
178 for t in rq.tasks:
179 raw += "%s \n" % thread_name(t)
180
181 if self.txt:
182 self.txt.Destroy()
183 self.txt = wx.StaticText(self.panel, -1, raw, (0, (self.screen_height / 2) + 50))
184
185
186 def on_mouse_down(self, event):
187 (x, y) = event.GetPositionTuple()
188 cpu = self.cpu_from_ypixel(y)
189 if cpu == -1:
190 return
191
192 t = self.px_to_us(x) + self.timeslices[0].start
193
194 self.update_summary(cpu, t)
195
196
197 def update_width_virtual(self):
198 self.width_virtual = self.us_to_px(self.ts_end - self.ts_start)
199
200 def __zoom(self, x):
201 self.update_width_virtual()
202 (xpos, ypos) = self.scroll.GetViewStart()
203 xpos = self.us_to_px(x) / self.scroll_scale
204 self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, 100 / 10, xpos, ypos)
205 self.Refresh()
206
207 def zoom_in(self):
208 x = self.scroll_start_us()
209 self.zoom *= 2
210 self.__zoom(x)
211
212 def zoom_out(self):
213 x = self.scroll_start_us()
214 self.zoom /= 2
215 self.__zoom(x)
216
217
218 def on_key_press(self, event):
219 key = event.GetRawKeyCode()
220 if key == ord("+"):
221 self.zoom_in()
222 return
223 if key == ord("-"):
224 self.zoom_out()
225 return
226
227 key = event.GetKeyCode()
228 (x, y) = self.scroll.GetViewStart()
229 if key == wx.WXK_RIGHT:
230 self.scroll.Scroll(x + 1, y)
231 elif key == wx.WXK_LEFT:
232 self.scroll.Scroll(x -1, y)
233
234
235threads = { 0 : "idle"}
236
237def thread_name(pid):
238 return "%s:%d" % (threads[pid], pid)
239
240class EventHeaders:
241 def __init__(self, common_cpu, common_secs, common_nsecs,
242 common_pid, common_comm):
243 self.cpu = common_cpu
244 self.secs = common_secs
245 self.nsecs = common_nsecs
246 self.pid = common_pid
247 self.comm = common_comm
248
249 def ts(self):
250 return (self.secs * (10 ** 9)) + self.nsecs
251
252 def ts_format(self):
253 return "%d.%d" % (self.secs, int(self.nsecs / 1000))
254
255
256def taskState(state):
257 states = {
258 0 : "R",
259 1 : "S",
260 2 : "D",
261 64: "DEAD"
262 }
263
264 if state not in states:
Frederic Weisbecker207f90f2010-07-21 23:10:38 +0200265 return "Unknown"
Frederic Weisbecker880d22f2010-07-20 21:55:33 +0200266
267 return states[state]
268
269
270class RunqueueEventUnknown:
271 @staticmethod
272 def color():
273 return None
274
275 def __repr__(self):
276 return "unknown"
277
278class RunqueueEventSleep:
279 @staticmethod
280 def color():
281 return (0, 0, 0xff)
282
283 def __init__(self, sleeper):
284 self.sleeper = sleeper
285
286 def __repr__(self):
287 return "%s gone to sleep" % thread_name(self.sleeper)
288
289class RunqueueEventWakeup:
290 @staticmethod
291 def color():
292 return (0xff, 0xff, 0)
293
294 def __init__(self, wakee):
295 self.wakee = wakee
296
297 def __repr__(self):
298 return "%s woke up" % thread_name(self.wakee)
299
300class RunqueueEventFork:
301 @staticmethod
302 def color():
303 return (0, 0xff, 0)
304
305 def __init__(self, child):
306 self.child = child
307
308 def __repr__(self):
309 return "new forked task %s" % thread_name(self.child)
310
311class RunqueueMigrateIn:
312 @staticmethod
313 def color():
314 return (0, 0xf0, 0xff)
315
316 def __init__(self, new):
317 self.new = new
318
319 def __repr__(self):
320 return "task migrated in %s" % thread_name(self.new)
321
322class RunqueueMigrateOut:
323 @staticmethod
324 def color():
325 return (0xff, 0, 0xff)
326
327 def __init__(self, old):
328 self.old = old
329
330 def __repr__(self):
331 return "task migrated out %s" % thread_name(self.old)
332
333class RunqueueSnapshot:
334 def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
335 self.tasks = tuple(tasks)
336 self.event = event
337
338 def sched_switch(self, prev, prev_state, next):
339 event = RunqueueEventUnknown()
340
341 if taskState(prev_state) == "R" and next in self.tasks \
342 and prev in self.tasks:
343 return self
344
345 if taskState(prev_state) != "R":
346 event = RunqueueEventSleep(prev)
347
348 next_tasks = list(self.tasks[:])
349 if prev in self.tasks:
350 if taskState(prev_state) != "R":
351 next_tasks.remove(prev)
352 elif taskState(prev_state) == "R":
353 next_tasks.append(prev)
354
355 if next not in next_tasks:
356 next_tasks.append(next)
357
358 return RunqueueSnapshot(next_tasks, event)
359
360 def migrate_out(self, old):
361 if old not in self.tasks:
362 return self
363 next_tasks = [task for task in self.tasks if task != old]
364
365 return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
366
367 def __migrate_in(self, new, event):
368 if new in self.tasks:
369 self.event = event
370 return self
371 next_tasks = self.tasks[:] + tuple([new])
372
373 return RunqueueSnapshot(next_tasks, event)
374
375 def migrate_in(self, new):
376 return self.__migrate_in(new, RunqueueMigrateIn(new))
377
378 def wake_up(self, new):
379 return self.__migrate_in(new, RunqueueEventWakeup(new))
380
381 def wake_up_new(self, new):
382 return self.__migrate_in(new, RunqueueEventFork(new))
383
384 def load(self):
385 """ Provide the number of tasks on the runqueue.
386 Don't count idle"""
387 return len(self.tasks) - 1
388
389 def __repr__(self):
390 ret = self.tasks.__repr__()
391 ret += self.origin_tostring()
392
393 return ret
394
395class TimeSlice:
396 def __init__(self, start, prev):
397 self.start = start
398 self.prev = prev
399 self.end = start
400 # cpus that triggered the event
401 self.event_cpus = []
402 if prev is not None:
403 self.total_load = prev.total_load
404 self.rqs = prev.rqs.copy()
405 else:
406 self.rqs = defaultdict(RunqueueSnapshot)
407 self.total_load = 0
408
409 def __update_total_load(self, old_rq, new_rq):
410 diff = new_rq.load() - old_rq.load()
411 self.total_load += diff
412
413 def sched_switch(self, ts_list, prev, prev_state, next, cpu):
414 old_rq = self.prev.rqs[cpu]
415 new_rq = old_rq.sched_switch(prev, prev_state, next)
416
417 if old_rq is new_rq:
418 return
419
420 self.rqs[cpu] = new_rq
421 self.__update_total_load(old_rq, new_rq)
422 ts_list.append(self)
423 self.event_cpus = [cpu]
424
425 def migrate(self, ts_list, new, old_cpu, new_cpu):
426 if old_cpu == new_cpu:
427 return
428 old_rq = self.prev.rqs[old_cpu]
429 out_rq = old_rq.migrate_out(new)
430 self.rqs[old_cpu] = out_rq
431 self.__update_total_load(old_rq, out_rq)
432
433 new_rq = self.prev.rqs[new_cpu]
434 in_rq = new_rq.migrate_in(new)
435 self.rqs[new_cpu] = in_rq
436 self.__update_total_load(new_rq, in_rq)
437
438 ts_list.append(self)
Frederic Weisbecker749e5072010-07-21 22:45:51 +0200439
440 if old_rq is not out_rq:
441 self.event_cpus.append(old_cpu)
442 self.event_cpus.append(new_cpu)
Frederic Weisbecker880d22f2010-07-20 21:55:33 +0200443
444 def wake_up(self, ts_list, pid, cpu, fork):
445 old_rq = self.prev.rqs[cpu]
446 if fork:
447 new_rq = old_rq.wake_up_new(pid)
448 else:
449 new_rq = old_rq.wake_up(pid)
450
451 if new_rq is old_rq:
452 return
453 self.rqs[cpu] = new_rq
454 self.__update_total_load(old_rq, new_rq)
455 ts_list.append(self)
456 self.event_cpus = [cpu]
457
458 def next(self, t):
459 self.end = t
460 return TimeSlice(t, self)
461
462class TimeSliceList(UserList):
463 def __init__(self, arg = []):
464 self.data = arg
465
466 def get_time_slice(self, ts):
467 if len(self.data) == 0:
468 slice = TimeSlice(ts, TimeSlice(-1, None))
469 else:
470 slice = self.data[-1].next(ts)
471 return slice
472
473 def find_time_slice(self, ts):
474 start = 0
475 end = len(self.data)
476 found = -1
477 searching = True
478 while searching:
479 if start == end or start == end - 1:
480 searching = False
481
482 i = (end + start) / 2
483 if self.data[i].start <= ts and self.data[i].end >= ts:
484 found = i
485 end = i
486 continue
487
488 if self.data[i].end < ts:
489 start = i
490
491 elif self.data[i].start > ts:
492 end = i
493
494 return found
495
496 def interval(self):
497 if len(self.data) == 0:
498 return (0, 0)
499
500 return (self.data[0].start, self.data[-1].end)
501
502
503class SchedEventProxy:
504 def __init__(self):
505 self.current_tsk = defaultdict(lambda : -1)
506 self.timeslices = TimeSliceList()
507
508 def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
509 next_comm, next_pid, next_prio):
510 """ Ensure the task we sched out this cpu is really the one
511 we logged. Otherwise we may have missed traces """
512
513 on_cpu_task = self.current_tsk[headers.cpu]
514
515 if on_cpu_task != -1 and on_cpu_task != prev_pid:
516 print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
517 (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
518
519 threads[prev_pid] = prev_comm
520 threads[next_pid] = next_comm
521 self.current_tsk[headers.cpu] = next_pid
522
523 ts = self.timeslices.get_time_slice(headers.ts())
524 ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
525
526 def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
527 ts = self.timeslices.get_time_slice(headers.ts())
528 ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
529
530 def wake_up(self, headers, comm, pid, success, target_cpu, fork):
531 if success == 0:
532 return
533 ts = self.timeslices.get_time_slice(headers.ts())
534 ts.wake_up(self.timeslices, pid, target_cpu, fork)
535
536
537def trace_begin():
538 global parser
539 parser = SchedEventProxy()
540
541def trace_end():
542 app = wx.App(False)
543 timeslices = parser.timeslices
544 frame = RootFrame(timeslices)
545 app.MainLoop()
546
547def sched__sched_stat_runtime(event_name, context, common_cpu,
548 common_secs, common_nsecs, common_pid, common_comm,
549 comm, pid, runtime, vruntime):
550 pass
551
552def sched__sched_stat_iowait(event_name, context, common_cpu,
553 common_secs, common_nsecs, common_pid, common_comm,
554 comm, pid, delay):
555 pass
556
557def sched__sched_stat_sleep(event_name, context, common_cpu,
558 common_secs, common_nsecs, common_pid, common_comm,
559 comm, pid, delay):
560 pass
561
562def sched__sched_stat_wait(event_name, context, common_cpu,
563 common_secs, common_nsecs, common_pid, common_comm,
564 comm, pid, delay):
565 pass
566
567def sched__sched_process_fork(event_name, context, common_cpu,
568 common_secs, common_nsecs, common_pid, common_comm,
569 parent_comm, parent_pid, child_comm, child_pid):
570 pass
571
572def sched__sched_process_wait(event_name, context, common_cpu,
573 common_secs, common_nsecs, common_pid, common_comm,
574 comm, pid, prio):
575 pass
576
577def sched__sched_process_exit(event_name, context, common_cpu,
578 common_secs, common_nsecs, common_pid, common_comm,
579 comm, pid, prio):
580 pass
581
582def sched__sched_process_free(event_name, context, common_cpu,
583 common_secs, common_nsecs, common_pid, common_comm,
584 comm, pid, prio):
585 pass
586
587def sched__sched_migrate_task(event_name, context, common_cpu,
588 common_secs, common_nsecs, common_pid, common_comm,
589 comm, pid, prio, orig_cpu,
590 dest_cpu):
591 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
592 common_pid, common_comm)
593 parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
594
595def sched__sched_switch(event_name, context, common_cpu,
596 common_secs, common_nsecs, common_pid, common_comm,
597 prev_comm, prev_pid, prev_prio, prev_state,
598 next_comm, next_pid, next_prio):
599
600 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
601 common_pid, common_comm)
602 parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
603 next_comm, next_pid, next_prio)
604
605def sched__sched_wakeup_new(event_name, context, common_cpu,
606 common_secs, common_nsecs, common_pid, common_comm,
607 comm, pid, prio, success,
608 target_cpu):
609 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
610 common_pid, common_comm)
611 parser.wake_up(headers, comm, pid, success, target_cpu, 1)
612
613def sched__sched_wakeup(event_name, context, common_cpu,
614 common_secs, common_nsecs, common_pid, common_comm,
615 comm, pid, prio, success,
616 target_cpu):
617 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
618 common_pid, common_comm)
619 parser.wake_up(headers, comm, pid, success, target_cpu, 0)
620
621def sched__sched_wait_task(event_name, context, common_cpu,
622 common_secs, common_nsecs, common_pid, common_comm,
623 comm, pid, prio):
624 pass
625
626def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
627 common_secs, common_nsecs, common_pid, common_comm,
628 ret):
629 pass
630
631def sched__sched_kthread_stop(event_name, context, common_cpu,
632 common_secs, common_nsecs, common_pid, common_comm,
633 comm, pid):
634 pass
635
636def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
637 common_pid, common_comm):
638 pass