qemu-e2k/tests/test-write-threshold.c
Vladimir Sementsov-Ogievskiy 8b1170012b block: introduce BDRV_MAX_LENGTH
We are going to modify block layer to work with 64bit requests. And
first step is moving to int64_t type for both offset and bytes
arguments in all block request related functions.

It's mostly safe (when widening signed or unsigned int to int64_t), but
switching from uint64_t is questionable.

So, let's first establish the set of requests we want to work with.
First signed int64_t should be enough, as off_t is signed anyway. Then,
obviously offset + bytes should not overflow.

And most interesting: (offset + bytes) being aligned up should not
overflow as well. Aligned to what alignment? First thing that comes in
mind is bs->bl.request_alignment, as we align up request to this
alignment. But there is another thing: look at
bdrv_mark_request_serialising(). It aligns request up to some given
alignment. And this parameter may be bdrv_get_cluster_size(), which is
often a lot greater than bs->bl.request_alignment.
Note also, that bdrv_mark_request_serialising() uses signed int64_t for
calculations. So, actually, we already depend on some restrictions.

Happily, bdrv_get_cluster_size() returns int and
bs->bl.request_alignment has 32bit unsigned type, but defined to be a
power of 2 less than INT_MAX. So, we may establish, that INT_MAX is
absolute maximum for any kind of alignment that may occur with the
request.

Note, that bdrv_get_cluster_size() is not documented to return power
of 2, still bdrv_mark_request_serialising() behaves like it is.
Also, backup uses bdi.cluster_size and is not prepared to it not being
power of 2.
So, let's establish that Qemu supports only power-of-2 clusters and
alignments.

So, alignment can't be greater than 2^30.

Finally to be safe with calculations, to not calculate different
maximums for different nodes (depending on cluster size and
request_alignment), let's simply set QEMU_ALIGN_DOWN(INT64_MAX, 2^30)
as absolute maximum bytes length for Qemu. Actually, it's not much less
than INT64_MAX.

OK, then, let's apply it to block/io.

Let's consider all block/io entry points of offset/bytes:

4 bytes/offset interface functions: bdrv_co_preadv_part(),
bdrv_co_pwritev_part(), bdrv_co_copy_range_internal() and
bdrv_co_pdiscard() and we check them all with bdrv_check_request().

We also have one entry point with only offset: bdrv_co_truncate().
Check the offset.

And one public structure: BdrvTrackedRequest. Happily, it has only
three external users:

 file-posix.c: adopted by this patch
 write-threshold.c: only read fields
 test-write-threshold.c: sets obviously small constant values

Better is to make the structure private and add corresponding
interfaces.. Still it's not obvious what kind of interface is needed
for file-posix.c. Let's keep it public but add corresponding
assertions.

After this patch we'll convert functions in block/io.c to int64_t bytes
and offset parameters. We can assume that offset/bytes pair always
satisfy new restrictions, and make
corresponding assertions where needed. If we reach some offset/bytes
point in block/io.c missing bdrv_check_request() it is considered a
bug. As well, if block/io.c modifies a offset/bytes request, expanding
it more then aligning up to request_alignment, it's a bug too.

For all io requests except for discard we keep for now old restriction
of 32bit request length.

iotest 206 output error message changed, as now test disk size is
larger than new limit. Add one more test case with new maximum disk
size to cover too-big-L1 case.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20201203222713.13507-5-vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2020-12-11 17:52:40 +01:00

123 lines
3.0 KiB
C

/*
* Test block device write threshold
*
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
* See the COPYING.LIB file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include "block/block_int.h"
#include "block/write-threshold.h"
static void test_threshold_not_set_on_init(void)
{
uint64_t res;
BlockDriverState bs;
memset(&bs, 0, sizeof(bs));
g_assert(!bdrv_write_threshold_is_set(&bs));
res = bdrv_write_threshold_get(&bs);
g_assert_cmpint(res, ==, 0);
}
static void test_threshold_set_get(void)
{
uint64_t threshold = 4 * 1024 * 1024;
uint64_t res;
BlockDriverState bs;
memset(&bs, 0, sizeof(bs));
bdrv_write_threshold_set(&bs, threshold);
g_assert(bdrv_write_threshold_is_set(&bs));
res = bdrv_write_threshold_get(&bs);
g_assert_cmpint(res, ==, threshold);
}
static void test_threshold_multi_set_get(void)
{
uint64_t threshold1 = 4 * 1024 * 1024;
uint64_t threshold2 = 15 * 1024 * 1024;
uint64_t res;
BlockDriverState bs;
memset(&bs, 0, sizeof(bs));
bdrv_write_threshold_set(&bs, threshold1);
bdrv_write_threshold_set(&bs, threshold2);
res = bdrv_write_threshold_get(&bs);
g_assert_cmpint(res, ==, threshold2);
}
static void test_threshold_not_trigger(void)
{
uint64_t amount = 0;
uint64_t threshold = 4 * 1024 * 1024;
BlockDriverState bs;
BdrvTrackedRequest req;
memset(&bs, 0, sizeof(bs));
memset(&req, 0, sizeof(req));
req.offset = 1024;
req.bytes = 1024;
assert(bdrv_check_request(req.offset, req.bytes) == 0);
bdrv_write_threshold_set(&bs, threshold);
amount = bdrv_write_threshold_exceeded(&bs, &req);
g_assert_cmpuint(amount, ==, 0);
}
static void test_threshold_trigger(void)
{
uint64_t amount = 0;
uint64_t threshold = 4 * 1024 * 1024;
BlockDriverState bs;
BdrvTrackedRequest req;
memset(&bs, 0, sizeof(bs));
memset(&req, 0, sizeof(req));
req.offset = (4 * 1024 * 1024) - 1024;
req.bytes = 2 * 1024;
assert(bdrv_check_request(req.offset, req.bytes) == 0);
bdrv_write_threshold_set(&bs, threshold);
amount = bdrv_write_threshold_exceeded(&bs, &req);
g_assert_cmpuint(amount, >=, 1024);
}
typedef struct TestStruct {
const char *name;
void (*func)(void);
} TestStruct;
int main(int argc, char **argv)
{
size_t i;
TestStruct tests[] = {
{ "/write-threshold/not-set-on-init",
test_threshold_not_set_on_init },
{ "/write-threshold/set-get",
test_threshold_set_get },
{ "/write-threshold/multi-set-get",
test_threshold_multi_set_get },
{ "/write-threshold/not-trigger",
test_threshold_not_trigger },
{ "/write-threshold/trigger",
test_threshold_trigger },
{ NULL, NULL }
};
g_test_init(&argc, &argv, NULL);
for (i = 0; tests[i].name != NULL; i++) {
g_test_add_func(tests[i].name, tests[i].func);
}
return g_test_run();
}