]> git.proxmox.com Git - qemu.git/blame - tests/test-throttle.c
target-openrisc: Remove executable flag for every page
[qemu.git] / tests / test-throttle.c
CommitLineData
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>
15#include "qemu/throttle.h"
16
17LeakyBucket bkt;
18ThrottleConfig cfg;
19ThrottleState ts;
20
73f395fa 21/* useful function */
f17cfe81
BC
22static bool double_cmp(double x, double y)
23{
24 return fabsl(x - y) < 1e-6;
25}
26
27/* tests for single bucket operations */
28static void test_leak_bucket(void)
29{
30 /* set initial value */
31 bkt.avg = 150;
32 bkt.max = 15;
33 bkt.level = 1.5;
34
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));
40
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));
46
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));
52}
53
54static void test_compute_wait(void)
55{
56 int64_t wait;
57 int64_t result;
58
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);
65
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);
72
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);
79
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);
88}
89
90/* functions to test ThrottleState initialization/destroy methods */
91static void read_timer_cb(void *opaque)
92{
93}
94
95static void write_timer_cb(void *opaque)
96{
97}
98
99static void test_init(void)
100{
101 int i;
102
103 /* fill the structure with crap */
104 memset(&ts, 1, sizeof(ts));
105
106 /* init the structure */
107 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
108
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]);
113
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);
121 }
122
123 throttle_destroy(&ts);
124}
125
126static void test_destroy(void)
127{
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]);
133 }
134}
135
136/* function to test throttle_config and throttle_get_config */
137static void test_config_functions(void)
138{
139 int i;
140 ThrottleConfig orig_cfg, final_cfg;
141
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;
145
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;
149
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;
153
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;
157
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;
161
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;
165
166 orig_cfg.op_size = 1;
167
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);
172
173 /* has previous leak been initialized by throttle_config ? */
174 g_assert(ts.previous_leak);
175
176 /* get back the fixed configuration */
177 throttle_get_config(&ts, &final_cfg);
178
179 throttle_destroy(&ts);
180
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);
184
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);
188
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);
192
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);
196
197 g_assert(final_cfg.op_size == 1);
198
199 /* check bucket have been cleared */
200 for (i = 0; i < BUCKETS_COUNT; i++) {
201 g_assert(!final_cfg.buckets[i].level);
202 }
203}
204
205/* functions to test is throttle is enabled by a config */
206static void set_cfg_value(bool is_max, int index, int value)
207{
208 if (is_max) {
209 cfg.buckets[index].max = value;
210 } else {
211 cfg.buckets[index].avg = value;
212 }
213}
214
215static void test_enabled(void)
216{
217 int i;
218
219 memset(&cfg, 0, sizeof(cfg));
220 g_assert(!throttle_enabled(&cfg));
221
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));
226 }
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
235/* tests functions for throttle_conflicting */
236
237static void test_conflicts_for_one_set(bool is_max,
238 int total,
239 int read,
240 int write)
241{
242 memset(&cfg, 0, sizeof(cfg));
243 g_assert(!throttle_conflicting(&cfg));
244
245 set_cfg_value(is_max, total, 1);
246 set_cfg_value(is_max, read, 1);
247 g_assert(throttle_conflicting(&cfg));
248
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));
253
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));
259
260 memset(&cfg, 0, sizeof(cfg));
261 set_cfg_value(is_max, total, 1);
262 g_assert(!throttle_conflicting(&cfg));
263
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));
268}
269
270static void test_conflicting_config(void)
271{
272 /* bps average conflicts */
273 test_conflicts_for_one_set(false,
274 THROTTLE_BPS_TOTAL,
275 THROTTLE_BPS_READ,
276 THROTTLE_BPS_WRITE);
277
278 /* ops average conflicts */
279 test_conflicts_for_one_set(false,
280 THROTTLE_OPS_TOTAL,
281 THROTTLE_OPS_READ,
282 THROTTLE_OPS_WRITE);
283
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);
294}
295/* functions to test the throttle_is_valid function */
296static void test_is_valid_for_value(int value, bool should_be_valid)
297{
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);
304 }
305 }
306}
307
308static void test_is_valid(void)
309{
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);
316}
317
318static void test_have_timer(void)
319{
320 /* zero the structure */
321 memset(&ts, 0, sizeof(ts));
322
73f395fa 323 /* no timer set should return false */
f17cfe81
BC
324 g_assert(!throttle_have_timer(&ts));
325
326 /* init the structure */
327 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
328
329 /* timer set by init should return true */
330 g_assert(throttle_have_timer(&ts));
331
332 throttle_destroy(&ts);
333}
334
335static 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)
342{
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;
352
353 for (i = 0; i < 3; i++) {
354 BucketType index = to_test[is_ops][i];
355 cfg.buckets[index].avg = avg;
356 }
357
358 cfg.op_size = op_size;
359
360 throttle_init(&ts, QEMU_CLOCK_VIRTUAL, read_timer_cb, write_timer_cb, &ts);
361 throttle_config(&ts, &cfg);
362
363 /* account a read */
364 throttle_account(&ts, false, size);
365 /* account a write */
366 throttle_account(&ts, true, size);
367
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;
372 }
373
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;
378 }
379
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;
384 }
385
386 throttle_destroy(&ts);
387
388 return true;
389}
390
391static void test_accounting(void)
392{
393 /* tests for bps */
394
395 /* op of size 1 */
396 g_assert(do_test_accounting(false,
397 1 * 512,
398 150,
399 0,
400 1024,
401 512,
402 512));
403
404 /* op of size 2 */
405 g_assert(do_test_accounting(false,
406 2 * 512,
407 150,
408 0,
409 2048,
410 1024,
411 1024));
412
413 /* op of size 2 and orthogonal parameter change */
414 g_assert(do_test_accounting(false,
415 2 * 512,
416 150,
417 17,
418 2048,
419 1024,
420 1024));
421
422
423 /* tests for ops */
424
425 /* op of size 1 */
426 g_assert(do_test_accounting(true,
427 1 * 512,
428 150,
429 0,
430 2,
431 1,
432 1));
433
434 /* op of size 2 */
435 g_assert(do_test_accounting(true,
436 2 * 512,
437 150,
438 0,
439 2,
440 1,
441 1));
442
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)));
451
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)));
460}
461
462int main(int argc, char **argv)
463{
464 init_clocks();
465 do {} while (g_main_context_iteration(NULL, false));
466
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();
480}
481