sched-migration.py 12 KB

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