]>
Commit | Line | Data |
---|---|---|
f17cfe81 BC |
1 | /* |
2 | * Throttle infrastructure tests | |
3 | * | |
4 | * Copyright Nodalink, SARL. 2013 | |
5 | * | |
6 | * Authors: | |
7 | * Benoît Canet <benoit.canet@irqsave.net> | |
8 | * | |
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. | |
11 | */ | |
12 | ||
13 | #include <glib.h> | |
14 | #include <math.h> | |
13af91eb | 15 | #include "block/aio.h" |
f17cfe81 | 16 | #include "qemu/throttle.h" |
2f78e491 | 17 | #include "qemu/error-report.h" |
f17cfe81 | 18 | |
748bfb4e SW |
19 | static AioContext *ctx; |
20 | static LeakyBucket bkt; | |
21 | static ThrottleConfig cfg; | |
22 | static ThrottleState ts; | |
f17cfe81 | 23 | |
73f395fa | 24 | /* useful function */ |
f17cfe81 BC |
25 | static bool double_cmp(double x, double y) |
26 | { | |
27 | return fabsl(x - y) < 1e-6; | |
28 | } | |
29 | ||
30 | /* tests for single bucket operations */ | |
31 | static void test_leak_bucket(void) | |
32 | { | |
33 | /* set initial value */ | |
34 | bkt.avg = 150; | |
35 | bkt.max = 15; | |
36 | bkt.level = 1.5; | |
37 | ||
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)); | |
43 | ||
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)); | |
49 | ||
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)); | |
55 | } | |
56 | ||
57 | static void test_compute_wait(void) | |
58 | { | |
59 | int64_t wait; | |
60 | int64_t result; | |
61 | ||
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); | |
68 | ||
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); | |
75 | ||
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); | |
82 | ||
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); | |
91 | } | |
92 | ||
93 | /* functions to test ThrottleState initialization/destroy methods */ | |
94 | static void read_timer_cb(void *opaque) | |
95 | { | |
96 | } | |
97 | ||
98 | static void write_timer_cb(void *opaque) | |
99 | { | |
100 | } | |
101 | ||
102 | static void test_init(void) | |
103 | { | |
104 | int i; | |
105 | ||
106 | /* fill the structure with crap */ | |
107 | memset(&ts, 1, sizeof(ts)); | |
108 | ||
109 | /* init the structure */ | |
13af91eb SH |
110 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, |
111 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
112 | |
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]); | |
117 | ||
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); | |
125 | } | |
126 | ||
127 | throttle_destroy(&ts); | |
128 | } | |
129 | ||
130 | static void test_destroy(void) | |
131 | { | |
132 | int i; | |
13af91eb SH |
133 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, |
134 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
135 | throttle_destroy(&ts); |
136 | for (i = 0; i < 2; i++) { | |
137 | g_assert(!ts.timers[i]); | |
138 | } | |
139 | } | |
140 | ||
141 | /* function to test throttle_config and throttle_get_config */ | |
142 | static void test_config_functions(void) | |
143 | { | |
144 | int i; | |
145 | ThrottleConfig orig_cfg, final_cfg; | |
146 | ||
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; | |
150 | ||
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; | |
154 | ||
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; | |
158 | ||
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; | |
162 | ||
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; | |
166 | ||
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; | |
170 | ||
171 | orig_cfg.op_size = 1; | |
172 | ||
13af91eb SH |
173 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, |
174 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
175 | /* structure reset by throttle_init previous_leak should be null */ |
176 | g_assert(!ts.previous_leak); | |
177 | throttle_config(&ts, &orig_cfg); | |
178 | ||
179 | /* has previous leak been initialized by throttle_config ? */ | |
180 | g_assert(ts.previous_leak); | |
181 | ||
182 | /* get back the fixed configuration */ | |
183 | throttle_get_config(&ts, &final_cfg); | |
184 | ||
185 | throttle_destroy(&ts); | |
186 | ||
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); | |
190 | ||
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); | |
194 | ||
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); | |
198 | ||
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); | |
202 | ||
203 | g_assert(final_cfg.op_size == 1); | |
204 | ||
205 | /* check bucket have been cleared */ | |
206 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
207 | g_assert(!final_cfg.buckets[i].level); | |
208 | } | |
209 | } | |
210 | ||
211 | /* functions to test is throttle is enabled by a config */ | |
212 | static void set_cfg_value(bool is_max, int index, int value) | |
213 | { | |
214 | if (is_max) { | |
215 | cfg.buckets[index].max = value; | |
216 | } else { | |
217 | cfg.buckets[index].avg = value; | |
218 | } | |
219 | } | |
220 | ||
221 | static void test_enabled(void) | |
222 | { | |
223 | int i; | |
224 | ||
225 | memset(&cfg, 0, sizeof(cfg)); | |
226 | g_assert(!throttle_enabled(&cfg)); | |
227 | ||
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)); | |
232 | } | |
233 | ||
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)); | |
238 | } | |
239 | } | |
240 | ||
241 | /* tests functions for throttle_conflicting */ | |
242 | ||
243 | static void test_conflicts_for_one_set(bool is_max, | |
244 | int total, | |
245 | int read, | |
246 | int write) | |
247 | { | |
248 | memset(&cfg, 0, sizeof(cfg)); | |
249 | g_assert(!throttle_conflicting(&cfg)); | |
250 | ||
251 | set_cfg_value(is_max, total, 1); | |
252 | set_cfg_value(is_max, read, 1); | |
253 | g_assert(throttle_conflicting(&cfg)); | |
254 | ||
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)); | |
259 | ||
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)); | |
265 | ||
266 | memset(&cfg, 0, sizeof(cfg)); | |
267 | set_cfg_value(is_max, total, 1); | |
268 | g_assert(!throttle_conflicting(&cfg)); | |
269 | ||
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)); | |
274 | } | |
275 | ||
276 | static void test_conflicting_config(void) | |
277 | { | |
278 | /* bps average conflicts */ | |
279 | test_conflicts_for_one_set(false, | |
280 | THROTTLE_BPS_TOTAL, | |
281 | THROTTLE_BPS_READ, | |
282 | THROTTLE_BPS_WRITE); | |
283 | ||
284 | /* ops average conflicts */ | |
285 | test_conflicts_for_one_set(false, | |
286 | THROTTLE_OPS_TOTAL, | |
287 | THROTTLE_OPS_READ, | |
288 | THROTTLE_OPS_WRITE); | |
289 | ||
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); | |
300 | } | |
301 | /* functions to test the throttle_is_valid function */ | |
302 | static void test_is_valid_for_value(int value, bool should_be_valid) | |
303 | { | |
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); | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | static void test_is_valid(void) | |
315 | { | |
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); | |
322 | } | |
323 | ||
324 | static void test_have_timer(void) | |
325 | { | |
326 | /* zero the structure */ | |
327 | memset(&ts, 0, sizeof(ts)); | |
328 | ||
73f395fa | 329 | /* no timer set should return false */ |
f17cfe81 BC |
330 | g_assert(!throttle_have_timer(&ts)); |
331 | ||
332 | /* init the structure */ | |
13af91eb SH |
333 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, |
334 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
335 | |
336 | /* timer set by init should return true */ | |
337 | g_assert(throttle_have_timer(&ts)); | |
338 | ||
339 | throttle_destroy(&ts); | |
340 | } | |
341 | ||
22524f72 SH |
342 | static void test_detach_attach(void) |
343 | { | |
344 | /* zero the structure */ | |
345 | memset(&ts, 0, sizeof(ts)); | |
346 | ||
347 | /* init the structure */ | |
348 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, | |
349 | read_timer_cb, write_timer_cb, &ts); | |
350 | ||
351 | /* timer set by init should return true */ | |
352 | g_assert(throttle_have_timer(&ts)); | |
353 | ||
354 | /* timer should no longer exist after detaching */ | |
355 | throttle_detach_aio_context(&ts); | |
356 | g_assert(!throttle_have_timer(&ts)); | |
357 | ||
358 | /* timer should exist again after attaching */ | |
359 | throttle_attach_aio_context(&ts, ctx); | |
360 | g_assert(throttle_have_timer(&ts)); | |
361 | ||
362 | throttle_destroy(&ts); | |
363 | } | |
364 | ||
f17cfe81 BC |
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) | |
372 | { | |
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; | |
382 | ||
383 | for (i = 0; i < 3; i++) { | |
384 | BucketType index = to_test[is_ops][i]; | |
385 | cfg.buckets[index].avg = avg; | |
386 | } | |
387 | ||
388 | cfg.op_size = op_size; | |
389 | ||
13af91eb SH |
390 | throttle_init(&ts, ctx, QEMU_CLOCK_VIRTUAL, |
391 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
392 | throttle_config(&ts, &cfg); |
393 | ||
394 | /* account a read */ | |
395 | throttle_account(&ts, false, size); | |
396 | /* account a write */ | |
397 | throttle_account(&ts, true, size); | |
398 | ||
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; | |
403 | } | |
404 | ||
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; | |
409 | } | |
410 | ||
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; | |
415 | } | |
416 | ||
417 | throttle_destroy(&ts); | |
418 | ||
419 | return true; | |
420 | } | |
421 | ||
422 | static void test_accounting(void) | |
423 | { | |
424 | /* tests for bps */ | |
425 | ||
426 | /* op of size 1 */ | |
427 | g_assert(do_test_accounting(false, | |
428 | 1 * 512, | |
429 | 150, | |
430 | 0, | |
431 | 1024, | |
432 | 512, | |
433 | 512)); | |
434 | ||
435 | /* op of size 2 */ | |
436 | g_assert(do_test_accounting(false, | |
437 | 2 * 512, | |
438 | 150, | |
439 | 0, | |
440 | 2048, | |
441 | 1024, | |
442 | 1024)); | |
443 | ||
444 | /* op of size 2 and orthogonal parameter change */ | |
445 | g_assert(do_test_accounting(false, | |
446 | 2 * 512, | |
447 | 150, | |
448 | 17, | |
449 | 2048, | |
450 | 1024, | |
451 | 1024)); | |
452 | ||
453 | ||
454 | /* tests for ops */ | |
455 | ||
456 | /* op of size 1 */ | |
457 | g_assert(do_test_accounting(true, | |
458 | 1 * 512, | |
459 | 150, | |
460 | 0, | |
461 | 2, | |
462 | 1, | |
463 | 1)); | |
464 | ||
465 | /* op of size 2 */ | |
466 | g_assert(do_test_accounting(true, | |
467 | 2 * 512, | |
468 | 150, | |
469 | 0, | |
470 | 2, | |
471 | 1, | |
472 | 1)); | |
473 | ||
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))); | |
482 | ||
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))); | |
491 | } | |
492 | ||
493 | int main(int argc, char **argv) | |
494 | { | |
13af91eb | 495 | GSource *src; |
2f78e491 | 496 | Error *local_error = NULL; |
13af91eb | 497 | |
f17cfe81 | 498 | init_clocks(); |
13af91eb | 499 | |
2f78e491 CN |
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); | |
506 | } | |
13af91eb SH |
507 | src = aio_get_g_source(ctx); |
508 | g_source_attach(src, NULL); | |
509 | g_source_unref(src); | |
510 | ||
f17cfe81 BC |
511 | do {} while (g_main_context_iteration(NULL, false)); |
512 | ||
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); | |
22524f72 | 520 | g_test_add_func("/throttle/detach_attach", test_detach_attach); |
f17cfe81 BC |
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(); | |
527 | } | |
528 |