Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20170927a' into...
[qemu.git] / tests / test-write-threshold.c
blob97ca12f710870fc92838fbddc963d9b9de62a283
1 /*
2 * Test block device write threshold
4 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
5 * See the COPYING.LIB file in the top-level directory.
7 */
9 #include "qemu/osdep.h"
10 #include "block/block_int.h"
11 #include "block/write-threshold.h"
14 static void test_threshold_not_set_on_init(void)
16 uint64_t res;
17 BlockDriverState bs;
18 memset(&bs, 0, sizeof(bs));
20 g_assert(!bdrv_write_threshold_is_set(&bs));
22 res = bdrv_write_threshold_get(&bs);
23 g_assert_cmpint(res, ==, 0);
26 static void test_threshold_set_get(void)
28 uint64_t threshold = 4 * 1024 * 1024;
29 uint64_t res;
30 BlockDriverState bs;
31 memset(&bs, 0, sizeof(bs));
33 bdrv_write_threshold_set(&bs, threshold);
35 g_assert(bdrv_write_threshold_is_set(&bs));
37 res = bdrv_write_threshold_get(&bs);
38 g_assert_cmpint(res, ==, threshold);
41 static void test_threshold_multi_set_get(void)
43 uint64_t threshold1 = 4 * 1024 * 1024;
44 uint64_t threshold2 = 15 * 1024 * 1024;
45 uint64_t res;
46 BlockDriverState bs;
47 memset(&bs, 0, sizeof(bs));
49 bdrv_write_threshold_set(&bs, threshold1);
50 bdrv_write_threshold_set(&bs, threshold2);
51 res = bdrv_write_threshold_get(&bs);
52 g_assert_cmpint(res, ==, threshold2);
55 static void test_threshold_not_trigger(void)
57 uint64_t amount = 0;
58 uint64_t threshold = 4 * 1024 * 1024;
59 BlockDriverState bs;
60 BdrvTrackedRequest req;
62 memset(&bs, 0, sizeof(bs));
63 memset(&req, 0, sizeof(req));
64 req.offset = 1024;
65 req.bytes = 1024;
67 bdrv_write_threshold_set(&bs, threshold);
68 amount = bdrv_write_threshold_exceeded(&bs, &req);
69 g_assert_cmpuint(amount, ==, 0);
73 static void test_threshold_trigger(void)
75 uint64_t amount = 0;
76 uint64_t threshold = 4 * 1024 * 1024;
77 BlockDriverState bs;
78 BdrvTrackedRequest req;
80 memset(&bs, 0, sizeof(bs));
81 memset(&req, 0, sizeof(req));
82 req.offset = (4 * 1024 * 1024) - 1024;
83 req.bytes = 2 * 1024;
85 bdrv_write_threshold_set(&bs, threshold);
86 amount = bdrv_write_threshold_exceeded(&bs, &req);
87 g_assert_cmpuint(amount, >=, 1024);
90 typedef struct TestStruct {
91 const char *name;
92 void (*func)(void);
93 } TestStruct;
96 int main(int argc, char **argv)
98 size_t i;
99 TestStruct tests[] = {
100 { "/write-threshold/not-set-on-init",
101 test_threshold_not_set_on_init },
102 { "/write-threshold/set-get",
103 test_threshold_set_get },
104 { "/write-threshold/multi-set-get",
105 test_threshold_multi_set_get },
106 { "/write-threshold/not-trigger",
107 test_threshold_not_trigger },
108 { "/write-threshold/trigger",
109 test_threshold_trigger },
110 { NULL, NULL }
113 g_test_init(&argc, &argv, NULL);
114 for (i = 0; tests[i].name != NULL; i++) {
115 g_test_add_func(tests[i].name, tests[i].func);
117 return g_test_run();