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