]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-throttle.c
throttle: Use throttle_config_init() to initialize ThrottleConfig
[mirror_qemu.git] / tests / test-throttle.c
1 /*
2 * Throttle infrastructure tests
3 *
4 * Copyright Nodalink, EURL. 2013-2014
5 * Copyright Igalia, S.L. 2015
6 *
7 * Authors:
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
10 *
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.
13 */
14
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"
22
23 static AioContext *ctx;
24 static LeakyBucket bkt;
25 static ThrottleConfig cfg;
26 static ThrottleState ts;
27 static ThrottleTimers tt;
28
29 /* useful function */
30 static bool double_cmp(double x, double y)
31 {
32 return fabsl(x - y) < 1e-6;
33 }
34
35 /* tests for single bucket operations */
36 static void test_leak_bucket(void)
37 {
38 throttle_config_init(&cfg);
39 bkt = cfg.buckets[THROTTLE_BPS_TOTAL];
40
41 /* set initial value */
42 bkt.avg = 150;
43 bkt.max = 15;
44 bkt.level = 1.5;
45
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));
51
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));
57
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));
63 }
64
65 static void test_compute_wait(void)
66 {
67 int64_t wait;
68 int64_t result;
69
70 throttle_config_init(&cfg);
71 bkt = cfg.buckets[THROTTLE_BPS_TOTAL];
72
73 /* no operation limit set */
74 bkt.avg = 0;
75 bkt.max = 15;
76 bkt.level = 1.5;
77 wait = throttle_compute_wait(&bkt);
78 g_assert(!wait);
79
80 /* zero delta */
81 bkt.avg = 150;
82 bkt.max = 15;
83 bkt.level = 15;
84 wait = throttle_compute_wait(&bkt);
85 g_assert(!wait);
86
87 /* below zero delta */
88 bkt.avg = 150;
89 bkt.max = 15;
90 bkt.level = 9;
91 wait = throttle_compute_wait(&bkt);
92 g_assert(!wait);
93
94 /* half an operation above max */
95 bkt.avg = 150;
96 bkt.max = 15;
97 bkt.level = 15.5;
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);
102 }
103
104 /* functions to test ThrottleState initialization/destroy methods */
105 static void read_timer_cb(void *opaque)
106 {
107 }
108
109 static void write_timer_cb(void *opaque)
110 {
111 }
112
113 static void test_init(void)
114 {
115 int i;
116
117 /* fill the structures with crap */
118 memset(&ts, 1, sizeof(ts));
119 memset(&tt, 1, sizeof(tt));
120
121 /* init structures */
122 throttle_init(&ts);
123 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
124 read_timer_cb, write_timer_cb, &ts);
125
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]);
130
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);
138 }
139
140 throttle_timers_destroy(&tt);
141 }
142
143 static void test_destroy(void)
144 {
145 int i;
146 throttle_init(&ts);
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]);
152 }
153 }
154
155 /* function to test throttle_config and throttle_get_config */
156 static void test_config_functions(void)
157 {
158 int i;
159 ThrottleConfig orig_cfg, final_cfg;
160
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;
164
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;
168
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;
172
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;
176
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;
180
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;
184
185 orig_cfg.op_size = 1;
186
187 throttle_init(&ts);
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);
193
194 /* has previous leak been initialized by throttle_config ? */
195 g_assert(ts.previous_leak);
196
197 /* get back the fixed configuration */
198 throttle_get_config(&ts, &final_cfg);
199
200 throttle_timers_destroy(&tt);
201
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);
205
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);
209
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);
213
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);
217
218 g_assert(final_cfg.op_size == 1);
219
220 /* check bucket have been cleared */
221 for (i = 0; i < BUCKETS_COUNT; i++) {
222 g_assert(!final_cfg.buckets[i].level);
223 }
224 }
225
226 /* functions to test is throttle is enabled by a config */
227 static void set_cfg_value(bool is_max, int index, int value)
228 {
229 if (is_max) {
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);
233 } else {
234 cfg.buckets[index].avg = value;
235 }
236 }
237
238 static void test_enabled(void)
239 {
240 int i;
241
242 throttle_config_init(&cfg);
243 g_assert(!throttle_enabled(&cfg));
244
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));
249 }
250
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));
255 }
256 }
257
258 /* tests functions for throttle_conflicting */
259
260 static void test_conflicts_for_one_set(bool is_max,
261 int total,
262 int read,
263 int write)
264 {
265 throttle_config_init(&cfg);
266 g_assert(throttle_is_valid(&cfg, NULL));
267
268 set_cfg_value(is_max, total, 1);
269 set_cfg_value(is_max, read, 1);
270 g_assert(!throttle_is_valid(&cfg, NULL));
271
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));
276
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));
282
283 throttle_config_init(&cfg);
284 set_cfg_value(is_max, total, 1);
285 g_assert(throttle_is_valid(&cfg, NULL));
286
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));
291 }
292
293 static void test_conflicting_config(void)
294 {
295 /* bps average conflicts */
296 test_conflicts_for_one_set(false,
297 THROTTLE_BPS_TOTAL,
298 THROTTLE_BPS_READ,
299 THROTTLE_BPS_WRITE);
300
301 /* ops average conflicts */
302 test_conflicts_for_one_set(false,
303 THROTTLE_OPS_TOTAL,
304 THROTTLE_OPS_READ,
305 THROTTLE_OPS_WRITE);
306
307 /* bps average conflicts */
308 test_conflicts_for_one_set(true,
309 THROTTLE_BPS_TOTAL,
310 THROTTLE_BPS_READ,
311 THROTTLE_BPS_WRITE);
312 /* ops average conflicts */
313 test_conflicts_for_one_set(true,
314 THROTTLE_OPS_TOTAL,
315 THROTTLE_OPS_READ,
316 THROTTLE_OPS_WRITE);
317 }
318 /* functions to test the throttle_is_valid function */
319 static void test_is_valid_for_value(int value, bool should_be_valid)
320 {
321 int is_max, index;
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);
327 }
328 }
329 }
330
331 static void test_is_valid(void)
332 {
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);
339 }
340
341 static void test_max_is_missing_limit(void)
342 {
343 int i;
344
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));
350
351 cfg.buckets[i].max = 0;
352 cfg.buckets[i].avg = 0;
353 g_assert(throttle_is_valid(&cfg, NULL));
354
355 cfg.buckets[i].max = 0;
356 cfg.buckets[i].avg = 100;
357 g_assert(throttle_is_valid(&cfg, NULL));
358 }
359 }
360
361 static void test_have_timer(void)
362 {
363 /* zero structures */
364 memset(&ts, 0, sizeof(ts));
365 memset(&tt, 0, sizeof(tt));
366
367 /* no timer set should return false */
368 g_assert(!throttle_timers_are_initialized(&tt));
369
370 /* init structures */
371 throttle_init(&ts);
372 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
373 read_timer_cb, write_timer_cb, &ts);
374
375 /* timer set by init should return true */
376 g_assert(throttle_timers_are_initialized(&tt));
377
378 throttle_timers_destroy(&tt);
379 }
380
381 static void test_detach_attach(void)
382 {
383 /* zero structures */
384 memset(&ts, 0, sizeof(ts));
385 memset(&tt, 0, sizeof(tt));
386
387 /* init the structure */
388 throttle_init(&ts);
389 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
390 read_timer_cb, write_timer_cb, &ts);
391
392 /* timer set by init should return true */
393 g_assert(throttle_timers_are_initialized(&tt));
394
395 /* timer should no longer exist after detaching */
396 throttle_timers_detach_aio_context(&tt);
397 g_assert(!throttle_timers_are_initialized(&tt));
398
399 /* timer should exist again after attaching */
400 throttle_timers_attach_aio_context(&tt, ctx);
401 g_assert(throttle_timers_are_initialized(&tt));
402
403 throttle_timers_destroy(&tt);
404 }
405
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 */
410 double total_result,
411 double read_result,
412 double write_result)
413 {
414 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
415 THROTTLE_BPS_READ,
416 THROTTLE_BPS_WRITE, },
417 { THROTTLE_OPS_TOTAL,
418 THROTTLE_OPS_READ,
419 THROTTLE_OPS_WRITE, } };
420 ThrottleConfig cfg;
421 BucketType index;
422 int i;
423
424 for (i = 0; i < 3; i++) {
425 BucketType index = to_test[is_ops][i];
426 cfg.buckets[index].avg = avg;
427 }
428
429 cfg.op_size = op_size;
430
431 throttle_init(&ts);
432 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
433 read_timer_cb, write_timer_cb, &ts);
434 throttle_config(&ts, &tt, &cfg);
435
436 /* account a read */
437 throttle_account(&ts, false, size);
438 /* account a write */
439 throttle_account(&ts, true, size);
440
441 /* check total result */
442 index = to_test[is_ops][0];
443 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
444 return false;
445 }
446
447 /* check read result */
448 index = to_test[is_ops][1];
449 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
450 return false;
451 }
452
453 /* check write result */
454 index = to_test[is_ops][2];
455 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
456 return false;
457 }
458
459 throttle_timers_destroy(&tt);
460
461 return true;
462 }
463
464 static void test_accounting(void)
465 {
466 /* tests for bps */
467
468 /* op of size 1 */
469 g_assert(do_test_accounting(false,
470 1 * 512,
471 150,
472 0,
473 1024,
474 512,
475 512));
476
477 /* op of size 2 */
478 g_assert(do_test_accounting(false,
479 2 * 512,
480 150,
481 0,
482 2048,
483 1024,
484 1024));
485
486 /* op of size 2 and orthogonal parameter change */
487 g_assert(do_test_accounting(false,
488 2 * 512,
489 150,
490 17,
491 2048,
492 1024,
493 1024));
494
495
496 /* tests for ops */
497
498 /* op of size 1 */
499 g_assert(do_test_accounting(true,
500 1 * 512,
501 150,
502 0,
503 2,
504 1,
505 1));
506
507 /* op of size 2 */
508 g_assert(do_test_accounting(true,
509 2 * 512,
510 150,
511 0,
512 2,
513 1,
514 1));
515
516 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
517 g_assert(do_test_accounting(true,
518 64 * 512,
519 150,
520 13 * 512,
521 (64.0 * 2) / 13,
522 (64.0 / 13),
523 (64.0 / 13)));
524
525 /* same with orthogonal parameters changes */
526 g_assert(do_test_accounting(true,
527 64 * 512,
528 300,
529 13 * 512,
530 (64.0 * 2) / 13,
531 (64.0 / 13),
532 (64.0 / 13)));
533 }
534
535 static void test_groups(void)
536 {
537 ThrottleConfig cfg1, cfg2;
538 BlockDriverState *bdrv1, *bdrv2, *bdrv3;
539
540 bdrv1 = bdrv_new();
541 bdrv2 = bdrv_new();
542 bdrv3 = bdrv_new();
543
544 g_assert(bdrv1->throttle_state == NULL);
545 g_assert(bdrv2->throttle_state == NULL);
546 g_assert(bdrv3->throttle_state == NULL);
547
548 throttle_group_register_bs(bdrv1, "bar");
549 throttle_group_register_bs(bdrv2, "foo");
550 throttle_group_register_bs(bdrv3, "bar");
551
552 g_assert(bdrv1->throttle_state != NULL);
553 g_assert(bdrv2->throttle_state != NULL);
554 g_assert(bdrv3->throttle_state != NULL);
555
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);
559
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);
567
568 throttle_group_get_config(bdrv1, &cfg1);
569 throttle_group_get_config(bdrv3, &cfg2);
570 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
571
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);
577
578 throttle_group_get_config(bdrv1, &cfg1);
579 throttle_group_get_config(bdrv3, &cfg2);
580 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
581
582 throttle_group_unregister_bs(bdrv1);
583 throttle_group_unregister_bs(bdrv2);
584 throttle_group_unregister_bs(bdrv3);
585
586 g_assert(bdrv1->throttle_state == NULL);
587 g_assert(bdrv2->throttle_state == NULL);
588 g_assert(bdrv3->throttle_state == NULL);
589 }
590
591 int main(int argc, char **argv)
592 {
593 qemu_init_main_loop(&error_fatal);
594 ctx = qemu_get_aio_context();
595 bdrv_init();
596
597 do {} while (g_main_context_iteration(NULL, false));
598
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);
614 return g_test_run();
615 }
616