aboutsummaryrefslogtreecommitdiffstats
path: root/sched_trace/file.py
blob: 5c18163867ebf0c20fdb8b63edd76ecd7a75558f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Convenience wrapper around mmap'ed sched_trace binary files.
"""

from .format import RECORD_LEN, unpack, get_event, EVENTS

import mmap

class SchedTraceFile(object):
    def __init__(self, fname):
        self.source = fname
        with open(fname, 'rw') as f:
            self.mem = mmap.mmap(f.fileno(), 0, mmap.MAP_PRIVATE)

    def __len__(self):
        return len(self.mem) / RECORD_LEN

    def events_of_type(self, event_ids):
        for i in xrange(len(self)):
            if get_event(self.mem, i) in event_ids:
                record = self.mem[i * RECORD_LEN:]
                yield unpack(record)

    def __iter__(self):
        for i in xrange(len(self)):
            record = self.mem[i * RECORD_LEN:]
            yield unpack(record)

    def __reversed__(self):
        for i in reversed(xrange(len(self))):
            record = self.mem[i * RECORD_LEN:]
            yield unpack(record)

    def raw_iter(self):
        for i in xrange(len(self)):
            record = self.mem[i * RECORD_LEN:(i + 1) * RECORD_LEN]
            yield record

    def scheduling_intervals(self):
        to_id = EVENTS['ST_SWITCH_TO']
        away_id = EVENTS['ST_SWITCH_AWAY']

        # assumption: we are looking at a per-CPU trace
        events = self.events_of_type(set((to_id, away_id)))

        rec = events.next()
        while True:
            # fast-forward to next SWITCH_TO
            while rec[0] != to_id:
                rec = events.next()

            to = rec
            # next one on this CPU should be a SWITCH_AWAY
            rec = events.next()
            away = rec
            # check for event ID and matching PID and CPU and monotonic time
            if away[0] == away_id and away[1] == to[1] and away[2] == to[2] \
               and to[4] <= away[4]:
                yield (to, away)