Skip to contentSkip to conversation

trace qmp commands

PATCH Applied Vladimir Sementsov-Ogievskiy 3 participants 20 messages Jan 26, 2022 – Jan 27, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v6 cover trace qmp commands

This series aims to add trace points for each qmp command with help of qapi code generator.

v6:
01-04,07: add Stefan's r-b
01: - subject changed

    - rename:
       gen_trace_events --> gen_tracing
       _gen_trace_events --> _gen_tracing
       _gent() --> _gen_trace_events()
    - a bit more compact code for gent initializing
03: - rename:
       gen_trace_events --> gen_tracing
       _gen_trace_events --> _gen_tracing

04: fix --add-trace-events -> --gen-trace in commit message 05: - drop extra two sentences
- reword
- add example .trace-events file
06: reword
07: rename option to --suppress-tracing

Vladimir Sementsov-Ogievskiy (7):
qapi/gen: Add FOO.trace-events output module qapi/commands: refactor error handling code qapi/commands: Optionally generate trace for QMP commands
meson: generate trace events for qmp commands docs/qapi-code-gen: update to cover trace events code generation
meson: document, why we don't generate trace events for tests/ and
qga/
qapi: generate trace events by default

 docs/devel/qapi-code-gen.rst |  25 ++++++++-
 meson.build                  |   3 ++
 qapi/meson.build             |   7 +++
 qga/meson.build              |  10 +++-
 scripts/qapi/commands.py     | 101 ++++++++++++++++++++++++++++++-----
 scripts/qapi/gen.py          |  31 +++++++++--
 scripts/qapi/main.py         |  14 +++--
 tests/meson.build            |  10 +++-
 trace/meson.build            |  11 ++--
 9 files changed, 185 insertions(+), 27 deletions(-)
Signature · 1 line
2.31.1
#2 lore raw

PATCH v6 2/7 qapi/commands: refactor error handling code

Move error_propagate() to if (err) and make "if (err)" block mandatory. This is to simplify further commit, which will bring trace events generation for QMP commands.

---
 docs/devel/qapi-code-gen.rst |  2 +-
 scripts/qapi/commands.py     | 10 +++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
docs/devel/qapi-code-gen.rst +1 -1
index a3b5473089..feafed79b5 100644
@@ -1690,8 +1690,8 @@ Example::
         }
 
         retval = qmp_my_command(arg.arg1, &err);
-        error_propagate(errp, err);
         if (err) {
+            error_propagate(errp, err);
             goto out;
         }
 
