sched-migration.py 17 KB

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