All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: Stefan Hajnoczi <shajnocz@redhat.com>,
	"Daniel P . Berrange" <berrange@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Fam Zheng <famz@redhat.com>,
	Jiri Denemark <jdenemar@redhat.com>,
	Juan Quintela <quintela@redhat.com>,
	mdroth@linux.vnet.ibm.com, peterx@redhat.com,
	Eric Blake <eblake@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	marcandre.lureau@redhat.com,
	Markus Armbruster <armbru@redhat.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [RFC v3 23/27] qmp: isolate responses into io thread
Date: Mon,  6 Nov 2017 17:46:39 +0800	[thread overview]
Message-ID: <20171106094643.14881-24-peterx@redhat.com> (raw)
In-Reply-To: <20171106094643.14881-1-peterx@redhat.com>

For those monitors who has enabled IO thread, we'll offload the
responding procedure into IO thread.  The main reason is that chardev is
not thread safe, and we need to do all the read/write IOs in the same
thread.  For use_io_thr=true monitors, that thread is the IO thread.

We do this isolation in similar pattern as what we have done to the
request queue: we first create one response queue for each monitor, then
instead of reply directly in main thread, we queue the responses and
kick the IO thread to do the rest of the job for us.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 monitor.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 1 deletion(-)

diff --git a/monitor.c b/monitor.c
index 45d293469a..a2550d79a8 100644
--- a/monitor.c
+++ b/monitor.c
@@ -172,6 +172,8 @@ typedef struct {
     QemuMutex qmp_queue_lock;
     /* Input queue that holds all the parsed QMP requests */
     GQueue *qmp_requests;
+    /* Output queue contains all the QMP responses in order */
+    GQueue *qmp_responses;
 } MonitorQMP;
 
 /*
@@ -219,6 +221,8 @@ struct MonitorGlobal {
     IOThread *mon_iothread;
     /* Bottom half to dispatch the requests received from IO thread */
     QEMUBH *qmp_dispatcher_bh;
+    /* Bottom half to deliver the responses back to clients */
+    QEMUBH *qmp_respond_bh;
 };
 
 static struct MonitorGlobal mon_global;
@@ -395,7 +399,8 @@ int monitor_fprintf(FILE *stream, const char *fmt, ...)
     return 0;
 }
 
-static void monitor_json_emitter(Monitor *mon, const QObject *data)
+static void monitor_json_emitter_raw(Monitor *mon,
+                                     QObject *data)
 {
     QString *json;
 
@@ -409,6 +414,71 @@ static void monitor_json_emitter(Monitor *mon, const QObject *data)
     QDECREF(json);
 }
 
+static void monitor_json_emitter(Monitor *mon, QObject *data)
+{
+    if (mon->use_io_thr) {
+        /*
+         * If using IO thread, we need to queue the item so that IO
+         * thread will do the rest for us.  Take refcount so that
+         * caller won't free the data (which will be finally freed in
+         * responder thread).
+         */
+        qobject_incref(data);
+        qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
+        g_queue_push_tail(mon->qmp.qmp_responses, (void *)data);
+        qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
+        qemu_bh_schedule(mon_global.qmp_respond_bh);
+    } else {
+        /*
+         * If not using monitor IO thread, then we are in main thread.
+         * Do the emission right away.
+         */
+        monitor_json_emitter_raw(mon, data);
+    }
+}
+
+struct QMPResponse {
+    Monitor *mon;
+    QObject *data;
+};
+typedef struct QMPResponse QMPResponse;
+
+/*
+ * Return one QMPResponse.  The response is only valid if
+ * response.data is not NULL.
+ */
+static QMPResponse monitor_qmp_response_pop_one(void)
+{
+    Monitor *mon;
+    QObject *data = NULL;
+
+    qemu_mutex_lock(&monitor_lock);
+    QTAILQ_FOREACH(mon, &mon_list, entry) {
+        qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
+        data = g_queue_pop_head(mon->qmp.qmp_responses);
+        qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
+        if (data) {
+            break;
+        }
+    }
+    qemu_mutex_unlock(&monitor_lock);
+    return (QMPResponse) { .mon = mon, .data = data };
+}
+
+static void monitor_qmp_bh_responder(void *opaque)
+{
+    QMPResponse response;
+
+    while (true) {
+        response = monitor_qmp_response_pop_one();
+        if (!response.data) {
+            break;
+        }
+        monitor_json_emitter_raw(response.mon, response.data);
+        qobject_decref(response.data);
+    }
+}
+
 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
     /* Limit guest-triggerable events to 1 per second */
     [QAPI_EVENT_RTC_CHANGE]        = { 1000 * SCALE_MS },
@@ -595,6 +665,7 @@ static void monitor_data_init(Monitor *mon, bool skip_flush,
     mon->skip_flush = skip_flush;
     mon->use_io_thr = use_io_thr;
     mon->qmp.qmp_requests = g_queue_new();
+    mon->qmp.qmp_responses = g_queue_new();
 }
 
 static void monitor_data_destroy(Monitor *mon)
@@ -609,6 +680,7 @@ static void monitor_data_destroy(Monitor *mon)
     qemu_mutex_destroy(&mon->out_lock);
     qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
     g_queue_free(mon->qmp.qmp_requests);
+    g_queue_free(mon->qmp.qmp_responses);
 }
 
 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
@@ -4294,6 +4366,11 @@ static GMainContext *monitor_io_context_get(void)
     return iothread_get_g_main_context(mon_global.mon_iothread);
 }
 
