diff mbox series

[1/8] trace-cruncher: Prefix all python class names with 'tc_'

Message ID 20220210152339.363943-2-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series trace-cruncher:Fixes before v0.2 (Beta) | expand

Commit Message

Yordan Karadzhov Feb. 10, 2022, 3:23 p.m. UTC
All classes defined by the high-level python APIs are renamed to
include 'tc_' prefix. This is done in order to avoid confusion with
the equivalent constructor methods, defined by the low-level APIs.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/hist_oop.py                          | 10 ++--
 examples/kprobe_open.py                       |  6 +-
 examples/synth_oop.py                         | 14 ++---
 tracecruncher/ft_utils.py                     | 47 ++++++++-------
 tracecruncher/ks_utils.py                     |  6 +-
 .../tests/1_unit/test_01_ftracepy_unit.py     | 58 +++++++++----------
 6 files changed, 74 insertions(+), 67 deletions(-)
 mode change 100644 => 100755 examples/hist_oop.py
 mode change 100644 => 100755 examples/synth_oop.py
diff mbox series

Patch

diff --git a/examples/hist_oop.py b/examples/hist_oop.py
old mode 100644
new mode 100755
index 1a3aab6..578b699
--- a/examples/hist_oop.py
+++ b/examples/hist_oop.py
@@ -15,7 +15,7 @@  name = 'khist_example_oop'
 
 cmds = ['start', 'stop', 'show', 'continue', 'clear', 'close']
 
-evt = tc.event('kmem', 'kmalloc')
+evt = tc.tc_event('kmem', 'kmalloc')
 
 axes={'call_site': 'sym',
       'bytes_req': 'n'}
@@ -36,8 +36,8 @@  if __name__ == "__main__":
     arg1 = sys.argv[1]
     if arg1.isdigit() or arg1 == 'start':
         # Create the kernel tracing histogram.
-        hist = tc.create_khist(name=name, event=evt, axes=axes, weights=weights,
-                               sort_keys=sort_keys, sort_dir=sort_dir)
+        hist = tc.create_hist(name=name, event=evt, axes=axes, weights=weights,
+                              sort_keys=sort_keys, sort_dir=sort_dir)
         # Start taking data.
         hist.start()
 
@@ -58,8 +58,8 @@  if __name__ == "__main__":
     else:
         # Try to find an existing histogram with the same definition.
         # The returned histogram is detached from the trace-cruncher module.
-        hist = tc.find_khist(name=name, event=evt, axes=axes, weights=weights,
-                             sort_keys=sort_keys, sort_dir=sort_dir)
+        hist = tc.find_hist(name=name, event=evt, axes=axes, weights=weights,
+                            sort_keys=sort_keys, sort_dir=sort_dir)
 
         if arg1 == 'stop':
             # Stop taking data.
diff --git a/examples/kprobe_open.py b/examples/kprobe_open.py
index 141df66..8ab5ae3 100755
--- a/examples/kprobe_open.py
+++ b/examples/kprobe_open.py
@@ -38,9 +38,9 @@  fields = tc.kprobe_add_ptr_arg(name='mode',
                                fields=fields)
 
 # Create the kprobe event.
-open_probe = tc.kprobe(name='open',
-                       func='do_sys_openat2',
-                       fields=fields)
+open_probe = tc.tc_kprobe(name='open',
+                          func='do_sys_openat2',
+                          fields=fields)
 
 tep = tc.local_tep()
 tc.short_kprobe_print(tep, [open_probe])
diff --git a/examples/synth_oop.py b/examples/synth_oop.py
old mode 100644
new mode 100755
index e95f796..201e4df
--- a/examples/synth_oop.py
+++ b/examples/synth_oop.py
@@ -14,25 +14,25 @@  import tracecruncher.ft_utils as tc
 
 # Get the static kernel event 'sched_waking' and 'sched_switch' from system
 # 'sched'. Those two events will be used to create the synthetic event.
