trace: Multi-backend tracing
PATCH 1/1 trace: Multi-backend tracing
Adds support to compile QEMU with multiple tracing backends at the same time.
For example, you can compile QEMU with:
$ ./configure --enable-trace-backend=ftrace,dtrace
Where 'ftrace' can be handy for having an in-flight record of events, and 'dtrace' can be later used to extract more information from the system.
This patch allows having both available without recompiling QEMU.
Signed-off-by Lluís Vilanova <[email protected]>
--- Makefile | 2 + configure | 42 +++++++++++++--------------- qemu-io.c | 2 + scripts/tracetool.py | 39 +++++++++++++------------- scripts/tracetool/__init__.py | 22 +++++++-------- scripts/tracetool/backend/__init__.py | 15 +++++----- trace/Makefile.objs | 16 +++++------ trace/control-internal.h | 4 +-- trace/control.c | 49 ++++++++++++++++++++++++++++++++- trace/control.h | 27 +++--------------- trace/default.c | 40 --------------------------- trace/ftrace.c | 25 +---------------- trace/ftrace.h | 5 +++ trace/simple.c | 19 +------------ trace/simple.h | 1 + trace/stderr.c | 30 -------------------- vl.c | 4 +-- 17 files changed, 130 insertions(+), 212 deletions(-) delete mode 100644 trace/default.c delete mode 100644 trace/stderr.c
Makefile +1 -1
@@ -52,7 +52,7 @@ GENERATED_HEADERS += trace/generated-events.h GENERATED_SOURCES += trace/generated-events.c GENERATED_HEADERS += trace/generated-tracers.h -ifeq ($(TRACE_BACKEND),dtrace) +ifeq ($(findstring dtrace,$(TRACE_BACKEND)),dtrace) GENERATED_HEADERS += trace/generated-tracers-dtrace.h endif GENERATED_SOURCES += trace/generated-tracers.c
configure +19 -23
111 lines not on this page · raw message
qemu-io.c +1 -1
@@ -417,7 +417,7 @@ int main(int argc, char **argv) }break ;case 'T' : -if (!trace_backend_init (optarg,NULL )) { +if (!trace_init_backends (optarg,NULL )) {exit (1 );/* error message will have been printed */ }break ;
scripts/tracetool.py +20 -19
@@ -6,7 +6,7 @@ Command-line wrapper for the tracetool machinery. """ __author__ = "Lluís Vilanova <[email protected]>" -__copyright__ = "Copyright 2012, Lluís Vilanova <[email protected]>" +__copyright__ = "Copyright 2012-2014, Lluís Vilanova <[email protected]>" __license__ = "GPL version 2 or (at your option) any later version" __maintainer__ = "Stefan Hajnoczi"
@@ -65,16 +65,17 @@ def main(args): global _SCRIPT _SCRIPT = args[0] - long_opts = [ "backend=", "format=", "help", "list-backends", "check-backend" ] - long_opts += [ "binary=", "target-type=", "target-name=", "probe-prefix=" ] + long_opts = ["backends=", "format=", "help", "list-backends", + "check-backends"] + long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="] try: opts, args = getopt.getopt(args[1:], "", long_opts) except getopt.GetoptError, err: error_opt(str(err)) - check_backend = False - arg_backend = "" + check_backends = False + arg_backends = [] arg_format = "" binary = None target_type = None
@@ -84,8 +85,8 @@ def main(args): if opt == "--help": error_opt() - elif opt == "--backend": - arg_backend = arg + elif opt == "--backends": + arg_backends = arg.split(",") elif opt == "--format": arg_format = arg
@@ -93,8 +94,8 @@ def main(args): 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 + elif opt == "--check-backends": + check_backends = True elif opt == "--binary": binary = arg
@@ -108,14 +109,14 @@ def main(args): else: error_opt("unhandled option: %s" % opt) - if arg_backend is None: - error_opt("backend not set") + if len(arg_backends) == 0: + error_opt("no backends specified") - if check_backend: - if tracetool.backend.exists(arg_backend): - sys.exit(0) - else: - sys.exit(1) + if check_backends: + for backend in arg_backends: + if not tracetool.backend.exists(backend): + sys.exit(1) + sys.exit(0) if arg_format == "stap": if binary is None:
@@ -126,11 +127,11 @@ def main(args): error_opt("--target-name is required for SystemTAP tapset generator") if probe_prefix is None: - probe_prefix = ".".join([ "qemu", target_type, target_name ]) + probe_prefix = ".".join(["qemu", target_type, target_name]) try: - tracetool.generate(sys.stdin, arg_format, arg_backend, - binary = binary, probe_prefix = probe_prefix) + tracetool.generate(sys.stdin, arg_format, arg_backends, + binary=binary, probe_prefix=probe_prefix) except tracetool.TracetoolError, e: error_opt(str(e))
scripts/tracetool/__init__.py +11 -11
@@ -228,9 +228,9 @@ def try_import(mod_name, attr_name=None, attr_default=None): return False, None -def generate(fevents, format, backend, +def generate(fevents, format, backends, binary=None, probe_prefix=None): - """Generate the output for the given (format, backend) pair. + """Generate the output for the given (format, backends) pair. Parameters ----------
@@ -238,8 +238,8 @@ def generate(fevents, format, backend, Event description file. format : str Output format name. - backend : str - Output backend name. + backends : list + Output backend names. binary : str or None See tracetool.backend.dtrace.BINARY. probe_prefix : str or None
@@ -255,13 +255,13 @@ def generate(fevents, format, backend, raise TracetoolError("unknown format: %s" % format) format = format.replace("-", "_") - backend = str(backend) - if len(backend) is 0: - raise TracetoolError("backend not set") - if not tracetool.backend.exists(backend): - raise TracetoolError("unknown backend: %s" % backend) - backend = backend.replace("-", "_") - backend = tracetool.backend.Wrapper(backend, format) + if len(backends) is 0: + raise TracetoolError("no backends specified") + for backend in backends: + if not tracetool.backend.exists(backend): + raise TracetoolError("unknown backend: %s" % backend) + backends = [backend.replace("-", "_") for backend in backends] + backend = tracetool.backend.Wrapper(backends, format) import tracetool.backend.dtrace tracetool.backend.dtrace.BINARY = binary
scripts/tracetool/backend/__init__.py +8 -7
@@ -99,17 +99,18 @@ def exists(name): class Wrapper: - def __init__(self, backend, format): - self._backend = backend.replace("-", "_") + def __init__(self, backends, format): + self._backends = [backend.replace("-", "_") for backend in backends] self._format = format.replace("-", "_") - assert exists(self._backend) + assert all(exists(backend) for backend in self._backends) assert tracetool.format.exists(self._format) def _run_function(self, name, *args, **kwargs): - func = tracetool.try_import("tracetool.backend." + self._backend, - name % self._format, None)[1] - if func is not None: - func(*args, **kwargs) + for backend in self._backends: + func = tracetool.try_import("tracetool.backend." + backend, + name % self._format, None)[1] + if func is not None: + func(*args, **kwargs) def generate_begin(self, events): self._run_function("generate_%s_begin", events)
trace/Makefile.objs +7 -9
@@ -7,7 +7,7 @@ $(obj)/generated-events.h: $(obj)/generated-events.h-timestamp $(obj)/generated-events.h-timestamp: $(SRC_PATH)/trace-events $(call quiet-command,$(TRACETOOL) \ --format=events-h \ - --backend=$(TRACE_BACKEND) \ + --backend=$(TRACE_BACKENDS) \ < $< > $@," GEN $(patsubst %-timestamp,%,$@)") @cmp -s $@ $(patsubst %-timestamp,%,$@) || cp $@ $(patsubst %-timestamp,%,$@)
@@ -15,7 +15,7 @@ $(obj)/generated-events.c: $(obj)/generated-events.c-timestamp $(BUILD_DIR)/conf $(obj)/generated-events.c-timestamp: $(SRC_PATH)/trace-events $(call quiet-command,$(TRACETOOL) \ --format=events-c \ - --backend=$(TRACE_BACKEND) \ + --backend=$(TRACE_BACKENDS) \ < $< > $@," GEN $(patsubst %-timestamp,%,$@)") @cmp -s $@ $(patsubst %-timestamp,%,$@) || cp $@ $(patsubst %-timestamp,%,$@)
@@ -30,19 +30,19 @@ $(obj)/generated-tracers.h: $(obj)/generated-tracers.h-timestamp $(obj)/generated-tracers.h-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak $(call quiet-command,$(TRACETOOL) \ --format=h \ - --backend=$(TRACE_BACKEND) \ + --backend=$(TRACE_BACKENDS) \ < $< > $@," GEN $(patsubst %-timestamp,%,$@)") ###################################################################### # Auto-generated tracing routines (non-DTrace) -ifneq ($(TRACE_BACKEND),dtrace) +ifneq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace) $(obj)/generated-tracers.c: $(obj)/generated-tracers.c-timestamp @cmp -s $< $@ || cp $< $@ $(obj)/generated-tracers.c-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak $(call quiet-command,$(TRACETOOL) \ --format=c \ - --backend=$(TRACE_BACKEND) \ + --backend=$(TRACE_BACKENDS) \ < $< > $@," GEN $(patsubst %-timestamp,%,$@)") $(obj)/generated-tracers.o: $(obj)/generated-tracers.c $(obj)/generated-tracers.h
@@ -55,12 +55,12 @@ endif # Normal practice is to name DTrace probe file with a '.d' extension # but that gets picked up by QEMU's Makefile as an external dependency # rule file. So we use '.dtrace' instead -ifeq ($(TRACE_BACKEND),dtrace) +ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace) $(obj)/generated-tracers.dtrace: $(obj)/generated-tracers.dtrace-timestamp $(obj)/generated-tracers.dtrace-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak $(call quiet-command,$(TRACETOOL) \ --format=d \ - --backend=$(TRACE_BACKEND) \ + --backend=$(TRACE_BACKENDS) \ < $< > $@," GEN $(patsubst %-timestamp,%,$@)") @cmp -s $@ $(patsubst %-timestamp,%,$@) || cp $@ $(patsubst %-timestamp,%,$@)
@@ -73,9 +73,7 @@ endif ###################################################################### # Backend code -util-obj-$(CONFIG_TRACE_DEFAULT) += default.o util-obj-$(CONFIG_TRACE_SIMPLE) += simple.o -util-obj-$(CONFIG_TRACE_STDERR) += stderr.o util-obj-$(CONFIG_TRACE_FTRACE) += ftrace.o util-obj-y += control.o util-obj-y += generated-tracers.o
trace/control-internal.h +2 -2
@@ -1,7 +1,7 @@/* * Interface for configuring and controlling the state of tracing events. * -* Copyright (C) 2011-2012 Lluís Vilanova <[email protected]> +* Copyright (C) 2011-2014 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.
@@ -61,7 +61,7 @@ static inline void trace_event_set_state_dynamic(TraceEvent *ev, bool state) {assert (ev !=NULL );assert (trace_event_get_state_static (ev)); -return trace_event_set_state_dynamic_backend (ev, state); + ev->dstate = state; }#endif /* TRACE__CONTROL_INTERNAL_H */
trace/control.c +47 -2
70 lines not on this page · raw message
trace/control.h +4 -23
@@ -1,7 +1,7 @@/* * Interface for configuring and controlling the state of tracing events. * -* Copyright (C) 2011-2012 Lluís Vilanova <[email protected]> +* Copyright (C) 2011-2014 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.
@@ -146,26 +146,17 @@ static bool trace_event_get_state_dynamic(TraceEvent *ev);*/ static void trace_event_set_state_dynamic (TraceEvent *ev,bool state); -/** -* trace_event_set_state_dynamic_backend: -* -* Warning: This function must be implemented by each tracing backend. -*/ -void trace_event_set_state_dynamic_backend (TraceEvent *ev,bool state); -/** * trace_print_events: * * Print the state of all events. -* -* Warning: This function must be implemented by each tracing backend. */ void trace_print_events (FILE *stream,fprintf_function stream_printf);/** -* trace_backend_init: +* trace_init_backends: * @events: Name of file with events to be enabled at startup; may be NULL. * Corresponds to commandline option "-trace events=...". * @file: Name of trace output file; may be NULL.
@@ -173,19 +164,9 @@ void trace_print_events(FILE *stream, fprintf_function stream_printf);* * Initialize the tracing backend. * -* Warning: This function must be implemented by each tracing backend. -* -* Returns: Whether the backend could be successfully initialized. -*/ -bool trace_backend_init (const char *events,const char *file); - -/** -* trace_backend_init_events: -* @fname: Name of file with events to enable; may be NULL. -* -* Generic function to initialize the state of events. +* Returns: Whether the backends could be successfully initialized. */ -void trace_backend_init_events (const char *fname); +bool trace_init_backends (const char *events,const char *file);#include "trace/control-internal.h"
trace/default.c deleted +0 -40
@@ -1,40 +0,0 @@ -/* -* Default implementation for backend initialization from commandline. -* -* 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. -*/ - -#include "trace/control.h" - - -void trace_print_events (FILE *stream,fprintf_function stream_printf) -{ -fprintf (stderr,"warning: " -"cannot print the trace events with the current backend\n" ); -stream_printf (stream,"error: " -"operation not supported with the current backend\n" ); -} - -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" ); -} - -bool trace_backend_init (const char *events,const char *file) -{ -if (events) { -fprintf (stderr,"error: -trace events=...: " -"option not supported by the selected tracing backend\n" ); -return false; - } -if (file) { -fprintf (stderr,"error: -trace file=...: " -"option not supported by the selected tracing backend\n" ); -return false; - } -return true; -}
trace/ftrace.c +1 -24
@@ -42,35 +42,13 @@ static int find_debugfs(char *debugfs)return 1 ; } -void trace_print_events (FILE *stream,fprintf_function stream_printf) -{ -TraceEventID 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_event_get_name (ev), i,trace_event_get_state_dynamic (ev)); - } -} - -void trace_event_set_state_dynamic_backend (TraceEvent *ev,bool state) -{ - ev->dstate = state; -} - -bool trace_backend_init (const char *events,const char *file) +bool ftrace_init (void ) {char debugfs[PATH_MAX ];char path[PATH_MAX ];int debugfs_found;int trace_fd =-1 ; -if (file) { -fprintf (stderr,"error: -trace file=...: " -"option not supported by the selected tracing backend\n" ); -return false; - } - debugfs_found =find_debugfs (debugfs);if (debugfs_found) {snprintf (path,PATH_MAX ,"%s/tracing/tracing_on" , debugfs);
@@ -97,6 +75,5 @@ bool trace_backend_init(const char *events, const char *file)return false; } -trace_backend_init_events (events);return true; }
trace/ftrace.h +5 -0
@@ -1,10 +1,15 @@#ifndef TRACE_FTRACE_H #define TRACE_FTRACE_H +#include <stdint.h> + +#define MAX_TRACE_STRLEN 512#define _STR (x) #x#define STR (x) _STR(x)extern int trace_marker_fd; +bool ftrace_init (void ); +#endif /* ! TRACE_FTRACE_H */
trace/simple.c +1 -18
@@ -366,22 +366,6 @@ void st_flush_trace_buffer(void)flush_trace_file (true); } -void trace_print_events (FILE *stream,fprintf_function stream_printf) -{ -unsigned int 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_event_get_name (ev), i,trace_event_get_state_dynamic (ev)); - } -} - -void trace_event_set_state_dynamic_backend (TraceEvent *ev,bool state) -{ - ev->dstate = state; -} -/* Helper function to create a thread with signals blocked. Use glib's * portable threads since QEMU abstractions cannot be used due to reentrancy in * the tracer. Also note the signal masking on POSIX hosts so that the thread
@@ -410,7 +394,7 @@ static GThread *trace_thread_create(GThreadFunc fn)return thread; } -bool trace_backend_init (const char *events,const char *file) +bool st_init (const char *file) {GThread *thread;
@@ -435,7 +419,6 @@ bool trace_backend_init(const char *events, const char *file) }atexit (st_flush_trace_buffer); -trace_backend_init_events (events);st_set_trace_file (file);return true; }
trace/simple.h +1 -0
@@ -21,6 +21,7 @@void st_print_trace_file_status (FILE *stream,fprintf_function stream_printf);void st_set_trace_file_enabled (bool enable);bool st_set_trace_file (const char *file); +bool st_init (const char *file);void st_flush_trace_buffer (void ); typedef struct {
trace/stderr.c deleted +0 -30
@@ -1,30 +0,0 @@ -#include "trace.h" -#include "trace/control.h" - - -void trace_print_events (FILE *stream,fprintf_function stream_printf) -{ -TraceEventID 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_event_get_name (ev), i,trace_event_get_state_dynamic (ev)); - } -} - -void trace_event_set_state_dynamic_backend (TraceEvent *ev,bool state) -{ - ev->dstate = state; -} - -bool trace_backend_init (const char *events,const char *file) -{ -if (file) { -fprintf (stderr,"error: -trace file=...: " -"option not supported by the selected tracing backend\n" ); -return false; - } -trace_backend_init_events (events); -return true; -}
vl.c +2 -2
@@ -3880,7 +3880,7 @@ int main(int argc, char **argv, char **envp) }if (!is_daemonized ()) { -if (!trace_backend_init (trace_events, trace_file)) { +if (!trace_init_backends (trace_events, trace_file)) {exit (1 ); } }
@@ -4382,7 +4382,7 @@ int main(int argc, char **argv, char **envp)os_setup_post ();if (is_daemonized ()) { -if (!trace_backend_init (trace_events, trace_file)) { +if (!trace_init_backends (trace_events, trace_file)) {exit (1 ); } }
This patch depends on the "Tracetool cleanup" series. The idea is based on Kazuya's "trace backend: introduce multi tracing backend" series.
Thanks,
Lluis