Received: from eggs.gnu.org ([208.118.235.92]:33652) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US6vI-0005KN-1r for qemu-devel@nongnu.org; Tue, 16 Apr 2013 10:32:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US6hO-0003Qk-Jf for qemu-devel@nongnu.org; Tue, 16 Apr 2013 10:17:34 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:44825) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US6Gj-0002ga-Tv for qemu-devel@nongnu.org; Tue, 16 Apr 2013 09:49:58 -0400 Received: from gw.ac.upc.edu (gw.ac.upc.es [147.83.30.3]) by roura.ac.upc.es (8.13.8/8.13.8) with ESMTP id r3GDnvIx030754 for ; Tue, 16 Apr 2013 15:49:57 +0200 Received: from localhost (unknown [84.88.51.85]) by gw.ac.upc.edu (Postfix) with ESMTP id E726B6B01C9 for ; Tue, 16 Apr 2013 15:49:56 +0200 (CEST) From: =?utf-8?b?TGx1w61z?= Vilanova Date: Tue, 16 Apr 2013 15:49:56 +0200 Message-Id: <20130416134956.21588.17922.stgit@fimbulvetr.bsc.es> In-Reply-To: <20130416134949.21588.80064.stgit@fimbulvetr.bsc.es> References: <20130416134949.21588.80064.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 01/23] instrument: Add documentation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Signed-off-by: Llu=C3=ADs Vilanova --- docs/instrumentation.txt | 496 ++++++++++++++++++++++++++++++++++++++++= ++++++ docs/tracing.txt | 9 + 2 files changed, 505 insertions(+) create mode 100644 docs/instrumentation.txt diff --git a/docs/instrumentation.txt b/docs/instrumentation.txt new file mode 100644 index 0000000..8b33d76 --- /dev/null +++ b/docs/instrumentation.txt @@ -0,0 +1,496 @@ +=3D Trace instrumentation =3D + +=3D=3D Introduction =3D=3D + +This document describes how the events provided by the tracing infrastru= cture +(described in the document "tracing.txt") can be extended with user-prov= ided +code. + +Instrumentation comes in two (three) flavours; dynamic and static (and n= one at +all). Both provide means for you to associate code to specific tracing e= vents +that have the "instrument" property in the "trace-events" file. + +As opposed to using tracing backends that can dynamically load user-defi= ned code +into the tracing events (e.g., "dtrace"), trace instrumentation provides= a +well-defined API for the user-provided code to interact with QEMU. + +In the case of dynamic instrumentation, you can load and unload a shared= object +(a dynamic library) to establish your instrumentation callbacks. This me= chanism +provides a trade-off between performance and the simplicity of reusing t= he same +QEMU binary for different instrumentation scenarios. + +In the case of static instrumentation, you must provide a static library= that is +compiled directly into QEMU. This mechanism can provide better performan= ce, at +the expense of having to recompile QEMU for each different instrumentati= on +scenario. + + + +=3D=3D Selecting the events to instrument =3D=3D + +You must declare which events to instrument before compiling QEMU. + +This can be done by adding the "instrument" property (and removing the "= disable" +property, if any) in the events listed in the "trace-events" file. + +In order to avoid modifying the QEMU sources, you can simply create a ne= w +trace-events file with your modifications: + + cp /path/to/qemu-source/trace-events /tmp/trace-events + sed -i -e "s/qemu_vmalloc(/instrument qemu_vmalloc(/g" /tmp/trace-ev= ents + +Note: You must remove the "disable" property (if any) if you want to add= the +"instrument" property of an event, as these are mutually exclusive. + + +=3D=3D Pre-defined instrumentation callbacks =3D=3D + +If you are using dynamic auto-instrumentation (see 'QI_CTRL_INSTR_AUTO' = below) +or static instrumentation, your callbacks must follow a specific naming = scheme: +'qi_event_${event}'. + +Two other default callbacks are provided by QEMU: + +* 'qi_event_${event}_nop' + + Does nothing. + + This is the default callback for all instrumentable events when using = dynamic + instrumentation. + +* 'qi_event_${event}_trace' + + Call QEMU's tracing backend. That is, trace the event using whatever t= racing + backend QEMU has been configured with. + + See document "tracing.txt" to learn more about tracing events with QEM= U (e.g., + write events into a file or print them on the screen). + +All these callbacks are declared in the "qemu-instr/events.h" header. + + +=3D=3D Dynamic instrumentation =3D=3D + +Dynamic instrumentation is based on dynamically loadable libraries that = can be +loaded and unloaded from QEMU at any point in time. This also includes a= n API to +dynamically set the per-event tracing instrumentation callback at any po= int in +time. + +Being able to load and unload an instrumentation library provides a comf= ortable +mechanism to test multiple instrumentation approaches without having to +recompile or restart QEMU between instrumentation tests. + +Note: By default, events are set to use the 'qi_event_${event}_nop' call= back. + + +=3D=3D=3D Example =3D=3D=3D + +1. Select the events to instrument in file "trace-events" (see above). + +2. Build QEMU with dynamic trace intrumentation: + + mkdir -p /tmp/qemu-build + cd /tmp/qemu-build + /path/to/qemu-source/configure \ + --with-trace-events=3D/tmp/trace-events \ + --with-trace-instrument=3Ddynamic \ + --enable-trace-backend=3Dstderr \ + --prefix=3D/tmp/qemu-install + make + make install + +3. Create the "Makefile" to build the instrumentation library: + + mkdir -p /tmp/my-dinstrument + =20 + cat > /tmp/my-dinstrument/Makefile < /tmp/my-dinstrument/instrument.c < + #include + =20 + /* get event declarations */ + #include + =20 + /* enumerate events and manipulate their instrumentation callbacks *= / + #include + =20 + /* manipulate the dynamic tracing state of events */ + #include + =20 + /* define instrumentation callbacks during event execution: + * qi_event_${event} + */ + =20 + /* called every time QEMU traces event 'qemu_vmalloc' */ + void qi_event_qemu_vmalloc(size_t size, void *ptr) + { + fprintf(stderr, "qemu_vmalloc! [instrument]\n"); + =20 + /* call the original tracing routine */ + qi_event_qemu_vmalloc_trace(size, ptr); + =20 + /* disable instrumentation and tracing after 10 calls */ + static int c =3D 0; + if (c++ > 10) { + QIEvent *ev =3D qi_ctrl_event_id(QI_EVENT_QEMU_VMALLOC); + qi_ctrl_event_set(ev, QI_CTRL_INSTR_NOP); + qi_trace_event_set_state_dynamic(ev, false); + } + } + =20 + =20 + =20 + /* mandatory initialization callback */ + void qi_init(int argc, const char *argv) + { + int i; + fprintf(stderr, "init!\n"); + fprintf(stderr, " argc :: %d\n", argc); + for (i =3D 0; i < argc; i++) { + fprintf(stderr, " -> %s\n", argv[i]); + } + =20 + /* iterate on all trace events */ + QIEvent *ev =3D NULL; + while ((ev =3D qi_ctrl_event_pattern("*", ev)) !=3D NULL) { + + /* auto-instrument all events instrumentable at execution ti= me */ + if (qi_ctrl_event_is_available(ev)) { + =20 + printf("[exec] instrumenting '%s'\n", + qi_ctrl_event_get_name(ev)); + =20 + /* auto-detect instrumentation routines */ + bool instrumented =3D qi_ctrl_event_set(ev, QI_CTRL_INST= R_AUTO); + assert(instrumented); + =20 + /* activate tracing for that event + * (otherwise qi_event_${event}_trace does nothing when = using + * the 'stderr' backend) + */ + qi_trace_event_set_state_dynamic(ev, true); + } + } + } + =20 + /* mandatory finalization callback */ + void qi_fini(void) + { + fprintf(stderr, "fini!\n"); + =20 + /* ensure all tracing is disabled */ + QIEvent *ev =3D NULL; + while ((ev =3D qi_ctrl_event_pattern("*", ev)) !=3D NULL) { + qi_trace_event_set_state_dynamic(ev, false); + } + =20 + /* instrumentation callbacks are automatically reset by QEMU */ + } + EOF + +5. Compile the instrumentation library: + + make -C /tmp/my-dinstrument + +6. Start QEMU with the instrumentation library: + + /tmp/qemu-install/bin/qemu-system-x86_64 \ + -instr file=3D/tmp/my-dinstrument/.libs/libtrace-instrument.so + + +Alternatively, you can explicitly set which events and with which callba= ck you +want to instrument them: + + /* ... */ + void qi_init(int argc, const char *argv) + { + int i; + fprintf(stderr, "init!\n"); + fprintf(stderr, " argc :: %d\n", argc); + for (i =3D 0; i < argc; i++) { + fprintf(stderr, " -> %s\n", argv[i]); + } + =20 + QIEvent *ev; + bool instrumented; + =20 + /* NOTE: the callbacks can be pointers to arbitrary routines; + * there's no need to follow the 'qi_event_${event}' namin= g + * scheme if you're not using QI_CTRL_INSTR_AUTO. + */ + =20 + ev =3D qi_ctrl_event_id(QI_EVENT_QEMU_VMALLOC); + assert(ev); + printf("[exec] instrumenting '%s'\n", + qi_ctrl_event_get_name(ev)); + instrumented =3D qi_ctrl_event_set(ev, execute_qemu_vmalloc); + assert(instrumented); + qi_trace_event_set_state_dynamic(ev, true); + } + /* ... */ + + + +=3D=3D Static instrumentation =3D=3D + +Static instrumentation relies on you providing a directory that QEMU wil= l +compile on to produce a static library. The instrumentation library is c= ompiled +during the compilation of QEMU's target-dependant code, and the director= y you +provided will be present on the include path. This static library will t= hen be +linked into QEMU itself. + +This mechanism allows you to inline your own instrumentation code into t= he QEMU +tracing routines. This requires the (partial) recompilation of QEMU when= ever the +instrumentation analysis code changes, and is thus intended for +performance-critical event instrumentation analysis. + +For this reason, you can prototype your analysis using dynamic instrumen= tation +before moving to static instrumentation. + + +=3D=3D=3D Example =3D=3D=3D + +1. Select the events to instrument in file "trace-events" (see above). + +2. Create the "Makefile" to build the instrumentation library: + + mkdir -p /tmp/my-sinstrument + =20 + cat > /tmp/my-sinstrument/Makefile < + + // file residing in "/tmp/my-sinstrument/my-header.h", + // which declares 'my_trace_qemu_vmalloc_slow' + #include "my-header.h" + + + // requires "#define QI_EVENT_QEMU_VMALLOC_INLINE 1" in "events-pre.= h" + static inline void qi_event_qemu_vmalloc(size_t size, void *ptr) + { + if (/* ... performance-critical condition ... */) { + /* ... performance-critical code ... */ + } else { + my_trace_qemu_vmalloc_slow(size, ptr); + } + } + =20 + #endif /* EVENTS_POST_H */ + +Note: All these headers are completely optional, and will only be used i= f they +exist at compile time. + + + +=3D=3D Loading and unloading instrumentation libraries =3D=3D + +QEMU starts with all instrumentation callbacks set to their default valu= e, which +corresponds to a call to 'qi_event_${event}_nop'. + +To load a dynamic instrumentation library, the user can either use the "= -instr" +command line argument, or the "instr-load" command available in the QEMU +monitor, QAPI and QMP. + +Once loaded, QEMU will call the 'qi_init' routine in the instrumentation= library. + +To unload a dynamic instrumentation library, the user can use the "instr= -unload" +command. + +When unloading, QEMU will call the 'qi_fini' routine in the instrumentat= ion +library, and immediately after restore all the instrumentation callbacks= to +their default value. + +In the case of static instrumentation, the commands "instr-load" and +"instr-unload" are not available, and thus the instrumentation library i= s +already loaded when QEMU starts, and is unloaded when QEMU exits. + + + +=3D=3D Instrumentation API =3D=3D + +Both dynamic and static trace instrumentation libraries can interact wit= h QEMU +using the API provided in the headers found under the "qemu-instr" direc= tory +(installed alongside QEMU). + + +=3D=3D=3D Event enumeration =3D=3D=3D + +Events can be obtained either by identifier or by name. Identifiers are = values +of the enumeration 'QIEventID' and follow the naming scheme 'QI_EVENT_${= EVENT}' +(e.g., 'QI_EVENT_QEMU_VMALLOC' for the "qemu_vmalloc" event). + +Identifiers, together with defines indicating if an event is enabled at +compile-time ('QI_EVENT_${EVENT}_ENABLED') are located in the +"qemu-instr/events-list.h" header. + +The interface to obtain and enumerate these events is located in the +"qemu-instr/control.h" header. + + +=3D=3D=3D Event tracing =3D=3D=3D + +You can interact with the state of QEMU's event tracing backend (see doc= ument +"tracing.txt") through the API in the "qemu-instr/trace.h" header. + + +=3D=3D=3D Event instrumentation =3D=3D=3D + +You can control the instrumentation callbacks of events through the API = in the +"qemu-instr/control.h" header. + + +=3D=3D=3D Instrumentation type discrimination =3D=3D=3D + +The same source code for an instrumentation library can result in differ= ent code +depending on the current instrumentation type (either dynamic or static)= : + +* "qemu-instr/config.h" + + The define QI_TYPE_STATIC (QI_TYPE_DYNAMIC) will be available and set = to 1 if + QEMU has been compiled with static (dynamic) instrumentation. + +* "qemu-instr/control.h" + + Routine 'qi_ctrl_dynamic' can be used to discriminate if dynamic + instrumentation is available. + + Similarly, routine 'qi_ctrl_event_is_available' indicates whether the = given + event is available for instrumentation (both static and dynamic). + +With this, you can ensure the same code will work under both instrumenta= tion +types. + +Example initialization: + + #if defined(QI_TYPE_DYNAMIC) + #include "events-post.h" + #endif + =20 + void qi_init(int argc, const char *argv) + { + int i; + fprintf(stderr, "init!\n"); + fprintf(stderr, " argc :: %d\n", argc); + for (i =3D 0; i < argc; i++) { + fprintf(stderr, " -> %s\n", argv[i]); + } + =20 + QIEvent *ev =3D NULL; + while ((ev =3D qi_ctrl_event_pattern("*", ev)) !=3D NULL) { + + if (qi_ctrl_event_is_available(ev)) { + + /* changing the callback will fail during static instrum= entation */ + if (qi_ctrl_dynamic()) { + bool instrumented =3D qi_ctrl_event_set(ev, QI_CTRL_= INSTR_AUTO); + assert(instrumented); + } + + qi_trace_event_set_state_dynamic(ev, true); + } + } + } + +Example "events-post.h": + + #if defined(QI_TYPE_STATIC) + static inline + #endif + void qi_event_qemu_vmalloc(size_t size, void *ptr) + { + /* ... */ + } diff --git a/docs/tracing.txt b/docs/tracing.txt index cf53c17..d33717b 100644 --- a/docs/tracing.txt +++ b/docs/tracing.txt @@ -255,3 +255,12 @@ guard such computations and avoid its compilation wh= en the event is disabled: You can check both if the event has been disabled and is dynamically ena= bled at the same time using the 'trace_event_get_state' routine (see header "trace/control.h" for more information). + +=3D=3D=3D "instrument" =3D=3D=3D + +When compiling QEMU with trace instrumentation enabled, the "instrument" +property lets you provide your own implementation for that trace event. = This +implementation can override and/or wrap the backend-specific tracing cod= e +(regardless of the tracing backend). + +See the document "instrumentation.txt" for more information.