2 * Throttle infrastructure tests
4 * Copyright Nodalink, SARL. 2013
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.
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
;
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 */
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)
62 /* no operation limit set */
66 wait
= throttle_compute_wait(&bkt
);
73 wait
= throttle_compute_wait(&bkt
);
76 /* below zero delta */
80 wait
= throttle_compute_wait(&bkt
);
83 /* half an operation above max */
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)
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)
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)
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
)
215 cfg
.buckets
[index
].max
= value
;
217 cfg
.buckets
[index
].avg
= value
;
221 static void test_enabled(void)
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
,
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,
284 /* ops average conflicts */
285 test_conflicts_for_one_set(false,
290 /* bps average conflicts */
291 test_conflicts_for_one_set(true,
295 /* ops average conflicts */
296 test_conflicts_for_one_set(true,
301 /* functions to test the throttle_is_valid function */
302 static void test_is_valid_for_value(int value
, bool should_be_valid
)
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 */
373 BucketType to_test
[2][3] = { { THROTTLE_BPS_TOTAL
,
375 THROTTLE_BPS_WRITE
, },
376 { THROTTLE_OPS_TOTAL
,
378 THROTTLE_OPS_WRITE
, } };
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
);
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
)) {
405 /* check read result */
406 index
= to_test
[is_ops
][1];
407 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, read_result
)) {
411 /* check write result */
412 index
= to_test
[is_ops
][2];
413 if (!double_cmp(ts
.cfg
.buckets
[index
].level
, write_result
)) {
417 throttle_destroy(&ts
);
422 static void test_accounting(void)
427 g_assert(do_test_accounting(false,
436 g_assert(do_test_accounting(false,
444 /* op of size 2 and orthogonal parameter change */
445 g_assert(do_test_accounting(false,
457 g_assert(do_test_accounting(true,
466 g_assert(do_test_accounting(true,
474 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
475 g_assert(do_test_accounting(true,
483 /* same with orthogonal parameters changes */
484 g_assert(do_test_accounting(true,
493 int main(int argc
, char **argv
)
496 Error
*local_error
= NULL
;
500 ctx
= aio_context_new(&local_error
);
502 error_report("Failed to create AIO Context: '%s'",
503 error_get_pretty(local_error
));
504 error_free(local_error
);
507 src
= aio_get_g_source(ctx
);
508 g_source_attach(src
, NULL
);
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
);