Skip to contentSkip to conversation

Tracetool cleanup

PATCH In review Lluís Vilanova 3 participants 10 messages Feb 17, 2014 – Feb 20, 2014

lore
Share & tools
lore
#0 lore raw

cover Tracetool cleanup

Minimizes the amount of backend code, making it simpler to add new/different backends.

Also performs other cleanups all around.

---

Lluís Vilanova (4):
trace: [tracetool] Add method 'Event.api' to build event names
trace: [tracetool,trivial] Style changes
trace: [tracetool] Identify formats directly used by QEMU
trace: [tracetool] Minimize the amount of per-backend code

 scripts/simpletrace.py                |    6 --
 scripts/tracetool/__init__.py         |   53 +++++++------
 scripts/tracetool/backend/__init__.py |   74 ++++++++-----------
 scripts/tracetool/backend/dtrace.py   |   81 ++------------------
 scripts/tracetool/backend/events.py   |   23 ------
 scripts/tracetool/backend/ftrace.py   |   56 ++++++--------
 scripts/tracetool/backend/simple.py   |  132 ++++++++++++++++-----------------
 scripts/tracetool/backend/stderr.py   |   43 ++++-------
 scripts/tracetool/backend/ust.py      |   44 +++++------
 scripts/tracetool/format/__init__.py  |   61 +++++++--------
 scripts/tracetool/format/c.py         |   16 +++-
 scripts/tracetool/format/d.py         |   30 +++++++-
 scripts/tracetool/format/events_c.py  |   18 ++---
 scripts/tracetool/format/events_h.py  |   18 ++---
 scripts/tracetool/format/h.py         |   33 +++++---
 scripts/tracetool/format/stap.py      |   42 ++++++++++-
 trace/Makefile.objs                   |    4 +
 17 files changed, 343 insertions(+), 391 deletions(-)
 delete mode 100644 scripts/tracetool/backend/events.py

To: [email protected]
Cc: Stefan Hajnoczi <[email protected]>
Cc: Kazuya Saito <[email protected]>

#1 lore raw

PATCH 1/4 trace: [tracetool] Add method 'Event.api' to build event names

Makes it easier to ensure proper naming across the different frontends and backends.

---
 scripts/tracetool/__init__.py       |   10 +++++++++-
 scripts/tracetool/backend/dtrace.py |    6 +++---
 scripts/tracetool/backend/simple.py |   10 +++++-----
 scripts/tracetool/backend/stderr.py |    5 +++--
 scripts/tracetool/backend/ust.py    |    8 +++++---
 scripts/tracetool/format/h.py       |    6 +++---
 6 files changed, 28 insertions(+), 17 deletions(-)
scripts/tracetool/__init__.py +9 -1
index 175df08..7f9161d 100644
@@ -6,7 +6,7 @@ Machinery for generating tracing-related intermediate files.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -173,6 +173,14 @@ class Event(object):
                                           self.args,
                                           self.fmt)
 
+    QEMU_TRACE              = "trace_%(name)s"
+
+    def api(self, fmt=None):
+        if fmt is None:
+            fmt = Event.QEMU_TRACE
+        return fmt % {"name": self.name}
+
+
 def _read_events(fobj):
     res = []
     for line in fobj:
scripts/tracetool/backend/dtrace.py +3 -3
index e31bc79..3c369c4 100644
@@ -6,7 +6,7 @@ DTrace/SystemTAP backend.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -44,10 +44,10 @@ def h(events):
         '')
 
     for e in events:
