acpi: add aml_varpackage() term
[qemu/qmp-unstable.git] / tests / test-throttle.c
blobd8ba415e43fa8cae2261de573581f713850af2ab
1 /*
2 * Throttle infrastructure tests
4 * Copyright Nodalink, SARL. 2013
6 * Authors:
7 * BenoƮt Canet <benoit.canet@irqsave.net>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include <glib.h>
14 #include <math.h>
15 #include "block/aio.h"
16 #include "qemu/throttle.h"
17 #include "qemu/error-report.h"
19 static AioContext *ctx;
20 static LeakyBucket bkt;
21 static ThrottleConfig cfg;
22 static ThrottleState ts;
24 /* useful function */
25 static bool double_cmp(double x, double y)
27 return fabsl(x - y) < 1e-6;
30 /* tests for single bucket operations */
31 static void test_leak_bucket(void)
33 /* set initial value */
34 bkt.avg = 150;
35 bkt.max = 15;
36 bkt.level = 1.5;
38 /* leak an op work of time */
39 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
40 g_assert(bkt.avg == 150);
41 g_assert(bkt.max == 15);
42 g_assert(double_cmp(bkt.level, 0.5));
44 /* leak again emptying the bucket */
45 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
46 g_assert(bkt.avg == 150);
47 g_assert(bkt.max == 15);
48 g_assert(double_cmp(bkt.level, 0));
50 /* check that the bucket level won't go lower */
51 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
52 g_assert(bkt.avg == 150);
53 g_assert(bkt.max == 15);
54 g_assert(double_cmp(bkt.level, 0));
57 static void test_compute_wait(void)
59 int64_t wait;
60 int64_t result;
62 /* no operation limit set */
63 bkt.avg = 0;
64 bkt.max = 15;
65 bkt.level = 1.5;
66 wait = throttle_compute_wait(&bkt);
67 g_assert(!wait);
69 /* zero delta */
70 bkt.avg = 150;
71 bkt.max = 15;
72 bkt.level = 15;
73 wait = throttle_compute_wait(&bkt);
74 g_assert(!wait);
76 /* below zero delta */
77 bkt.avg = 150;
78 bkt.max = 15;
79 bkt.level = 9;
80 wait = throttle_compute_wait(&bkt);
81 g_assert(!wait);
83 /* half an operation above max */
84 bkt.avg = 150;
85 bkt.max = 15;
86 bkt.level = 15.5;
87 wait = throttle_compute_wait(&bkt);
88 /* time required to do half an operation */
89 result = (int64_t) NANOSECONDS_PER_SECOND / 150 / 2;
90 g_assert(wait == result);
93 /* functions to test ThrottleState initialization/destroy methods */
94 static void read_timer_cb(void *opaque)
98 static void write_timer_cb(void *opaque)
102 static void test_init(void)
104 int i;
106 /* fill the structure with crap */
107 memset(&ts, 1, sizeof(ts));
109 /* init the structure */
110 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
111 read_timer_cb, write_timer_cb, &ts);
113 /* check initialized fields */
114 g_assert(ts.clock_type == QEMU_CLOCK_VIRTUAL);
115 g_assert(ts.timers[0]);
116 g_assert(ts.timers[1]);
118 /* check other fields where cleared */
119 g_assert(!ts.previous_leak);
120 g_assert(!ts.cfg.op_size);
121 for (i = 0; i < BUCKETS_COUNT; i++) {
122 g_assert(!ts.cfg.buckets[i].avg);
123 g_assert(!ts.cfg.buckets[i].max);
124 g_assert(!ts.cfg.buckets[i].level);
127 throttle_destroy(&ts);
130 static void test_destroy(void)
132 int i;
133 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
134 read_timer_cb, write_timer_cb, &ts);
135 throttle_destroy(&ts);
136 for (i = 0; i < 2; i++) {
137 g_assert(!ts.timers[i]);
141 /* function to test throttle_config and throttle_get_config */
142 static void test_config_functions(void)
144 int i;
145 ThrottleConfig orig_cfg, final_cfg;
147 orig_cfg.buckets[THROTTLE_BPS_TOTAL].avg = 153;
148 orig_cfg.buckets[THROTTLE_BPS_READ].avg = 56;
149 orig_cfg.buckets[THROTTLE_BPS_WRITE].avg = 1;
151 orig_cfg.buckets[THROTTLE_OPS_TOTAL].avg = 150;
152 orig_cfg.buckets[THROTTLE_OPS_READ].avg = 69;
153 orig_cfg.buckets[THROTTLE_OPS_WRITE].avg = 23;
155 orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0; /* should be corrected */
156 orig_cfg.buckets[THROTTLE_BPS_READ].max = 1; /* should not be corrected */
157 orig_cfg.buckets[THROTTLE_BPS_WRITE].max = 120;
159 orig_cfg.buckets[THROTTLE_OPS_TOTAL].max = 150;
160 orig_cfg.buckets[THROTTLE_OPS_READ].max = 400;
161 orig_cfg.buckets[THROTTLE_OPS_WRITE].max = 500;
163 orig_cfg.buckets[THROTTLE_BPS_TOTAL].level = 45;
164 orig_cfg.buckets[THROTTLE_BPS_READ].level = 65;
165 orig_cfg.buckets[THROTTLE_BPS_WRITE].level = 23;
167 orig_cfg.buckets[THROTTLE_OPS_TOTAL].level = 1;
168 orig_cfg.buckets[THROTTLE_OPS_READ].level = 90;
169 orig_cfg.buckets[THROTTLE_OPS_WRITE].level = 75;
171 orig_cfg.op_size = 1;
173 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
174 read_timer_cb, write_timer_cb, &ts);
175 /* structure reset by throttle_init previous_leak should be null */
176 g_assert(!ts.previous_leak);
177 throttle_config(&ts, &orig_cfg);
179 /* has previous leak been initialized by throttle_config ? */
180 g_assert(ts.previous_leak);
182 /* get back the fixed configuration */
183 throttle_get_config(&ts, &final_cfg);
185 throttle_destroy(&ts);
187 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153);
188 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg == 56);
189 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].avg == 1);
191 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].avg == 150);
192 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].avg == 69);
193 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].avg == 23);
195 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 15.3);/* fixed */
196 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 1); /* not fixed */
197 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].max == 120);
199 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].max == 150);
200 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].max == 400);
201 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].max == 500);
203 g_assert(final_cfg.op_size == 1);
205 /* check bucket have been cleared */
206 for (i = 0; i < BUCKETS_COUNT; i++) {
207 g_assert(!final_cfg.buckets[i].level);
211 /* functions to test is throttle is enabled by a config */
212 static void set_cfg_value(bool is_max, int index, int value)
214 if (is_max) {
215 cfg.buckets[index].max = value;
216 } else {
217 cfg.buckets[index].avg = value;
221 static void test_enabled(void)
223 int i;
225 memset(&cfg, 0, sizeof(cfg));
226 g_assert(!throttle_enabled(&cfg));
228 for (i = 0; i < BUCKETS_COUNT; i++) {
229 memset(&cfg, 0, sizeof(cfg));
230 set_cfg_value(false, i, 150);
231 g_assert(throttle_enabled(&cfg));
234 for (i = 0; i < BUCKETS_COUNT; i++) {
235 memset(&cfg, 0, sizeof(cfg));
236 set_cfg_value(false, i, -150);
237 g_assert(!throttle_enabled(&cfg));
241 /* tests functions for throttle_conflicting */
243 static void test_conflicts_for_one_set(bool is_max,
244 int total,
245 int read,
246 int write)
248 memset(&cfg, 0, sizeof(cfg));
249 g_assert(!throttle_conflicting(&cfg));
251 set_cfg_value(is_max, total, 1);
252 set_cfg_value(is_max, read, 1);
253 g_assert(throttle_conflicting(&cfg));
255 memset(&cfg, 0, sizeof(cfg));
256 set_cfg_value(is_max, total, 1);
257 set_cfg_value(is_max, write, 1);
258 g_assert(throttle_conflicting(&cfg));
260 memset(&cfg, 0, sizeof(cfg));
261 set_cfg_value(is_max, total, 1);
262 set_cfg_value(is_max, read, 1);
263 set_cfg_value(is_max, write, 1);
264 g_assert(throttle_conflicting(&cfg));
266 memset(&cfg, 0, sizeof(cfg));
267 set_cfg_value(is_max, total, 1);
268 g_assert(!throttle_conflicting(&cfg));
270 memset(&cfg, 0, sizeof(cfg));
271 set_cfg_value(is_max, read, 1);
272 set_cfg_value(is_max, write, 1);
273 g_assert(!throttle_conflicting(&cfg));
276 static void test_conflicting_config(void)
278 /* bps average conflicts */
279 test_conflicts_for_one_set(false,
280 THROTTLE_BPS_TOTAL,
281 THROTTLE_BPS_READ,
282 THROTTLE_BPS_WRITE);
284 /* ops average conflicts */
285 test_conflicts_for_one_set(false,
286 THROTTLE_OPS_TOTAL,
287 THROTTLE_OPS_READ,
288 THROTTLE_OPS_WRITE);
290 /* bps average conflicts */
291 test_conflicts_for_one_set(true,
292 THROTTLE_BPS_TOTAL,
293 THROTTLE_BPS_READ,
294 THROTTLE_BPS_WRITE);
295 /* ops average conflicts */
296 test_conflicts_for_one_set(true,
297 THROTTLE_OPS_TOTAL,
298 THROTTLE_OPS_READ,
299 THROTTLE_OPS_WRITE);
301 /* functions to test the throttle_is_valid function */
302 static void test_is_valid_for_value(int value, bool should_be_valid)
304 int is_max, index;
305 for (is_max = 0; is_max < 2; is_max++) {
306 for (index = 0; index < BUCKETS_COUNT; index++) {
307 memset(&cfg, 0, sizeof(cfg));
308 set_cfg_value(is_max, index, value);
309 g_assert(throttle_is_valid(&cfg) == should_be_valid);
314 static void test_is_valid(void)
316 /* negative number are invalid */
317 test_is_valid_for_value(-1, false);
318 /* zero are valids */
319 test_is_valid_for_value(0, true);
320 /* positives numers are valids */
321 test_is_valid_for_value(1, true);
324 static void test_have_timer(void)
326 /* zero the structure */
327 memset(&ts, 0, sizeof(ts));
329 /* no timer set should return false */
330 g_assert(!throttle_have_timer(&ts));
332 /* init the structure */
333 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
334 read_timer_cb, write_timer_cb, &ts);
336 /* timer set by init should return true */
337 g_assert(throttle_have_timer(&ts));
339 throttle_destroy(&ts);
342 static void test_detach_attach(void)
344 /* zero the structure */
345 memset(&ts, 0, sizeof(ts));
347 /* init the structure */
348 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
349 read_timer_cb, write_timer_cb, &ts);
351 /* timer set by init should return true */
352 g_assert(throttle_have_timer(&ts));
354 /* timer should no longer exist after detaching */
355 throttle_detach_aio_context(&ts);
356 g_assert(!throttle_have_timer(&ts));
358 /* timer should exist again after attaching */
359 throttle_attach_aio_context(&ts, ctx);
360 g_assert(throttle_have_timer(&ts));
362 throttle_destroy(&ts);
365 static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
366 int size, /* size of the operation to do */
367 double avg, /* io limit */
368 uint64_t op_size, /* ideal size of an io */
369 double total_result,
370 double read_result,
371 double write_result)
373 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
374 THROTTLE_BPS_READ,
375 THROTTLE_BPS_WRITE, },
376 { THROTTLE_OPS_TOTAL,
377 THROTTLE_OPS_READ,
378 THROTTLE_OPS_WRITE, } };
379 ThrottleConfig cfg;
380 BucketType index;
381 int i;
383 for (i = 0; i < 3; i++) {
384 BucketType index = to_test[is_ops][i];
385 cfg.buckets[index].avg = avg;
388 cfg.op_size = op_size;
390 throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL,
391 read_timer_cb, write_timer_cb, &ts);
392 throttle_config(&ts, &cfg);
394 /* account a read */
395 throttle_account(&ts, false, size);
396 /* account a write */
397 throttle_account(&ts, true, size);
399 /* check total result */
400 index = to_test[is_ops][0];
401 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
402 return false;
405 /* check read result */
406 index = to_test[is_ops][1];
407 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
408 return false;
411 /* check write result */
412 index = to_test[is_ops][2];
413 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
414 return false;
417 throttle_destroy(&ts);
419 return true;
422 static void test_accounting(void)
424 /* tests for bps */
426 /* op of size 1 */
427 g_assert(do_test_accounting(false,
428 1 * 512,
429 150,
431 1024,
432 512,
433 512));
435 /* op of size 2 */
436 g_assert(do_test_accounting(false,
437 2 * 512,
438 150,
440 2048,
441 1024,
442 1024));
444 /* op of size 2 and orthogonal parameter change */
445 g_assert(do_test_accounting(false,
446 2 * 512,
447 150,
449 2048,
450 1024,
451 1024));
454 /* tests for ops */
456 /* op of size 1 */
457 g_assert(do_test_accounting(true,
458 1 * 512,
459 150,
463 1));
465 /* op of size 2 */
466 g_assert(do_test_accounting(true,
467 2 * 512,
468 150,
472 1));
474 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
475 g_assert(do_test_accounting(true,
476 64 * 512,
477 150,
478 13 * 512,
479 (64.0 * 2) / 13,
480 (64.0 / 13),
481 (64.0 / 13)));
483 /* same with orthogonal parameters changes */
484 g_assert(do_test_accounting(true,
485 64 * 512,
486 300,
487 13 * 512,
488 (64.0 * 2) / 13,
489 (64.0 / 13),
490 (64.0 / 13)));
493 int main(int argc, char **argv)
495 GSource *src;
496 Error *local_error = NULL;
498 init_clocks();
500 ctx = aio_context_new(&local_error);
501 if (!ctx) {
502 error_report("Failed to create AIO Context: '%s'",
503 error_get_pretty(local_error));
504 error_free(local_error);
505 exit(1);
507 src = aio_get_g_source(ctx);
508 g_source_attach(src, NULL);
509 g_source_unref(src);
511 do {} while (g_main_context_iteration(NULL, false));
513 /* tests in the same order as the header function declarations */
514 g_test_init(&argc, &argv, NULL);
515 g_test_add_func("/throttle/leak_bucket", test_leak_bucket);
516 g_test_add_func("/throttle/compute_wait", test_compute_wait);
517 g_test_add_func("/throttle/init", test_init);
518 g_test_add_func("/throttle/destroy", test_destroy);
519 g_test_add_func("/throttle/have_timer", test_have_timer);
520 g_test_add_func("/throttle/detach_attach", test_detach_attach);
521 g_test_add_func("/throttle/config/enabled", test_enabled);
522 g_test_add_func("/throttle/config/conflicting", test_conflicting_config);
523 g_test_add_func("/throttle/config/is_valid", test_is_valid);
524 g_test_add_func("/throttle/config_functions", test_config_functions);
525 g_test_add_func("/throttle/accounting", test_accounting);
526 return g_test_run();