diff options
author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2016-03-22 15:53:55 -0400 |
---|---|---|
committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2016-03-22 16:00:36 -0400 |
commit | 27474dfff59e40e1abf33e0b5dfe7a16dc73f4b1 (patch) | |
tree | ead56b3783117e666d008498120f8de7f6627bfe /sched_trace/file.py | |
parent | 17fedde91157d9e57a3c98c00c11ca03b0afcc78 (diff) |
add st-draw: a pycairo based drawing tool for st_trace schedules
Diffstat (limited to 'sched_trace/file.py')
-rw-r--r-- | sched_trace/file.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/sched_trace/file.py b/sched_trace/file.py new file mode 100644 index 0000000..5c18163 --- /dev/null +++ b/sched_trace/file.py | |||
@@ -0,0 +1,58 @@ | |||
1 | """Convenience wrapper around mmap'ed sched_trace binary files. | ||
2 | """ | ||
3 | |||
4 | from .format import RECORD_LEN, unpack, get_event, EVENTS | ||
5 | |||
6 | import mmap | ||
7 | |||
8 | class SchedTraceFile(object): | ||
9 | def __init__(self, fname): | ||
10 | self.source = fname | ||
11 | with open(fname, 'rw') as f: | ||
12 | self.mem = mmap.mmap(f.fileno(), 0, mmap.MAP_PRIVATE) | ||
13 | |||
14 | def __len__(self): | ||
15 | return len(self.mem) / RECORD_LEN | ||
16 | |||
17 | def events_of_type(self, event_ids): | ||
18 | for i in xrange(len(self)): | ||
19 | if get_event(self.mem, i) in event_ids: | ||
20 | record = self.mem[i * RECORD_LEN:] | ||
21 | yield unpack(record) | ||
22 | |||
23 | def __iter__(self): | ||
24 | for i in xrange(len(self)): | ||
25 | record = self.mem[i * RECORD_LEN:] | ||
26 | yield unpack(record) | ||
27 | |||
28 | def __reversed__(self): | ||
29 | for i in reversed(xrange(len(self))): | ||
30 | record = self.mem[i * RECORD_LEN:] | ||
31 | yield unpack(record) | ||
32 | |||
33 | def raw_iter(self): | ||
34 | for i in xrange(len(self)): | ||
35 | record = self.mem[i * RECORD_LEN:(i + 1) * RECORD_LEN] | ||
36 | yield record | ||
37 | |||
38 | def scheduling_intervals(self): | ||
39 | to_id = EVENTS['ST_SWITCH_TO'] | ||
40 | away_id = EVENTS['ST_SWITCH_AWAY'] | ||
41 | |||
42 | # assumption: we are looking at a per-CPU trace | ||
43 | events = self.events_of_type(set((to_id, away_id))) | ||
44 | |||
45 | rec = events.next() | ||
46 | while True: | ||
47 | # fast-forward to next SWITCH_TO | ||
48 | while rec[0] != to_id: | ||
49 | rec = events.next() | ||
50 | |||
51 | to = rec | ||
52 | # next one on this CPU should be a SWITCH_AWAY | ||
53 | rec = events.next() | ||
54 | away = rec | ||
55 | # check for event ID and matching PID and CPU and monotonic time | ||
56 | if away[0] == away_id and away[1] == to[1] and away[2] == to[2] \ | ||
57 | and to[4] <= away[4]: | ||
58 | yield (to, away) | ||