diff options
| author | Johannes Berg <johannes@sipsolutions.net> | 2010-05-20 04:12:34 -0400 |
|---|---|---|
| committer | Johannes Berg <johannes@sipsolutions.net> | 2010-05-25 07:17:03 -0400 |
| commit | c7ef40e2af1961a98d8b2d605e65cceef6cfb5d8 (patch) | |
| tree | 09516b1a1f998f4332c0b1a1b53de2fd43b13d83 | |
| parent | c6ed0d9b74dc0ffc29b3e8b11fb199517b2c251e (diff) | |
python: provide more classes
This adds more classes that can be used
from python. Also, change the Event class
a bit so that we pass in the format to be
able to instantiate it from C later.
Acked-by: Darren Hart <dvhltc@us.ibm.com>
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
| -rw-r--r-- | tracecmd.py | 88 |
1 files changed, 75 insertions, 13 deletions
diff --git a/tracecmd.py b/tracecmd.py index aa690f9..12af09b 100644 --- a/tracecmd.py +++ b/tracecmd.py | |||
| @@ -32,12 +32,14 @@ TODO: consider a complete class hierarchy of ftrace events... | |||
| 32 | """ | 32 | """ |
| 33 | 33 | ||
| 34 | class Event(object): | 34 | class Event(object): |
| 35 | def __init__(self, pevent, record, cpu): | 35 | """ |
| 36 | This class can be used to access event data | ||
| 37 | according to an event's record and format. | ||
| 38 | """ | ||
| 39 | def __init__(self, pevent, record, format): | ||
| 36 | self._pevent = pevent | 40 | self._pevent = pevent |
| 37 | self._record = record | 41 | self._record = record |
| 38 | self.cpu = cpu | 42 | self._format = format |
| 39 | type = pevent_data_type(pevent, record) | ||
| 40 | self._format = pevent_data_event_from_type(pevent, type) | ||
| 41 | 43 | ||
| 42 | def __str__(self): | 44 | def __str__(self): |
| 43 | return "%d.%d CPU%d %s: pid=%d comm=%s type=%d" % \ | 45 | return "%d.%d CPU%d %s: pid=%d comm=%s type=%d" % \ |
| @@ -45,8 +47,16 @@ class Event(object): | |||
| 45 | self.num_field("common_pid"), self.comm, self.type) | 47 | self.num_field("common_pid"), self.comm, self.type) |
| 46 | 48 | ||
| 47 | def __del__(self): | 49 | def __del__(self): |
| 48 | free_record(self._record); | 50 | free_record(self._record) |
| 51 | |||
| 52 | def __getitem__(self, n): | ||
| 53 | f = pevent_find_field(self._format, n) | ||
| 54 | if f is None: | ||
| 55 | raise KeyError("no field '%s'" % n) | ||
| 56 | return Field(self._record, f) | ||
| 49 | 57 | ||
| 58 | def keys(self): | ||
| 59 | return py_format_get_keys(self._format) | ||
| 50 | 60 | ||
| 51 | # TODO: consider caching the results of the properties | 61 | # TODO: consider caching the results of the properties |
| 52 | @property | 62 | @property |
| @@ -54,6 +64,10 @@ class Event(object): | |||
| 54 | return pevent_data_comm_from_pid(self._pevent, self.pid) | 64 | return pevent_data_comm_from_pid(self._pevent, self.pid) |
| 55 | 65 | ||
| 56 | @property | 66 | @property |
| 67 | def cpu(self): | ||
| 68 | return record_cpu_get(self._record) | ||
| 69 | |||
| 70 | @property | ||
| 57 | def name(self): | 71 | def name(self): |
| 58 | return event_format_name_get(self._format) | 72 | return event_format_name_get(self._format) |
| 59 | 73 | ||
| @@ -79,6 +93,53 @@ class Event(object): | |||
| 79 | return val | 93 | return val |
| 80 | 94 | ||
| 81 | 95 | ||
| 96 | class TraceSeq(object): | ||
| 97 | def __init__(self, trace_seq): | ||
| 98 | self._trace_seq = trace_seq | ||
| 99 | |||
| 100 | def puts(self, s): | ||
| 101 | return trace_seq_puts(self._trace_seq, s) | ||
| 102 | |||
| 103 | class FieldError(Exception): | ||
| 104 | pass | ||
| 105 | |||
| 106 | class Field(object): | ||
| 107 | def __init__(self, record, field): | ||
| 108 | self._record = record | ||
| 109 | self._field = field | ||
| 110 | |||
| 111 | @property | ||
| 112 | def data(self): | ||
| 113 | return py_field_get_data(self._field, self._record) | ||
| 114 | |||
| 115 | def __long__(self): | ||
| 116 | ret, val = pevent_read_number_field(self._field, | ||
| 117 | record_data_get(self._record)) | ||
| 118 | if ret: | ||
| 119 | raise FieldError("Not a number field") | ||
| 120 | return val | ||
| 121 | __int__ = __long__ | ||
| 122 | |||
| 123 | class PEvent(object): | ||
| 124 | def __init__(self, pevent): | ||
| 125 | self._pevent = pevent | ||
| 126 | |||
| 127 | def _handler(self, cb, s, record, event_fmt): | ||
| 128 | return cb(TraceSeq(s), Event(self._pevent, record, event_fmt)) | ||
| 129 | |||
| 130 | def register_event_handler(self, subsys, event_name, callback): | ||
| 131 | l = lambda s, r, e: self._handler(callback, s, r, e) | ||
| 132 | |||
| 133 | py_pevent_register_event_handler( | ||
| 134 | self._pevent, -1, subsys, event_name, l) | ||
| 135 | |||
| 136 | @property | ||
| 137 | def file_endian(self): | ||
| 138 | if pevent_is_file_bigendian(self._pevent): | ||
| 139 | return '>' | ||
| 140 | return '<' | ||
| 141 | |||
| 142 | |||
| 82 | class FileFormatError(Exception): | 143 | class FileFormatError(Exception): |
| 83 | pass | 144 | pass |
| 84 | 145 | ||
| @@ -107,9 +168,10 @@ class Trace(object): | |||
| 107 | def read_event(self, cpu): | 168 | def read_event(self, cpu): |
| 108 | rec = tracecmd_read_data(self._handle, cpu) | 169 | rec = tracecmd_read_data(self._handle, cpu) |
| 109 | if rec: | 170 | if rec: |
| 110 | #rec.acquire() | 171 | type = pevent_data_type(self._pevent, rec) |
| 111 | #rec.thisown = 1 | 172 | format = pevent_data_event_from_type(self._pevent, type) |
| 112 | return Event(self._pevent, rec, cpu) | 173 | # rec ownership goes over to Event instance |
| 174 | return Event(self._pevent, rec, format) | ||
| 113 | return None | 175 | return None |
| 114 | 176 | ||
| 115 | def read_event_at(self, offset): | 177 | def read_event_at(self, offset): |
| @@ -117,11 +179,11 @@ class Trace(object): | |||
| 117 | # SWIG only returns the CPU if the record is None for some reason | 179 | # SWIG only returns the CPU if the record is None for some reason |
| 118 | if isinstance(res, int): | 180 | if isinstance(res, int): |
| 119 | return None | 181 | return None |
| 120 | rec,cpu = res | 182 | rec, cpu = res |
| 121 | #rec.acquire() | 183 | type = pevent_data_type(self._pevent, rec) |
| 122 | #rec.thisown = 1 | 184 | format = pevent_data_event_from_type(self._pevent, type) |
| 123 | ev = Event(self._pevent, rec, cpu) | 185 | # rec ownership goes over to Event instance |
| 124 | return ev | 186 | return Event(self._pevent, rec, format) |
| 125 | 187 | ||
| 126 | def peek_event(self, cpu): | 188 | def peek_event(self, cpu): |
| 127 | pass | 189 | pass |
