trace: Generic event state description
v6 cover trace: Generic event state description
NOTE: Not sure if compilation of files in trace/ should be moved from
Makefile.objs to trace/Makefile.objs
Provides a generic event state description structure (TraceEvent) and a more detailed event control and query interface.
This is achieved by creating a new "non-public" tracing backend (i.e., not selectable by the user at configure time) that will generate the appropriate event description information.
---
Changes in v6:
* Fixed typos in the documentation of the 'TraceEvent' struct.
Changes in v5:
* Rebase on dbaf26b3 from master.
* Always initialize temporary 'ev' in 'trace_backend_init_events'.
* Make common sanity checks in 'trace_event_set_state_dynamic' and delay backend-specific code to 'trace_event_set_state_dynamic_backend'.
Changes in v4:
* Documentation fixes and (re)formatting.
Changes in v3:
* Add some assertions.
* Remove debugging printf's.
* Improve documentation.
* Make 'trace_event_get_state_static' use run-time information, and leave TRACE_*_ENABLED for compile-time checks.
Changes in v2:
* Minor compilation fixes.
Lluís Vilanova (7):
tracetool: Explicitly identify public backends
trace: Provide a generic tracing event descriptor
trace: Provide a detailed event control interface
trace: [monitor] Use new event control interface
trace: [default] Port to generic event information and new 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 | 21 +++- docs/tracing.txt | 42 +++---- monitor.c | 15 ++- scripts/tracetool.py | 4 - scripts/tracetool/backend/__init__.py | 16 +++ scripts/tracetool/backend/dtrace.py | 3 + scripts/tracetool/backend/events.py | 23 ++++ scripts/tracetool/backend/simple.py | 15 +-- scripts/tracetool/backend/stderr.py | 28 ++--- scripts/tracetool/backend/ust.py | 3 + scripts/tracetool/format/events_c.py | 39 +++++++ scripts/tracetool/format/events_h.py | 50 +++++++++ scripts/tracetool/format/h.py | 9 -- trace/control-internal.h | 67 ++++++++++++ trace/control.c | 92 +++++++++++++++- trace/control.h | 189 ++++++++++++++++++++++++++++++--- trace/default.c | 5 - trace/event-internal.h | 33 ++++++ trace/simple.c | 36 +----- trace/simple.h | 6 - trace/stderr.c | 34 +----- trace/stderr.h | 11 -- 23 files changed, 576 insertions(+), 170 deletions(-) create mode 100644 scripts/tracetool/backend/events.py create mode 100644 scripts/tracetool/format/events_c.py create mode 100644 scripts/tracetool/format/events_h.py 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]>
PATCH v6 1/7 tracetool: Explicitly identify public backends
Public backends are those printed by "--list-backends" and thus considered valid by the configure script.
--- scripts/tracetool.py | 4 ++-- scripts/tracetool/backend/__init__.py | 16 +++++++++++++++- scripts/tracetool/backend/dtrace.py | 3 +++ scripts/tracetool/backend/simple.py | 3 +++ scripts/tracetool/backend/stderr.py | 3 +++ scripts/tracetool/backend/ust.py | 3 +++ 6 files changed, 29 insertions(+), 3 deletions(-)
scripts/tracetool.py +2 -2
@@ -90,8 +90,8 @@ def main(args): arg_format = arg elif opt == "--list-backends": - backends = tracetool.backend.get_list() - out(", ".join([ b for b,_ in backends ])) + public_backends = tracetool.backend.get_list(only_public = True) + out(", ".join([ b for b,_ in public_backends ])) sys.exit(0) elif opt == "--check-backend": check_backend = True
scripts/tracetool/backend/__init__.py +15 -1
@@ -17,6 +17,16 @@ considered its short description. All backends must generate their contents through the 'tracetool.out' routine. +Backend attributes +------------------ + +========= ==================================================================== +Attribute Description +========= ==================================================================== +PUBLIC If exists and is set to 'True', the backend is considered "public". +========= ==================================================================== + + Backend functions -----------------
@@ -42,7 +52,7 @@ import os import tracetool -def get_list(): +def get_list(only_public = False): """Get a list of (name, description) pairs.""" res = [("nop", "Tracing disabled.")] modnames = []
@@ -57,6 +67,10 @@ def get_list(): continue module = module[1] + public = getattr(module, "PUBLIC", False) + if only_public and not public: + continue + doc = module.__doc__ if doc is None: doc = ""
scripts/tracetool/backend/dtrace.py +3 -0
@@ -16,6 +16,9 @@ __email__ = "[email protected]" from tracetool import out +PUBLIC = True + + PROBEPREFIX = None def _probeprefix():
scripts/tracetool/backend/simple.py +3 -0
@@ -16,6 +16,9 @@ __email__ = "[email protected]" from tracetool import out +PUBLIC = True + + def c(events): out('#include "trace.h"', '',
scripts/tracetool/backend/stderr.py +3 -0
@@ -16,6 +16,9 @@ __email__ = "[email protected]" from tracetool import out +PUBLIC = True + + def c(events): out('#include "trace.h"', '',
scripts/tracetool/backend/ust.py +3 -0
@@ -16,6 +16,9 @@ __email__ = "[email protected]" from tracetool import out +PUBLIC = True + + def c(events): out('#include <ust/marker.h>', '#undef mutex_lock',
PATCH v6 2/7 trace: Provide a generic tracing event descriptor
In reply to Lluís VilanovaUses tracetool to generate a backend-independent tracing event description (struct TraceEvent).
The values for such structure are generated with the non-public "events" backend ("events-c" frontend).
The generation of the defines to check if an event is statically enabled is also moved to the "events" backend ("events-h" frontend).
--- Makefile | 5 +++ Makefile.objs | 21 ++++++++++++++ scripts/tracetool/backend/events.py | 23 ++++++++++++++++ scripts/tracetool/format/events_c.py | 39 +++++++++++++++++++++++++++ scripts/tracetool/format/events_h.py | 50 ++++++++++++++++++++++++++++++++++ scripts/tracetool/format/h.py | 9 +----- trace/event-internal.h | 33 ++++++++++++++++++++++ 7 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 scripts/tracetool/backend/events.py create mode 100644 scripts/tracetool/format/events_c.py create mode 100644 scripts/tracetool/format/events_h.py create mode 100644 trace/event-internal.h
Makefile +5 -0
@@ -19,9 +19,14 @@ config-host.mak: endif 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 +20 -1
@@ -134,6 +134,25 @@ libdis-$(CONFIG_LM32_DIS) += lm32-dis.o ###################################################################### # trace +trace-events.h: trace-events.h-timestamp +trace-events.h-timestamp: $(SRC_PATH)/trace-events + $(call quiet-command,$(TRACETOOL) \ + --format=events-h \ + --backend=events \ + < $< > $@," GEN trace-events.h") + @cmp -s $@ trace-events.h || cp $@ trace-events.h + +trace-events.c: trace-events.c-timestamp +trace-events.c-timestamp: $(SRC_PATH)/trace-events + $(call quiet-command,$(TRACETOOL) \ + --format=events-c \ + --backend=events \ + < $< > $@," GEN trace-events.c") + @cmp -s $@ trace-events.c || cp $@ trace-events.c + +trace-obj-y += trace-events.o + + ifeq ($(TRACE_BACKEND),dtrace) TRACE_H_EXTRA_DEPS=trace-dtrace.h endif
@@ -184,7 +203,7 @@ trace/simple.o: trace/simple.c $(GENERATED_HEADERS) trace-obj-$(CONFIG_TRACE_DTRACE) += trace-dtrace.o ifneq ($(TRACE_BACKEND),dtrace) -trace-obj-y = trace.o +trace-obj-y += trace.o endif trace-obj-$(CONFIG_TRACE_DEFAULT) += trace/default.o
scripts/tracetool/backend/events.py new file +23 -0
@@ -0,0 +1,23 @@ +#!/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/format/events_c.py new file +39 -0
@@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Generate .c for event description. +""" + +__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]" + + +from tracetool import out + + +def begin(events): + out('/* This file is autogenerated by tracetool, do not edit. */', + '', + '#include "trace.h"', + '#include "trace-events.h"', + '#include "trace/control.h"', + '', + ) + + out('TraceEvent trace_events[TRACE_EVENT_COUNT] = {') + + for e in 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(), + ) + + out('};', + '', + )
scripts/tracetool/format/events_h.py new file +50 -0
50 lines not on this page · raw message
scripts/tracetool/format/h.py +1 -8
@@ -25,14 +25,7 @@ def begin(events): '#include "qemu-common.h"') def end(events): - for e in events: - if "disable" in e.properties: - enabled = 0 - else: - enabled = 1 - out('#define TRACE_%s_ENABLED %d' % (e.name.upper(), enabled)) - out('', - '#endif /* TRACE_H */') + out('#endif /* TRACE_H */') def nop(events): for e in events:
trace/event-internal.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* 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" + + +/** +* TraceEvent: +* @id: Unique event identifier. +* @name: Event name. +* @sstate: Static tracing state. +* @dstate: Dynamic tracing state. +* +* Opaque generic description of a tracing event. +*/ +typedef struct TraceEvent { +TraceEventID id; +const char * name; +const bool sstate; +bool dstate; +}TraceEvent ; + + +#endif /* TRACE__EVENT_H */
PATCH v6 3/7 trace: Provide a detailed event control interface
In reply to Lluís VilanovaThis interface decouples event obtention from interaction.
Events can be obtained through three different methods:
* identifier
* name
* simple wildcard pattern
--- docs/tracing.txt | 42 ++++------ trace/control-internal.h | 67 ++++++++++++++++ trace/control.c | 92 +++++++++++++++++++++- trace/control.h | 189 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 339 insertions(+), 51 deletions(-) create mode 100644 trace/control-internal.h
docs/tracing.txt +17 -25
@@ -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-dependent). -* 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 ==
@@ -268,3 +256,7 @@ guard such computations and avoid its compilation when the event is disabled: } return ptr; } + +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 +"trace/control.h" for more information).
trace/control-internal.h new file +67 -0
67 lines not on this page · raw message
trace/control.c +85 -7
110 lines not on this page · raw message
trace/control.h +170 -19
211 lines not on this page · raw message
PATCH v6 4/7 trace: [monitor] Use new event control interface
In reply to Lluís Vilanova--- monitor.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
monitor.c +12 -3
@@ -589,10 +589,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); + } } }
PATCH v6 5/7 trace: [default] Port to generic event information and new control interface
In reply to Lluís Vilanova--- trace/default.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
trace/default.c +2 -3
@@ -1,7 +1,7 @@/* * Default implementation for backend initialization from commandline. * -* Copyright (C) 2011 Lluís Vilanova <[email protected]> +* Copyright (C) 2011-2012 Lluís Vilanova <[email protected]> * * This work is licensed under the terms of the GNU GPL, version 2. See * the COPYING file in the top-level directory.
@@ -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_backend (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)
PATCH v6 6/7 trace: [simple] Port to generic event information and new control interface
In reply to Lluís Vilanova--- scripts/tracetool/backend/simple.py | 12 +----------- trace/simple.c | 36 ++++++++--------------------------- trace/simple.h | 6 +----- trace/stderr.h | 11 ----------- 4 files changed, 10 insertions(+), 55 deletions(-) delete mode 100644 trace/stderr.h
scripts/tracetool/backend/simple.py +1 -11
@@ -22,14 +22,7 @@ PUBLIC = True def c(events): out('#include "trace.h"', '', - 'TraceEvent trace_list[] = {') - - for e in events: - out('{.tp_name = "%(name)s", .state=0},', - name = e.name, - ) - - out('};') + ) def h(events): out('#include "trace/simple.h"',
@@ -53,6 +46,3 @@ def h(events): argc = len(e.args), trace_args = simple_args, ) - - out('#define NR_TRACE_EVENTS %d' % len(events)) - out('extern TraceEvent trace_list[NR_TRACE_EVENTS];')
trace/simple.c +8 -28
@@ -156,7 +156,9 @@ static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3,unsigned int idx;uint64_t timestamp; -if (!trace_list[event].state) { +TraceEvent *eventp =trace_event_id (event); +bool _state =trace_event_get_state_dynamic (eventp); +if (!_state) {return ; }
@@ -315,38 +317,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_backend (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
@@ -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 trace0 (TraceEventID event);void trace1 (TraceEventID event,uint64_t x1);
trace/stderr.h deleted +0 -11
@@ -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 */
PATCH v6 7/7 trace: [stderr] Port to generic event information and new control interface
In reply to Lluís Vilanova--- scripts/tracetool/backend/stderr.py | 27 ++++++++------------------- trace/stderr.c | 34 ++++++---------------------------- 2 files changed, 14 insertions(+), 47 deletions(-)
scripts/tracetool/backend/stderr.py +8 -19
@@ -20,40 +20,29 @@ PUBLIC = True def c(events): - out('#include "trace.h"', - '', - 'TraceEvent trace_list[] = {') - - for e in events: - out('{.tp_name = "%(name)s", .state=0},', - name = e.name, - ) - - out('};') + pass def h(events): out('#include <stdio.h>', - '#include "trace/stderr.h"', + '#include "trace/control.h"', '', - 'extern TraceEvent trace_list[];') + ) - for num, e in enumerate(events): + for e in events: argnames = ", ".join(e.args.names()) if len(e.args) > 0: argnames = ", " + argnames out('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 = e.name, args = e.args, - event_num = num, - fmt = e.fmt, + event_id = "TRACE_" + e.name.upper(), + fmt = e.fmt.rstrip("\n"), argnames = argnames, ) - - out('', - '#define NR_TRACE_EVENTS %d' % len(events))
trace/stderr.c +6 -28
@@ -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_backend (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)