sched-migration.py 12 KB

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