forked from rostedt/trace-cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address one TODO item and cache all properties (since currently all of them are immutable). Acked-by: Darren Hart <[email protected]> Signed-off-by: Johannes Berg <[email protected]>
- Loading branch information
Showing
1 changed file
with
28 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
# 2009-Dec-17: Initial version by Darren Hart <[email protected]> | ||
# | ||
|
||
from functools import update_wrapper | ||
from ctracecmd import * | ||
|
||
""" | ||
|
@@ -31,6 +32,24 @@ | |
TODO: consider a complete class hierarchy of ftrace events... | ||
""" | ||
|
||
def cached_property(func, name=None): | ||
if name is None: | ||
name = func.__name__ | ||
def _get(self): | ||
try: | ||
return self.__cached_properties[name] | ||
except AttributeError: | ||
self.__cached_properties = {} | ||
except KeyError: | ||
pass | ||
value = func(self) | ||
self.__cached_properties[name] = value | ||
return value | ||
update_wrapper(_get, func) | ||
def _del(self): | ||
self.__cached_properties.pop(name, None) | ||
return property(_get, None, _del) | ||
|
||
class Event(object): | ||
""" | ||
This class can be used to access event data | ||
|
@@ -58,28 +77,27 @@ def __getitem__(self, n): | |
def keys(self): | ||
return py_format_get_keys(self._format) | ||
|
||
# TODO: consider caching the results of the properties | ||
@property | ||
@cached_property | ||
def comm(self): | ||
return pevent_data_comm_from_pid(self._pevent, self.pid) | ||
|
||
@property | ||
@cached_property | ||
def cpu(self): | ||
return record_cpu_get(self._record) | ||
|
||
@property | ||
@cached_property | ||
def name(self): | ||
return event_format_name_get(self._format) | ||
|
||
@property | ||
@cached_property | ||
def pid(self): | ||
return pevent_data_pid(self._pevent, self._record) | ||
|
||
@property | ||
@cached_property | ||
def ts(self): | ||
return record_ts_get(self._record) | ||
|
||
@property | ||
@cached_property | ||
def type(self): | ||
return pevent_data_type(self._pevent, self._record) | ||
|
||
|
@@ -108,7 +126,7 @@ def __init__(self, record, field): | |
self._record = record | ||
self._field = field | ||
|
||
@property | ||
@cached_property | ||
def data(self): | ||
return py_field_get_data(self._field, self._record) | ||
|
||
|
@@ -133,7 +151,7 @@ def register_event_handler(self, subsys, event_name, callback): | |
py_pevent_register_event_handler( | ||
self._pevent, -1, subsys, event_name, l) | ||
|
||
@property | ||
@cached_property | ||
def file_endian(self): | ||
if pevent_is_file_bigendian(self._pevent): | ||
return '>' | ||
|
@@ -161,7 +179,7 @@ def __init__(self, filename): | |
|
||
self._pevent = tracecmd_get_pevent(self._handle) | ||
|
||
@property | ||
@cached_property | ||
def cpus(self): | ||
return tracecmd_cpus(self._handle) | ||
|
||
|