2 * Throttle infrastructure tests
4 * Copyright Nodalink, EURL. 2013-2014
5 * Copyright Igalia, S.L. 2015
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
11 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
12 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
18 #include "block/aio.h"
19 #include "qemu/throttle.h"
20 #include "qemu/error-report.h"
21 #include "block/throttle-groups.h"
23 static AioContext
*ctx
;
24 static LeakyBucket bkt
;
25 static ThrottleConfig cfg
;
26 static ThrottleState ts
;
27 static ThrottleTimers tt
;
30 static bool double_cmp(double x
, double y
)
32 return fabsl(x
- y
) < 1e-6;
35 /* tests for single bucket operations */
36 static void test_leak_bucket(void)
38 /* set initial value */
43 /* leak an op work of time */
44 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
45 g_assert(bkt
.avg
== 150);
46 g_assert(bkt
.max
== 15);
47 g_assert(double_cmp(bkt
.level
, 0.5));
49 /* leak again emptying the bucket */
50 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
51 g_assert(bkt
.avg
== 150);
52 g_assert(bkt
.max
== 15);
53 g_assert(double_cmp(bkt
.level
, 0));
55 /* check that the bucket level won't go lower */
56 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
57 g_assert(bkt
.avg
== 150);
58 g_assert(bkt
.max
== 15);
59 g_assert(double_cmp(bkt
.level
, 0));
62 static void test_compute_wait(void)
67 /* no operation limit set */
71 wait
= throttle_compute_wait(&bkt
);
78 wait
= throttle_compute_wait(&bkt
);
81 /* below zero delta */
85 wait
= throttle_compute_wait(&bkt
);
88 /* half an operation above max */
92 wait
= throttle_compute_wait(&bkt
);
93 /* time required to do half an operation */
94 result
= (int64_t) NANOSECONDS_PER_SECOND
/ 150 / 2;
95 g_assert(wait
== result
);
98 /* functions to test ThrottleState initialization/destroy methods */
99 static void read_timer_cb(void *opaque
)
103 static void write_timer_cb(void *opaque
)
107 static void test_init(void)
111 /* fill the structures with crap */
112 memset(&ts
, 1, sizeof(ts
));
113 memset(&tt
, 1, sizeof(tt
));
115 /* init structures */
117 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
118 read_timer_cb
, write_timer_cb
, &ts
);
120 /* check initialized fields */
121 g_assert(tt
.clock_type
== QEMU_CLOCK_VIRTUAL
);
122 g_assert(tt
.timers
[0]);
123 g_assert(tt
.timers
[1]);
125 /* check other fields where cleared */
126 g_assert(!ts
.previous_leak
);
127 g_assert(!ts
.cfg
.op_size
);
128 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
129 g_assert(!ts
.cfg
.buckets
[i
].avg
);
130 g_assert(!ts
.cfg
.buckets
[i
].max
);
131 g_assert(!ts
.cfg
.buckets
[i
].level
);
134 throttle_timers_destroy(&tt
);
137 static void test_destroy(void)
141 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
142 read_timer_cb
, write_timer_cb
, &ts
);
143 throttle_timers_destroy(&tt
);
144 for (i
= 0; i
< 2; i
++) {
145 g_assert(!tt
.timers
[i
]);
149 /* function to test throttle_config and throttle_get_config */
150 static void test_config_functions(void)
153 ThrottleConfig orig_cfg
, final_cfg
;
155 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= 153;
156 orig_cfg
.buckets
[THROTTLE_BPS_READ
].avg
= 56;
157 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1;
159 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= 150;
160 orig_cfg
.buckets
[THROTTLE_OPS_READ
].avg
= 69;
161 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= 23;
163 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= 0; /* should be corrected */
164 orig_cfg
.buckets
[THROTTLE_BPS_READ
].max
= 1; /* should not be corrected */
165 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= 120;
167 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= 150;
168 orig_cfg
.buckets
[THROTTLE_OPS_READ
].max
= 400;
169 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= 500;
171 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
= 45;
172 orig_cfg
.buckets
[THROTTLE_BPS_READ
].level
= 65;
173 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].level
= 23;
175 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
= 1;
176 orig_cfg
.buckets
[THROTTLE_OPS_READ
].level
= 90;
177 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].level
= 75;
179 orig_cfg
.op_size
= 1;
182 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
183 read_timer_cb
, write_timer_cb
, &ts
);
184 /* structure reset by throttle_init previous_leak should be null */
185 g_assert(!ts
.previous_leak
);
186 throttle_config(&ts
, &tt
, &orig_cfg
);
188 /* has previous leak been initialized by throttle_config ? */
189 g_assert(ts
.previous_leak
);
191 /* get back the fixed configuration */
192 throttle_get_config(&ts
, &final_cfg
);
194 throttle_timers_destroy(&tt
);
196 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
== 153);
197 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].avg
== 56);
198 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
== 1);
200 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
== 150);
201 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].avg
== 69);
202 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
== 23);
204 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
== 15.3);/* fixed */
205 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].max
== 1); /* not fixed */
206 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
== 120);
208 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
== 150);
209 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].max
== 400);
210 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
== 500);
212 g_assert(final_cfg
.op_size
== 1);
214 /* check bucket have been cleared */
215 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
216 g_assert(!final_cfg
.buckets
[i
].level
);
220 /* functions to test is throttle is enabled by a config */
221 static void set_cfg_value(bool is_max
, int index
, int value
)
224 cfg
.buckets
[index
].max
= value
;
226 cfg
.buckets
[index
].avg
= value
;
230 static void test_enabled(void)
234 memset(&cfg
, 0, sizeof(cfg
));
235 g_assert(!throttle_enabled(&cfg
));
237 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
238 memset(&cfg
, 0, sizeof(cfg
));
239 set_cfg_value(false, i
, 150);
240 g_assert(throttle_enabled(&cfg
));
243 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
244 memset(&cfg
, 0, sizeof(cfg
));
245 set_cfg_value(false, i
, -150);
246 g_assert(!throttle_enabled(&cfg
));
250 /* tests functions for throttle_conflicting */
252 static void test_conflicts_for_one_set(bool is_max
,
257 memset(&cfg
, 0, sizeof(cfg
));
258 g_assert(!throttle_conflicting(&cfg
));
260 set_cfg_value(is_max
, total
, 1);
261 set_cfg_value(is_max
, read
, 1);
262 g_assert(throttle_conflicting(&cfg
));
264 memset(&cfg
, 0, sizeof(cfg
));
265 set_cfg_value(is_max
, total
, 1);
266 set_cfg_value(is_max
, write
, 1);
267 g_assert(throttle_conflicting(&cfg
));
269 memset(&cfg
, 0, sizeof(cfg
));
270 set_cfg_value(is_max
, total
, 1);
271 set_cfg_value(is_max
, read
, 1);
272 set_cfg_value(is_max
, write
, 1);
273 g_assert(throttle_conflicting(&cfg
));
275 memset(&cfg
, 0, sizeof(cfg
));
276 set_cfg_value(is_max
, total
, 1);
277 g_assert(!throttle_conflicting(&cfg
));
279 memset(&cfg
, 0, sizeof(cfg
));
280 set_cfg_value(is_max
, read
, 1);
281 set_cfg_value(is_max
, write
, 1);
282 g_assert(!throttle_conflicting(&cfg
));
285 static void test_conflicting_config(void)
287 /* bps average conflicts */
288 test_conflicts_for_one_set(false,
293 /* ops average conflicts */
294 test_conflicts_for_one_set(false,
299 /* bps average conflicts */
300 test_conflicts_for_one_set(true,
304 /* ops average conflicts */
305 test_conflicts_for_one_set(true,
310 /* functions to test the throttle_is_valid function */
311 static void test_is_valid_for_value(int value
, bool should_be_valid
)
314 for (is_max
= 0; is_max
< 2; is_max
++) {
315 for (index
= 0; index
< BUCKETS_COUNT
; index
++) {
316 memset(&cfg
, 0, sizeof(cfg
));
317 set_cfg_value(is_max
, index
, value
);
318 g_assert(throttle_is_valid(&cfg
) == should_be_valid
);
323 static void test_is_valid(void)
325 /* negative number are invalid */
326 test_is_valid_for_value(-1, false);
327 /* zero are valids */
328 test_is_valid_for_value(0, true);
329 /* positives numers are valids */
330 test_is_valid_for_value(1, true);
333 static void test_max_is_missing_limit(void)
337 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
338 memset(&cfg
, 0, sizeof(cfg
));
339 cfg
.buckets
[i
].max
= 100;
340 cfg
.buckets
[i
].avg
= 0;
341 g_assert(throttle_max_is_missing_limit(&cfg
));
343 cfg
.buckets
[i
].max
= 0;
344 cfg
.buckets
[i
].avg
= 0;
345 g_assert(!throttle_max_is_missing_limit(&cfg
));
347 cfg
.buckets
[i
].max
= 0;
348 cfg
.buckets
[i
].avg
= 100;
349 g_assert(!throttle_max_is_missing_limit(&cfg
));
353 static void test_have_timer(void)
355 /* zero structures */
356 memset(&ts
, 0, sizeof(ts
));
357 memset(&tt
, 0, sizeof(tt
));
359 /* no timer set should return false */
360 g_assert(!throttle_timers_are_initialized(&tt
));
362 /* init structures */
364 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
365 read_timer_cb
, write_timer_cb
, &ts
);
367 /* timer set by init should return true */
368 g_assert(throttle_timers_are_initialized(&tt
));
370 throttle_timers_destroy(&tt
);
373 static void test_detach_attach(void)
375 /* zero structures */
376 memset(&ts
, 0, sizeof(ts
));
377 memset(&tt
, 0, sizeof(tt
));
379 /* init the structure */
381 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
382 read_timer_cb
, write_timer_cb
, &ts
);
384 /* timer set by init should return true */
385 g_assert(throttle_timers_are_initialized(&tt
));
387 /* timer should no longer exist after detaching */
388 throttle_timers_detach_aio_context(&tt
);
389 g_assert(!throttle_timers_are_initialized(&tt
));
391 /* timer should exist again after attaching */
392 throttle_timers_attach_aio_context(&tt
, ctx
);
393 g_assert(throttle_timers_are_initialized(&tt
));
395 throttle_timers_destroy(&tt
);
398 static bool do_test_accounting(bool is_ops
, /* are we testing bps or ops */
399 int size
, /* size of the operation to do */
400 double avg
, /* io limit */
401 uint64_t op_size
, /* ideal size of an io */
406 BucketType to_test
[2][3] = { { THROTTLE_BPS_TOTAL
,
408 THROTTLE_BPS_WRITE
, },
409 { THROTTLE_OPS_TOTAL
,
411 THROTTLE_OPS_WRITE
, } };
416 for (i
= 0; i
< 3; i
++) {
417 BucketType index
= to_test
[is_ops
][i
];
418 cfg
.buckets
[index
].avg
= avg
;
421 cfg
.op_size
= op_size
;
424 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
425 read_timer_cb
, write_timer_cb
, &ts
);
426 throttle_config(&ts
, &tt
, &cfg
);
429 throttle_account(&ts
, false, size
);
430 /* account a write */
431 throttle_account(&ts
, true, size
);
433 /* check total result */
434 index
= to_test
[is_ops
][0];
435 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, total_result
)) {
439 /* check read result */
440 index
= to_test
[is_ops
][1];
441 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, read_result
)) {
445 /* check write result */
446 index
= to_test
[is_ops
][2];
447 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, write_result
)) {
451 throttle_timers_destroy(&tt
);
456 static void test_accounting(void)
461 g_assert(do_test_accounting(false,
470 g_assert(do_test_accounting(false,
478 /* op of size 2 and orthogonal parameter change */
479 g_assert(do_test_accounting(false,
491 g_assert(do_test_accounting(true,
500 g_assert(do_test_accounting(true,
508 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
509 g_assert(do_test_accounting(true,
517 /* same with orthogonal parameters changes */
518 g_assert(do_test_accounting(true,
527 static void test_groups(void)
529 ThrottleConfig cfg1
, cfg2
;
530 BlockDriverState
*bdrv1
, *bdrv2
, *bdrv3
;
536 g_assert(bdrv1
->throttle_state
== NULL
);
537 g_assert(bdrv2
->throttle_state
== NULL
);
538 g_assert(bdrv3
->throttle_state
== NULL
);
540 throttle_group_register_bs(bdrv1
, "bar");
541 throttle_group_register_bs(bdrv2
, "foo");
542 throttle_group_register_bs(bdrv3
, "bar");
544 g_assert(bdrv1
->throttle_state
!= NULL
);
545 g_assert(bdrv2
->throttle_state
!= NULL
);
546 g_assert(bdrv3
->throttle_state
!= NULL
);
548 g_assert(!strcmp(throttle_group_get_name(bdrv1
), "bar"));
549 g_assert(!strcmp(throttle_group_get_name(bdrv2
), "foo"));
550 g_assert(bdrv1
->throttle_state
== bdrv3
->throttle_state
);
552 /* Setting the config of a group member affects the whole group */
553 memset(&cfg1
, 0, sizeof(cfg1
));
554 cfg1
.buckets
[THROTTLE_BPS_READ
].avg
= 500000;
555 cfg1
.buckets
[THROTTLE_BPS_WRITE
].avg
= 285000;
556 cfg1
.buckets
[THROTTLE_OPS_READ
].avg
= 20000;
557 cfg1
.buckets
[THROTTLE_OPS_WRITE
].avg
= 12000;
558 throttle_group_config(bdrv1
, &cfg1
);
560 throttle_group_get_config(bdrv1
, &cfg1
);
561 throttle_group_get_config(bdrv3
, &cfg2
);
562 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
564 cfg2
.buckets
[THROTTLE_BPS_READ
].avg
= 4547;
565 cfg2
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1349;
566 cfg2
.buckets
[THROTTLE_OPS_READ
].avg
= 123;
567 cfg2
.buckets
[THROTTLE_OPS_WRITE
].avg
= 86;
568 throttle_group_config(bdrv3
, &cfg1
);
570 throttle_group_get_config(bdrv1
, &cfg1
);
571 throttle_group_get_config(bdrv3
, &cfg2
);
572 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
574 throttle_group_unregister_bs(bdrv1
);
575 throttle_group_unregister_bs(bdrv2
);
576 throttle_group_unregister_bs(bdrv3
);
578 g_assert(bdrv1
->throttle_state
== NULL
);
579 g_assert(bdrv2
->throttle_state
== NULL
);
580 g_assert(bdrv3
->throttle_state
== NULL
);
583 int main(int argc
, char **argv
)
585 qemu_init_main_loop(&error_fatal
);
586 ctx
= qemu_get_aio_context();
589 do {} while (g_main_context_iteration(NULL
, false));
591 /* tests in the same order as the header function declarations */
592 g_test_init(&argc
, &argv
, NULL
);
593 g_test_add_func("/throttle/leak_bucket", test_leak_bucket
);
594 g_test_add_func("/throttle/compute_wait", test_compute_wait
);
595 g_test_add_func("/throttle/init", test_init
);
596 g_test_add_func("/throttle/destroy", test_destroy
);
597 g_test_add_func("/throttle/have_timer", test_have_timer
);
598 g_test_add_func("/throttle/detach_attach", test_detach_attach
);
599 g_test_add_func("/throttle/config/enabled", test_enabled
);
600 g_test_add_func("/throttle/config/conflicting", test_conflicting_config
);
601 g_test_add_func("/throttle/config/is_valid", test_is_valid
);
602 g_test_add_func("/throttle/config/max", test_max_is_missing_limit
);
603 g_test_add_func("/throttle/config_functions", test_config_functions
);
604 g_test_add_func("/throttle/accounting", test_accounting
);
605 g_test_add_func("/throttle/groups", test_groups
);