Linux Audio

Check our new training course

Loading...
v6.8
 
 
  1# Cpu task migration overview toy
  2#
  3# Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com>
  4#
  5# perf script event handlers have been generated by perf script -g python
  6#
  7# This software is distributed under the terms of the GNU General
  8# Public License ("GPL") version 2 as published by the Free Software
  9# Foundation.
 10from __future__ import print_function
 11
 12import os
 13import sys
 14
 15from collections import defaultdict
 16try:
 17	from UserList import UserList
 18except ImportError:
 19	# Python 3: UserList moved to the collections package
 20	from collections import UserList
 21
 22sys.path.append(os.environ['PERF_EXEC_PATH'] + \
 23	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
 24sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace')
 25
 26from perf_trace_context import *
 27from Core import *
 28from SchedGui import *
 29
 30
 31threads = { 0 : "idle"}
 32
 33def thread_name(pid):
 34	return "%s:%d" % (threads[pid], pid)
 35
 36class RunqueueEventUnknown:
 37	@staticmethod
 38	def color():
 39		return None
 40
 41	def __repr__(self):
 42		return "unknown"
 43
 44class RunqueueEventSleep:
 45	@staticmethod
 46	def color():
 47		return (0, 0, 0xff)
 48
 49	def __init__(self, sleeper):
 50		self.sleeper = sleeper
 51
 52	def __repr__(self):
 53		return "%s gone to sleep" % thread_name(self.sleeper)
 54
 55class RunqueueEventWakeup:
 56	@staticmethod
 57	def color():
 58		return (0xff, 0xff, 0)
 59
 60	def __init__(self, wakee):
 61		self.wakee = wakee
 62
 63	def __repr__(self):
 64		return "%s woke up" % thread_name(self.wakee)
 65
 66class RunqueueEventFork:
 67	@staticmethod
 68	def color():
 69		return (0, 0xff, 0)
 70
 71	def __init__(self, child):
 72		self.child = child
 73
 74	def __repr__(self):
 75		return "new forked task %s" % thread_name(self.child)
 76
 77class RunqueueMigrateIn:
 78	@staticmethod
 79	def color():
 80		return (0, 0xf0, 0xff)
 81
 82	def __init__(self, new):
 83		self.new = new
 84
 85	def __repr__(self):
 86		return "task migrated in %s" % thread_name(self.new)
 87
 88class RunqueueMigrateOut:
 89	@staticmethod
 90	def color():
 91		return (0xff, 0, 0xff)
 92
 93	def __init__(self, old):
 94		self.old = old
 95
 96	def __repr__(self):
 97		return "task migrated out %s" % thread_name(self.old)
 98
 99class RunqueueSnapshot:
100	def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
101		self.tasks = tuple(tasks)
102		self.event = event
103
104	def sched_switch(self, prev, prev_state, next):
105		event = RunqueueEventUnknown()
106
107		if taskState(prev_state) == "R" and next in self.tasks \
108			and prev in self.tasks:
109			return self
110
111		if taskState(prev_state) != "R":
112			event = RunqueueEventSleep(prev)
113
114		next_tasks = list(self.tasks[:])
115		if prev in self.tasks:
116			if taskState(prev_state) != "R":
117				next_tasks.remove(prev)
118		elif taskState(prev_state) == "R":
119			next_tasks.append(prev)
120
121		if next not in next_tasks:
122			next_tasks.append(next)
123
124		return RunqueueSnapshot(next_tasks, event)
125
126	def migrate_out(self, old):
127		if old not in self.tasks:
128			return self
129		next_tasks = [task for task in self.tasks if task != old]
130
131		return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
132
133	def __migrate_in(self, new, event):
134		if new in self.tasks:
135			self.event = event
136			return self
137		next_tasks = self.tasks[:] + tuple([new])
138
139		return RunqueueSnapshot(next_tasks, event)
140
141	def migrate_in(self, new):
142		return self.__migrate_in(new, RunqueueMigrateIn(new))
143
144	def wake_up(self, new):
145		return self.__migrate_in(new, RunqueueEventWakeup(new))
146
147	def wake_up_new(self, new):
148		return self.__migrate_in(new, RunqueueEventFork(new))
149
150	def load(self):
151		""" Provide the number of tasks on the runqueue.
152		    Don't count idle"""
153		return len(self.tasks) - 1
154
155	def __repr__(self):
156		ret = self.tasks.__repr__()
157		ret += self.origin_tostring()
158
159		return ret
160
161class TimeSlice:
162	def __init__(self, start, prev):
163		self.start = start
164		self.prev = prev
165		self.end = start
166		# cpus that triggered the event
167		self.event_cpus = []
168		if prev is not None:
169			self.total_load = prev.total_load
170			self.rqs = prev.rqs.copy()
171		else:
172			self.rqs = defaultdict(RunqueueSnapshot)
173			self.total_load = 0
174
175	def __update_total_load(self, old_rq, new_rq):
176		diff = new_rq.load() - old_rq.load()
177		self.total_load += diff
178
179	def sched_switch(self, ts_list, prev, prev_state, next, cpu):
180		old_rq = self.prev.rqs[cpu]
181		new_rq = old_rq.sched_switch(prev, prev_state, next)
182
183		if old_rq is new_rq:
184			return
185
186		self.rqs[cpu] = new_rq
187		self.__update_total_load(old_rq, new_rq)
188		ts_list.append(self)
189		self.event_cpus = [cpu]
190
191	def migrate(self, ts_list, new, old_cpu, new_cpu):
192		if old_cpu == new_cpu:
193			return
194		old_rq = self.prev.rqs[old_cpu]
195		out_rq = old_rq.migrate_out(new)
196		self.rqs[old_cpu] = out_rq
197		self.__update_total_load(old_rq, out_rq)
198
199		new_rq = self.prev.rqs[new_cpu]
200		in_rq = new_rq.migrate_in(new)
201		self.rqs[new_cpu] = in_rq
202		self.__update_total_load(new_rq, in_rq)
203
204		ts_list.append(self)
205
206		if old_rq is not out_rq:
207			self.event_cpus.append(old_cpu)
208		self.event_cpus.append(new_cpu)
209
210	def wake_up(self, ts_list, pid, cpu, fork):
211		old_rq = self.prev.rqs[cpu]
212		if fork:
213			new_rq = old_rq.wake_up_new(pid)
214		else:
215			new_rq = old_rq.wake_up(pid)
216
217		if new_rq is old_rq:
218			return
219		self.rqs[cpu] = new_rq
220		self.__update_total_load(old_rq, new_rq)
221		ts_list.append(self)
222		self.event_cpus = [cpu]
223
224	def next(self, t):
225		self.end = t
226		return TimeSlice(t, self)
227
228class TimeSliceList(UserList):
229	def __init__(self, arg = []):
230		self.data = arg
231
232	def get_time_slice(self, ts):
233		if len(self.data) == 0:
234			slice = TimeSlice(ts, TimeSlice(-1, None))
235		else:
236			slice = self.data[-1].next(ts)
237		return slice
238
239	def find_time_slice(self, ts):
240		start = 0
241		end = len(self.data)
242		found = -1
243		searching = True
244		while searching:
245			if start == end or start == end - 1:
246				searching = False
247
248			i = (end + start) / 2
249			if self.data[i].start <= ts and self.data[i].end >= ts:
250				found = i
251				end = i
252				continue
253
254			if self.data[i].end < ts:
255				start = i
256
257			elif self.data[i].start > ts:
258				end = i
259
260		return found
261
262	def set_root_win(self, win):
263		self.root_win = win
264
265	def mouse_down(self, cpu, t):
266		idx = self.find_time_slice(t)
267		if idx == -1:
268			return
269
270		ts = self[idx]
271		rq = ts.rqs[cpu]
272		raw = "CPU: %d\n" % cpu
273		raw += "Last event : %s\n" % rq.event.__repr__()
274		raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
275		raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
276		raw += "Load = %d\n" % rq.load()
277		for t in rq.tasks:
278			raw += "%s \n" % thread_name(t)
279
280		self.root_win.update_summary(raw)
281
282	def update_rectangle_cpu(self, slice, cpu):
283		rq = slice.rqs[cpu]
284
285		if slice.total_load != 0:
286			load_rate = rq.load() / float(slice.total_load)
287		else:
288			load_rate = 0
289
290		red_power = int(0xff - (0xff * load_rate))
291		color = (0xff, red_power, red_power)
292
293		top_color = None
294
295		if cpu in slice.event_cpus:
296			top_color = rq.event.color()
297
298		self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
299
300	def fill_zone(self, start, end):
301		i = self.find_time_slice(start)
302		if i == -1:
303			return
304
305		for i in range(i, len(self.data)):
306			timeslice = self.data[i]
307			if timeslice.start > end:
308				return
309
310			for cpu in timeslice.rqs:
311				self.update_rectangle_cpu(timeslice, cpu)
312
313	def interval(self):
314		if len(self.data) == 0:
315			return (0, 0)
316
317		return (self.data[0].start, self.data[-1].end)
318
319	def nr_rectangles(self):
320		last_ts = self.data[-1]
321		max_cpu = 0
322		for cpu in last_ts.rqs:
323			if cpu > max_cpu:
324				max_cpu = cpu
325		return max_cpu
326
327
328class SchedEventProxy:
329	def __init__(self):
330		self.current_tsk = defaultdict(lambda : -1)
331		self.timeslices = TimeSliceList()
332
333	def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
334			 next_comm, next_pid, next_prio):
335		""" Ensure the task we sched out this cpu is really the one
336		    we logged. Otherwise we may have missed traces """
337
338		on_cpu_task = self.current_tsk[headers.cpu]
339
340		if on_cpu_task != -1 and on_cpu_task != prev_pid:
341			print("Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
342				headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
343
344		threads[prev_pid] = prev_comm
345		threads[next_pid] = next_comm
346		self.current_tsk[headers.cpu] = next_pid
347
348		ts = self.timeslices.get_time_slice(headers.ts())
349		ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
350
351	def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
352		ts = self.timeslices.get_time_slice(headers.ts())
353		ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
354
355	def wake_up(self, headers, comm, pid, success, target_cpu, fork):
356		if success == 0:
357			return
358		ts = self.timeslices.get_time_slice(headers.ts())
359		ts.wake_up(self.timeslices, pid, target_cpu, fork)
360
361
362def trace_begin():
363	global parser
364	parser = SchedEventProxy()
365
366def trace_end():
367	app = wx.App(False)
368	timeslices = parser.timeslices
369	frame = RootFrame(timeslices, "Migration")
370	app.MainLoop()
371
372def sched__sched_stat_runtime(event_name, context, common_cpu,
373	common_secs, common_nsecs, common_pid, common_comm,
374	common_callchain, comm, pid, runtime, vruntime):
375	pass
376
377def sched__sched_stat_iowait(event_name, context, common_cpu,
378	common_secs, common_nsecs, common_pid, common_comm,
379	common_callchain, comm, pid, delay):
380	pass
381
382def sched__sched_stat_sleep(event_name, context, common_cpu,
383	common_secs, common_nsecs, common_pid, common_comm,
384	common_callchain, comm, pid, delay):
385	pass
386
387def sched__sched_stat_wait(event_name, context, common_cpu,
388	common_secs, common_nsecs, common_pid, common_comm,
389	common_callchain, comm, pid, delay):
390	pass
391
392def sched__sched_process_fork(event_name, context, common_cpu,
393	common_secs, common_nsecs, common_pid, common_comm,
394	common_callchain, parent_comm, parent_pid, child_comm, child_pid):
395	pass
396
397def sched__sched_process_wait(event_name, context, common_cpu,
398	common_secs, common_nsecs, common_pid, common_comm,
399	common_callchain, comm, pid, prio):
400	pass
401
402def sched__sched_process_exit(event_name, context, common_cpu,
403	common_secs, common_nsecs, common_pid, common_comm,
404	common_callchain, comm, pid, prio):
405	pass
406
407def sched__sched_process_free(event_name, context, common_cpu,
408	common_secs, common_nsecs, common_pid, common_comm,
409	common_callchain, comm, pid, prio):
410	pass
411
412def sched__sched_migrate_task(event_name, context, common_cpu,
413	common_secs, common_nsecs, common_pid, common_comm,
414	common_callchain, comm, pid, prio, orig_cpu,
415	dest_cpu):
416	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
417				common_pid, common_comm, common_callchain)
418	parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
419
420def sched__sched_switch(event_name, context, common_cpu,
421	common_secs, common_nsecs, common_pid, common_comm, common_callchain,
422	prev_comm, prev_pid, prev_prio, prev_state,
423	next_comm, next_pid, next_prio):
424
425	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
426				common_pid, common_comm, common_callchain)
427	parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
428			 next_comm, next_pid, next_prio)
429
430def sched__sched_wakeup_new(event_name, context, common_cpu,
431	common_secs, common_nsecs, common_pid, common_comm,
432	common_callchain, comm, pid, prio, success,
433	target_cpu):
434	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
435				common_pid, common_comm, common_callchain)
436	parser.wake_up(headers, comm, pid, success, target_cpu, 1)
437
438def sched__sched_wakeup(event_name, context, common_cpu,
439	common_secs, common_nsecs, common_pid, common_comm,
440	common_callchain, comm, pid, prio, success,
441	target_cpu):
442	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
443				common_pid, common_comm, common_callchain)
444	parser.wake_up(headers, comm, pid, success, target_cpu, 0)
445
446def sched__sched_wait_task(event_name, context, common_cpu,
447	common_secs, common_nsecs, common_pid, common_comm,
448	common_callchain, comm, pid, prio):
449	pass
450
451def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
452	common_secs, common_nsecs, common_pid, common_comm,
453	common_callchain, ret):
454	pass
455
456def sched__sched_kthread_stop(event_name, context, common_cpu,
457	common_secs, common_nsecs, common_pid, common_comm,
458	common_callchain, comm, pid):
459	pass
460
461def trace_unhandled(event_name, context, event_fields_dict):
 