-        out('static inline void trace_%(name)s(%(args)s) {',
+        out('static inline void %(api)s(%(args)s) {',
             '    QEMU_%(uppername)s(%(argnames)s);',
             '}',
-            name = e.name,
+            api = e.api(),
             args = e.args,
             uppername = e.name.upper(),
             argnames = ", ".join(e.args.names()),
scripts/tracetool/backend/simple.py +5 -5
index 3dde372..ca48e12 100644
@@ -6,7 +6,7 @@ Simple built-in backend.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -34,10 +34,10 @@ def c(events):
         )
 
     for num, event in enumerate(events):
-        out('void trace_%(name)s(%(args)s)',
+        out('void %(api)s(%(args)s)',
             '{',
             '    TraceBufferRecord rec;',
-            name = event.name,
+            api = event.api(),
             args = event.args,
             )
         sizes = []
@@ -95,7 +95,7 @@ def c(events):
 
 def h(events):
     for event in events:
-        out('void trace_%(name)s(%(args)s);',
-            name = event.name,
+        out('void %(api)s(%(args)s);',
+            api = event.api(),
             args = event.args,
             )
scripts/tracetool/backend/stderr.py +3 -2
index 6f93dbd..6681e26 100644
@@ -6,7 +6,7 @@ Stderr built-in backend.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -33,13 +33,14 @@ def h(events):
         if len(e.args) > 0:
             argnames = ", " + argnames
 
-        out('static inline void trace_%(name)s(%(args)s)',
+        out('static inline void %(api)s(%(args)s)',
             '{',
             '    bool _state = trace_event_get_state(%(event_id)s);',
             '    if (_state) {',
             '        fprintf(stderr, "%(name)s " %(fmt)s "\\n" %(argnames)s);',
             '    }',
             '}',
+            api = e.api(),
             name = e.name,
             args = e.args,
             event_id = "TRACE_" + e.name.upper(),
scripts/tracetool/backend/ust.py +5 -3
index ea36995..180b1bf 100644
@@ -6,7 +6,7 @@ LTTng User Space Tracing backend.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -78,7 +78,8 @@ def h(events):
     for e in events:
         if len(e.args) > 0:
             out('DECLARE_TRACE(ust_%(name)s, TP_PROTO(%(args)s), TP_ARGS(%(argnames)s));',
-                '#define trace_%(name)s trace_ust_%(name)s',
+                '#define %(api)s trace_ust_%(name)s',
+                api = e.api(),
                 name = e.name,
                 args = e.args,
                 argnames = ", ".join(e.args.names()),
@@ -86,7 +87,8 @@ def h(events):
 
         else:
             out('_DECLARE_TRACEPOINT_NOARGS(ust_%(name)s);',
-                '#define trace_%(name)s trace_ust_%(name)s',
+                '#define %(api)s trace_ust_%(name)s',
+                api = e.api(),
                 name = e.name,
                 )
 
scripts/tracetool/format/h.py +3 -3
index 93132fc..9b0903d 100644
@@ -6,7 +6,7 @@ Generate .h file.
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -30,9 +30,9 @@ def end(events):
 def nop(events):
     for e in events:
         out('',
-            'static inline void trace_%(name)s(%(args)s)',
+            'static inline void %(api)s(%(args)s)',
             '{',
             '}',
-            name = e.name,
+            api = e.api(),
             args = e.args,
             )
#2 lore raw

PATCH 2/4 trace: [tracetool,trivial] Style changes

In reply to Lluís Vilanova
---
 scripts/tracetool/__init__.py         |   25 +++++++++++++++----------
 scripts/tracetool/backend/__init__.py |    2 +-
 scripts/tracetool/format/__init__.py  |    2 +-
 scripts/tracetool/format/c.py         |    4 ++--
 scripts/tracetool/format/d.py         |    4 ++--
 scripts/tracetool/format/events_c.py  |    4 ++--
 scripts/tracetool/format/events_h.py  |    4 ++--
 scripts/tracetool/format/h.py         |    2 +-
 8 files changed, 26 insertions(+), 21 deletions(-)
scripts/tracetool/__init__.py +15 -10
index 7f9161d..cca5072 100644
@@ -144,7 +144,12 @@ class Event(object):
 
         unknown_props = set(self.properties) - self._VALID_PROPS
         if len(unknown_props) > 0:
-            raise ValueError("Unknown properties: %s" % ", ".join(unknown_props))
+            raise ValueError("Unknown properties: %s" %
+                             ", ".join(unknown_props))
+
+    def copy(other):
+        return Event(other.name, list(other.properties), other.fmt, other.args,
+                     other)
 
     @staticmethod
     def build(line_str):
@@ -197,7 +202,7 @@ class TracetoolError (Exception):
     pass
 
 
-def try_import(mod_name, attr_name = None, attr_default = None):
+def try_import(mod_name, attr_name=None, attr_default=None):
     """Try to import a module and get an attribute from it.
 
     Parameters
@@ -224,7 +229,7 @@ def try_import(mod_name, attr_name = None, attr_default = None):
 
 
 def generate(fevents, format, backend,
-             binary = None, probe_prefix = None):
+             binary=None, probe_prefix=None):
     """Generate the output for the given (format, backend) pair.
 
     Parameters
@@ -246,18 +251,18 @@ def generate(fevents, format, backend,
     format = str(format)
     if len(format) is 0:
         raise TracetoolError("format not set")
-    mformat = format.replace("-", "_")
-    if not tracetool.format.exists(mformat):
+    if not tracetool.format.exists(format):
         raise TracetoolError("unknown format: %s" % format)
+    format = format.replace("-", "_")
 
     backend = str(backend)
     if len(backend) is 0:
         raise TracetoolError("backend not set")
-    mbackend = backend.replace("-", "_")
-    if not tracetool.backend.exists(mbackend):
+    if not tracetool.backend.exists(backend):
         raise TracetoolError("unknown backend: %s" % backend)
+    backend = backend.replace("-", "_")
 
-    if not tracetool.backend.compatible(mbackend, mformat):
+    if not tracetool.backend.compatible(backend, format):
         raise TracetoolError("backend '%s' not compatible with format '%s'" %
                              (backend, format))
 
@@ -270,7 +275,7 @@ def generate(fevents, format, backend,
     if backend == "nop":
         ( e.properies.add("disable") for e in events )
 
-    tracetool.format.generate_begin(mformat, events)
+    tracetool.format.generate_begin(format, events)
     tracetool.backend.generate("nop", format,
                                [ e
                                  for e in events
@@ -279,4 +284,4 @@ def generate(fevents, format, backend,
                                [ e
                                  for e in events
                                  if "disable" not in e.properties ])
-    tracetool.format.generate_end(mformat, events)
+    tracetool.format.generate_end(format, events)
scripts/tracetool/backend/__init__.py +1 -1
index f0314ee..88f94fd 100644
@@ -59,7 +59,7 @@ def get_list(only_public = False):
     for filename in os.listdir(tracetool.backend.__path__[0]):
         if filename.endswith('.py') and filename != '__init__.py':
             modnames.append(filename.rsplit('.', 1)[0])
-    for modname in modnames:
+    for modname in sorted(modnames):
         module = tracetool.try_import("tracetool.backend." + modname)
 
         # just in case; should never fail unless non-module files are put there
scripts/tracetool/format/__init__.py +1 -1
index 3c2a0d8..577a224 100644
@@ -53,7 +53,7 @@ def get_list():
     for filename in os.listdir(tracetool.format.__path__[0]):
         if filename.endswith('.py') and filename != '__init__.py':
             modnames.append(filename.rsplit('.', 1)[0])
-    for modname in modnames:
+    for modname in sorted(modnames):
         module = tracetool.try_import("tracetool.format." + modname)
 
         # just in case; should never fail unless non-module files are put there
scripts/tracetool/format/c.py +2 -2
index 35555ae..930140b 100644
@@ -2,11 +2,11 @@
 # -*- coding: utf-8 -*-
 
 """
-Generate .c file.
+trace/generated-tracers.c
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
scripts/tracetool/format/d.py +2 -2
index a2d5947..74ee0d3 100644
@@ -2,11 +2,11 @@
 # -*- coding: utf-8 -*-
 
 """
-Generate .d file (DTrace only).
+trace/generated-tracers.dtrace (DTrace only).
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
scripts/tracetool/format/events_c.py +2 -2
index d670ec8..ea668ee 100644
@@ -2,11 +2,11 @@
 # -*- coding: utf-8 -*-
 
 """
-Generate .c for event description.
+trace/generated-events.c
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
scripts/tracetool/format/events_h.py +2 -2
index d30ccea..f3febae 100644
@@ -2,11 +2,11 @@
 # -*- coding: utf-8 -*-
 
 """
-Generate .h for event description.
+trace/generated-events.h
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
scripts/tracetool/format/h.py +1 -1
index 9b0903d..85f011f 100644
@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 
 """
-Generate .h file.
+trace/generated-tracers.h
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
#3 lore raw

PATCH 3/4 trace: [tracetool] Identify formats directly used by QEMU

In reply to Lluís Vilanova
---
 scripts/tracetool/format/__init__.py |   13 ++++++++++++-
 scripts/tracetool/format/c.py        |    3 +++
 scripts/tracetool/format/d.py        |    3 +++
 scripts/tracetool/format/events_c.py |    3 +++
 scripts/tracetool/format/events_h.py |    3 +++
 scripts/tracetool/format/h.py        |    3 +++
 scripts/tracetool/format/stap.py     |    5 ++++-
 7 files changed, 31 insertions(+), 2 deletions(-)
scripts/tracetool/format/__init__.py +12 -1
index 577a224..ddbcac9 100644
@@ -17,6 +17,17 @@ considered its short description.
 All formats must generate their contents through the 'tracetool.out' routine.
 
 
+Format attributes
+-----------------
+
+========= =================================================================
+Attribute Description
+========= =================================================================
+API       If exists and is set to 'True', the format defines identifiers
+          (functions, variables, defines, etc.) directly used by QEMU code.
+========= =================================================================
+
+
 Format functions
 ----------------
 
@@ -34,7 +45,7 @@ nop      Called to generate the per-event contents when the event is disabled or
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
scripts/tracetool/format/c.py +3 -0
index 930140b..8327dae 100644
@@ -16,5 +16,8 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = False
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */')
scripts/tracetool/format/d.py +3 -0
index 74ee0d3..4d784d3 100644
@@ -16,5 +16,8 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = False
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */')
scripts/tracetool/format/events_c.py +3 -0
index ea668ee..030dd37 100644
@@ -16,6 +16,9 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = True
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
scripts/tracetool/format/events_h.py +3 -0
index f3febae..983f2b2 100644
@@ -16,6 +16,9 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = True
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
scripts/tracetool/format/h.py +3 -0
index 85f011f..2809730 100644
@@ -16,6 +16,9 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = True
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
scripts/tracetool/format/stap.py +4 -1
index 50a4c69..134c197 100644
@@ -6,7 +6,7 @@ Generate .stp file (DTrace with SystemTAP only).
 """
 
 __author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
+__copyright__  = "Copyright 2012-2014, Lluís Vilanova <[email protected]>"
 __license__    = "GPL version 2 or (at your option) any later version"
 
 __maintainer__ = "Stefan Hajnoczi"
@@ -16,5 +16,8 @@ __email__      = "[email protected]"
 from tracetool import out
 
 
+API = False
+
+
 def begin(events):
     out('/* This file is autogenerated by tracetool, do not edit. */')
#4 lore raw

PATCH 4/4 trace: [tracetool] Minimize the amount of per-backend code

In reply to Lluís Vilanova

Backends now only contain the essential backend-specific code, and most of the work is moved to frontend code.

---
 scripts/simpletrace.py                |    6 --
 scripts/tracetool/__init__.py         |   24 ++----
 scripts/tracetool/backend/__init__.py |   72 ++++++++----------
 scripts/tracetool/backend/dtrace.py   |   79 ++------------------
 scripts/tracetool/backend/events.py   |   23 ------
 scripts/tracetool/backend/ftrace.py   |   56 ++++++--------
 scripts/tracetool/backend/simple.py   |  130 ++++++++++++++++-----------------
 scripts/tracetool/backend/stderr.py   |   42 ++++-------
 scripts/tracetool/backend/ust.py      |   44 +++++------
 scripts/tracetool/format/__init__.py  |   46 ++++--------
 scripts/tracetool/format/c.py         |    9 ++
 scripts/tracetool/format/d.py         |   23 +++++-
 scripts/tracetool/format/events_c.py  |   11 +--
 scripts/tracetool/format/events_h.py  |   11 +--
 scripts/tracetool/format/h.py         |   24 ++++--
 scripts/tracetool/format/stap.py      |   37 +++++++++
 trace/Makefile.objs                   |    4 +
 17 files changed, 274 insertions(+), 367 deletions(-)
 delete mode 100644 scripts/tracetool/backend/events.py
scripts/simpletrace.py +1 -5
index 8bbcb42..03d032e 100755
@@ -109,14 +109,10 @@ def process(events, log, analyzer):
     if isinstance(log, str):
         log = open(log, 'rb')
 
-    enabled_events = []
     dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")
     edict = {dropped_event_id: dropped_event}
 
-    for e in events:
-        if 'disable' not in e.properties:
-            enabled_events.append(e)
-    for num, event in enumerate(enabled_events):
+    for num, event in enumerate(events):
         edict[num] = event
 
     def build_fn(analyzer, event):
scripts/tracetool/__init__.py +7 -17
index cca5072..fd705b6 100644
@@ -261,10 +261,7 @@ def generate(fevents, format, backend,
     if not tracetool.backend.exists(backend):
         raise TracetoolError("unknown backend: %s" % backend)
     backend = backend.replace("-", "_")
-
-    if not tracetool.backend.compatible(backend, format):
-        raise TracetoolError("backend '%s' not compatible with format '%s'" %
-                             (backend, format))
+    backend = tracetool.backend.Wrapper(backend, format)
 
     import tracetool.backend.dtrace
     tracetool.backend.dtrace.BINARY = binary
@@ -272,16 +269,9 @@ def generate(fevents, format, backend,
 
     events = _read_events(fevents)
 
-    if backend == "nop":
-        ( e.properies.add("disable") for e in events )
-
-    tracetool.format.generate_begin(format, events)
-    tracetool.backend.generate("nop", format,
-                               [ e
-                                 for e in events
-                                 if "disable" in e.properties ])
-    tracetool.backend.generate(backend, format,
-                               [ e
-                                 for e in events
-                                 if "disable" not in e.properties ])
-    tracetool.format.generate_end(format, events)
+    # Except for QEMU's API, formats don't care about disabled events
+    format_api = tracetool.try_import("tracetool.frontend." + format,
+                                      "API", False)
+    if not format_api:
+        events = [e for e in events if "disable" not in e.properties]
+    tracetool.format.generate(events, format, backend)
scripts/tracetool/backend/__init__.py +32 -40

88 lines not on this page · raw message

scripts/tracetool/backend/dtrace.py +8 -71

96 lines not on this page · raw message

scripts/tracetool/backend/events.py deleted +0 -23
deleted file mode 100644
index 5afce3e..0000000
@@ -1,23 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-"""
-Generic event description.
-
-This is a dummy backend to establish appropriate frontend/backend compatibility
-checks.
-"""
-
-__author__     = "Lluís Vilanova <[email protected]>"
-__copyright__  = "Copyright 2012, Lluís Vilanova <[email protected]>"
-__license__    = "GPL version 2 or (at your option) any later version"
-
-__maintainer__ = "Stefan Hajnoczi"
-__email__      = "[email protected]"
-
-
-def events_h(events):
-    pass
-
-def events_c(events):
-    pass
scripts/tracetool/backend/ftrace.py +25 -31

61 lines not on this page · raw message

scripts/tracetool/backend/simple.py +64 -66

140 lines not on this page · raw message

scripts/tracetool/backend/stderr.py +16 -26

47 lines not on this page · raw message

scripts/tracetool/backend/ust.py +19 -25

76 lines not on this page · raw message

scripts/tracetool/format/__init__.py +14 -32

56 lines not on this page · raw message

scripts/tracetool/format/c.py +7 -2
index 8327dae..c12d25f 100644
@@ -19,5 +19,10 @@ from tracetool import out
 API = False
 
 
-def begin(events):
-    out('/* This file is autogenerated by tracetool, do not edit. */')
+def generate(events, backend):
+    out('/* This file is autogenerated by tracetool, do not edit. */',
+        '')
+    backend.generate_begin(events)
+    for event in events:
+        backend.generate(event)
+    backend.generate_end(events)
scripts/tracetool/format/d.py +21 -2
index 4d784d3..80c7090 100644
@@ -19,5 +19,24 @@ from tracetool import out
 API = False
 
 
-def begin(events):
-    out('/* This file is autogenerated by tracetool, do not edit. */')
+def generate(events, backend):
+    out('/* This file is autogenerated by tracetool, do not edit. */'
+        '',
+        'provider qemu {')
+
+    for e in events:
+        args = str(e.args)
+
+        # DTrace provider syntax expects foo() for empty
+        # params, not foo(void)
+        if args == 'void':
+            args = ''
+
+        # Define prototype for probe arguments
+        out('',
+            'probe %(name)s(%(args)s);',
+            name=e.name,
+            args=args)
+
+    out('',
+        '};')
scripts/tracetool/format/events_c.py +4 -7
index 030dd37..03cd14b 100644
@@ -19,14 +19,13 @@ from tracetool import out
 API = True
 
 
-def begin(events):
+def generate(events, backend):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
         '#include "trace.h"',
         '#include "trace/generated-events.h"',
         '#include "trace/control.h"',
-        '',
-        )
+        '')
 
     out('TraceEvent trace_events[TRACE_EVENT_COUNT] = {')
 
@@ -34,9 +33,7 @@ def begin(events):
         out('    { .id = %(id)s, .name = \"%(name)s\", .sstate = %(sstate)s, .dstate = 0 },',
             id = "TRACE_" + e.name.upper(),
             name = e.name,
-            sstate = "TRACE_%s_ENABLED" % e.name.upper(),
-            )
+            sstate = "TRACE_%s_ENABLED" % e.name.upper())
 
     out('};',
-        '',
-        )
+        '')
scripts/tracetool/format/events_h.py +4 -7
index 983f2b2..a56aef6 100644
@@ -19,15 +19,14 @@ from tracetool import out
 API = True
 
 
-def begin(events):
+def generate(events, backend):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
         '#ifndef TRACE__GENERATED_EVENTS_H',
         '#define TRACE__GENERATED_EVENTS_H',
         '',
         '#include <stdbool.h>',
-        ''
-        )
+        '')
 
     # event identifiers
     out('typedef enum {')
@@ -36,8 +35,7 @@ def begin(events):
         out('    TRACE_%s,' % e.name.upper())
 
     out('    TRACE_EVENT_COUNT',
-        '} TraceEventID;',
-        )
+        '} TraceEventID;')
 
     # static state
     for e in events:
@@ -49,5 +47,4 @@ def begin(events):
 
     out('#include "trace/event-internal.h"',
         '',
-        '#endif  /* TRACE__GENERATED_EVENTS_H */',
-        )
+        '#endif  /* TRACE__GENERATED_EVENTS_H */')
scripts/tracetool/format/h.py +15 -9
index 2809730..a8b9ac3 100644
@@ -19,23 +19,29 @@ from tracetool import out
 API = True
 
 
-def begin(events):
+def generate(events, backend):
     out('/* This file is autogenerated by tracetool, do not edit. */',
         '',
         '#ifndef TRACE__GENERATED_TRACERS_H',
         '#define TRACE__GENERATED_TRACERS_H',
         '',
-        '#include "qemu-common.h"')
+        '#include "qemu-common.h"',
+        '')
 
-def end(events):
-    out('#endif /* TRACE__GENERATED_TRACERS_H */')
+    backend.generate_begin(events)
 
-def nop(events):
     for e in events:
         out('',
             'static inline void %(api)s(%(args)s)',
             '{',
-            '}',
-            api = e.api(),
-            args = e.args,
-            )
+            api=e.api(),
+            args=e.args)
+
+        if "disable" not in e.properties:
+            backend.generate(e)
+
+        out('}')
+
+    backend.generate_end(events)
+
+    out('#endif /* TRACE__GENERATED_TRACERS_H */')
scripts/tracetool/format/stap.py +35 -2
index 134c197..42847ab 100644
@@ -14,10 +14,43 @@ __email__      = "[email protected]"
 
 
 from tracetool import out
+from tracetool.backend.dtrace import binary, probeprefix
 
 
 API = False
 
 
-def begin(events):
-    out('/* This file is autogenerated by tracetool, do not edit. */')
+# Technically 'self' is not used by systemtap yet, but
+# they recommended we keep it in the reserved list anyway
+RESERVED_WORDS = (
+    'break', 'catch', 'continue', 'delete', 'else', 'for',
+    'foreach', 'function', 'global', 'if', 'in', 'limit',
+    'long', 'next', 'probe', 'return', 'self', 'string',
+    'try', 'while'
+    )
+
+
+def generate(events, backend):
+    out('/* This file is autogenerated by tracetool, do not edit. */',
+        '')
+
+    for e in events:
+        # Define prototype for probe arguments
+        out('probe %(probeprefix)s.%(name)s = process("%(binary)s").mark("%(name)s")',
+            '{',
+            probeprefix=probeprefix(),
+            name=e.name,
+            binary=binary())
+
+        i = 1
+        if len(e.args) > 0:
+            for name in e.args.names():
+                # Append underscore to reserved keywords
+                if name in RESERVED_WORDS:
+                    name += '_'
+                out('  %s = $arg%d;' % (name, i))
+                i += 1
+
+        out('}')
+
+    out()
trace/Makefile.objs +2 -2
index 3b88e49..68d905b 100644
@@ -7,7 +7,7 @@ $(obj)/generated-events.h: $(obj)/generated-events.h-timestamp
 $(obj)/generated-events.h-timestamp: $(SRC_PATH)/trace-events
 	$(call quiet-command,$(TRACETOOL) \
 		--format=events-h \
-		--backend=events \
+		--backend=$(TRACE_BACKEND) \
 		< $< > $@,"  GEN   $(patsubst %-timestamp,%,$@)")
 	@cmp -s $@ $(patsubst %-timestamp,%,$@) || cp $@ $(patsubst %-timestamp,%,$@)
 
@@ -15,7 +15,7 @@ $(obj)/generated-events.c: $(obj)/generated-events.c-timestamp $(BUILD_DIR)/conf
 $(obj)/generated-events.c-timestamp: $(SRC_PATH)/trace-events
 	$(call quiet-command,$(TRACETOOL) \
 		--format=events-c \
-		--backend=events \
+		--backend=$(TRACE_BACKEND) \
 		< $< > $@,"  GEN   $(patsubst %-timestamp,%,$@)")
 	@cmp -s $@ $(patsubst %-timestamp,%,$@) || cp $@ $(patsubst %-timestamp,%,$@)
 
#5 lore raw

Re: [Qemu-devel] [PATCH 4/4] trace: [tracetool] Minimize the amount of per-backend code

Quoted 15 lines · Lluís Vilanova

Backends now only contain the essential backend-specific code, and most of the work is moved to frontend code.

---
 scripts/simpletrace.py                |    6 --
 scripts/tracetool/__init__.py         |   24 ++----
 scripts/tracetool/backend/__init__.py |   72 ++++++++----------
 scripts/tracetool/backend/dtrace.py   |   79 ++------------------
 scripts/tracetool/backend/events.py   |   23 ------
 scripts/tracetool/backend/ftrace.py   |   56 ++++++--------
 scripts/tracetool/backend/simple.py   |  130 ++++++++++++++++-----------------
 scripts/tracetool/backend/stderr.py   |   42 ++++-------
 scripts/tracetool/backend/ust.py      |   44 +++++------
 scripts/tracetool/format/__init__.py  |   46 ++++--------
 scripts/tracetool/format/c.py         |    9 ++
Quoted 8 lines
 scripts/tracetool/format/d.py         |   23 +++++-
 scripts/tracetool/format/events_c.py  |   11 +--
 scripts/tracetool/format/events_h.py  |   11 +--
 scripts/tracetool/format/h.py         |   24 ++++--
 scripts/tracetool/format/stap.py      |   37 +++++++++
 trace/Makefile.objs                   |    4 +
 17 files changed, 274 insertions(+), 367 deletions(-)
 delete mode 100644 scripts/tracetool/backend/events.py

Please split the coding style changes into a separate commit (e.g. removing trailing commas).

Quoted 10 lines
scripts/simpletrace.py +0 -1
index 8bbcb42..03d032e 100755
@@ -109,14 +109,10 @@ def process(events, log, analyzer):
     if isinstance(log, str):
         log = open(log, 'rb')
 
-    enabled_events = []
     dropped_event = Event.build("Dropped_Event(uint64_t num_events_dropped)")

scripts/simpletrace.py def process(events, log, analyzer): · line 118

Quoted 10 lines
scripts/simpletrace.py +1 -4
     edict = {dropped_event_id: dropped_event}
 
-    for e in events:
-        if 'disable' not in e.properties:
-            enabled_events.append(e)
-    for num, event in enumerate(enabled_events):
+    for num, event in enumerate(events):
         edict[num] = event
 
     def build_fn(analyzer, event):

This patch breaks existing simpletrace files. Imagine you are developing code and use simpletrace. Then you decide to rebase onto the latest qemu.git/master. This patch changes the semantics - now simpletrace.py outputs junk when analyzing the existing file.

We can't break the file format for convenience. Either bump the file format version number or don't change the semantics.

#6 lore raw
In reply to Lluís Vilanova
Quoted 5 lines · Lluís Vilanova

Minimizes the amount of backend code, making it simpler to add new/different backends.

Also performs other cleanups all around.

Quoted 8 lines

---

Lluís Vilanova (4):
trace: [tracetool] Add method 'Event.api' to build event names
trace: [tracetool,trivial] Style changes
trace: [tracetool] Identify formats directly used by QEMU
trace: [tracetool] Minimize the amount of per-backend code

I think we stretched the concepts of backends and formats too far. There are formats that only work with one backend (like 'stap'). And there are backends that behave differently from all other backends.

As a result we're trying to abstract and make common a bunch of stuff that isn't really common. This problem existed before this patch series, but I feel we're going further down a direction that increasingly seems to be wrong.

It's simpler if we turn the design inside-out. Instead of making backends export all sorts of interfaces and flags, tracetool should just parse trace-events and hand the list over to the backend.

Let the backend do whatever it wants. The format option simply becomes an option telling the backend which type of output to generate (events.h, events.c, .stp, etc).

Common behavior should live in plain old Python modules/functions.

TL;DR moving to a library design would simplify and clean up more than trying to improve the current framework design

What do you think?

Stefan

#7 lore raw
Quoted 32 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Minimizes the amount of backend code, making it simpler to add new/different backends.

Also performs other cleanups all around.

Quoted 8 lines

---

Lluís Vilanova (4):
trace: [tracetool] Add method 'Event.api' to build event names
trace: [tracetool,trivial] Style changes
trace: [tracetool] Identify formats directly used by QEMU
trace: [tracetool] Minimize the amount of per-backend code

I think we stretched the concepts of backends and formats too far. There are formats that only work with one backend (like 'stap'). And there are backends that behave differently from all other backends.

As a result we're trying to abstract and make common a bunch of stuff that isn't really common. This problem existed before this patch series, but I feel we're going further down a direction that increasingly seems to be wrong.

It's simpler if we turn the design inside-out. Instead of making backends export all sorts of interfaces and flags, tracetool should just parse trace-events and hand the list over to the backend.

Let the backend do whatever it wants. The format option simply becomes an option telling the backend which type of output to generate (events.h, events.c, .stp, etc).

Quoted 6 lines

Common behavior should live in plain old Python modules/functions.

TL;DR moving to a library design would simplify and clean up more than trying to improve the current framework design

What do you think?

This series moves into that direction; some of the formats are simply not handled by backends. For example, the "stap", "events_c" and "events_h" formats have no backend-specific contents.

Also, having common code for the "format", and then relying on backends for a small piece of the contents simplifies later patches like the multi-backend tracing.

The thing here is that maybe we should change format to "file", since it actually refers to a specific output file.

Thanks,
Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
#8 lore raw
Quoted 45 lines · Lluís Vilanova
Quoted 32 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Minimizes the amount of backend code, making it simpler to add new/different backends.

Also performs other cleanups all around.

Quoted 8 lines

---

Lluís Vilanova (4):
trace: [tracetool] Add method 'Event.api' to build event names
trace: [tracetool,trivial] Style changes
trace: [tracetool] Identify formats directly used by QEMU
trace: [tracetool] Minimize the amount of per-backend code

I think we stretched the concepts of backends and formats too far. There are formats that only work with one backend (like 'stap'). And there are backends that behave differently from all other backends.

As a result we're trying to abstract and make common a bunch of stuff that isn't really common. This problem existed before this patch series, but I feel we're going further down a direction that increasingly seems to be wrong.

It's simpler if we turn the design inside-out. Instead of making backends export all sorts of interfaces and flags, tracetool should just parse trace-events and hand the list over to the backend.

Let the backend do whatever it wants. The format option simply becomes an option telling the backend which type of output to generate (events.h, events.c, .stp, etc).

Quoted 6 lines

Common behavior should live in plain old Python modules/functions.

TL;DR moving to a library design would simplify and clean up more than trying to improve the current framework design

What do you think?

This series moves into that direction; some of the formats are simply not handled by backends. For example, the "stap", "events_c" and "events_h" formats have no backend-specific contents.

Quoted 6 lines

Also, having common code for the "format", and then relying on backends for a small piece of the contents simplifies later patches like the multi-backend tracing.

The thing here is that maybe we should change format to "file", since it actually refers to a specific output file.

You have a point. tracetool needs to output a particular file (e.g. generated-events.c, generated-tracers.h), which is kind of what a "format" has become.

So the "format" is the primary piece of code that emits output. But if it needs to do something backend-specific (like generated-tracers.h) then it should call into backend modules.

I am still concerned about the weird and wonderful interfaces that we're creating (like the "API" field in this patch series). They make it harder to understand the code and add new backends. Will think about this more when reviewing the next revision of this series.

Stefan

#9 lore raw
Quoted 53 lines · Stefan Hajnoczi
Quoted 45 lines · Lluís Vilanova
Quoted 32 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Minimizes the amount of backend code, making it simpler to add new/different backends.

Also performs other cleanups all around.

Quoted 8 lines

---

Lluís Vilanova (4):
trace: [tracetool] Add method 'Event.api' to build event names
trace: [tracetool,trivial] Style changes
trace: [tracetool] Identify formats directly used by QEMU
trace: [tracetool] Minimize the amount of per-backend code

I think we stretched the concepts of backends and formats too far. There are formats that only work with one backend (like 'stap'). And there are backends that behave differently from all other backends.

As a result we're trying to abstract and make common a bunch of stuff that isn't really common. This problem existed before this patch series, but I feel we're going further down a direction that increasingly seems to be wrong.

It's simpler if we turn the design inside-out. Instead of making backends export all sorts of interfaces and flags, tracetool should just parse trace-events and hand the list over to the backend.

Let the backend do whatever it wants. The format option simply becomes an option telling the backend which type of output to generate (events.h, events.c, .stp, etc).

Quoted 6 lines

Common behavior should live in plain old Python modules/functions.

TL;DR moving to a library design would simplify and clean up more than trying to improve the current framework design

What do you think?

This series moves into that direction; some of the formats are simply not handled by backends. For example, the "stap", "events_c" and "events_h" formats have no backend-specific contents.

Quoted 6 lines

Also, having common code for the "format", and then relying on backends for a small piece of the contents simplifies later patches like the multi-backend tracing.

The thing here is that maybe we should change format to "file", since it actually refers to a specific output file.

Quoted 7 lines

You have a point. tracetool needs to output a particular file (e.g. generated-events.c, generated-tracers.h), which is kind of what a "format" has become.

So the "format" is the primary piece of code that emits output. But if it needs to do something backend-specific (like generated-tracers.h) then it should call into backend modules.

Exactly.

Quoted 4 lines

I am still concerned about the weird and wonderful interfaces that we're creating (like the "API" field in this patch series). They make it harder to understand the code and add new backends. Will think about this more when reviewing the next revision of this series.

Right. To be honest, the "API" sounded like a nice idea when I wrote it, but the only thing it buys you is that non-API formats will only receive non-disabled events. A really small benefit for adding more inter-module semantics. I will probably remove it after rebasing the series.

Thanks,
Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth