Blame SOURCES/kvm-qcow2-Assign-the-L2-cache-relatively-to-the-image-si.patch

383d26
From 2790e1d1df870d455bbd493dc7c342e34df6e4dd Mon Sep 17 00:00:00 2001
383d26
From: Kevin Wolf <kwolf@redhat.com>
383d26
Date: Tue, 19 Feb 2019 17:00:18 +0100
383d26
Subject: [PATCH 17/23] qcow2: Assign the L2 cache relatively to the image size
383d26
383d26
RH-Author: Kevin Wolf <kwolf@redhat.com>
383d26
Message-id: <20190219170023.27826-9-kwolf@redhat.com>
383d26
Patchwork-id: 84548
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 08/13] qcow2: Assign the L2 cache relatively to the image size
383d26
Bugzilla: 1656913
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Leonid Bloch <lbloch@janustech.com>
383d26
383d26
Sufficient L2 cache can noticeably improve the performance when using
383d26
large images with frequent I/O.
383d26
383d26
Previously, unless 'cache-size' was specified and was large enough, the
383d26
L2 cache was set to a certain size without taking the virtual image size
383d26
into account.
383d26
383d26
Now, the L2 cache assignment is aware of the virtual size of the image,
383d26
and will cover the entire image, unless the cache size needed for that is
383d26
larger than a certain maximum. This maximum is set to 1 MB by default
383d26
(enough to cover an 8 GB image with the default cluster size) but can
383d26
be increased or decreased using the 'l2-cache-size' option. This option
383d26
was previously documented as the *maximum* L2 cache size, and this patch
383d26
makes it behave as such, instead of as a constant size. Also, the
383d26
existing option 'cache-size' can limit the sum of both L2 and refcount
383d26
caches, as previously.
383d26
383d26
Signed-off-by: Leonid Bloch <lbloch@janustech.com>
383d26
Reviewed-by: Alberto Garcia <berto@igalia.com>
383d26
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
383d26
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
383d26
(cherry picked from commit b749562d9822d14ef69c9eaa5f85903010b86c30)
383d26
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 block/qcow2.c              | 21 +++++++++------------
383d26
 block/qcow2.h              |  4 +---
383d26
 docs/qcow2-cache.txt       | 15 ++++++++++-----
383d26
 qemu-options.hx            |  6 +++---
383d26
 tests/qemu-iotests/137     |  8 +++++++-
383d26
 tests/qemu-iotests/137.out |  4 +++-
383d26
 6 files changed, 33 insertions(+), 25 deletions(-)
383d26
383d26
diff --git a/block/qcow2.c b/block/qcow2.c
383d26
index f3b2860..fc6bddd 100644
383d26
--- a/block/qcow2.c
383d26
+++ b/block/qcow2.c
383d26
@@ -773,29 +773,35 @@ static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
383d26
                              uint64_t *refcount_cache_size, Error **errp)
383d26
 {
383d26
     BDRVQcow2State *s = bs->opaque;
383d26
-    uint64_t combined_cache_size;
383d26
+    uint64_t combined_cache_size, l2_cache_max_setting;
383d26
     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
383d26
     int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
383d26
+    uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
383d26
+    uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
383d26
 
383d26
     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
383d26
     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
383d26
     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
383d26
 
383d26
     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
383d26
-    *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
383d26
+    l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE,
383d26
+                                             DEFAULT_L2_CACHE_MAX_SIZE);
383d26
     *refcount_cache_size = qemu_opt_get_size(opts,
383d26
                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
383d26
 
383d26
     *l2_cache_entry_size = qemu_opt_get_size(
383d26
         opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
383d26
 
383d26
+    *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting);
383d26
+
383d26
     if (combined_cache_size_set) {
383d26
         if (l2_cache_size_set && refcount_cache_size_set) {
383d26
             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
383d26
                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
383d26
                        "the same time");
383d26
             return;
383d26
-        } else if (*l2_cache_size > combined_cache_size) {
383d26
+        } else if (l2_cache_size_set &&
383d26
+                   (l2_cache_max_setting > combined_cache_size)) {
383d26
             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
383d26
                        QCOW2_OPT_CACHE_SIZE);
383d26
             return;
383d26
@@ -810,9 +816,6 @@ static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
383d26
         } else if (refcount_cache_size_set) {
383d26
             *l2_cache_size = combined_cache_size - *refcount_cache_size;
383d26
         } else {
383d26
-            uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
383d26
-            uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
383d26
-
383d26
             /* Assign as much memory as possible to the L2 cache, and
383d26
              * use the remainder for the refcount cache */
383d26
             if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
383d26
@@ -824,12 +827,6 @@ static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
383d26
                 *l2_cache_size = combined_cache_size - *refcount_cache_size;
383d26
             }
