qcow2: Allow runtime specification of cache sizes
cover qcow2: Allow runtime specification of cache sizes
Currently, the metadata cache size is only tunable on compile time through macros. However, some users may want to use the minimal cache size (for whatever reason) and others may want to increase the cache size because they have enough memory and want to increase performance.
This series adds runtime options for setting the cache size in bytes (which is an easily comprehensible unit) in various ways (by setting each cache explicitly or the total size).
This series (patch 2) depends on Markus' series "[PATCH v2 0/4] block: Use g_new() & friends more".
Max Reitz (4):
qcow2: Constant cache size in bytes
qcow2: Use g_try_new0() for cache array
qcow2: Add runtime options for cache sizes
iotests: Add test for qcow2's cache options
block/qcow2-cache.c | 7 ++- block/qcow2.c | 120 +++++++++++++++++++++++++++++++++++++++++---- block/qcow2.h | 13 ++++- tests/qemu-iotests/103 | 99 +++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/103.out | 29 +++++++++++ tests/qemu-iotests/group | 1 + 6 files changed, 255 insertions(+), 14 deletions(-) create mode 100755 tests/qemu-iotests/103 create mode 100644 tests/qemu-iotests/103.out
Signature · 1 line
2.0.4
PATCH 1/4 qcow2: Constant cache size in bytes
Specifying the metadata cache sizes in clusters results in less clusters (and much less bytes) covered for small cluster sizes and vice versa. Using a constant byte size reduces this difference, and makes it possible to manually specify the cache size in an easily comprehensible unit.
--- block/qcow2.c | 16 ++++++++++++++-- block/qcow2.h | 10 ++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-)
block/qcow2.c +14 -2
@@ -470,6 +470,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,uint64_t l1_vm_state_index;const char *opt_overlap_check;int overlap_check_template =0 ; +uint64_t l2_cache_size, refcount_cache_size; ret =bdrv_pread (bs->file,0 , &header,sizeof (header));if (ret <0 ) {
@@ -707,8 +708,19 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, }/* alloc L2 table/refcount block cache */ - s->l2_table_cache =qcow2_cache_create (bs,L2_CACHE_SIZE ); - s->refcount_block_cache =qcow2_cache_create (bs,REFCOUNT_CACHE_SIZE ); + l2_cache_size =DEFAULT_L2_CACHE_BYTE_SIZE / s->cluster_size; +if (l2_cache_size <MIN_L2_CACHE_SIZE ) { + l2_cache_size =MIN_L2_CACHE_SIZE ; + } + + refcount_cache_size = l2_cache_size + / (DEFAULT_L2_REFCOUNT_SIZE_RATIO * s->cluster_size); +if (refcount_cache_size <MIN_REFCOUNT_CACHE_SIZE ) { + refcount_cache_size =MIN_REFCOUNT_CACHE_SIZE ; + } + + s->l2_table_cache =qcow2_cache_create (bs, l2_cache_size); + s->refcount_block_cache =qcow2_cache_create (bs, refcount_cache_size);if (s->l2_table_cache ==NULL || s->refcount_block_cache ==NULL ) {error_setg (errp,"Could not allocate metadata caches" ); ret = -ENOMEM ;
block/qcow2.h +8 -2
@@ -64,10 +64,16 @@#define MIN_CLUSTER_BITS 9#define MAX_CLUSTER_BITS 21 -#define L2_CACHE_SIZE 16 +#define MIN_L2_CACHE_SIZE 1/* cluster */ /* Must be at least 4 to cover all cases of refcount table growth */ -#define REFCOUNT_CACHE_SIZE 4 +#define MIN_REFCOUNT_CACHE_SIZE 4/* clusters */ + +#define DEFAULT_L2_CACHE_BYTE_SIZE 1048576/* bytes */ + +/* The refblock cache needs only a fourth of the L2 cache size to cover as many +* clusters */ +#define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4#define DEFAULT_CLUSTER_SIZE 65536
Signature · 1 line
2.0.4
PATCH 4/4 iotests: Add test for qcow2's cache options
In reply to Max ReitzAdd a test which tests various combinations of qcow2's cache options (some of which are valid, some of which are not).
--- tests/qemu-iotests/103 | 99 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/103.out | 29 ++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 129 insertions(+) create mode 100755 tests/qemu-iotests/103 create mode 100644 tests/qemu-iotests/103.out
tests/qemu-iotests/103 new file +99 -0
@@ -0,0 +1,99 @@ +#!/bin/bash +# +# Test case for qcow2 metadata cache size specification +# +# Copyright (C) 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +# creator [email protected] + +seq=$(basename $0) +echo "QA output created by $seq" + +here=$PWD +tmp=/tmp/$$ +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux + +IMG_SIZE=64K + +_make_test_img $IMG_SIZE +$QEMU_IO -c 'write -P 42 0 64k' "$TEST_IMG" | _filter_qemu_io + +echo +echo '=== Testing invalid option combinations ===' +echo + +# all sizes set at the same time +$QEMU_IO -c "open -o cache-size=1.25M,l2-cache-size=1M,refcount-cache-size=0.25M $TEST_IMG" \ + 2>&1 | _filter_testdir | _filter_imgfmt +# l2-cache-size may not exceed cache-size +$QEMU_IO -c "open -o cache-size=1M,l2-cache-size=2M $TEST_IMG" 2>&1 \ + | _filter_testdir | _filter_imgfmt +# refcount-cache-size may not exceed cache-size +$QEMU_IO -c "open -o cache-size=1M,refcount-cache-size=2M $TEST_IMG" 2>&1 \ + | _filter_testdir | _filter_imgfmt +# 0 should be a valid size (e.g. for enforcing the minimum), so this should not +# work +$QEMU_IO -c "open -o cache-size=0,l2-cache-size=0,refcount-cache-size=0 $TEST_IMG" \ + 2>&1 | _filter_testdir | _filter_imgfmt + +echo +echo '=== Testing valid option combinations ===' +echo + +# There should be a reasonable and working minimum +$QEMU_IO -c "open -o cache-size=0 $TEST_IMG" -c 'read -P 42 0 64k' \ + | _filter_qemu_io +$QEMU_IO -c "open -o l2-cache-size=0 $TEST_IMG" -c 'read -P 42 0 64k' \ + | _filter_qemu_io +$QEMU_IO -c "open -o refcount-cache-size=0 $TEST_IMG" -c 'read -P 42 0 64k' \ + | _filter_qemu_io + +# Derive cache sizes from combined size (with a reasonable ratio, but we cannot +# test that) +$QEMU_IO -c "open -o cache-size=2M $TEST_IMG" -c 'read -P 42 0 64k' \ + | _filter_qemu_io +# Fix one cache, derive the other +$QEMU_IO -c "open -o cache-size=2M,l2-cache-size=1M $TEST_IMG" \ + -c 'read -P 42 0 64k' \ + | _filter_qemu_io +$QEMU_IO -c "open -o cache-size=2M,refcount-cache-size=1M $TEST_IMG" \ + -c 'read -P 42 0 64k' \ + | _filter_qemu_io +# Directly set both caches +$QEMU_IO -c "open -o l2-cache-size=1M,refcount-cache-size=0.25M $TEST_IMG" \ + -c 'read -P 42 0 64k' \ + | _filter_qemu_io + +# success, all done +echo '*** done' +rm -f $seq.full +status=0
tests/qemu-iotests/103.out new file +29 -0
@@ -0,0 +1,29 @@ +QA output created by 103 +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=65536 +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=== Testing invalid option combinations === + +qemu-io: can't open device TEST_DIR/t.IMGFMT: cache-size, l2-cache-size and refcount-cache-size may not be set the same time +qemu-io: can't open device TEST_DIR/t.IMGFMT: l2-cache-size may not exceed cache-size +qemu-io: can't open device TEST_DIR/t.IMGFMT: refcount-cache-size may not exceed cache-size +qemu-io: can't open device TEST_DIR/t.IMGFMT: cache-size, l2-cache-size and refcount-cache-size may not be set the same time + +=== Testing valid option combinations === + +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +*** done
tests/qemu-iotests/group +1 -0
@@ -100,3 +100,4 @@ 091 rw auto quick 092 rw auto quick 095 rw auto quick +103 rw auto quick
Signature · 1 line
2.0.4
PATCH 2/4 qcow2: Use g_try_new0() for cache array
In reply to Max ReitzWith a variable cache size, the number given to qcow2_cache_create() may be huge. Therefore, use g_try_new0().
While at it, use g_new0() instead of g_malloc0() for allocating the Qcow2Cache object.
--- block/qcow2-cache.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
block/qcow2-cache.c +5 -2
@@ -48,9 +48,12 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)Qcow2Cache *c;int i; - c =g_malloc0 (sizeof (*c)); + c =g_new0 (Qcow2Cache,1 ); c->size = num_tables; - c->entries =g_new0 (Qcow2CachedTable, num_tables); + c->entries =g_try_new0 (Qcow2CachedTable, num_tables); +if (!c->entries) { + goto fail; + }for (i =0 ; i < c->size; i++) { c->entries[i].table =qemu_try_blockalign (bs->file, s->cluster_size);
Signature · 1 line
2.0.4
PATCH 3/4 qcow2: Add runtime options for cache sizes
In reply to Max ReitzAdd options for specifying the size of the metadata caches. This can either be done directly for each cache (if only one is given, the other will be derived according to a default ratio) or combined for both.
--- block/qcow2.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++------- block/qcow2.h | 3 ++ 2 files changed, 103 insertions(+), 12 deletions(-)
block/qcow2.c +100 -12
@@ -442,6 +442,22 @@ static QemuOptsList qcow2_runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Check for unintended writes into an inactive L2 table", }, + { + .name = QCOW2_OPT_CACHE_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Maximum combined metadata (L2 tables and refcount blocks) " + "cache size", + }, + { + .name = QCOW2_OPT_L2_CACHE_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Maximum L2 table cache size", + }, + { + .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Maximum refcount block cache size", + }, {/* end of list */ } }, };
@@ -457,6 +473,61 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, }; +static void read_cache_sizes (QemuOpts *opts,uint64_t *l2_cache_size, +uint64_t *refcount_cache_size,Error **errp) +{ +uint64_t combined_cache_size; +bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set; + + combined_cache_size_set =qemu_opt_get (opts,QCOW2_OPT_CACHE_SIZE ); + l2_cache_size_set =qemu_opt_get (opts,QCOW2_OPT_L2_CACHE_SIZE ); + refcount_cache_size_set =qemu_opt_get (opts,QCOW2_OPT_REFCOUNT_CACHE_SIZE ); + + combined_cache_size =qemu_opt_get_size (opts,QCOW2_OPT_CACHE_SIZE ,0 ); + *l2_cache_size =qemu_opt_get_size (opts,QCOW2_OPT_L2_CACHE_SIZE ,0 ); + *refcount_cache_size =qemu_opt_get_size (opts, +QCOW2_OPT_REFCOUNT_CACHE_SIZE ,0 ); + +if (combined_cache_size_set) { +if (l2_cache_size_set && refcount_cache_size_set) { +error_setg (errp,QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE +" and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set " +"the same time" ); +return ; + }else if (*l2_cache_size > combined_cache_size) { +error_setg (errp,QCOW2_OPT_L2_CACHE_SIZE " may not exceed " +QCOW2_OPT_CACHE_SIZE ); +return ; + }else if (*refcount_cache_size > combined_cache_size) { +error_setg (errp,QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed " +QCOW2_OPT_CACHE_SIZE ); +return ; + } + +if (l2_cache_size_set) { + *refcount_cache_size = combined_cache_size - *l2_cache_size; + }else if (refcount_cache_size_set) { + *l2_cache_size = combined_cache_size - *refcount_cache_size; + }else { + *refcount_cache_size = combined_cache_size + / (DEFAULT_L2_REFCOUNT_SIZE_RATIO +1 ); + *l2_cache_size = combined_cache_size - *refcount_cache_size; + } + }else { +if (!l2_cache_size_set && !refcount_cache_size_set) { + *l2_cache_size =DEFAULT_L2_CACHE_BYTE_SIZE ; + *refcount_cache_size = *l2_cache_size + /DEFAULT_L2_REFCOUNT_SIZE_RATIO ; + }else if (!l2_cache_size_set) { + *l2_cache_size = *refcount_cache_size + *DEFAULT_L2_REFCOUNT_SIZE_RATIO ; + }else if (!refcount_cache_size_set) { + *refcount_cache_size = *l2_cache_size + /DEFAULT_L2_REFCOUNT_SIZE_RATIO ; + } + } +} +static int qcow2_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -707,18 +778,43 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, } } -/* alloc L2 table/refcount block cache */ - l2_cache_size =DEFAULT_L2_CACHE_BYTE_SIZE / s->cluster_size; +/* get L2 table/refcount block cache size from command line options */ + opts =qemu_opts_create (&qcow2_runtime_opts,NULL ,0 , &error_abort); +qemu_opts_absorb_qdict (opts, options, &local_err); +if (local_err) { +error_propagate (errp, local_err); + ret = -EINVAL ; + goto fail; + } + +read_cache_sizes (opts, &l2_cache_size, &refcount_cache_size, &local_err); +if (local_err) { +error_propagate (errp, local_err); + ret = -EINVAL ; + goto fail; + } + + l2_cache_size /= s->cluster_size;if (l2_cache_size <MIN_L2_CACHE_SIZE ) { l2_cache_size =MIN_L2_CACHE_SIZE ; } +if (l2_cache_size >INT_MAX ) { +error_setg (errp,"L2 cache size too big" ); + ret = -EINVAL ; + goto fail; + } - refcount_cache_size = l2_cache_size - / (DEFAULT_L2_REFCOUNT_SIZE_RATIO * s->cluster_size); + refcount_cache_size /= s->cluster_size;if (refcount_cache_size <MIN_REFCOUNT_CACHE_SIZE ) { refcount_cache_size =MIN_REFCOUNT_CACHE_SIZE ; } +if (refcount_cache_size >INT_MAX ) { +error_setg (errp,"Refcount cache size too big" ); + ret = -EINVAL ; + goto fail; + } +/* alloc L2 table/refcount block cache */ s->l2_table_cache =qcow2_cache_create (bs, l2_cache_size); s->refcount_block_cache =qcow2_cache_create (bs, refcount_cache_size);if (s->l2_table_cache ==NULL || s->refcount_block_cache ==NULL ) {
@@ -810,14 +906,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, }/* Enable lazy_refcounts according to image and command line options */ - opts =qemu_opts_create (&qcow2_runtime_opts,NULL ,0 , &error_abort); -qemu_opts_absorb_qdict (opts, options, &local_err); -if (local_err) { -error_propagate (errp, local_err); - ret = -EINVAL ; - goto fail; - } - s->use_lazy_refcounts =qemu_opt_get_bool (opts,QCOW2_OPT_LAZY_REFCOUNTS , (s->compatible_features &QCOW2_COMPAT_LAZY_REFCOUNTS ));
block/qcow2.h +3 -0
@@ -91,6 +91,9 @@#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" +#define QCOW2_OPT_CACHE_SIZE "cache-size" +#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" +#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size" typedef struct QCowHeader { uint32_t magic;
Signature · 1 line
2.0.4
Re: [Qemu-devel] [PATCH 2/4] qcow2: Use g_try_new0() for cache array
In reply to Max ReitzQuoted 17 lines · Max Reitz
With a variable cache size, the number given to
qcow2_cache_create()may be huge. Therefore, useg_try_new0().While at it, use
g_new0()instead ofg_malloc0()for allocating the Qcow2Cache object.Signed-off-by Max Reitz <[email protected]>--- block/qcow2-cache.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)block/qcow2-cache.c +0
-0@@ -48,9 +48,12 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)Qcow2Cache *c;
block/qcow2-cache.c Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) · line 56
Quoted 10 lines
block/qcow2-cache.c +5
-2int i; - c =g_malloc0 (sizeof (*c)); + c =g_new0 (Qcow2Cache,1 ); c->size = num_tables; - c->entries =g_new0 (Qcow2CachedTable, num_tables); + c->entries =g_try_new0 (Qcow2CachedTable, num_tables); +if (!c->entries) { + goto fail; + }
Self-NACK: The fail path needs to acknowledge that c->entries may be NULL.
Max
Re: [Qemu-devel] [PATCH 3/4] qcow2: Add runtime options for cache sizes
In reply to Max ReitzQuoted 9 lines · Max Reitz
Add options for specifying the size of the metadata caches. This can either be done directly for each cache (if only one is given, the other will be derived according to a default ratio) or combined for both.
Signed-off-by Max Reitz <[email protected]>--- block/qcow2.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++------- block/qcow2.h | 3 ++ 2 files changed, 103 insertions(+), 12 deletions(-)
I would suspect that you need to also modify qapi/block-core.json to document the new QMP parameters that can be tuned when hot-plugging a qcow2 disk.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 3/4] qcow2: Add runtime options for cache sizes
Quoted 5 lines · Eric Blake
Quoted 4 lines · Max Reitz
Add options for specifying the size of the metadata caches. This can either be done directly for each cache (if only one is given, the other will be derived according to a default ratio) or combined for both.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Max Reitz <[email protected]>--- block/qcow2.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++------- block/qcow2.h | 3 ++ 2 files changed, 103 insertions(+), 12 deletions(-)I would suspect that you need to also modify
qapi/block-core.jsonto document the new QMP parameters that can be tuned when hot-plugging a qcow2 disk.
Oh, you're right - that structure is missing all of the corruption detection parameters as well. I'll prepare a follow-up patch tomorrow.
Max
Re: [Qemu-devel] [PATCH 3/4] qcow2: Add runtime options for cache sizes
Quoted 12 lines · Max Reitz
Quoted 11 lines · Eric Blake
Quoted 10 lines · Max Reitz
Add options for specifying the size of the metadata caches. This can either be done directly for each cache (if only one is given, the other will be derived according to a default ratio) or combined for both.
Signed-off-by Max Reitz <[email protected]>--- block/qcow2.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++------- block/qcow2.h | 3 ++ 2 files changed, 103 insertions(+), 12 deletions(-)
Quoted 5 lines
Quoted 3 lines
I would suspect that you need to also modify
qapi/block-core.jsonto document the new QMP parameters that can be tuned when hot-plugging a qcow2 disk.Oh, you're right - that structure is missing all of the corruption detection parameters as well. I'll prepare a follow-up patch tomorrow.
Make that "on Wednesday", as I won't be around tomorrow.
Max