block/iscsi: allow fall back to WRITE SAME without UNMAP
[qemu/ar7.git] / tests / test-throttle.c
blob1d4ffd3603542d8be137b127e13a7acf96bf5dc9
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 "qemu/throttle.h"
17 LeakyBucket bkt;
18 ThrottleConfig cfg;
19 ThrottleState ts;
21 /* useful function */
22 static bool double_cmp(double x, double y)
24 return fabsl(x - y) < 1e-6;
27 /* tests for single bucket operations */
28 static void test_leak_bucket(void)
30 /* set initial value */
31 bkt.avg = 150;
32 bkt.max = 15;
33 bkt.level = 1.5;
35 /* leak an op work of time */
36 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
37 g_assert(bkt.avg == 150);
38 g_assert(bkt.max == 15);
39 g_assert(double_cmp(bkt.level, 0.5));
41 /* leak again emptying the bucket */
42 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
43 g_assert(bkt.avg == 150);
44 g_assert(bkt.max == 15);
45 g_assert(double_cmp(bkt.level, 0));
47 /* check that the bucket level won't go lower */
48 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
49 g_assert(bkt.avg == 150);
50 g_assert(bkt.max == 15);
51 g_assert(double_cmp(bkt.level, 0));
54 static void test_compute_wait(void)
56 int64_t wait;
57 int64_t result;
59 /* no operation limit set */
60 bkt.avg = 0;
61 bkt.max = 15;
62 bkt.level = 1.5;
63 wait = throttle_compute_wait(&bkt);
64 g_assert(!wait);
66 /* zero delta */
67 bkt.avg = 150;
68 bkt.max = 15;
69 bkt.level = 15;
70 wait = throttle_compute_wait(&bkt);
71 g_assert(!wait);
73 /* below zero delta */
74 bkt.avg = 150;
75 bkt.max = 15;
76 bkt.level = 9;
77 wait = throttle_compute_wait(&bkt);
78 g_assert(!wait);
80 /* half an operation above max */
81 bkt.avg = 150;
82 bkt.max = 15;
83 bkt.level = 15.5;
84 wait = throttle_compute_wait(&bkt);
85 /* time required to do half an operation */
86 result = (int64_t) NANOSECONDS_PER_SECOND / 150 / 2;
87 g_assert(wait == result);
90 /* functions to test ThrottleState initialization/destroy methods */
91 static void read_timer_cb(void *opaque)
95 static void write_timer_cb(void *opaque)
99 static void test_init(void)
101 int i;
103 /* fill the structure with crap */
104 memset(&ts, 1, sizeof(ts));
106 /* init the structure */
107 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
109 /* check initialized fields */
110 g_assert(ts.clock_type == QEMU_CLOCK_VIRTUAL);
111 g_assert(ts.timers[0]);
112 g_assert(ts.timers[1]);
114 /* check other fields where cleared */
115 g_assert(!ts.previous_leak);
116 g_assert(!ts.cfg.op_size);
117 for (i = 0; i < BUCKETS_COUNT; i++) {
118 g_assert(!ts.cfg.buckets[i].avg);
119 g_assert(!ts.cfg.buckets[i].max);
120 g_assert(!ts.cfg.buckets[i].level);
123 throttle_destroy(&ts);
126 static void test_destroy(void)
128 int i;
129 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
130 throttle_destroy(&ts);
131 for (i = 0; i < 2; i++) {
132 g_assert(!ts.timers[i]);
136 /* function to test throttle_config and throttle_get_config */
137 static void test_config_functions(void)
139 int i;
140 ThrottleConfig orig_cfg, final_cfg;
142 orig_cfg.buckets[THROTTLE_BPS_TOTAL].avg = 153;
143 orig_cfg.buckets[THROTTLE_BPS_READ].avg = 56;
144 orig_cfg.buckets[THROTTLE_BPS_WRITE].avg = 1;
146 orig_cfg.buckets[THROTTLE_OPS_TOTAL].avg = 150;
147 orig_cfg.buckets[THROTTLE_OPS_READ].avg = 69;
148 orig_cfg.buckets[THROTTLE_OPS_WRITE].avg = 23;
150 orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0; /* should be corrected */
151 orig_cfg.buckets[THROTTLE_BPS_READ].max = 1; /* should not be corrected */
152 orig_cfg.buckets[THROTTLE_BPS_WRITE].max = 120;
154 orig_cfg.buckets[THROTTLE_OPS_TOTAL].max = 150;
155 orig_cfg.buckets[THROTTLE_OPS_READ].max = 400;
156 orig_cfg.buckets[THROTTLE_OPS_WRITE].max = 500;
158 orig_cfg.buckets[THROTTLE_BPS_TOTAL].level = 45;
159 orig_cfg.buckets[THROTTLE_BPS_READ].level = 65;
160 orig_cfg.buckets[THROTTLE_BPS_WRITE].level = 23;
162 orig_cfg.buckets[THROTTLE_OPS_TOTAL].level = 1;
163 orig_cfg.buckets[THROTTLE_OPS_READ].level = 90;
164 orig_cfg.buckets[THROTTLE_OPS_WRITE].level = 75;
166 orig_cfg.op_size = 1;
168 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
169 /* structure reset by throttle_init previous_leak should be null */
170 g_assert(!ts.previous_leak);
171 throttle_config(&ts, &orig_cfg);
173 /* has previous leak been initialized by throttle_config ? */
174 g_assert(ts.previous_leak);
176 /* get back the fixed configuration */
177 throttle_get_config(&ts, &final_cfg);
179 throttle_destroy(&ts);
181 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153);
182 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg == 56);
183 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].avg == 1);
185 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].avg == 150);
186 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].avg == 69);
187 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].avg == 23);
189 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 15.3);/* fixed */
190 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 1); /* not fixed */
191 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].max == 120);
193 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].max == 150);
194 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].max == 400);
195 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].max == 500);
197 g_assert(final_cfg.op_size == 1);
199 /* check bucket have been cleared */
200 for (i = 0; i < BUCKETS_COUNT; i++) {
201 g_assert(!final_cfg.buckets[i].level);
205 /* functions to test is throttle is enabled by a config */
206 static void set_cfg_value(bool is_max, int index, int value)
208 if (is_max) {
209 cfg.buckets[index].max = value;
210 } else {
211 cfg.buckets[index].avg = value;
215 static void test_enabled(void)
217 int i;
219 memset(&cfg, 0, sizeof(cfg));
220 g_assert(!throttle_enabled(&cfg));
222 for (i = 0; i < BUCKETS_COUNT; i++) {
223 memset(&cfg, 0, sizeof(cfg));
224 set_cfg_value(false, i, 150);
225 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));
235 /* tests functions for throttle_conflicting */
237 static void test_conflicts_for_one_set(bool is_max,
238 int total,
239 int read,
240 int write)
242 memset(&cfg, 0, sizeof(cfg));
243 g_assert(!throttle_conflicting(&cfg));
245 set_cfg_value(is_max, total, 1);
246 set_cfg_value(is_max, read, 1);
247 g_assert(throttle_conflicting(&cfg));
249 memset(&cfg, 0, sizeof(cfg));
250 set_cfg_value(is_max, total, 1);
251 set_cfg_value(is_max, write, 1);
252 g_assert(throttle_conflicting(&cfg));
254 memset(&cfg, 0, sizeof(cfg));
255 set_cfg_value(is_max, total, 1);
256 set_cfg_value(is_max, read, 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 g_assert(!throttle_conflicting(&cfg));
264 memset(&cfg, 0, sizeof(cfg));
265 set_cfg_value(is_max, read, 1);
266 set_cfg_value(is_max, write, 1);
267 g_assert(!throttle_conflicting(&cfg));
270 static void test_conflicting_config(void)
272 /* bps average conflicts */
273 test_conflicts_for_one_set(false,
274 THROTTLE_BPS_TOTAL,
275 THROTTLE_BPS_READ,
276 THROTTLE_BPS_WRITE);
278 /* ops average conflicts */
279 test_conflicts_for_one_set(false,
280 THROTTLE_OPS_TOTAL,
281 THROTTLE_OPS_READ,
282 THROTTLE_OPS_WRITE);
284 /* bps average conflicts */
285 test_conflicts_for_one_set(true,
286 THROTTLE_BPS_TOTAL,
287 THROTTLE_BPS_READ,
288 THROTTLE_BPS_WRITE);
289 /* ops average conflicts */
290 test_conflicts_for_one_set(true,
291 THROTTLE_OPS_TOTAL,
292 THROTTLE_OPS_READ,
293 THROTTLE_OPS_WRITE);
295 /* functions to test the throttle_is_valid function */
296 static void test_is_valid_for_value(int value, bool should_be_valid)
298 int is_max, index;
299 for (is_max = 0; is_max < 2; is_max++) {
300 for (index = 0; index < BUCKETS_COUNT; index++) {
301 memset(&cfg, 0, sizeof(cfg));
302 set_cfg_value(is_max, index, value);
303 g_assert(throttle_is_valid(&cfg) == should_be_valid);
308 static void test_is_valid(void)
310 /* negative number are invalid */
311 test_is_valid_for_value(-1, false);
312 /* zero are valids */
313 test_is_valid_for_value(0, true);
314 /* positives numers are valids */
315 test_is_valid_for_value(1, true);
318 static void test_have_timer(void)
320 /* zero the structure */
321 memset(&ts, 0, sizeof(ts));
323 /* no timer set should return false */
324 g_assert(!throttle_have_timer(&ts));
326 /* init the structure */
327 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
329 /* timer set by init should return true */
330 g_assert(throttle_have_timer(&ts));
332 throttle_destroy(&ts);
335 static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
336 int size, /* size of the operation to do */
337 double avg, /* io limit */
338 uint64_t op_size, /* ideal size of an io */
339 double total_result,
340 double read_result,
341 double write_result)
343 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
344 THROTTLE_BPS_READ,
345 THROTTLE_BPS_WRITE, },
346 { THROTTLE_OPS_TOTAL,
347 THROTTLE_OPS_READ,
348 THROTTLE_OPS_WRITE, } };
349 ThrottleConfig cfg;
350 BucketType index;
351 int i;
353 for (i = 0; i < 3; i++) {
354 BucketType index = to_test[is_ops][i];
355 cfg.buckets[index].avg = avg;
358 cfg.op_size = op_size;
360 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
361 throttle_config(&ts, &cfg);
363 /* account a read */
364 throttle_account(&ts, false, size);
365 /* account a write */
366 throttle_account(&ts, true, size);
368 /* check total result */
369 index = to_test[is_ops][0];
370 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
371 return false;
374 /* check read result */
375 index = to_test[is_ops][1];
376 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
377 return false;
380 /* check write result */
381 index = to_test[is_ops][2];
382 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
383 return false;
386 throttle_destroy(&ts);
388 return true;
391 static void test_accounting(void)
393 /* tests for bps */
395 /* op of size 1 */
396 g_assert(do_test_accounting(false,
397 1 * 512,
398 150,
400 1024,
401 512,
402 512));
404 /* op of size 2 */
405 g_assert(do_test_accounting(false,
406 2 * 512,
407 150,
409 2048,
410 1024,
411 1024));
413 /* op of size 2 and orthogonal parameter change */
414 g_assert(do_test_accounting(false,
415 2 * 512,
416 150,
418 2048,
419 1024,
420 1024));
423 /* tests for ops */
425 /* op of size 1 */
426 g_assert(do_test_accounting(true,
427 1 * 512,
428 150,
432 1));
434 /* op of size 2 */
435 g_assert(do_test_accounting(true,
436 2 * 512,
437 150,
441 1));
443 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
444 g_assert(do_test_accounting(true,
445 64 * 512,
446 150,
447 13 * 512,
448 (64.0 * 2) / 13,
449 (64.0 / 13),
450 (64.0 / 13)));
452 /* same with orthogonal parameters changes */
453 g_assert(do_test_accounting(true,
454 64 * 512,
455 300,
456 13 * 512,
457 (64.0 * 2) / 13,
458 (64.0 / 13),
459 (64.0 / 13)));
462 int main(int argc, char **argv)
464 init_clocks();
465 do {} while (g_main_context_iteration(NULL, false));
467 /* tests in the same order as the header function declarations */
468 g_test_init(&argc, &argv, NULL);
469 g_test_add_func("/throttle/leak_bucket", test_leak_bucket);
470 g_test_add_func("/throttle/compute_wait", test_compute_wait);
471 g_test_add_func("/throttle/init", test_init);
472 g_test_add_func("/throttle/destroy", test_destroy);
473 g_test_add_func("/throttle/have_timer", test_have_timer);
474 g_test_add_func("/throttle/config/enabled", test_enabled);
475 g_test_add_func("/throttle/config/conflicting", test_conflicting_config);
476 g_test_add_func("/throttle/config/is_valid", test_is_valid);
477 g_test_add_func("/throttle/config_functions", test_config_functions);
478 g_test_add_func("/throttle/accounting", test_accounting);
479 return g_test_run();