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