]>
Commit | Line | Data |
---|---|---|
f17cfe81 BC |
1 | /* |
2 | * Throttle infrastructure tests | |
3 | * | |
1fee955f AG |
4 | * Copyright Nodalink, EURL. 2013-2014 |
5 | * Copyright Igalia, S.L. 2015 | |
f17cfe81 BC |
6 | * |
7 | * Authors: | |
1fee955f AG |
8 | * Benoît Canet <[email protected]> |
9 | * Alberto Garcia <[email protected]> | |
f17cfe81 BC |
10 | * |
11 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
12 | * See the COPYING.LIB file in the top-level directory. | |
13 | */ | |
14 | ||
681c28a3 | 15 | #include "qemu/osdep.h" |
f17cfe81 | 16 | #include <math.h> |
13af91eb | 17 | #include "block/aio.h" |
da34e65c | 18 | #include "qapi/error.h" |
f17cfe81 | 19 | #include "qemu/throttle.h" |
2f78e491 | 20 | #include "qemu/error-report.h" |
db725815 | 21 | #include "qemu/main-loop.h" |
0b8fa32f | 22 | #include "qemu/module.h" |
1fee955f | 23 | #include "block/throttle-groups.h" |
31dce3cc | 24 | #include "sysemu/block-backend.h" |
f17cfe81 | 25 | |
748bfb4e SW |
26 | static AioContext *ctx; |
27 | static LeakyBucket bkt; | |
28 | static ThrottleConfig cfg; | |
c61791fc | 29 | static ThrottleGroupMember tgm; |
748bfb4e | 30 | static ThrottleState ts; |
c61791fc | 31 | static ThrottleTimers *tt; |
f17cfe81 | 32 | |
73f395fa | 33 | /* useful function */ |
f17cfe81 BC |
34 | static bool double_cmp(double x, double y) |
35 | { | |
36 | return fabsl(x - y) < 1e-6; | |
37 | } | |
38 | ||
39 | /* tests for single bucket operations */ | |
40 | static void test_leak_bucket(void) | |
41 | { | |
1588ab5d AG |
42 | throttle_config_init(&cfg); |
43 | bkt = cfg.buckets[THROTTLE_BPS_TOTAL]; | |
44 | ||
f17cfe81 BC |
45 | /* set initial value */ |
46 | bkt.avg = 150; | |
47 | bkt.max = 15; | |
48 | bkt.level = 1.5; | |
49 | ||
50 | /* leak an op work of time */ | |
13566fe3 | 51 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150); |
f17cfe81 BC |
52 | g_assert(bkt.avg == 150); |
53 | g_assert(bkt.max == 15); | |
54 | g_assert(double_cmp(bkt.level, 0.5)); | |
55 | ||
56 | /* leak again emptying the bucket */ | |
13566fe3 | 57 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150); |
f17cfe81 BC |
58 | g_assert(bkt.avg == 150); |
59 | g_assert(bkt.max == 15); | |
60 | g_assert(double_cmp(bkt.level, 0)); | |
61 | ||
62 | /* check that the bucket level won't go lower */ | |
13566fe3 | 63 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150); |
f17cfe81 BC |
64 | g_assert(bkt.avg == 150); |
65 | g_assert(bkt.max == 15); | |
66 | g_assert(double_cmp(bkt.level, 0)); | |
eb8a1a1c AG |
67 | |
68 | /* check that burst_level leaks correctly */ | |
69 | bkt.burst_level = 6; | |
70 | bkt.max = 250; | |
71 | bkt.burst_length = 2; /* otherwise burst_level will not leak */ | |
72 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100); | |
73 | g_assert(double_cmp(bkt.burst_level, 3.5)); | |
74 | ||
75 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100); | |
76 | g_assert(double_cmp(bkt.burst_level, 1)); | |
77 | ||
78 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100); | |
79 | g_assert(double_cmp(bkt.burst_level, 0)); | |
80 | ||
81 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100); | |
82 | g_assert(double_cmp(bkt.burst_level, 0)); | |
f17cfe81 BC |
83 | } |
84 | ||
85 | static void test_compute_wait(void) | |
86 | { | |
f9d05885 | 87 | unsigned i; |
f17cfe81 BC |
88 | int64_t wait; |
89 | int64_t result; | |
90 | ||
1588ab5d AG |
91 | throttle_config_init(&cfg); |
92 | bkt = cfg.buckets[THROTTLE_BPS_TOTAL]; | |
93 | ||
f17cfe81 BC |
94 | /* no operation limit set */ |
95 | bkt.avg = 0; | |
96 | bkt.max = 15; | |
97 | bkt.level = 1.5; | |
98 | wait = throttle_compute_wait(&bkt); | |
99 | g_assert(!wait); | |
100 | ||
101 | /* zero delta */ | |
102 | bkt.avg = 150; | |
103 | bkt.max = 15; | |
104 | bkt.level = 15; | |
105 | wait = throttle_compute_wait(&bkt); | |
106 | g_assert(!wait); | |
107 | ||
108 | /* below zero delta */ | |
109 | bkt.avg = 150; | |
110 | bkt.max = 15; | |
111 | bkt.level = 9; | |
112 | wait = throttle_compute_wait(&bkt); | |
113 | g_assert(!wait); | |
114 | ||
115 | /* half an operation above max */ | |
116 | bkt.avg = 150; | |
117 | bkt.max = 15; | |
118 | bkt.level = 15.5; | |
119 | wait = throttle_compute_wait(&bkt); | |
120 | /* time required to do half an operation */ | |
13566fe3 | 121 | result = (int64_t) NANOSECONDS_PER_SECOND / 150 / 2; |
f17cfe81 | 122 | g_assert(wait == result); |
f9d05885 AG |
123 | |
124 | /* Perform I/O for 2.2 seconds at a rate of bkt.max */ | |
125 | bkt.burst_length = 2; | |
126 | bkt.level = 0; | |
127 | bkt.avg = 10; | |
128 | bkt.max = 200; | |
129 | for (i = 0; i < 22; i++) { | |
130 | double units = bkt.max / 10; | |
131 | bkt.level += units; | |
132 | bkt.burst_level += units; | |
133 | throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 10); | |
134 | wait = throttle_compute_wait(&bkt); | |
135 | g_assert(double_cmp(bkt.burst_level, 0)); | |
136 | g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10)); | |
137 | /* We can do bursts for the 2 seconds we have configured in | |
138 | * burst_length. We have 100 extra miliseconds of burst | |
139 | * because bkt.level has been leaking during this time. | |
140 | * After that, we have to wait. */ | |
141 | result = i < 21 ? 0 : 1.8 * NANOSECONDS_PER_SECOND; | |
142 | g_assert(wait == result); | |
143 | } | |
f17cfe81 BC |
144 | } |
145 | ||
146 | /* functions to test ThrottleState initialization/destroy methods */ | |
147 | static void read_timer_cb(void *opaque) | |
148 | { | |
149 | } | |
150 | ||
151 | static void write_timer_cb(void *opaque) | |
152 | { | |
153 | } | |
154 | ||
155 | static void test_init(void) | |
156 | { | |
157 | int i; | |
158 | ||
c61791fc MP |
159 | tt = &tgm.throttle_timers; |
160 | ||
0e5b0a2d | 161 | /* fill the structures with crap */ |
f17cfe81 | 162 | memset(&ts, 1, sizeof(ts)); |
c61791fc | 163 | memset(tt, 1, sizeof(*tt)); |
f17cfe81 | 164 | |
0e5b0a2d BC |
165 | /* init structures */ |
166 | throttle_init(&ts); | |
c61791fc | 167 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 168 | read_timer_cb, write_timer_cb, &ts); |
f17cfe81 BC |
169 | |
170 | /* check initialized fields */ | |
c61791fc MP |
171 | g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL); |
172 | g_assert(tt->timers[0]); | |
173 | g_assert(tt->timers[1]); | |
f17cfe81 BC |
174 | |
175 | /* check other fields where cleared */ | |
176 | g_assert(!ts.previous_leak); | |
177 | g_assert(!ts.cfg.op_size); | |
178 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
179 | g_assert(!ts.cfg.buckets[i].avg); | |
180 | g_assert(!ts.cfg.buckets[i].max); | |
181 | g_assert(!ts.cfg.buckets[i].level); | |
182 | } | |
183 | ||
c61791fc | 184 | throttle_timers_destroy(tt); |
f17cfe81 BC |
185 | } |
186 | ||
187 | static void test_destroy(void) | |
188 | { | |
189 | int i; | |
0e5b0a2d | 190 | throttle_init(&ts); |
c61791fc | 191 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 192 | read_timer_cb, write_timer_cb, &ts); |
c61791fc | 193 | throttle_timers_destroy(tt); |
f17cfe81 | 194 | for (i = 0; i < 2; i++) { |
c61791fc | 195 | g_assert(!tt->timers[i]); |
f17cfe81 BC |
196 | } |
197 | } | |
198 | ||
199 | /* function to test throttle_config and throttle_get_config */ | |
200 | static void test_config_functions(void) | |
201 | { | |
202 | int i; | |
203 | ThrottleConfig orig_cfg, final_cfg; | |
204 | ||
205 | orig_cfg.buckets[THROTTLE_BPS_TOTAL].avg = 153; | |
206 | orig_cfg.buckets[THROTTLE_BPS_READ].avg = 56; | |
207 | orig_cfg.buckets[THROTTLE_BPS_WRITE].avg = 1; | |
208 | ||
209 | orig_cfg.buckets[THROTTLE_OPS_TOTAL].avg = 150; | |
210 | orig_cfg.buckets[THROTTLE_OPS_READ].avg = 69; | |
211 | orig_cfg.buckets[THROTTLE_OPS_WRITE].avg = 23; | |
212 | ||
d72915c6 SH |
213 | orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0; |
214 | orig_cfg.buckets[THROTTLE_BPS_READ].max = 56; | |
f17cfe81 BC |
215 | orig_cfg.buckets[THROTTLE_BPS_WRITE].max = 120; |
216 | ||
217 | orig_cfg.buckets[THROTTLE_OPS_TOTAL].max = 150; | |
218 | orig_cfg.buckets[THROTTLE_OPS_READ].max = 400; | |
219 | orig_cfg.buckets[THROTTLE_OPS_WRITE].max = 500; | |
220 | ||
221 | orig_cfg.buckets[THROTTLE_BPS_TOTAL].level = 45; | |
222 | orig_cfg.buckets[THROTTLE_BPS_READ].level = 65; | |
223 | orig_cfg.buckets[THROTTLE_BPS_WRITE].level = 23; | |
224 | ||
225 | orig_cfg.buckets[THROTTLE_OPS_TOTAL].level = 1; | |
226 | orig_cfg.buckets[THROTTLE_OPS_READ].level = 90; | |
227 | orig_cfg.buckets[THROTTLE_OPS_WRITE].level = 75; | |
228 | ||
229 | orig_cfg.op_size = 1; | |
230 | ||
0e5b0a2d | 231 | throttle_init(&ts); |
c61791fc | 232 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 233 | read_timer_cb, write_timer_cb, &ts); |
f17cfe81 BC |
234 | /* structure reset by throttle_init previous_leak should be null */ |
235 | g_assert(!ts.previous_leak); | |
27e4cf13 | 236 | throttle_config(&ts, QEMU_CLOCK_VIRTUAL, &orig_cfg); |
f17cfe81 BC |
237 | |
238 | /* has previous leak been initialized by throttle_config ? */ | |
239 | g_assert(ts.previous_leak); | |
240 | ||
241 | /* get back the fixed configuration */ | |
242 | throttle_get_config(&ts, &final_cfg); | |
243 | ||
c61791fc | 244 | throttle_timers_destroy(tt); |
f17cfe81 BC |
245 | |
246 | g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153); | |
247 | g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg == 56); | |
248 | g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].avg == 1); | |
249 | ||
250 | g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].avg == 150); | |
251 | g_assert(final_cfg.buckets[THROTTLE_OPS_READ].avg == 69); | |
252 | g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].avg == 23); | |
253 | ||
d72915c6 SH |
254 | g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 0); |
255 | g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 56); | |
f17cfe81 BC |
256 | g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].max == 120); |
257 | ||
258 | g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].max == 150); | |
259 | g_assert(final_cfg.buckets[THROTTLE_OPS_READ].max == 400); | |
260 | g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].max == 500); | |
261 | ||
262 | g_assert(final_cfg.op_size == 1); | |
263 | ||
264 | /* check bucket have been cleared */ | |
265 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
266 | g_assert(!final_cfg.buckets[i].level); | |
267 | } | |
268 | } | |
269 | ||
270 | /* functions to test is throttle is enabled by a config */ | |
271 | static void set_cfg_value(bool is_max, int index, int value) | |
272 | { | |
273 | if (is_max) { | |
274 | cfg.buckets[index].max = value; | |
6f9b6d57 AG |
275 | /* If max is set, avg should never be 0 */ |
276 | cfg.buckets[index].avg = MAX(cfg.buckets[index].avg, 1); | |
f17cfe81 BC |
277 | } else { |
278 | cfg.buckets[index].avg = value; | |
279 | } | |
280 | } | |
281 | ||
282 | static void test_enabled(void) | |
283 | { | |
284 | int i; | |
285 | ||
1588ab5d | 286 | throttle_config_init(&cfg); |
f17cfe81 BC |
287 | g_assert(!throttle_enabled(&cfg)); |
288 | ||
289 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
1588ab5d | 290 | throttle_config_init(&cfg); |
f17cfe81 | 291 | set_cfg_value(false, i, 150); |
d00e6923 | 292 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
293 | g_assert(throttle_enabled(&cfg)); |
294 | } | |
295 | ||
296 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
1588ab5d | 297 | throttle_config_init(&cfg); |
f17cfe81 | 298 | set_cfg_value(false, i, -150); |
d00e6923 | 299 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
300 | } |
301 | } | |
302 | ||
303 | /* tests functions for throttle_conflicting */ | |
304 | ||
305 | static void test_conflicts_for_one_set(bool is_max, | |
306 | int total, | |
307 | int read, | |
308 | int write) | |
309 | { | |
1588ab5d | 310 | throttle_config_init(&cfg); |
d5851089 | 311 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
312 | |
313 | set_cfg_value(is_max, total, 1); | |
314 | set_cfg_value(is_max, read, 1); | |
d5851089 | 315 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 316 | |
1588ab5d | 317 | throttle_config_init(&cfg); |
f17cfe81 BC |
318 | set_cfg_value(is_max, total, 1); |
319 | set_cfg_value(is_max, write, 1); | |
d5851089 | 320 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 321 | |
1588ab5d | 322 | throttle_config_init(&cfg); |
f17cfe81 BC |
323 | set_cfg_value(is_max, total, 1); |
324 | set_cfg_value(is_max, read, 1); | |
325 | set_cfg_value(is_max, write, 1); | |
d5851089 | 326 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 327 | |
1588ab5d | 328 | throttle_config_init(&cfg); |
f17cfe81 | 329 | set_cfg_value(is_max, total, 1); |
d5851089 | 330 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 331 | |
1588ab5d | 332 | throttle_config_init(&cfg); |
f17cfe81 BC |
333 | set_cfg_value(is_max, read, 1); |
334 | set_cfg_value(is_max, write, 1); | |
d5851089 | 335 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
336 | } |
337 | ||
338 | static void test_conflicting_config(void) | |
339 | { | |
340 | /* bps average conflicts */ | |
341 | test_conflicts_for_one_set(false, | |
342 | THROTTLE_BPS_TOTAL, | |
343 | THROTTLE_BPS_READ, | |
344 | THROTTLE_BPS_WRITE); | |
345 | ||
346 | /* ops average conflicts */ | |
347 | test_conflicts_for_one_set(false, | |
348 | THROTTLE_OPS_TOTAL, | |
349 | THROTTLE_OPS_READ, | |
350 | THROTTLE_OPS_WRITE); | |
351 | ||
352 | /* bps average conflicts */ | |
353 | test_conflicts_for_one_set(true, | |
354 | THROTTLE_BPS_TOTAL, | |
355 | THROTTLE_BPS_READ, | |
356 | THROTTLE_BPS_WRITE); | |
357 | /* ops average conflicts */ | |
358 | test_conflicts_for_one_set(true, | |
359 | THROTTLE_OPS_TOTAL, | |
360 | THROTTLE_OPS_READ, | |
361 | THROTTLE_OPS_WRITE); | |
362 | } | |
363 | /* functions to test the throttle_is_valid function */ | |
364 | static void test_is_valid_for_value(int value, bool should_be_valid) | |
365 | { | |
366 | int is_max, index; | |
367 | for (is_max = 0; is_max < 2; is_max++) { | |
368 | for (index = 0; index < BUCKETS_COUNT; index++) { | |
1588ab5d | 369 | throttle_config_init(&cfg); |
f17cfe81 | 370 | set_cfg_value(is_max, index, value); |
03ba36c8 | 371 | g_assert(throttle_is_valid(&cfg, NULL) == should_be_valid); |
f17cfe81 BC |
372 | } |
373 | } | |
374 | } | |
375 | ||
376 | static void test_is_valid(void) | |
377 | { | |
378 | /* negative number are invalid */ | |
379 | test_is_valid_for_value(-1, false); | |
380 | /* zero are valids */ | |
381 | test_is_valid_for_value(0, true); | |
382 | /* positives numers are valids */ | |
383 | test_is_valid_for_value(1, true); | |
384 | } | |
385 | ||
d942feec AG |
386 | static void test_ranges(void) |
387 | { | |
388 | int i; | |
389 | ||
390 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
391 | LeakyBucket *b = &cfg.buckets[i]; | |
392 | throttle_config_init(&cfg); | |
393 | ||
394 | /* avg = 0 means throttling is disabled, but the config is valid */ | |
395 | b->avg = 0; | |
396 | g_assert(throttle_is_valid(&cfg, NULL)); | |
397 | g_assert(!throttle_enabled(&cfg)); | |
398 | ||
399 | /* These are valid configurations (values <= THROTTLE_VALUE_MAX) */ | |
400 | b->avg = 1; | |
401 | g_assert(throttle_is_valid(&cfg, NULL)); | |
402 | ||
403 | b->avg = THROTTLE_VALUE_MAX; | |
404 | g_assert(throttle_is_valid(&cfg, NULL)); | |
405 | ||
406 | b->avg = THROTTLE_VALUE_MAX; | |
407 | b->max = THROTTLE_VALUE_MAX; | |
408 | g_assert(throttle_is_valid(&cfg, NULL)); | |
409 | ||
410 | /* Values over THROTTLE_VALUE_MAX are not allowed */ | |
411 | b->avg = THROTTLE_VALUE_MAX + 1; | |
412 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
413 | ||
414 | b->avg = THROTTLE_VALUE_MAX; | |
415 | b->max = THROTTLE_VALUE_MAX + 1; | |
416 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
417 | ||
418 | /* burst_length must be between 1 and THROTTLE_VALUE_MAX */ | |
419 | b->avg = 1; | |
420 | b->max = 1; | |
421 | b->burst_length = 0; | |
422 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
423 | ||
424 | b->avg = 1; | |
425 | b->max = 1; | |
426 | b->burst_length = 1; | |
427 | g_assert(throttle_is_valid(&cfg, NULL)); | |
428 | ||
429 | b->avg = 1; | |
430 | b->max = 1; | |
431 | b->burst_length = THROTTLE_VALUE_MAX; | |
432 | g_assert(throttle_is_valid(&cfg, NULL)); | |
433 | ||
434 | b->avg = 1; | |
435 | b->max = 1; | |
436 | b->burst_length = THROTTLE_VALUE_MAX + 1; | |
437 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
438 | ||
439 | /* burst_length * max cannot exceed THROTTLE_VALUE_MAX */ | |
440 | b->avg = 1; | |
441 | b->max = 2; | |
442 | b->burst_length = THROTTLE_VALUE_MAX / 2; | |
443 | g_assert(throttle_is_valid(&cfg, NULL)); | |
444 | ||
445 | b->avg = 1; | |
446 | b->max = 3; | |
447 | b->burst_length = THROTTLE_VALUE_MAX / 2; | |
448 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
449 | ||
450 | b->avg = 1; | |
451 | b->max = THROTTLE_VALUE_MAX; | |
452 | b->burst_length = 1; | |
453 | g_assert(throttle_is_valid(&cfg, NULL)); | |
454 | ||
455 | b->avg = 1; | |
456 | b->max = THROTTLE_VALUE_MAX; | |
457 | b->burst_length = 2; | |
458 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
459 | } | |
460 | } | |
461 | ||
92e11a17 SH |
462 | static void test_max_is_missing_limit(void) |
463 | { | |
464 | int i; | |
465 | ||
466 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
1588ab5d | 467 | throttle_config_init(&cfg); |
92e11a17 SH |
468 | cfg.buckets[i].max = 100; |
469 | cfg.buckets[i].avg = 0; | |
d5851089 | 470 | g_assert(!throttle_is_valid(&cfg, NULL)); |
92e11a17 SH |
471 | |
472 | cfg.buckets[i].max = 0; | |
473 | cfg.buckets[i].avg = 0; | |
d5851089 | 474 | g_assert(throttle_is_valid(&cfg, NULL)); |
92e11a17 SH |
475 | |
476 | cfg.buckets[i].max = 0; | |
477 | cfg.buckets[i].avg = 100; | |
d5851089 | 478 | g_assert(throttle_is_valid(&cfg, NULL)); |
5fc8c052 AG |
479 | |
480 | cfg.buckets[i].max = 30; | |
481 | cfg.buckets[i].avg = 100; | |
482 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
483 | ||
484 | cfg.buckets[i].max = 100; | |
485 | cfg.buckets[i].avg = 100; | |
486 | g_assert(throttle_is_valid(&cfg, NULL)); | |
92e11a17 SH |
487 | } |
488 | } | |
489 | ||
8860eabd SH |
490 | static void test_iops_size_is_missing_limit(void) |
491 | { | |
492 | /* A total/read/write iops limit is required */ | |
493 | throttle_config_init(&cfg); | |
494 | cfg.op_size = 4096; | |
495 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
496 | } | |
497 | ||
f17cfe81 BC |
498 | static void test_have_timer(void) |
499 | { | |
0e5b0a2d | 500 | /* zero structures */ |
f17cfe81 | 501 | memset(&ts, 0, sizeof(ts)); |
c61791fc | 502 | memset(tt, 0, sizeof(*tt)); |
f17cfe81 | 503 | |
73f395fa | 504 | /* no timer set should return false */ |
c61791fc | 505 | g_assert(!throttle_timers_are_initialized(tt)); |
f17cfe81 | 506 | |
0e5b0a2d BC |
507 | /* init structures */ |
508 | throttle_init(&ts); | |
c61791fc | 509 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 510 | read_timer_cb, write_timer_cb, &ts); |
f17cfe81 BC |
511 | |
512 | /* timer set by init should return true */ | |
c61791fc | 513 | g_assert(throttle_timers_are_initialized(tt)); |
f17cfe81 | 514 | |
c61791fc | 515 | throttle_timers_destroy(tt); |
f17cfe81 BC |
516 | } |
517 | ||
22524f72 SH |
518 | static void test_detach_attach(void) |
519 | { | |
0e5b0a2d | 520 | /* zero structures */ |
22524f72 | 521 | memset(&ts, 0, sizeof(ts)); |
c61791fc | 522 | memset(tt, 0, sizeof(*tt)); |
22524f72 SH |
523 | |
524 | /* init the structure */ | |
0e5b0a2d | 525 | throttle_init(&ts); |
c61791fc | 526 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 527 | read_timer_cb, write_timer_cb, &ts); |
22524f72 SH |
528 | |
529 | /* timer set by init should return true */ | |
c61791fc | 530 | g_assert(throttle_timers_are_initialized(tt)); |
22524f72 SH |
531 | |
532 | /* timer should no longer exist after detaching */ | |
c61791fc MP |
533 | throttle_timers_detach_aio_context(tt); |
534 | g_assert(!throttle_timers_are_initialized(tt)); | |
22524f72 SH |
535 | |
536 | /* timer should exist again after attaching */ | |
c61791fc MP |
537 | throttle_timers_attach_aio_context(tt, ctx); |
538 | g_assert(throttle_timers_are_initialized(tt)); | |
22524f72 | 539 | |
c61791fc | 540 | throttle_timers_destroy(tt); |
22524f72 SH |
541 | } |
542 | ||
f17cfe81 BC |
543 | static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */ |
544 | int size, /* size of the operation to do */ | |
545 | double avg, /* io limit */ | |
546 | uint64_t op_size, /* ideal size of an io */ | |
547 | double total_result, | |
548 | double read_result, | |
549 | double write_result) | |
550 | { | |
551 | BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL, | |
552 | THROTTLE_BPS_READ, | |
553 | THROTTLE_BPS_WRITE, }, | |
554 | { THROTTLE_OPS_TOTAL, | |
555 | THROTTLE_OPS_READ, | |
556 | THROTTLE_OPS_WRITE, } }; | |
557 | ThrottleConfig cfg; | |
558 | BucketType index; | |
559 | int i; | |
560 | ||
561 | for (i = 0; i < 3; i++) { | |
562 | BucketType index = to_test[is_ops][i]; | |
563 | cfg.buckets[index].avg = avg; | |
564 | } | |
565 | ||
566 | cfg.op_size = op_size; | |
567 | ||
0e5b0a2d | 568 | throttle_init(&ts); |
c61791fc | 569 | throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL, |
0e5b0a2d | 570 | read_timer_cb, write_timer_cb, &ts); |
27e4cf13 | 571 | throttle_config(&ts, QEMU_CLOCK_VIRTUAL, &cfg); |
f17cfe81 BC |
572 | |
573 | /* account a read */ | |
574 | throttle_account(&ts, false, size); | |
575 | /* account a write */ | |
576 | throttle_account(&ts, true, size); | |
577 | ||
578 | /* check total result */ | |
579 | index = to_test[is_ops][0]; | |
580 | if (!double_cmp(ts.cfg.buckets[index].level, total_result)) { | |
581 | return false; | |
582 | } | |
583 | ||
584 | /* check read result */ | |
585 | index = to_test[is_ops][1]; | |
586 | if (!double_cmp(ts.cfg.buckets[index].level, read_result)) { | |
587 | return false; | |
588 | } | |
589 | ||
590 | /* check write result */ | |
591 | index = to_test[is_ops][2]; | |
592 | if (!double_cmp(ts.cfg.buckets[index].level, write_result)) { | |
593 | return false; | |
594 | } | |
595 | ||
c61791fc | 596 | throttle_timers_destroy(tt); |
f17cfe81 BC |
597 | |
598 | return true; | |
599 | } | |
600 | ||
601 | static void test_accounting(void) | |
602 | { | |
603 | /* tests for bps */ | |
604 | ||
605 | /* op of size 1 */ | |
606 | g_assert(do_test_accounting(false, | |
607 | 1 * 512, | |
608 | 150, | |
609 | 0, | |
610 | 1024, | |
611 | 512, | |
612 | 512)); | |
613 | ||
614 | /* op of size 2 */ | |
615 | g_assert(do_test_accounting(false, | |
616 | 2 * 512, | |
617 | 150, | |
618 | 0, | |
619 | 2048, | |
620 | 1024, | |
621 | 1024)); | |
622 | ||
623 | /* op of size 2 and orthogonal parameter change */ | |
624 | g_assert(do_test_accounting(false, | |
625 | 2 * 512, | |
626 | 150, | |
627 | 17, | |
628 | 2048, | |
629 | 1024, | |
630 | 1024)); | |
631 | ||
632 | ||
633 | /* tests for ops */ | |
634 | ||
635 | /* op of size 1 */ | |
636 | g_assert(do_test_accounting(true, | |
637 | 1 * 512, | |
638 | 150, | |
639 | 0, | |
640 | 2, | |
641 | 1, | |
642 | 1)); | |
643 | ||
644 | /* op of size 2 */ | |
645 | g_assert(do_test_accounting(true, | |
646 | 2 * 512, | |
647 | 150, | |
648 | 0, | |
649 | 2, | |
650 | 1, | |
651 | 1)); | |
652 | ||
653 | /* jumbo op accounting fragmentation : size 64 with op size of 13 units */ | |
654 | g_assert(do_test_accounting(true, | |
655 | 64 * 512, | |
656 | 150, | |
657 | 13 * 512, | |
658 | (64.0 * 2) / 13, | |
659 | (64.0 / 13), | |
660 | (64.0 / 13))); | |
661 | ||
662 | /* same with orthogonal parameters changes */ | |
663 | g_assert(do_test_accounting(true, | |
664 | 64 * 512, | |
665 | 300, | |
666 | 13 * 512, | |
667 | (64.0 * 2) / 13, | |
668 | (64.0 / 13), | |
669 | (64.0 / 13))); | |
670 | } | |
671 | ||
1fee955f AG |
672 | static void test_groups(void) |
673 | { | |
674 | ThrottleConfig cfg1, cfg2; | |
a5614993 | 675 | BlockBackend *blk1, *blk2, *blk3; |
27ccdd52 | 676 | BlockBackendPublic *blkp1, *blkp2, *blkp3; |
022cdc9f | 677 | ThrottleGroupMember *tgm1, *tgm2, *tgm3; |
1fee955f | 678 | |
2807c0cd | 679 | /* No actual I/O is performed on these devices */ |
d861ab3a KW |
680 | blk1 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
681 | blk2 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); | |
682 | blk3 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); | |
a5614993 | 683 | |
27ccdd52 KW |
684 | blkp1 = blk_get_public(blk1); |
685 | blkp2 = blk_get_public(blk2); | |
686 | blkp3 = blk_get_public(blk3); | |
687 | ||
022cdc9f MP |
688 | tgm1 = &blkp1->throttle_group_member; |
689 | tgm2 = &blkp2->throttle_group_member; | |
690 | tgm3 = &blkp3->throttle_group_member; | |
1fee955f | 691 | |
022cdc9f MP |
692 | g_assert(tgm1->throttle_state == NULL); |
693 | g_assert(tgm2->throttle_state == NULL); | |
694 | g_assert(tgm3->throttle_state == NULL); | |
1fee955f | 695 | |
c61791fc MP |
696 | throttle_group_register_tgm(tgm1, "bar", blk_get_aio_context(blk1)); |
697 | throttle_group_register_tgm(tgm2, "foo", blk_get_aio_context(blk2)); | |
698 | throttle_group_register_tgm(tgm3, "bar", blk_get_aio_context(blk3)); | |
1fee955f | 699 | |
022cdc9f MP |
700 | g_assert(tgm1->throttle_state != NULL); |
701 | g_assert(tgm2->throttle_state != NULL); | |
702 | g_assert(tgm3->throttle_state != NULL); | |
703 | ||
704 | g_assert(!strcmp(throttle_group_get_name(tgm1), "bar")); | |
705 | g_assert(!strcmp(throttle_group_get_name(tgm2), "foo")); | |
706 | g_assert(tgm1->throttle_state == tgm3->throttle_state); | |
1fee955f AG |
707 | |
708 | /* Setting the config of a group member affects the whole group */ | |
1588ab5d | 709 | throttle_config_init(&cfg1); |
1fee955f AG |
710 | cfg1.buckets[THROTTLE_BPS_READ].avg = 500000; |
711 | cfg1.buckets[THROTTLE_BPS_WRITE].avg = 285000; | |
712 | cfg1.buckets[THROTTLE_OPS_READ].avg = 20000; | |
713 | cfg1.buckets[THROTTLE_OPS_WRITE].avg = 12000; | |
022cdc9f | 714 | throttle_group_config(tgm1, &cfg1); |
1fee955f | 715 | |
022cdc9f MP |
716 | throttle_group_get_config(tgm1, &cfg1); |
717 | throttle_group_get_config(tgm3, &cfg2); | |
1fee955f AG |
718 | g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1))); |
719 | ||
720 | cfg2.buckets[THROTTLE_BPS_READ].avg = 4547; | |
721 | cfg2.buckets[THROTTLE_BPS_WRITE].avg = 1349; | |
722 | cfg2.buckets[THROTTLE_OPS_READ].avg = 123; | |
723 | cfg2.buckets[THROTTLE_OPS_WRITE].avg = 86; | |
022cdc9f | 724 | throttle_group_config(tgm3, &cfg1); |
1fee955f | 725 | |
022cdc9f MP |
726 | throttle_group_get_config(tgm1, &cfg1); |
727 | throttle_group_get_config(tgm3, &cfg2); | |
1fee955f AG |
728 | g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1))); |
729 | ||
022cdc9f MP |
730 | throttle_group_unregister_tgm(tgm1); |
731 | throttle_group_unregister_tgm(tgm2); | |
732 | throttle_group_unregister_tgm(tgm3); | |
1fee955f | 733 | |
022cdc9f MP |
734 | g_assert(tgm1->throttle_state == NULL); |
735 | g_assert(tgm2->throttle_state == NULL); | |
736 | g_assert(tgm3->throttle_state == NULL); | |
1fee955f AG |
737 | } |
738 | ||
f17cfe81 BC |
739 | int main(int argc, char **argv) |
740 | { | |
73eaa047 | 741 | qemu_init_main_loop(&error_fatal); |
1fee955f | 742 | ctx = qemu_get_aio_context(); |
1fee955f | 743 | bdrv_init(); |
432d889e | 744 | module_call_init(MODULE_INIT_QOM); |
13af91eb | 745 | |
f17cfe81 BC |
746 | do {} while (g_main_context_iteration(NULL, false)); |
747 | ||
748 | /* tests in the same order as the header function declarations */ | |
749 | g_test_init(&argc, &argv, NULL); | |
750 | g_test_add_func("/throttle/leak_bucket", test_leak_bucket); | |
751 | g_test_add_func("/throttle/compute_wait", test_compute_wait); | |
752 | g_test_add_func("/throttle/init", test_init); | |
753 | g_test_add_func("/throttle/destroy", test_destroy); | |
754 | g_test_add_func("/throttle/have_timer", test_have_timer); | |
22524f72 | 755 | g_test_add_func("/throttle/detach_attach", test_detach_attach); |
f17cfe81 BC |
756 | g_test_add_func("/throttle/config/enabled", test_enabled); |
757 | g_test_add_func("/throttle/config/conflicting", test_conflicting_config); | |
758 | g_test_add_func("/throttle/config/is_valid", test_is_valid); | |
d942feec | 759 | g_test_add_func("/throttle/config/ranges", test_ranges); |
92e11a17 | 760 | g_test_add_func("/throttle/config/max", test_max_is_missing_limit); |
8860eabd SH |
761 | g_test_add_func("/throttle/config/iops_size", |
762 | test_iops_size_is_missing_limit); | |
f17cfe81 BC |
763 | g_test_add_func("/throttle/config_functions", test_config_functions); |
764 | g_test_add_func("/throttle/accounting", test_accounting); | |
1fee955f | 765 | g_test_add_func("/throttle/groups", test_groups); |
f17cfe81 BC |
766 | return g_test_run(); |
767 | } | |
768 |