Skip to contentSkip to conversation

trace: Generic event state description

PATCH Superseded Lluís Vilanova 1 participant 7 messages Feb 3, 2012 Kbuild

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

cover trace: Generic event state description

NOTE: Applies on top of the tracetool-handling trivial changes.

Provides a generic event state description and a more detailed event control and query interface.

---

Lluís Vilanova (6):
trace: Provide a generic tracing event descriptor
trace: Provide a detailed event control interface
trace: [monitor] Use new event control interface
trace: [default] Use new event control interface
trace: [simple] Port to generic event information and new control interface
trace: [stderr] Port to generic event information and new control interface

 Makefile                 |    5 ++
 Makefile.objs            |   13 ++++
 docs/tracing.txt         |   38 ++++--------
 monitor.c                |   15 ++++-
 scripts/tracetool.py     |  144 ++++++++++++++++++++++++++++++++--------------
 trace/control-internal.h |   49 ++++++++++++++++
 trace/control.c          |   89 ++++++++++++++++++++++++++--
 trace/control.h          |  126 +++++++++++++++++++++++++++++++++++++---
 trace/default.c          |    3 -
 trace/event-internal.h   |   31 ++++++++++
 trace/simple.c           |   32 ++--------
 trace/simple.h           |    6 --
 trace/stderr.c           |   34 ++---------
 trace/stderr.h           |   11 ----
 14 files changed, 434 insertions(+), 162 deletions(-)
 create mode 100644 trace/control-internal.h
 create mode 100644 trace/event-internal.h
 delete mode 100644 trace/stderr.h

To: [email protected]
Cc: Stefan Hajnoczi <[email protected]>
Cc: Blue Swirl <[email protected]>

#1 lore raw

PATCH 1/6 trace: Provide a generic tracing event descriptor

Uses tracetool to generate a backend-independent tracing event description.

---
 Makefile               |    5 +++
 Makefile.objs          |   13 +++++++
 scripts/tracetool.py   |   90 ++++++++++++++++++++++++++++++++++++++++++++----
 trace/event-internal.h |   31 +++++++++++++++++
 4 files changed, 132 insertions(+), 7 deletions(-)
 create mode 100644 trace/event-internal.h
Makefile +5 -0
index a9f3c7e..13e603c 100644
@@ -4,9 +4,14 @@
 BUILD_DIR=$(CURDIR)
 
 GENERATED_HEADERS = config-host.h trace.h qemu-options.def
+
+GENERATED_HEADERS += trace-events.h
+GENERATED_SOURCES += trace-events.c
+
 ifeq ($(TRACE_BACKEND),dtrace)
 GENERATED_HEADERS += trace-dtrace.h
 endif
+
 GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h
 GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c trace.c
 
Makefile.objs +13 -0
index e6644c6..194a33d 100644
@@ -348,6 +348,19 @@ libdis-$(CONFIG_SPARC_DIS) += sparc-dis.o
 ######################################################################
 # trace
 
+trace-events.h: trace-events.h-timestamp
+trace-events.h-timestamp: $(SRC_PATH)/trace-events $(TRACETOOL)
+	$(call tracetool-gen,events-h,events)
+	$(call tracetool-ci)
+
+trace-events.c: trace-events.c-timestamp
+trace-events.c-timestamp: $(SRC_PATH)/trace-events $(TRACETOOL)
+	$(call tracetool-gen,events-c,events)
+	$(call tracetool-ci)
+
+trace-obj-y += trace-events.o
+
+
 ifeq ($(TRACE_BACKEND),dtrace)
 TRACE_H_EXTRA_DEPS=trace-dtrace.h
 endif
scripts/tracetool.py +83 -7

108 lines not on this page · raw message

trace/event-internal.h new file +31 -0
new file mode 100644
index 0000000..48f1298
@@ -0,0 +1,31 @@
+/*
+ * Interface for configuring and controlling the state of tracing events.
+ *
+ * Copyright (C) 2012 Lluís Vilanova <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef TRACE__EVENT_H
+#define TRACE__EVENT_H
+
+#include "trace-events.h"
+
+
+/**
+ * Generic description of a tracing event.
+ */
+typedef struct {
+    /** Unique event identifier. */
+    TraceEventID id;
+    /** Event name. */
+    const char * name;
+    /** Static instrumentation state. */
+    const bool sstate;
+    /** Dynamic instrumentation state. */
+    bool dstate;
+} TraceEvent;
+
+
+#endif  /* TRACE__EVENT_H */
#2 lore raw

