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 14/16 v8] run dump at the background
Date: Fri, 02 Mar 2012 18:44:52 +0800 [thread overview]
Message-ID: <4F50A4A4.3070201@cn.fujitsu.com> (raw)
In-Reply-To: <4F509A00.80207@cn.fujitsu.com>
The new monitor command dump may take long time to finish. So we need run it
at the background.
Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
---
dump.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
vl.c | 5 +-
2 files changed, 150 insertions(+), 23 deletions(-)
diff --git a/dump.c b/dump.c
index d569867..4c8038d 100644
--- a/dump.c
+++ b/dump.c
@@ -80,9 +80,21 @@ typedef struct DumpState {
bool resume;
char *error;
target_phys_addr_t memory_offset;
+
+ /*
+ * Return value:
+ * -2: EAGAIN
+ * -1: error
+ * 0: success
+ */
write_core_dump_function f;
void (*cleanup)(void *opaque);
+ int (*dump_begin_iterate)(struct DumpState *, void *opaque);
void *opaque;
+ RAMBlock *block;
+ ram_addr_t start;
+ target_phys_addr_t offset;
+ VMChangeStateEntry *handler;
} DumpState;
static DumpState *dump_get_current(void)
@@ -100,6 +112,12 @@ static int dump_cleanup(DumpState *s)
memory_mapping_list_free(&s->list);
s->cleanup(s->opaque);
+
+ if (s->handler) {
+ qemu_del_vm_change_state_handler(s->handler);
+ s->handler = NULL;
+ }
+
if (s->resume) {
vm_start();
}
@@ -373,40 +391,70 @@ static int write_elf_section(DumpState *s, target_phys_addr_t *offset, int type)
return 0;
}
+/*
+ * Return value:
+ * -2: blocked
+ * -1: failed
+ * 0: sucess
+ */
static int write_data(DumpState *s, void *buf, int length,
target_phys_addr_t *offset)
{
int ret;
ret = s->f(*offset, buf, length, s->opaque);
- if (ret < 0) {
+ if (ret == -1) {
dump_error(s, "dump: failed to save memory.\n");
return -1;
}
+ if (ret == -2) {
+ return -2;
+ }
+
*offset += length;
return 0;
}
/* write the memroy to vmcore. 1 page per I/O. */
-static int write_memory(DumpState *s, RAMBlock *block,
- target_phys_addr_t *offset)
+static int write_memory(DumpState *s, RAMBlock *block, ram_addr_t start,
+ target_phys_addr_t *offset, int64_t *size,
+ int64_t deadline)
{
int i, ret;
+ int64_t writen_size = 0;
+ int64_t time;
- for (i = 0; i < block->length / TARGET_PAGE_SIZE; i++) {
- ret = write_data(s, block->host + i * TARGET_PAGE_SIZE,
+ *size = block->length - start;
+ for (i = 0; i < *size / TARGET_PAGE_SIZE; i++) {
+ ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE,
TARGET_PAGE_SIZE, offset);
if (ret < 0) {
- return -1;
+ *size = writen_size;
+ return ret;
+ }
+
+ writen_size += TARGET_PAGE_SIZE;
+ time = qemu_get_clock_ms(rt_clock);
+ if (time >= deadline) {
+ /* time out */
+ *size = writen_size;
+ return -2;
}
}
- if ((block->length % TARGET_PAGE_SIZE) != 0) {
- ret = write_data(s, block->host + i * TARGET_PAGE_SIZE,
- block->length % TARGET_PAGE_SIZE, offset);
+ if ((*size % TARGET_PAGE_SIZE) != 0) {
+ ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE,
+ *size % TARGET_PAGE_SIZE, offset);
if (ret < 0) {
- return -1;
+ *size = writen_size;
+ return ret;
+ }
+
+ time = qemu_get_clock_ms(rt_clock);
+ if (time >= deadline) {
+ /* time out */
+ return -2;
}
}
@@ -501,6 +549,7 @@ static int dump_begin(DumpState *s)
}
s->memory_offset = offset;
+ s->offset = offset;
return 0;
}
@@ -528,22 +577,65 @@ static int dump_completed(DumpState *s)
return 0;
}
-/* write all memory to vmcore */
-static int dump_iterate(DumpState *s)
+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;
+
+ return 0;
+ }
+}
+
+/* write memory to vmcore */
+static void dump_iterate(void *opaque)
{
+ DumpState *s = opaque;
RAMBlock *block;
- target_phys_addr_t offset = s->memory_offset;
+ target_phys_addr_t offset = s->offset;
+ int64_t size;
+ int64_t deadline, now;
int ret;
- /* write all memory to vmcore */
- QLIST_FOREACH(block, &ram_list.blocks, next) {
- ret = write_memory(s, block, &offset);
- if (ret < 0) {
- return -1;
+ now = qemu_get_clock_ms(rt_clock);
+ deadline = now + 5;
+ while(1) {
+ block = s->block;
+ ret = write_memory(s, block, s->start, &offset, &size, deadline);
+ if (ret == -1) {
+ return;
+ }
+
+ if (ret == -2) {
+ break;
+ }
+
+ ret = get_next_block(s, block);
+ if (ret == 1) {
+ dump_completed(s);
+ return;
}
}
- return dump_completed(s);
+ if (size == block->length - s->start) {
+ ret = get_next_block(s, block);
+ if (ret == 1) {
+ dump_completed(s);
+ return;
+ }
+ } else {
+ s->start += size;
+ }
+
+ s->offset = offset;
+
+ return;
}
static int create_vmcore(DumpState *s)
@@ -555,7 +647,7 @@ static int create_vmcore(DumpState *s)
return -1;
}
- ret = dump_iterate(s);
+ ret = s->dump_begin_iterate(s, s->opaque);
if (ret < 0) {
return -1;
}
@@ -563,6 +655,17 @@ static int create_vmcore(DumpState *s)
return 0;
}
+static void dump_vm_state_change(void *opaque, int running, RunState state)
+{
+ DumpState *s = opaque;
+
+ if (running) {
+ qmp_dump_cancel(NULL);
+ s->state = DUMP_STATE_ERROR;
+ s->error = g_strdup("vm state is changed to run\n");
+ }
+}
+
static DumpState *dump_init(bool paging, Error **errp)
{
CPUState *env;
@@ -580,6 +683,9 @@ static DumpState *dump_init(bool paging, Error **errp)
g_free(s->error);
s->error = NULL;
}
+ s->block = QLIST_FIRST(&ram_list.blocks);
+ s->start = 0;
+ s->handler = qemu_add_vm_change_state_handler(dump_vm_state_change, s);
/*
* get dump info: endian, class and architecture.
@@ -639,14 +745,24 @@ static int fd_write_vmcore(target_phys_addr_t offset, void *buf, size_t size,
ret = lseek(fd, offset, SEEK_SET);
if (ret < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ return -2;
+ }
return -1;
}
ret = write(fd, buf, size);
- if (ret != size) {
+ if (ret < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ return -2;
+ }
return -1;
}
+ if (ret != size) {
+ return -2;
+ }
+
return 0;
}
@@ -655,10 +771,18 @@ static void fd_cleanup(void *opaque)
int fd = (int)(intptr_t)opaque;
if (fd != -1) {
+ qemu_set_fd_handler(fd, NULL, NULL, NULL);
close(fd);
}
}
+static int fd_dump_begin_iterate(DumpState *s, void *opaque)
+{
+ int fd = (int)(intptr_t)opaque;
+
+ return qemu_set_fd_handler(fd, NULL, dump_iterate, s);
+}
+
static DumpState *dump_init_fd(int fd, bool paging, Error **errp)
{
DumpState *s = dump_init(paging, errp);
@@ -669,7 +793,9 @@ static DumpState *dump_init_fd(int fd, bool paging, Error **errp)
s->f = fd_write_vmcore;
s->cleanup = fd_cleanup;
+ s->dump_begin_iterate = fd_dump_begin_iterate;
s->opaque = (void *)(intptr_t)fd;
+ fcntl(fd, F_SETFL, O_NONBLOCK);
return s;
}
diff --git a/vl.c b/vl.c
index 4a77696..0f31906 100644
--- a/vl.c
+++ b/vl.c
@@ -1249,11 +1249,12 @@ void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
void vm_state_notify(int running, RunState state)
{
- VMChangeStateEntry *e;
+ VMChangeStateEntry *e, *next;
trace_vm_state_notify(running, state);
- for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
+ /* e->cb() may remove itself */
+ QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
e->cb(e->opaque, running, state);
}
}
--
1.7.1
next prev parent reply other threads:[~2012-03-02 10:44 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-02 9:59 [Qemu-devel] [RFC][PATCH 00/16 v8] introducing a new, dedicated memory dump mechanism Wen Congyang
2012-03-02 10:02 ` [Qemu-devel] [RFC][PATCH 01/16 v8] Add API to create memory mapping list Wen Congyang
2012-03-02 10:06 ` [Qemu-devel] [RFC][PATCH 02/16 v8] Add API to check whether a physical address is I/O address Wen Congyang
2012-03-02 10:08 ` [Qemu-devel] [RFC][PATCH 03/16 v8] implement cpu_get_memory_mapping() Wen Congyang
2012-03-02 10:12 ` [Qemu-devel] [RFC][PATCH 04/16 v8] Add API to check whether paging mode is enabled Wen Congyang
2012-03-02 10:18 ` [Qemu-devel] [RFC][PATCH 05/16 v8] Add API to get memory mapping Wen Congyang
2012-03-07 15:27 ` HATAYAMA Daisuke
2012-03-08 8:52 ` Wen Congyang
2012-03-09 0:40 ` HATAYAMA Daisuke
2012-03-09 1:46 ` Wen Congyang
2012-03-09 2:05 ` HATAYAMA Daisuke
2012-03-09 2:26 ` Wen Congyang
2012-03-09 2:53 ` HATAYAMA Daisuke
2012-03-09 9:41 ` Jan Kiszka
2012-03-09 9:57 ` Wen Congyang
2012-03-09 10:05 ` Jan Kiszka
2012-03-09 10:06 ` Jan Kiszka
2012-03-09 12:53 ` HATAYAMA Daisuke
2012-03-09 13:24 ` Jan Kiszka
2012-03-12 6:16 ` HATAYAMA Daisuke
2012-03-12 6:26 ` HATAYAMA Daisuke
2012-03-12 1:52 ` Wen Congyang
2012-03-09 9:43 ` Jan Kiszka
2012-03-02 10:23 ` [Qemu-devel] [RFC][PATCH 06/16 v8] Add API to get memory mapping without doing paging Wen Congyang
2012-03-02 10:27 ` [Qemu-devel] [RFC][PATCH 07/16 v8] target-i386: Add API to write elf notes to core file Wen Congyang
2012-03-02 10:31 ` [Qemu-devel] [RFC][PATCH 08/16 v8] target-i386: Add API to write cpu status " Wen Congyang
2012-03-02 10:33 ` [Qemu-devel] [RFC][PATCH 09/16 v8] target-i386: add API to get dump info Wen Congyang
2012-03-02 10:38 ` [Qemu-devel] [RFC][PATCH 10/16 v8] make gdb_id() generally avialable Wen Congyang
2012-03-02 10:42 ` [Qemu-devel] [RFC][PATCH 11/16 v8] introduce a new monitor command 'dump' to dump guest's memory Wen Congyang
2012-03-02 10:43 ` [Qemu-devel] [RFC][PATCH 12/16 v8] support to cancel the current dumping Wen Congyang
2012-03-02 10:44 ` [Qemu-devel] [RFC][PATCH 13/16 v8] support to query dumping status Wen Congyang
2012-03-02 10:44 ` Wen Congyang [this message]
2012-03-02 10:45 ` [Qemu-devel] [RFC][PATCH 15/16 v8] support detached dump Wen Congyang
2012-03-02 10:46 ` [Qemu-devel] [RFC][PATCH 16/16 v8] allow user to dump a fraction of the memory Wen Congyang
2012-03-05 9:12 ` [Qemu-devel] [RFC][PATCH 00/16 v8] introducing a new, dedicated memory dump mechanism Wen Congyang
2012-03-06 0:41 ` Luiz Capitulino
2012-03-07 17:38 ` Luiz Capitulino
2012-03-08 8:55 ` 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=4F50A4A4.3070201@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.