Received: from eggs.gnu.org ([2001:4830:134:3::10]:57452) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPGac-00066T-GW for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bPGaa-0005io-9H for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46548) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPGaZ-0005ig-V0 for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:36 -0400 From: Stefan Hajnoczi Date: Mon, 18 Jul 2016 23:00:16 +0100 Message-Id: <1468879219-24500-7-git-send-email-stefanha@redhat.com> In-Reply-To: <1468879219-24500-1-git-send-email-stefanha@redhat.com> References: <1468879219-24500-1-git-send-email-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 6/9] trace: Add per-vCPU tracing states for events with the 'vcpu' property List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , =?UTF-8?q?Llu=C3=ADs=20Vilanova?= , Stefan Hajnoczi From: Llu=C3=ADs Vilanova Each vCPU gets a 'trace_dstate' bitmap to control the per-vCPU dynamic tracing state of events with the 'vcpu' property. Signed-off-by: Llu=C3=ADs Vilanova Signed-off-by: Stefan Hajnoczi --- bsd-user/main.c | 1 + include/qom/cpu.h | 6 +++++ linux-user/main.c | 1 + qom/cpu.c | 1 + stubs/Makefile.objs | 1 + stubs/trace-control.c | 28 ++++++++++++++++++++++ trace/Makefile.objs | 4 ++++ trace/control-internal.h | 30 +++++++++++++++++------ trace/control-target.c | 53 +++++++++++++++++++++++++++++++++++++++++ trace/control.c | 29 ++++++++++++++++++++-- trace/control.h | 62 ++++++++++++++++++++++++++++++++++++++++++= +++++- translate-all.h | 3 +++ vl.c | 1 + 13 files changed, 210 insertions(+), 10 deletions(-) create mode 100644 stubs/trace-control.c create mode 100644 trace/control-target.c diff --git a/bsd-user/main.c b/bsd-user/main.c index 6d6e3e3..315ba1d 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -1131,6 +1131,7 @@ int main(int argc, char **argv) gdbserver_start (gdbstub_port); gdb_handlesig(cpu, 0); } + trace_init_vcpu_events(); cpu_loop(env); /* never exits */ return 0; diff --git a/include/qom/cpu.h b/include/qom/cpu.h index a6c6ed8..cbcd64c 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -24,8 +24,10 @@ #include "disas/bfd.h" #include "exec/hwaddr.h" #include "exec/memattrs.h" +#include "qemu/bitmap.h" #include "qemu/queue.h" #include "qemu/thread.h" +#include "trace/generated-events.h" =20 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size, void *opaque); @@ -280,6 +282,7 @@ struct qemu_work_item { * @kvm_fd: vCPU file descriptor for KVM. * @work_mutex: Lock to prevent multiple access to queued_work_*. * @queued_work_first: First asynchronous work pending. + * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask= ). * * State of one CPU core or thread. */ @@ -347,6 +350,9 @@ struct CPUState { struct KVMState *kvm_state; struct kvm_run *kvm_run; =20 + /* Used for events with 'vcpu' and *without* the 'disabled' properti= es */ + DECLARE_BITMAP(trace_dstate, TRACE_VCPU_EVENT_COUNT); + /* TODO Move common fields from CPUArchState here. */ int cpu_index; /* used by alpha TCG */ uint32_t halted; /* used by alpha, cris, ppc TCG */ diff --git a/linux-user/main.c b/linux-user/main.c index a053c22..462e820 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4810,6 +4810,7 @@ int main(int argc, char **argv, char **envp) } gdb_handlesig(cpu, 0); } + trace_init_vcpu_events(); cpu_loop(env); /* never exits */ return 0; diff --git a/qom/cpu.c b/qom/cpu.c index a9727a1..42b5631 100644 --- a/qom/cpu.c +++ b/qom/cpu.c @@ -345,6 +345,7 @@ static void cpu_common_initfn(Object *obj) qemu_mutex_init(&cpu->work_mutex); QTAILQ_INIT(&cpu->breakpoints); QTAILQ_INIT(&cpu->watchpoints); + bitmap_zero(cpu->trace_dstate, TRACE_VCPU_EVENT_COUNT); } =20 static void cpu_common_finalize(Object *obj) diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs index 7cdcad4..55edd15 100644 --- a/stubs/Makefile.objs +++ b/stubs/Makefile.objs @@ -30,6 +30,7 @@ stub-obj-y +=3D runstate-check.o stub-obj-y +=3D set-fd-handler.o stub-obj-y +=3D slirp.o stub-obj-y +=3D sysbus.o +stub-obj-y +=3D trace-control.o stub-obj-y +=3D uuid.o stub-obj-y +=3D vm-stop.o stub-obj-y +=3D vmstate.o diff --git a/stubs/trace-control.c b/stubs/trace-control.c new file mode 100644 index 0000000..fe59836 --- /dev/null +++ b/stubs/trace-control.c @@ -0,0 +1,28 @@ +/* + * Interface for configuring and controlling the state of tracing events= . + * + * Copyright (C) 2014-2016 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "trace/control.h" + + +void trace_event_set_state_dynamic(TraceEvent *ev, bool state) +{ + TraceEventID id; + assert(trace_event_get_state_static(ev)); + id =3D trace_event_get_id(ev); + trace_events_enabled_count +=3D state - trace_events_dstate[id]; + trace_events_dstate[id] =3D state; +} + +void trace_event_set_vcpu_state_dynamic(CPUState *vcpu, + TraceEvent *ev, bool state) +{ + /* should never be called on non-target binaries */ + abort(); +} diff --git a/trace/Makefile.objs b/trace/Makefile.objs index cbe188e..4d91b3b 100644 --- a/trace/Makefile.objs +++ b/trace/Makefile.objs @@ -15,6 +15,7 @@ $(BUILD_DIR)/trace-events-all: $(trace-events-y:%=3D$(S= RC_PATH)/%) # Auto-generated event descriptions for LTTng ust code =20 ifeq ($(findstring ust,$(TRACE_BACKENDS)),ust) + $(obj)/generated-ust-provider.h: $(obj)/generated-ust-provider.h-timesta= mp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ $(obj)/generated-ust-provider.h-timestamp: $(BUILD_DIR)/trace-events-all= $(tracetool-y) @@ -33,6 +34,7 @@ $(obj)/generated-ust.c-timestamp: $(BUILD_DIR)/trace-ev= ents-all $(tracetool-y) =20 $(obj)/generated-events.h: $(obj)/generated-ust-provider.h $(obj)/generated-events.c: $(obj)/generated-ust.c + endif =20 ###################################################################### @@ -91,6 +93,7 @@ $(obj)/generated-tracers.o: $(obj)/generated-tracers.c = $(obj)/generated-tracers. # but that gets picked up by QEMU's Makefile as an external dependency # rule file. So we use '.dtrace' instead ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace) + $(obj)/generated-tracers-dtrace.dtrace: $(obj)/generated-tracers-dtrace.= dtrace-timestamp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ $(obj)/generated-tracers-dtrace.dtrace-timestamp: $(BUILD_DIR)/trace-eve= nts-all $(BUILD_DIR)/config-host.mak $(tracetool-y) @@ -155,4 +158,5 @@ util-obj-$(CONFIG_TRACE_SIMPLE) +=3D simple.o generat= ed-tracers.o util-obj-$(CONFIG_TRACE_FTRACE) +=3D ftrace.o util-obj-$(CONFIG_TRACE_UST) +=3D generated-ust.o util-obj-y +=3D control.o +target-obj-y +=3D control-target.o util-obj-y +=3D qmp.o diff --git a/trace/control-internal.h b/trace/control-internal.h index b326d88..a4e5f4a 100644 --- a/trace/control-internal.h +++ b/trace/control-internal.h @@ -10,8 +10,13 @@ #ifndef TRACE__CONTROL_INTERNAL_H #define TRACE__CONTROL_INTERNAL_H =20 +#include /* size_t */ + +#include "qom/cpu.h" + + extern TraceEvent trace_events[]; -extern bool trace_events_dstate[]; +extern uint16_t trace_events_dstate[]; extern int trace_events_enabled_count; =20 =20 @@ -74,13 +79,24 @@ static inline bool trace_event_get_state_dynamic(Trac= eEvent *ev) return trace_event_get_state_dynamic_by_id(id); } =20 -static inline void trace_event_set_state_dynamic(TraceEvent *ev, bool st= ate) +static inline bool trace_event_get_vcpu_state_dynamic_by_vcpu_id(CPUStat= e *vcpu, + TraceEv= entVCPUID id) { - int id =3D trace_event_get_id(ev); - assert(ev !=3D NULL); - assert(trace_event_get_state_static(ev)); - trace_events_enabled_count +=3D state - trace_events_dstate[id]; - trace_events_dstate[id] =3D state; + /* it's on fast path, avoid consistency checks (asserts) */ + if (unlikely(trace_events_enabled_count)) { + return test_bit(id, vcpu->trace_dstate); + } else { + return false; + } +} + +static inline bool trace_event_get_vcpu_state_dynamic(CPUState *vcpu, + TraceEvent *ev) +{ + TraceEventVCPUID id; + assert(trace_event_is_vcpu(ev)); + id =3D trace_event_get_vcpu_id(ev); + return trace_event_get_vcpu_state_dynamic_by_vcpu_id(vcpu, id); } =20 #endif /* TRACE__CONTROL_INTERNAL_H */ diff --git a/trace/control-target.c b/trace/control-target.c new file mode 100644 index 0000000..74c029a --- /dev/null +++ b/trace/control-target.c @@ -0,0 +1,53 @@ +/* + * Interface for configuring and controlling the state of tracing events= . + * + * Copyright (C) 2014-2016 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "trace/control.h" +#include "translate-all.h" + + +void trace_event_set_state_dynamic(TraceEvent *ev, bool state) +{ + CPUState *vcpu; + assert(trace_event_get_state_static(ev)); + if (trace_event_is_vcpu(ev)) { + CPU_FOREACH(vcpu) { + trace_event_set_vcpu_state_dynamic(vcpu, ev, state); + } + } else { + TraceEventID id =3D trace_event_get_id(ev); + trace_events_enabled_count +=3D state - trace_events_dstate[id]; + trace_events_dstate[id] =3D state; + } +} + +void trace_event_set_vcpu_state_dynamic(CPUState *vcpu, + TraceEvent *ev, bool state) +{ + TraceEventID id; + TraceEventVCPUID vcpu_id; + bool state_pre; + assert(trace_event_get_state_static(ev)); + assert(trace_event_is_vcpu(ev)); + id =3D trace_event_get_id(ev); + vcpu_id =3D trace_event_get_vcpu_id(ev); + state_pre =3D test_bit(vcpu_id, vcpu->trace_dstate); + if (state_pre !=3D state) { + if (state) { + trace_events_enabled_count++; + set_bit(vcpu_id, vcpu->trace_dstate); + trace_events_dstate[id]++; + } else { + trace_events_enabled_count--; + clear_bit(vcpu_id, vcpu->trace_dstate); + trace_events_dstate[id]--; + } + } +} diff --git a/trace/control.c b/trace/control.c index 86de8b9..d173c09 100644 --- a/trace/control.c +++ b/trace/control.c @@ -1,7 +1,7 @@ /* * Interface for configuring and controlling the state of tracing events= . * - * Copyright (C) 2011-2014 Llu=C3=ADs Vilanova + * Copyright (C) 2011-2016 Llu=C3=ADs Vilanova * * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. * See the COPYING file in the top-level directory. @@ -25,7 +25,14 @@ #include "monitor/monitor.h" =20 int trace_events_enabled_count; -bool trace_events_dstate[TRACE_EVENT_COUNT]; +/* + * Interpretation depends on wether the event has the 'vcpu' property: + * - false: Boolean value indicating whether the event is active. + * - true : Integral counting the number of vCPUs that have this event e= nabled. + */ +uint16_t trace_events_dstate[TRACE_EVENT_COUNT]; +/* Marks events for late vCPU state init */ +static bool trace_events_dstate_init[TRACE_EVENT_COUNT]; =20 QemuOptsList qemu_trace_opts =3D { .name =3D "trace", @@ -135,7 +142,10 @@ static void do_trace_enable_events(const char *line_= buf) TraceEvent *ev =3D NULL; while ((ev =3D trace_event_pattern(line_ptr, ev)) !=3D NULL) { if (trace_event_get_state_static(ev)) { + /* start tracing */ trace_event_set_state_dynamic(ev, enable); + /* mark for late vCPU init */ + trace_events_dstate_init[ev->id] =3D true; } } } else { @@ -147,7 +157,10 @@ static void do_trace_enable_events(const char *line_= buf) error_report("WARNING: trace event '%s' is not traceable", line_ptr); } else { + /* start tracing */ trace_event_set_state_dynamic(ev, enable); + /* mark for late vCPU init */ + trace_events_dstate_init[ev->id] =3D true; } } } @@ -257,3 +270,15 @@ char *trace_opt_parse(const char *optarg) =20 return trace_file; } + +void trace_init_vcpu_events(void) +{ + TraceEvent *ev =3D NULL; + while ((ev =3D trace_event_pattern("*", ev)) !=3D NULL) { + if (trace_event_is_vcpu(ev) && + trace_event_get_state_static(ev) && + trace_events_dstate_init[ev->id]) { + trace_event_set_state_dynamic(ev, true); + } + } +} diff --git a/trace/control.h b/trace/control.h index d7d6500..0413b28 100644 --- a/trace/control.h +++ b/trace/control.h @@ -124,6 +124,23 @@ static const char * trace_event_get_name(TraceEvent = *ev); ((id ##_ENABLED) && trace_event_get_state_dynamic_by_id(id)) =20 /** + * trace_event_get_vcpu_state: + * @vcpu: Target vCPU. + * @id: Event identifier (TraceEventID). + * @vcpu_id: Per-vCPU event identifier (TraceEventVCPUID). + * + * Get the tracing state of an event (both static and dynamic) for the g= iven + * vCPU. + * + * If the event has the disabled property, the check will have no perfor= mance + * impact. + * + * As a down side, you must always use an immediate #TraceEventID value. + */ +#define trace_event_get_vcpu_state(vcpu, id, vcpu_id) = \ + ((id ##_ENABLED) && trace_event_get_vcpu_state_dynamic_by_vcpu_id(vc= pu, vcpu_id)) + +/** * trace_event_get_state_static: * @id: Event identifier. * @@ -138,10 +155,19 @@ static bool trace_event_get_state_static(TraceEvent= *ev); * trace_event_get_state_dynamic: * * Get the dynamic tracing state of an event. + * + * If the event has the 'vcpu' property, gets the OR'ed state of all vCP= Us. */ static bool trace_event_get_state_dynamic(TraceEvent *ev); =20 /** + * trace_event_get_vcpu_state_dynamic: + * + * Get the dynamic tracing state of an event for the given vCPU. + */ +static bool trace_event_get_vcpu_state_dynamic(CPUState *vcpu, TraceEven= t *ev); + +/** * trace_event_set_state: * * Set the tracing state of an event (only if possible). @@ -155,13 +181,38 @@ static bool trace_event_get_state_dynamic(TraceEven= t *ev); } while (0) =20 /** + * trace_event_set_vcpu_state: + * + * Set the tracing state of an event for the given vCPU (only if not dis= abled). + */ +#define trace_event_set_vcpu_state(vcpu, id, state) = \ + do { = \ + if ((id ##_ENABLED)) { = \ + TraceEvent *_e =3D trace_event_id(id); = \ + trace_event_set_vcpu_state_dynamic(vcpu, _e, state); = \ + } = \ + } while (0) + +/** * trace_event_set_state_dynamic: * * Set the dynamic tracing state of an event. * + * If the event has the 'vcpu' property, sets the state on all vCPUs. + * * Pre-condition: trace_event_get_state_static(ev) =3D=3D true */ -static void trace_event_set_state_dynamic(TraceEvent *ev, bool state); +void trace_event_set_state_dynamic(TraceEvent *ev, bool state); + +/** + * trace_event_set_vcpu_state_dynamic: + * + * Set the dynamic tracing state of an event for the given vCPU. + * + * Pre-condition: trace_event_get_vcpu_state_static(ev) =3D=3D true + */ +void trace_event_set_vcpu_state_dynamic(CPUState *vcpu, + TraceEvent *ev, bool state); =20 =20 =20 @@ -218,6 +269,15 @@ extern QemuOptsList qemu_trace_opts; */ char *trace_opt_parse(const char *optarg); =20 +/** + * trace_init_vcpu_events: + * + * Re-synchronize initial event state with vCPUs (which can be created a= fter + * trace_init_events()). + */ +void trace_init_vcpu_events(void); + + #include "trace/control-internal.h" =20 #endif /* TRACE__CONTROL_H */ diff --git a/translate-all.h b/translate-all.h index ce6071b..ba8e4d6 100644 --- a/translate-all.h +++ b/translate-all.h @@ -19,6 +19,9 @@ #ifndef TRANSLATE_ALL_H #define TRANSLATE_ALL_H =20 +#include "exec/exec-all.h" + + /* translate-all.c */ void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len); void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t = end, diff --git a/vl.c b/vl.c index d3ec532..a455947 100644 --- a/vl.c +++ b/vl.c @@ -4598,6 +4598,7 @@ int main(int argc, char **argv, char **envp) =20 os_setup_post(); =20 + trace_init_vcpu_events(); main_loop(); replay_disable_events(); =20 --=20 2.7.4