sched-migration.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. # The whole is licensed under the terms of the GNU GPL License version 2
  10. try:
  11. import wx
  12. except ImportError:
  13. raise ImportError, "You need to install the wxpython lib for this script"
  14. import os
  15. import sys
  16. from collections import defaultdict
  17. from UserList import UserList
  18. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  19. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  20. from perf_trace_context import *
  21. from Core import *
  22. class RootFrame(wx.Frame):
  23. def __init__(self, timeslices, parent = None, id = -1, title = "Migration"):
  24. wx.Frame.__init__(self, parent, id, title)
  25. (self.screen_width, self.screen_height) = wx.GetDisplaySize()
  26. self.screen_width -= 10
  27. self.screen_height -= 10
  28. self.zoom = 0.5
  29. self.scroll_scale = 20
  30. self.timeslices = timeslices
  31. (self.ts_start, self.ts_end) = timeslices.interval()
  32. self.update_width_virtual()
  33. # whole window panel
  34. self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height))
  35. # scrollable container
  36. self.scroll = wx.ScrolledWindow(self.panel)
  37. self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, 100 / 10)
  38. self.scroll.EnableScrolling(True, True)
  39. self.scroll.SetFocus()
  40. # scrollable drawing area
  41. self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width, self.screen_height / 2))
  42. self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint)
  43. self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
  44. self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
  45. self.scroll.Bind(wx.EVT_PAINT, self.on_paint)
  46. self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
  47. self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
  48. self.scroll.Fit()
  49. self.Fit()
  50. self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, -1, wx.SIZE_USE_EXISTING)
  51. self.max_cpu = -1
  52. self.txt = None
  53. self.Show(True)
  54. def us_to_px(self, val):
  55. return val / (10 ** 3) * self.zoom
  56. def px_to_us(self, val):
  57. return (val / self.zoom) * (10 ** 3)
  58. def scroll_start(self):
  59. (x, y) = self.scroll.GetViewStart()
  60. return (x * self.scroll_scale, y * self.scroll_scale)
  61. def scroll_start_us(self):
  62. (x, y) = self.scroll_start()
  63. return self.px_to_us(x)
  64. def update_rectangle_cpu(self, dc, slice, cpu, offset_time):
  65. rq = slice.rqs[cpu]
  66. if slice.total_load != 0:
  67. load_rate = rq.load() / float(slice.total_load)
  68. else:
  69. load_rate = 0
  70. offset_px = self.us_to_px(slice.start - offset_time)
  71. width_px = self.us_to_px(slice.end - slice.start)
  72. (x, y) = self.scroll_start()
  73. if width_px == 0:
  74. return
  75. offset_py = 100 + (cpu * 150)
  76. width_py = 100
  77. if cpu in slice.event_cpus:
  78. rgb = rq.event.color()
  79. if rgb is not None:
  80. (r, g, b) = rgb
  81. color = wx.Colour(r, g, b)
  82. brush = wx.Brush(color, wx.SOLID)
  83. dc.SetBrush(brush)
  84. dc.DrawRectangle(offset_px, offset_py, width_px, 5)
  85. width_py -= 5
  86. offset_py += 5
  87. red_power = int(0xff - (0xff * load_rate))
  88. color = wx.Colour(0xff, red_power, red_power)
  89. brush = wx.Brush(color, wx.SOLID)
  90. dc.SetBrush(brush)
  91. dc.DrawRectangle(offset_px, offset_py, width_px, width_py)
  92. def update_rectangles(self, dc, start, end):
  93. if len(self.timeslices) == 0:
  94. return
  95. start += self.timeslices[0].start
  96. end += self.timeslices[0].start
  97. color = wx.Colour(0, 0, 0)
  98. brush = wx.Brush(color, wx.SOLID)
  99. dc.SetBrush(brush)
  100. i = self.timeslices.find_time_slice(start)
  101. if i == -1:
  102. return
  103. for i in xrange(i, len(self.timeslices)):
  104. timeslice = self.timeslices[i]
  105. if timeslice.start > end:
  106. return
  107. for cpu in timeslice.rqs:
  108. self.update_rectangle_cpu(dc, timeslice, cpu, self.timeslices[0].start)
  109. if cpu > self.max_cpu:
  110. self.max_cpu = cpu
  111. def on_paint(self, event):
  112. color = wx.Colour(0xff, 0xff, 0xff)
  113. brush = wx.Brush(color, wx.SOLID)
  114. dc = wx.PaintDC(self.scroll_panel)
  115. dc.SetBrush(brush)
  116. width = min(self.width_virtual, self.screen_width)
  117. (x, y) = self.scroll_start()
  118. start = self.px_to_us(x)
  119. end = self.px_to_us(x + width)
  120. self.update_rectangles(dc, start, end)
  121. def cpu_from_ypixel(self, y):
  122. y -= 100
  123. cpu = y / 150
  124. height = y % 150
  125. if cpu < 0 or cpu > self.max_cpu or height > 100:
  126. return -1
  127. return cpu
  128. def update_summary(self, cpu, t):
  129. idx = self.timeslices.find_time_slice(t)
  130. if idx == -1:
  131. return
  132. ts = self.timeslices[idx]
  133. rq = ts.rqs[cpu]
  134. raw = "CPU: %d\n" % cpu
  135. raw += "Last event : %s\n" % rq.event.__repr__()
  136. raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
  137. raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
  138. raw += "Load = %d\n" % rq.load()
  139. for t in rq.tasks:
  140. raw += "%s \n" % thread_name(t)
  141. if self.txt:
  142. self.txt.Destroy()
  143. self.txt = wx.StaticText(self.panel, -1, raw, (0, (self.screen_height / 2) + 50))
  144. def on_mouse_down(self, event):
  145. (x, y) = event.GetPositionTuple()
  146. cpu = self.cpu_from_ypixel(y)
  147. if cpu == -1:
  148. return
  149. t = self.px_to_us(x) + self.timeslices[0].start
  150. self.update_summary(cpu, t)
  151. def update_width_virtual(self):
  152. self.width_virtual = self.us_to_px(self.ts_end - self.ts_start)
  153. def __zoom(self, x):
  154. self.update_width_virtual()
  155. (xpos, ypos) = self.scroll.GetViewStart()
  156. xpos = self.us_to_px(x) / self.scroll_scale
  157. self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, 100 / 10, xpos, ypos)
  158. self.Refresh()
  159. def zoom_in(self):
  160. x = self.scroll_start_us()
  161. self.zoom *= 2
  162. self.__zoom(x)
  163. def zoom_out(self):
  164. x = self.scroll_start_us()
  165. self.zoom /= 2
  166. self.__zoom(x)
  167. def on_key_press(self, event):
  168. key = event.GetRawKeyCode()
  169. if key == ord("+"):
  170. self.zoom_in()
  171. return
  172. if key == ord("-"):
  173. self.zoom_out()
  174. return
  175. key = event.GetKeyCode()
  176. (x, y) = self.scroll.GetViewStart()
  177. if key == wx.WXK_RIGHT:
  178. self.scroll.Scroll(x + 1, y)
  179. elif key == wx.WXK_LEFT:
  180. self.scroll.Scroll(x -1, y)
  181. threads = { 0 : "idle"}
  182. def thread_name(pid):
  183. return "%s:%d" % (threads[pid], pid)
  184. class EventHeaders:
  185. def __init__(self, common_cpu, common_secs, common_nsecs,
  186. common_pid, common_comm):
  187. self.cpu = common_cpu
  188. self.secs = common_secs
  189. self.nsecs = common_nsecs
  190. self.pid = common_pid
  191. self.comm = common_comm
  192. def ts(self):
  193. return (self.secs * (10 ** 9)) + self.nsecs
  194. def ts_format(self):
  195. return "%d.%d" % (self.secs, int(self.nsecs / 1000))
  196. def taskState(state):
  197. states = {
  198. 0 : "R",
  199. 1 : "S",
  200. 2 : "D",
  201. 64: "DEAD"
  202. }
  203. if state not in states:
  204. return "Unknown"
  205. return states[state]
  206. class RunqueueEventUnknown:
  207. @staticmethod
  208. def color():
  209. return None
  210. def __repr__(self):
  211. return "unknown"
  212. class RunqueueEventSleep:
  213. @staticmethod
  214. def color():
  215. return (0, 0, 0xff)
  216. def __init__(self, sleeper):
  217. self.sleeper = sleeper
  218. def __repr__(self):
  219. return "%s gone to sleep" % thread_name(self.sleeper)
  220. class RunqueueEventWakeup:
  221. @staticmethod
  222. def color():
  223. return (0xff, 0xff, 0)
  224. def __init__(self, wakee):
  225. self.wakee = wakee
  226. def __repr__(self):
  227. return "%s woke up" % thread_name(self.wakee)
  228. class RunqueueEventFork:
  229. @staticmethod
  230. def color():
  231. return (0, 0xff, 0)
  232. def __init__(self, child):
  233. self.child = child
  234. def __repr__(self):
  235. return "new forked task %s" % thread_name(self.child)
  236. class RunqueueMigrateIn:
  237. @staticmethod
  238. def color():
  239. return (0, 0xf0, 0xff)
  240. def __init__(self, new):
  241. self.new = new
  242. def __repr__(self):
  243. return "task migrated in %s" % thread_name(self.new)
  244. class RunqueueMigrateOut:
  245. @staticmethod
  246. def color():
  247. return (0xff, 0, 0xff)
  248. def __init__(self, old):
  249. self.old = old
  250. def __repr__(self):
  251. return "task migrated out %s" % thread_name(self.old)
  252. class RunqueueSnapshot:
  253. def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
  254. self.tasks = tuple(tasks)
  255. self.event = event
  256. def sched_switch(self, prev, prev_state, next):
  257. event = RunqueueEventUnknown()
  258. if taskState(prev_state) == "R" and next in self.tasks \
  259. and prev in self.tasks:
  260. return self
  261. if taskState(prev_state) != "R":
  262. event = RunqueueEventSleep(prev)
  263. next_tasks = list(self.tasks[:])
  264. if prev in self.tasks:
  265. if taskState(prev_state) != "R":
  266. next_tasks.remove(prev)
  267. elif taskState(prev_state) == "R":
  268. next_tasks.append(prev)
  269. if next not in next_tasks:
  270. next_tasks.append(next)
  271. return RunqueueSnapshot(next_tasks, event)
  272. def migrate_out(self, old):
  273. if old not in self.tasks:
  274. return self
  275. next_tasks = [task for task in self.tasks if task != old]
  276. return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
  277. def __migrate_in(self, new, event):
  278. if new in self.tasks:
  279. self.event = event
  280. return self
  281. next_tasks = self.tasks[:] + tuple([new])
  282. return RunqueueSnapshot(next_tasks, event)
  283. def migrate_in(self, new):
  284. return self.__migrate_in(new, RunqueueMigrateIn(new))
  285. def wake_up(self, new):
  286. return self.__migrate_in(new, RunqueueEventWakeup(new))
  287. def wake_up_new(self, new):
  288. return self.__migrate_in(new, RunqueueEventFork(new))
  289. def load(self):
  290. """ Provide the number of tasks on the runqueue.
  291. Don't count idle"""
  292. return len(self.tasks) - 1
  293. def __repr__(self):
  294. ret = self.tasks.__repr__()
  295. ret += self.origin_tostring()
  296. return ret
  297. class TimeSlice:
  298. def __init__(self, start, prev):
  299. self.start = start
  300. self.prev = prev
  301. self.end = start
  302. # cpus that triggered the event
  303. self.event_cpus = []
  304. if prev is not None:
  305. self.total_load = prev.total_load
  306. self.rqs = prev.rqs.copy()
  307. else:
  308. self.rqs = defaultdict(RunqueueSnapshot)
  309. self.total_load = 0
  310. def __update_total_load(self, old_rq, new_rq):
  311. diff = new_rq.load() - old_rq.load()
  312. self.total_load += diff
  313. def sched_switch(self, ts_list, prev, prev_state, next, cpu):
  314. old_rq = self.prev.rqs[cpu]
  315. new_rq = old_rq.sched_switch(prev, prev_state, next)
  316. if old_rq is new_rq:
  317. return
  318. self.rqs[cpu] = new_rq
  319. self.__update_total_load(old_rq, new_rq)
  320. ts_list.append(self)
  321. self.event_cpus = [cpu]
  322. def migrate(self, ts_list, new, old_cpu, new_cpu):
  323. if old_cpu == new_cpu:
  324. return
  325. old_rq = self.prev.rqs[old_cpu]
  326. out_rq = old_rq.migrate_out(new)
  327. self.rqs[old_cpu] = out_rq
  328. self.__update_total_load(old_rq, out_rq)
  329. new_rq = self.prev.rqs[new_cpu]
  330. in_rq = new_rq.migrate_in(new)
  331. self.rqs[new_cpu] = in_rq
  332. self.__update_total_load(new_rq, in_rq)
  333. ts_list.append(self)
  334. if old_rq is not out_rq:
  335. self.event_cpus.append(old_cpu)
  336. self.event_cpus.append(new_cpu)
  337. def wake_up(self, ts_list, pid, cpu, fork):
  338. old_rq = self.prev.rqs[cpu]
  339. if fork:
  340. new_rq = old_rq.wake_up_new(pid)
  341. else:
  342. new_rq = old_rq.wake_up(pid)
  343. if new_rq is old_rq:
  344. return
  345. self.rqs[cpu] = new_rq
  346. self.__update_total_load(old_rq, new_rq)
  347. ts_list.append(self)
  348. self.event_cpus = [cpu]
  349. def next(self, t):
  350. self.end = t
  351. return TimeSlice(t, self)
  352. class TimeSliceList(UserList):
  353. def __init__(self, arg = []):
  354. self.data = arg
  355. def get_time_slice(self, ts):
  356. if len(self.data) == 0:
  357. slice = TimeSlice(ts, TimeSlice(-1, None))
  358. else:
  359. slice = self.data[-1].next(ts)
  360. return slice
  361. def find_time_slice(self, ts):
  362. start = 0
  363. end = len(self.data)
  364. found = -1
  365. searching = True
  366. while searching:
  367. if start == end or start == end - 1:
  368. searching = False
  369. i = (end + start) / 2
  370. if self.data[i].start <= ts and self.data[i].end >= ts:
  371. found = i
  372. end = i
  373. continue
  374. if self.data[i].end < ts:
  375. start = i
  376. elif self.data[i].start > ts:
  377. end = i
  378. return found
  379. def interval(self):
  380. if len(self.data) == 0:
  381. return (0, 0)
  382. return (self.data[0].start, self.data[-1].end)
  383. class SchedEventProxy:
  384. def __init__(self):
  385. self.current_tsk = defaultdict(lambda : -1)
  386. self.timeslices = TimeSliceList()
  387. def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
  388. next_comm, next_pid, next_prio):
  389. """ Ensure the task we sched out this cpu is really the one
  390. we logged. Otherwise we may have missed traces """
  391. on_cpu_task = self.current_tsk[headers.cpu]
  392. if on_cpu_task != -1 and on_cpu_task != prev_pid:
  393. print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
  394. (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
  395. threads[prev_pid] = prev_comm
  396. threads[next_pid] = next_comm
  397. self.current_tsk[headers.cpu] = next_pid
  398. ts = self.timeslices.get_time_slice(headers.ts())
  399. ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
  400. def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
  401. ts = self.timeslices.get_time_slice(headers.ts())
  402. ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
  403. def wake_up(self, headers, comm, pid, success, target_cpu, fork):
  404. if success == 0:
  405. return
  406. ts = self.timeslices.get_time_slice(headers.ts())
  407. ts.wake_up(self.timeslices, pid, target_cpu, fork)
  408. def trace_begin():
  409. global parser
  410. parser = SchedEventProxy()
  411. def trace_end():
  412. app = wx.App(False)
  413. timeslices = parser.timeslices
  414. frame = RootFrame(timeslices)
  415. app.MainLoop()
  416. def sched__sched_stat_runtime(event_name, context, common_cpu,
  417. common_secs, common_nsecs, common_pid, common_comm,
  418. comm, pid, runtime, vruntime):
  419. pass
  420. def sched__sched_stat_iowait(event_name, context, common_cpu,
  421. common_secs, common_nsecs, common_pid, common_comm,
  422. comm, pid, delay):
  423. pass
  424. def sched__sched_stat_sleep(event_name, context, common_cpu,
  425. common_secs, common_nsecs, common_pid, common_comm,
  426. comm, pid, delay):
  427. pass
  428. def sched__sched_stat_wait(event_name, context, common_cpu,
  429. common_secs, common_nsecs, common_pid, common_comm,
  430. comm, pid, delay):
  431. pass
  432. def sched__sched_process_fork(event_name, context, common_cpu,
  433. common_secs, common_nsecs, common_pid, common_comm,
  434. parent_comm, parent_pid, child_comm, child_pid):
  435. pass
  436. def sched__sched_process_wait(event_name, context, common_cpu,
  437. common_secs, common_nsecs, common_pid, common_comm,
  438. comm, pid, prio):
  439. pass
  440. def sched__sched_process_exit(event_name, context, common_cpu,
  441. common_secs, common_nsecs, common_pid, common_comm,
  442. comm, pid, prio):
  443. pass
  444. def sched__sched_process_free(event_name, context, common_cpu,
  445. common_secs, common_nsecs, common_pid, common_comm,
  446. comm, pid, prio):
  447. pass
  448. def sched__sched_migrate_task(event_name, context, common_cpu,
  449. common_secs, common_nsecs, common_pid, common_comm,
  450. comm, pid, prio, orig_cpu,
  451. dest_cpu):
  452. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  453. common_pid, common_comm)
  454. parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
  455. def sched__sched_switch(event_name, context, common_cpu,
  456. common_secs, common_nsecs, common_pid, common_comm,
  457. prev_comm, prev_pid, prev_prio, prev_state,
  458. next_comm, next_pid, next_prio):
  459. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  460. common_pid, common_comm)
  461. parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
  462. next_comm, next_pid, next_prio)
  463. def sched__sched_wakeup_new(event_name, context, common_cpu,
  464. common_secs, common_nsecs, common_pid, common_comm,
  465. comm, pid, prio, success,
  466. target_cpu):
  467. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  468. common_pid, common_comm)
  469. parser.wake_up(headers, comm, pid, success, target_cpu, 1)
  470. def sched__sched_wakeup(event_name, context, common_cpu,
  471. common_secs, common_nsecs, common_pid, common_comm,
  472. comm, pid, prio, success,
  473. target_cpu):
  474. headers = EventHeaders(common_cpu, common_secs, common_nsecs,
  475. common_pid, common_comm)
  476. parser.wake_up(headers, comm, pid, success, target_cpu, 0)
  477. def sched__sched_wait_task(event_name, context, common_cpu,
  478. common_secs, common_nsecs, common_pid, common_comm,
  479. comm, pid, prio):
  480. pass
  481. def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
  482. common_secs, common_nsecs, common_pid, common_comm,
  483. ret):
  484. pass
  485. def sched__sched_kthread_stop(event_name, context, common_cpu,
  486. common_secs, common_nsecs, common_pid, common_comm,
  487. comm, pid):
  488. pass
  489. def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
  490. common_pid, common_comm):
  491. pass