scripts/qapi/commands.py +7 -3
index 21001bbd6b..17e5ed2414 100644
@@ -74,14 +74,18 @@ def gen_call(name: str,
     ret = mcgen('''
 
     %(lhs)sqmp_%(c_name)s(%(args)s&err);
-    error_propagate(errp, err);
 ''',
                 c_name=c_name(name), args=argstr, lhs=lhs)
-    if ret_type:
-        ret += mcgen('''
+
+    ret += mcgen('''
     if (err) {
+        error_propagate(errp, err);
         goto out;
     }
+''')
+
+    if ret_type:
+        ret += mcgen('''
 
     qmp_marshal_output_%(c_name)s(retval, ret, errp);
 ''',
Signature · 1 line
2.31.1
#14 lore raw

PATCH v6 7/7 qapi: generate trace events by default

In reply to Vladimir Sementsov-Ogievskiy

We don't generate trace events for tests/ and qga/ because that it is not simple and not necessary. We have corresponding comments in both tests/meson.build and qga/meson.build.

Still to not miss possible future qapi code generation call, and not to forget to enable trace events generation, let's enable it by default. So, turn option --gen-trace into opposite --no-trace-events and use new option only in tests/ and qga/ where we already have good comments why we don't generate trace events code.

Note that this commit enables trace-events generation for qapi-gen.py call from tests/qapi-schema/meson.build and storage-daemon/meson.build. Still, both are kind of noop: tests/qapi-schema/ doesn't seem to generate any QMP command code and no .trace-events files anyway, storage-daemon/ uses common QMP command implementations and just generate empty .trace-events

---
 qapi/meson.build     | 2 +-
 qga/meson.build      | 3 ++-
 scripts/qapi/main.py | 8 ++++----
 tests/meson.build    | 3 ++-
 4 files changed, 9 insertions(+), 7 deletions(-)
qapi/meson.build +1 -1
index b22558ca73..656ef0e039 100644
@@ -127,7 +127,7 @@ endforeach
 qapi_files = custom_target('shared QAPI source files',
   output: qapi_util_outputs + qapi_specific_outputs + qapi_nonmodule_outputs,
   input: [ files('qapi-schema.json') ],
-  command: [ qapi_gen, '-o', 'qapi', '-b', '@INPUT0@', '--gen-trace' ],
+  command: [ qapi_gen, '-o', 'qapi', '-b', '@INPUT0@' ],
   depend_files: [ qapi_inputs, qapi_gen_depends ])
 
 # Now go through all the outputs and add them to the right sourceset.
qga/meson.build +2 -1
index 724d5a667b..f06b726ad3 100644
@@ -25,7 +25,8 @@ qga_qapi_outputs = [
 qga_qapi_files = custom_target('QGA QAPI files',
                                output: qga_qapi_outputs,
                                input: 'qapi-schema.json',
-                               command: [ qapi_gen, '-o', 'qga', '-p', 'qga-', '@INPUT0@' ],
+                               command: [ qapi_gen, '-o', 'qga', '-p', 'qga-', '@INPUT0@',
+                                          '--suppress-tracing' ],
                                depend_files: qapi_gen_depends)
 
 qga_ss = ss.source_set()
scripts/qapi/main.py +4 -4
index 687d408aba..fc216a53d3 100644
@@ -76,10 +76,10 @@ def main() -> int:
                         dest='unmask',
                         help="expose non-ABI names in introspection")
 
-    # Option --gen-trace exists so we can avoid solving build system
+    # Option --suppress-tracing exists so we can avoid solving build system
     # problems.  TODO Drop it when we no longer need it.
-    parser.add_argument('--gen-trace', action='store_true',
-                        help="add trace events to qmp marshals")
+    parser.add_argument('--suppress-tracing', action='store_true',
+                        help="suppress adding trace events to qmp marshals")
 
     parser.add_argument('schema', action='store')
     args = parser.parse_args()
@@ -96,7 +96,7 @@ def main() -> int:
                  prefix=args.prefix,
                  unmask=args.unmask,
                  builtins=args.builtins,
-                 gen_tracing=args.gen_trace)
+                 gen_tracing=not args.suppress_tracing)
     except QAPIError as err:
         print(f"{sys.argv[0]}: {str(err)}", file=sys.stderr)
         return 1
tests/meson.build +2 -1
index af8f5b1746..6d11dd436e 100644
@@ -44,7 +44,8 @@ test_qapi_files = custom_target('Test QAPI files',
                                              'qapi-schema/include/sub-module.json',
                                              'qapi-schema/sub-sub-module.json'),
                                 command: [ qapi_gen, '-o', meson.current_build_dir(),
-                                           '-b', '-p', 'test-', '@INPUT0@' ],
+                                           '-b', '-p', 'test-', '@INPUT0@',
+                                           '--suppress-tracing' ],
                                 depend_files: qapi_gen_depends)
 
 # meson doesn't like generated output in other directories
Signature · 1 line
2.31.1
#9 lore raw

PATCH v6 6/7 meson: document, why we don't generate trace events for tests/ and qga/

In reply to Vladimir Sementsov-Ogievskiy

Making trace generation work for tests/ and qga/ would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there anyway, we bypass that problem.

Let's add corresponding comments.

---
 qga/meson.build   | 7 +++++++
 tests/meson.build | 7 +++++++
 2 files changed, 14 insertions(+)
qga/meson.build +7 -0
index cfb1fbc085..724d5a667b 100644
@@ -15,6 +15,13 @@ qga_qapi_outputs = [
   'qga-qapi-visit.h',
 ]
 
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
 qga_qapi_files = custom_target('QGA QAPI files',
                                output: qga_qapi_outputs,
                                input: 'qapi-schema.json',
tests/meson.build +7 -0
index 3f3882748a..af8f5b1746 100644
@@ -31,6 +31,13 @@ test_qapi_outputs = [
   'test-qapi-visit.h',
 ]
 
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
 test_qapi_files = custom_target('Test QAPI files',
                                 output: test_qapi_outputs,
                                 input: files('qapi-schema/qapi-schema-test.json',
Signature · 1 line
2.31.1
#7 lore raw

PATCH v6 5/7 docs/qapi-code-gen: update to cover trace events code generation

In reply to Vladimir Sementsov-Ogievskiy

Previous commits enabled trace events generation for most of QAPI generated code (except for tests/ and qga/). Let's update documentation to illustrate it.

---
 docs/devel/qapi-code-gen.rst | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
docs/devel/qapi-code-gen.rst +23 -0
index feafed79b5..246709ede8 100644
@@ -1630,6 +1630,9 @@ The following files are generated:
  ``$(prefix)qapi-commands.h``
      Function prototypes for the QMP commands specified in the schema
 
+ ``$(prefix)qapi-commands.trace-events``
+     Trace event declarations, see :ref:`tracing`.
+
  ``$(prefix)qapi-init-commands.h``
      Command initialization prototype
 
@@ -1650,6 +1653,13 @@ Example::
     void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp);
 
     #endif /* EXAMPLE_QAPI_COMMANDS_H */
+
+    $ cat qapi-generated/example-qapi-commands.trace-events
+    # AUTOMATICALLY GENERATED, DO NOT MODIFY
+
+    qmp_enter_my_command(const char *json) "%s"
+    qmp_exit_my_command(const char *result, bool succeeded) "%s %d"
+
     $ cat qapi-generated/example-qapi-commands.c
     [Uninteresting stuff omitted...]
 
@@ -1689,14 +1699,27 @@ Example::
             goto out;
         }
 
+        if (trace_event_get_state_backends(TRACE_QMP_ENTER_MY_COMMAND)) {
+            g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
+
+            trace_qmp_enter_my_command(req_json->str);
+        }
+
         retval = qmp_my_command(arg.arg1, &err);
         if (err) {
+            trace_qmp_exit_my_command(error_get_pretty(err), false);
             error_propagate(errp, err);
             goto out;
         }
 
         qmp_marshal_output_UserDefOne(retval, ret, errp);
 
+        if (trace_event_get_state_backends(TRACE_QMP_EXIT_MY_COMMAND)) {
+            g_autoptr(GString) ret_json = qobject_to_json(*ret);
+
+            trace_qmp_exit_my_command(ret_json->str, true);
+        }
+
     out:
         visit_free(v);
         v = qapi_dealloc_visitor_new();
Signature · 1 line
2.31.1
#6 lore raw

PATCH v6 4/7 meson: generate trace events for qmp commands

In reply to Vladimir Sementsov-Ogievskiy

1. Use --gen-trace when generate qmp commands
2. Add corresponding .trace-events files as outputs in qapi_files custom target
3. Define global qapi_trace_events list of .trace-events file targets, to fill in trace/qapi.build and to use in trace/meson.build
4. In trace/meson.build use the new array as an additional source of .trace_events files to be processed

---
 meson.build       |  3 +++
 qapi/meson.build  |  9 ++++++++-
 trace/meson.build | 11 ++++++++---
 3 files changed, 19 insertions(+), 4 deletions(-)
meson.build +3 -0
index 833fd6bc4c..e0cfafe8d9 100644
@@ -41,6 +41,7 @@ qemu_icondir = get_option('datadir') / 'icons'
 
 config_host_data = configuration_data()
 genh = []
+qapi_trace_events = []
 
 target_dirs = config_host['TARGET_DIRS'].split()
 have_linux_user = false
@@ -2557,6 +2558,8 @@ if 'CONFIG_VHOST_USER' in config_host
   vhost_user = libvhost_user.get_variable('vhost_user_dep')
 endif
 
+# NOTE: the trace/ subdirectory needs the qapi_trace_events variable
+# that is filled in by qapi/.
 subdir('qapi')
 subdir('qobject')
 subdir('stubs')
qapi/meson.build +8 -1
index c0c49c15e4..b22558ca73 100644
@@ -114,6 +114,7 @@ foreach module : qapi_all_modules
       'qapi-events-@[email protected]'.format(module),
       'qapi-commands-@[email protected]'.format(module),
       'qapi-commands-@[email protected]'.format(module),
+      'qapi-commands-@[email protected]'.format(module),
     ]
   endif
   if module.endswith('-target')
@@ -126,7 +127,7 @@ endforeach
 qapi_files = custom_target('shared QAPI source files',
   output: qapi_util_outputs + qapi_specific_outputs + qapi_nonmodule_outputs,
   input: [ files('qapi-schema.json') ],
-  command: [ qapi_gen, '-o', 'qapi', '-b', '@INPUT0@' ],
+  command: [ qapi_gen, '-o', 'qapi', '-b', '@INPUT0@', '--gen-trace' ],
   depend_files: [ qapi_inputs, qapi_gen_depends ])
 
 # Now go through all the outputs and add them to the right sourceset.
@@ -137,6 +138,9 @@ foreach output : qapi_util_outputs
   if output.endswith('.h')
     genh += qapi_files[i]
   endif
+  if output.endswith('.trace-events')
+    qapi_trace_events += qapi_files[i]
+  endif
   util_ss.add(qapi_files[i])
   i = i + 1
 endforeach
@@ -145,6 +149,9 @@ foreach output : qapi_specific_outputs + qapi_nonmodule_outputs
   if output.endswith('.h')
     genh += qapi_files[i]
   endif
+  if output.endswith('.trace-events')
+    qapi_trace_events += qapi_files[i]
+  endif
   specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: qapi_files[i])
   i = i + 1
 endforeach
trace/meson.build +8 -3
index 573dd699c6..c4794a1f2a 100644
@@ -2,10 +2,15 @@
 specific_ss.add(files('control-target.c'))
 
 trace_events_files = []
-foreach dir : [ '.' ] + trace_events_subdirs
-  trace_events_file = meson.project_source_root() / dir / 'trace-events'
+foreach item : [ '.' ] + trace_events_subdirs + qapi_trace_events
+  if item in qapi_trace_events
+    trace_events_file = item
+    group_name = item.full_path().split('/')[-1].underscorify()
+  else
+    trace_events_file = meson.project_source_root() / item / 'trace-events'
+    group_name = item == '.' ? 'root' : item.underscorify()
+  endif
   trace_events_files += [ trace_events_file ]
-  group_name = dir == '.' ? 'root' : dir.underscorify()
   group = '--group=' + group_name
   fmt = '@0@-' + group_name + '.@1@'
 
Signature · 1 line
2.31.1
#1 lore raw

PATCH v6 1/7 qapi/gen: Add FOO.trace-events output module

In reply to Vladimir Sementsov-Ogievskiy

We are going to generate trace events for QMP commands. We should generate both trace_*() function calls and trace-events files listing events for trace generator.

So, add an output module FOO.trace-events for each FOO schema module.

Since we're going to add trace events only to command marshallers, make the trace-events output optional, so we don't generate so many useless empty files.

Currently nobody set add_trace_events to True, so new functionality is disabled. It will be enabled for QAPISchemaGenCommandVisitor in a further commit.

---
 scripts/qapi/gen.py | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
scripts/qapi/gen.py +27 -4
index 995a97d2b8..113b49134d 100644
@@ -192,6 +192,11 @@ def _bottom(self) -> str:
         return guardend(self.fname)
 
 
+class QAPIGenTrace(QAPIGen):
+    def _top(self) -> str:
+        return super()._top() + '# AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n'
+
+
 @contextmanager
 def ifcontext(ifcond: QAPISchemaIfCond, *args: QAPIGenCCode) -> Iterator[None]:
     """
@@ -244,15 +249,18 @@ def __init__(self,
                  what: str,
                  user_blurb: str,
                  builtin_blurb: Optional[str],
-                 pydoc: str):
+                 pydoc: str,
+                 gen_tracing: bool = False):
         self._prefix = prefix
         self._what = what
         self._user_blurb = user_blurb
         self._builtin_blurb = builtin_blurb
         self._pydoc = pydoc
         self._current_module: Optional[str] = None
-        self._module: Dict[str, Tuple[QAPIGenC, QAPIGenH]] = {}
+        self._module: Dict[str, Tuple[QAPIGenC, QAPIGenH,
+                                      Optional[QAPIGenTrace]]] = {}
         self._main_module: Optional[str] = None
+        self._gen_tracing = gen_tracing
 
     @property
     def _genc(self) -> QAPIGenC:
@@ -264,6 +272,14 @@ def _genh(self) -> QAPIGenH:
         assert self._current_module is not None
         return self._module[self._current_module][1]
 
+    @property
+    def _gen_trace_events(self) -> QAPIGenTrace:
+        assert self._gen_tracing
+        assert self._current_module is not None
+        gent = self._module[self._current_module][2]
+        assert gent is not None
+        return gent
+
     @staticmethod
     def _module_dirname(name: str) -> str:
         if QAPISchemaModule.is_user_module(name):
@@ -293,7 +309,12 @@ def _add_module(self, name: str, blurb: str) -> None:
         basename = self._module_filename(self._what, name)
         genc = QAPIGenC(basename + '.c', blurb, self._pydoc)
         genh = QAPIGenH(basename + '.h', blurb, self._pydoc)
-        self._module[name] = (genc, genh)
+
+        gent: Optional[QAPIGenTrace] = None
+        if self._gen_tracing:
+            gent = QAPIGenTrace(basename + '.trace-events')
+
+        self._module[name] = (genc, genh, gent)
         self._current_module = name
 
     @contextmanager
@@ -304,11 +325,13 @@ def _temp_module(self, name: str) -> Iterator[None]:
         self._current_module = old_module
 
     def write(self, output_dir: str, opt_builtins: bool = False) -> None:
-        for name, (genc, genh) in self._module.items():
+        for name, (genc, genh, gent) in self._module.items():
             if QAPISchemaModule.is_builtin_module(name) and not opt_builtins:
                 continue
             genc.write(output_dir)
             genh.write(output_dir)
+            if gent is not None:
+                gent.write(output_dir)
 
     def _begin_builtin_module(self) -> None:
         pass
Signature · 1 line
2.31.1
#3 lore raw

PATCH v6 3/7 qapi/commands: Optionally generate trace for QMP commands

In reply to Vladimir Sementsov-Ogievskiy

Add trace generation disabled by default and new option --gen-trace to enable it. The next commit will enable it for qapi/, but not for qga/ and tests/. Making it work for the latter two would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there, we'll bypass that problem.

---
 scripts/qapi/commands.py | 91 +++++++++++++++++++++++++++++++++++-----
 scripts/qapi/main.py     | 14 +++++--
 2 files changed, 91 insertions(+), 14 deletions(-)
scripts/qapi/commands.py +80 -11
index 17e5ed2414..fa90b6246b 100644
@@ -53,7 +53,8 @@ def gen_command_decl(name: str,
 def gen_call(name: str,
              arg_type: Optional[QAPISchemaObjectType],
              boxed: bool,
-             ret_type: Optional[QAPISchemaType]) -> str:
+             ret_type: Optional[QAPISchemaType],
+             gen_tracing: bool) -> str:
     ret = ''
 
     argstr = ''
@@ -71,14 +72,37 @@ def gen_call(name: str,
     if ret_type:
         lhs = 'retval = '
 
-    ret = mcgen('''
+    name = c_name(name)
+    upper = name.upper()
 
-    %(lhs)sqmp_%(c_name)s(%(args)s&err);
+    if gen_tracing:
+        ret += mcgen('''
+
+    if (trace_event_get_state_backends(TRACE_QMP_ENTER_%(upper)s)) {
+        g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
+
+        trace_qmp_enter_%(name)s(req_json->str);
+    }
+    ''',
+                     upper=upper, name=name)
+
+    ret += mcgen('''
+
+    %(lhs)sqmp_%(name)s(%(args)s&err);
 ''',
-                c_name=c_name(name), args=argstr, lhs=lhs)
+                 name=name, args=argstr, lhs=lhs)
 
     ret += mcgen('''
     if (err) {
+''')
+
+    if gen_tracing:
+        ret += mcgen('''
+        trace_qmp_exit_%(name)s(error_get_pretty(err), false);
+''',
+                     name=name)
+
+    ret += mcgen('''
         error_propagate(errp, err);
         goto out;
     }
@@ -90,6 +114,25 @@ def gen_call(name: str,
     qmp_marshal_output_%(c_name)s(retval, ret, errp);
 ''',
                      c_name=ret_type.c_name())
+
+    if gen_tracing:
+        if ret_type:
+            ret += mcgen('''
+
+    if (trace_event_get_state_backends(TRACE_QMP_EXIT_%(upper)s)) {
+        g_autoptr(GString) ret_json = qobject_to_json(*ret);
+
+        trace_qmp_exit_%(name)s(ret_json->str, true);
+    }
+    ''',
+                         upper=upper, name=name)
+        else:
+            ret += mcgen('''
+
+    trace_qmp_exit_%(name)s("{}", true);
+    ''',
+                         name=name)
+
     return ret
 
 
@@ -126,10 +169,19 @@ def gen_marshal_decl(name: str) -> str:
                  proto=build_marshal_proto(name))
 
 
