Skip to contentSkip to conversation

Add basic "detach" support for dump-guest-memory

PATCH New Peter Xu 1 participant 3 messages Nov 23, 2015

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover Add basic "detach" support for dump-guest-memory

Currently, dump-guest-memory supports synchronous operation only. This patch sets are adding "detach" support for it (just like "migrate -d" for migration). When "-d" is provided, dump-guest-memory command will return immediately without hanging user. This should be useful when the backend storage for the dump file is very slow.

Peter Xu (2):
dump-guest-memory: add "detach" flag for QMP/HMP interfaces
dump-guest-memory: add basic "detach" support.

 dump.c                | 62 ++++++++++++++++++++++++++++++++++++++++++++++-----
 hmp-commands.hx       |  5 +++--
 hmp.c                 |  3 ++-
 include/sysemu/dump.h |  4 ++++
 qapi-schema.json      |  3 ++-
 qmp-commands.hx       |  4 ++--
 qmp.c                 |  9 ++++++++
 vl.c                  |  3 +++
 8 files changed, 81 insertions(+), 12 deletions(-)
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH 1/2 dump-guest-memory: add "detach" flag for QMP/HMP interfaces

This patch only add the interfaces, but not implementing them.

---
 dump.c           | 3 ++-
 hmp-commands.hx  | 5 +++--
 hmp.c            | 3 ++-
 qapi-schema.json | 3 ++-
 qmp-commands.hx  | 4 ++--
 5 files changed, 11 insertions(+), 7 deletions(-)
dump.c +2 -1
index 78b7d84..3ec3423 100644
@@ -1598,7 +1598,8 @@ cleanup:
 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
                            int64_t begin, bool has_length,
                            int64_t length, bool has_format,
-                           DumpGuestMemoryFormat format, Error **errp)
+                           DumpGuestMemoryFormat format, bool detach,
+                           Error **errp)
 {
     const char *p;
     int fd = -1;
hmp-commands.hx +3 -2
index bb52e4d..8e27671 100644
@@ -1056,10 +1056,11 @@ ETEXI
 
     {
         .name       = "dump-guest-memory",
-        .args_type  = "paging:-p,zlib:-z,lzo:-l,snappy:-s,filename:F,begin:i?,length:i?",
-        .params     = "[-p] [-z|-l|-s] filename [begin length]",
+        .args_type  = "paging:-p,detach:-d,zlib:-z,lzo:-l,snappy:-s,filename:F,begin:i?,length:i?",
+        .params     = "[-p] [-d] [-z|-l|-s] filename [begin length]",
         .help       = "dump guest memory into file 'filename'.\n\t\t\t"
                       "-p: do paging to get guest's memory mapping.\n\t\t\t"
+                      "-d: return immediately (not wait for completion).\n\t\t\t"
                       "-z: dump in kdump-compressed format, with zlib compression.\n\t\t\t"
                       "-l: dump in kdump-compressed format, with lzo compression.\n\t\t\t"
                       "-s: dump in kdump-compressed format, with snappy compression.\n\t\t\t"
hmp.c +2 -1
index 2140605..4c5480d 100644
@@ -1580,6 +1580,7 @@ void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
 {
     Error *err = NULL;
     bool paging = qdict_get_try_bool(qdict, "paging", false);
+    bool detach = qdict_get_try_bool(qdict, "detach", false);
     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
@@ -1619,7 +1620,7 @@ void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
     prot = g_strconcat("file:", file, NULL);
 
     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
-                          true, dump_format, &err);
+                          true, dump_format, detach, &err);
     hmp_handle_error(mon, &err);
     g_free(prot);
 }
qapi-schema.json +2 -1
index 8b1a423..1211082 100644
@@ -2132,7 +2132,8 @@
 ##
 { 'command': 'dump-guest-memory',
   'data': { 'paging': 'bool', 'protocol': 'str', '*begin': 'int',
-            '*length': 'int', '*format': 'DumpGuestMemoryFormat' } }
+            '*length': 'int', '*format': 'DumpGuestMemoryFormat',
+            'detach': 'bool'} }
 
 ##
 # @DumpGuestMemoryCapability:
qmp-commands.hx +2 -2
index 9d8b42f..86864f6 100644
@@ -840,8 +840,8 @@ EQMP
 
     {
         .name       = "dump-guest-memory",
-        .args_type  = "paging:b,protocol:s,begin:i?,end:i?,format:s?",
-        .params     = "-p protocol [begin] [length] [format]",
+        .args_type  = "paging:b,detach:d,protocol:s,begin:i?,end:i?,format:s?",
+        .params     = "-p protocol [-d] [begin] [length] [format]",
         .help       = "dump guest memory to file",
         .mhandler.cmd_new = qmp_marshal_dump_guest_memory,
     },
