9ae3a8
From 714dfa20586988e535dc7290be0d5d8d6b0853f3 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Wed, 15 Jan 2014 15:39:10 +0100
9ae3a8
Subject: [PATCH 33/37] qemu-io: New command 'sleep'
9ae3a8
9ae3a8
Message-id: <1392117622-28812-34-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 57198
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 33/37] qemu-io: New command 'sleep'
9ae3a8
Bugzilla: 748906
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
There is no easy way to check that a request correctly waits for a
9ae3a8
different request. With a sleep command we can at least approximate it.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit cd33d02a1012e58ee0d3c8259159e8c60cfa0a4d)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	qemu-io-cmds.c
9ae3a8
9ae3a8
Conflicts because RHEL 7 doesn't have...
9ae3a8
- ...the qemu-io refactoring for supporting qemu-io from the monitor
9ae3a8
- ...the new timer API
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 qemu-io.c | 42 ++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 42 insertions(+)
9ae3a8
---
9ae3a8
 qemu-io.c |   42 ++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 files changed, 42 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/qemu-io.c b/qemu-io.c
9ae3a8
index 7e258a5..0959178 100644
9ae3a8
--- a/qemu-io.c
9ae3a8
+++ b/qemu-io.c
9ae3a8
@@ -22,6 +22,7 @@
9ae3a8
 #include "block/qapi.h"
9ae3a8
 #include "cmd.h"
9ae3a8
 #include "trace/control.h"
9ae3a8
+#include "qemu/timer.h"
9ae3a8
 
9ae3a8
 #define VERSION	"0.0.1"
9ae3a8
 
9ae3a8
@@ -1773,6 +1774,46 @@ static const cmdinfo_t close_cmd = {
9ae3a8
     .oneline    = "close the current open file",
9ae3a8
 };
9ae3a8
 
9ae3a8
+static void sleep_cb(void *opaque)
9ae3a8
+{
9ae3a8
+    bool *expired = opaque;
9ae3a8
+    *expired = true;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static int sleep_f(int argc, char **argv)
9ae3a8
+{
9ae3a8
+    char *endptr;
9ae3a8
+    long ms;
9ae3a8
+    struct QEMUTimer *timer;
9ae3a8
+    bool expired = false;
9ae3a8
+
9ae3a8
+    ms = strtol(argv[1], &endptr, 0);
9ae3a8
+    if (ms < 0 || *endptr != '\0') {
9ae3a8
+        printf("%s is not a valid number\n", argv[1]);
9ae3a8
+        return 0;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    timer = qemu_new_timer_ns(host_clock, sleep_cb, &expired);
9ae3a8
+    qemu_mod_timer(timer, qemu_get_clock_ns(host_clock) + SCALE_MS * ms);
9ae3a8
+
9ae3a8
+    while (!expired) {
9ae3a8
+        main_loop_wait(false);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    qemu_free_timer(timer);
9ae3a8
+
9ae3a8
+    return 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const cmdinfo_t sleep_cmd = {
9ae3a8
+       .name           = "sleep",
9ae3a8
+       .argmin         = 1,
9ae3a8
+       .argmax         = 1,
9ae3a8
+       .cfunc          = sleep_f,
9ae3a8
+       .flags          = CMD_NOFILE_OK,
9ae3a8
+       .oneline        = "waits for the given value in milliseconds",
9ae3a8
+};
9ae3a8
+
9ae3a8
 static int openfile(char *name, int flags, int growable, QDict *opts)
9ae3a8
 {
9ae3a8
     Error *local_err = NULL;
9ae3a8
@@ -2052,6 +2093,7 @@ int main(int argc, char **argv)
9ae3a8
     add_command(&resume_cmd);
9ae3a8
     add_command(&wait_break_cmd);
9ae3a8
     add_command(&abort_cmd);
9ae3a8
+    add_command(&sleep_cmd);
9ae3a8
 
9ae3a8
     add_args_command(init_args_command);
9ae3a8
     add_check_command(init_check_command);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8