sched-migration.py 16 KB

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