Signature · 1 line
2.4.3
author
#2 lore raw

PATCH 2/2 dump-guest-memory: add basic "detach" support.

In reply to Peter Xu

This will allow the user specify "-d" (just like command "migrate") when using "dump-guest-memory" command. When specified, one background thread is created to do the dump work. One flag is added to show whether there is a background dump work in progress.

---
 dump.c                | 59 ++++++++++++++++++++++++++++++++++++++++++++++-----
 include/sysemu/dump.h |  4 ++++
 qmp.c                 |  9 ++++++++
 vl.c                  |  3 +++
 4 files changed, 70 insertions(+), 5 deletions(-)
dump.c +54 -5
index 3ec3423..c2e14cd 100644
@@ -1442,6 +1442,9 @@ static void dump_init(DumpState *s, int fd, bool has_format,
     Error *err = NULL;
     int ret;
 
+    s->has_format = has_format;
+    s->format = format;
+
     /* kdump-compressed is conflict with paging and filter */
     if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
         assert(!paging && !has_filter);
@@ -1595,6 +1598,45 @@ cleanup:
     dump_cleanup(s);
 }
 
+/* whether there is a dump in progress */
+extern bool dump_in_progress_p;
+
+static bool dump_ownership_set(bool value)
+{
+    return atomic_xchg(&dump_in_progress_p, value);
+}
+
+/* return true when owner taken, false otherwise */
+static bool dump_ownership_take(void)
+{
+    bool ret = dump_ownership_set(1);
+    return ret == 0;
+}
+
+/* we should only call this after all dump work finished */
+static void dump_ownership_release(void)
+{
+    dump_ownership_set(0);
+}
+
+static void dump_process(DumpState *s, Error **errp)
+{
+    if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
+        create_kdump_vmcore(s, errp);
+    } else {
+        create_vmcore(s, errp);
+    }
+    dump_ownership_release();
+}
+
+static void *dump_thread(void *data)
+{
+    DumpState *s = (DumpState *)data;
+    dump_process(s, NULL);
+    g_free(s);
+    return NULL;
+}
+
 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
                            int64_t begin, bool has_length,
                            int64_t length, bool has_format,
@@ -1662,6 +1704,12 @@ void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
         return;
     }
 
+    /* we could only allow one dump at a time. */
+    if (!dump_ownership_take()) {
+        error_setg(errp, "another dump is in progress.");
+        return;
+    }
+
     s = g_malloc0(sizeof(DumpState));
 
     dump_init(s, fd, has_format, format, paging, has_begin,
@@ -1672,13 +1720,14 @@ void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
         return;
     }
 
-    if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
-        create_kdump_vmcore(s, errp);
+    if (!detach) {
+        dump_process(s, errp);
+        g_free(s);
     } else {
-        create_vmcore(s, errp);
+        qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
+                           s, QEMU_THREAD_DETACHED);
+        /* DumpState is freed in dump thread */
     }
-
-    g_free(s);
 }
 
 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
include/sysemu/dump.h +4 -0
index 7e4ec5c..2aeffc8 100644
@@ -183,6 +183,10 @@ typedef struct DumpState {
     off_t offset_page;          /* offset of page part in vmcore */
     size_t num_dumpable;        /* number of page that can be dumped */
     uint32_t flag_compress;     /* indicate the compression format */
+
+    QemuThread dump_thread;     /* only used when do async dump */
+    bool has_format;            /* whether format is provided */
+    DumpGuestMemoryFormat format; /* valid only if has_format == true */
 } DumpState;
 
 uint16_t cpu_to_dump16(DumpState *s, uint16_t val);
qmp.c +9 -0
index 0a1fa19..ea57b57 100644
@@ -168,12 +168,21 @@ SpiceInfo *qmp_query_spice(Error **errp)
 };
 #endif
 
+extern bool dump_in_progress_p;
+
 void qmp_cont(Error **errp)
 {
     Error *local_err = NULL;
     BlockBackend *blk;
     BlockDriverState *bs;
 
+    /* if there is a dump in background, we should wait until the dump
+     * finished */
+    if (dump_in_progress_p) {
+        error_setg(errp, "there is a dump in process, please wait.");
+        return;
+    }
+
     if (runstate_needs_reset()) {
         error_setg(errp, "Resetting the Virtual Machine is required");
         return;
vl.c +3 -0
index 525929b..ef7a936 100644
@@ -204,6 +204,9 @@ bool xen_allowed;
 uint32_t xen_domid;
 enum xen_mode xen_mode = XEN_EMULATE;
 
+/* whether dump is in progress */
+bool dump_in_progress_p = false;
+
 static int has_defaults = 1;
 static int default_serial = 1;
 static int default_parallel = 1;
Signature · 1 line
2.4.3