qcow2_cache_flush() writes dirty cache to the disk and invokes bdrv_flush() to make the data durable. But even if there is no dirty cache, qcow2_cache_flush() would invoke bdrv_flush(). In fact, bdrv_flush() will invoke fdatasync(), and it is an expensive operation. The patch will not invoke bdrv_flush if there is not dirty cache. The reason that I modify the return value of qcow2_cache_flush() is qcow2_co_flush_to_os needs to know whether flush operation is called. Following is the patch:
>From 23f9f83da4178e8fbb53d2cffe128f5a2d3a239a Mon Sep 17 00:00:00 2001 From: Qingshu Chen <qingshu.chen...@gmail.com> Date: Wed, 1 Jul 2015 14:45:23 +0800 Subject: [PATCH 1/2] ignore bdrv_flush operation when no qcow2 cache item is dirty Signed-off-by: Qingshu Chen <qingshu.chen...@gmail.com> --- block/qcow2-cache.c | 9 ++++++++- block/qcow2.c | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index ed92a09..57c0601 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -174,6 +174,7 @@ int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c) int result = 0; int ret; int i; + int flag = 0; trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache); @@ -182,15 +183,21 @@ int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c) if (ret < 0 && result != -ENOSPC) { result = ret; } + if (c->entries[i].dirty && c->entries[i].offset) { + flag = 1; + } } - if (result == 0) { + if (result == 0 && flag == 1) { ret = bdrv_flush(bs->file); if (ret < 0) { result = ret; } } + if (flag == 0 && result >= 0) { + result = 1; + } + return result; } diff --git a/block/qcow2.c b/block/qcow2.c index d522ec7..ab4613a 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2504,6 +2504,8 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) if (ret < 0) { qemu_co_mutex_unlock(&s->lock); return ret; + } else if (ret == 1) { + bdrv_flush(bs->file); } if (qcow2_need_accurate_refcounts(s)) { -- 1.9.1 ------------------------------------------------------------------------------------------------------------- Qingshu Chen, Institute of Parallel and Distributed Systems (IPADS), School of Software, Shanghai Jiao Tong University ---------------------------------------------------------------------------------------------------------------