-swaking = tc.event('sched', 'sched_waking')
-sswitch = tc.event('sched', 'sched_switch')
+swaking = tc.tc_event('sched', 'sched_waking')
+sswitch = tc.tc_event('sched', 'sched_switch')
 
 # Add to the synth. event two fields from the 'start' event (sched_waking). In the
 # synth. event, the field 'target_cpu' will be renamed to 'cpu'. Use the 'pid' field
 # for matching with the corresponding 'end' event.
-start = tc.ksynth_event_item(event=swaking, fields=['target_cpu', 'prio'], match='pid')
-start = tc.ksynth_field_rename(start, field='target_cpu', name='cpu')
+start = tc.synth_event_item(event=swaking, fields=['target_cpu', 'prio'], match='pid')
+start = tc.synth_field_rename(start, field='target_cpu', name='cpu')
 
 # Add to the synth. event one field from the 'end' event (sched_switch).
 # Use the 'next_pid' field for matching with the corresponding 'start' event.
-end = tc.ksynth_event_item(event=sswitch, fields=['prev_prio'], match='next_pid')
+end = tc.synth_event_item(event=sswitch, fields=['prev_prio'], match='next_pid')
 
 # Define a synthetic event. The 'match' value will be recorder as a field in the
 # synth. event using 'pid' as name. We also add to the synth. event a field that
 # measures the time-difference between the 'start' and 'end' events. This new field
 # will use 'hd' time resolution (nanoseconds).
-synth = tc.ksynth(name='synth_wakeup', start_event=start, end_event=end,
-                  synth_fields=[tc.ksynth_field_deltaT(hd=True)], match_name='pid')
+synth = tc.tc_synth(name='synth_wakeup', start_event=start, end_event=end,
+                    synth_fields=[tc.synth_field_deltaT(hd=True)], match_name='pid')
 
 # Create new instance of Ftrace.
 inst = ft.create_instance()
diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py
index 741887b..f4d1f05 100644
--- a/tracecruncher/ft_utils.py
+++ b/tracecruncher/ft_utils.py
@@ -37,7 +37,7 @@  def short_kprobe_print(tep, events):
             tep.short_kprobe_print(id=e.evt_id, system=e.system, event=e.name)
 
 
-class event:
+class tc_event:
     def __init__(self, system, name, static=True):
         """ Constructor.
         """
@@ -81,7 +81,7 @@  class event:
                               event=self.name)
 
 
-class _kprobe_base(event):
+class _kprobe_base(tc_event):
     def __init__(self, name, func):
         """ Constructor.
         """
@@ -96,7 +96,8 @@  class _kprobe_base(event):
         self.kp.register()
         self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name)
 
-class kprobe(_kprobe_base):
+
+class tc_kprobe(_kprobe_base):
     def __init__(self, name, func, fields):
         """ Constructor.
         """
@@ -106,24 +107,28 @@  class kprobe(_kprobe_base):
         self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe)
         self.register()
 
+
 def kprobe_add_raw_field(name, probe, fields={}):
     """ Add a raw definition of a data field to the probe descriptor.
     """
     fields[str(name)] = str(probe)
     return fields
 
+
 def kprobe_add_arg(name, param_id, param_type, fields={}):
     """ Add a function parameter data field to the probe descriptor.
     """
     probe = '$arg{0}:{1}'.format(param_id, param_type)
     return kprobe_add_raw_field(name=name, probe=probe, fields=fields)
 
+
 def kprobe_add_ptr_arg(name, param_id, param_type, offset=0, fields={}):
     """ Add a pointer function parameter data field to the probe descriptor.
     """
     probe = '+{0}($arg{1}):{2}'.format(offset, param_id, param_type)
     return kprobe_add_raw_field(name=name, probe=probe, fields=fields)
 
