[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC v2 20/22] qmp: isolate responses into io thread
From: |
Peter Xu |
Subject: |
[Qemu-devel] [RFC v2 20/22] qmp: isolate responses into io thread |
Date: |
Fri, 29 Sep 2017 11:38:42 +0800 |
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 <address@hidden>
---
monitor.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 85 insertions(+), 1 deletion(-)
diff --git a/monitor.c b/monitor.c
index 2be79eadcc..649491a2ce 100644
--- a/monitor.c
+++ b/monitor.c
@@ -211,12 +211,16 @@ struct Monitor {
QTAILQ_ENTRY(Monitor) entry;
/* Input queue that hangs all the parsed QMP requests */
GQueue *qmp_requests;
+ /* Output queue contains all the QMP responses in order */
+ GQueue *qmp_responses;
};
struct MonitorGlobal {
IOThread *mon_io_thread;
/* 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;
@@ -393,7 +397,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;
@@ -407,6 +412,67 @@ 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);
+ g_queue_push_tail(mon->qmp_responses, (void *)data);
+ 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) {
+ data = g_queue_pop_head(mon->qmp_responses);
+ 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 },
@@ -592,6 +658,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_requests = g_queue_new();
+ mon->qmp_responses = g_queue_new();
}
static void monitor_data_destroy(Monitor *mon)
@@ -604,6 +671,7 @@ static void monitor_data_destroy(Monitor *mon)
QDECREF(mon->outbuf);
qemu_mutex_destroy(&mon->out_lock);
g_queue_free(mon->qmp_requests);
+ g_queue_free(mon->qmp_responses);
}
char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
@@ -4197,6 +4265,11 @@ static GMainContext *monitor_io_context_get(void)
return iothread_get_g_main_context(mon_global.mon_io_thread);
}
+static AioContext *monitor_aio_context_get(void)
+{
+ return iothread_get_aio_context(mon_global.mon_io_thread);
+}
+
static void monitor_io_thread_init(void)
{
mon_global.mon_io_thread = iothread_create("monitor_io_thr",
@@ -4215,6 +4288,15 @@ static void monitor_io_thread_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)
@@ -4321,6 +4403,8 @@ static void monitor_io_thread_destroy(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_io_thread);
mon_global.mon_io_thread = NULL;
--
2.13.5
- [Qemu-devel] [RFC v2 10/22] monitor: allow to use IO thread for parsing, (continued)
- [Qemu-devel] [RFC v2 10/22] monitor: allow to use IO thread for parsing, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 11/22] monitor: introduce monitor_qmp_respond(), Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 12/22] monitor: let mon_list be tail queue, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 13/22] monitor: separate QMP parser and dispatcher, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 14/22] qmp: add new event "request-dropped", Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 15/22] monitor: send event when request queue full, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 16/22] monitor: enable IO thread for (qmp & !mux) typed, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 17/22] qapi: introduce new cmd option "allow-oob", Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 18/22] qmp: support out-of-band (oob) execution, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 19/22] qmp: let migrate-incoming allow out-of-band, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 20/22] qmp: isolate responses into io thread,
Peter Xu <=
- [Qemu-devel] [RFC v2 21/22] qmp: introduce QMPCapability, Peter Xu, 2017/09/28
- [Qemu-devel] [RFC v2 22/22] docs: update QMP documents for OOB commands, Peter Xu, 2017/09/28
- Re: [Qemu-devel] [RFC v2 00/22] QMP: out-of-band (OOB) execution support, no-reply, 2017/09/28
- Re: [Qemu-devel] [RFC v2 00/22] QMP: out-of-band (OOB) execution support, no-reply, 2017/09/29