qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wen Congyang <wency@cn.fujitsu.com>
To: qemu-devel <qemu-devel@nongnu.org>,
	Jan Kiszka <jan.kiszka@siemens.com>,
	Dave Anderson <anderson@redhat.com>,
	HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>,
	Luiz Capitulino <lcapitulino@redhat.com>,
	Eric Blake <eblake@redhat.com>
Subject: [Qemu-devel] [RFC][PATCH 16/16 v6] allow user to dump a fraction of the memory
Date: Thu, 09 Feb 2012 11:34:54 +0800	[thread overview]
Message-ID: <4F333EDE.600@cn.fujitsu.com> (raw)
In-Reply-To: <4F333AAA.1070601@cn.fujitsu.com>

Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
---
 dump.c           |  206 ++++++++++++++++++++++++++++++++++++++++--------------
 hmp-commands.hx  |    6 +-
 hmp.c            |   13 +++-
 memory_mapping.c |   27 +++++++
 memory_mapping.h |    2 +
 qapi-schema.json |    6 ++-
 qmp-commands.hx  |    4 +-
 7 files changed, 205 insertions(+), 59 deletions(-)

diff --git a/dump.c b/dump.c
index 6322d1a..60a5180 100644
--- a/dump.c
+++ b/dump.c
@@ -77,6 +77,9 @@ typedef struct DumpState {
     char *error;
     int fd;
     bool detach;
+    bool has_filter;
+    int64_t begin;
+    int64_t length;
     target_phys_addr_t memory_offset;
     int64_t bandwidth;
     RAMBlock *block;
@@ -397,23 +400,82 @@ static int write_memory(DumpState *s, RAMBlock *block, ram_addr_t start,
 
 /* get the memory's offset in the vmcore */
 static target_phys_addr_t get_offset(target_phys_addr_t phys_addr,
-                                     target_phys_addr_t memory_offset)
+                                     DumpState *s)
 {
     RAMBlock *block;
-    target_phys_addr_t offset = memory_offset;
+    target_phys_addr_t offset = s->memory_offset;
+    int64_t size_in_block, start;
+
+    if (s->has_filter) {
+        if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
+            return -1;
+        }
+    }
 
     QLIST_FOREACH(block, &ram_list.blocks, next) {
-        if (phys_addr >= block->offset &&
-            phys_addr < block->offset + block->length) {
-            return phys_addr - block->offset + offset;
+        if (s->has_filter) {
+            if (block->offset >= s->begin + s->length ||
+                block->offset + block->length <= s->begin) {
+                /* This block is out of the range */
+                continue;
+            }
+
+            if (s->begin <= block->offset) {
+                start = block->offset;
+            } else {
+                start = s->begin;
+            }
+
+            size_in_block = block->length - (start - block->offset);
+            if (s->begin + s->length < block->offset + block->length) {
+                size_in_block -= block->offset + block->length -
+                                 (s->begin + s->length);
+            }
+        } else {
+            start = block->offset;
+            size_in_block = block->length;
         }
-        offset += block->length;
+
+        if (phys_addr >= start && phys_addr < start + size_in_block) {
+            return phys_addr - start + offset;
+        }
+
+        offset += size_in_block;
     }
 
     return -1;
 }
 
-static DumpState *dump_init(int fd, bool detach, Error **errp)
+static ram_addr_t get_start_block(DumpState *s)
+{
+    RAMBlock *block;
+
+    if (!s->has_filter) {
+        s->block = QLIST_FIRST(&ram_list.blocks);
+        return 0;
+    }
+
+    QLIST_FOREACH(block, &ram_list.blocks, next) {
+        if (block->offset >= s->begin + s->length ||
+            block->offset + block->length <= s->begin) {
+            /* This block is out of the range */
+            continue;
+        }
+
+        s->block = block;
+        if (s->begin > block->offset ) {
+            s->start = s->begin - block->offset;
+        } else {
+            s->start = 0;
+        }
+        return s->start;
+    }
+
+    return -1;
+}
+
+static DumpState *dump_init(int fd, bool detach, bool has_filter, int64_t begin,
+                            int64_t length, Error **errp)
 {
     CPUState *env;
     DumpState *s = dump_get_current();
@@ -427,10 +489,17 @@ static DumpState *dump_init(int fd, bool detach, Error **errp)
         s->error = NULL;
     }
     s->fd = fd;
-    s->block = QLIST_FIRST(&ram_list.blocks);
-    s->start = 0;
     s->timer = NULL;
     s->detach = detach;
+    s->has_filter = has_filter;
+    s->begin = begin;
+    s->length = length;
+
+    s->start = get_start_block(s);
+    if (s->start == -1) {
+        error_set(errp, QERR_INVALID_PARAMETER, "begin");
+        return NULL;
+    }
 
     /*
      * get dump info: endian, class and architecture.
@@ -454,6 +523,10 @@ static DumpState *dump_init(int fd, bool detach, Error **errp)
     QTAILQ_INIT(&s->list.head);
     get_memory_mapping(&s->list);
 
+    if (s->has_filter) {
+        filter_memory_mapping(&s->list, s->begin, s->length);
+    }
+
     /* crash needs extra memory mapping to determine phys_base. */
     ret = cpu_add_extra_memory_mapping(&s->list);
     if (ret < 0) {
@@ -547,7 +620,7 @@ static int dump_completed(DumpState *s)
     int phdr_index = 1, ret;
 
     QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
-        offset = get_offset(memory_mapping->phys_addr, s->memory_offset);
+        offset = get_offset(memory_mapping->phys_addr, s);
         if (s->dump_info.d_class == ELFCLASS64) {
             ret = write_elf64_load(s, memory_mapping, phdr_index++, offset);
         } else {
@@ -563,6 +636,33 @@ static int dump_completed(DumpState *s)
     return 0;
 }
 
+static int get_next_block(DumpState *s, RAMBlock *block)
+{
+    while (1) {
+        block = QLIST_NEXT(block, next);
+        if (!block) {
+            /* no more block */
+            return 1;
+        }
+
+        s->start = 0;
+        s->block = block;
+        if (s->has_filter) {
+            if (block->offset >= s->begin + s->length ||
+                block->offset + block->length <= s->begin) {
+                /* This block is out of the range */
+                continue;
+            }
+
+            if (s->begin > block->offset) {
+                s->start = s->begin - block->offset;
+            }
+        }
+
+        return 0;
+    }
+}
+
 /*
  * write memory to vmcore.
  *
@@ -573,47 +673,39 @@ static int dump_completed(DumpState *s)
  */
 static int dump_iterate(DumpState *s, int64_t deadline)
 {
-    RAMBlock *block = s->block;
+    RAMBlock *block;
     target_phys_addr_t offset = s->offset;
-    int64_t size, remain, writen_size;
+    int64_t size, writen_size, size_in_block;
     int64_t total = s->bandwidth / 10;
     int ret;
+    bool first = true;
 
-    if ((block->length - s->start) >= total) {
-        size = total;
-    } else {
-        size = block->length - s->start;
-    }
-
-    ret = write_memory(s, block, s->start, &offset, &size, deadline);
-    if (ret < 0) {
-        return -1;
-    }
-
-    if (size == total || ret == 1) {
-        if ((size + s->start) == block->length) {
-            s->block = QLIST_NEXT(block, next);
-            s->start = 0;
+    size = 0;
+    while (size < total) {
+        if (first) {
+            first = false;
         } else {
-            s->start += size;
+            ret = get_next_block(s, block);
+            if (ret == 1) {
+                /* we have finished */
+                return 1;
+            }
         }
-        goto end;
-    }
 
-    while (size < total) {
-        block = QLIST_NEXT(block, next);
-        if (!block) {
-            /* we have finished */
-            return 1;
+        block = s->block;
+        writen_size = total - size;
+        size_in_block = block->length - s->start;
+        if (s->has_filter &&
+            s->begin + s->length < block->offset + block->length) {
+            size_in_block -= block->offset + block->length -
+                             (s->begin + s->length);
         }
 
-        remain = total - size;
-        if (remain >= block->length) {
-            writen_size = block->length;
-        } else {
-            writen_size = remain;
+        if (writen_size >= size_in_block) {
+            writen_size = size_in_block;
         }
-        ret = write_memory(s, block, 0, &offset, &writen_size, deadline);
+
+        ret = write_memory(s, block, s->start, &offset, &writen_size, deadline);
         if (ret < 0) {
             return -1;
         } else if (ret == 1) {
@@ -621,21 +713,17 @@ static int dump_iterate(DumpState *s, int64_t deadline)
         }
         size += writen_size;
     }
-    if (writen_size == block->length) {
-        s->block = QLIST_NEXT(block, next);
-        s->start = 0;
+    if (writen_size == size_in_block) {
+        ret = get_next_block(s, block);
+        if (ret == 1) {
+            /* we have finished */
+            return 1;
+        }
     } else {
-        s->block = block;
-        s->start = writen_size;
+        s->start += writen_size;
     }
 
-end:
     s->offset = offset;
-    if (!s->block) {
-        /* we have finished */
-        return 1;
-    }
-
     return 0;
 }
 
@@ -687,12 +775,22 @@ static void dump_vm_state_change(void *opaque, int running, RunState state)
     }
 }
 
-void qmp_dump(bool detach, const char *file, Error **errp)
+void qmp_dump(bool detach, const char *file, bool has_begin, int64_t begin,
+              bool has_length, int64_t length, Error **errp)
 {
     const char *p;
     int fd = -1;
     DumpState *s;
 
+    if (has_begin && !has_length) {
+        error_set(errp, QERR_MISSING_PARAMETER, "length");
+        return;
+    }
+    if (!has_begin && has_length) {
+        error_set(errp, QERR_MISSING_PARAMETER, "begin");
+        return;
+    }
+
 #if !defined(WIN32)
     if (strstart(file, "fd:", &p)) {
         fd = qemu_get_fd(p);
@@ -716,7 +814,7 @@ void qmp_dump(bool detach, const char *file, Error **errp)
         return;
     }
 
-    s = dump_init(fd, detach, errp);
+    s = dump_init(fd, detach, has_begin, begin, length, errp);
     if (!s) {
         return;
     }
diff --git a/hmp-commands.hx b/hmp-commands.hx
index a026905..388b9ac 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -868,9 +868,11 @@ ETEXI
 
     {
         .name       = "dump",
-        .args_type  = "detach:-d,file:s",
+        .args_type  = "detach:-d,file:s,begin:i?,length:i?",
         .params     = "file",
-        .help       = "dump to file (using -d to not wait for completion)",
+        .help       = "dump to file (using -d to not wait for completion)"
+                      "\n\t\t\t begin(optional): the starting physical address"
+                      "\n\t\t\t length(optional): the memory size, in bytes",
         .user_print = monitor_user_noop,
         .mhandler.cmd = hmp_dump,
     },
diff --git a/hmp.c b/hmp.c
index b36921c..e0c5c62 100644
--- a/hmp.c
+++ b/hmp.c
@@ -857,8 +857,19 @@ void hmp_dump(Monitor *mon, const QDict *qdict)
     Error *errp = NULL;
     bool detach = qdict_get_try_bool(qdict, "detach", 0);
     const char *file = qdict_get_str(qdict, "file");
+    bool has_begin = qdict_haskey(qdict, "begin");
+    bool has_length = qdict_haskey(qdict, "length");
+    int64_t begin = 0;
+    int64_t length = 0;
 
-    qmp_dump(detach, file, &errp);
+    if (has_begin) {
+        begin = qdict_get_int(qdict, "begin");
+    }
+    if (has_length) {
+        length = qdict_get_int(qdict, "length");
+    }
+
+    qmp_dump(detach, file, has_begin, begin, has_length, length, &errp);
     hmp_handle_error(mon, &errp);
 }
 
diff --git a/memory_mapping.c b/memory_mapping.c
index fc0ddee..e2fcf2e 100644
--- a/memory_mapping.c
+++ b/memory_mapping.c
@@ -193,3 +193,30 @@ void get_memory_mapping(MemoryMappingList *list)
 
     return;
 }
+
+void filter_memory_mapping(MemoryMappingList *list, int64_t begin,
+                           int64_t length)
+{
+    MemoryMapping *cur, *next;
+
+    QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
+        if (cur->phys_addr >= begin + length ||
+            cur->phys_addr + cur->length <= begin) {
+            QTAILQ_REMOVE(&list->head, cur, next);
+            list->num--;
+            continue;
+        }
+
+        if (cur->phys_addr < begin) {
+            cur->length -= begin - cur->phys_addr;
+            if (cur->virt_addr) {
+                cur->virt_addr += begin - cur->phys_addr;
+            }
+            cur->phys_addr = begin;
+        }
+
+        if (cur->phys_addr + cur->length > begin + length) {
+            cur->length -= cur->phys_addr + cur->length - begin - length;
+        }
+    }
+}
diff --git a/memory_mapping.h b/memory_mapping.h
index 679f9ef..c7bb4fa 100644
--- a/memory_mapping.h
+++ b/memory_mapping.h
@@ -35,5 +35,7 @@ void add_to_memory_mapping(MemoryMappingList *list,
 
 void free_memory_mapping_list(MemoryMappingList *list);
 void get_memory_mapping(MemoryMappingList *list);
+void filter_memory_mapping(MemoryMappingList *list, int64_t begin,
+                           int64_t length);
 
 #endif
diff --git a/qapi-schema.json b/qapi-schema.json
index 27d5199..aee9efa 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -1590,12 +1590,16 @@
 #
 # @detach: detached dumping.
 # @file: the filename or file descriptor of the vmcore.
+# @begin: if specified, the starting physical address.
+# @length: if specified, the memory size, in bytes.
 #
 # Returns: nothing on success
 #
 # Since: 1.1
 ##
-{ 'command': 'dump', 'data': { 'detach': 'bool', 'file': 'str' } }
+{ 'command': 'dump',
+  'data': { 'detach': 'bool', 'file': 'str', '*begin': 'int',
+            '*length': 'int' } }
 
 ##
 # @dump_cancel
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 1656eea..0c72536 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -567,7 +567,7 @@ EQMP
 
     {
         .name       = "dump",
-        .args_type  = "detach:-d,file:s",
+        .args_type  = "detach:-d,file:s,begin:i?,end:i?",
         .params     = "file",
         .help       = "dump to file (using -d to not wait for completion)",
         .user_print = monitor_user_noop,
@@ -584,6 +584,8 @@ Arguments:
 
 - "detach": detached dumping (json-bool, optional)
 - "file":   Destination file (json-string)
+- "begin":  the starting physical address (json-int)
+- "length": the memory size, in bytes (json-int)
 
 Example:
 
-- 
1.7.1

  parent reply	other threads:[~2012-02-09  3:31 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-09  3:16 [Qemu-devel] [RFC][PATCH 00/16 v6] introducing a new, dedicated memory dump mechanism Wen Congyang
2012-02-09  3:19 ` [Qemu-devel] [RFC][PATCH 01/16 v6] monitor: introduce qemu_suspend_monitor()/qemu_resume_monitor() Wen Congyang
2012-02-14 16:19   ` Jan Kiszka
2012-02-15  2:54     ` Wen Congyang
2012-02-15  8:51       ` Jan Kiszka
2012-02-15 13:01         ` Luiz Capitulino
2012-02-16  1:35           ` Wen Congyang
2012-02-09  3:20 ` [Qemu-devel] [RFC][PATCH 02/16 v6] Add API to create memory mapping list Wen Congyang
2012-02-14 16:39   ` Jan Kiszka
2012-02-15  3:00     ` Wen Congyang
2012-02-09  3:21 ` [Qemu-devel] [RFC][PATCH 03/16 v6] Add API to check whether a physical address is I/O address Wen Congyang
2012-02-14 16:52   ` Jan Kiszka
2012-02-15  3:03     ` Wen Congyang
2012-02-09  3:21 ` [Qemu-devel] [RFC][PATCH 04/16 v6] target-i386: implement cpu_get_memory_mapping() Wen Congyang
2012-02-14 17:07   ` Jan Kiszka
2012-02-15  3:05     ` Wen Congyang
2012-02-09  3:22 ` [Qemu-devel] [RFC][PATCH 05/16 v6] Add API to get memory mapping Wen Congyang
2012-02-14 17:21   ` Jan Kiszka
2012-02-15  4:07     ` Wen Congyang
2012-02-15  9:17       ` Jan Kiszka
2012-02-15  9:41         ` Wen Congyang
2012-02-15  9:47           ` HATAYAMA Daisuke
2012-02-15 10:19             ` Jan Kiszka
2012-02-09  3:24 ` [Qemu-devel] [RFC][PATCH 06/16 v6] target-i386: Add API to write elf notes to core file Wen Congyang
2012-02-14 17:31   ` Jan Kiszka
2012-02-15  3:16     ` Wen Congyang
2012-02-09  3:24 ` [Qemu-devel] [RFC][PATCH 07/16 v6] target-i386: Add API to add extra memory mapping Wen Congyang
2012-02-14 17:35   ` Jan Kiszka
2012-02-15  5:19     ` Wen Congyang
2012-02-15  9:21       ` Jan Kiszka
2012-02-15  9:44         ` Wen Congyang
2012-02-15 10:21           ` Jan Kiszka
2012-02-17  9:32             ` Wen Congyang
2012-02-17 11:34               ` HATAYAMA Daisuke
2012-02-09  3:26 ` [Qemu-devel] [RFC][PATCH 08/16 v6] target-i386: add API to get dump info Wen Congyang
2012-02-14 17:39   ` Jan Kiszka
2012-02-15  3:30     ` Wen Congyang
2012-02-15  9:05       ` Jan Kiszka
2012-02-15  9:10         ` Wen Congyang
2012-02-15  9:12   ` Peter Maydell
2012-02-15  9:19     ` Wen Congyang
2012-02-09  3:28 ` [Qemu-devel] [RFC][PATCH 09/16 v6] introduce a new monitor command 'dump' to dump guest's memory Wen Congyang
2012-02-14 17:59   ` Jan Kiszka
2012-02-15  3:44     ` Wen Congyang
2012-02-17  8:52     ` Wen Congyang
2012-02-17  9:26       ` Jan Kiszka
2012-02-17  9:35         ` Wen Congyang
2012-02-17  9:35           ` Jan Kiszka
2012-02-17 16:32       ` Eric Blake
2012-02-17 16:51         ` Jan Kiszka
2012-02-17 17:05           ` Eric Blake
2012-02-09  3:28 ` [Qemu-devel] [RFC][PATCH 10/16 v6] run dump at the background Wen Congyang
2012-02-14 18:05   ` Jan Kiszka
2012-02-14 18:27     ` Jan Kiszka
2012-02-15  3:47       ` Wen Congyang
2012-02-15  9:07         ` Jan Kiszka
2012-02-15  9:22           ` Wen Congyang
2012-02-15  9:21             ` Jan Kiszka
2012-02-15  9:35               ` Wen Congyang
2012-02-15 10:16                 ` Jan Kiszka
2012-02-09  3:29 ` [Qemu-devel] [RFC][PATCH 11/16 v6] support detached dump Wen Congyang
2012-02-09  3:30 ` [Qemu-devel] [RFC][PATCH 12/16 v6] support to cancel the current dumping Wen Congyang
2012-02-09  3:32 ` [Qemu-devel] [RFC][PATCH 13/16 v6] support to set dumping speed Wen Congyang
2012-02-09  3:32 ` [Qemu-devel] [RFC][PATCH 14/16 v6] support to query dumping status Wen Congyang
2012-02-09  3:33 ` [Qemu-devel] [RFC][PATCH 15/16 v6] auto cancel dumping after vm state is changed to run Wen Congyang
2012-02-09  3:34 ` Wen Congyang [this message]
2012-02-14 18:27   ` [Qemu-devel] [RFC][PATCH 16/16 v6] allow user to dump a fraction of the memory Jan Kiszka
2012-02-13  1:45 ` [Qemu-devel] [RFC][PATCH 00/16 v6] introducing a new, dedicated memory dump mechanism Wen Congyang

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=4F333EDE.600@cn.fujitsu.com \
    --to=wency@cn.fujitsu.com \
    --cc=anderson@redhat.com \
    --cc=d.hatayama@jp.fujitsu.com \
    --cc=eblake@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=lcapitulino@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).