383d26
         }
383d26
-    } else {
383d26
-        if (!l2_cache_size_set) {
383d26
-            *l2_cache_size = MAX(DEFAULT_L2_CACHE_SIZE,
383d26
-                                 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
383d26
-                                 * s->cluster_size);
383d26
-        }
383d26
     }
383d26
     /* l2_cache_size and refcount_cache_size are ensured to have at least
383d26
      * their minimum values in qcow2_update_options_prepare() */
383d26
diff --git a/block/qcow2.h b/block/qcow2.h
383d26
index f73a48a..d0dd4a2 100644
383d26
--- a/block/qcow2.h
383d26
+++ b/block/qcow2.h
383d26
@@ -74,9 +74,7 @@
383d26
 /* Must be at least 4 to cover all cases of refcount table growth */
383d26
 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
383d26
 
383d26
-/* Whichever is more */
383d26
-#define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */
383d26
-#define DEFAULT_L2_CACHE_SIZE S_1MiB
383d26
+#define DEFAULT_L2_CACHE_MAX_SIZE S_1MiB
383d26
 
383d26
 #define DEFAULT_CLUSTER_SIZE S_64KiB
383d26
 
383d26
diff --git a/docs/qcow2-cache.txt b/docs/qcow2-cache.txt
383d26
index 7e28b41..750447e 100644
383d26
--- a/docs/qcow2-cache.txt
383d26
+++ b/docs/qcow2-cache.txt
383d26
@@ -125,8 +125,12 @@ There are a few things that need to be taken into account:
383d26
  - Both caches must have a size that is a multiple of the cluster size
383d26
    (or the cache entry size: see "Using smaller cache sizes" below).
383d26
 
383d26
- - The default L2 cache size is 8 clusters or 1MB (whichever is more),
383d26
-   and the minimum is 2 clusters (or 2 cache entries, see below).
383d26
+ - The maximum L2 cache size is 1 MB by default (enough for full coverage
383d26
+   of 8 GB images, with the default cluster size). This value can be
383d26
+   modified using the "l2-cache-size" option. QEMU will not use more memory
383d26
+   than needed to hold all of the image's L2 tables, regardless of this max.
383d26
+   value. The minimal L2 cache size is 2 clusters (or 2 cache entries, see
383d26
+   below).
383d26
 
383d26
  - The default (and minimum) refcount cache size is 4 clusters.
383d26
 
383d26
@@ -184,9 +188,10 @@ Some things to take into account:
383d26
    always uses the cluster size as the entry size.
383d26
 
383d26
  - If the L2 cache is big enough to hold all of the image's L2 tables