+
 def kprobe_add_array_arg(name, param_id, param_type, offset=0,
                          size=-1, fields={}):
     """ Add an array function parameter data field to the probe descriptor.
@@ -138,6 +143,7 @@  def kprobe_add_array_arg(name, param_id, param_type, offset=0,
         probe += '($arg{0})):{1}'.format(param_id, param_type)
         return kprobe_add_raw_field(name=field_name, probe=probe, fields=fields)
 
+
 def kprobe_add_string_arg(name, param_id, offset=0, usr_space=False, fields={}):
     """ Add a string function parameter data field to the probe descriptor.
     """
@@ -148,6 +154,7 @@  def kprobe_add_string_arg(name, param_id, offset=0, usr_space=False, fields={}):
                               offset=offset,
                               fields=fields)
 
+
 def kprobe_add_string_array_arg(name, param_id, offset=0, usr_space=False,
                                 size=-1, fields={}):
     """ Add a string array function parameter data field to the probe descriptor.
@@ -160,6 +167,7 @@  def kprobe_add_string_array_arg(name, param_id, offset=0, usr_space=False,
                                 size=size,
                                 fields=fields)
 
+
 def kprobe_parse_record_array_field(event, record, field, size=-1):
     """ Parse the content of an array function parameter data field.
     """
@@ -177,7 +185,7 @@  def kprobe_parse_record_array_field(event, record, field, size=-1):
     return arr
 
 
-class kretval_probe(_kprobe_base):
+class tc_kretval_probe(_kprobe_base):
     def __init__(self, name, func):
         """ Constructor.
         """
@@ -186,7 +194,7 @@  class kretval_probe(_kprobe_base):
         self.kp.register()
 
 
-class khist:
+class tc_hist:
     def __init__(self, name, event, axes, weights=[],
                  sort_keys=[], sort_dir={}, find=False):
         """ Constructor.
@@ -282,13 +290,12 @@  class khist:
         return self.data()
 
 
-def create_khist(name, event, axes, weights=[],
-                 sort_keys=[], sort_dir={}):
+def create_hist(name, event, axes, weights=[], sort_keys=[], sort_dir={}):
     """ Create new kernel histogram.
     """
     try:
-        hist = khist(name=name, event=event, axes=axes, weights=weights,
-                     sort_keys=sort_keys, sort_dir=sort_dir, find=False)
+        hist = tc_hist(name=name, event=event, axes=axes, weights=weights,
+                       sort_keys=sort_keys, sort_dir=sort_dir, find=False)
     except Exception as err:
         msg = 'Failed to create histogram \'{0}\''.format(name)
         raise RuntimeError(msg) from err
@@ -296,20 +303,20 @@  def create_khist(name, event, axes, weights=[],
     return hist
 
 
-def find_khist(name, event, axes, instance=None,
-               weights=[], sort_keys=[], sort_dir={}):
+def find_hist(name, event, axes, weights=[], sort_keys=[], sort_dir={}):
     """ Find existing kernel histogram.
     """
     try:
-        hist = khist(name=name, event=event, axes=axes, weights=weights,
-                     sort_keys=sort_keys, sort_dir=sort_dir, find=True)
+        hist = tc_hist(name=name, event=event, axes=axes, weights=weights,
+                       sort_keys=sort_keys, sort_dir=sort_dir, find=True)
     except Exception as err:
         msg = 'Failed to find histogram \'{0}\''.format(name)
         raise RuntimeError(msg) from err
 
     return hist
 
-class ksynth(event):
+
+class tc_synth(tc_event):
     def __init__(self, name, start_event, end_event,
                  synth_fields=None, match_name=ft.no_arg()):
         """ Constructor.
@@ -380,7 +387,7 @@  class ksynth(event):
         return self.synth.repr(event=True, hist_start=True, hist_end=True)
 
 
-def ksynth_event_item(event, match, fields=[]):
+def synth_event_item(event, match, fields=[]):
     """ Create descriptor for an event item (component) of a synthetic event.
         To be used as a start/end event.
     """
