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 throttle_config_init(&cfg
);
39 bkt
= cfg
.buckets
[THROTTLE_BPS_TOTAL
];
41 /* set initial value */
46 /* leak an op work of time */
47 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
48 g_assert(bkt
.avg
== 150);
49 g_assert(bkt
.max
== 15);
50 g_assert(double_cmp(bkt
.level
, 0.5));
52 /* leak again emptying the bucket */
53 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
54 g_assert(bkt
.avg
== 150);
55 g_assert(bkt
.max
== 15);
56 g_assert(double_cmp(bkt
.level
, 0));
58 /* check that the bucket level won't go lower */
59 throttle_leak_bucket(&bkt
, NANOSECONDS_PER_SECOND
/ 150);
60 g_assert(bkt
.avg
== 150);
61 g_assert(bkt
.max
== 15);
62 g_assert(double_cmp(bkt
.level
, 0));
65 static void test_compute_wait(void)
70 throttle_config_init(&cfg
);
71 bkt
= cfg
.buckets
[THROTTLE_BPS_TOTAL
];
73 /* no operation limit set */
77 wait
= throttle_compute_wait(&bkt
);
84 wait
= throttle_compute_wait(&bkt
);
87 /* below zero delta */
91 wait
= throttle_compute_wait(&bkt
);
94 /* half an operation above max */
98 wait
= throttle_compute_wait(&bkt
);
99 /* time required to do half an operation */
100 result
= (int64_t) NANOSECONDS_PER_SECOND
/ 150 / 2;
101 g_assert(wait
== result
);
104 /* functions to test ThrottleState initialization/destroy methods */
105 static void read_timer_cb(void *opaque
)
109 static void write_timer_cb(void *opaque
)
113 static void test_init(void)
117 /* fill the structures with crap */
118 memset(&ts
, 1, sizeof(ts
));
119 memset(&tt
, 1, sizeof(tt
));
121 /* init structures */
123 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
124 read_timer_cb
, write_timer_cb
, &ts
);
126 /* check initialized fields */
127 g_assert(tt
.clock_type
== QEMU_CLOCK_VIRTUAL
);
128 g_assert(tt
.timers
[0]);
129 g_assert(tt
.timers
[1]);
131 /* check other fields where cleared */
132 g_assert(!ts
.previous_leak
);
133 g_assert(!ts
.cfg
.op_size
);
134 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
135 g_assert(!ts
.cfg
.buckets
[i
].avg
);
136 g_assert(!ts
.cfg
.buckets
[i
].max
);
137 g_assert(!ts
.cfg
.buckets
[i
].level
);
140 throttle_timers_destroy(&tt
);
143 static void test_destroy(void)
147 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
148 read_timer_cb
, write_timer_cb
, &ts
);
149 throttle_timers_destroy(&tt
);
150 for (i
= 0; i
< 2; i
++) {
151 g_assert(!tt
.timers
[i
]);
155 /* function to test throttle_config and throttle_get_config */
156 static void test_config_functions(void)
159 ThrottleConfig orig_cfg
, final_cfg
;
161 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= 153;
162 orig_cfg
.buckets
[THROTTLE_BPS_READ
].avg
= 56;
163 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1;
165 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= 150;
166 orig_cfg
.buckets
[THROTTLE_OPS_READ
].avg
= 69;
167 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= 23;
169 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= 0; /* should be corrected */
170 orig_cfg
.buckets
[THROTTLE_BPS_READ
].max
= 1; /* should not be corrected */
171 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= 120;
173 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= 150;
174 orig_cfg
.buckets
[THROTTLE_OPS_READ
].max
= 400;
175 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= 500;
177 orig_cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
= 45;
178 orig_cfg
.buckets
[THROTTLE_BPS_READ
].level
= 65;
179 orig_cfg
.buckets
[THROTTLE_BPS_WRITE
].level
= 23;
181 orig_cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
= 1;
182 orig_cfg
.buckets
[THROTTLE_OPS_READ
].level
= 90;
183 orig_cfg
.buckets
[THROTTLE_OPS_WRITE
].level
= 75;
185 orig_cfg
.op_size
= 1;
188 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
189 read_timer_cb
, write_timer_cb
, &ts
);
190 /* structure reset by throttle_init previous_leak should be null */
191 g_assert(!ts
.previous_leak
);
192 throttle_config(&ts
, &tt
, &orig_cfg
);
194 /* has previous leak been initialized by throttle_config ? */
195 g_assert(ts
.previous_leak
);
197 /* get back the fixed configuration */
198 throttle_get_config(&ts
, &final_cfg
);
200 throttle_timers_destroy(&tt
);
202 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
== 153);
203 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].avg
== 56);
204 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
== 1);
206 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
== 150);
207 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].avg
== 69);
208 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
== 23);
210 g_assert(final_cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
== 15.3);/* fixed */
211 g_assert(final_cfg
.buckets
[THROTTLE_BPS_READ
].max
== 1); /* not fixed */
212 g_assert(final_cfg
.buckets
[THROTTLE_BPS_WRITE
].max
== 120);
214 g_assert(final_cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
== 150);
215 g_assert(final_cfg
.buckets
[THROTTLE_OPS_READ
].max
== 400);
216 g_assert(final_cfg
.buckets
[THROTTLE_OPS_WRITE
].max
== 500);
218 g_assert(final_cfg
.op_size
== 1);
220 /* check bucket have been cleared */
221 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
222 g_assert(!final_cfg
.buckets
[i
].level
);
226 /* functions to test is throttle is enabled by a config */
227 static void set_cfg_value(bool is_max
, int index
, int value
)
230 cfg
.buckets
[index
].max
= value
;
231 /* If max is set, avg should never be 0 */
232 cfg
.buckets
[index
].avg
= MAX(cfg
.buckets
[index
].avg
, 1);
234 cfg
.buckets
[index
].avg
= value
;
238 static void test_enabled(void)
242 throttle_config_init(&cfg
);
243 g_assert(!throttle_enabled(&cfg
));
245 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
246 throttle_config_init(&cfg
);
247 set_cfg_value(false, i
, 150);
248 g_assert(throttle_enabled(&cfg
));
251 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
252 throttle_config_init(&cfg
);
253 set_cfg_value(false, i
, -150);
254 g_assert(!throttle_enabled(&cfg
));
258 /* tests functions for throttle_conflicting */
260 static void test_conflicts_for_one_set(bool is_max
,
265 throttle_config_init(&cfg
);
266 g_assert(throttle_is_valid(&cfg
, NULL
));
268 set_cfg_value(is_max
, total
, 1);
269 set_cfg_value(is_max
, read
, 1);
270 g_assert(!throttle_is_valid(&cfg
, NULL
));
272 throttle_config_init(&cfg
);
273 set_cfg_value(is_max
, total
, 1);
274 set_cfg_value(is_max
, write
, 1);
275 g_assert(!throttle_is_valid(&cfg
, NULL
));
277 throttle_config_init(&cfg
);
278 set_cfg_value(is_max
, total
, 1);
279 set_cfg_value(is_max
, read
, 1);
280 set_cfg_value(is_max
, write
, 1);
281 g_assert(!throttle_is_valid(&cfg
, NULL
));
283 throttle_config_init(&cfg
);
284 set_cfg_value(is_max
, total
, 1);
285 g_assert(throttle_is_valid(&cfg
, NULL
));
287 throttle_config_init(&cfg
);
288 set_cfg_value(is_max
, read
, 1);
289 set_cfg_value(is_max
, write
, 1);
290 g_assert(throttle_is_valid(&cfg
, NULL
));
293 static void test_conflicting_config(void)
295 /* bps average conflicts */
296 test_conflicts_for_one_set(false,
301 /* ops average conflicts */
302 test_conflicts_for_one_set(false,
307 /* bps average conflicts */
308 test_conflicts_for_one_set(true,
312 /* ops average conflicts */
313 test_conflicts_for_one_set(true,
318 /* functions to test the throttle_is_valid function */
319 static void test_is_valid_for_value(int value
, bool should_be_valid
)
322 for (is_max
= 0; is_max
< 2; is_max
++) {
323 for (index
= 0; index
< BUCKETS_COUNT
; index
++) {
324 throttle_config_init(&cfg
);
325 set_cfg_value(is_max
, index
, value
);
326 g_assert(throttle_is_valid(&cfg
, NULL
) == should_be_valid
);
331 static void test_is_valid(void)
333 /* negative number are invalid */
334 test_is_valid_for_value(-1, false);
335 /* zero are valids */
336 test_is_valid_for_value(0, true);
337 /* positives numers are valids */
338 test_is_valid_for_value(1, true);
341 static void test_max_is_missing_limit(void)
345 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
346 throttle_config_init(&cfg
);
347 cfg
.buckets
[i
].max
= 100;
348 cfg
.buckets
[i
].avg
= 0;
349 g_assert(!throttle_is_valid(&cfg
, NULL
));
351 cfg
.buckets
[i
].max
= 0;
352 cfg
.buckets
[i
].avg
= 0;
353 g_assert(throttle_is_valid(&cfg
, NULL
));
355 cfg
.buckets
[i
].max
= 0;
356 cfg
.buckets
[i
].avg
= 100;
357 g_assert(throttle_is_valid(&cfg
, NULL
));
361 static void test_have_timer(void)
363 /* zero structures */
364 memset(&ts
, 0, sizeof(ts
));
365 memset(&tt
, 0, sizeof(tt
));
367 /* no timer set should return false */
368 g_assert(!throttle_timers_are_initialized(&tt
));
370 /* init structures */
372 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
373 read_timer_cb
, write_timer_cb
, &ts
);
375 /* timer set by init should return true */
376 g_assert(throttle_timers_are_initialized(&tt
));
378 throttle_timers_destroy(&tt
);
381 static void test_detach_attach(void)
383 /* zero structures */
384 memset(&ts
, 0, sizeof(ts
));
385 memset(&tt
, 0, sizeof(tt
));
387 /* init the structure */
389 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
390 read_timer_cb
, write_timer_cb
, &ts
);
392 /* timer set by init should return true */
393 g_assert(throttle_timers_are_initialized(&tt
));
395 /* timer should no longer exist after detaching */
396 throttle_timers_detach_aio_context(&tt
);
397 g_assert(!throttle_timers_are_initialized(&tt
));
399 /* timer should exist again after attaching */
400 throttle_timers_attach_aio_context(&tt
, ctx
);
401 g_assert(throttle_timers_are_initialized(&tt
));
403 throttle_timers_destroy(&tt
);
406 static bool do_test_accounting(bool is_ops
, /* are we testing bps or ops */
407 int size
, /* size of the operation to do */
408 double avg
, /* io limit */
409 uint64_t op_size
, /* ideal size of an io */
414 BucketType to_test
[2][3] = { { THROTTLE_BPS_TOTAL
,
416 THROTTLE_BPS_WRITE
, },
417 { THROTTLE_OPS_TOTAL
,
419 THROTTLE_OPS_WRITE
, } };
424 for (i
= 0; i
< 3; i
++) {
425 BucketType index
= to_test
[is_ops
][i
];
426 cfg
.buckets
[index
].avg
= avg
;
429 cfg
.op_size
= op_size
;
432 throttle_timers_init(&tt
, ctx
, QEMU_CLOCK_VIRTUAL
,
433 read_timer_cb
, write_timer_cb
, &ts
);
434 throttle_config(&ts
, &tt
, &cfg
);
437 throttle_account(&ts
, false, size
);
438 /* account a write */
439 throttle_account(&ts
, true, size
);
441 /* check total result */
442 index
= to_test
[is_ops
][0];
443 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, total_result
)) {
447 /* check read result */
448 index
= to_test
[is_ops
][1];
449 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, read_result
)) {
453 /* check write result */
454 index
= to_test
[is_ops
][2];
455 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, write_result
)) {
459 throttle_timers_destroy(&tt
);
464 static void test_accounting(void)
469 g_assert(do_test_accounting(false,
478 g_assert(do_test_accounting(false,
486 /* op of size 2 and orthogonal parameter change */
487 g_assert(do_test_accounting(false,
499 g_assert(do_test_accounting(true,
508 g_assert(do_test_accounting(true,
516 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
517 g_assert(do_test_accounting(true,
525 /* same with orthogonal parameters changes */
526 g_assert(do_test_accounting(true,
535 static void test_groups(void)
537 ThrottleConfig cfg1
, cfg2
;
538 BlockDriverState
*bdrv1
, *bdrv2
, *bdrv3
;
544 g_assert(bdrv1
->throttle_state
== NULL
);
545 g_assert(bdrv2
->throttle_state
== NULL
);
546 g_assert(bdrv3
->throttle_state
== NULL
);
548 throttle_group_register_bs(bdrv1
, "bar");
549 throttle_group_register_bs(bdrv2
, "foo");
550 throttle_group_register_bs(bdrv3
, "bar");
552 g_assert(bdrv1
->throttle_state
!= NULL
);
553 g_assert(bdrv2
->throttle_state
!= NULL
);
554 g_assert(bdrv3
->throttle_state
!= NULL
);
556 g_assert(!strcmp(throttle_group_get_name(bdrv1
), "bar"));
557 g_assert(!strcmp(throttle_group_get_name(bdrv2
), "foo"));
558 g_assert(bdrv1
->throttle_state
== bdrv3
->throttle_state
);
560 /* Setting the config of a group member affects the whole group */
561 throttle_config_init(&cfg1
);
562 cfg1
.buckets
[THROTTLE_BPS_READ
].avg
= 500000;
563 cfg1
.buckets
[THROTTLE_BPS_WRITE
].avg
= 285000;
564 cfg1
.buckets
[THROTTLE_OPS_READ
].avg
= 20000;
565 cfg1
.buckets
[THROTTLE_OPS_WRITE
].avg
= 12000;
566 throttle_group_config(bdrv1
, &cfg1
);
568 throttle_group_get_config(bdrv1
, &cfg1
);
569 throttle_group_get_config(bdrv3
, &cfg2
);
570 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
572 cfg2
.buckets
[THROTTLE_BPS_READ
].avg
= 4547;
573 cfg2
.buckets
[THROTTLE_BPS_WRITE
].avg
= 1349;
574 cfg2
.buckets
[THROTTLE_OPS_READ
].avg
= 123;
575 cfg2
.buckets
[THROTTLE_OPS_WRITE
].avg
= 86;
576 throttle_group_config(bdrv3
, &cfg1
);
578 throttle_group_get_config(bdrv1
, &cfg1
);
579 throttle_group_get_config(bdrv3
, &cfg2
);
580 g_assert(!memcmp(&cfg1
, &cfg2
, sizeof(cfg1
)));
582 throttle_group_unregister_bs(bdrv1
);
583 throttle_group_unregister_bs(bdrv2
);
584 throttle_group_unregister_bs(bdrv3
);
586 g_assert(bdrv1
->throttle_state
== NULL
);
587 g_assert(bdrv2
->throttle_state
== NULL
);
588 g_assert(bdrv3
->throttle_state
== NULL
);
591 int main(int argc
, char **argv
)
593 qemu_init_main_loop(&error_fatal
);
594 ctx
= qemu_get_aio_context();
597 do {} while (g_main_context_iteration(NULL
, false));
599 /* tests in the same order as the header function declarations */
600 g_test_init(&argc
, &argv
, NULL
);
601 g_test_add_func("/throttle/leak_bucket", test_leak_bucket
);
602 g_test_add_func("/throttle/compute_wait", test_compute_wait
);
603 g_test_add_func("/throttle/init", test_init
);
604 g_test_add_func("/throttle/destroy", test_destroy
);
605 g_test_add_func("/throttle/have_timer", test_have_timer
);
606 g_test_add_func("/throttle/detach_attach", test_detach_attach
);
607 g_test_add_func("/throttle/config/enabled", test_enabled
);
608 g_test_add_func("/throttle/config/conflicting", test_conflicting_config
);
609 g_test_add_func("/throttle/config/is_valid", test_is_valid
);
610 g_test_add_func("/throttle/config/max", test_max_is_missing_limit
);
611 g_test_add_func("/throttle/config_functions", test_config_functions
);
612 g_test_add_func("/throttle/accounting", test_accounting
);
613 g_test_add_func("/throttle/groups", test_groups
);