Skip to contentSkip to conversation

trace: fix trace_event_get_state() for SystemTap and LTTng UST

PATCH Applied Stefan Hajnoczi 4 participants 11 messages Jul 31, 2017 – Aug 1, 2017 Networking

lore
Share & tools
lore
#0 lore raw

v2 cover trace: fix trace_event_get_state() for SystemTap and LTTng UST

v2:
* Don't special-case QEMU dstate [Daniel Berrange]
* Use _backends() postfix to clarify function purpose [Lluís]

Trace events that compute their arguments can affect performance. The following idom can be used to avoid computing arguments when the trace event is disabled:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      char *str = g_strdup_printf("Expensive string ...", ...);
      trace_my_event(str);
      g_free(str);
  }

Unfortunately this breaks the trace event for SystemTap and LTTng UST since those tracers manage their own enabled/disabled state.

These patches add per-backend dstate to trace_event_get_state() so that the trace event fires as expected when enabled via SystemTap or LTTng UST.

Stefan Hajnoczi (2):
trace: add TRACE_<event>_BACKEND_DSTATE()
trace: add trace_event_get_state_backends()

 docs/devel/tracing.txt                |  2 +-
 trace/control.h                       | 18 +++++++++++++++++-
 hw/usb/hcd-ohci.c                     | 13 +++++--------
 net/colo-compare.c                    | 11 ++++++-----
 net/filter-rewriter.c                 |  4 ++--
 scripts/tracetool/__init__.py         |  1 +
 scripts/tracetool/backend/__init__.py |  3 +++
 scripts/tracetool/backend/dtrace.py   | 12 ++++++++++++
 scripts/tracetool/backend/ftrace.py   |  5 +++++
 scripts/tracetool/backend/log.py      |  5 +++++
 scripts/tracetool/backend/simple.py   |  5 +++++
 scripts/tracetool/backend/syslog.py   |  5 +++++
 scripts/tracetool/backend/ust.py      |  5 +++++
 scripts/tracetool/format/h.py         | 10 ++++++++++
 14 files changed, 82 insertions(+), 17 deletions(-)
Signature · 1 line
2.13.3
#1 lore raw

PATCH v2 1/2 trace: add TRACE_<event>_BACKEND_DSTATE()

QEMU keeps track of trace event enabled/disabled state and provides monitor commands to inspect and modify the "dstate". SystemTap and LTTng UST maintain independent enabled/disabled states for each trace event, the other backends rely on QEMU dstate.

Introduce a new per-event macro that combines backend-specific dstate like this:

  #define TRACE_MY_EVENT_BACKEND_DSTATE() ( \
      QEMU_MY_EVENT_ENABLED() || /* SystemTap */ \
      tracepoint_enabled(qemu, my_event) /* LTTng UST */ || \
      false)

This will be used to extend trace_event_get_state() in the next patch.

---
v2:
* Don't special-case QEMU dstate [Daniel Berrange]

---
 scripts/tracetool/__init__.py         |  1 +
 scripts/tracetool/backend/__init__.py |  3 +++
 scripts/tracetool/backend/dtrace.py   | 12 ++++++++++++
 scripts/tracetool/backend/ftrace.py   |  5 +++++
 scripts/tracetool/backend/log.py      |  5 +++++
 scripts/tracetool/backend/simple.py   |  5 +++++
 scripts/tracetool/backend/syslog.py   |  5 +++++
 scripts/tracetool/backend/ust.py      |  5 +++++
 scripts/tracetool/format/h.py         | 10 ++++++++++
 9 files changed, 51 insertions(+)
scripts/tracetool/__init__.py +1 -0
index d4c204a472..0670ec17d5 100644
@@ -271,6 +271,7 @@ class Event(object):
     QEMU_TRACE_NOCHECK       = "_nocheck__" + QEMU_TRACE
     QEMU_TRACE_TCG           = QEMU_TRACE + "_tcg"
     QEMU_DSTATE              = "_TRACE_%(NAME)s_DSTATE"
+    QEMU_BACKEND_DSTATE      = "TRACE_%(NAME)s_BACKEND_DSTATE"
     QEMU_EVENT               = "_TRACE_%(NAME)s_EVENT"
 
     def api(self, fmt=None):