383d26
-   (as explained in the "Choosing the right cache sizes" section
383d26
-   earlier in this document) then none of this is necessary and you
383d26
-   can omit the "l2-cache-entry-size" parameter altogether.
383d26
+   (as explained in the "Choosing the right cache sizes" and "How to
383d26
+   configure the cache sizes" sections in this document) then none of
383d26
+   this is necessary and you can omit the "l2-cache-entry-size"
383d26
+   parameter altogether.
383d26
 
383d26
 
383d26
 Reducing the memory usage
383d26
diff --git a/qemu-options.hx b/qemu-options.hx
383d26
index 3308b94..e1fbc5b 100644
383d26
--- a/qemu-options.hx
383d26
+++ b/qemu-options.hx
383d26
@@ -756,9 +756,9 @@ The maximum total size of the L2 table and refcount block caches in bytes
383d26
 
383d26
 @item l2-cache-size
383d26
 The maximum size of the L2 table cache in bytes
383d26
-(default: if cache-size is not defined - 1048576 bytes or 8 clusters, whichever
383d26
-is larger; otherwise, as large as possible or needed within the cache-size,
383d26
-while permitting the requested or the minimal refcount cache size)
383d26
+(default: if cache-size is not specified - 1M; otherwise, as large as possible
383d26
+within the cache-size, while permitting the requested or the minimal refcount
383d26
+cache size)
383d26
 
383d26
 @item refcount-cache-size
383d26
 The maximum size of the refcount block cache in bytes
383d26
diff --git a/tests/qemu-iotests/137 b/tests/qemu-iotests/137
383d26
index 8796562..19e8597 100755
383d26
--- a/tests/qemu-iotests/137
383d26
+++ b/tests/qemu-iotests/137
383d26
@@ -109,7 +109,6 @@ $QEMU_IO \
383d26
     -c "reopen -o cache-size=1M,l2-cache-size=64k,refcount-cache-size=64k" \
383d26
     -c "reopen -o cache-size=1M,l2-cache-size=2M" \
383d26
     -c "reopen -o cache-size=1M,refcount-cache-size=2M" \
383d26
-    -c "reopen -o l2-cache-size=256T" \
383d26
     -c "reopen -o l2-cache-entry-size=33k" \
383d26
     -c "reopen -o l2-cache-entry-size=128k" \
383d26
     -c "reopen -o refcount-cache-size=256T" \
383d26
@@ -119,6 +118,13 @@ $QEMU_IO \
383d26
     -c "reopen -o cache-clean-interval=-1" \
383d26
     "$TEST_IMG" | _filter_qemu_io
383d26
 
383d26
+IMGOPTS="cluster_size=256k" _make_test_img 32P
383d26
+$QEMU_IO \
383d26
+    -c "reopen -o l2-cache-entry-size=512,l2-cache-size=1T" \
383d26
+    "$TEST_IMG" | _filter_qemu_io
383d26
+
383d26
+_make_test_img 64M
383d26
+
383d26
 echo
383d26
 echo === Test transaction semantics ===
383d26
 echo
383d26
diff --git a/tests/qemu-iotests/137.out b/tests/qemu-iotests/137.out
383d26
index 96724a6..afcc000 100644
383d26
--- a/tests/qemu-iotests/137.out
383d26
+++ b/tests/qemu-iotests/137.out
383d26
@@ -19,7 +19,6 @@ Parameter 'lazy-refcounts' expects 'on' or 'off'
383d26
 cache-size, l2-cache-size and refcount-cache-size may not be set the same time
383d26
 l2-cache-size may not exceed cache-size
383d26
 refcount-cache-size may not exceed cache-size
383d26
-L2 cache size too big
383d26
 L2 cache entry size must be a power of two between 512 and the cluster size (65536)
383d26
 L2 cache entry size must be a power of two between 512 and the cluster size (65536)
383d26
 Refcount cache size too big
383d26
@@ -27,6 +26,9 @@ Conflicting values for qcow2 options 'overlap-check' ('constant') and 'overlap-c
383d26
 Unsupported value 'blubb' for qcow2 option 'overlap-check'. Allowed are any of the following: none, constant, cached, all
383d26
 Unsupported value 'blubb' for qcow2 option 'overlap-check'. Allowed are any of the following: none, constant, cached, all
383d26
 Cache clean interval too big
383d26
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=36028797018963968
383d26
+L2 cache size too big
383d26
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
383d26
 
383d26
 === Test transaction semantics ===
383d26
 
383d26
-- 
383d26
1.8.3.1
383d26