From patchwork Fri Mar 15 07:05:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 227858 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5E0D22C00D2 for ; Fri, 15 Mar 2013 18:05:33 +1100 (EST) Received: from localhost ([::1]:56227 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGOhm-0004tA-Un for incoming@patchwork.ozlabs.org; Fri, 15 Mar 2013 03:05:30 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54391) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGOhY-0004t5-T4 for qemu-devel@nongnu.org; Fri, 15 Mar 2013 03:05:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGOhX-00081Y-IG for qemu-devel@nongnu.org; Fri, 15 Mar 2013 03:05:16 -0400 Received: from [222.73.24.84] (port=36935 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGOhX-000819-0R for qemu-devel@nongnu.org; Fri, 15 Mar 2013 03:05:15 -0400 X-IronPort-AV: E=Sophos;i="4.84,850,1355068800"; d="scan'208";a="6877642" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 15 Mar 2013 15:02:43 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id r2F759MY015314 for ; Fri, 15 Mar 2013 15:05:09 +0800 Received: from localhost.localdomain ([10.167.233.156]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2013031515035178-893970 ; Fri, 15 Mar 2013 15:03:51 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Fri, 15 Mar 2013 15:05:16 +0800 Message-Id: <1363331116-22117-1-git-send-email-hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.8.1.4 X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/03/15 15:03:51, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/03/15 15:03:52, Serialize complete at 2013/03/15 15:03:52 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Subject: [Qemu-devel] [RFC][PATCH] save/load cpu runstate X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch make it aviable to save cpu runstate when do savevm, and restore it when do loadvm, correspondingly. If qemu is started with -S, then the saved runstate is ignored. WARNING: this patch breaks backward migration compatibility, Paolo thinks it's not worth it. But I'm still posting it for other comments. Signed-off-by: Hu Tao --- include/sysemu/sysemu.h | 2 ++ migration.c | 6 +----- monitor.c | 5 ++--- savevm.c | 1 + vl.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h index 6578782..e0ec448 100644 --- a/include/sysemu/sysemu.h +++ b/include/sysemu/sysemu.h @@ -19,6 +19,8 @@ extern uint8_t qemu_uuid[]; int qemu_uuid_parse(const char *str, uint8_t *uuid); #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" +void save_run_state(void); +void load_run_state(void); bool runstate_check(RunState state); void runstate_set(RunState new_state); int runstate_is_running(void); diff --git a/migration.c b/migration.c index 185d112..1a6ac7d 100644 --- a/migration.c +++ b/migration.c @@ -108,11 +108,7 @@ static void process_incoming_migration_co(void *opaque) /* Make sure all file formats flush their mutable metadata */ bdrv_invalidate_cache_all(); - if (autostart) { - vm_start(); - } else { - runstate_set(RUN_STATE_PAUSED); - } + load_run_state(); } void process_incoming_migration(QEMUFile *f) diff --git a/monitor.c b/monitor.c index 1b5acf8..ebe68fe 100644 --- a/monitor.c +++ b/monitor.c @@ -2060,13 +2060,12 @@ void qmp_closefd(const char *fdname, Error **errp) static void do_loadvm(Monitor *mon, const QDict *qdict) { - int saved_vm_running = runstate_is_running(); const char *name = qdict_get_str(qdict, "name"); vm_stop(RUN_STATE_RESTORE_VM); - if (load_vmstate(name) == 0 && saved_vm_running) { - vm_start(); + if (load_vmstate(name) == 0) { + load_run_state(); } } diff --git a/savevm.c b/savevm.c index 147e2d2..a6cce75 100644 --- a/savevm.c +++ b/savevm.c @@ -2175,6 +2175,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) } saved_vm_running = runstate_is_running(); + save_run_state(); vm_stop(RUN_STATE_SAVE_VM); memset(sn, 0, sizeof(*sn)); diff --git a/vl.c b/vl.c index a621aec..aac6659 100644 --- a/vl.c +++ b/vl.c @@ -549,6 +549,7 @@ static int default_driver_check(QemuOpts *opts, void *opaque) /* QEMU state */ static RunState current_run_state = RUN_STATE_PRELAUNCH; +static RunState saved_run_state = RUN_STATE_RUNNING; typedef struct { RunState from; @@ -572,6 +573,7 @@ static const RunStateTransition runstate_transitions_def[] = { { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE }, { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING }, + { RUN_STATE_POSTMIGRATE, RUN_STATE_PAUSED }, { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE }, { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, @@ -582,6 +584,7 @@ static const RunStateTransition runstate_transitions_def[] = { { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE }, { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING }, + { RUN_STATE_RESTORE_VM, RUN_STATE_PAUSED }, { RUN_STATE_RUNNING, RUN_STATE_DEBUG }, { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR }, @@ -611,11 +614,39 @@ static const RunStateTransition runstate_transitions_def[] = { static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX]; +void save_run_state(void) +{ + saved_run_state = current_run_state; +} + +void load_run_state(void) +{ + if (saved_run_state == RUN_STATE_RUNNING) { + vm_start(); + } else if (!runstate_check(saved_run_state)) { + runstate_set(saved_run_state); + } else { + ; /* leave unchanged */ + } +} + bool runstate_check(RunState state) { return current_run_state == state; } +static void runstate_save(QEMUFile *f, void *opaque) +{ + qemu_put_byte(f, saved_run_state); +} + +static int runstate_load(QEMUFile *f, void *opaque, int version_id) +{ + saved_run_state = qemu_get_byte(f); + + return 0; +} + static void runstate_init(void) { const RunStateTransition *p; @@ -625,6 +656,9 @@ static void runstate_init(void) for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) { runstate_valid_transitions[p->from][p->to] = true; } + + register_savevm(NULL, "runstate", 0, 1, + runstate_save, runstate_load, NULL); } /* This function will abort() on invalid state transitions */