Skip to contentSkip to conversation

qcow2 metadata cache

PATCH In review Kevin Wolf 2 participants 7 messages Jan 24, 2011 – Jan 28, 2011

Subsystems (2)
lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

v5 cover qcow2 metadata cache

block-queue turned out to be too big effort to be useful for quickly fixing the performance problems that qcow2 got since we introduced the metadata flushes. While I still think the idea is right, it needs more time and qcow2 doesn't have more time. Let's come back to block-queue later when the most urgent qcow2 problems are fixed.

So this is the idea of block-queue applied to the very specific case of qcow2. Whereas block-queue tried to be a generic solution for all kind of things and tried to make all writes asynchronous at the same time, this is only about batching writes to refcount blocks and L2 tables in qcow2 and getting the dependencies right. (Yes, the L1 table and refcount table is left alone. They are almost never written to anyway.)

This should be much easier to understand and review, and I myself feel a bit more confident about it than with block-queue, too.

v1:
- Don't read newly allocated tables from the disk before memsetting them to zero

v2:
- Addressed Stefan's review comments
- Added patch 3 to avoid an unnecessary bdrv_flush after COW

v3:
- Some error path fixes (esp. missing or double qcow2_cache_put)

v4:
- Pay attention to make writethrough performance not even worse in update_refcount
- Add some blkdebug events back so that "failures" in qemu-iotests are kept minimal. Some diff between master and this series is left in test 026, but it's harmless.

v5:
- Fixed return value of alloc_refcount_block
- Removed unnecessary bdrv_flush

Kevin Wolf (3):
qcow2: Add QcowCache
qcow2: Use QcowCache
qcow2: Batch flushes for COW

 Makefile.objs          |    2 +-
 block/qcow2-cache.c    |  314 ++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2-cluster.c  |  208 +++++++++++---------------------
 block/qcow2-refcount.c |  260 ++++++++++++++++-----------------------
 block/qcow2.c          |   48 +++++++-
 block/qcow2.h          |   32 ++++-
 6 files changed, 565 insertions(+), 299 deletions(-)
 create mode 100644 block/qcow2-cache.c
Signature · 1 line
1.7.2.3
author
#1 lore raw

PATCH v5 1/3 qcow2: Add QcowCache

This adds some new cache functions to qcow2 which can be used for caching refcount blocks and L2 tables. When used with cache=writethrough they work like the old caching code which is spread all over qcow2, so for this case we have merely a cleanup.

The interesting case is with writeback caching (this includes cache=none) where data isn't written to disk immediately but only kept in cache initially. This leads to some form of metadata write batching which avoids the current "write to refcount block, flush, write to L2 table" pattern for each single request when a lot of cluster allocations happen. Instead, cache entries are only written out if its required to maintain the right order. In the pure cluster allocation case this means that all metadata updates for requests are done in memory initially and on sync, first the refcount blocks are written to disk, then fsync, then L2 tables.

This improves performance of scenarios with lots of cluster allocations noticably (e.g. installation or after taking a snapshot).

---
 Makefile.objs       |    2 +-
 block/qcow2-cache.c |  290 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h       |   19 ++++
 3 files changed, 310 insertions(+), 1 deletions(-)
 create mode 100644 block/qcow2-cache.c
Makefile.objs +1 -1
index fda366d..93406ff 100644
@@ -19,7 +19,7 @@ block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 
 block-nested-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
-block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o
+block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
 block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-nested-y += qed-check.o
 block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
block/qcow2-cache.c new file +290 -0

290 lines not on this page · raw message

block/qcow2.h +19 -0
index 5217bea..e5473e1 100644
@@ -78,6 +78,9 @@ typedef struct QCowSnapshot {
     uint64_t vm_clock_nsec;
 } QCowSnapshot;
 
+struct Qcow2Cache;
+typedef struct Qcow2Cache Qcow2Cache;
+
 typedef struct BDRVQcowState {
     int cluster_bits;
     int cluster_size;
@@ -215,4 +218,20 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
 void qcow2_free_snapshots(BlockDriverState *bs);
 int qcow2_read_snapshots(BlockDriverState *bs);
 
+/* qcow2-cache.c functions */
+Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
+    bool writethrough);
+int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
+
+void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
+int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
+int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
+    Qcow2Cache *dependency);
+
+int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
+    void **table);
+int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
+    void **table);
+int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
+
 #endif
Signature · 1 line
1.7.2.3
author
#2 lore raw

PATCH v5 2/3 qcow2: Use QcowCache

In reply to Kevin Wolf

Use the new functions of qcow2-cache.c for everything that works on refcount block and L2 tables.

---
 block/qcow2-cache.c    |   10 ++
 block/qcow2-cluster.c  |  208 +++++++++++++-------------------------
 block/qcow2-refcount.c |  260 ++++++++++++++++++++----------------------------
 block/qcow2.c          |   48 ++++++++-
 block/qcow2.h          |   12 ++-
 5 files changed, 240 insertions(+), 298 deletions(-)
block/qcow2-cache.c +10 -0
index f7c4e2a..5f1740b 100644
@@ -104,6 +104,12 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
         }
     }
 