PATCH 2/6 trace: Provide a detailed event control interface

In reply to Lluís Vilanova

This interface decouples event obtention from interaction.

Event can be obtained through three different methods:

* identifier
* name
* simple wildcard pattern

---
 docs/tracing.txt         |   38 +++++---------
 trace/control-internal.h |   49 ++++++++++++++++++
 trace/control.c          |   89 ++++++++++++++++++++++++++++++--
 trace/control.h          |  126 ++++++++++++++++++++++++++++++++++++++++++----
 4 files changed, 260 insertions(+), 42 deletions(-)
 create mode 100644 trace/control-internal.h
docs/tracing.txt +13 -25
index a92716f..787cabd 100644
@@ -100,44 +100,32 @@ respectively.  This ensures portability between 32- and 64-bit platforms.
 
 == Generic interface and monitor commands ==
 
-You can programmatically query and control the dynamic state of trace events
-through a backend-agnostic interface:
+You can programmatically query and control the state of trace events through a
+backend-agnostic interface provided by the file "trace/control.h".
 
-* trace_print_events
+Note that some of the backends do not provide an implementation for some parts
+of this interface, in which case QEMU will just print a warning (please refer to
+header "trace/control.h" to see which routines are backend-dependant).
 
-* trace_event_set_state
-  Enables or disables trace events at runtime inside QEMU.
-  The function returns "true" if the state of the event has been successfully
-  changed, or "false" otherwise:
-
-    #include "trace/control.h"
-    
-    trace_event_set_state("virtio_irq", true); /* enable */
-    [...]
-    trace_event_set_state("virtio_irq", false); /* disable */
-
-Note that some of the backends do not provide an implementation for this
-interface, in which case QEMU will just print a warning.
-
-This functionality is also provided through monitor commands:
+The state of events can also be queried and modified through monitor commands:
 
 * info trace-events
   View available trace events and their state.  State 1 means enabled, state 0
   means disabled.
 
 * trace-event NAME on|off
-  Enable/disable a given trace event or a group of events having common prefix
-  through wildcard.
+  Enable/disable a given trace event or a group of events (using wildcards).
 
 The "-trace events=<file>" command line argument can be used to enable the
 events listed in <file> from the very beginning of the program. This file must
 contain one event name per line.
 
-A basic wildcard matching is supported in both the monitor command "trace
--event" and the events list file. That means you can enable/disable the events
-having a common prefix in a batch. For example, virtio-blk trace events could
-be enabled using:
-  trace-event virtio_blk_* on
+Wildcard matching is supported in both the monitor command "trace -event" and
+the events list file. That means you can enable/disable the events having a
+common prefix in a batch. For example, virtio-blk trace events could be enabled
+using the following monitor command:
+
+    trace-event virtio_blk_* on
 
 == Trace backends ==
 
trace/control-internal.h new file +49 -0

49 lines not on this page · raw message

trace/control.c +82 -7

107 lines not on this page · raw message

trace/control.h +116 -10

157 lines not on this page · raw message

#3 lore raw

PATCH 3/6 trace: [monitor] Use new event control interface

In reply to Lluís Vilanova
---
 monitor.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
monitor.c +12 -3
index 5e099bd..010f659 100644
@@ -617,10 +617,19 @@ static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
 {
     const char *tp_name = qdict_get_str(qdict, "name");
     bool new_state = qdict_get_bool(qdict, "option");
-    int ret = trace_event_set_state(tp_name, new_state);
 
-    if (!ret) {
-        monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
+    if (trace_event_is_pattern(tp_name)) {
+        TraceEvent *ev = NULL;
+        while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
+            trace_event_set_state_dynamic(ev, new_state);
+        }
+    } else {
+        TraceEvent *ev = trace_event_name(tp_name);
+        if (ev == NULL) {
+            monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
+        } else {
+            trace_event_set_state_dynamic(ev, new_state);
+        }
     }
 }
 
#4 lore raw

PATCH 4/6 trace: [default] Use new event control interface

In reply to Lluís Vilanova
---
 trace/default.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