@@ -393,7 +400,7 @@  def ksynth_event_item(event, match, fields=[]):
     return sub_evt
 
 
-def ksynth_field_rename(event, field, name):
+def synth_field_rename(event, field, name):
     """ Change the name of a field in the event component of a synthetic event.
     """
     pos = event['fields'].index(field)
@@ -402,7 +409,7 @@  def ksynth_field_rename(event, field, name):
     return event
 
 
-def ksynth_field_deltaT(name='delta_T', hd=False):
+def synth_field_deltaT(name='delta_T', hd=False):
     """ Create descriptor for time-diference synthetic field.
     """
     if hd:
@@ -411,19 +418,19 @@  def ksynth_field_deltaT(name='delta_T', hd=False):
     return 'delta_t {0} hd'.format(name)
 
 
-def ksynth_field_delta_start(name, start_field, end_field):
+def synth_field_delta_start(name, start_field, end_field):
     """ Create descriptor for field diference (start - end) synthetic field.
     """
     return 'delta_start {0} {1} {2}'.format(name, start_field, end_field)
 
 
-def ksynth_field_delta_end(name, start_field, end_field):
+def synth_field_delta_end(name, start_field, end_field):
     """ Create descriptor for field diference (end - start)  synthetic field.
     """
     return 'delta_end {0} {1} {2}'.format(name, start_field, end_field)
 
 
-def ksynth_field_sum(name, start_field, end_field):
+def synth_field_sum(name, start_field, end_field):
     """ Create descriptor for field sum synthetic field.
     """
     return 'sum {0} {1} {2}'.format(name, start_field, end_field)
diff --git a/tracecruncher/ks_utils.py b/tracecruncher/ks_utils.py
index 15c7835..90dca31 100644
--- a/tracecruncher/ks_utils.py
+++ b/tracecruncher/ks_utils.py
@@ -21,7 +21,7 @@  def size(data):
     raise Exception('Data size is unknown.')
 
 
-class trace_file_stream:
+class tc_file_stream:
     def __init__(self, file_name='', buffer_name='top'):
         """ Constructor.
         """
@@ -216,12 +216,12 @@  class ks_session:
 def open_file(file_name):
     """ Open a trace file for reading.
     """
-    return trace_file_stream(file_name)
+    return tc_file_stream(file_name)
 
 
 def open_buffer(file_name, buffer_name):
     """ Open a aprticular buffer in a trace file for reading.
     """
-    s = trace_file_stream()
+    s = tc_file_stream()
     s.open_buffer(file_name, buffer_name)
     return s
diff --git a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
index 70f5d29..aea29db 100644
--- a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
+++ b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py
@@ -664,8 +664,8 @@  tgr_file = '{0}/instances/{1}_inst/events/{2}/{3}/trigger'.format(ft.dir(), hist
 
 class HistOopTestCase(unittest.TestCase):
     def test_hist_create(self):
-        evt = tc.event(evt_sys, evt_name)
-        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+        evt = tc.tc_event(evt_sys, evt_name)
+        hist = tc.create_hist(name=hist_name, event=evt, axes=axes, weights=weights,
                                sort_keys=sort_keys, sort_dir=sort_dir)
         f = open(tgr_file)
         h_buff = f.read()
@@ -677,8 +677,8 @@  class HistOopTestCase(unittest.TestCase):
         f.close()
 
     def test_hist_ctrl(self):
-        evt = tc.event(evt_sys, evt_name)
-        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
+        evt = tc.tc_event(evt_sys, evt_name)
+        hist = tc.create_hist(name=hist_name, event=evt, axes=axes, weights=weights,
                                sort_keys=sort_keys, sort_dir=sort_dir)
         f = open(tgr_file)
         h_buff = f.read()
@@ -715,32 +715,32 @@  class HistOopTestCase(unittest.TestCase):
         f.close()
 
     def test_1_detach(self):
-        evt = tc.event(evt_sys, evt_name)
-        hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights,
-                               sort_keys=sort_keys, sort_dir=sort_dir)
+        evt = tc.tc_event(evt_sys, evt_name)
+        hist = tc.create_hist(name=hist_name, event=evt, axes=axes, weights=weights,
+                              sort_keys=sort_keys, sort_dir=sort_dir)
         self.assertTrue(hist.is_attached())
         hist.detach()
         self.assertFalse(hist.is_attached())
 
     def test_2_attach(self):
