vfio/pci: Split out VGA setup
[qemu/ar7.git] / tests / test-throttle.c
blob59675fa57bf9eef2e390dfb1b220bcc803653279
1 /*
2 * Throttle infrastructure tests
4 * Copyright Nodalink, EURL. 2013-2014
5 * Copyright Igalia, S.L. 2015
7 * Authors:
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"
16 #include <glib.h>
17 #include <math.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;
29 /* useful function */
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 */
42 bkt.avg = 150;
43 bkt.max = 15;
44 bkt.level = 1.5;
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));
64 /* check that burst_level leaks correctly */
65 bkt.burst_level = 6;
66 bkt.max = 250;
67 bkt.burst_length = 2; /* otherwise burst_level will not leak */
68 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
69 g_assert(double_cmp(bkt.burst_level, 3.5));
71 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
72 g_assert(double_cmp(bkt.burst_level, 1));
74 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
75 g_assert(double_cmp(bkt.burst_level, 0));
77 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
78 g_assert(double_cmp(bkt.burst_level, 0));
81 static void test_compute_wait(void)
83 unsigned i;
84 int64_t wait;
85 int64_t result;
87 throttle_config_init(&cfg);
88 bkt = cfg.buckets[THROTTLE_BPS_TOTAL];
90 /* no operation limit set */
91 bkt.avg = 0;
92 bkt.max = 15;
93 bkt.level = 1.5;
94 wait = throttle_compute_wait(&bkt);
95 g_assert(!wait);
97 /* zero delta */
98 bkt.avg = 150;
99 bkt.max = 15;
100 bkt.level = 15;
101 wait = throttle_compute_wait(&bkt);
102 g_assert(!wait);
104 /* below zero delta */
105 bkt.avg = 150;
106 bkt.max = 15;
107 bkt.level = 9;
108 wait = throttle_compute_wait(&bkt);
109 g_assert(!wait);
111 /* half an operation above max */
112 bkt.avg = 150;
113 bkt.max = 15;
114 bkt.level = 15.5;
115 wait = throttle_compute_wait(&bkt);
116 /* time required to do half an operation */
117 result = (int64_t) NANOSECONDS_PER_SECOND / 150 / 2;
118 g_assert(wait == result);
120 /* Perform I/O for 2.2 seconds at a rate of bkt.max */
121 bkt.burst_length = 2;
122 bkt.level = 0;
123 bkt.avg = 10;
124 bkt.max = 200;
125 for (i = 0; i < 22; i++) {
126 double units = bkt.max / 10;
127 bkt.level += units;
128 bkt.burst_level += units;
129 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 10);
130 wait = throttle_compute_wait(&bkt);
131 g_assert(double_cmp(bkt.burst_level, 0));
132 g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10));
133 /* We can do bursts for the 2 seconds we have configured in
134 * burst_length. We have 100 extra miliseconds of burst
135 * because bkt.level has been leaking during this time.
136 * After that, we have to wait. */
137 result = i < 21 ? 0 : 1.8 * NANOSECONDS_PER_SECOND;
138 g_assert(wait == result);
142 /* functions to test ThrottleState initialization/destroy methods */
143 static void read_timer_cb(void *opaque)
147 static void write_timer_cb(void *opaque)
151 static void test_init(void)
153 int i;
155 /* fill the structures with crap */
156 memset(&ts, 1, sizeof(ts));
157 memset(&tt, 1, sizeof(tt));
159 /* init structures */
160 throttle_init(&ts);
161 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
162 read_timer_cb, write_timer_cb, &ts);
164 /* check initialized fields */
165 g_assert(tt.clock_type == QEMU_CLOCK_VIRTUAL);
166 g_assert(tt.timers[0]);
167 g_assert(tt.timers[1]);
169 /* check other fields where cleared */
170 g_assert(!ts.previous_leak);
171 g_assert(!ts.cfg.op_size);
172 for (i = 0; i < BUCKETS_COUNT; i++) {
173 g_assert(!ts.cfg.buckets[i].avg);
174 g_assert(!ts.cfg.buckets[i].max);
175 g_assert(!ts.cfg.buckets[i].level);
178 throttle_timers_destroy(&tt);
181 static void test_destroy(void)
183 int i;
184 throttle_init(&ts);
185 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
186 read_timer_cb, write_timer_cb, &ts);
187 throttle_timers_destroy(&tt);
188 for (i = 0; i < 2; i++) {
189 g_assert(!tt.timers[i]);
193 /* function to test throttle_config and throttle_get_config */
194 static void test_config_functions(void)
196 int i;
197 ThrottleConfig orig_cfg, final_cfg;
199 orig_cfg.buckets[THROTTLE_BPS_TOTAL].avg = 153;
200 orig_cfg.buckets[THROTTLE_BPS_READ].avg = 56;
201 orig_cfg.buckets[THROTTLE_BPS_WRITE].avg = 1;
203 orig_cfg.buckets[THROTTLE_OPS_TOTAL].avg = 150;
204 orig_cfg.buckets[THROTTLE_OPS_READ].avg = 69;
205 orig_cfg.buckets[THROTTLE_OPS_WRITE].avg = 23;
207 orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0; /* should be corrected */
208 orig_cfg.buckets[THROTTLE_BPS_READ].max = 1; /* should not be corrected */
209 orig_cfg.buckets[THROTTLE_BPS_WRITE].max = 120;
211 orig_cfg.buckets[THROTTLE_OPS_TOTAL].max = 150;
212 orig_cfg.buckets[THROTTLE_OPS_READ].max = 400;
213 orig_cfg.buckets[THROTTLE_OPS_WRITE].max = 500;
215 orig_cfg.buckets[THROTTLE_BPS_TOTAL].level = 45;
216 orig_cfg.buckets[THROTTLE_BPS_READ].level = 65;
217 orig_cfg.buckets[THROTTLE_BPS_WRITE].level = 23;
219 orig_cfg.buckets[THROTTLE_OPS_TOTAL].level = 1;
220 orig_cfg.buckets[THROTTLE_OPS_READ].level = 90;
221 orig_cfg.buckets[THROTTLE_OPS_WRITE].level = 75;
223 orig_cfg.op_size = 1;
225 throttle_init(&ts);
226 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
227 read_timer_cb, write_timer_cb, &ts);
228 /* structure reset by throttle_init previous_leak should be null */
229 g_assert(!ts.previous_leak);
230 throttle_config(&ts, &tt, &orig_cfg);
232 /* has previous leak been initialized by throttle_config ? */
233 g_assert(ts.previous_leak);
235 /* get back the fixed configuration */
236 throttle_get_config(&ts, &final_cfg);
238 throttle_timers_destroy(&tt);
240 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153);
241 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg == 56);
242 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].avg == 1);
244 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].avg == 150);
245 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].avg == 69);
246 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].avg == 23);
248 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 15.3);/* fixed */
249 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 1); /* not fixed */
250 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].max == 120);
252 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].max == 150);
253 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].max == 400);
254 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].max == 500);
256 g_assert(final_cfg.op_size == 1);
258 /* check bucket have been cleared */
259 for (i = 0; i < BUCKETS_COUNT; i++) {
260 g_assert(!final_cfg.buckets[i].level);
264 /* functions to test is throttle is enabled by a config */
265 static void set_cfg_value(bool is_max, int index, int value)
267 if (is_max) {
268 cfg.buckets[index].max = value;
269 /* If max is set, avg should never be 0 */
270 cfg.buckets[index].avg = MAX(cfg.buckets[index].avg, 1);
271 } else {
272 cfg.buckets[index].avg = value;
276 static void test_enabled(void)
278 int i;
280 throttle_config_init(&cfg);
281 g_assert(!throttle_enabled(&cfg));
283 for (i = 0; i < BUCKETS_COUNT; i++) {
284 throttle_config_init(&cfg);
285 set_cfg_value(false, i, 150);
286 g_assert(throttle_enabled(&cfg));
289 for (i = 0; i < BUCKETS_COUNT; i++) {
290 throttle_config_init(&cfg);
291 set_cfg_value(false, i, -150);
292 g_assert(!throttle_enabled(&cfg));
296 /* tests functions for throttle_conflicting */
298 static void test_conflicts_for_one_set(bool is_max,
299 int total,
300 int read,
301 int write)
303 throttle_config_init(&cfg);
304 g_assert(throttle_is_valid(&cfg, NULL));
306 set_cfg_value(is_max, total, 1);
307 set_cfg_value(is_max, read, 1);
308 g_assert(!throttle_is_valid(&cfg, NULL));
310 throttle_config_init(&cfg);
311 set_cfg_value(is_max, total, 1);
312 set_cfg_value(is_max, write, 1);
313 g_assert(!throttle_is_valid(&cfg, NULL));
315 throttle_config_init(&cfg);
316 set_cfg_value(is_max, total, 1);
317 set_cfg_value(is_max, read, 1);
318 set_cfg_value(is_max, write, 1);
319 g_assert(!throttle_is_valid(&cfg, NULL));
321 throttle_config_init(&cfg);
322 set_cfg_value(is_max, total, 1);
323 g_assert(throttle_is_valid(&cfg, NULL));
325 throttle_config_init(&cfg);
326 set_cfg_value(is_max, read, 1);
327 set_cfg_value(is_max, write, 1);
328 g_assert(throttle_is_valid(&cfg, NULL));
331 static void test_conflicting_config(void)
333 /* bps average conflicts */
334 test_conflicts_for_one_set(false,
335 THROTTLE_BPS_TOTAL,
336 THROTTLE_BPS_READ,
337 THROTTLE_BPS_WRITE);
339 /* ops average conflicts */
340 test_conflicts_for_one_set(false,
341 THROTTLE_OPS_TOTAL,
342 THROTTLE_OPS_READ,
343 THROTTLE_OPS_WRITE);
345 /* bps average conflicts */
346 test_conflicts_for_one_set(true,
347 THROTTLE_BPS_TOTAL,
348 THROTTLE_BPS_READ,
349 THROTTLE_BPS_WRITE);
350 /* ops average conflicts */
351 test_conflicts_for_one_set(true,
352 THROTTLE_OPS_TOTAL,
353 THROTTLE_OPS_READ,
354 THROTTLE_OPS_WRITE);
356 /* functions to test the throttle_is_valid function */
357 static void test_is_valid_for_value(int value, bool should_be_valid)
359 int is_max, index;
360 for (is_max = 0; is_max < 2; is_max++) {
361 for (index = 0; index < BUCKETS_COUNT; index++) {
362 throttle_config_init(&cfg);
363 set_cfg_value(is_max, index, value);
364 g_assert(throttle_is_valid(&cfg, NULL) == should_be_valid);
369 static void test_is_valid(void)
371 /* negative number are invalid */
372 test_is_valid_for_value(-1, false);
373 /* zero are valids */
374 test_is_valid_for_value(0, true);
375 /* positives numers are valids */
376 test_is_valid_for_value(1, true);
379 static void test_max_is_missing_limit(void)
381 int i;
383 for (i = 0; i < BUCKETS_COUNT; i++) {
384 throttle_config_init(&cfg);
385 cfg.buckets[i].max = 100;
386 cfg.buckets[i].avg = 0;
387 g_assert(!throttle_is_valid(&cfg, NULL));
389 cfg.buckets[i].max = 0;
390 cfg.buckets[i].avg = 0;
391 g_assert(throttle_is_valid(&cfg, NULL));
393 cfg.buckets[i].max = 0;
394 cfg.buckets[i].avg = 100;
395 g_assert(throttle_is_valid(&cfg, NULL));
399 static void test_have_timer(void)
401 /* zero structures */
402 memset(&ts, 0, sizeof(ts));
403 memset(&tt, 0, sizeof(tt));
405 /* no timer set should return false */
406 g_assert(!throttle_timers_are_initialized(&tt));
408 /* init structures */
409 throttle_init(&ts);
410 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
411 read_timer_cb, write_timer_cb, &ts);
413 /* timer set by init should return true */
414 g_assert(throttle_timers_are_initialized(&tt));
416 throttle_timers_destroy(&tt);
419 static void test_detach_attach(void)
421 /* zero structures */
422 memset(&ts, 0, sizeof(ts));
423 memset(&tt, 0, sizeof(tt));
425 /* init the structure */
426 throttle_init(&ts);
427 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
428 read_timer_cb, write_timer_cb, &ts);
430 /* timer set by init should return true */
431 g_assert(throttle_timers_are_initialized(&tt));
433 /* timer should no longer exist after detaching */
434 throttle_timers_detach_aio_context(&tt);
435 g_assert(!throttle_timers_are_initialized(&tt));
437 /* timer should exist again after attaching */
438 throttle_timers_attach_aio_context(&tt, ctx);
439 g_assert(throttle_timers_are_initialized(&tt));
441 throttle_timers_destroy(&tt);
444 static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
445 int size, /* size of the operation to do */
446 double avg, /* io limit */
447 uint64_t op_size, /* ideal size of an io */
448 double total_result,
449 double read_result,
450 double write_result)
452 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
453 THROTTLE_BPS_READ,
454 THROTTLE_BPS_WRITE, },
455 { THROTTLE_OPS_TOTAL,
456 THROTTLE_OPS_READ,
457 THROTTLE_OPS_WRITE, } };
458 ThrottleConfig cfg;
459 BucketType index;
460 int i;
462 for (i = 0; i < 3; i++) {
463 BucketType index = to_test[is_ops][i];
464 cfg.buckets[index].avg = avg;
467 cfg.op_size = op_size;
469 throttle_init(&ts);
470 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
471 read_timer_cb, write_timer_cb, &ts);
472 throttle_config(&ts, &tt, &cfg);
474 /* account a read */
475 throttle_account(&ts, false, size);
476 /* account a write */
477 throttle_account(&ts, true, size);
479 /* check total result */
480 index = to_test[is_ops][0];
481 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
482 return false;
485 /* check read result */
486 index = to_test[is_ops][1];
487 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
488 return false;
491 /* check write result */
492 index = to_test[is_ops][2];
493 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
494 return false;
497 throttle_timers_destroy(&tt);
499 return true;
502 static void test_accounting(void)
504 /* tests for bps */
506 /* op of size 1 */
507 g_assert(do_test_accounting(false,
508 1 * 512,
509 150,
511 1024,
512 512,
513 512));
515 /* op of size 2 */
516 g_assert(do_test_accounting(false,
517 2 * 512,
518 150,
520 2048,
521 1024,
522 1024));
524 /* op of size 2 and orthogonal parameter change */
525 g_assert(do_test_accounting(false,
526 2 * 512,
527 150,
529 2048,
530 1024,
531 1024));
534 /* tests for ops */
536 /* op of size 1 */
537 g_assert(do_test_accounting(true,
538 1 * 512,
539 150,
543 1));
545 /* op of size 2 */
546 g_assert(do_test_accounting(true,
547 2 * 512,
548 150,
552 1));
554 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
555 g_assert(do_test_accounting(true,
556 64 * 512,
557 150,
558 13 * 512,
559 (64.0 * 2) / 13,
560 (64.0 / 13),
561 (64.0 / 13)));
563 /* same with orthogonal parameters changes */
564 g_assert(do_test_accounting(true,
565 64 * 512,
566 300,
567 13 * 512,
568 (64.0 * 2) / 13,
569 (64.0 / 13),
570 (64.0 / 13)));
573 static void test_groups(void)
575 ThrottleConfig cfg1, cfg2;
576 BlockDriverState *bdrv1, *bdrv2, *bdrv3;
578 bdrv1 = bdrv_new();
579 bdrv2 = bdrv_new();
580 bdrv3 = bdrv_new();
582 g_assert(bdrv1->throttle_state == NULL);
583 g_assert(bdrv2->throttle_state == NULL);
584 g_assert(bdrv3->throttle_state == NULL);
586 throttle_group_register_bs(bdrv1, "bar");
587 throttle_group_register_bs(bdrv2, "foo");
588 throttle_group_register_bs(bdrv3, "bar");
590 g_assert(bdrv1->throttle_state != NULL);
591 g_assert(bdrv2->throttle_state != NULL);
592 g_assert(bdrv3->throttle_state != NULL);
594 g_assert(!strcmp(throttle_group_get_name(bdrv1), "bar"));
595 g_assert(!strcmp(throttle_group_get_name(bdrv2), "foo"));
596 g_assert(bdrv1->throttle_state == bdrv3->throttle_state);
598 /* Setting the config of a group member affects the whole group */
599 throttle_config_init(&cfg1);
600 cfg1.buckets[THROTTLE_BPS_READ].avg = 500000;
601 cfg1.buckets[THROTTLE_BPS_WRITE].avg = 285000;
602 cfg1.buckets[THROTTLE_OPS_READ].avg = 20000;
603 cfg1.buckets[THROTTLE_OPS_WRITE].avg = 12000;
604 throttle_group_config(bdrv1, &cfg1);
606 throttle_group_get_config(bdrv1, &cfg1);
607 throttle_group_get_config(bdrv3, &cfg2);
608 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
610 cfg2.buckets[THROTTLE_BPS_READ].avg = 4547;
611 cfg2.buckets[THROTTLE_BPS_WRITE].avg = 1349;
612 cfg2.buckets[THROTTLE_OPS_READ].avg = 123;
613 cfg2.buckets[THROTTLE_OPS_WRITE].avg = 86;
614 throttle_group_config(bdrv3, &cfg1);
616 throttle_group_get_config(bdrv1, &cfg1);
617 throttle_group_get_config(bdrv3, &cfg2);
618 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
620 throttle_group_unregister_bs(bdrv1);
621 throttle_group_unregister_bs(bdrv2);
622 throttle_group_unregister_bs(bdrv3);
624 g_assert(bdrv1->throttle_state == NULL);
625 g_assert(bdrv2->throttle_state == NULL);
626 g_assert(bdrv3->throttle_state == NULL);
629 int main(int argc, char **argv)
631 qemu_init_main_loop(&error_fatal);
632 ctx = qemu_get_aio_context();
633 bdrv_init();
635 do {} while (g_main_context_iteration(NULL, false));
637 /* tests in the same order as the header function declarations */
638 g_test_init(&argc, &argv, NULL);
639 g_test_add_func("/throttle/leak_bucket", test_leak_bucket);
640 g_test_add_func("/throttle/compute_wait", test_compute_wait);
641 g_test_add_func("/throttle/init", test_init);
642 g_test_add_func("/throttle/destroy", test_destroy);
643 g_test_add_func("/throttle/have_timer", test_have_timer);
644 g_test_add_func("/throttle/detach_attach", test_detach_attach);
645 g_test_add_func("/throttle/config/enabled", test_enabled);
646 g_test_add_func("/throttle/config/conflicting", test_conflicting_config);
647 g_test_add_func("/throttle/config/is_valid", test_is_valid);
648 g_test_add_func("/throttle/config/max", test_max_is_missing_limit);
649 g_test_add_func("/throttle/config_functions", test_config_functions);
650 g_test_add_func("/throttle/accounting", test_accounting);
651 g_test_add_func("/throttle/groups", test_groups);
652 return g_test_run();