scripts/tracetool/backend/__init__.py +3 -0
index f735a259c0..259c6a6531 100644
@@ -119,5 +119,8 @@ class Wrapper:
     def generate(self, event, group):
         self._run_function("generate_%s", event, group)
 
+    def generate_backend_dstate(self, event, group):
+        self._run_function("generate_%s_backend_dstate", event, group)
+
     def generate_end(self, events, group):
         self._run_function("generate_%s_end", events, group)
scripts/tracetool/backend/dtrace.py +12 -0
index c6812b70a2..17f902cc62 100644
@@ -44,8 +44,20 @@ def generate_h_begin(events, group):
     out('#include "%s"' % header,
         '')
 
+    # SystemTap defines <provider>_<name>_ENABLED() but other DTrace
+    # implementations might not.
+    for e in events:
+        out('#ifndef QEMU_%(uppername)s_ENABLED',
+            '#define QEMU_%(uppername)s_ENABLED() false',
+            '#endif',
+            uppername=e.name.upper())
 
 def generate_h(event, group):
     out('    QEMU_%(uppername)s(%(argnames)s);',
         uppername=event.name.upper(),
         argnames=", ".join(event.args.names()))
+
+
+def generate_h_backend_dstate(event, group):
+    out('    QEMU_%(uppername)s_ENABLED() || \\',
+        uppername=event.name.upper())
scripts/tracetool/backend/ftrace.py +5 -0
index dd0eda4441..92f71b28f9 100644
@@ -45,3 +45,8 @@ def generate_h(event, group):
         event_id="TRACE_" + event.name.upper(),
         fmt=event.fmt.rstrip("\n"),
         argnames=argnames)
+
+
+def generate_h_backend_dstate(event, group):
+    out('    trace_event_get_state_dynamic_by_id(%(event_id)s) || \\',
+        event_id="TRACE_" + event.name.upper())
scripts/tracetool/backend/log.py +5 -0
index 54f0a69886..da86f6b882 100644
@@ -48,3 +48,8 @@ def generate_h(event, group):
         name=event.name,
         fmt=event.fmt.rstrip("\n"),
         argnames=argnames)
+
+
+def generate_h_backend_dstate(event, group):
+    out('    trace_event_get_state_dynamic_by_id(%(event_id)s) || \\',
+        event_id="TRACE_" + event.name.upper())
scripts/tracetool/backend/simple.py +5 -0
index f983670ee1..c2fd1c24c4 100644
@@ -42,6 +42,11 @@ def generate_h(event, group):
         args=", ".join(event.args.names()))
 
 
+def generate_h_backend_dstate(event, group):
+    out('    trace_event_get_state_dynamic_by_id(%(event_id)s) || \\',
+        event_id="TRACE_" + event.name.upper())
+
+
 def generate_c_begin(events, group):
     out('#include "qemu/osdep.h"',
         '#include "trace/control.h"',
scripts/tracetool/backend/syslog.py +5 -0
index 1ce627f0fc..668fb73fee 100644
@@ -42,3 +42,8 @@ def generate_h(event, group):
         name=event.name,
         fmt=event.fmt.rstrip("\n"),
         argnames=argnames)
+
+
+def generate_h_backend_dstate(event, group):
+    out('    trace_event_get_state_dynamic_by_id(%(event_id)s) || \\',
+        event_id="TRACE_" + event.name.upper())
scripts/tracetool/backend/ust.py +5 -0
index 2adaf548d5..412592b8e0 100644
@@ -38,3 +38,8 @@ def generate_h(event, group):
     out('    tracepoint(qemu, %(name)s%(tp_args)s);',
         name=event.name,
         tp_args=argnames)
+
+
+def generate_h_backend_dstate(event, group):
+    out('    tracepoint_enabled(qemu, %(name)s) || \\',
+        name=event.name)
scripts/tracetool/format/h.py +10 -0
index aecf249d66..e06f0f27c5 100644
@@ -49,6 +49,16 @@ def generate(events, backend, group):
     backend.generate_begin(events, group)
 
     for e in events:
+        # tracer-specific dstate
+        out('',
+            '#define %(api)s() ( \\',
+            api=e.api(e.QEMU_BACKEND_DSTATE))
+
+        if "disable" not in e.properties:
+            backend.generate_backend_dstate(e, group)
+
+        out('    false)')
+
         # tracer without checks
         out('',
             'static inline void %(api)s(%(args)s)',
Signature · 1 line
2.13.3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] trace: add TRACE_<event>_BACKEND_DSTATE()

Quoted 23 lines · Stefan Hajnoczi

QEMU keeps track of trace event enabled/disabled state and provides monitor commands to inspect and modify the "dstate". SystemTap and LTTng UST maintain independent enabled/disabled states for each trace event, the other backends rely on QEMU dstate.

Introduce a new per-event macro that combines backend-specific dstate like this:

  #define TRACE_MY_EVENT_BACKEND_DSTATE() ( \
      QEMU_MY_EVENT_ENABLED() || /* SystemTap */ \
      tracepoint_enabled(qemu, my_event) /* LTTng UST */ || \
      false)

This will be used to extend trace_event_get_state() in the next patch.

scripts/tracetool/backend/dtrace.py +0 -0
index c6812b70a2..17f902cc62 100644

scripts/tracetool/backend/dtrace.py def generate_h_begin(events, group): · line 53

Quoted 10 lines
scripts/tracetool/backend/dtrace.py +7 -0
@@ -44,8 +44,20 @@ def generate_h_begin(events, group):
     out('#include "%s"' % header,
         '')
 
+    # SystemTap defines <provider>_<name>_ENABLED() but other DTrace
+    # implementations might not.
+    for e in events:
+        out('#ifndef QEMU_%(uppername)s_ENABLED',
+            '#define QEMU_%(uppername)s_ENABLED() false',
+            '#endif',
+            uppername=e.name.upper())

IIUC, this means that on other DTrace impls, any trace points guarded by QEMU_*_ENABLED() will never run, even if DTrace probes are set. Having some trace points silently never run makes it pretty useless IMHO.

IOW, you need the opposite, #define it to true. The probe will still only be executed if DTrace has enabled it, but you'll have to take the hit of evaluating the probe arguments regardless. Not as optimized performance-wise, but functionally correct at least.

scripts/tracetool/backend/dtrace.py def generate_h_begin(events, group): · line 63

Quoted 10 lines
scripts/tracetool/backend/dtrace.py +5 -0
 def generate_h(event, group):
     out('    QEMU_%(uppername)s(%(argnames)s);',
         uppername=event.name.upper(),
         argnames=", ".join(event.args.names()))
+
+
+def generate_h_backend_dstate(event, group):
+    out('    QEMU_%(uppername)s_ENABLED() || \\',
+        uppername=event.name.upper())

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] trace: add TRACE_<event>_BACKEND_DSTATE()

Quoted 22 lines · Daniel P. Berrange
Quoted 5 lines · Stefan Hajnoczi
scripts/tracetool/backend/dtrace.py +0 -0
index c6812b70a2..17f902cc62 100644

scripts/tracetool/backend/dtrace.py def generate_h_begin(events, group): · line 53

Quoted 10 lines
scripts/tracetool/backend/dtrace.py +7 -0
@@ -44,8 +44,20 @@ def generate_h_begin(events, group):
     out('#include "%s"' % header,
         '')
 
+    # SystemTap defines <provider>_<name>_ENABLED() but other DTrace
+    # implementations might not.
+    for e in events:
+        out('#ifndef QEMU_%(uppername)s_ENABLED',
+            '#define QEMU_%(uppername)s_ENABLED() false',
+            '#endif',
+            uppername=e.name.upper())

IIUC, this means that on other DTrace impls, any trace points guarded by QEMU_*_ENABLED() will never run, even if DTrace probes are set. Having some trace points silently never run makes it pretty useless IMHO.

Quoted 4 lines

IOW, you need the opposite, #define it to true. The probe will still only be executed if DTrace has enabled it, but you'll have to take the hit of evaluating the probe arguments regardless. Not as optimized performance-wise, but functionally correct at least.

Good point! Thank you, will fix when merging.

Stefan

Reviewed-by
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] trace: add TRACE_<event>_BACKEND_DSTATE()

