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