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 "qapi/error.h"
20 #include "qemu/throttle.h"
21 #include "qemu/error-report.h"
22 #include "block/throttle-groups.h"
23 #include "sysemu/block-backend.h"
25 static AioContext
*ctx
;
26 static LeakyBucket bkt
;
27 static ThrottleConfig cfg
;
28 static ThrottleState ts
;
29 static ThrottleTimers tt
;
32 static bool double_cmp(double x
, double y
)
34 return fabsl(x
- y
) < 1e-6;
37 /* tests for single bucket operations */
38 static void test_leak_bucket(void)
40 throttle_config_init(&cfg
);
41 bkt
= cfg
.buckets
[THROTTLE_BPS_TOTAL
];
43 /* set initial value */
48 /* leak an op work of time */
49 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
50 g_assert(bkt
.avg
== 150);
51 g_assert(bkt
.max
== 15);
52 g_assert(double_cmp(bkt
.level
, 0.5));
54 /* leak again emptying the bucket */
55 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
56 g_assert(bkt
.avg
== 150);
57 g_assert(bkt
.max
== 15);
58 g_assert(double_cmp(bkt
.level
, 0));
60 /* check that the bucket level won't go lower */
61 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
62 g_assert(bkt
.avg
== 150);
63 g_assert(bkt
.max
== 15);
64 g_assert(double_cmp(bkt
.level
, 0));
66 /* check that burst_level leaks correctly */
69 bkt
.burst_length
= 2; /* otherwise burst_level will not leak */
70 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 100);
71 g_assert(double_cmp(bkt
.burst_level
, 3.5));
73 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 100);
74 g_assert(double_cmp(bkt
.burst_level
, 1));
76 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 100);
77 g_assert(double_cmp(bkt
.burst_level
, 0));
79 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 100);
80 g_assert(double_cmp(bkt
.burst_level
, 0));
83 static void test_compute_wait(void)
89 throttle_config_init(&cfg
);
90 bkt
= cfg
.buckets
[THROTTLE_BPS_TOTAL
];
92 /* no operation limit set */
96 wait
= throttle_compute_wait(&bkt
);
103 wait
= throttle_compute_wait(&bkt
);
106 /* below zero delta */
110 wait
= throttle_compute_wait(&bkt
);
113 /* half an operation above max */
117 wait
= throttle_compute_wait(&bkt
);
118 /* time required to do half an operation */
119 result
= (int64_t) NANOSECONDS_PER_SECOND
/ 150 / 2;
120 g_assert(wait
== result
);
122 /* Perform I/O for 2.2 seconds at a rate of bkt.max */
123 bkt
.burst_length
= 2;
127 for (i
= 0; i
< 22; i
++) {
128 double units
= bkt
.max
/ 10;
130 bkt
.burst_level
+= units
;
131 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 10);
132 wait
= throttle_compute_wait(&bkt
);
133 g_assert(double_cmp(bkt
.burst_level
, 0));
134 g_assert(double_cmp(bkt
.level
, (i
+ 1) * (bkt
.max
- bkt
.avg
) / 10));
135 /* We can do bursts for the 2 seconds we have configured in
136 * burst_length. We have 100 extra miliseconds of burst
137 * because bkt.level has been leaking during this time.
138 * After that, we have to wait. */
139 result
= i
< 21 ? 0 : 1.8 * NANOSECONDS_PER_SECOND
;
140 g_assert(wait
== result
);
144 /* functions to test ThrottleState initialization/destroy methods */
145 static void read_timer_cb(void *opaque
)
149 static void write_timer_cb(void *opaque
)
153 static void test_init(void)
157 /* fill the structures with crap */
158 memset(&ts
, 1, sizeof(ts
));
159 memset(&tt
, 1, sizeof(tt
));
161 /* init structures */
163 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
164 read_timer_cb
, write_timer_cb
, &ts
);
166 /* check initialized fields */
167 g_assert(tt
.clock_type
== QEMU_CLOCK_VIRTUAL
);
168 g_assert(tt
.timers
[0]);
169 g_assert(tt
.timers
[1]);
171 /* check other fields where cleared */
172 g_assert(!ts
.previous_leak
);
173 g_assert(!ts
.cfg
.op_size
);
174 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
175 g_assert(!ts
.cfg
.buckets
[i
].avg
);
176 g_assert(!ts
.cfg
.buckets
[i
].max
);
177 g_assert(!ts
.cfg
.buckets
[i
].level
);
180 throttle_timers_destroy(&tt
);
183 static void test_destroy(void)
187 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
188 read_timer_cb
, write_timer_cb
, &ts
);
189 throttle_timers_destroy(&tt
);
190 for (i
= 0; i
< 2; i
++) {
191 g_assert(!tt
.timers
[i
]);
195 /* function to test throttle_config and throttle_get_config */
196 static void test_config_functions(void)
199 ThrottleConfig orig_cfg
, final_cfg
;
201 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= 153;
202 orig_cfg
.buckets
[THROTTLE_BPS_READ
].avg
= 56;
203 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1;
205 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= 150;
206 orig_cfg
.buckets
[THROTTLE_OPS_READ
].avg
= 69;
207 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= 23;
209 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= 0; /* should be corrected */
210 orig_cfg
.buckets
[THROTTLE_BPS_READ
].max
= 1; /* should not be corrected */
211 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= 120;
213 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= 150;
214 orig_cfg
.buckets
[THROTTLE_OPS_READ
].max
= 400;
215 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= 500;
217 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
= 45;
218 orig_cfg
.buckets
[THROTTLE_BPS_READ
].level
= 65;
219 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].level
= 23;
221 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
= 1;
222 orig_cfg
.buckets
[THROTTLE_OPS_READ
].level
= 90;
223 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].level
= 75;
225 orig_cfg
.op_size
= 1;
228 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
229 read_timer_cb
, write_timer_cb
, &ts
);
230 /* structure reset by throttle_init previous_leak should be null */
231 g_assert(!ts
.previous_leak
);
232 throttle_config(&ts
, &tt
, &orig_cfg
);
234 /* has previous leak been initialized by throttle_config ? */
235 g_assert(ts
.previous_leak
);
237 /* get back the fixed configuration */
238 throttle_get_config(&ts
, &final_cfg
);
240 throttle_timers_destroy(&tt
);
242 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
== 153);
243 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].avg
== 56);
244 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
== 1);
246 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
== 150);
247 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].avg
== 69);
248 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
== 23);
250 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
== 15.3);/* fixed */
251 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].max
== 1); /* not fixed */
252 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
== 120);
254 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
== 150);
255 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].max
== 400);
256 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
== 500);
258 g_assert(final_cfg
.op_size
== 1);
260 /* check bucket have been cleared */
261 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
262 g_assert(!final_cfg
.buckets
[i
].level
);
266 /* functions to test is throttle is enabled by a config */
267 static void set_cfg_value(bool is_max
, int index
, int value
)
270 cfg
.buckets
[index
].max
= value
;
271 /* If max is set, avg should never be 0 */
272 cfg
.buckets
[index
].avg
= MAX(cfg
.buckets
[index
].avg
, 1);
274 cfg
.buckets
[index
].avg
= value
;
278 static void test_enabled(void)
282 throttle_config_init(&cfg
);
283 g_assert(!throttle_enabled(&cfg
));
285 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
286 throttle_config_init(&cfg
);
287 set_cfg_value(false, i
, 150);
288 g_assert(throttle_enabled(&cfg
));
291 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
292 throttle_config_init(&cfg
);
293 set_cfg_value(false, i
, -150);
294 g_assert(!throttle_enabled(&cfg
));
298 /* tests functions for throttle_conflicting */
300 static void test_conflicts_for_one_set(bool is_max
,
305 throttle_config_init(&cfg
);
306 g_assert(throttle_is_valid(&cfg
, NULL
));
308 set_cfg_value(is_max
, total
, 1);
309 set_cfg_value(is_max
, read
, 1);
310 g_assert(!throttle_is_valid(&cfg
, NULL
));
312 throttle_config_init(&cfg
);
313 set_cfg_value(is_max
, total
, 1);
314 set_cfg_value(is_max
, write
, 1);
315 g_assert(!throttle_is_valid(&cfg
, NULL
));
317 throttle_config_init(&cfg
);
318 set_cfg_value(is_max
, total
, 1);
319 set_cfg_value(is_max
, read
, 1);
320 set_cfg_value(is_max
, write
, 1);
321 g_assert(!throttle_is_valid(&cfg
, NULL
));
323 throttle_config_init(&cfg
);
324 set_cfg_value(is_max
, total
, 1);
325 g_assert(throttle_is_valid(&cfg
, NULL
));
327 throttle_config_init(&cfg
);
328 set_cfg_value(is_max
, read
, 1);
329 set_cfg_value(is_max
, write
, 1);
330 g_assert(throttle_is_valid(&cfg
, NULL
));
333 static void test_conflicting_config(void)
335 /* bps average conflicts */
336 test_conflicts_for_one_set(false,
341 /* ops average conflicts */
342 test_conflicts_for_one_set(false,
347 /* bps average conflicts */
348 test_conflicts_for_one_set(true,
352 /* ops average conflicts */
353 test_conflicts_for_one_set(true,
358 /* functions to test the throttle_is_valid function */
359 static void test_is_valid_for_value(int value
, bool should_be_valid
)
362 for (is_max
= 0; is_max
< 2; is_max
++) {
363 for (index
= 0; index
< BUCKETS_COUNT
; index
++) {
364 throttle_config_init(&cfg
);
365 set_cfg_value(is_max
, index
, value
);
366 g_assert(throttle_is_valid(&cfg
, NULL
) == should_be_valid
);
371 static void test_is_valid(void)
373 /* negative number are invalid */
374 test_is_valid_for_value(-1, false);
375 /* zero are valids */
376 test_is_valid_for_value(0, true);
377 /* positives numers are valids */
378 test_is_valid_for_value(1, true);
381 static void test_max_is_missing_limit(void)
385 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
386 throttle_config_init(&cfg
);
387 cfg
.buckets
[i
].max
= 100;
388 cfg
.buckets
[i
].avg
= 0;
389 g_assert(!throttle_is_valid(&cfg
, NULL
));
391 cfg
.buckets
[i
].max
= 0;
392 cfg
.buckets
[i
].avg
= 0;
393 g_assert(throttle_is_valid(&cfg
, NULL
));
395 cfg
.buckets
[i
].max
= 0;
396 cfg
.buckets
[i
].avg
= 100;
397 g_assert(throttle_is_valid(&cfg
, NULL
));
401 static void test_have_timer(void)
403 /* zero structures */
404 memset(&ts
, 0, sizeof(ts
));
405 memset(&tt
, 0, sizeof(tt
));
407 /* no timer set should return false */
408 g_assert(!throttle_timers_are_initialized(&tt
));
410 /* init structures */
412 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
413 read_timer_cb
, write_timer_cb
, &ts
);
415 /* timer set by init should return true */
416 g_assert(throttle_timers_are_initialized(&tt
));
418 throttle_timers_destroy(&tt
);
421 static void test_detach_attach(void)
423 /* zero structures */
424 memset(&ts
, 0, sizeof(ts
));
425 memset(&tt
, 0, sizeof(tt
));
427 /* init the structure */
429 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
430 read_timer_cb
, write_timer_cb
, &ts
);
432 /* timer set by init should return true */
433 g_assert(throttle_timers_are_initialized(&tt
));
435 /* timer should no longer exist after detaching */
436 throttle_timers_detach_aio_context(&tt
);
437 g_assert(!throttle_timers_are_initialized(&tt
));
439 /* timer should exist again after attaching */
440 throttle_timers_attach_aio_context(&tt
, ctx
);
441 g_assert(throttle_timers_are_initialized(&tt
));
443 throttle_timers_destroy(&tt
);
446 static bool do_test_accounting(bool is_ops
, /* are we testing bps or ops */
447 int size
, /* size of the operation to do */
448 double avg
, /* io limit */
449 uint64_t op_size
, /* ideal size of an io */
454 BucketType to_test
[2][3] = { { THROTTLE_BPS_TOTAL
,
456 THROTTLE_BPS_WRITE
, },
457 { THROTTLE_OPS_TOTAL
,
459 THROTTLE_OPS_WRITE
, } };
464 for (i
= 0; i
< 3; i
++) {
465 BucketType index
= to_test
[is_ops
][i
];
466 cfg
.buckets
[index
].avg
= avg
;
469 cfg
.op_size
= op_size
;
472 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
473 read_timer_cb
, write_timer_cb
, &ts
);
474 throttle_config(&ts
, &tt
, &cfg
);
477 throttle_account(&ts
, false, size
);
478 /* account a write */
479 throttle_account(&ts
, true, size
);
481 /* check total result */
482 index
= to_test
[is_ops
][0];
483 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, total_result
)) {
487 /* check read result */
488 index
= to_test
[is_ops
][1];
489 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, read_result
)) {
493 /* check write result */
494 index
= to_test
[is_ops
][2];
495 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, write_result
)) {
499 throttle_timers_destroy(&tt
);
504 static void test_accounting(void)
509 g_assert(do_test_accounting(false,
518 g_assert(do_test_accounting(false,
526 /* op of size 2 and orthogonal parameter change */
527 g_assert(do_test_accounting(false,
539 g_assert(do_test_accounting(true,
548 g_assert(do_test_accounting(true,
556 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
557 g_assert(do_test_accounting(true,
565 /* same with orthogonal parameters changes */
566 g_assert(do_test_accounting(true,
575 static void test_groups(void)
577 ThrottleConfig cfg1
, cfg2
;
578 BlockBackend
*blk1
, *blk2
, *blk3
;
579 BlockBackendPublic
*blkp1
, *blkp2
, *blkp3
;
581 blk1
= blk_new_with_bs(&error_abort
);
582 blk2
= blk_new_with_bs(&error_abort
);
583 blk3
= blk_new_with_bs(&error_abort
);
585 blkp1
= blk_get_public(blk1
);
586 blkp2
= blk_get_public(blk2
);
587 blkp3
= blk_get_public(blk3
);
589 g_assert(blkp1
->throttle_state
== NULL
);
590 g_assert(blkp2
->throttle_state
== NULL
);
591 g_assert(blkp3
->throttle_state
== NULL
);
593 throttle_group_register_blk(blk1
, "bar");
594 throttle_group_register_blk(blk2
, "foo");
595 throttle_group_register_blk(blk3
, "bar");
597 g_assert(blkp1
->throttle_state
!= NULL
);
598 g_assert(blkp2
->throttle_state
!= NULL
);
599 g_assert(blkp3
->throttle_state
!= NULL
);
601 g_assert(!strcmp(throttle_group_get_name(blk1
), "bar"));
602 g_assert(!strcmp(throttle_group_get_name(blk2
), "foo"));
603 g_assert(blkp1
->throttle_state
== blkp3
->throttle_state
);
605 /* Setting the config of a group member affects the whole group */
606 throttle_config_init(&cfg1
);
607 cfg1
.buckets
[THROTTLE_BPS_READ
].avg
= 500000;
608 cfg1
.buckets
[THROTTLE_BPS_WRITE
].avg
= 285000;
609 cfg1
.buckets
[THROTTLE_OPS_READ
].avg
= 20000;
610 cfg1
.buckets
[THROTTLE_OPS_WRITE
].avg
= 12000;
611 throttle_group_config(blk1
, &cfg1
);
613 throttle_group_get_config(blk1
, &cfg1
);
614 throttle_group_get_config(blk3
, &cfg2
);
615 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
617 cfg2
.buckets
[THROTTLE_BPS_READ
].avg
= 4547;
618 cfg2
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1349;
619 cfg2
.buckets
[THROTTLE_OPS_READ
].avg
= 123;
620 cfg2
.buckets
[THROTTLE_OPS_WRITE
].avg
= 86;
621 throttle_group_config(blk3
, &cfg1
);
623 throttle_group_get_config(blk1
, &cfg1
);
624 throttle_group_get_config(blk3
, &cfg2
);
625 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
627 throttle_group_unregister_blk(blk1
);
628 throttle_group_unregister_blk(blk2
);
629 throttle_group_unregister_blk(blk3
);
631 g_assert(blkp1
->throttle_state
== NULL
);
632 g_assert(blkp2
->throttle_state
== NULL
);
633 g_assert(blkp3
->throttle_state
== NULL
);
636 int main(int argc
, char **argv
)
638 qemu_init_main_loop(&error_fatal
);
639 ctx
= qemu_get_aio_context();
642 do {} while (g_main_context_iteration(NULL
, false));
644 /* tests in the same order as the header function declarations */
645 g_test_init(&argc
, &argv
, NULL
);
646 g_test_add_func("/throttle/leak_bucket", test_leak_bucket
);
647 g_test_add_func("/throttle/compute_wait", test_compute_wait
);
648 g_test_add_func("/throttle/init", test_init
);
649 g_test_add_func("/throttle/destroy", test_destroy
);
650 g_test_add_func("/throttle/have_timer", test_have_timer
);
651 g_test_add_func("/throttle/detach_attach", test_detach_attach
);
652 g_test_add_func("/throttle/config/enabled", test_enabled
);
653 g_test_add_func("/throttle/config/conflicting", test_conflicting_config
);
654 g_test_add_func("/throttle/config/is_valid", test_is_valid
);
655 g_test_add_func("/throttle/config/max", test_max_is_missing_limit
);
656 g_test_add_func("/throttle/config_functions", test_config_functions
);
657 g_test_add_func("/throttle/accounting", test_accounting
);
658 g_test_add_func("/throttle/groups", test_groups
);