trace/default.c +1 -2
index c9b27a2..12db18d 100644
@@ -18,11 +18,10 @@ void trace_print_events(FILE *stream, fprintf_function stream_printf)
                   "operation not supported with the current backend\n");
 }
 
-bool trace_event_set_state(const char *name, bool state)
+void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
 {
     fprintf(stderr, "warning: "
             "cannot set the state of a trace event with the current backend\n");
-    return false;
 }
 
 bool trace_backend_init(const char *events, const char *file)
#5 lore raw

PATCH 5/6 trace: [simple] Port to generic event information and new control interface

In reply to Lluís Vilanova
---
 scripts/tracetool.py |   28 +++++++++-------------------
 trace/simple.c       |   32 +++++---------------------------
 trace/simple.h       |    6 +-----
 trace/stderr.h       |   11 -----------
 4 files changed, 15 insertions(+), 62 deletions(-)
 delete mode 100644 trace/stderr.h
scripts/tracetool.py +9 -19
index 91da998..f4ce927 100755
@@ -261,28 +261,17 @@ def simple_h(events):
     'name': event.name,
     'args': event.args
 }
-    print
-    print '#define NR_TRACE_EVENTS %d' % len(events)
-    print 'extern TraceEvent trace_list[NR_TRACE_EVENTS];'
 
 @for_backend("simple", "c")
 def simple_c(events):
     rec_off = 0
-    print '#include "trace.h"'
-    print '#include "trace/simple.h"'
-    print
-    print 'TraceEvent trace_list[] = {'
-    print
-    for event in events:
-        print '{.tp_name = "%(name)s", .state=0},' % {
-    'name': event.name
-}
-        print
-    print '};'
-    print
+    print """\
+#include "trace.h"
+#include "trace/simple.h"
+"""
 
-    for num, event in enumerate(events):
-        sizes = []
+    for event in events:
+        sizes = ["0"]
         for type_, name in event.args:
             if type_is_string(type_):
                 sizes.append("4 + strlen(%s)" % name)
@@ -297,7 +286,8 @@ def simple_c(events):
     uint64_t pvar64 __attribute__ ((unused));
     uint32_t slen __attribute__ ((unused));
 