+def gen_trace(name: str) -> str:
+    return mcgen('''
+qmp_enter_%(name)s(const char *json) "%%s"
+qmp_exit_%(name)s(const char *result, bool succeeded) "%%s %%d"
+''',
+                 name=c_name(name))
+
+
 def gen_marshal(name: str,
                 arg_type: Optional[QAPISchemaObjectType],
                 boxed: bool,
-                ret_type: Optional[QAPISchemaType]) -> str:
+                ret_type: Optional[QAPISchemaType],
+                gen_tracing: bool) -> str:
     have_args = boxed or (arg_type and not arg_type.is_empty())
     if have_args:
         assert arg_type is not None
@@ -184,7 +236,7 @@ def gen_marshal(name: str,
     }
 ''')
 
-    ret += gen_call(name, arg_type, boxed, ret_type)
+    ret += gen_call(name, arg_type, boxed, ret_type, gen_tracing)
 
     ret += mcgen('''
 
@@ -242,11 +294,13 @@ def gen_register_command(name: str,
 
 
 class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
-    def __init__(self, prefix: str):
+    def __init__(self, prefix: str, gen_tracing: bool):
         super().__init__(
             prefix, 'qapi-commands',
-            ' * Schema-defined QAPI/QMP commands', None, __doc__)
+            ' * Schema-defined QAPI/QMP commands', None, __doc__,
+            gen_tracing=gen_tracing)
         self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
