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 # This software is distributed under the terms of the GNU General
10 # Public License ("GPL") version 2 as published by the Free Software
17 from collections import defaultdict
18 from UserList import UserList
20 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
21 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
22 sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace')
24 from perf_trace_context import *
26 from SchedGui import *
29 threads = { 0 : "idle"}
32 return "%s:%d" % (threads[pid], pid)
35 def __init__(self, common_cpu, common_secs, common_nsecs,
36 common_pid, common_comm):
38 self.secs = common_secs
39 self.nsecs = common_nsecs
41 self.comm = common_comm
44 return (self.secs * (10 ** 9)) + self.nsecs
47 return "%d.%d" % (self.secs, int(self.nsecs / 1000))
58 if state not in states:
64 class RunqueueEventUnknown:
72 class RunqueueEventSleep:
77 def __init__(self, sleeper):
78 self.sleeper = sleeper
81 return "%s gone to sleep" % thread_name(self.sleeper)
83 class RunqueueEventWakeup:
86 return (0xff, 0xff, 0)
88 def __init__(self, wakee):
92 return "%s woke up" % thread_name(self.wakee)
94 class RunqueueEventFork:
99 def __init__(self, child):
103 return "new forked task %s" % thread_name(self.child)
105 class RunqueueMigrateIn:
108 return (0, 0xf0, 0xff)
110 def __init__(self, new):
114 return "task migrated in %s" % thread_name(self.new)
116 class RunqueueMigrateOut:
119 return (0xff, 0, 0xff)
121 def __init__(self, old):
125 return "task migrated out %s" % thread_name(self.old)
127 class RunqueueSnapshot:
128 def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
129 self.tasks = tuple(tasks)
132 def sched_switch(self, prev, prev_state, next):
133 event = RunqueueEventUnknown()
135 if taskState(prev_state) == "R" and next in self.tasks \
136 and prev in self.tasks:
139 if taskState(prev_state) != "R":
140 event = RunqueueEventSleep(prev)
142 next_tasks = list(self.tasks[:])
143 if prev in self.tasks:
144 if taskState(prev_state) != "R":
145 next_tasks.remove(prev)
146 elif taskState(prev_state) == "R":
147 next_tasks.append(prev)
149 if next not in next_tasks:
150 next_tasks.append(next)
152 return RunqueueSnapshot(next_tasks, event)
154 def migrate_out(self, old):
155 if old not in self.tasks:
157 next_tasks = [task for task in self.tasks if task != old]
159 return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
161 def __migrate_in(self, new, event):
162 if new in self.tasks:
165 next_tasks = self.tasks[:] + tuple([new])
167 return RunqueueSnapshot(next_tasks, event)
169 def migrate_in(self, new):
170 return self.__migrate_in(new, RunqueueMigrateIn(new))
172 def wake_up(self, new):
173 return self.__migrate_in(new, RunqueueEventWakeup(new))
175 def wake_up_new(self, new):
176 return self.__migrate_in(new, RunqueueEventFork(new))
179 """ Provide the number of tasks on the runqueue.
181 return len(self.tasks) - 1
184 ret = self.tasks.__repr__()
185 ret += self.origin_tostring()
190 def __init__(self, start, prev):
194 # cpus that triggered the event
197 self.total_load = prev.total_load
198 self.rqs = prev.rqs.copy()
200 self.rqs = defaultdict(RunqueueSnapshot)
203 def __update_total_load(self, old_rq, new_rq):
204 diff = new_rq.load() - old_rq.load()
205 self.total_load += diff
207 def sched_switch(self, ts_list, prev, prev_state, next, cpu):
208 old_rq = self.prev.rqs[cpu]
209 new_rq = old_rq.sched_switch(prev, prev_state, next)
214 self.rqs[cpu] = new_rq
215 self.__update_total_load(old_rq, new_rq)
217 self.event_cpus = [cpu]
219 def migrate(self, ts_list, new, old_cpu, new_cpu):
220 if old_cpu == new_cpu:
222 old_rq = self.prev.rqs[old_cpu]
223 out_rq = old_rq.migrate_out(new)
224 self.rqs[old_cpu] = out_rq
225 self.__update_total_load(old_rq, out_rq)
227 new_rq = self.prev.rqs[new_cpu]
228 in_rq = new_rq.migrate_in(new)
229 self.rqs[new_cpu] = in_rq
230 self.__update_total_load(new_rq, in_rq)
234 if old_rq is not out_rq:
235 self.event_cpus.append(old_cpu)
236 self.event_cpus.append(new_cpu)
238 def wake_up(self, ts_list, pid, cpu, fork):
239 old_rq = self.prev.rqs[cpu]
241 new_rq = old_rq.wake_up_new(pid)
243 new_rq = old_rq.wake_up(pid)
247 self.rqs[cpu] = new_rq
248 self.__update_total_load(old_rq, new_rq)
250 self.event_cpus = [cpu]
254 return TimeSlice(t, self)
256 class TimeSliceList(UserList):
257 def __init__(self, arg = []):
260 def get_time_slice(self, ts):
261 if len(self.data) == 0:
262 slice = TimeSlice(ts, TimeSlice(-1, None))
264 slice = self.data[-1].next(ts)
267 def find_time_slice(self, ts):
273 if start == end or start == end - 1:
276 i = (end + start) / 2
277 if self.data[i].start <= ts and self.data[i].end >= ts:
282 if self.data[i].end < ts:
285 elif self.data[i].start > ts:
290 def set_root_win(self, win):
293 def mouse_down(self, cpu, t):
294 idx = self.find_time_slice(t)
300 raw = "CPU: %d\n" % cpu
301 raw += "Last event : %s\n" % rq.event.__repr__()
302 raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
303 raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
304 raw += "Load = %d\n" % rq.load()
306 raw += "%s \n" % thread_name(t)
308 self.root_win.update_summary(raw)
310 def update_rectangle_cpu(self, slice, cpu):
313 if slice.total_load != 0:
314 load_rate = rq.load() / float(slice.total_load)
318 red_power = int(0xff - (0xff * load_rate))
319 color = (0xff, red_power, red_power)
323 if cpu in slice.event_cpus:
324 top_color = rq.event.color()
326 self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
328 def fill_zone(self, start, end):
329 i = self.find_time_slice(start)
333 for i in xrange(i, len(self.data)):
334 timeslice = self.data[i]
335 if timeslice.start > end:
338 for cpu in timeslice.rqs:
339 self.update_rectangle_cpu(timeslice, cpu)
342 if len(self.data) == 0:
345 return (self.data[0].start, self.data[-1].end)
347 def nr_rectangles(self):
348 last_ts = self.data[-1]
350 for cpu in last_ts.rqs:
356 class SchedEventProxy:
358 self.current_tsk = defaultdict(lambda : -1)
359 self.timeslices = TimeSliceList()
361 def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
362 next_comm, next_pid, next_prio):
363 """ Ensure the task we sched out this cpu is really the one
364 we logged. Otherwise we may have missed traces """
366 on_cpu_task = self.current_tsk[headers.cpu]
368 if on_cpu_task != -1 and on_cpu_task != prev_pid:
369 print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
370 (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
372 threads[prev_pid] = prev_comm
373 threads[next_pid] = next_comm
374 self.current_tsk[headers.cpu] = next_pid
376 ts = self.timeslices.get_time_slice(headers.ts())
377 ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
379 def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
380 ts = self.timeslices.get_time_slice(headers.ts())
381 ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
383 def wake_up(self, headers, comm, pid, success, target_cpu, fork):
386 ts = self.timeslices.get_time_slice(headers.ts())
387 ts.wake_up(self.timeslices, pid, target_cpu, fork)
392 parser = SchedEventProxy()
396 timeslices = parser.timeslices
397 frame = RootFrame(timeslices, "Migration")
400 def sched__sched_stat_runtime(event_name, context, common_cpu,
401 common_secs, common_nsecs, common_pid, common_comm,
402 comm, pid, runtime, vruntime):
405 def sched__sched_stat_iowait(event_name, context, common_cpu,
406 common_secs, common_nsecs, common_pid, common_comm,
410 def sched__sched_stat_sleep(event_name, context, common_cpu,
411 common_secs, common_nsecs, common_pid, common_comm,
415 def sched__sched_stat_wait(event_name, context, common_cpu,
416 common_secs, common_nsecs, common_pid, common_comm,
420 def sched__sched_process_fork(event_name, context, common_cpu,
421 common_secs, common_nsecs, common_pid, common_comm,
422 parent_comm, parent_pid, child_comm, child_pid):
425 def sched__sched_process_wait(event_name, context, common_cpu,
426 common_secs, common_nsecs, common_pid, common_comm,
430 def sched__sched_process_exit(event_name, context, common_cpu,
431 common_secs, common_nsecs, common_pid, common_comm,
435 def sched__sched_process_free(event_name, context, common_cpu,
436 common_secs, common_nsecs, common_pid, common_comm,
440 def sched__sched_migrate_task(event_name, context, common_cpu,
441 common_secs, common_nsecs, common_pid, common_comm,
442 comm, pid, prio, orig_cpu,
444 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
445 common_pid, common_comm)
446 parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
448 def sched__sched_switch(event_name, context, common_cpu,
449 common_secs, common_nsecs, common_pid, common_comm,
450 prev_comm, prev_pid, prev_prio, prev_state,
451 next_comm, next_pid, next_prio):
453 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
454 common_pid, common_comm)
455 parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
456 next_comm, next_pid, next_prio)
458 def sched__sched_wakeup_new(event_name, context, common_cpu,
459 common_secs, common_nsecs, common_pid, common_comm,
460 comm, pid, prio, success,
462 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
463 common_pid, common_comm)
464 parser.wake_up(headers, comm, pid, success, target_cpu, 1)
466 def sched__sched_wakeup(event_name, context, common_cpu,
467 common_secs, common_nsecs, common_pid, common_comm,
468 comm, pid, prio, success,
470 headers = EventHeaders(common_cpu, common_secs, common_nsecs,
471 common_pid, common_comm)
472 parser.wake_up(headers, comm, pid, success, target_cpu, 0)
474 def sched__sched_wait_task(event_name, context, common_cpu,
475 common_secs, common_nsecs, common_pid, common_comm,
479 def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
480 common_secs, common_nsecs, common_pid, common_comm,
484 def sched__sched_kthread_stop(event_name, context, common_cpu,
485 common_secs, common_nsecs, common_pid, common_comm,
489 def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
490 common_pid, common_comm):