462	pass
v3.5.6
  1#!/usr/bin/python
  2#
  3# Cpu task migration overview toy
  4#
  5# Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com>
  6#
  7# perf script event handlers have been generated by perf script -g python
  8#
  9# This software is distributed under the terms of the GNU General
 10# Public License ("GPL") version 2 as published by the Free Software
 11# Foundation.
 12
 13
 14import os
 15import sys
 16
 17from collections import defaultdict
 18from UserList import UserList
 
 
 
 
 19
 20sys.path.append(os.environ['PERF_EXEC_PATH'] + \
 21	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
 22sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace')
 23
 24from perf_trace_context import *
 25from Core import *
 26from SchedGui import *
 27
 28
 29threads = { 0 : "idle"}
 30
 31def thread_name(pid):
 32	return "%s:%d" % (threads[pid], pid)
 33
 34class RunqueueEventUnknown:
 35	@staticmethod
 36	def color():
 37		return None
 38
 39	def __repr__(self):
 40		return "unknown"
 41
 42class RunqueueEventSleep:
 43	@staticmethod
 44	def color():
 45		return (0, 0, 0xff)
 46
 47	def __init__(self, sleeper):
 48		self.sleeper = sleeper
 49
 50	def __repr__(self):
 51		return "%s gone to sleep" % thread_name(self.sleeper)
 52
 53class RunqueueEventWakeup:
 54	@staticmethod
 55	def color():
 56		return (0xff, 0xff, 0)
 57
 58	def __init__(self, wakee):
 59		self.wakee = wakee
 60
 61	def __repr__(self):
 62		return "%s woke up" % thread_name(self.wakee)
 63
 64class RunqueueEventFork:
 65	@staticmethod
 66	def color():
 67		return (0, 0xff, 0)
 68
 69	def __init__(self, child):
 70		self.child = child
 71
 72	def __repr__(self):
 73		return "new forked task %s" % thread_name(self.child)
 74
 75class RunqueueMigrateIn:
 76	@staticmethod
 77	def color():
 78		return (0, 0xf0, 0xff)
 79
 80	def __init__(self, new):
 81		self.new = new
 82
 83	def __repr__(self):
 84		return "task migrated in %s" % thread_name(self.new)
 85
 86class RunqueueMigrateOut:
 87	@staticmethod
 88	def color():
 89		return (0xff, 0, 0xff)
 90
 91	def __init__(self, old):
 92		self.old = old
 93
 94	def __repr__(self):
 95		return "task migrated out %s" % thread_name(self.old)
 96
 97class RunqueueSnapshot:
 98	def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
 99		self.tasks = tuple(tasks)
100		self.event = event
101
102	def sched_switch(self, prev, prev_state, next):
103		event = RunqueueEventUnknown()
104
105		if taskState(prev_state) == "R" and next in self.tasks \
106			and prev in self.tasks:
107			return self
108
109		if taskState(prev_state) != "R":
110			event = RunqueueEventSleep(prev)
111
112		next_tasks = list(self.tasks[:])
113		if prev in self.tasks:
114			if taskState(prev_state) != "R":
115				next_tasks.remove(prev)
116		elif taskState(prev_state) == "R":
117			next_tasks.append(prev)
118
119		if next not in next_tasks:
120			next_tasks.append(next)
121
122		return RunqueueSnapshot(next_tasks, event)
123
124	def migrate_out(self, old):
125		if old not in self.tasks:
126			return self
127		next_tasks = [task for task in self.tasks if task != old]
128
129		return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
130
131	def __migrate_in(self, new, event):
132		if new in self.tasks:
133			self.event = event
134			return self
135		next_tasks = self.tasks[:] + tuple([new])
136
137		return RunqueueSnapshot(next_tasks, event)
138
139	def migrate_in(self, new):
140		return self.__migrate_in(new, RunqueueMigrateIn(new))
141
142	def wake_up(self, new):
143		return self.__migrate_in(new, RunqueueEventWakeup(new))
144
145	def wake_up_new(self, new):
146		return self.__migrate_in(new, RunqueueEventFork(new))
147
148	def load(self):
149		""" Provide the number of tasks on the runqueue.
150		    Don't count idle"""
151		return len(self.tasks) - 1
152
153	def __repr__(self):
154		ret = self.tasks.__repr__()
155		ret += self.origin_tostring()
156
157		return ret
158
159class TimeSlice:
160	def __init__(self, start, prev):
161		self.start = start
162		self.prev = prev
163		self.end = start
164		# cpus that triggered the event
165		self.event_cpus = []
166		if prev is not None:
167			self.total_load = prev.total_load
168			self.rqs = prev.rqs.copy()
169		else:
170			self.rqs = defaultdict(RunqueueSnapshot)
171			self.total_load = 0
172
173	def __update_total_load(self, old_rq, new_rq):
174		diff = new_rq.load() - old_rq.load()
175		self.total_load += diff
176
177	def sched_switch(self, ts_list, prev, prev_state, next, cpu):
178		old_rq = self.prev.rqs[cpu]
179		new_rq = old_rq.sched_switch(prev, prev_state, next)
180
181		if old_rq is new_rq:
182			return
183
184		self.rqs[cpu] = new_rq
185		self.__update_total_load(old_rq, new_rq)
186		ts_list.append(self)
187		self.event_cpus = [cpu]
188
189	def migrate(self, ts_list, new, old_cpu, new_cpu):
190		if old_cpu == new_cpu:
191			return
192		old_rq = self.prev.rqs[old_cpu]
193		out_rq = old_rq.migrate_out(new)
194		self.rqs[old_cpu] = out_rq
195		self.__update_total_load(old_rq, out_rq)
196
197		new_rq = self.prev.rqs[new_cpu]
198		in_rq = new_rq.migrate_in(new)
199		self.rqs[new_cpu] = in_rq
200		self.__update_total_load(new_rq, in_rq)
201
202		ts_list.append(self)
203
204		if old_rq is not out_rq:
205			self.event_cpus.append(old_cpu)
206		self.event_cpus.append(new_cpu)
207
208	def wake_up(self, ts_list, pid, cpu, fork):
209		old_rq = self.prev.rqs[cpu]
210		if fork:
211			new_rq = old_rq.wake_up_new(pid)
212		else:
213			new_rq = old_rq.wake_up(pid)
214
215		if new_rq is old_rq:
216			return
217		self.rqs[cpu] = new_rq
218		self.__update_total_load(old_rq, new_rq)
219		ts_list.append(self)
220		self.event_cpus = [cpu]
221
222	def next(self, t):
223		self.end = t
224		return TimeSlice(t, self)
225
226class TimeSliceList(UserList):
227	def __init__(self, arg = []):
228		self.data = arg
229
230	def get_time_slice(self, ts):
231		if len(self.data) == 0:
232			slice = TimeSlice(ts, TimeSlice(-1, None))
233		else:
234			slice = self.data[-1].next(ts)
235		return slice
236
237	def find_time_slice(self, ts):
238		start = 0
239		end = len(self.data)
240		found = -1
241		searching = True
242		while searching:
243			if start == end or start == end - 1:
244				searching = False
245
246			i = (end + start) / 2
247			if self.data[i].start <= ts and self.data[i].end >= ts:
248				found = i
249				end = i
250				continue
251
252			if self.data[i].end < ts:
253				start = i
254
255			elif self.data[i].start > ts:
256				end = i
257
258		return found
259
260	def set_root_win(self, win):
261		self.root_win = win
262
263	def mouse_down(self, cpu, t):
264		idx = self.find_time_slice(t)
265		if idx == -1:
266			return
267
268		ts = self[idx]
269		rq = ts.rqs[cpu]
270		raw = "CPU: %d\n" % cpu
271		raw += "Last event : %s\n" % rq.event.__repr__()
272		raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
273		raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
274		raw += "Load = %d\n" % rq.load()
275		for t in rq.tasks:
276			raw += "%s \n" % thread_name(t)
277
278		self.root_win.update_summary(raw)
279
280	def update_rectangle_cpu(self, slice, cpu):
281		rq = slice.rqs[cpu]
282
283		if slice.total_load != 0:
284			load_rate = rq.load() / float(slice.total_load)
285		else:
286			load_rate = 0
287
288		red_power = int(0xff - (0xff * load_rate))
289		color = (0xff, red_power, red_power)
290
291		top_color = None
292
293		if cpu in slice.event_cpus:
294			top_color = rq.event.color()
295
296		self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
297
298	def fill_zone(self, start, end):
299		i = self.find_time_slice(start)
300		if i == -1:
301			return
302
303		for i in xrange(i, len(self.data)):
304			timeslice = self.data[i]
305			if timeslice.start > end:
306				return
307
308			for cpu in timeslice.rqs:
309				self.update_rectangle_cpu(timeslice, cpu)
310
311	def interval(self):
312		if len(self.data) == 0:
313			return (0, 0)
314
315		return (self.data[0].start, self.data[-1].end)
316
317	def nr_rectangles(self):
318		last_ts = self.data[-1]
319		max_cpu = 0
320		for cpu in last_ts.rqs:
321			if cpu > max_cpu:
322				max_cpu = cpu
323		return max_cpu
324
325
326class SchedEventProxy:
327	def __init__(self):
328		self.current_tsk = defaultdict(lambda : -1)
329		self.timeslices = TimeSliceList()
330
331	def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
332			 next_comm, next_pid, next_prio):
333		""" Ensure the task we sched out this cpu is really the one
334		    we logged. Otherwise we may have missed traces """
335
336		on_cpu_task = self.current_tsk[headers.cpu]
337
338		if on_cpu_task != -1 and on_cpu_task != prev_pid:
339			print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
340				(headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
341
342		threads[prev_pid] = prev_comm
343		threads[next_pid] = next_comm
344		self.current_tsk[headers.cpu] = next_pid
345
346		ts = self.timeslices.get_time_slice(headers.ts())
347		ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
348
349	def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
350		ts = self.timeslices.get_time_slice(headers.ts())
351		ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
352
353	def wake_up(self, headers, comm, pid, success, target_cpu, fork):
354		if success == 0:
355			return
356		ts = self.timeslices.get_time_slice(headers.ts())
357		ts.wake_up(self.timeslices, pid, target_cpu, fork)
358
359
360def trace_begin():
361	global parser
362	parser = SchedEventProxy()
363
364def trace_end():
365	app = wx.App(False)
366	timeslices = parser.timeslices
367	frame = RootFrame(timeslices, "Migration")
368	app.MainLoop()
369
370def sched__sched_stat_runtime(event_name, context, common_cpu,
371	common_secs, common_nsecs, common_pid, common_comm,
372	comm, pid, runtime, vruntime):
373	pass
374
375def sched__sched_stat_iowait(event_name, context, common_cpu,
376	common_secs, common_nsecs, common_pid, common_comm,
377	comm, pid, delay):
378	pass
379
380def sched__sched_stat_sleep(event_name, context, common_cpu,
381	common_secs, common_nsecs, common_pid, common_comm,
382	comm, pid, delay):
383	pass
384
385def sched__sched_stat_wait(event_name, context, common_cpu,
386	common_secs, common_nsecs, common_pid, common_comm,
387	comm, pid, delay):
388	pass
389
390def sched__sched_process_fork(event_name, context, common_cpu,
391	common_secs, common_nsecs, common_pid, common_comm,
392	parent_comm, parent_pid, child_comm, child_pid):
393	pass
394
395def sched__sched_process_wait(event_name, context, common_cpu,
396	common_secs, common_nsecs, common_pid, common_comm,
397	comm, pid, prio):
398	pass
399
400def sched__sched_process_exit(event_name, context, common_cpu,
401	common_secs, common_nsecs, common_pid, common_comm,
402	comm, pid, prio):
403	pass
404
405def sched__sched_process_free(event_name, context, common_cpu,
406	common_secs, common_nsecs, common_pid, common_comm,
407	comm, pid, prio):
408	pass
409
410def sched__sched_migrate_task(event_name, context, common_cpu,
411	common_secs, common_nsecs, common_pid, common_comm,
412	comm, pid, prio, orig_cpu,
413	dest_cpu):
414	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
415				common_pid, common_comm)
416	parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
417
418def sched__sched_switch(event_name, context, common_cpu,
419	common_secs, common_nsecs, common_pid, common_comm,
420	prev_comm, prev_pid, prev_prio, prev_state,
421	next_comm, next_pid, next_prio):
422
423	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
424				common_pid, common_comm)
425	parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
426			 next_comm, next_pid, next_prio)
427
428def sched__sched_wakeup_new(event_name, context, common_cpu,
429	common_secs, common_nsecs, common_pid, common_comm,
430	comm, pid, prio, success,
431	target_cpu):
432	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
433				common_pid, common_comm)
434	parser.wake_up(headers, comm, pid, success, target_cpu, 1)
435
436def sched__sched_wakeup(event_name, context, common_cpu,
437	common_secs, common_nsecs, common_pid, common_comm,
438	comm, pid, prio, success,
439	target_cpu):
440	headers = EventHeaders(common_cpu, common_secs, common_nsecs,
441				common_pid, common_comm)
442	parser.wake_up(headers, comm, pid, success, target_cpu, 0)
443
444def sched__sched_wait_task(event_name, context, common_cpu,
445	common_secs, common_nsecs, common_pid, common_comm,
446	comm, pid, prio):
447	pass
448
449def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
450	common_secs, common_nsecs, common_pid, common_comm,
451	ret):
452	pass
453
454def sched__sched_kthread_stop(event_name, context, common_cpu,
455	common_secs, common_nsecs, common_pid, common_comm,
456	comm, pid):
457	pass
458
459def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
460		common_pid, common_comm):
461	pass