-    if (!trace_list[%(event_id)s].state) {
+    bool _state = trace_event_get_state(%(event_id)s);
+    if (!_state) {
         return;
     }
 
@@ -306,7 +296,7 @@ def simple_c(events):
 ''' % {
     'name': event.name,
     'args': event.args,
-    'event_id': num,
+    'event_id': "TRACE_" + event.name.upper(),
     'sizestr' : sizestr,
 }
 
trace/simple.c +5 -27
index f5aa3bd..d4da9d3 100644
@@ -263,38 +263,16 @@ void trace_print_events(FILE *stream, fprintf_function stream_printf)
 {
     unsigned int i;
 
-    for (i = 0; i < NR_TRACE_EVENTS; i++) {
+    for (i = 0; i < trace_event_count(); i++) {
+        TraceEvent *ev = trace_event_id(i);
         stream_printf(stream, "%s [Event ID %u] : state %u\n",
-                      trace_list[i].tp_name, i, trace_list[i].state);
+                      trace_event_get_name(ev), i, trace_event_get_state_dynamic(ev));
     }
 }
 
-bool trace_event_set_state(const char *name, bool state)
+void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
 {
-    unsigned int i;
-    unsigned int len;
-    bool wildcard = false;
-    bool matched = false;
-
-    len = strlen(name);
-    if (len > 0 && name[len - 1] == '*') {
-        wildcard = true;
-        len -= 1;
-    }
-    for (i = 0; i < NR_TRACE_EVENTS; i++) {
-        if (wildcard) {
-            if (!strncmp(trace_list[i].tp_name, name, len)) {
-                trace_list[i].state = state;
-                matched = true;
-            }
-            continue;
-        }
-        if (!strcmp(trace_list[i].tp_name, name)) {
-            trace_list[i].state = state;
-            return true;
-        }
-    }
-    return matched;
+    ev->dstate = state;
 }
 
 /* Helper function to create a thread with signals blocked.  Use glib's
trace/simple.h +1 -5
index 671cbeb..6850ac5 100644
@@ -15,12 +15,8 @@
 #include <stdbool.h>
 #include <stdio.h>
 
-typedef uint64_t TraceEventID;
+#include "trace-events.h"
 
-typedef struct {
-    const char *tp_name;
-    bool state;
-} TraceEvent;
 
 void st_print_trace(FILE *stream, fprintf_function stream_printf);
 void st_print_trace_file_status(FILE *stream, fprintf_function stream_printf);
trace/stderr.h deleted +0 -11
deleted file mode 100644
index d575b61..0000000
@@ -1,11 +0,0 @@
-#ifndef TRACE_STDERR_H
-#define TRACE_STDERR_H
-
-typedef uint64_t TraceEventID;
-
-typedef struct {
-    const char *tp_name;
-    bool state;
-} TraceEvent;
-
-#endif /* ! TRACE_STDERR_H */
#6 lore raw

PATCH 6/6 trace: [stderr] Port to generic event information and new control interface

In reply to Lluís Vilanova
---
 scripts/tracetool.py |   26 ++++++++------------------
 trace/stderr.c       |   34 ++++++----------------------------
 2 files changed, 14 insertions(+), 46 deletions(-)
scripts/tracetool.py +8 -18
index f4ce927..c6f5ea6 100755
@@ -343,10 +343,10 @@ def simple_c(events):
 
 @for_backend("stderr", "h", "Stderr built-in backend")
 def stderr_h(events):
-    print '''#include <stdio.h>
-#include "trace/stderr.h"
-
-extern TraceEvent trace_list[];'''
+    print """\
+#include <stdio.h>
+#include "trace/control.h"
+"""
 
     for num, event in enumerate(events):
         argnames = ", ".join(event.args.names())
@@ -355,31 +355,21 @@ extern TraceEvent trace_list[];'''
         print '''
 static inline void trace_%(name)s(%(args)s)
 {
-    if (trace_list[%(event_num)s].state != 0) {
+    bool _state = trace_event_get_state(%(event_id)s);
+    if (_state) {
         fprintf(stderr, "%(name)s " %(fmt)s "\\n" %(argnames)s);
     }
 }''' % {
     'name': event.name,
     'args': event.args,
-    'event_num': num,
+    'event_id': "TRACE_" + event.name.upper(),
     'fmt': event.fmt.rstrip('\n'),
     'argnames': argnames
 }
-    print
-    print '#define NR_TRACE_EVENTS %d' % len(events)
 
 @for_backend("stderr", "c")
 def stderr_c(events):
-    print '''#include "trace.h"
-
-TraceEvent trace_list[] = {
-'''
-    for event in events:
-        print '{.tp_name = "%(name)s", .state=0},' % {
-    'name': event.name
-}
-        print
-    print '};'
+    pass
 
 
 ##################################################
trace/stderr.c +6 -28
index 0810d6f..6685aeb 100644
@@ -4,40 +4,18 @@
 
 void trace_print_events(FILE *stream, fprintf_function stream_printf)
 {
-    unsigned int i;
+    TraceEventID i;
 
-    for (i = 0; i < NR_TRACE_EVENTS; i++) {
+    for (i = 0; i < trace_event_count(); i++) {
+        TraceEvent *ev = trace_event_id(i);
         stream_printf(stream, "%s [Event ID %u] : state %u\n",
-                      trace_list[i].tp_name, i, trace_list[i].state);
+                      trace_event_get_name(ev), i, trace_event_get_state_dynamic(ev));
     }
 }
 
-bool trace_event_set_state(const char *name, bool state)
+void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
 {
-    unsigned int i;
-    unsigned int len;
-    bool wildcard = false;
-    bool matched = false;
-
-    len = strlen(name);
-    if (len > 0 && name[len - 1] == '*') {
-        wildcard = true;
-        len -= 1;
-    }
-    for (i = 0; i < NR_TRACE_EVENTS; i++) {
-        if (wildcard) {
-            if (!strncmp(trace_list[i].tp_name, name, len)) {
-                trace_list[i].state = state;
-                matched = true;
-            }
-            continue;
-        }
-        if (!strcmp(trace_list[i].tp_name, name)) {
-            trace_list[i].state = state;
-            return true;
-        }
-    }
-    return matched;
+    ev->dstate = state;
 }
 
 bool trace_backend_init(const char *events, const char *file)