sched-migration.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. import os
  13. import sys
  14. from collections import defaultdict
  15. from UserList import UserList
  16. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  17. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  18. sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  19. from perf_trace_context import *
  20. from Core import *
  21. from SchedGui import *
  22. threads = { 0 : "idle"}
  23. def thread_name(pid):
  24. return "%s:%d" % (threads[pid], pid)
  25. class RunqueueEventUnknown:
  26. @staticmethod
  27. def color():
  28. return None
  29. def __repr__(self):
  30. return "unknown"
  31. class RunqueueEventSleep:
  32. @staticmethod
  33. def color():
  34. return (0, 0, 0xff)
  35. def __init__(self, sleeper):
  36. self.sleeper = sleeper
  37. def __repr__(self):
  38. return "%s gone to sleep" % thread_name(self.sleeper)
  39. class RunqueueEventWakeup:
  40. @staticmethod
  41. def color():
  42. return (0xff, 0xff, 0)
  43. def __init__(self, wakee):
  44. self.wakee = wakee
  45. def __repr__(self):
  46. return "%s woke up" % thread_name(self.wakee)
  47. class RunqueueEventFork:
  48. @staticmethod
  49. def color():
  50. return (0, 0xff, 0)
  51. def __init__(self, child):
  52. self.child = child
  53. def __repr__(self):
  54. return "new forked task %s" % thread_name(self.child)
  55. class RunqueueMigrateIn:
  56. @staticmethod
  57. def color():
  58. return (0, 0xf0, 0xff)
  59. def __init__(self, new):
  60. self.new = new
  61. def __repr__(self):
  62. return "task migrated in %s" % thread_name(self.new)
  63. class RunqueueMigrateOut:
  64. @staticmethod
  65. def color():
  66. return (0xff, 0, 0xff)
  67. def __init__(self, old):
  68. self.old = old
  69. def __repr__(self):
  70. return "task migrated out %s" % thread_name(self.old)
  71. class RunqueueSnapshot:
  72. def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
  73. self.tasks = tuple(tasks)
  74. self.event = event
  75. def sched_switch(self, prev, prev_state, next):
  76. event = RunqueueEventUnknown()
  77. if taskState(prev_state) == "R" and next in self.tasks \
  78. and prev in self.tasks:
  79. return self
  80. if taskState(prev_state) != "R":
  81. event = RunqueueEventSleep(prev)
  82. next_tasks = list(self.tasks[:])
  83. if prev in self.tasks:
  84. if taskState(prev_state) != "R":
  85. next_tasks.remove(prev)
  86. elif taskState(prev_state) == "R":
  87. next_tasks.append(prev)
  88. if next not in next_tasks:
  89. next_tasks.append(next)
  90. return RunqueueSnapshot(next_tasks, event)
  91. def migrate_out(self, old):
  92. if old not in self.tasks:
  93. return self
  94. next_tasks = [task for task in self.tasks if task != old]
  95. return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
  96. def __migrate_in(self, new, event):
  97. if new in self.tasks:
  98. self.event = event
  99. return self
  100. next_tasks = self.tasks[:] + tuple([new])
  101. return RunqueueSnapshot(next_tasks, event)
  102. def migrate_in(self, new):
  103. return self.__migrate_in(new, RunqueueMigrateIn(new))
  104. def wake_up(self, new):
  105. return self.__migrate_in(new, RunqueueEventWakeup(new))
  106. def wake_up_new(self, new):
  107. return self.__migrate_in(new, RunqueueEventFork(new))
  108. def load(self):
  109. """ Provide the number of tasks on the runqueue.
  110. Don't count idle"""
  111. return len(self.tasks) - 1
  112. def __repr__(self):
  113. ret = self.tasks.__repr__()
  114. ret += self.origin_tostring()
  115. return ret
  116. class TimeSlice:
  117. def __init__(self, start, prev):
  118. self.start = start
  119. self.prev = prev
  120. self.end = start
  121. # cpus that triggered the event
  122. self.event_cpus = []
  123. if prev is not None:
  124. self.total_load = prev.total_load
  125. self.rqs = prev.rqs.copy()
  126. else:
  127. self.rqs = defaultdict(RunqueueSnapshot)
  128. self.total_load = 0
  129. def __update_total_load(self, old_rq, new_rq):
  130. diff = new_rq.load() - old_rq.load()
  131. self.total_load += diff
  132. def sched_switch(self, ts_list, prev, prev_state, next, cpu):
  133. old_rq = self.prev.rqs[cpu]
  134. new_rq = old_rq.sched_switch(prev, prev_state, next)
  135. if old_rq is new_rq:
  136. return
  137. self.rqs[cpu] = new_rq
  138. self.__update_total_load(old_rq, new_rq)
  139. ts_list.append(self)
  140. self.event_cpus = [cpu]
  141. def migrate(self, ts_list, new, old_cpu, new_cpu):
  142. if old_cpu == new_cpu:
  143. return
  144. old_rq = self.prev.rqs[old_cpu]
  145. out_rq = old_rq.migrate_out(new)
  146. self.rqs[old_cpu] = out_rq
  147. self.__update_total_load(old_rq, out_rq)
  148. new_rq = self.prev.rqs[new_cpu]
  149. in_rq = new_rq.migrate_in(new)
  150. self.rqs[new_cpu] = in_rq
  151. self.__update_total_load(new_rq, in_rq)
  152. ts_list.append(self)
  153. if old_rq is not out_rq:
  154. self.event_cpus.append(old_cpu)
  155. self.event_cpus.append(new_cpu)
  156. def wake_up(self, ts_list, pid, cpu, fork):
  157. old_rq = self.prev.rqs[cpu]
  158. if fork:
  159. new_rq = old_rq.wake_up_new(pid)
  160. else:
  161. new_rq = old_rq.wake_up(pid)
  162. if new_rq is old_rq:
  163. return
  164. self.rqs[cpu] = new_rq
  165. self.__update_total_load(old_rq, new_rq)
  166. ts_list.append(self)
  167. self.event_cpus = [cpu]
  168. def next(self, t):
  169. self.end = t
  170. return TimeSlice(t, self)
  171. class TimeSliceList(UserList):
  172. def __init__(self, arg = []):
  173. self.data = arg
  174. def get_time_slice(self, ts):
  175. if len(self.data) == 0:
  176. slice = TimeSlice(ts, TimeSlice(-1, None))
  177. else:
  178. slice = self.data[-1].next(ts)
  179. return slice
  180. def find_time_slice(self, ts):
  181. start = 0
  182. end = len(self.data)
  183. found = -1
  184. searching = True
  185. while searching:
  186. if start == end or start == end - 1:
  187. searching = False
  188. i = (end + start) / 2
  189. if self.data[i].start <= ts and self.data[i].end >= ts:
  190. found = i
  191. end = i
  192. continue
  193. if self.data[i].end < ts:
  194. start = i
  195. elif self.data[i].start > ts:
  196. end = i
  197. return found
  198. def set_root_win(self, win):
  199. self.root_win = win
  200. def mouse_down(self, cpu, t):
  201. idx = self.find_time_slice(t)
  202. if idx == -1:
  203. return
  204. ts = self[idx]
  205. rq = ts.rqs[cpu]
  206. raw = "CPU: %d\n" % cpu
  207. raw += "Last event : %s\n" % rq.event.__repr__()
  208. raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
  209. raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
  210. raw += "Load = %d\n" % rq.load()
  211. for t in rq.tasks:
  212. raw += "%s \n" % thread_name(t)
  213. self.root_win.update_summary(raw)
  214. def update_rectangle_cpu(self, slice, cpu):
  215. rq = slice.rqs[cpu]
  216. if slice.total_load != 0:
  217. load_rate = rq.load() / float(slice.total_load)
  218. else:
  219. load_rate = 0
  220. red_power = int(0xff - (0xff * load_rate))
  221. color = (0xff, red_power, red_power)
  222. top_color = None
  223. if cpu in slice.event_cpus:
  224. top_color = rq.event.color()
  225. self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
  226. def fill_zone(self, start, end):
  227. i = self.find_time_slice(start)
  228. if i == -1:
  229. return
  230. for i in xrange(i, len(self.data)):
  231. timeslice = self.data[i]
  232. if timeslice.start > end:
  233. return
  234. for cpu in timeslice.rqs:
  235. self.update_rectangle_cpu(timeslice, cpu)
  236. def interval(self):
  237. if len(self.data) == 0:
  238. return (0, 0)
  239. return (self.data[0].start, self.data[-1].end)
  240. def nr_rectangles(self):
  241. last_ts = self.data[-1]
  242. max_cpu = 0
  243. for cpu in last_ts.rqs:
  244. if cpu > max_cpu:
  245. max_cpu = cpu
  246. return max_cpu
  247. class SchedEventProxy:
  248. def __init__(self):
  249. self.current_tsk = defaultdict(lambda : -1)
  250. self.timeslices = TimeSliceList()
  251. def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
  252. next_comm, next_pid, next_prio):
  253. """ Ensure the task we sched out this cpu is really the one
  254. we logged. Otherwise we may have missed traces """
  255. on_cpu_task = self.current_tsk[headers.cpu]
  256. if on_cpu_task != -1 and on_cpu_task != prev_pid:
  257. print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
  258. (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
  259. threads[prev_pid] = prev_comm
  260. threads[next_pid] = next_comm
  261. self.current_tsk[headers.cpu] = next_pid
  262. ts = self.timeslices.get_time_slice(headers.ts())
  263. ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
  264. def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
  265. ts = self.timeslices.get_time_slice(headers.ts())
  266. ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
  267. def wake_up(self, headers, comm, pid, success, target_cpu, fork):
  268. if success == 0:
  269. return
  270. ts = self.timeslices.get_time_slice(headers.ts())
  271. ts.wake_up(self.timeslices, pid, target_cpu, fork)
  272. def trace_begin():
  273. global parser
  274. parser = SchedEventProxy()
  275. def trace_end():
  276. app = wx.App(False)
  277. timeslices = parser.timeslices
  278. frame = RootFrame(timeslices, "Migration")
  279. app.MainLoop()
  280. def sched__sched_stat_runtime(event_name, context, common_cpu,
  281. common_secs, common_nsecs, common_pid, common_comm,
  282. common_callchain, comm, pid, runtime, vruntime):
  283. pass
  284. def sched__sched_stat_iowait(event_name, context, common_cpu,
  285. common_secs, common_nsecs, common_pid, common_comm,
  286. common_callchain, comm, pid, delay):
  287. pass
  288. def sched__sched_stat_sleep(event_name, context, common_cpu,
  289. common_secs, common_nsecs, common_pid, common_comm,
  290. common_callchain, comm, pid, delay):
  291. pass
  292. def sched__sched_stat_wait(event_name, context, common_cpu,
  293. common_secs, common_nsecs, common_pid, common_comm,
  294. common_callchain, comm, pid, delay):
  295. pass
  296. def sched__sched_process_fork(event_name, context, common_cpu,
  297. common_secs, common_nsecs, common_pid, common_comm,
  298. common_callchain, parent_comm, parent_pid, child_comm, child_pid):
  299. pass
  300. def sched__sched_process_wait(event_name, context, common_cpu,
  301. common_secs, common_nsecs, common_pid, common_comm,
  302. common_callchain, comm, pid, prio):
  303. pass
  304. def sched__sched_process_exit(event_name, context, common_cpu,
  305. common_secs, common_nsecs, common_pid, common_comm,
  306. common_callchain, comm, pid, prio):
  307. pass
  308. def sched__sched_process_free(event_name, context, common_cpu,
  309. common_secs, common_nsecs, common_pid, common_comm,
  310. common_callchain, comm, pid, prio):
  311. pass
  312. def sched__sched_migrate_task(event_name, context, common_cpu,
  313. common_secs, common_nsecs, common_pid, common_comm,
  314. common_callchain, comm, pid, prio, orig_cpu,
  315. dest_cpu):
  316. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  317. common_pid, common_comm, common_callchain)
  318. parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
  319. def sched__sched_switch(event_name, context, common_cpu,
  320. common_secs, common_nsecs, common_pid, common_comm, common_callchain,
  321. prev_comm, prev_pid, prev_prio, prev_state,
  322. next_comm, next_pid, next_prio):
  323. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  324. common_pid, common_comm, common_callchain)
  325. parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
  326. next_comm, next_pid, next_prio)
  327. def sched__sched_wakeup_new(event_name, context, common_cpu,
  328. common_secs, common_nsecs, common_pid, common_comm,
  329. common_callchain, comm, pid, prio, success,
  330. target_cpu):
  331. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  332. common_pid, common_comm, common_callchain)
  333. parser.wake_up(headers, comm, pid, success, target_cpu, 1)
  334. def sched__sched_wakeup(event_name, context, common_cpu,
  335. common_secs, common_nsecs, common_pid, common_comm,
  336. common_callchain, comm, pid, prio, success,
  337. target_cpu):
  338. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  339. common_pid, common_comm, common_callchain)
  340. parser.wake_up(headers, comm, pid, success, target_cpu, 0)
  341. def sched__sched_wait_task(event_name, context, common_cpu,
  342. common_secs, common_nsecs, common_pid, common_comm,
  343. common_callchain, comm, pid, prio):
  344. pass
  345. def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
  346. common_secs, common_nsecs, common_pid, common_comm,
  347. common_callchain, ret):
  348. pass
  349. def sched__sched_kthread_stop(event_name, context, common_cpu,
  350. common_secs, common_nsecs, common_pid, common_comm,
  351. common_callchain, comm, pid):
  352. pass
  353. def trace_unhandled(event_name, context, event_fields_dict):
  354. pass