xref: /qemu/tests/unit/test-write-threshold.c (revision 7cebff0d)
1 /*
2  * Test block device write threshold
3  *
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.
6  *
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "block/block_int.h"
12 #include "block/write-threshold.h"
13 
14 
15 static void test_threshold_not_set_on_init(void)
16 {
17     uint64_t res;
18     BlockDriverState bs;
19     memset(&bs, 0, sizeof(bs));
20 
21     g_assert(!bdrv_write_threshold_is_set(&bs));
22 
23     res = bdrv_write_threshold_get(&bs);
24     g_assert_cmpint(res, ==, 0);
25 }
26 
27 static void test_threshold_set_get(void)
28 {
29     uint64_t threshold = 4 * 1024 * 1024;
30     uint64_t res;
31     BlockDriverState bs;
32     memset(&bs, 0, sizeof(bs));
33 
34     bdrv_write_threshold_set(&bs, threshold);
35 
36     g_assert(bdrv_write_threshold_is_set(&bs));
37 
38     res = bdrv_write_threshold_get(&bs);
39     g_assert_cmpint(res, ==, threshold);
40 }
41 
42 static void test_threshold_multi_set_get(void)
43 {
44     uint64_t threshold1 = 4 * 1024 * 1024;
45     uint64_t threshold2 = 15 * 1024 * 1024;
46     uint64_t res;
47     BlockDriverState bs;
48     memset(&bs, 0, sizeof(bs));
49 
50     bdrv_write_threshold_set(&bs, threshold1);
51     bdrv_write_threshold_set(&bs, threshold2);
52     res = bdrv_write_threshold_get(&bs);
53     g_assert_cmpint(res, ==, threshold2);
54 }
55 
56 static void test_threshold_not_trigger(void)
57 {
58     uint64_t amount = 0;
59     uint64_t threshold = 4 * 1024 * 1024;
60     BlockDriverState bs;
61     BdrvTrackedRequest req;
62 
63     memset(&bs, 0, sizeof(bs));
64     memset(&req, 0, sizeof(req));
65     req.offset = 1024;
66     req.bytes = 1024;
67 
68     bdrv_check_request(req.offset, req.bytes, &error_abort);
69 
70     bdrv_write_threshold_set(&bs, threshold);
71     amount = bdrv_write_threshold_exceeded(&bs, &req);
72     g_assert_cmpuint(amount, ==, 0);
73 }
74 
75 
76 static void test_threshold_trigger(void)
77 {
78     uint64_t amount = 0;
79     uint64_t threshold = 4 * 1024 * 1024;
80     BlockDriverState bs;
81     BdrvTrackedRequest req;
82 
83     memset(&bs, 0, sizeof(bs));
84     memset(&req, 0, sizeof(req));
85     req.offset = (4 * 1024 * 1024) - 1024;
86     req.bytes = 2 * 1024;
87 
88     bdrv_check_request(req.offset, req.bytes, &error_abort);
89 
90     bdrv_write_threshold_set(&bs, threshold);
91     amount = bdrv_write_threshold_exceeded(&bs, &req);
92     g_assert_cmpuint(amount, >=, 1024);
93 }
94 
95 typedef struct TestStruct {
96     const char *name;
97     void (*func)(void);
98 } TestStruct;
99 
100 
101 int main(int argc, char **argv)
102 {
103     size_t i;
104     TestStruct tests[] = {
105         { "/write-threshold/not-set-on-init",
106           test_threshold_not_set_on_init },
107         { "/write-threshold/set-get",
108           test_threshold_set_get },
109         { "/write-threshold/multi-set-get",
110           test_threshold_multi_set_get },
111         { "/write-threshold/not-trigger",
112           test_threshold_not_trigger },
113         { "/write-threshold/trigger",
114           test_threshold_trigger },
115         { NULL, NULL }
116     };
117 
118     g_test_init(&argc, &argv, NULL);
119     for (i = 0; tests[i].name != NULL; i++) {
120         g_test_add_func(tests[i].name, tests[i].func);
121     }
122     return g_test_run();
123 }
124