Received: from eggs.gnu.org ([2001:4830:134:3::10]:57483) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPGaj-0006CE-NW for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bPGag-0005ju-3B for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:12964) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bPGaf-0005jq-K8 for qemu-devel@nongnu.org; Mon, 18 Jul 2016 18:00:41 -0400 From: Stefan Hajnoczi Date: Mon, 18 Jul 2016 23:00:19 +0100 Message-Id: <1468879219-24500-10-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 9/9] trace: Add QAPI/QMP interfaces to query and control per-vCPU tracing state 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 Signed-off-by: Llu=C3=ADs Vilanova Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Stefan Hajnoczi --- hmp-commands-info.hx | 6 +- hmp-commands.hx | 7 ++- monitor.c | 17 +++++- qapi/trace.json | 33 +++++++++-- qmp-commands.hx | 35 +++++++++++- trace/qmp.c | 154 ++++++++++++++++++++++++++++++++++++++++-----= ------ 6 files changed, 206 insertions(+), 46 deletions(-) diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx index 3d07ca6..74446c6 100644 --- a/hmp-commands-info.hx +++ b/hmp-commands-info.hx @@ -646,10 +646,10 @@ ETEXI =20 { .name =3D "trace-events", - .args_type =3D "name:s?", - .params =3D "[name]", + .args_type =3D "name:s?,vcpu:i?", + .params =3D "[name] [vcpu]", .help =3D "show available trace-events & their state " - "(name: event name pattern)", + "(name: event name pattern; vcpu: vCPU to query, d= efault is any)", .mhandler.cmd =3D hmp_info_trace_events, .command_completion =3D info_trace_events_completion, }, diff --git a/hmp-commands.hx b/hmp-commands.hx index 98b4b1a..848efee 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -281,9 +281,10 @@ ETEXI =20 { .name =3D "trace-event", - .args_type =3D "name:s,option:b", - .params =3D "name on|off", - .help =3D "changes status of a specific trace event", + .args_type =3D "name:s,option:b,vcpu:i?", + .params =3D "name on|off [vcpu]", + .help =3D "changes status of a specific trace event " + "(vcpu: vCPU to set, default is all)", .mhandler.cmd =3D hmp_trace_event, .command_completion =3D trace_event_completion, }, diff --git a/monitor.c b/monitor.c index ab31725..c1591f2 100644 --- a/monitor.c +++ b/monitor.c @@ -904,9 +904,16 @@ static void hmp_trace_event(Monitor *mon, const QDic= t *qdict) { const char *tp_name =3D qdict_get_str(qdict, "name"); bool new_state =3D qdict_get_bool(qdict, "option"); + bool has_vcpu =3D qdict_haskey(qdict, "vcpu"); + int vcpu =3D qdict_get_try_int(qdict, "vcpu", 0); Error *local_err =3D NULL; =20 - qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err= ); + if (vcpu < 0) { + monitor_printf(mon, "argument vcpu must be positive"); + return; + } + + qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, = vcpu, &local_err); if (local_err) { error_report_err(local_err); } @@ -1066,6 +1073,8 @@ static void hmp_info_cpustats(Monitor *mon, const Q= Dict *qdict) static void hmp_info_trace_events(Monitor *mon, const QDict *qdict) { const char *name =3D qdict_get_try_str(qdict, "name"); + bool has_vcpu =3D qdict_haskey(qdict, "vcpu"); + int vcpu =3D qdict_get_try_int(qdict, "vcpu", 0); TraceEventInfoList *events; TraceEventInfoList *elem; Error *local_err =3D NULL; @@ -1073,8 +1082,12 @@ static void hmp_info_trace_events(Monitor *mon, co= nst QDict *qdict) if (name =3D=3D NULL) { name =3D "*"; } + if (vcpu < 0) { + monitor_printf(mon, "argument vcpu must be positive"); + return; + } =20 - events =3D qmp_trace_event_get_state(name, &local_err); + events =3D qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_er= r); if (local_err) { error_report_err(local_err); return; diff --git a/qapi/trace.json b/qapi/trace.json index 01b0a52..e872146 100644 --- a/qapi/trace.json +++ b/qapi/trace.json @@ -1,6 +1,6 @@ # -*- mode: python -*- # -# 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 lat= er. # See the COPYING file in the top-level directory. @@ -29,11 +29,15 @@ # # @name: Event name. # @state: Tracing state. +# @vcpu: Whether this is a per-vCPU event (since 2.7). +# +# An event is per-vCPU if it has the "vcpu" property in the "trace-event= s" +# files. # # Since 2.2 ## { 'struct': 'TraceEventInfo', - 'data': {'name': 'str', 'state': 'TraceEventState'} } + 'data': {'name': 'str', 'state': 'TraceEventState', 'vcpu': 'bool'} } =20 ## # @trace-event-get-state: @@ -41,13 +45,23 @@ # Query the state of events. # # @name: Event name pattern (case-sensitive glob). +# @vcpu: #optional The vCPU to query (any by default; since 2.7). # # Returns: a list of @TraceEventInfo for the matching events # +# An event is returned if: +# - its name matches the @name pattern, and +# - if @vcpu is given, the event has the "vcpu" property. +# +# Therefore, if @vcpu is given, the operation will only match per-vCPU e= vents, +# returning their state on the specified vCPU. Special case: if @name is= an +# exact match, @vcpu is given and the event does not have the "vcpu" pro= perty, +# an error is returned. +# # Since 2.2 ## { 'command': 'trace-event-get-state', - 'data': {'name': 'str'}, + 'data': {'name': 'str', '*vcpu': 'int'}, 'returns': ['TraceEventInfo'] } =20 ## @@ -58,8 +72,19 @@ # @name: Event name pattern (case-sensitive glob). # @enable: Whether to enable tracing. # @ignore-unavailable: #optional Do not match unavailable events with @n= ame. +# @vcpu: #optional The vCPU to act upon (all by default; since 2.7). +# +# An event's state is modified if: +# - its name matches the @name pattern, and +# - if @vcpu is given, the event has the "vcpu" property. +# +# Therefore, if @vcpu is given, the operation will only match per-vCPU e= vents, +# setting their state on the specified vCPU. Special case: if @name is a= n exact +# match, @vcpu is given and the event does not have the "vcpu" property,= an +# error is returned. # # Since 2.2 ## { 'command': 'trace-event-set-state', - 'data': {'name': 'str', 'enable': 'bool', '*ignore-unavailable': 'bool= '} } + 'data': {'name': 'str', 'enable': 'bool', '*ignore-unavailable': 'bool= ', + '*vcpu': 'int'} } diff --git a/qmp-commands.hx b/qmp-commands.hx index c46c65c..496d73c 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -4715,7 +4715,7 @@ EQMP =20 { .name =3D "trace-event-get-state", - .args_type =3D "name:s", + .args_type =3D "name:s,vcpu:i?", .mhandler.cmd_new =3D qmp_marshal_trace_event_get_state, }, =20 @@ -4725,6 +4725,20 @@ trace-event-get-state =20 Query the state of events. =20 +Arguments: + +- "name": Event name pattern (json-string). +- "vcpu": The vCPU to query, any vCPU by default (json-int, optional). + +An event is returned if: +- its name matches the "name" pattern, and +- if "vcpu" is given, the event has the "vcpu" property. + +Therefore, if "vcpu" is given, the operation will only match per-vCPU ev= ents, +returning their state on the specified vCPU. Special case: if "name" is = an exact +match, "vcpu" is given and the event does not have the "vcpu" property, = an error +is returned. + Example: =20 -> { "execute": "trace-event-get-state", "arguments": { "name": "qemu_me= malign" } } @@ -4733,7 +4747,7 @@ EQMP =20 { .name =3D "trace-event-set-state", - .args_type =3D "name:s,enable:b,ignore-unavailable:b?", + .args_type =3D "name:s,enable:b,ignore-unavailable:b?,vcpu:i?", .mhandler.cmd_new =3D qmp_marshal_trace_event_set_state, }, =20 @@ -4743,6 +4757,23 @@ trace-event-set-state =20 Set the state of events. =20 +Arguments: + +- "name": Event name pattern (json-string). +- "enable": Whether to enable or disable the event (json-bool). +- "ignore-unavailable": Whether to ignore errors for events that cannot = be + changed (json-bool, optional). +- "vcpu": The vCPU to act upon, all vCPUs by default (json-int, optional= ). + +An event's state is modified if: +- its name matches the "name" pattern, and +- if "vcpu" is given, the event has the "vcpu" property. + +Therefore, if "vcpu" is given, the operation will only match per-vCPU ev= ents, +setting their state on the specified vCPU. Special case: if "name" is an= exact +match, "vcpu" is given and the event does not have the "vcpu" property, = an error +is returned. + Example: =20 -> { "execute": "trace-event-set-state", "arguments": { "name": "qemu_me= malign", "enable": "true" } } diff --git a/trace/qmp.c b/trace/qmp.c index 8aa2660..11d2564 100644 --- a/trace/qmp.c +++ b/trace/qmp.c @@ -1,7 +1,7 @@ /* * QMP commands for tracing events. * - * Copyright (C) 2014 Llu=C3=ADs Vilanova + * 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. @@ -12,63 +12,153 @@ #include "trace/control.h" =20 =20 -TraceEventInfoList *qmp_trace_event_get_state(const char *name, Error **= errp) +static CPUState *get_cpu(bool has_vcpu, int vcpu, Error **errp) { + if (has_vcpu) { + CPUState *cpu =3D qemu_get_cpu(vcpu); + if (cpu =3D=3D NULL) { + error_setg(errp, "invalid vCPU index %u", vcpu); + } + return cpu; + } else { + return NULL; + } +} + +static bool check_events(bool has_vcpu, bool ignore_unavailable, bool is= _pattern, + const char *name, Error **errp) +{ + if (!is_pattern) { + TraceEvent *ev =3D trace_event_name(name); + + /* error for non-existing event */ + if (ev =3D=3D NULL) { + error_setg(errp, "unknown event \"%s\"", name); + return false; + } + + /* error for non-vcpu event */ + if (has_vcpu && !trace_event_is_vcpu(ev)) { + error_setg(errp, "event \"%s\" is not vCPU-specific", name); + return false; + } + + /* error for unavailable event */ + if (!ignore_unavailable && !trace_event_get_state_static(ev)) { + error_setg(errp, "event \"%s\" is disabled", name); + return false; + } + + return true; + } else { + /* error for unavailable events */ + TraceEvent *ev =3D NULL; + while ((ev =3D trace_event_pattern(name, ev)) !=3D NULL) { + if (!ignore_unavailable && !trace_event_get_state_static(ev)= ) { + error_setg(errp, "event \"%s\" is disabled", trace_event= _get_name(ev)); + return false; + } + } + return true; + } +} + +TraceEventInfoList *qmp_trace_event_get_state(const char *name, + bool has_vcpu, int64_t vcp= u, + Error **errp) +{ + Error *err =3D NULL; TraceEventInfoList *events =3D NULL; - bool found =3D false; TraceEvent *ev; + bool is_pattern =3D trace_event_is_pattern(name); + CPUState *cpu; =20 + /* Check provided vcpu */ + cpu =3D get_cpu(has_vcpu, vcpu, &err); + if (err) { + error_propagate(errp, err); + return NULL; + } + + /* Check events */ + if (!check_events(has_vcpu, true, is_pattern, name, errp)) { + return NULL; + } + + /* Get states (all errors checked above) */ ev =3D NULL; while ((ev =3D trace_event_pattern(name, ev)) !=3D NULL) { - TraceEventInfoList *elem =3D g_new(TraceEventInfoList, 1); + TraceEventInfoList *elem; + bool is_vcpu =3D trace_event_is_vcpu(ev); + if (has_vcpu && !is_vcpu) { + continue; + } + + elem =3D g_new(TraceEventInfoList, 1); elem->value =3D g_new(TraceEventInfo, 1); + elem->value->vcpu =3D is_vcpu; elem->value->name =3D g_strdup(trace_event_get_name(ev)); + if (!trace_event_get_state_static(ev)) { elem->value->state =3D TRACE_EVENT_STATE_UNAVAILABLE; - } else if (!trace_event_get_state_dynamic(ev)) { - elem->value->state =3D TRACE_EVENT_STATE_DISABLED; } else { - elem->value->state =3D TRACE_EVENT_STATE_ENABLED; + if (has_vcpu) { + if (is_vcpu) { + if (trace_event_get_vcpu_state_dynamic(cpu, ev)) { + elem->value->state =3D TRACE_EVENT_STATE_ENABLED= ; + } else { + elem->value->state =3D TRACE_EVENT_STATE_DISABLE= D; + } + } + /* else: already skipped above */ + } else { + if (trace_event_get_state_dynamic(ev)) { + elem->value->state =3D TRACE_EVENT_STATE_ENABLED; + } else { + elem->value->state =3D TRACE_EVENT_STATE_DISABLED; + } + } } elem->next =3D events; events =3D elem; - found =3D true; - } - - if (!found && !trace_event_is_pattern(name)) { - error_setg(errp, "unknown event \"%s\"", name); } =20 return events; } =20 void qmp_trace_event_set_state(const char *name, bool enable, - bool has_ignore_unavailable, - bool ignore_unavailable, Error **errp) + bool has_ignore_unavailable, bool ignore_= unavailable, + bool has_vcpu, int64_t vcpu, + Error **errp) { - bool found =3D false; + Error *err =3D NULL; TraceEvent *ev; + bool is_pattern =3D trace_event_is_pattern(name); + CPUState *cpu; =20 - /* Check all selected events are dynamic */ + /* Check provided vcpu */ + cpu =3D get_cpu(has_vcpu, vcpu, &err); + if (err) { + error_propagate(errp, err); + return; + } + + /* Check events */ + if (!check_events(has_vcpu, has_ignore_unavailable && ignore_unavail= able, + is_pattern, name, errp)) { + return; + } + + /* Apply changes (all errors checked above) */ ev =3D NULL; while ((ev =3D trace_event_pattern(name, ev)) !=3D NULL) { - found =3D true; - if (!(has_ignore_unavailable && ignore_unavailable) && - !trace_event_get_state_static(ev)) { - error_setg(errp, "cannot set dynamic tracing state for \"%s\= "", - trace_event_get_name(ev)); - return; + if (!trace_event_get_state_static(ev) || + (has_vcpu && !trace_event_is_vcpu(ev))) { + continue; } - } - if (!found && !trace_event_is_pattern(name)) { - error_setg(errp, "unknown event \"%s\"", name); - return; - } - - /* Apply changes */ - ev =3D NULL; - while ((ev =3D trace_event_pattern(name, ev)) !=3D NULL) { - if (trace_event_get_state_static(ev)) { + if (has_vcpu) { + trace_event_set_vcpu_state_dynamic(cpu, ev, enable); + } else { trace_event_set_state_dynamic(ev, enable); } } --=20 2.7.4