2 * Throttle infrastructure tests
4 * Copyright Nodalink, SARL. 2013
7 * BenoƮt Canet <benoit.canet@irqsave.net>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/throttle.h"
22 static bool double_cmp(double x
, double y
)
24 return fabsl(x
- y
) < 1e-6;
27 /* tests for single bucket operations */
28 static void test_leak_bucket(void)
30 /* set initial value */
35 /* leak an op work of time */
36 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
37 g_assert(bkt
.avg
== 150);
38 g_assert(bkt
.max
== 15);
39 g_assert(double_cmp(bkt
.level
, 0.5));
41 /* leak again emptying the bucket */
42 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
43 g_assert(bkt
.avg
== 150);
44 g_assert(bkt
.max
== 15);
45 g_assert(double_cmp(bkt
.level
, 0));
47 /* check that the bucket level won't go lower */
48 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
49 g_assert(bkt
.avg
== 150);
50 g_assert(bkt
.max
== 15);
51 g_assert(double_cmp(bkt
.level
, 0));
54 static void test_compute_wait(void)
59 /* no operation limit set */
63 wait
= throttle_compute_wait(&bkt
);
70 wait
= throttle_compute_wait(&bkt
);
73 /* below zero delta */
77 wait
= throttle_compute_wait(&bkt
);
80 /* half an operation above max */
84 wait
= throttle_compute_wait(&bkt
);
85 /* time required to do half an operation */
86 result
= (int64_t) NANOSECONDS_PER_SECOND
/ 150 / 2;
87 g_assert(wait
== result
);
90 /* functions to test ThrottleState initialization/destroy methods */
91 static void read_timer_cb(void *opaque
)
95 static void write_timer_cb(void *opaque
)
99 static void test_init(void)
103 /* fill the structure with crap */
104 memset(&ts
, 1, sizeof(ts
));
106 /* init the structure */
107 throttle_init(&ts
, QEMU_CLOCK_VIRTUAL
, read_timer_cb
, write_timer_cb
, &ts
);
109 /* check initialized fields */
110 g_assert(ts
.clock_type
== QEMU_CLOCK_VIRTUAL
);
111 g_assert(ts
.timers
[0]);
112 g_assert(ts
.timers
[1]);
114 /* check other fields where cleared */
115 g_assert(!ts
.previous_leak
);
116 g_assert(!ts
.cfg
.op_size
);
117 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
118 g_assert(!ts
.cfg
.buckets
[i
].avg
);
119 g_assert(!ts
.cfg
.buckets
[i
].max
);
120 g_assert(!ts
.cfg
.buckets
[i
].level
);
123 throttle_destroy(&ts
);
126 static void test_destroy(void)
129 throttle_init(&ts
, QEMU_CLOCK_VIRTUAL
, read_timer_cb
, write_timer_cb
, &ts
);
130 throttle_destroy(&ts
);
131 for (i
= 0; i
< 2; i
++) {
132 g_assert(!ts
.timers
[i
]);
136 /* function to test throttle_config and throttle_get_config */
137 static void test_config_functions(void)
140 ThrottleConfig orig_cfg
, final_cfg
;
142 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= 153;
143 orig_cfg
.buckets
[THROTTLE_BPS_READ
].avg
= 56;
144 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1;
146 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= 150;
147 orig_cfg
.buckets
[THROTTLE_OPS_READ
].avg
= 69;
148 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= 23;
150 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= 0; /* should be corrected */
151 orig_cfg
.buckets
[THROTTLE_BPS_READ
].max
= 1; /* should not be corrected */
152 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= 120;
154 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= 150;
155 orig_cfg
.buckets
[THROTTLE_OPS_READ
].max
= 400;
156 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= 500;
158 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
= 45;
159 orig_cfg
.buckets
[THROTTLE_BPS_READ
].level
= 65;
160 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].level
= 23;
162 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
= 1;
163 orig_cfg
.buckets
[THROTTLE_OPS_READ
].level
= 90;
164 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].level
= 75;
166 orig_cfg
.op_size
= 1;
168 throttle_init(&ts
, QEMU_CLOCK_VIRTUAL
, read_timer_cb
, write_timer_cb
, &ts
);
169 /* structure reset by throttle_init previous_leak should be null */
170 g_assert(!ts
.previous_leak
);
171 throttle_config(&ts
, &orig_cfg
);
173 /* has previous leak been initialized by throttle_config ? */
174 g_assert(ts
.previous_leak
);
176 /* get back the fixed configuration */
177 throttle_get_config(&ts
, &final_cfg
);
179 throttle_destroy(&ts
);
181 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
== 153);
182 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].avg
== 56);
183 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
== 1);
185 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
== 150);
186 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].avg
== 69);
187 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
== 23);
189 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
== 15.3);/* fixed */
190 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].max
== 1); /* not fixed */
191 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
== 120);
193 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
== 150);
194 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].max
== 400);
195 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
== 500);
197 g_assert(final_cfg
.op_size
== 1);
199 /* check bucket have been cleared */
200 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
201 g_assert(!final_cfg
.buckets
[i
].level
);
205 /* functions to test is throttle is enabled by a config */
206 static void set_cfg_value(bool is_max
, int index
, int value
)
209 cfg
.buckets
[index
].max
= value
;
211 cfg
.buckets
[index
].avg
= value
;
215 static void test_enabled(void)
219 memset(&cfg
, 0, sizeof(cfg
));
220 g_assert(!throttle_enabled(&cfg
));
222 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
223 memset(&cfg
, 0, sizeof(cfg
));
224 set_cfg_value(false, i
, 150);
225 g_assert(throttle_enabled(&cfg
));
228 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
229 memset(&cfg
, 0, sizeof(cfg
));
230 set_cfg_value(false, i
, -150);
231 g_assert(!throttle_enabled(&cfg
));
235 /* tests functions for throttle_conflicting */
237 static void test_conflicts_for_one_set(bool is_max
,
242 memset(&cfg
, 0, sizeof(cfg
));
243 g_assert(!throttle_conflicting(&cfg
));
245 set_cfg_value(is_max
, total
, 1);
246 set_cfg_value(is_max
, read
, 1);
247 g_assert(throttle_conflicting(&cfg
));
249 memset(&cfg
, 0, sizeof(cfg
));
250 set_cfg_value(is_max
, total
, 1);
251 set_cfg_value(is_max
, write
, 1);
252 g_assert(throttle_conflicting(&cfg
));
254 memset(&cfg
, 0, sizeof(cfg
));
255 set_cfg_value(is_max
, total
, 1);
256 set_cfg_value(is_max
, read
, 1);
257 set_cfg_value(is_max
, write
, 1);
258 g_assert(throttle_conflicting(&cfg
));
260 memset(&cfg
, 0, sizeof(cfg
));
261 set_cfg_value(is_max
, total
, 1);
262 g_assert(!throttle_conflicting(&cfg
));
264 memset(&cfg
, 0, sizeof(cfg
));
265 set_cfg_value(is_max
, read
, 1);
266 set_cfg_value(is_max
, write
, 1);
267 g_assert(!throttle_conflicting(&cfg
));
270 static void test_conflicting_config(void)
272 /* bps average conflicts */
273 test_conflicts_for_one_set(false,
278 /* ops average conflicts */
279 test_conflicts_for_one_set(false,
284 /* bps average conflicts */
285 test_conflicts_for_one_set(true,
289 /* ops average conflicts */
290 test_conflicts_for_one_set(true,
295 /* functions to test the throttle_is_valid function */
296 static void test_is_valid_for_value(int value
, bool should_be_valid
)
299 for (is_max
= 0; is_max
< 2; is_max
++) {
300 for (index
= 0; index
< BUCKETS_COUNT
; index
++) {
301 memset(&cfg
, 0, sizeof(cfg
));
302 set_cfg_value(is_max
, index
, value
);
303 g_assert(throttle_is_valid(&cfg
) == should_be_valid
);
308 static void test_is_valid(void)
310 /* negative number are invalid */
311 test_is_valid_for_value(-1, false);
312 /* zero are valids */
313 test_is_valid_for_value(0, true);
314 /* positives numers are valids */
315 test_is_valid_for_value(1, true);
318 static void test_have_timer(void)
320 /* zero the structure */
321 memset(&ts
, 0, sizeof(ts
));
323 /* no timer set should return false */
324 g_assert(!throttle_have_timer(&ts
));
326 /* init the structure */
327 throttle_init(&ts
, QEMU_CLOCK_VIRTUAL
, read_timer_cb
, write_timer_cb
, &ts
);
329 /* timer set by init should return true */
330 g_assert(throttle_have_timer(&ts
));
332 throttle_destroy(&ts
);
335 static bool do_test_accounting(bool is_ops
, /* are we testing bps or ops */
336 int size
, /* size of the operation to do */
337 double avg
, /* io limit */
338 uint64_t op_size
, /* ideal size of an io */
343 BucketType to_test
[2][3] = { { THROTTLE_BPS_TOTAL
,
345 THROTTLE_BPS_WRITE
, },
346 { THROTTLE_OPS_TOTAL
,
348 THROTTLE_OPS_WRITE
, } };
353 for (i
= 0; i
< 3; i
++) {
354 BucketType index
= to_test
[is_ops
][i
];
355 cfg
.buckets
[index
].avg
= avg
;
358 cfg
.op_size
= op_size
;
360 throttle_init(&ts
, QEMU_CLOCK_VIRTUAL
, read_timer_cb
, write_timer_cb
, &ts
);
361 throttle_config(&ts
, &cfg
);
364 throttle_account(&ts
, false, size
);
365 /* account a write */
366 throttle_account(&ts
, true, size
);
368 /* check total result */
369 index
= to_test
[is_ops
][0];
370 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, total_result
)) {
374 /* check read result */
375 index
= to_test
[is_ops
][1];
376 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, read_result
)) {
380 /* check write result */
381 index
= to_test
[is_ops
][2];
382 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, write_result
)) {
386 throttle_destroy(&ts
);
391 static void test_accounting(void)
396 g_assert(do_test_accounting(false,
405 g_assert(do_test_accounting(false,
413 /* op of size 2 and orthogonal parameter change */
414 g_assert(do_test_accounting(false,
426 g_assert(do_test_accounting(true,
435 g_assert(do_test_accounting(true,
443 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
444 g_assert(do_test_accounting(true,
452 /* same with orthogonal parameters changes */
453 g_assert(do_test_accounting(true,
462 int main(int argc
, char **argv
)
465 do {} while (g_main_context_iteration(NULL
, false));
467 /* tests in the same order as the header function declarations */
468 g_test_init(&argc
, &argv
, NULL
);
469 g_test_add_func("/throttle/leak_bucket", test_leak_bucket
);
470 g_test_add_func("/throttle/compute_wait", test_compute_wait
);
471 g_test_add_func("/throttle/init", test_init
);
472 g_test_add_func("/throttle/destroy", test_destroy
);
473 g_test_add_func("/throttle/have_timer", test_have_timer
);
474 g_test_add_func("/throttle/config/enabled", test_enabled
);
475 g_test_add_func("/throttle/config/conflicting", test_conflicting_config
);
476 g_test_add_func("/throttle/config/is_valid", test_is_valid
);
477 g_test_add_func("/throttle/config_functions", test_config_functions
);
478 g_test_add_func("/throttle/accounting", test_accounting
);