sched-migration.py 12 KB

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