qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: libvir-list@redhat.com, Anthony Liguori <aliguori@us.ibm.com>,
	Jan Kiszka <jan.kiszka@web.de>,
	Hollis Blanchard <hollisb@us.ibm.com>
Subject: [Qemu-devel] [PATCH 2/3] Introduce monitor 'wait' command
Date: Wed,  8 Apr 2009 09:16:43 -0500	[thread overview]
Message-ID: <1239200204-4934-2-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1239200204-4934-1-git-send-email-aliguori@us.ibm.com>

The wait command will pause the monitor the command was issued in until a new
event becomes available.  Events are queued if there isn't a waiter present.
The wait command completes after a single event is available.

Today, we queue events indefinitely but in the future, I suspect we'll drop
events that are older than a certain amount of time to avoid infinitely
allocating memory for long running VMs.

To make use of the new notification mechanism, this patch introduces a
qemu_notify_event() API.  This API takes three parameters: a class which is
meant to classify the type of event being generated, a name which is meant to
distinguish which event in the class that has been generated, and a details
parameters which is meant to allow events to send arbitrary data with a given
event.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

diff --git a/Makefile b/Makefile
index 633774e..b87870b 100644
--- a/Makefile
+++ b/Makefile
@@ -84,7 +84,7 @@ OBJS+=usb-serial.o usb-net.o
 OBJS+=sd.o ssi-sd.o
 OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
 OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
-OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o
+OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o wait.o
 
 ifdef CONFIG_BRLAPI
 OBJS+= baum.o
diff --git a/monitor.c b/monitor.c
index ca1c11c..5245f7c 100644
--- a/monitor.c
+++ b/monitor.c
@@ -42,6 +42,7 @@
 #include "migration.h"
 #include "kvm.h"
 #include "acl.h"
+#include "wait.h"
 
 //#define DEBUG
 //#define DEBUG_COMPLETION
@@ -1745,6 +1746,7 @@ static const mon_cmd_t mon_cmds[] = {
                                "acl allow vnc.username fred\n"
                                "acl deny vnc.username bob\n"
                                "acl reset vnc.username\n" },
+    { "wait", "", do_wait, "", "wait for an asynchronous event to occur" },
     { NULL, NULL, },
 };
 
diff --git a/wait.c b/wait.c
new file mode 100644
index 0000000..092433a
--- /dev/null
+++ b/wait.c
@@ -0,0 +1,97 @@
+/*
+ * Asynchronous monitor notification support
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "monitor.h"
+#include "sys-queue.h"
+#include "osdep.h"
+#include "wait.h"
+
+typedef struct WaitEvent
+{
+    qemu_timeval timestamp;
+    char *class;
+    char *name;
+    char *details;
+    TAILQ_ENTRY(WaitEvent) node;
+} WaitEvent;
+
+typedef struct PendingWaiter
+{
+    Monitor *mon;
+    TAILQ_ENTRY(PendingWaiter) node;
+} PendingWaiter;
+
+static TAILQ_HEAD(, WaitEvent) pending_events = TAILQ_HEAD_INITIALIZER(pending_events);
+static TAILQ_HEAD(, PendingWaiter) pending_waiters = TAILQ_HEAD_INITIALIZER(pending_waiters);
+
+static void dispatch_event(PendingWaiter *w, WaitEvent *e)
+{
+    monitor_printf(w->mon, "%ld.%06ld: %s: %s\n%s",
+                   e->timestamp.tv_sec, e->timestamp.tv_usec,
+                   e->class, e->name,
+                   e->details);
+    monitor_resume(w->mon);
+}
+
+static void try_to_process_events(void)
+{
+    while (!TAILQ_EMPTY(&pending_events) && !TAILQ_EMPTY(&pending_waiters)) {
+        WaitEvent *e;
+        PendingWaiter *w;
+
+        e = TAILQ_FIRST(&pending_events);
+        TAILQ_REMOVE(&pending_events, e, node);
+
+        w = TAILQ_FIRST(&pending_waiters);
+        TAILQ_REMOVE(&pending_waiters, w, node);
+
+        dispatch_event(w, e);
+
+        qemu_free(e->details);
+        qemu_free(e->name);
+        qemu_free(e->class);
+
+        qemu_free(w);
+    }
+}
+
+void qemu_notify_event(const char *class, const char *name, const char *details)
+{
+    WaitEvent *e;
+
+    e = qemu_mallocz(sizeof(*e));
+
+    qemu_gettimeofday(&e->timestamp);
+    e->class = qemu_strdup(class);
+    e->name = qemu_strdup(name);
+    e->details = qemu_strdup(details);
+
+    TAILQ_INSERT_TAIL(&pending_events, e, node);
+
+    try_to_process_events();
+}
+
+void do_wait(Monitor *mon)
+{
+    PendingWaiter *w;
+
+    w = qemu_mallocz(sizeof(*w));
+    w->mon = mon;
+
+    TAILQ_INSERT_TAIL(&pending_waiters, w, node);
+
+    monitor_suspend(w->mon);
+
+    try_to_process_events();
+}
diff --git a/wait.h b/wait.h
new file mode 100644
index 0000000..51b6467
--- /dev/null
+++ b/wait.h
@@ -0,0 +1,22 @@
+/*
+ * Asynchronous monitor notification support
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_WAIT_H
+#define QEMU_WAIT_H
+
+#include "monitor.h"
+
+void qemu_notify_event(const char *class, const char *name, const char *details);
+void do_wait(Monitor *mon);
+
+#endif

  reply	other threads:[~2009-04-08 14:16 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-08 14:16 [Qemu-devel] [PATCH 1/3] Allow multiple monitor devices Anthony Liguori
2009-04-08 14:16 ` Anthony Liguori [this message]
2009-04-08 14:33   ` [Qemu-devel] Re: [PATCH 2/3] Introduce monitor 'wait' command Daniel P. Berrange
2009-04-08 14:39     ` Anthony Liguori
2009-04-08 15:03     ` [Qemu-devel] Re: [libvirt] " Gerd Hoffmann
2009-04-08 15:25       ` Jan Kiszka
2009-04-08 17:44       ` Anthony Liguori
2009-04-08 19:06         ` Jamie Lokier
2009-04-08 19:35           ` Anthony Liguori
2009-04-08 20:28             ` Hollis Blanchard
2009-04-08 21:14               ` Anthony Liguori
2009-04-08 21:31                 ` Hollis Blanchard
2009-04-09 13:59                   ` Anthony Liguori
2009-04-08 21:39                 ` Paul Brook
2009-04-09  8:24                   ` Avi Kivity
2009-04-09 13:56                   ` Anthony Liguori
2009-04-09 17:12                     ` Jamie Lokier
2009-04-08 21:27             ` Zachary Amsden
2009-04-09  9:55           ` Daniel P. Berrange
2009-04-09 17:13             ` Jamie Lokier
2009-04-09  9:44         ` Gerd Hoffmann
2009-04-09 13:31           ` Anthony Liguori
2009-04-08 14:16 ` [Qemu-devel] [PATCH 3/3] Implement vm-state notifications Anthony Liguori
2009-04-08 14:27 ` [Qemu-devel] Re: [PATCH 1/3] Allow multiple monitor devices Jan Kiszka

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=1239200204-4934-2-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.com \
    --cc=hollisb@us.ibm.com \
    --cc=jan.kiszka@web.de \
    --cc=libvir-list@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).