qcow2: relax subclusters allocation dependencies
v2 cover qcow2: relax subclusters allocation dependencies
Hi all!
v2:
01: improve documentation
02: add Hanna's and Eric's r-bs, add tiny grammar fix 03: fix test by filtering instead of reducing number of writes
Parallel small writes to unallocated cluster works bad when subclusters enabled.
Look, without subclusters, one of write requests will allocate the whole cluster, and all other writes to this cluster will be independent of each other, they depend only on the first one that does allocation.
With subclusters, each write to unallocated subcluster will block the whole cluster for parallel writing.
So, assume we write 8 consecutive 4k chunks in parallel:
Without subclusters, one of the chunks will block all the cluster and write L2 entry. The remaining 7 chunks are written in parallel.
With subclusters, each of the chunks will allocate new subcluster and block the whole cluster. All the chunks are dependent on each other and queue depth becomes 1. That's not good.
Let's improve the situation.
Vladimir Sementsov-Ogievskiy (3):
simplebench: add img_bench_templater.py
qcow2: refactor handle_dependencies() loop body
qcow2: handle_dependencies(): relax conflict detection
block/qcow2-cluster.c | 60 +++++++++----- scripts/simplebench/img_bench_templater.py | 95 ++++++++++++++++++++++ scripts/simplebench/table_templater.py | 62 ++++++++++++++ tests/qemu-iotests/271 | 5 +- tests/qemu-iotests/271.out | 4 +- 5 files changed, 202 insertions(+), 24 deletions(-) create mode 100755 scripts/simplebench/img_bench_templater.py create mode 100644 scripts/simplebench/table_templater.py
Signature · 1 line
2.29.2
PATCH v2 1/3 simplebench: add img_bench_templater.py
Add simple grammar-parsing template benchmark. New tool consume test template written in bash with some special grammar injections and produces multiple tests, run them and finally print a performance comparison table of different tests produced from one template.
--- scripts/simplebench/img_bench_templater.py | 95 ++++++++++++++++++++++ scripts/simplebench/table_templater.py | 62 ++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 scripts/simplebench/img_bench_templater.py create mode 100644 scripts/simplebench/table_templater.py
scripts/simplebench/img_bench_templater.py new file +95 -0
@@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# +# Process img-bench test templates +# +# Copyright (c) 2021 Virtuozzo International GmbH. +# +# 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/>. +# + + +import sys +import subprocess +import re +import json + +import simplebench +from results_to_text import results_to_text +from table_templater import Templater + + +def bench_func(env, case): + test = templater.gen(env['data'], case['data']) + + p = subprocess.run(test, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, universal_newlines=True) + + if p.returncode == 0: + try: + m = re.search(r'Run completed in (\d+.\d+) seconds.', p.stdout) + return {'seconds': float(m.group(1))} + except Exception: + return {'error': f'failed to parse qemu-img output: {p.stdout}'} + else: + return {'error': f'qemu-img failed: {p.returncode}: {p.stdout}'} + + +if __name__ == '__main__': + if len(sys.argv) > 1: + print(""" +Usage: img_bench_templater.py < path/to/test-template.sh + +This script generates performance tests from a test template (example below), +runs them, and displays the results in a table. The template is read from +stdin. It must be written in bash and end with a `qemu-img bench` invocation +(whose result is parsed to get the test instance’s result). + +Use the following syntax in the template to create the various different test +instances: + + column templating: {var1|var2|...} - test will use different values in + different columns. You may use several {} constructions in the test, in this + case product of all choice-sets will be used. + + row templating: [var1|var2|...] - similar thing to define rows (test-cases) + +Test template example: + +Assume you want to compare two qemu-img binaries, called qemu-img-old and +qemu-img-new in your build directory in two test-cases with 4K writes and 64K +writes. The template may look like this: + +qemu_img=/path/to/qemu/build/qemu-img-{old|new} +$qemu_img create -f qcow2 /ssd/x.qcow2 1G +$qemu_img bench -c 100 -d 8 [-s 4K|-s 64K] -w -t none -n /ssd/x.qcow2 + +When passing this to stdin of img_bench_templater.py, the resulting comparison +table will contain two columns (for two binaries) and two rows (for two +test-cases). + +In addition to displaying the results, script also stores results in JSON +format into results.json file in current directory. +""") + sys.exit() + + templater = Templater(sys.stdin.read()) + + envs = [{'id': ' / '.join(x), 'data': x} for x in templater.columns] + cases = [{'id': ' / '.join(x), 'data': x} for x in templater.rows] + + result = simplebench.bench(bench_func, envs, cases, count=5, + initial_run=False) + print(results_to_text(result)) + with open('results.json', 'w') as f: + json.dump(result, f, indent=4)
scripts/simplebench/table_templater.py new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.29.2
Re: [PATCH v2 1/3] simplebench: add img_bench_templater.py
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Add simple grammar-parsing template benchmark. New tool consume test template written in bash with some special grammar injections and produces multiple tests, run them and finally print a performance comparison table of different tests produced from one template.
Quoted 7 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- scripts/simplebench/img_bench_templater.py | 95 ++++++++++++++++++++++ scripts/simplebench/table_templater.py | 62 ++++++++++++++ 2 files changed, 157 insertions(+)create mode 100755
scripts/simplebench/img_bench_templater.pycreate mode 100644scripts/simplebench/table_templater.py
PATCH v2 2/3 qcow2: refactor handle_dependencies() loop body
In reply to Vladimir Sementsov-OgievskiyNo logic change, just prepare for the following commit. While being here do also small grammar fix in a comment.
--- block/qcow2-cluster.c | 49 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-)
block/qcow2-cluster.c +28 -21
@@ -1400,29 +1400,36 @@ static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,if (end <= old_start || start >= old_end) {/* No intersection */ - }else { -if (start < old_start) { -/* Stop at the start of a running allocation */ - bytes = old_start - start; - }else { - bytes =0 ; - } +continue ; + } -/* Stop if already an l2meta exists. After yielding, it wouldn't -* be valid any more, so we'd have to clean up the old L2Metas -* and deal with requests depending on them before starting to -* gather new ones. Not worth the trouble. */ -if (bytes ==0 && *m) { - *cur_bytes =0 ; -return 0 ; - } +/* Conflict */ -if (bytes ==0 ) { -/* Wait for the dependency to complete. We need to recheck -* the free/allocated clusters when we continue. */ -qemu_co_queue_wait (&old_alloc->dependent_requests, &s->lock); -return -EAGAIN ; - } +if (start < old_start) { +/* Stop at the start of a running allocation */ + bytes = old_start - start; + }else { + bytes =0 ; + } + +/* +* Stop if an l2meta already exists. After yielding, it wouldn't +* be valid any more, so we'd have to clean up the old L2Metas +* and deal with requests depending on them before starting to +* gather new ones. Not worth the trouble. +*/ +if (bytes ==0 && *m) { + *cur_bytes =0 ; +return 0 ; + } + +if (bytes ==0 ) { +/* +* Wait for the dependency to complete. We need to recheck +* the free/allocated clusters when we continue. +*/ +qemu_co_queue_wait (&old_alloc->dependent_requests, &s->lock); +return -EAGAIN ; } }
Signature · 1 line
2.29.2
PATCH v2 3/3 qcow2: handle_dependencies(): relax conflict detection
In reply to Vladimir Sementsov-OgievskiyThere is no conflict and no dependency if we have parallel writes to different subclusters of one cluster when the cluster itself is already allocated. So, relax extra dependency.
Measure performance:
First, prepare build/qemu-img-old and build/qemu-img-new images.
cd scripts/simplebench
./img_bench_templater.py
Paste the following to stdin of running script:
qemu_img=../../build/qemu-img-{old|new}
$qemu_img create -f qcow2 -o extended_l2=on /ssd/x.qcow2 1G
$qemu_img bench -c 100000 -d 8 [-s 2K|-s 2K -o 512|-s $((1024*2+512))] \
-w -t none -n /ssd/x.qcow2The result:
All results are in seconds
------------------ --------- ---------
old new
-s 2K 6.7 ± 15% 6.2 ± 12%
-7%
-s 2K -o 512 13 ± 3% 11 ± 5%
-16%
-s $((1024*2+512)) 9.5 ± 4% 8.4
-12%------------------ --------- ---------
So small writes are more independent now and that helps to keep deeper io queue which improves performance.
271 iotest output becomes racy for three allocation in one cluster. Second and third writes may finish in different order. Second and third requests don't depend on each other any more. Still they both depend on first request anyway. Filter out second and third write offsets to cover both possible outputs.
--- block/qcow2-cluster.c | 11 +++++++++++ tests/qemu-iotests/271 | 5 ++++- tests/qemu-iotests/271.out | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-)
block/qcow2-cluster.c +11 -0
@@ -1403,6 +1403,17 @@ static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,continue ; } +if (old_alloc->keep_old_clusters && + (end <=l2meta_cow_start (old_alloc) || + start >=l2meta_cow_end (old_alloc))) + { +/* +* Clusters intersect but COW areas don't. And cluster itself is +* already allocated. So, there is no actual conflict. +*/ +continue ; + } +/* Conflict */ if (start < old_start) {
tests/qemu-iotests/271 +4 -1
@@ -893,7 +893,10 @@ EOF } _make_test_img -o extended_l2=on 1M -_concurrent_io | $QEMU_IO | _filter_qemu_io +# Second an third writes in _concurrent_io() are independent and may finish in +# different order. So, filter offset out to match both possible variants. +_concurrent_io | $QEMU_IO | _filter_qemu_io | \ + $SED -e 's/\(20480\|40960\)/OFFSET/' _concurrent_verify | $QEMU_IO | _filter_qemu_io # success, all done
tests/qemu-iotests/271.out +2 -2
@@ -719,8 +719,8 @@ blkdebug: Suspended request 'A' blkdebug: Resuming request 'A' wrote 2048/2048 bytes at offset 30720 2 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) -wrote 2048/2048 bytes at offset 20480 +wrote 2048/2048 bytes at offset OFFSET 2 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) -wrote 2048/2048 bytes at offset 40960 +wrote 2048/2048 bytes at offset OFFSET 2 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) *** done
Signature · 1 line
2.29.2
Re: [PATCH v2 3/3] qcow2: handle_dependencies(): relax conflict detection
Quoted 4 lines · Vladimir Sementsov-Ogievskiy
There is no conflict and no dependency if we have parallel writes to different subclusters of one cluster when the cluster itself is already allocated. So, relax extra dependency.
...
Quoted 19 lines
So small writes are more independent now and that helps to keep deeper io queue which improves performance.
271 iotest output becomes racy for three allocation in one cluster. Second and third writes may finish in different order. Second and third requests don't depend on each other any more. Still they both depend on first request anyway. Filter out second and third write offsets to cover both possible outputs.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/qcow2-cluster.c | 11 +++++++++++ tests/qemu-iotests/271 | 5 ++++- tests/qemu-iotests/271.out | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-)+++ b/tests/qemu-iotests/271
tests/qemu-iotests/271 EOF · line 896
Quoted 5 lines
tests/qemu-iotests/271 +1
-1@@ -893,7 +893,10 @@ EOF } _make_test_img -o extended_l2=on 1M -_concurrent_io | $QEMU_IO | _filter_qemu_io +# Second an third writes in _concurrent_io() are independent and may finish in
and
tests/qemu-iotests/271 EOF · line 901
Quoted 5 lines
tests/qemu-iotests/271 +3
-0+# different order. So, filter offset out to match both possible variants. +_concurrent_io | $QEMU_IO | _filter_qemu_io | \ + $SED -e 's/\(20480\|40960\)/OFFSET/' _concurrent_verify | $QEMU_IO | _filter_qemu_io
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v2 3/3] qcow2: handle_dependencies(): relax conflict detection
In reply to Vladimir Sementsov-OgievskiyQuoted 46 lines · Vladimir Sementsov-Ogievskiy
There is no conflict and no dependency if we have parallel writes to different subclusters of one cluster when the cluster itself is already allocated. So, relax extra dependency.
Measure performance:
First, prepare build/qemu-img-old and build/qemu-img-new images.cd
scripts/simplebench
./img_bench_templater.pyPaste the following to stdin of running script:
qemu_img=../../build/qemu-img-{old|new}
$qemu_img create -f qcow2 -o extended_l2=on /ssd/x.qcow2 1G$qemu_img bench -c 100000 -d 8 [-s 2K|-s 2K -o 512|-s $((1024*2+512))] \ -w -t none -n /ssd/x.qcow2The result:
All results are in seconds
------------------ --------- ---------
old new(quoted diff) +0
-4-s 2K 6.7 ± 15% 6.2 ± 12% -7% -s 2K -o 512 13 ± 3% 11 ± 5% -16% -s $((1024*2+512)) 9.5 ± 4% 8.4 -12% ------------------ --------- ---------So small writes are more independent now and that helps to keep deeper io queue which improves performance.
271 iotest output becomes racy for three allocation in one cluster. Second and third writes may finish in different order. Second and third requests don't depend on each other any more. Still they both depend on first request anyway. Filter out second and third write offsets to cover both possible outputs.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/qcow2-cluster.c | 11 +++++++++++ tests/qemu-iotests/271 | 5 ++++- tests/qemu-iotests/271.out | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-)
[...]
tests/qemu-iotests/271 EOF · line 896
Quoted 10 lines
tests/qemu-iotests/271 +1
-1@@ -893,7 +893,10 @@ EOF } _make_test_img -o extended_l2=on 1M -_concurrent_io | $QEMU_IO | _filter_qemu_io +# Second an third writes in _concurrent_io() are independent and may finish in
s/ an / and /
With that fixed:
Quoted 6 lines
tests/qemu-iotests/271 +3
-0+# different order. So, filter offset out to match both possible variants. +_concurrent_io | $QEMU_IO | _filter_qemu_io | \ + $SED -e 's/\(20480\|40960\)/OFFSET/' _concurrent_verify | $QEMU_IO | _filter_qemu_io # success, all done
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Hi all!
v2:
01: improve documentation
02: add Hanna's and Eric's r-bs, add tiny grammar fix 03: fix test by filtering instead of reducing number of writes
Thanks, I’ve fixed the typo in patch 3 and applied the series to my block branch:
https://github.com/XanClic/qemu/commits/block
Hanna