+static AioContext *monitor_aio_context_get(void)
+{
+    return iothread_get_aio_context(mon_global.mon_iothread);
+}
+
 static void monitor_iothread_init(void)
 {
     mon_global.mon_iothread = iothread_create("monitor_iothread",
@@ -4312,6 +4389,15 @@ static void monitor_iothread_init(void)
     mon_global.qmp_dispatcher_bh = aio_bh_new(qemu_get_aio_context(),
                                               monitor_qmp_bh_dispatcher,
                                               NULL);
+
+    /*
+     * Unlike the dispatcher BH, this must be run on the monitor IO
+     * thread, so that monitors that are using IO thread will make
+     * sure read/write operations are all done on the IO thread.
+     */
+    mon_global.qmp_respond_bh = aio_bh_new(monitor_aio_context_get(),
+                                           monitor_qmp_bh_responder,
+                                           NULL);
 }
 
 void monitor_init_globals(void)
@@ -4435,6 +4521,8 @@ void monitor_cleanup(void)
     /* QEMUBHs needs to be deleted before destroying the IOThread. */
     qemu_bh_delete(mon_global.qmp_dispatcher_bh);
     mon_global.qmp_dispatcher_bh = NULL;
+    qemu_bh_delete(mon_global.qmp_respond_bh);
+    mon_global.qmp_respond_bh = NULL;
 
     iothread_destroy(mon_global.mon_iothread);
     mon_global.mon_iothread = NULL;
-- 
2.13.5

  parent reply	other threads:[~2017-11-06  9:51 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-06  9:46 [Qemu-devel] [RFC v3 00/27] QMP: out-of-band (OOB) execution support Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 01/27] char-io: fix possible race on IOWatchPoll Peter Xu
2017-11-07  6:43   ` Fam Zheng
2017-11-13 16:52   ` Stefan Hajnoczi
2017-11-14  6:09     ` Peter Xu
2017-11-14 10:32       ` Stefan Hajnoczi
2017-11-14 11:31         ` Peter Xu
2017-11-15  9:37           ` Stefan Hajnoczi
2017-11-15  9:48             ` Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 02/27] qobject: introduce qstring_get_try_str() Peter Xu
2017-11-07  6:47   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 03/27] qobject: introduce qobject_get_try_str() Peter Xu
2017-11-07  6:48   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 04/27] qobject: let object_property_get_str() use new API Peter Xu
2017-11-07  6:50   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 05/27] monitor: move skip_flush into monitor_data_init Peter Xu
2017-11-07  6:51   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 06/27] qjson: add "opaque" field to JSONMessageParser Peter Xu
2017-11-07  6:54   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 07/27] monitor: move the cur_mon hack deeper for QMP Peter Xu
2017-11-07  6:58   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 08/27] monitor: unify global init Peter Xu
2017-11-07  7:03   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 09/27] monitor: let mon_list be tail queue Peter Xu
2017-11-07  7:05   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 10/27] monitor: create monitor dedicate iothread Peter Xu
2017-11-07  7:11   ` Fam Zheng
2017-11-08  7:25     ` Peter Xu
2017-11-08 11:18       ` Fam Zheng
2017-11-08 11:35         ` Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 11/27] monitor: allow to use IO thread for parsing Peter Xu
2017-11-07  7:17   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 12/27] qmp: introduce QMPCapability Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 13/27] qmp: negociate QMP capabilities Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 14/27] qmp: introduce some capability helpers Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 15/27] monitor: introduce monitor_qmp_respond() Peter Xu
2017-11-07  7:24   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 16/27] monitor: let monitor_{suspend|resume} thread safe Peter Xu
2017-11-07  7:26   ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 17/27] monitor: separate QMP parser and dispatcher Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 18/27] qmp: add new event "request-dropped" Peter Xu
2017-11-15 10:50   ` Stefan Hajnoczi
2017-11-16  5:56     ` Peter Xu
2017-11-16  6:29       ` Peter Xu
2017-11-16  6:49         ` Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 19/27] monitor: send event when request queue full Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 20/27] qapi: introduce new cmd option "allow-oob" Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 21/27] qmp: support out-of-band (oob) execution Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 22/27] qmp: let migrate-incoming allow out-of-band Peter Xu
2017-11-06  9:46 ` Peter Xu [this message]
2017-11-07  7:57   ` [Qemu-devel] [RFC v3 23/27] qmp: isolate responses into io thread Fam Zheng
2017-11-08  7:31     ` Peter Xu
2017-11-08 11:16       ` Fam Zheng
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 24/27] monitor: enable IO thread for (qmp & !mux) typed Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 25/27] docs: update QMP documents for OOB commands Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 26/27] tests: qmp-test: verify command batching Peter Xu
2017-11-06  9:46 ` [Qemu-devel] [RFC v3 27/27] tests: qmp-test: add oob test Peter Xu
2017-11-15 10:21   ` Stefan Hajnoczi
2017-11-16  8:02     ` Peter Xu
2017-11-06 10:12 ` [Qemu-devel] [RFC v3 00/27] QMP: out-of-band (OOB) execution support no-reply
2017-11-06 13:08   ` Peter Xu
2017-11-15  9:42     ` Stefan Hajnoczi
2017-11-16  5:32       ` Peter Xu
2017-11-16  5:39         ` Fam Zheng
2017-11-16  6:36           ` Peter Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171106094643.14881-24-peterx@redhat.com \
    --to=peterx@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=jdenemar@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=shajnocz@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.