]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-throttle.c
throttle: Make throttle_is_valid() set errp
[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 /* set initial value */
39 bkt.avg = 150;
40 bkt.max = 15;
41 bkt.level = 1.5;
42
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));
48
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));
54
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));
60 }
61
62 static void test_compute_wait(void)
63 {
64 int64_t wait;
65 int64_t result;
66
67 /* no operation limit set */
68 bkt.avg = 0;
69 bkt.max = 15;
70 bkt.level = 1.5;
71 wait = throttle_compute_wait(&bkt);
72 g_assert(!wait);
73
74 /* zero delta */
75 bkt.avg = 150;
76 bkt.max = 15;
77 bkt.level = 15;
78 wait = throttle_compute_wait(&bkt);
79 g_assert(!wait);
80
81 /* below zero delta */
82 bkt.avg = 150;
83 bkt.max = 15;
84 bkt.level = 9;
85 wait = throttle_compute_wait(&bkt);
86 g_assert(!wait);
87
88 /* half an operation above max */
89 bkt.avg = 150;
90 bkt.max = 15;
91 bkt.level = 15.5;
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);
96 }
97
98 /* functions to test ThrottleState initialization/destroy methods */
99 static void read_timer_cb(void *opaque)
100 {
101 }
102
103 static void write_timer_cb(void *opaque)
104 {
105 }
106
107 static void test_init(void)
108 {
109 int i;
110
111 /* fill the structures with crap */
112 memset(&ts, 1, sizeof(ts));
113 memset(&tt, 1, sizeof(tt));
114
115 /* init structures */
116 throttle_init(&ts);
117 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
118 read_timer_cb, write_timer_cb, &ts);
119
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]);
124
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);
132 }
133
134 throttle_timers_destroy(&tt);
135 }
136
137 static void test_destroy(void)
138 {
139 int i;
140 throttle_init(&ts);
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]);
146 }
147 }
148
149 /* function to test throttle_config and throttle_get_config */
150 static void test_config_functions(void)
151 {
152 int i;
153 ThrottleConfig orig_cfg, final_cfg;
154
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;
158
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;
162
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;
166
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;
170
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;
174
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;
178
179 orig_cfg.op_size = 1;
180
181 throttle_init(&ts);
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);
187
188 /* has previous leak been initialized by throttle_config ? */
189 g_assert(ts.previous_leak);
190
191 /* get back the fixed configuration */
192 throttle_get_config(&ts, &final_cfg);
193
194 throttle_timers_destroy(&tt);
195
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);
199
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);
203
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);
207
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);
211
212 g_assert(final_cfg.op_size == 1);
213
214 /* check bucket have been cleared */
215 for (i = 0; i < BUCKETS_COUNT; i++) {
216 g_assert(!final_cfg.buckets[i].level);
217 }
218 }
219
220 /* functions to test is throttle is enabled by a config */
221 static void set_cfg_value(bool is_max, int index, int value)
222 {
223 if (is_max) {
224 cfg.buckets[index].max = value;
225 } else {
226 cfg.buckets[index].avg = value;
227 }
228 }
229
230 static void test_enabled(void)
231 {
232 int i;
233
234 memset(&cfg, 0, sizeof(cfg));
235 g_assert(!throttle_enabled(&cfg));
236
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));
241 }
242
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));
247 }
248 }
249
250 /* tests functions for throttle_conflicting */
251
252 static void test_conflicts_for_one_set(bool is_max,
253 int total,
254 int read,
255 int write)
256 {
257 memset(&cfg, 0, sizeof(cfg));
258 g_assert(!throttle_conflicting(&cfg, NULL));
259
260 set_cfg_value(is_max, total, 1);
261 set_cfg_value(is_max, read, 1);
262 g_assert(throttle_conflicting(&cfg, NULL));
263
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, NULL));
268
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, NULL));
274
275 memset(&cfg, 0, sizeof(cfg));
276 set_cfg_value(is_max, total, 1);
277 g_assert(!throttle_conflicting(&cfg, NULL));
278
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, NULL));
283 }
284
285 static void test_conflicting_config(void)
286 {
287 /* bps average conflicts */
288 test_conflicts_for_one_set(false,
289 THROTTLE_BPS_TOTAL,
290 THROTTLE_BPS_READ,
291 THROTTLE_BPS_WRITE);
292
293 /* ops average conflicts */
294 test_conflicts_for_one_set(false,
295 THROTTLE_OPS_TOTAL,
296 THROTTLE_OPS_READ,
297 THROTTLE_OPS_WRITE);
298
299 /* bps average conflicts */
300 test_conflicts_for_one_set(true,
301 THROTTLE_BPS_TOTAL,
302 THROTTLE_BPS_READ,
303 THROTTLE_BPS_WRITE);
304 /* ops average conflicts */
305 test_conflicts_for_one_set(true,
306 THROTTLE_OPS_TOTAL,
307 THROTTLE_OPS_READ,
308 THROTTLE_OPS_WRITE);
309 }
310 /* functions to test the throttle_is_valid function */
311 static void test_is_valid_for_value(int value, bool should_be_valid)
312 {
313 int is_max, index;
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, NULL) == should_be_valid);
319 }
320 }
321 }
322
323 static void test_is_valid(void)
324 {
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);
331 }
332
333 static void test_max_is_missing_limit(void)
334 {
335 int i;
336
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, NULL));
342
343 cfg.buckets[i].max = 0;
344 cfg.buckets[i].avg = 0;
345 g_assert(!throttle_max_is_missing_limit(&cfg, NULL));
346
347 cfg.buckets[i].max = 0;
348 cfg.buckets[i].avg = 100;
349 g_assert(!throttle_max_is_missing_limit(&cfg, NULL));
350 }
351 }
352
353 static void test_have_timer(void)
354 {
355 /* zero structures */
356 memset(&ts, 0, sizeof(ts));
357 memset(&tt, 0, sizeof(tt));
358
359 /* no timer set should return false */
360 g_assert(!throttle_timers_are_initialized(&tt));
361
362 /* init structures */
363 throttle_init(&ts);
364 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
365 read_timer_cb, write_timer_cb, &ts);
366
367 /* timer set by init should return true */
368 g_assert(throttle_timers_are_initialized(&tt));
369
370 throttle_timers_destroy(&tt);
371 }
372
373 static void test_detach_attach(void)
374 {
375 /* zero structures */
376 memset(&ts, 0, sizeof(ts));
377 memset(&tt, 0, sizeof(tt));
378
379 /* init the structure */
380 throttle_init(&ts);
381 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
382 read_timer_cb, write_timer_cb, &ts);
383
384 /* timer set by init should return true */
385 g_assert(throttle_timers_are_initialized(&tt));
386
387 /* timer should no longer exist after detaching */
388 throttle_timers_detach_aio_context(&tt);
389 g_assert(!throttle_timers_are_initialized(&tt));
390
391 /* timer should exist again after attaching */
392 throttle_timers_attach_aio_context(&tt, ctx);
393 g_assert(throttle_timers_are_initialized(&tt));
394
395 throttle_timers_destroy(&tt);
396 }
397
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 */
402 double total_result,
403 double read_result,
404 double write_result)
405 {
406 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
407 THROTTLE_BPS_READ,
408 THROTTLE_BPS_WRITE, },
409 { THROTTLE_OPS_TOTAL,
410 THROTTLE_OPS_READ,
411 THROTTLE_OPS_WRITE, } };
412 ThrottleConfig cfg;
413 BucketType index;
414 int i;
415
416 for (i = 0; i < 3; i++) {
417 BucketType index = to_test[is_ops][i];
418 cfg.buckets[index].avg = avg;
419 }
420
421 cfg.op_size = op_size;
422
423 throttle_init(&ts);
424 throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
425 read_timer_cb, write_timer_cb, &ts);
426 throttle_config(&ts, &tt, &cfg);
427
428 /* account a read */
429 throttle_account(&ts, false, size);
430 /* account a write */
431 throttle_account(&ts, true, size);
432
433 /* check total result */
434 index = to_test[is_ops][0];
435 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
436 return false;
437 }
438
439 /* check read result */
440 index = to_test[is_ops][1];
441 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
442 return false;
443 }
444
445 /* check write result */
446 index = to_test[is_ops][2];
447 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
448 return false;
449 }
450
451 throttle_timers_destroy(&tt);
452
453 return true;
454 }
455
456 static void test_accounting(void)
457 {
458 /* tests for bps */
459
460 /* op of size 1 */
461 g_assert(do_test_accounting(false,
462 1 * 512,
463 150,
464 0,
465 1024,
466 512,
467 512));
468
469 /* op of size 2 */
470 g_assert(do_test_accounting(false,
471 2 * 512,
472 150,
473 0,
474 2048,
475 1024,
476 1024));
477
478 /* op of size 2 and orthogonal parameter change */
479 g_assert(do_test_accounting(false,
480 2 * 512,
481 150,
482 17,
483 2048,
484 1024,
485 1024));
486
487
488 /* tests for ops */
489
490 /* op of size 1 */
491 g_assert(do_test_accounting(true,
492 1 * 512,
493 150,
494 0,
495 2,
496 1,
497 1));
498
499 /* op of size 2 */
500 g_assert(do_test_accounting(true,
501 2 * 512,
502 150,
503 0,
504 2,
505 1,
506 1));
507
508 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
509 g_assert(do_test_accounting(true,
510 64 * 512,
511 150,
512 13 * 512,
513 (64.0 * 2) / 13,
514 (64.0 / 13),
515 (64.0 / 13)));
516
517 /* same with orthogonal parameters changes */
518 g_assert(do_test_accounting(true,
519 64 * 512,
520 300,
521 13 * 512,
522 (64.0 * 2) / 13,
523 (64.0 / 13),
524 (64.0 / 13)));
525 }
526
527 static void test_groups(void)
528 {
529 ThrottleConfig cfg1, cfg2;
530 BlockDriverState *bdrv1, *bdrv2, *bdrv3;
531
532 bdrv1 = bdrv_new();
533 bdrv2 = bdrv_new();
534 bdrv3 = bdrv_new();
535
536 g_assert(bdrv1->throttle_state == NULL);
537 g_assert(bdrv2->throttle_state == NULL);
538 g_assert(bdrv3->throttle_state == NULL);
539
540 throttle_group_register_bs(bdrv1, "bar");
541 throttle_group_register_bs(bdrv2, "foo");
542 throttle_group_register_bs(bdrv3, "bar");
543
544 g_assert(bdrv1->throttle_state != NULL);
545 g_assert(bdrv2->throttle_state != NULL);
546 g_assert(bdrv3->throttle_state != NULL);
547
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);
551
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);
559
560 throttle_group_get_config(bdrv1, &cfg1);
561 throttle_group_get_config(bdrv3, &cfg2);
562 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
563
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);
569
570 throttle_group_get_config(bdrv1, &cfg1);
571 throttle_group_get_config(bdrv3, &cfg2);
572 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
573
574 throttle_group_unregister_bs(bdrv1);
575 throttle_group_unregister_bs(bdrv2);
576 throttle_group_unregister_bs(bdrv3);
577
578 g_assert(bdrv1->throttle_state == NULL);
579 g_assert(bdrv2->throttle_state == NULL);
580 g_assert(bdrv3->throttle_state == NULL);
581 }
582
583 int main(int argc, char **argv)
584 {
585 qemu_init_main_loop(&error_fatal);
586 ctx = qemu_get_aio_context();
587 bdrv_init();
588
589 do {} while (g_main_context_iteration(NULL, false));
590
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);
606 return g_test_run();
607 }
608