-        evt = tc.event(evt_sys, evt_name)
-        hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights,
-                             sort_keys=sort_keys, sort_dir=sort_dir)
+        evt = tc.tc_event(evt_sys, evt_name)
+        hist = tc.find_hist(name=hist_name, event=evt, axes=axes, weights=weights,
+                            sort_keys=sort_keys, sort_dir=sort_dir)
         self.assertFalse(hist.is_attached())
         hist.attach()
         self.assertTrue(hist.is_attached())
 
     def test_hist_err(self):
-        evt = tc.event(evt_sys, evt_name)
+        evt = tc.tc_event(evt_sys, evt_name)
 
         err = 'Failed to find histogram'
         with self.assertRaises(Exception) as context:
-            hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights,
-                                 sort_keys=sort_keys, sort_dir=sort_dir)
+            hist = tc.find_hist(name=hist_name, event=evt, axes=axes, weights=weights,
+                                sort_keys=sort_keys, sort_dir=sort_dir)
         self.assertTrue(err in str(context.exception))
 
 
-class SyntTestCase(unittest.TestCase):
+class SynthTestCase(unittest.TestCase):
     def test_synt_create(self):
         synth = ft.synth(name='wakeup_lat',
                          start_sys='sched', start_evt='sched_waking',
@@ -825,32 +825,32 @@  class SyntTestCase(unittest.TestCase):
         self.assertEqual('none', synth.get_filter())
         synth.unregister()
 
-swaking = tc.event('sched', 'sched_waking')
-sswitch = tc.event('sched', 'sched_switch')
+swaking = tc.tc_event('sched', 'sched_waking')
+sswitch = tc.tc_event('sched', 'sched_switch')
 
-class SyntTestCase(unittest.TestCase):
+class SynthOopTestCase(unittest.TestCase):
     def test_synt_create(self):
-        start = tc.ksynth_event_item(event=swaking,
-                                     fields=['target_cpu', 'prio'],
-                                     match='pid')
+        start = tc.synth_event_item(event=swaking,
+                                    fields=['target_cpu', 'prio'],
+                                    match='pid')
         self.assertEqual(start['fields'], ['target_cpu', 'prio'])
         self.assertEqual(start['match'], 'pid')
         self.assertEqual(start['field_names'], [None, None])
 
-        start = tc.ksynth_field_rename(start,
-                                       field='target_cpu', name='cpu')
+        start = tc.synth_field_rename(start,
+                                      field='target_cpu', name='cpu')
         self.assertEqual(start['field_names'], ['cpu', None])
 
-        end = tc.ksynth_event_item(event=sswitch,
-                                   fields=['prev_prio'],
-                                   match='next_pid')
+        end = tc.synth_event_item(event=sswitch,
+                                  fields=['prev_prio'],
+                                  match='next_pid')
         self.assertEqual(end['fields'], ['prev_prio'])
         self.assertEqual(end['match'], 'next_pid')
 
-        synth = tc.ksynth(name='synth_wakeup',
-                          start_event=start, end_event=end,
-                          synth_fields=[tc.ksynth_field_deltaT(hd=True)],
-                          match_name='pid')
+        synth = tc.tc_synth(name='synth_wakeup',
+                            start_event=start, end_event=end,
+                            synth_fields=[tc.synth_field_deltaT(hd=True)],
+                            match_name='pid')
 
         synth_str = synth.__repr__().split('\n')
         event = synth_str[0]