trace-instrument: Let the user wrap/override specific event tracing routines
cover trace-instrument: Let the user wrap/override specific event tracing routines
XXX: Must be applied on top of the "backdoor" patch series and the patch "trace:
Provide a per-event status define for conditional compilation", but is independent otherwise.
Adds the "instrument" event property. When specified, this event property lets the user provide her own implementation for that specific tracing event.
Still, in case the user only wants to wrap around the tracing event, the original tracing implementation is accessible through function 'trace_<eventname>_backend', instead of the original 'trace_<eventname>' (which now the user has to provide).
The user-provided tracing functions are expected to be in the static library "libtrace-instrument.a", identified by the "--with-trace-instrument" configuration parameter and compiled by each of QEMU's target/frontend pairs.
See the documentation added in the second patch for more information.
---
Lluís Vilanova (4):
trace: [tracetool] Add 'get_api_name' to construct the name of tracing routines
trace-instrument: Add documentation
trace-instrument: Add support for user-provided code on a per-event basis
trace-instrument: Add a per-vCPU opaque pointer for the instrumentation backend
Makefile.objs | 23 +++++++ Makefile.target | 2 - configure | 38 ++++++++++++ cpu-defs.h | 6 +- docs/instrumentation.txt | 142 ++++++++++++++++++++++++++++++++++++++++++++++ docs/tracing.txt | 9 +++ scripts/simpletrace.py | 2 - scripts/tracetool | 57 +++++++++++++++--- 8 files changed, 261 insertions(+), 18 deletions(-) create mode 100644 docs/instrumentation.txt
RFC PATCH 1/4 trace: [tracetool] Add 'get_api_name' to construct the name of tracing routines
All backends now use 'get_api_name' to build the name of the routines that are used by QEMU code when invoking trace points. This is built based on the values of 'get_api_name_fmt' and 'get_api_name_fmt_default'.
The old 'get_name' is still available to refer to the name of an event.
This allows future tracetool options to change the naming of the tracing API on a per-event basis without having to modify backend-specific code.
--- scripts/tracetool | 41 +++++++++++++++++++++++++++++------------ 1 files changed, 29 insertions(+), 12 deletions(-)
scripts/tracetool +29 -12
@@ -57,6 +57,20 @@ get_name() echo "${name##* }" } +get_api_name_fmt_default="trace_%s" + +# Get the tracepoint name of a trace event (the name of the function QEMU uses) +# The result is 'printf "$fmt" $name', where 'fmt' is 'get_api_name_fmt' if set +# or 'get_api_name_fmt_default' otherwise +get_api_name() +{ + local name fmt + name=$(get_name "$1") + fmt="$get_api_name_fmt" + [ -n "$fmt" ] || fmt=$get_api_name_fmt_default + printf "$fmt" "$name" +} + # Get the given property of a trace event # 1: trace-events line # 2: property name
@@ -133,13 +147,13 @@ linetoh_begin_nop() linetoh_nop() { - local name args - name=$(get_name "$1") + local api args + api=$(get_api_name "$1") args=$(get_args "$1") # Define an empty function for the trace event cat <<EOF -static inline void trace_$name($args) +static inline void $api($args) { } EOF
@@ -185,8 +199,8 @@ cast_args_to_uint64_t() linetoh_simple() { - local name args argc trace_args - name=$(get_name "$1") + local api args argc trace_args + api=$(get_api_name "$1") args=$(get_args "$1") argc=$(get_argc "$1")
@@ -197,7 +211,7 @@ linetoh_simple() fi cat <<EOF -static inline void trace_$name($args) +static inline void $api($args) { trace$argc($trace_args); }
@@ -257,8 +271,9 @@ EOF linetoh_stderr() { - local name args argnames argc fmt + local name api args argnames argc fmt name=$(get_name "$1") + api=$(get_api_name "$1") args=$(get_args "$1") argnames=$(get_argnames "$1" ",") argc=$(get_argc "$1")
@@ -269,7 +284,7 @@ linetoh_stderr() fi cat <<EOF -static inline void trace_$name($args) +static inline void $api($args) { if (trace_list[$stderr_event_num].state != 0) { fprintf(stderr, "$name " $fmt "\n" $argnames);
@@ -333,14 +348,15 @@ linetoh_begin_ust() linetoh_ust() { - local name args argnames + local name api args argnames name=$(get_name "$1") + api=$(get_api_name "$1") args=$(get_args "$1") argnames=$(get_argnames "$1", ",") cat <<EOF DECLARE_TRACE(ust_$name, TP_PROTO($args), TP_ARGS($argnames)); -#define trace_$name trace_ust_$name +#define $api trace_ust_$name EOF }
@@ -405,8 +421,9 @@ EOF linetoh_dtrace() { - local name args argnames nameupper + local name api args argnames nameupper name=$(get_name "$1") + api=$(get_api_name "$1") args=$(get_args "$1") argnames=$(get_argnames "$1", ",")
@@ -414,7 +431,7 @@ linetoh_dtrace() # Define an empty function for the trace event cat <<EOF -static inline void trace_$name($args) { +static inline void $api($args) { if (QEMU_${nameupper}_ENABLED()) { QEMU_${nameupper}($argnames); }
RFC PATCH 2/4 trace-instrument: Add documentation
In reply to Lluís Vilanova--- docs/instrumentation.txt | 142 ++++++++++++++++++++++++++++++++++++++++++++++ docs/tracing.txt | 9 +++ 2 files changed, 151 insertions(+), 0 deletions(-) create mode 100644 docs/instrumentation.txt
docs/instrumentation.txt new file +142 -0
@@ -0,0 +1,142 @@ += Trace instrumentation = + +== Introduction == + +This document describes how the events provided by the tracing infrastructure +described in the document "tracing.txt" can be extended with user-defined code +that is compiled into QEMU. + +The "instrument" property provides means to statically override and/or wrap the +backend-specific tracing code on a per-event basis when compiling QEMU +(regardless of the tracing backend). + +As opposed to using tracing backends that can dynamically load user-defined code +into the tracing events (e.g., "dtrace"), trace instrumentation is performed at +compile time and thus provides means for the tracing hooks to directly interact +with QEMU internals. + +Static trace instrumentation trades the ease and flexibility of existing tracing +backends with the ability to directly interact with the internal routines of +QEMU, as well as the ability to embed highly performant and ad-hoc analyses +based on QEMU's tracing events directly into QEMU itself. + + +== Quickstart == + +1. Declare which tracing events to instrument: + + sed -i -e "s/qemu_vmalloc(/instrument qemu_vmalloc(/g" trace-events + +2. Create the trace instrumentation header ("trace-instrument.h") with the + headers for the instrumented tracing events: + + cat > /tmp/my-instrument/trace-instrument.h <<EOF + void qemu_vmalloc(size_t size, void *ptr); + EOF + +3. Create the "Makefile" to build the trace intrumentation library: + + cat > /tmp/my-intrument/Makefile <<EOF + include $(BUILD_DIR)/config-host.mak + include $(BUILD_DIR)/$(TARGET_DIR)../config-target.mak + include $(SRC_PATH)/rules.mak + + vpath %.c /tmp/my-instrument + + libtrace-instrument.a: instrument.o + + # Include automatically generated dependency files + -include $(wildcard *.d) + EOF + +4. Create the necessary source code files to implement the event instrumentation: + + cat > /tmp/my-instrument/instrument.c <<EOF + #include "trace.h" + + void trace_qemu_vmalloc(size_t size, void *ptr) + { + /* ... */ + } + EOF + +5. Build QEMU with trace intrumentation enabled: + + ./configure --with-trace-instrument=/tmp/my-instrument + make + + +== Compilation notes == + +The instrumentation library is compiled during the compilation of the +target-dependant code. This means that the instrumentation code is able to use +any target-specific code and access target-specific vCPU state. This includes +access to a per-vCPU opaque pointer ("instrument") that is reserved for the use +of the instrumentation code. + +In addition, the path to the static trace instrumentation library is added into +the list of directories to search for includes, so that any user-provided +headers can be included from the instrumentation headers, as well as any other +header from QEMU itself can be included in the intrumentation code. + + +== User-provided headers == + +The user must provide a set of headers in the directory identified by the +'--with-trace-instrument' configure option. These headers establish how and what +to do when a tracing routine for an instrumented event is invoked. + +=== "trace-instrument.h" === + +This file is included from the "trace.h" file whenever instrumentation is enabled. + +Usually, this file will only contain a declaration of the tracing routines for +the instrumented events, just as the "trace.h" would do if an event was not +instrumented. + +Still, if some part of the instrumentation is performance-critical, the header +could provide an inlined implementation of the fast path in the header, and +implement the slow path in the instrumentation library itself: + + void trace_qemu_vmalloc_slow(size_t size, void *ptr); + + static inline void trace_qemu_vmalloc(size_t size, void *ptr) + { + if (/* ... performance-critical condition ... */) { + /* ... performance-critical code ... */ + } else { + trace_qemu_vmalloc_slow(size, ptr); + } + } + + +== Mixing existing tracing backends and instrumentation == + +When enabling static trace instrumentation, you can still use the currently +selected tracing backend by invoking the tracing function with the '_backend' +suffix. + +Note that some of the tracing backends provide an interface for programmatically +controlling the tracing state of each event. Thus you can provide code to +control when the tracing must take place: + + void trace_qemu_vmalloc(size_t size, void *ptr) + { + /* only trace one malloc every 10 */ + /* note that this code is not thread-safe */ + static int i = 0; + static bool state = true; + if (i++ % 10 == 0) { + trace_event_set_state("qemu_vmalloc", state); + state = !state; + } + + /* call the original backend-specific tracing routine */ + trace_qemu_vmalloc_backend(size, ptr); + } + + +== Performance notes == + +The "trace-instrument.h" header can be used to inline the implementation of the +tracing routines for those events that have the "instrument" property.
docs/tracing.txt +9 -0
@@ -268,3 +268,12 @@ guard such computations and avoid its compilation when the event is disabled: } return ptr; } + +=== "instrument" === + +When compiling QEMU with trace instrumentation enabled, the "instrument" +property will force the user to provide an implementation for that trace +event. This implementation can override and/or wrap the backend-specific tracing +code at compile time (regardless of the tracing backend). + +See the document "instrumentation.txt" for more information.
RFC PATCH 3/4 trace-instrument: Add support for user-provided code on a per-event basis
In reply to Lluís VilanovaAdds support for the 'instrument' propery into the "trace-events" file.
Events with this property will generate tracing routines named '${api_name}_backend' (instead of '${api_name}'), and expect the user to provide its own implementation for the '${api_name}' routines.
--- Makefile.objs | 23 +++++++++++++++++++++-- Makefile.target | 2 +- configure | 38 ++++++++++++++++++++++++++++++++++++++ scripts/simpletrace.py | 2 +- scripts/tracetool | 16 ++++++++++++++++ 5 files changed, 77 insertions(+), 4 deletions(-)
Makefile.objs +21 -2
@@ -346,12 +346,12 @@ else trace.h: trace.h-timestamp endif trace.h-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -h < $< > $@," GEN trace.h") + $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool $(TRACETOOL_EXTRA) --$(TRACE_BACKEND) -h < $< > $@," GEN trace.h") @cmp -s $@ trace.h || cp $@ trace.h trace.c: trace.c-timestamp trace.c-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak - $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -c < $< > $@," GEN trace.c") + $(call quiet-command,sh $(SRC_PATH)/scripts/tracetool $(TRACETOOL_EXTRA) --$(TRACE_BACKEND) -c < $< > $@," GEN trace.c") @cmp -s $@ trace.c || cp $@ trace.c trace.o: trace.c $(GENERATED_HEADERS)
@@ -399,6 +399,25 @@ trace-obj-y += $(addprefix trace/, $(trace-nested-y)) $(trace-obj-y): $(GENERATED_HEADERS) ###################################################################### +# trace instrument + +ifdef CONFIG_TRACE_INSTRUMENT +LIBTRACE_INSTRUMENT = libtrace-instrument/libtrace-instrument.a + +.PHONY: force +force: + +$(LIBTRACE_INSTRUMENT): QEMU_CFLAGS += -I$(SRC_PATH)/fpu +$(LIBTRACE_INSTRUMENT): QEMU_CFLAGS += -I$(SRC_PATH)/tcg/$(TARGET_BASE_ARCH) +$(LIBTRACE_INSTRUMENT): VPATH = $(TRACE_INSTRUMENT_PATH) +$(LIBTRACE_INSTRUMENT): $(dir $(LIBTRACE_INSTRUMENT))/Makefile force + $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $(dir $@) \ + QEMU_CFLAGS="$(QEMU_CFLAGS)" \ + TARGET_DIR=$(TARGET_DIR)$(dir $@)/ VPATH=$(VPATH) \ + SRC_PATH=$(SRC_PATH) V="$(V)" $(notdir $@)) +endif + +###################################################################### # backdoor backdoor-nested-$(CONFIG_USER_ONLY) += user.o
Makefile.target +1 -1
@@ -415,7 +415,7 @@ endif # CONFIG_LINUX_USER obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o -$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR) +$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR) $(LIBTRACE_INSTRUMENT) $(call LINK,$^)
configure +38 -0
@@ -176,6 +176,7 @@ pie="" zero_malloc="" trace_backend="nop" trace_file="trace" +trace_instrument="" backdoor="" spice="" rbd=""
@@ -550,6 +551,8 @@ for opt do ;; --with-trace-file=*) trace_file="$optarg" ;; + --with-trace-instrument=*) trace_instrument="$optarg" + ;; --with-backdoor=*) backdoor="$optarg" ;; --enable-gprof) gprof="yes"
@@ -1065,6 +1068,8 @@ echo " --enable-trace-backend=B Set trace backend" echo " Available backends:" $("$source_path"/scripts/tracetool --list-backends) echo " --with-trace-file=NAME Full PATH,NAME of file to store traces" echo " Default:trace-<pid>" +echo " --with-trace-instrument=PATH" +echo " Directory to build user-provided trace event instrumentation library" echo " --with-backdoor=PATH Directory to build user-provided backdoor library" echo " --disable-spice disable spice" echo " --enable-spice enable spice"
@@ -2699,6 +2704,26 @@ if compile_prog "" "" ; then fi ########################################## +# check for a valid directory for trace instrumentation +if test -n "$trace_instrument"; then + if test ! -f "$trace_instrument/Makefile"; then + echo + echo "Error: cannot make into '$trace_instrument'" + echo "Please choose a directory where I can run 'make'" + echo + exit 1 + fi + if test ! -f "$trace_instrument/trace-instrument.h"; then + echo + echo "Error: directory '$trace_instrument' does not contain a \"trace-instrument.h\" file" + echo + exit 1 + fi + trace_instrument=`readlink -f "$trace_instrument"` +fi + + +########################################## # End of CC checks # After here, no more $cc or $ld runs
@@ -2856,6 +2881,9 @@ echo "uuid support $uuid" echo "vhost-net support $vhost_net" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-<pid>" +if test -n "$trace_instrument"; then +echo "Trace instrument $trace_instrument" +fi if test -n "$backdoor"; then echo "Backdoor lib dir $backdoor" fi
@@ -3251,6 +3279,12 @@ echo "CONFIG_TRACE_FILE=$trace_file" >> $config_host_mak if test "$trace_default" = "yes"; then echo "CONFIG_TRACE_DEFAULT=y" >> $config_host_mak fi +if test -n "$trace_instrument"; then + echo "CONFIG_TRACE_INSTRUMENT=y" >> $config_host_mak + echo "TRACE_INSTRUMENT_PATH=\"$trace_instrument\"" >> $config_host_mak + echo "TRACETOOL_EXTRA=--instrument" >> $config_host_mak + QEMU_CFLAGS="-I\"$trace_instrument\" $QEMU_CFLAGS" +fi # backdoor channel if test -n "$backdoor"; then
@@ -3370,6 +3404,10 @@ if test "$target" = "arm-linux-user" -o "$target" = "armeb-linux-user" -o "$targ mkdir -p $target_dir/nwfpe fi symlink $source_path/Makefile.target $target_dir/Makefile +if test -n "$trace_instrument"; then + mkdir -p $target_dir/libtrace-instrument + symlink $trace_instrument/Makefile $target_dir/libtrace-instrument/Makefile +fi if test -n "$backdoor"; then mkdir -p $target_dir/libbackdoor symlink $backdoor/Makefile $target_dir/libbackdoor/Makefile
scripts/simpletrace.py +1 -1
@@ -20,7 +20,7 @@ dropped_event_id = 0xfffffffffffffffe trace_fmt = '=QQQQQQQQ' trace_len = struct.calcsize(trace_fmt) -event_re = re.compile(r'(disable\s+)?([a-zA-Z0-9_]+)\(([^)]*)\).*') +event_re = re.compile(r'(disable\s+|instrument\s+)*([a-zA-Z0-9_]+)\(([^)]*)\).*') def parse_events(fobj): """Parse a trace-events file into {event_num: (name, arg1, ...)}."""
scripts/tracetool +16 -0
@@ -35,6 +35,7 @@ Options: --target-type [type] QEMU emulator target type ('system' or 'user') --probe-prefix [prefix] Prefix for dtrace probe names (default: qemu-\$targettype-\$targetarch) + --instrument Enable static trace point instrumentation EOF exit 1
@@ -553,8 +554,16 @@ convert() "lineto$1_nop" "$str" enabled=0 else + if has_property "$str" "instrument"; then + if [ "$instrument" != "1" ]; then + echo "Event '"$(get_name "$str")"' has the 'instrument' property but instrumentation is not enabled" >&2 + exit 1 + fi + get_api_name_fmt="${get_api_name_fmt_default}_backend" + fi "$process_line" "$str" enabled=1 + unset get_api_name_fmt fi if [ "$1" = "h" ]; then name=$(get_name "$str")
@@ -578,6 +587,11 @@ tracetoh() #include "qemu-common.h" EOF convert h + + if [ "$instrument" = "1" ]; then + echo '#include "trace-instrument.h"' + fi + echo "#endif /* TRACE_H */" }
@@ -629,6 +643,7 @@ binary= targettype= targetarch= probeprefix= +instrument= until [ -z "$1" ]
@@ -640,6 +655,7 @@ do "--target-arch") shift ; targetarch="$1" ;; "--target-type") shift ; targettype="$1" ;; "--probe-prefix") shift ; probeprefix="$1" ;; + "--instrument") instrument="1" ;; "-h" | "-c" | "-d") output="${1#-}" ;; "--stap") output="${1#--}" ;;
RFC PATCH 4/4 trace-instrument: Add a per-vCPU opaque pointer for the instrumentation backend
In reply to Lluís VilanovaThe 'instrument' opaque pointer can be used by the user-provided trace instrumentation backend to hold arbitrary data.
--- cpu-defs.h | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
cpu-defs.h +4 -2
@@ -218,6 +218,8 @@ typedef struct CPUWatchpoint {struct KVMState *kvm_state; \struct kvm_run *kvm_run; \int kvm_fd; \ -int kvm_vcpu_dirty; - +int kvm_vcpu_dirty; \ + \ +/* opaque pointer to instrumentation data */ \ +void *instrument;#endif