Back to home page

OSCL-LXR

 
 

    


0001 # Cpu task migration overview toy
0002 #
0003 # Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com>
0004 #
0005 # perf script event handlers have been generated by perf script -g python
0006 #
0007 # This software is distributed under the terms of the GNU General
0008 # Public License ("GPL") version 2 as published by the Free Software
0009 # Foundation.
0010 from __future__ import print_function
0011 
0012 import os
0013 import sys
0014 
0015 from collections import defaultdict
0016 try:
0017     from UserList import UserList
0018 except ImportError:
0019     # Python 3: UserList moved to the collections package
0020     from collections import UserList
0021 
0022 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
0023     '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
0024 sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace')
0025 
0026 from perf_trace_context import *
0027 from Core import *
0028 from SchedGui import *
0029 
0030 
0031 threads = { 0 : "idle"}
0032 
0033 def thread_name(pid):
0034     return "%s:%d" % (threads[pid], pid)
0035 
0036 class RunqueueEventUnknown:
0037     @staticmethod
0038     def color():
0039         return None
0040 
0041     def __repr__(self):
0042         return "unknown"
0043 
0044 class RunqueueEventSleep:
0045     @staticmethod
0046     def color():
0047         return (0, 0, 0xff)
0048 
0049     def __init__(self, sleeper):
0050         self.sleeper = sleeper
0051 
0052     def __repr__(self):
0053         return "%s gone to sleep" % thread_name(self.sleeper)
0054 
0055 class RunqueueEventWakeup:
0056     @staticmethod
0057     def color():
0058         return (0xff, 0xff, 0)
0059 
0060     def __init__(self, wakee):
0061         self.wakee = wakee
0062 
0063     def __repr__(self):
0064         return "%s woke up" % thread_name(self.wakee)
0065 
0066 class RunqueueEventFork:
0067     @staticmethod
0068     def color():
0069         return (0, 0xff, 0)
0070 
0071     def __init__(self, child):
0072         self.child = child
0073 
0074     def __repr__(self):
0075         return "new forked task %s" % thread_name(self.child)
0076 
0077 class RunqueueMigrateIn:
0078     @staticmethod
0079     def color():
0080         return (0, 0xf0, 0xff)
0081 
0082     def __init__(self, new):
0083         self.new = new
0084 
0085     def __repr__(self):
0086         return "task migrated in %s" % thread_name(self.new)
0087 
0088 class RunqueueMigrateOut:
0089     @staticmethod
0090     def color():
0091         return (0xff, 0, 0xff)
0092 
0093     def __init__(self, old):
0094         self.old = old
0095 
0096     def __repr__(self):
0097         return "task migrated out %s" % thread_name(self.old)
0098 
0099 class RunqueueSnapshot:
0100     def __init__(self, tasks = [0], event = RunqueueEventUnknown()):
0101         self.tasks = tuple(tasks)
0102         self.event = event
0103 
0104     def sched_switch(self, prev, prev_state, next):
0105         event = RunqueueEventUnknown()
0106 
0107         if taskState(prev_state) == "R" and next in self.tasks \
0108             and prev in self.tasks:
0109             return self
0110 
0111         if taskState(prev_state) != "R":
0112             event = RunqueueEventSleep(prev)
0113 
0114         next_tasks = list(self.tasks[:])
0115         if prev in self.tasks:
0116             if taskState(prev_state) != "R":
0117                 next_tasks.remove(prev)
0118         elif taskState(prev_state) == "R":
0119             next_tasks.append(prev)
0120 
0121         if next not in next_tasks:
0122             next_tasks.append(next)
0123 
0124         return RunqueueSnapshot(next_tasks, event)
0125 
0126     def migrate_out(self, old):
0127         if old not in self.tasks:
0128             return self
0129         next_tasks = [task for task in self.tasks if task != old]
0130 
0131         return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old))
0132 
0133     def __migrate_in(self, new, event):
0134         if new in self.tasks:
0135             self.event = event
0136             return self
0137         next_tasks = self.tasks[:] + tuple([new])
0138 
0139         return RunqueueSnapshot(next_tasks, event)
0140 
0141     def migrate_in(self, new):
0142         return self.__migrate_in(new, RunqueueMigrateIn(new))
0143 
0144     def wake_up(self, new):
0145         return self.__migrate_in(new, RunqueueEventWakeup(new))
0146 
0147     def wake_up_new(self, new):
0148         return self.__migrate_in(new, RunqueueEventFork(new))
0149 
0150     def load(self):
0151         """ Provide the number of tasks on the runqueue.
0152             Don't count idle"""
0153         return len(self.tasks) - 1
0154 
0155     def __repr__(self):
0156         ret = self.tasks.__repr__()
0157         ret += self.origin_tostring()
0158 
0159         return ret
0160 
0161 class TimeSlice:
0162     def __init__(self, start, prev):
0163         self.start = start
0164         self.prev = prev
0165         self.end = start
0166         # cpus that triggered the event
0167         self.event_cpus = []
0168         if prev is not None:
0169             self.total_load = prev.total_load
0170             self.rqs = prev.rqs.copy()
0171         else:
0172             self.rqs = defaultdict(RunqueueSnapshot)
0173             self.total_load = 0
0174 
0175     def __update_total_load(self, old_rq, new_rq):
0176         diff = new_rq.load() - old_rq.load()
0177         self.total_load += diff
0178 
0179     def sched_switch(self, ts_list, prev, prev_state, next, cpu):
0180         old_rq = self.prev.rqs[cpu]
0181         new_rq = old_rq.sched_switch(prev, prev_state, next)
0182 
0183         if old_rq is new_rq:
0184             return
0185 
0186         self.rqs[cpu] = new_rq
0187         self.__update_total_load(old_rq, new_rq)
0188         ts_list.append(self)
0189         self.event_cpus = [cpu]
0190 
0191     def migrate(self, ts_list, new, old_cpu, new_cpu):
0192         if old_cpu == new_cpu:
0193             return
0194         old_rq = self.prev.rqs[old_cpu]
0195         out_rq = old_rq.migrate_out(new)
0196         self.rqs[old_cpu] = out_rq
0197         self.__update_total_load(old_rq, out_rq)
0198 
0199         new_rq = self.prev.rqs[new_cpu]
0200         in_rq = new_rq.migrate_in(new)
0201         self.rqs[new_cpu] = in_rq
0202         self.__update_total_load(new_rq, in_rq)
0203 
0204         ts_list.append(self)
0205 
0206         if old_rq is not out_rq:
0207             self.event_cpus.append(old_cpu)
0208         self.event_cpus.append(new_cpu)
0209 
0210     def wake_up(self, ts_list, pid, cpu, fork):
0211         old_rq = self.prev.rqs[cpu]
0212         if fork:
0213             new_rq = old_rq.wake_up_new(pid)
0214         else:
0215             new_rq = old_rq.wake_up(pid)
0216 
0217         if new_rq is old_rq:
0218             return
0219         self.rqs[cpu] = new_rq
0220         self.__update_total_load(old_rq, new_rq)
0221         ts_list.append(self)
0222         self.event_cpus = [cpu]
0223 
0224     def next(self, t):
0225         self.end = t
0226         return TimeSlice(t, self)
0227 
0228 class TimeSliceList(UserList):
0229     def __init__(self, arg = []):
0230         self.data = arg
0231 
0232     def get_time_slice(self, ts):
0233         if len(self.data) == 0:
0234             slice = TimeSlice(ts, TimeSlice(-1, None))
0235         else:
0236             slice = self.data[-1].next(ts)
0237         return slice
0238 
0239     def find_time_slice(self, ts):
0240         start = 0
0241         end = len(self.data)
0242         found = -1
0243         searching = True
0244         while searching:
0245             if start == end or start == end - 1:
0246                 searching = False
0247 
0248             i = (end + start) / 2
0249             if self.data[i].start <= ts and self.data[i].end >= ts:
0250                 found = i
0251                 end = i
0252                 continue
0253 
0254             if self.data[i].end < ts:
0255                 start = i
0256 
0257             elif self.data[i].start > ts:
0258                 end = i
0259 
0260         return found
0261 
0262     def set_root_win(self, win):
0263         self.root_win = win
0264 
0265     def mouse_down(self, cpu, t):
0266         idx = self.find_time_slice(t)
0267         if idx == -1:
0268             return
0269 
0270         ts = self[idx]
0271         rq = ts.rqs[cpu]
0272         raw = "CPU: %d\n" % cpu
0273         raw += "Last event : %s\n" % rq.event.__repr__()
0274         raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000)
0275         raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6))
0276         raw += "Load = %d\n" % rq.load()
0277         for t in rq.tasks:
0278             raw += "%s \n" % thread_name(t)
0279 
0280         self.root_win.update_summary(raw)
0281 
0282     def update_rectangle_cpu(self, slice, cpu):
0283         rq = slice.rqs[cpu]
0284 
0285         if slice.total_load != 0:
0286             load_rate = rq.load() / float(slice.total_load)
0287         else:
0288             load_rate = 0
0289 
0290         red_power = int(0xff - (0xff * load_rate))
0291         color = (0xff, red_power, red_power)
0292 
0293         top_color = None
0294 
0295         if cpu in slice.event_cpus:
0296             top_color = rq.event.color()
0297 
0298         self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end)
0299 
0300     def fill_zone(self, start, end):
0301         i = self.find_time_slice(start)
0302         if i == -1:
0303             return
0304 
0305         for i in range(i, len(self.data)):
0306             timeslice = self.data[i]
0307             if timeslice.start > end:
0308                 return
0309 
0310             for cpu in timeslice.rqs:
0311                 self.update_rectangle_cpu(timeslice, cpu)
0312 
0313     def interval(self):
0314         if len(self.data) == 0:
0315             return (0, 0)
0316 
0317         return (self.data[0].start, self.data[-1].end)
0318 
0319     def nr_rectangles(self):
0320         last_ts = self.data[-1]
0321         max_cpu = 0
0322         for cpu in last_ts.rqs:
0323             if cpu > max_cpu:
0324                 max_cpu = cpu
0325         return max_cpu
0326 
0327 
0328 class SchedEventProxy:
0329     def __init__(self):
0330         self.current_tsk = defaultdict(lambda : -1)
0331         self.timeslices = TimeSliceList()
0332 
0333     def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state,
0334              next_comm, next_pid, next_prio):
0335         """ Ensure the task we sched out this cpu is really the one
0336             we logged. Otherwise we may have missed traces """
0337 
0338         on_cpu_task = self.current_tsk[headers.cpu]
0339 
0340         if on_cpu_task != -1 and on_cpu_task != prev_pid:
0341             print("Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \
0342                 headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid)
0343 
0344         threads[prev_pid] = prev_comm
0345         threads[next_pid] = next_comm
0346         self.current_tsk[headers.cpu] = next_pid
0347 
0348         ts = self.timeslices.get_time_slice(headers.ts())
0349         ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu)
0350 
0351     def migrate(self, headers, pid, prio, orig_cpu, dest_cpu):
0352         ts = self.timeslices.get_time_slice(headers.ts())
0353         ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu)
0354 
0355     def wake_up(self, headers, comm, pid, success, target_cpu, fork):
0356         if success == 0:
0357             return
0358         ts = self.timeslices.get_time_slice(headers.ts())
0359         ts.wake_up(self.timeslices, pid, target_cpu, fork)
0360 
0361 
0362 def trace_begin():
0363     global parser
0364     parser = SchedEventProxy()
0365 
0366 def trace_end():
0367     app = wx.App(False)
0368     timeslices = parser.timeslices
0369     frame = RootFrame(timeslices, "Migration")
0370     app.MainLoop()
0371 
0372 def sched__sched_stat_runtime(event_name, context, common_cpu,
0373     common_secs, common_nsecs, common_pid, common_comm,
0374     common_callchain, comm, pid, runtime, vruntime):
0375     pass
0376 
0377 def sched__sched_stat_iowait(event_name, context, common_cpu,
0378     common_secs, common_nsecs, common_pid, common_comm,
0379     common_callchain, comm, pid, delay):
0380     pass
0381 
0382 def sched__sched_stat_sleep(event_name, context, common_cpu,
0383     common_secs, common_nsecs, common_pid, common_comm,
0384     common_callchain, comm, pid, delay):
0385     pass
0386 
0387 def sched__sched_stat_wait(event_name, context, common_cpu,
0388     common_secs, common_nsecs, common_pid, common_comm,
0389     common_callchain, comm, pid, delay):
0390     pass
0391 
0392 def sched__sched_process_fork(event_name, context, common_cpu,
0393     common_secs, common_nsecs, common_pid, common_comm,
0394     common_callchain, parent_comm, parent_pid, child_comm, child_pid):
0395     pass
0396 
0397 def sched__sched_process_wait(event_name, context, common_cpu,
0398     common_secs, common_nsecs, common_pid, common_comm,
0399     common_callchain, comm, pid, prio):
0400     pass
0401 
0402 def sched__sched_process_exit(event_name, context, common_cpu,
0403     common_secs, common_nsecs, common_pid, common_comm,
0404     common_callchain, comm, pid, prio):
0405     pass
0406 
0407 def sched__sched_process_free(event_name, context, common_cpu,
0408     common_secs, common_nsecs, common_pid, common_comm,
0409     common_callchain, comm, pid, prio):
0410     pass
0411 
0412 def sched__sched_migrate_task(event_name, context, common_cpu,
0413     common_secs, common_nsecs, common_pid, common_comm,
0414     common_callchain, comm, pid, prio, orig_cpu,
0415     dest_cpu):
0416     headers = EventHeaders(common_cpu, common_secs, common_nsecs,
0417                 common_pid, common_comm, common_callchain)
0418     parser.migrate(headers, pid, prio, orig_cpu, dest_cpu)
0419 
0420 def sched__sched_switch(event_name, context, common_cpu,
0421     common_secs, common_nsecs, common_pid, common_comm, common_callchain,
0422     prev_comm, prev_pid, prev_prio, prev_state,
0423     next_comm, next_pid, next_prio):
0424 
0425     headers = EventHeaders(common_cpu, common_secs, common_nsecs,
0426                 common_pid, common_comm, common_callchain)
0427     parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state,
0428              next_comm, next_pid, next_prio)
0429 
0430 def sched__sched_wakeup_new(event_name, context, common_cpu,
0431     common_secs, common_nsecs, common_pid, common_comm,
0432     common_callchain, comm, pid, prio, success,
0433     target_cpu):
0434     headers = EventHeaders(common_cpu, common_secs, common_nsecs,
0435                 common_pid, common_comm, common_callchain)
0436     parser.wake_up(headers, comm, pid, success, target_cpu, 1)
0437 
0438 def sched__sched_wakeup(event_name, context, common_cpu,
0439     common_secs, common_nsecs, common_pid, common_comm,
0440     common_callchain, comm, pid, prio, success,
0441     target_cpu):
0442     headers = EventHeaders(common_cpu, common_secs, common_nsecs,
0443                 common_pid, common_comm, common_callchain)
0444     parser.wake_up(headers, comm, pid, success, target_cpu, 0)
0445 
0446 def sched__sched_wait_task(event_name, context, common_cpu,
0447     common_secs, common_nsecs, common_pid, common_comm,
0448     common_callchain, comm, pid, prio):
0449     pass
0450 
0451 def sched__sched_kthread_stop_ret(event_name, context, common_cpu,
0452     common_secs, common_nsecs, common_pid, common_comm,
0453     common_callchain, ret):
0454     pass
0455 
0456 def sched__sched_kthread_stop(event_name, context, common_cpu,
0457     common_secs, common_nsecs, common_pid, common_comm,
0458     common_callchain, comm, pid):
0459     pass
0460 
0461 def trace_unhandled(event_name, context, event_fields_dict):
0462     pass