Quoted 23 lines · Stefan Hajnoczi
Quoted 22 lines · Daniel P. Berrange
Quoted 5 lines · Stefan Hajnoczi
scripts/tracetool/backend/dtrace.py +0 -0
index c6812b70a2..17f902cc62 100644

scripts/tracetool/backend/dtrace.py def generate_h_begin(events, group): · line 53

Quoted 10 lines
scripts/tracetool/backend/dtrace.py +7 -0
@@ -44,8 +44,20 @@ def generate_h_begin(events, group):
     out('#include "%s"' % header,
         '')
 
+    # SystemTap defines <provider>_<name>_ENABLED() but other DTrace
+    # implementations might not.
+    for e in events:
+        out('#ifndef QEMU_%(uppername)s_ENABLED',
+            '#define QEMU_%(uppername)s_ENABLED() false',
+            '#endif',
+            uppername=e.name.upper())

IIUC, this means that on other DTrace impls, any trace points guarded by QEMU_*_ENABLED() will never run, even if DTrace probes are set. Having some trace points silently never run makes it pretty useless IMHO.

Quoted 6 lines
Quoted 4 lines

IOW, you need the opposite, #define it to true. The probe will still only be executed if DTrace has enabled it, but you'll have to take the hit of evaluating the probe arguments regardless. Not as optimized performance-wise, but functionally correct at least.

Good point! Thank you, will fix when merging.

Ok, You can add

if the change of false to true is made.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw

PATCH v2 2/2 trace: add trace_event_get_state_backends()

In reply to Stefan Hajnoczi

Code that checks dstate is unaware of SystemTap and LTTng UST dstate, so the following trace event will not fire when solely enabled by SystemTap or LTTng UST:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      str = g_strdup_printf("Expensive string to generate ...",
                            ...);
      trace_my_event(str);
      g_free(str);
  }

Add trace_event_get_state_backends() to fetch backend dstate. Those backends that use QEMU dstate fetch it as part of generate_h_backend_dstate().

Update existing trace_event_get_state() callers to use trace_event_get_state_backends() instead.

---
v2:
* Use _backends() postfix to clarify function purpose [Lluís]

---
 docs/devel/tracing.txt |  2 +-
 trace/control.h        | 18 +++++++++++++++++-
 hw/usb/hcd-ohci.c      | 13 +++++--------
 net/colo-compare.c     | 11 ++++++-----
 net/filter-rewriter.c  |  4 ++--
 5 files changed, 31 insertions(+), 17 deletions(-)
docs/devel/tracing.txt +1 -1
index 5768a0b7a2..07abbb345c 100644
@@ -353,7 +353,7 @@ guard such computations and avoid its compilation when the event is disabled:
     }
 
 You can check both if the event has been disabled and is dynamically enabled at
