From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50385) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHtLO-0006sP-Ip for qemu-devel@nongnu.org; Fri, 06 Sep 2013 06:32:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VHtLI-0000Ew-Iy for qemu-devel@nongnu.org; Fri, 06 Sep 2013 06:32:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:64133) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHtLI-0000DY-AK for qemu-devel@nongnu.org; Fri, 06 Sep 2013 06:32:44 -0400 From: Kevin Wolf Date: Fri, 6 Sep 2013 12:32:26 +0200 Message-Id: <1378463546-26671-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1378463546-26671-1-git-send-email-kwolf@redhat.com> References: <1378463546-26671-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 2/2] qcow2: Discard VM state in active L1 after creating snapshot List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, xuanmao_001@163.com, stefanha@redhat.com, mreitz@redhat.com During savevm, the VM state is written to the active L1 of the image and then a snapshot is taken. After that, the VM state isn't needed any more in the active L1 and should be discarded. This is implemented by this patch. The impact of not discarding the VM state is that a snapshot can never become smaller than any previous snapshot (because it would be padded with old VM state), and more importantly that future savevm operations cause unnecessary COWs (with associated flushes), which makes subsequent snapshots much slower. Signed-off-by: Kevin Wolf --- block/qcow2-snapshot.c | 7 +++++++ block/qcow2.c | 5 ----- block/qcow2.h | 5 +++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index e7e6013..ffead08 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -416,6 +416,13 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) g_free(old_snapshot_list); + /* The VM state isn't needed any more in the active L1 table; in fact, it + * hurts by causing expensive COW for the next snapshot. */ + qcow2_discard_clusters(bs, qcow2_vm_state_offset(s), + align_offset(sn->vm_state_size, s->cluster_size) + >> BDRV_SECTOR_BITS, + QCOW2_DISCARD_NEVER); + #ifdef DEBUG_ALLOC { BdrvCheckResult result = {0}; diff --git a/block/qcow2.c b/block/qcow2.c index 22569e6..d11ced1 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1747,11 +1747,6 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) return 0; } -static int64_t qcow2_vm_state_offset(BDRVQcowState *s) -{ - return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); -} - static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) { BDRVQcowState *s = bs->opaque; diff --git a/block/qcow2.h b/block/qcow2.h index 9c33b98..47af5c9 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -361,6 +361,11 @@ static inline int64_t align_offset(int64_t offset, int n) return offset; } +static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s) +{ + return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); +} + static inline int qcow2_get_cluster_type(uint64_t l2_entry) { if (l2_entry & QCOW_OFLAG_COMPRESSED) { -- 1.8.1.4