sched-migration.py 17 KB

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