-the same time using the 'trace_event_get_state' routine (see header
+the same time using the 'trace_event_get_state_backends' routine (see header
 "trace/control.h" for more information).
 
 === "tcg" ===
trace/control.h +17 -1
index b931824d60..1903e22975 100644
@@ -96,7 +96,7 @@ static const char * trace_event_get_name(TraceEvent *ev);
  * trace_event_get_state:
  * @id: Event identifier name.
  *
- * Get the tracing state of an event (both static and dynamic).
+ * Get the tracing state of an event, both static and the QEMU dynamic state.
  *
  * If the event has the disabled property, the check will have no performance
  * impact.
@@ -105,6 +105,22 @@ static const char * trace_event_get_name(TraceEvent *ev);
     ((id ##_ENABLED) && trace_event_get_state_dynamic_by_id(id))
 
 /**
+ * trace_event_get_state_backends:
+ * @id: Event identifier name.
+ *
+ * Get the tracing state of an event, both static and dynamic state from all
+ * compiled-in backends.
+ *
+ * If the event has the disabled property, the check will have no performance
+ * impact.
+ *
+ * Returns: true if at least one backend has the event enabled and the event
+ * does not have the disabled property.
+ */
+#define trace_event_get_state_backends(id)              \
+    ((id ##_ENABLED) && id ##_BACKEND_DSTATE())
+
+/**
  * trace_event_get_vcpu_state:
  * @vcpu: Target vCPU.
  * @id: Event identifier name.
hw/usb/hcd-ohci.c +5 -8
index 3ada35e954..267982e160 100644
@@ -936,16 +936,18 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
     return 1;
 }
 
-#ifdef trace_event_get_state
 static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
 {
-    bool print16 = !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_SHORT);
-    bool printall = !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_FULL);
+    bool print16;
+    bool printall;
     const int width = 16;
     int i;
     char tmp[3 * width + 1];
     char *p = tmp;
 
+    print16 = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_SHORT);
+    printall = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_FULL);
+
     if (!printall && !print16) {
         return;
     }
@@ -967,11 +969,6 @@ static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
         p += sprintf(p, " %.2x", buf[i]);
     }
 }
-#else
-static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
-{
-}
-#endif
 
 /* Service a transport descriptor.
    Returns nonzero to terminate processing of this endpoint.  */
net/colo-compare.c +6 -5
index ca67c68615..5fe8e3fad9 100644
@@ -188,7 +188,7 @@ static int packet_enqueue(CompareState *s, int mode)
  */
 static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
 {
-    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
@@ -274,7 +274,8 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
         res = -1;
     }
 
-    if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (res != 0 &&
+        trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
@@ -334,7 +335,7 @@ static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
     if (ret) {
         trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
         trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
-        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+        if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
                          ppkt->size);
             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
@@ -371,7 +372,7 @@ static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
                                            ppkt->size);
         trace_colo_compare_icmp_miscompare("Secondary pkt size",
                                            spkt->size);
-        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+        if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
                          ppkt->size);
             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
@@ -390,7 +391,7 @@ static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
 {
     trace_colo_compare_main("compare other");
-    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
net/filter-rewriter.c +2 -2
index 55a6cf56fd..98120095de 100644
@@ -69,7 +69,7 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
     struct tcphdr *tcp_pkt;
 
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
-    if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
+    if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
         trace_colo_filter_rewriter_pkt_info(__func__,
                     inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
@@ -115,7 +115,7 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
 
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
 
-    if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
+    if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
         trace_colo_filter_rewriter_pkt_info(__func__,
                     inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
Signature · 1 line
2.13.3
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] trace: add trace_event_get_state_backends()

Quoted 10 lines · Stefan Hajnoczi

Code that checks dstate is unaware of SystemTap and LTTng UST dstate, so the following trace event will not fire when solely enabled by SystemTap or LTTng UST:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      str = g_strdup_printf("Expensive string to generate ...",
                            ...);
      trace_my_event(str);
      g_free(str);
  }

I believe this should be trace_event_get_state_backends(). Same applies to the cover letter.

Cheers,
Lluis

Quoted 130 lines

Add trace_event_get_state_backends() to fetch backend dstate. Those backends that use QEMU dstate fetch it as part of generate_h_backend_dstate().

Update existing trace_event_get_state() callers to use trace_event_get_state_backends() instead.

---
v2:
* Use _backends() postfix to clarify function purpose [Lluís]

---
 docs/devel/tracing.txt |  2 +-
 trace/control.h        | 18 +++++++++++++++++-
 hw/usb/hcd-ohci.c      | 13 +++++--------
 net/colo-compare.c     | 11 ++++++-----
 net/filter-rewriter.c  |  4 ++--
 5 files changed, 31 insertions(+), 17 deletions(-)
docs/devel/tracing.txt +1 -1
index 5768a0b7a2..07abbb345c 100644
@@ -353,7 +353,7 @@ guard such computations and avoid its compilation when the event is disabled:
     }
 
 You can check both if the event has been disabled and is dynamically enabled at
-the same time using the 'trace_event_get_state' routine (see header
+the same time using the 'trace_event_get_state_backends' routine (see header
 "trace/control.h" for more information).
 
 === "tcg" ===
trace/control.h +17 -1
index b931824d60..1903e22975 100644
@@ -96,7 +96,7 @@ static const char * trace_event_get_name(TraceEvent *ev);
  * trace_event_get_state:
  * @id: Event identifier name.
  *
- * Get the tracing state of an event (both static and dynamic).
+ * Get the tracing state of an event, both static and the QEMU dynamic state.
  *
  * If the event has the disabled property, the check will have no performance
  * impact.
@@ -105,6 +105,22 @@ static const char * trace_event_get_name(TraceEvent *ev);
     ((id ##_ENABLED) && trace_event_get_state_dynamic_by_id(id))
 
 /**
+ * trace_event_get_state_backends:
+ * @id: Event identifier name.
+ *
+ * Get the tracing state of an event, both static and dynamic state from all
+ * compiled-in backends.
+ *
+ * If the event has the disabled property, the check will have no performance
+ * impact.
+ *
+ * Returns: true if at least one backend has the event enabled and the event
+ * does not have the disabled property.
+ */
+#define trace_event_get_state_backends(id)              \
+    ((id ##_ENABLED) && id ##_BACKEND_DSTATE())
+
+/**
  * trace_event_get_vcpu_state:
  * @vcpu: Target vCPU.
  * @id: Event identifier name.
hw/usb/hcd-ohci.c +5 -8
index 3ada35e954..267982e160 100644
@@ -936,16 +936,18 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
     return 1;
 }
 
-#ifdef trace_event_get_state
 static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
 {
-    bool print16 = !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_SHORT);
-    bool printall = !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_FULL);
+    bool print16;
+    bool printall;
     const int width = 16;
     int i;
     char tmp[3 * width + 1];
     char *p = tmp;
 
+    print16 = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_SHORT);
+    printall = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_FULL);
+
     if (!printall && !print16) {
         return;
     }
@@ -967,11 +969,6 @@ static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
         p += sprintf(p, " %.2x", buf[i]);
     }
 }
-#else
-static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
-{
-}
-#endif
 
 /* Service a transport descriptor.
    Returns nonzero to terminate processing of this endpoint.  */
net/colo-compare.c +3 -2
index ca67c68615..5fe8e3fad9 100644
@@ -188,7 +188,7 @@ static int packet_enqueue(CompareState *s, int mode)
  */
 static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
 {
-    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
@@ -274,7 +274,8 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
         res = -1;
     }
 
-    if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (res != 0 &&
+        trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));

net/colo-compare.c static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) · line 339

Quoted 6 lines
net/colo-compare.c +1 -1
@@ -334,7 +335,7 @@ static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
     if (ret) {
         trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
         trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
-        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+        if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",

ppkt-> size);

net/colo-compare.c static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)

Quoted 2 lines
net/colo-compare.c +0 -0
             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
@@ -371,7 +372,7 @@ static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)

ppkt-> size);

net/colo-compare.c static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) · line 373

Quoted 1 line
net/colo-compare.c +0 -0
         trace_colo_compare_icmp_miscompare("Secondary pkt size",

spkt-> size);

net/colo-compare.c static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) · line 339

Quoted 3 lines
net/colo-compare.c +1 -1
-        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+        if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",

ppkt-> size);

Quoted 34 lines
net/colo-compare.c +1 -1
             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
@@ -390,7 +391,7 @@ static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
 {
     trace_colo_compare_main("compare other");
-    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+    if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 
         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
net/filter-rewriter.c +2 -2
index 55a6cf56fd..98120095de 100644
@@ -69,7 +69,7 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
     struct tcphdr *tcp_pkt;
 
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
-    if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
+    if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
         trace_colo_filter_rewriter_pkt_info(__func__,
                     inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
@@ -115,7 +115,7 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
 
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
 
-    if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
+    if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
         trace_colo_filter_rewriter_pkt_info(__func__,
                     inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
Signature · 1 line
2.13.3
#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] trace: add trace_event_get_state_backends()

Quoted 13 lines · Lluís Vilanova
Quoted 10 lines · Stefan Hajnoczi

Code that checks dstate is unaware of SystemTap and LTTng UST dstate, so the following trace event will not fire when solely enabled by SystemTap or LTTng UST:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      str = g_strdup_printf("Expensive string to generate ...",
                            ...);
      trace_my_event(str);
      g_free(str);
  }
Quoted 2 lines

I believe this should be trace_event_get_state_backends(). Same applies to the cover letter.

This instance and the cover letter are both showing the pattern in existing code.

The description "event will not fire" is correct with trace_event_get_state(). If I change it to trace_event_get_state_backends() then the description is no longer correct :).

Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] trace: add trace_event_get_state_backends()

Quoted 17 lines · Stefan Hajnoczi
Quoted 13 lines · Lluís Vilanova
Quoted 10 lines · Stefan Hajnoczi

Code that checks dstate is unaware of SystemTap and LTTng UST dstate, so the following trace event will not fire when solely enabled by SystemTap or LTTng UST:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      str = g_strdup_printf("Expensive string to generate ...",
                            ...);
      trace_my_event(str);
      g_free(str);
  }