+        self._gen_tracing = gen_tracing
 
     def _begin_user_module(self, name: str) -> None:
         self._visited_ret_types[self._genc] = set()
@@ -265,6 +319,17 @@ def _begin_user_module(self, name: str) -> None:
 
 ''',
                              commands=commands, visit=visit))
+
+        if self._gen_tracing and commands != 'qapi-commands':
+            self._genc.add(mcgen('''
+#include "trace/trace-qapi.h"
+#include "qapi/qmp/qjson.h"
+#include "trace/trace-%(nm)s_trace_events.h"
+''',
+                                 nm=c_name(commands, protect=False)))
+            # We use c_name(commands, protect=False) to turn '-' into '_', to
+            # match .underscorify() in trace/meson.build
+
         self._genh.add(mcgen('''
 #include "%(types)s.h"
 
@@ -326,7 +391,10 @@ def visit_command(self,
         with ifcontext(ifcond, self._genh, self._genc):
             self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
             self._genh.add(gen_marshal_decl(name))
-            self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
+            self._genc.add(gen_marshal(name, arg_type, boxed, ret_type,
+                                       self._gen_tracing))
+            if self._gen_tracing:
+                self._gen_trace_events.add(gen_trace(name))
         with self._temp_module('./init'):
             with ifcontext(ifcond, self._genh, self._genc):
                 self._genc.add(gen_register_command(
@@ -336,7 +404,8 @@ def visit_command(self,
 
 def gen_commands(schema: QAPISchema,
                  output_dir: str,
-                 prefix: str) -> None:
-    vis = QAPISchemaGenCommandVisitor(prefix)
+                 prefix: str,
+                 gen_tracing: bool) -> None:
+    vis = QAPISchemaGenCommandVisitor(prefix, gen_tracing)
     schema.visit(vis)
     vis.write(output_dir)
scripts/qapi/main.py +11 -3
index f2ea6e0ce4..687d408aba 100644
@@ -32,7 +32,8 @@ def generate(schema_file: str,
              output_dir: str,
              prefix: str,
              unmask: bool = False,
-             builtins: bool = False) -> None:
+             builtins: bool = False,
+             gen_tracing: bool = False) -> None:
     """
     Generate C code for the given schema into the target directory.
 
@@ -49,7 +50,7 @@ def generate(schema_file: str,
     schema = QAPISchema(schema_file)
     gen_types(schema, output_dir, prefix, builtins)
     gen_visit(schema, output_dir, prefix, builtins)
-    gen_commands(schema, output_dir, prefix)
+    gen_commands(schema, output_dir, prefix, gen_tracing)
     gen_events(schema, output_dir, prefix)
     gen_introspect(schema, output_dir, prefix, unmask)
 
@@ -74,6 +75,12 @@ def main() -> int:
     parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
                         dest='unmask',
                         help="expose non-ABI names in introspection")
+
+    # Option --gen-trace exists so we can avoid solving build system
+    # problems.  TODO Drop it when we no longer need it.
+    parser.add_argument('--gen-trace', action='store_true',
+                        help="add trace events to qmp marshals")
+
     parser.add_argument('schema', action='store')
     args = parser.parse_args()
 
@@ -88,7 +95,8 @@ def main() -> int:
                  output_dir=args.output_dir,
                  prefix=args.prefix,
                  unmask=args.unmask,
-                 builtins=args.builtins)
+                 builtins=args.builtins,
+                 gen_tracing=args.gen_trace)
     except QAPIError as err:
         print(f"{sys.argv[0]}: {str(err)}", file=sys.stderr)
         return 1
Signature · 1 line
2.31.1
Reviewed-by on the v6 series
#15 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 2 lines · Vladimir Sementsov-Ogievskiy

This series aims to add trace points for each qmp command with help of qapi code generator.

Logging QMP traffic has worked well enough for me, but I can understand why you'd like to use tracing. Other uses of tracing might come up in the future.

I found a few last nits to pick. Happy to address them in my tree.

Except for PATCH 4

#4 lore raw

Re: [PATCH v6 3/7] qapi/commands: Optionally generate trace for QMP commands

In reply to Vladimir Sementsov-Ogievskiy
Quoted 18 lines · Vladimir Sementsov-Ogievskiy

Add trace generation disabled by default and new option --gen-trace to enable it. The next commit will enable it for qapi/, but not for qga/ and tests/. Making it work for the latter two would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there, we'll bypass that problem.

---
 scripts/qapi/commands.py | 91 +++++++++++++++++++++++++++++++++++-----
 scripts/qapi/main.py     | 14 +++++--
 2 files changed, 91 insertions(+), 14 deletions(-)
scripts/qapi/commands.py +0 -0
index 17e5ed2414..fa90b6246b 100644

[...]

scripts/qapi/commands.py def _begin_user_module(self, name: str) -> None: · line 325

Quoted 8 lines
scripts/qapi/commands.py +4 -0
@@ -265,6 +319,17 @@ def _begin_user_module(self, name: str) -> None:
 
 ''',
                              commands=commands, visit=visit))
+
+        if self._gen_tracing and commands != 'qapi-commands':
+            self._genc.add(mcgen('''
+#include "trace/trace-qapi.h"

I believe this include is superfluous.

Quoted 10 lines
scripts/qapi/commands.py +7 -0
+#include "qapi/qmp/qjson.h"
+#include "trace/trace-%(nm)s_trace_events.h"
+''',
+                                 nm=c_name(commands, protect=False)))
+            # We use c_name(commands, protect=False) to turn '-' into '_', to
+            # match .underscorify() in trace/meson.build
+
         self._genh.add(mcgen('''
 #include "%(types)s.h"
 

[...]

#11 lore raw

Re: [PATCH v6 6/7] meson: document, why we don't generate trace events for tests/ and qga/

In reply to Vladimir Sementsov-Ogievskiy
Quoted 36 lines · Vladimir Sementsov-Ogievskiy

Making trace generation work for tests/ and qga/ would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there anyway, we bypass that problem.

Let's add corresponding comments.

---
 qga/meson.build   | 7 +++++++
 tests/meson.build | 7 +++++++
 2 files changed, 14 insertions(+)
qga/meson.build +7 -0
index cfb1fbc085..724d5a667b 100644
@@ -15,6 +15,13 @@ qga_qapi_outputs = [
   'qga-qapi-visit.h',
 ]
 
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
 qga_qapi_files = custom_target('QGA QAPI files',
                                output: qga_qapi_outputs,
                                input: 'qapi-schema.json',
tests/meson.build +0 -0
index 3f3882748a..af8f5b1746 100644

tests/meson.build test_qapi_outputs = [ · line 37

Quoted 7 lines
tests/meson.build +4 -0
@@ -31,6 +31,13 @@ test_qapi_outputs = [
   'test-qapi-visit.h',
 ]
 
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.

Shouldn't this be subdir('tests')?

Quoted 6 lines
tests/meson.build +3 -0
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
 test_qapi_files = custom_target('Test QAPI files',
                                 output: test_qapi_outputs,
                                 input: files('qapi-schema/qapi-schema-test.json',
Reviewed-by on v6 6/7
#13 lore raw

Re: [PATCH v6 6/7] meson: document, why we don't generate trace events for tests/ and qga/

In reply to Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Making trace generation work for tests/ and qga/ would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there anyway, we bypass that problem.

Quoted 7 lines

Let's add corresponding comments.

---
 qga/meson.build   | 7 +++++++
 tests/meson.build | 7 +++++++
 2 files changed, 14 insertions(+)
Reviewed-by on v6 5/7
#8 lore raw

Re: [PATCH v6 5/7] docs/qapi-code-gen: update to cover trace events code generation

In reply to Vladimir Sementsov-Ogievskiy
Quoted 8 lines · Vladimir Sementsov-Ogievskiy

Previous commits enabled trace events generation for most of QAPI generated code (except for tests/ and qga/). Let's update documentation to illustrate it.

---
 docs/devel/qapi-code-gen.rst | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Reviewed-by on the v6 series
#18 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Vladimir Sementsov-Ogievskiy

This series aims to add trace points for each qmp command with help of qapi code generator.

v6:
01-04,07: add Stefan's r-b
01: - subject changed

    - rename:
       gen_trace_events --> gen_tracing
       _gen_trace_events --> _gen_tracing
       _gent() --> _gen_trace_events()
    - a bit more compact code for gent initializing
03: - rename:
       gen_trace_events --> gen_tracing
       _gen_trace_events --> _gen_tracing

04: fix --add-trace-events -> --gen-trace in commit message 05: - drop extra two sentences
- reword
- add example .trace-events file
06: reword
07: rename option to --suppress-tracing

Vladimir Sementsov-Ogievskiy (7):
qapi/gen: Add FOO.trace-events output module qapi/commands: refactor error handling code qapi/commands: Optionally generate trace for QMP commands
meson: generate trace events for qmp commands docs/qapi-code-gen: update to cover trace events code generation
meson: document, why we don't generate trace events for tests/ and
qga/
qapi: generate trace events by default

Quoted 14 lines
 docs/devel/qapi-code-gen.rst |  25 ++++++++-
 meson.build                  |   3 ++
 qapi/meson.build             |   7 +++
 qga/meson.build              |  10 +++-
 scripts/qapi/commands.py     | 101 ++++++++++++++++++++++++++++++-----
 scripts/qapi/gen.py          |  31 +++++++++--
 scripts/qapi/main.py         |  14 +++--
 tests/meson.build            |  10 +++-
 trace/meson.build            |  11 ++--
 9 files changed, 185 insertions(+), 27 deletions(-)
Signature · 1 line
2.31.1
#16 lore raw
In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster
Quoted 2 lines · Vladimir Sementsov-Ogievskiy

This series aims to add trace points for each qmp command with help of qapi code generator.

Logging QMP traffic has worked well enough for me, but I can understand why you'd like to use tracing. Other uses of tracing might come up in the future.

I found a few last nits to pick. Happy to address them in my tree.

That would be great, thanks a lot!

Quoted 2 lines

Except for PATCH 4

It has "Acked-by: Paolo", so seems we are done.

Quoted 2 lines

Thanks!

Signature · 2 lines
Best regards,
Vladimir
#19 lore raw
In reply to Stefan Hajnoczi
Quoted 43 lines · Stefan Hajnoczi
Quoted 42 lines · Vladimir Sementsov-Ogievskiy

This series aims to add trace points for each qmp command with help of qapi code generator.

v6:
01-04,07: add Stefan's r-b
01: - subject changed

     - rename:
        gen_trace_events --> gen_tracing
        _gen_trace_events --> _gen_tracing
        _gent() --> _gen_trace_events()
     - a bit more compact code for gent initializing
03: - rename:
        gen_trace_events --> gen_tracing
        _gen_trace_events --> _gen_tracing

04: fix --add-trace-events -> --gen-trace in commit message 05: - drop extra two sentences
- reword
- add example .trace-events file
06: reword
07: rename option to --suppress-tracing

Vladimir Sementsov-Ogievskiy (7):
qapi/gen: Add FOO.trace-events output module qapi/commands: refactor error handling code qapi/commands: Optionally generate trace for QMP commands
meson: generate trace events for qmp commands docs/qapi-code-gen: update to cover trace events code generation
meson: document, why we don't generate trace events for tests/ and
qga/
qapi: generate trace events by default

  docs/devel/qapi-code-gen.rst |  25 ++++++++-
  meson.build                  |   3 ++
  qapi/meson.build             |   7 +++
  qga/meson.build              |  10 +++-
  scripts/qapi/commands.py     | 101 ++++++++++++++++++++++++++++++-----
  scripts/qapi/gen.py          |  31 +++++++++--
  scripts/qapi/main.py         |  14 +++--
  tests/meson.build            |  10 +++-
  trace/meson.build            |  11 ++--
  9 files changed, 185 insertions(+), 27 deletions(-)
Quoted 6 lines
Quoted 3 lines
Signature · 1 line
2.31.1

Thanks a lot for reviewing!

Signature · 2 lines
Best regards,
Vladimir
#5 lore raw

Re: [PATCH v6 3/7] qapi/commands: Optionally generate trace for QMP commands

In reply to Markus Armbruster
Quoted 25 lines · Markus Armbruster
Quoted 18 lines · Vladimir Sementsov-Ogievskiy

Add trace generation disabled by default and new option --gen-trace to enable it. The next commit will enable it for qapi/, but not for qga/ and tests/. Making it work for the latter two would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there, we'll bypass that problem.

---
  scripts/qapi/commands.py | 91 +++++++++++++++++++++++++++++++++++-----
  scripts/qapi/main.py     | 14 +++++--
  2 files changed, 91 insertions(+), 14 deletions(-)
scripts/qapi/commands.py +0 -0
index 17e5ed2414..fa90b6246b 100644

[...]

Quoted 2 lines
scripts/qapi/commands.py +0 -0
@@ -265,6 +319,17 @@ def _begin_user_module(self, name: str) -> None:
 
Quoted 8 lines

scripts/qapi/commands.py def _begin_user_module(self, name: str) -> None: · line 325

Quoted 6 lines
scripts/qapi/commands.py +4 -0
  ''',
                               commands=commands, visit=visit))
+
+        if self._gen_tracing and commands != 'qapi-commands':
+            self._genc.add(mcgen('''
+#include "trace/trace-qapi.h"

I believe this include is superfluous.

No objections to drop it

Quoted 14 lines
Quoted 11 lines
scripts/qapi/commands.py +7 -0
+#include "qapi/qmp/qjson.h"
+#include "trace/trace-%(nm)s_trace_events.h"
+''',
+                                 nm=c_name(commands, protect=False)))
+            # We use c_name(commands, protect=False) to turn '-' into '_', to
+            # match .underscorify() in trace/meson.build
+
          self._genh.add(mcgen('''
  #include "%(types)s.h"
 

[...]

Signature · 2 lines
Best regards,
Vladimir
#12 lore raw

Re: [PATCH v6 6/7] meson: document, why we don't generate trace events for tests/ and qga/

In reply to Markus Armbruster
Quoted 41 lines · Markus Armbruster
Quoted 39 lines · Vladimir Sementsov-Ogievskiy

Making trace generation work for tests/ and qga/ would involve some Meson hackery to ensure we generate the trace-events files before trace-tool uses them. Since we don't actually support tracing there anyway, we bypass that problem.

Let's add corresponding comments.

---
  qga/meson.build   | 7 +++++++
  tests/meson.build | 7 +++++++
  2 files changed, 14 insertions(+)
qga/meson.build +7 -0
index cfb1fbc085..724d5a667b 100644
@@ -15,6 +15,13 @@ qga_qapi_outputs = [
    'qga-qapi-visit.h',
  ]
 
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
  qga_qapi_files = custom_target('QGA QAPI files',
                                 output: qga_qapi_outputs,
                                 input: 'qapi-schema.json',
tests/meson.build +0 -0
index 3f3882748a..af8f5b1746 100644
@@ -31,6 +31,13 @@ test_qapi_outputs = [
    'test-qapi-visit.h',
  ]
 
Quoted 6 lines

tests/meson.build test_qapi_outputs = [ · line 37

Quoted 4 lines
tests/meson.build +4 -0
+# Problem: to generate trace events, we'd have to add the .trace-events
+# file to qapi_trace_events like we do in qapi/meson.build.  Since
+# qapi_trace_events is used by trace/meson.build, we'd have to move
+# subdir('qga') above subdir('trace') in the top-level meson.build.

Shouldn't this be subdir('tests')?

Oops right.

Quoted 8 lines
Quoted 7 lines
tests/meson.build +3 -0
+# Can't, because it would break the dependency of qga on qemuutil (which
+# depends on trace_ss).  Not worth solving now; simply suppress trace
+# event generation instead.
  test_qapi_files = custom_target('Test QAPI files',
                                  output: test_qapi_outputs,
                                  input: files('qapi-schema/qapi-schema-test.json',
Signature · 2 lines
Best regards,
Vladimir
#17 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 11 lines · Vladimir Sementsov-Ogievskiy
Quoted 9 lines · Markus Armbruster
Quoted 2 lines · Vladimir Sementsov-Ogievskiy

This series aims to add trace points for each qmp command with help of qapi code generator.

Logging QMP traffic has worked well enough for me, but I can understand
why you'd like to use tracing. Other uses of tracing might come up in the future.
I found a few last nits to pick. Happy to address them in my tree.

Quoted 7 lines

That would be great, thanks a lot!

Quoted 1 line

Except for PATCH 4

It has "Acked-by: Paolo", so seems we are done.

Quoted 1 line

Queued, thanks!