+    if (c == s->refcount_block_cache) {
+        BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
+    } else if (c == s->l2_table_cache) {
+        BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
+    }
+
     ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table,
         s->cluster_size);
     if (ret < 0) {
@@ -218,6 +224,10 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
 
     c->entries[i].offset = 0;
     if (read_from_disk) {
+        if (c == s->l2_table_cache) {
+            BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
+        }
+
         ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size);
         if (ret < 0) {
             return ret;
block/qcow2-cluster.c +72 -136

362 lines not on this page · raw message

block/qcow2-refcount.c +107 -153

446 lines not on this page · raw message

block/qcow2.c +44 -4

84 lines not on this page · raw message

block/qcow2.h +7 -5
index e5473e1..11cbce3 100644
@@ -51,6 +51,9 @@
 
 #define L2_CACHE_SIZE 16
 
+/* Must be at least 4 to cover all cases of refcount table growth */
+#define REFCOUNT_CACHE_SIZE 4
+
 typedef struct QCowHeader {
     uint32_t magic;
     uint32_t version;
@@ -94,9 +97,10 @@ typedef struct BDRVQcowState {
     uint64_t cluster_offset_mask;
     uint64_t l1_table_offset;
     uint64_t *l1_table;
-    uint64_t *l2_cache;
-    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
-    uint32_t l2_cache_counts[L2_CACHE_SIZE];
+
+    Qcow2Cache* l2_table_cache;
+    Qcow2Cache* refcount_block_cache;
+
     uint8_t *cluster_cache;
     uint8_t *cluster_data;
     uint64_t cluster_cache_offset;
@@ -105,8 +109,6 @@ typedef struct BDRVQcowState {
     uint64_t *refcount_table;
     uint64_t refcount_table_offset;
     uint32_t refcount_table_size;
-    uint64_t refcount_block_cache_offset;
-    uint16_t *refcount_block_cache;
     int64_t free_cluster_index;
     int64_t free_byte_offset;
 
Signature · 1 line
1.7.2.3
author
#3 lore raw

PATCH v5 3/3 qcow2: Batch flushes for COW

In reply to Kevin Wolf

qcow2 calls bdrv_flush() after performing COW in order to ensure that the L2 table change is never written before the copy is safe on disk. Now that the L2 table is cached, we can wait with flushing until we write out the next L2 table.

---
 block/qcow2-cache.c   |   20 +++++++++++++++++---
 block/qcow2-cluster.c |    2 +-
 block/qcow2.h         |    1 +
 3 files changed, 19 insertions(+), 4 deletions(-)
block/qcow2-cache.c +17 -3
index 5f1740b..8f2955b 100644
@@ -38,6 +38,7 @@ struct Qcow2Cache {
     int                     size;
     Qcow2CachedTable*       entries;
     struct Qcow2Cache*      depends;
+    bool                    depends_on_flush;
     bool                    writethrough;
 };
 
@@ -85,13 +86,15 @@ static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
     }
 
     c->depends = NULL;
+    c->depends_on_flush = false;
+
     return 0;
 }
 
 static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
 {
     BDRVQcowState *s = bs->opaque;
-    int ret;
+    int ret = 0;
 
     if (!c->entries[i].dirty || !c->entries[i].offset) {
         return 0;
@@ -99,11 +102,17 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
 
     if (c->depends) {
         ret = qcow2_cache_flush_dependency(bs, c);
-        if (ret < 0) {
-            return ret;
+    } else if (c->depends_on_flush) {
+        ret = bdrv_flush(bs->file);
+        if (ret >= 0) {
+            c->depends_on_flush = false;
         }
     }
 
+    if (ret < 0) {
+        return ret;
+    }
+
     if (c == s->refcount_block_cache) {
         BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
     } else if (c == s->l2_table_cache) {
@@ -167,6 +176,11 @@ int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
     return 0;
 }
 
+void qcow2_cache_depends_on_flush(Qcow2Cache *c)
+{
+    c->depends_on_flush = true;
+}
+
 static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
 {
     int i;
block/qcow2-cluster.c +1 -1
index 76e7e07..1c2003a 100644
@@ -637,7 +637,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
      * handled.
      */
     if (cow) {
-        bdrv_flush(bs->file);
+        qcow2_cache_depends_on_flush(s->l2_table_cache);
     }
 
     qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
block/qcow2.h +1 -0
index 11cbce3..6d80120 100644
@@ -229,6 +229,7 @@ void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
     Qcow2Cache *dependency);
+void qcow2_cache_depends_on_flush(Qcow2Cache *c);
 
 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
     void **table);
Signature · 1 line
1.7.2.3
author
#5 lore raw

Am 27.01.2011 08:52, schrieb TeLeMan:

Quoted 1 line

These patches break booting Windows.

Can you be a bit more specific? It certainly doesn't break my Windows images. Just to be sure that I didn't miss a last-minute regression, I just installed another Win 7 VM and it works just fine.

Which Windows version do you use? What's your command line? Do you get block error events on a QMP monitor?

Kevin

#6 lore raw

Host: Win7
Guest: WinXP

Reproduce:
qemu-img create -f qcow2 winxp.qcow2 80G
qemu -L pc-bios -m 256 -hda winxp.qcow2 -cdrom WXPVOL_EN.iso -boot d

Error:
An unexpected error (3073) occurred at
line 5137 in d:\xp\client\base\boot\setup\setup.c

I tried adding "-snapshot" into the command line, it worked.