Skip to content

Commit

Permalink
python: cache properties
Browse files Browse the repository at this point in the history
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
jmberg committed May 25, 2010
1 parent c7ef40e commit f45a708
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions tracecmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# 2009-Dec-17: Initial version by Darren Hart <[email protected]>
#

from functools import update_wrapper
from ctracecmd import *

"""
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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 '>'
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f45a708

Please sign in to comment.