From nobody Mon Feb 9 03:13:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 150537589257847.46094640954618; Thu, 14 Sep 2017 00:58:12 -0700 (PDT) Received: from localhost ([::1]:46211 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsP2J-0004pW-Nf for importer@patchew.org; Thu, 14 Sep 2017 03:58:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52614) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsOwQ-0007Ef-Fp for qemu-devel@nongnu.org; Thu, 14 Sep 2017 03:52:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsOwO-0007qN-MI for qemu-devel@nongnu.org; Thu, 14 Sep 2017 03:52:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43400) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsOwO-0007o1-BN for qemu-devel@nongnu.org; Thu, 14 Sep 2017 03:52:04 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 409F87EA89; Thu, 14 Sep 2017 07:52:03 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-15-224.nay.redhat.com [10.66.15.224]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09CF570132; Thu, 14 Sep 2017 07:51:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 409F87EA89 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=peterx@redhat.com From: Peter Xu To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 15:50:32 +0800 Message-Id: <1505375436-28439-12-git-send-email-peterx@redhat.com> In-Reply-To: <1505375436-28439-1-git-send-email-peterx@redhat.com> References: <1505375436-28439-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 14 Sep 2017 07:52:03 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 11/15] monitor: separate QMP parser and dispatcher X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Fam Zheng , Juan Quintela , Markus Armbruster , peterx@redhat.com, mdroth@linux.vnet.ibm.com, Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , "Dr . David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Originally QMP is going throw these steps: JSON Parser --> QMP Dispatcher --> Respond /|\ (2) (3) | (1) | \|/ (4) +--------- main thread --------+ This patch does this: JSON Parser QMP Dispatcher --> Respond /|\ | /|\ (4) | | | (2) | (3) | (5) (1) | +-----> | \|/ +--------- main thread <-------+ So the parsing job and the dispatching job is isolated now. It gives us a chance in following up patches to totally move the parser outside. The isloation is done using one QEMUBH. Only one dispatcher BH for all the monitors. Signed-off-by: Peter Xu --- monitor.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++--------= ---- 1 file changed, 99 insertions(+), 23 deletions(-) diff --git a/monitor.c b/monitor.c index aa0c384..f649d6a 100644 --- a/monitor.c +++ b/monitor.c @@ -213,6 +213,10 @@ struct MonitorGlobal { QemuThread mon_io_thread; GMainContext *mon_context; GMainLoop *mon_loop; + /* Bottom half to dispatch the requests received from IO thread */ + QEMUBH *qmp_dispatcher_bh; + /* Input queue that hangs all the parsed QMP requests */ + GQueue *qmp_requests; }; =20 static struct MonitorGlobal mon_global; @@ -3858,29 +3862,31 @@ static void monitor_qmp_respond(Monitor *mon, QObje= ct *rsp, qobject_decref(rsp); } =20 -static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens, - void *opaque) +struct QMPRequest { + /* Owner of the request */ + Monitor *mon; + /* "id" field of the request */ + QObject *id; + /* Request object to be handled */ + QObject *req; +}; +typedef struct QMPRequest QMPRequest; + +/* + * Dispatch one single QMP request. The function will free the req_obj + * and objects inside it before return. + */ +static void monitor_qmp_dispatch_one(QMPRequest *req_obj) { - QObject *req, *rsp =3D NULL, *id =3D NULL; + Monitor *mon, *old_mon; + QObject *req, *rsp =3D NULL, *id; QDict *qdict =3D NULL; - Monitor *mon =3D opaque, *old_mon; - Error *err =3D NULL; =20 - req =3D json_parser_parse_err(tokens, NULL, &err); - if (!req && !err) { - /* json_parser_parse_err() sucks: can fail without setting @err */ - error_setg(&err, QERR_JSON_PARSING); - } - if (err) { - goto err_out; - } + req =3D req_obj->req; + mon =3D req_obj->mon; + id =3D req_obj->id; =20 - qdict =3D qobject_to_qdict(req); - if (qdict) { - id =3D qdict_get(qdict, "id"); - qobject_incref(id); - qdict_del(qdict, "id"); - } /* else will fail qmp_dispatch() */ + g_free(req_obj); =20 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { QString *req_json =3D qobject_to_json(req); @@ -3891,7 +3897,7 @@ static void handle_qmp_command(JSONMessageParser *par= ser, GQueue *tokens, old_mon =3D cur_mon; cur_mon =3D mon; =20 - rsp =3D qmp_dispatch(cur_mon->qmp.commands, req); + rsp =3D qmp_dispatch(mon->qmp.commands, req); =20 cur_mon =3D old_mon; =20 @@ -3907,12 +3913,66 @@ static void handle_qmp_command(JSONMessageParser *p= arser, GQueue *tokens, } } =20 -err_out: - monitor_qmp_respond(mon, rsp, err, id); - + /* Respond if necessary */ + monitor_qmp_respond(mon, rsp, NULL, id); qobject_decref(req); } =20 +static void monitor_qmp_bh_dispatcher(void *data) +{ + QMPRequest *req_obj; + + while (true) { + req_obj =3D g_queue_pop_head(mon_global.qmp_requests); + if (!req_obj) { + break; + } + monitor_qmp_dispatch_one(req_obj); + } +} + +static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens, + void *opaque) +{ + QObject *req, *id =3D NULL; + QDict *qdict =3D NULL; + Monitor *mon =3D opaque; + Error *err =3D NULL; + QMPRequest *req_obj; + + req =3D json_parser_parse_err(tokens, NULL, &err); + if (!req && !err) { + /* json_parser_parse_err() sucks: can fail without setting @err */ + error_setg(&err, QERR_JSON_PARSING); + } + if (err) { + monitor_qmp_respond(mon, NULL, err, NULL); + qobject_decref(req); + } + + qdict =3D qobject_to_qdict(req); + if (qdict) { + id =3D qdict_get(qdict, "id"); + qobject_incref(id); + qdict_del(qdict, "id"); + } /* else will fail qmp_dispatch() */ + + req_obj =3D g_new0(QMPRequest, 1); + req_obj->mon =3D mon; + req_obj->id =3D id; + req_obj->req =3D req; + + /* + * Put the request to the end of queue so that requests will be + * handled in time order. Ownership for req_obj, req, id, + * etc. will be delivered to the handler side. + */ + g_queue_push_tail(mon_global.qmp_requests, req_obj); + + /* Kick the dispatcher routine */ + qemu_bh_schedule(mon_global.qmp_dispatcher_bh); +} + static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) { Monitor *mon =3D opaque; @@ -4085,6 +4145,16 @@ static void monitor_io_thread_init(void) mon_global.mon_loop =3D g_main_loop_new(mon_global.mon_context, TRUE); qemu_thread_create(&mon_global.mon_io_thread, "mon-io-thr", monitor_io_thread, NULL, QEMU_THREAD_JOINABLE); + + /* + * This MUST be on main loop thread since we have commands that + * have assumption to be run on main loop thread (Yeah, we'd + * better remove this assumption in the future). + */ + mon_global.qmp_dispatcher_bh =3D aio_bh_new(qemu_get_aio_context(), + monitor_qmp_bh_dispatcher, + NULL); + mon_global.qmp_requests =3D g_queue_new(); } =20 void monitor_init_globals(void) @@ -4213,6 +4283,12 @@ void monitor_cleanup(void) qemu_mutex_unlock(&monitor_lock); =20 monitor_io_thread_destroy(); + + qemu_bh_delete(mon_global.qmp_dispatcher_bh); + mon_global.qmp_dispatcher_bh =3D NULL; + + g_queue_free(mon_global.qmp_requests); + mon_global.qmp_requests =3D NULL; } =20 QemuOptsList qemu_mon_opts =3D { --=20 2.7.4