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