Quoted 2 lines

I believe this should be trace_event_get_state_backends(). Same applies to the cover letter.

Quoted 7 lines

This instance and the cover letter are both showing the pattern in existing code.

The description "event will not fire" is correct with trace_event_get_state(). If I change it to trace_event_get_state_backends() then the description is no longer correct :).

Woops, I read this too quickly and assumed it was a usage example, sorry :)

Cheers,
Lluis

Reviewed-by on v2 2/2
#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] trace: add trace_event_get_state_backends()

In reply to Stefan Hajnoczi
Quoted 22 lines · Stefan Hajnoczi

Code that checks dstate is unaware of SystemTap and LTTng UST dstate, so the following trace event will not fire when solely enabled by SystemTap or LTTng UST:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      str = g_strdup_printf("Expensive string to generate ...",
                            ...);
      trace_my_event(str);
      g_free(str);
  }

Add trace_event_get_state_backends() to fetch backend dstate. Those backends that use QEMU dstate fetch it as part of generate_h_backend_dstate().

Update existing trace_event_get_state() callers to use trace_event_get_state_backends() instead.

---
v2:
* Use _backends() postfix to clarify function purpose [Lluís]

Quoted 7 lines
---
 docs/devel/tracing.txt |  2 +-
 trace/control.h        | 18 +++++++++++++++++-
 hw/usb/hcd-ohci.c      | 13 +++++--------
 net/colo-compare.c     | 11 ++++++-----
 net/filter-rewriter.c  |  4 ++--
 5 files changed, 31 insertions(+), 17 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw
In reply to Stefan Hajnoczi
Quoted 24 lines · Stefan Hajnoczi

v2:
* Don't special-case QEMU dstate [Daniel Berrange]
* Use _backends() postfix to clarify function purpose [Lluís]

Trace events that compute their arguments can affect performance. The following idom can be used to avoid computing arguments when the trace event is disabled:

  if (trace_event_get_state(TRACE_MY_EVENT)) {
      char *str = g_strdup_printf("Expensive string ...", ...);
      trace_my_event(str);
      g_free(str);
  }

Unfortunately this breaks the trace event for SystemTap and LTTng UST since those tracers manage their own enabled/disabled state.

These patches add per-backend dstate to trace_event_get_state() so that the trace event fires as expected when enabled via SystemTap or LTTng UST.

Stefan Hajnoczi (2):
trace: add TRACE_<event>_BACKEND_DSTATE()
trace: add trace_event_get_state_backends()

Quoted 20 lines
 docs/devel/tracing.txt                |  2 +-
 trace/control.h                       | 18 +++++++++++++++++-
 hw/usb/hcd-ohci.c                     | 13 +++++--------
 net/colo-compare.c                    | 11 ++++++-----
 net/filter-rewriter.c                 |  4 ++--
 scripts/tracetool/__init__.py         |  1 +
 scripts/tracetool/backend/__init__.py |  3 +++
 scripts/tracetool/backend/dtrace.py   | 12 ++++++++++++
 scripts/tracetool/backend/ftrace.py   |  5 +++++
 scripts/tracetool/backend/log.py      |  5 +++++
 scripts/tracetool/backend/simple.py   |  5 +++++
 scripts/tracetool/backend/syslog.py   |  5 +++++
 scripts/tracetool/backend/ust.py      |  5 +++++
 scripts/tracetool/format/h.py         | 10 ++++++++++
 14 files changed, 82 insertions(+), 17 deletions(-)
Signature · 1 line
2.13.3

Thanks, applied to my tracing tree:
https://github.com/stefanha/qemu/commits/tracing

Stefan