]>
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 | ||
208 | orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0; /* should be corrected */ | |
209 | orig_cfg.buckets[THROTTLE_BPS_READ].max = 1; /* should not be corrected */ | |
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); | |
0e5b0a2d | 231 | throttle_config(&ts, &tt, &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 | ||
249 | g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 15.3);/* fixed */ | |
250 | g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 1); /* not fixed */ | |
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 BC |
286 | set_cfg_value(false, i, 150); |
287 | g_assert(throttle_enabled(&cfg)); | |
288 | } | |
289 | ||
290 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
1588ab5d | 291 | throttle_config_init(&cfg); |
f17cfe81 BC |
292 | set_cfg_value(false, i, -150); |
293 | g_assert(!throttle_enabled(&cfg)); | |
294 | } | |
295 | } | |
296 | ||
297 | /* tests functions for throttle_conflicting */ | |
298 | ||
299 | static void test_conflicts_for_one_set(bool is_max, | |
300 | int total, | |
301 | int read, | |
302 | int write) | |
303 | { | |
1588ab5d | 304 | throttle_config_init(&cfg); |
d5851089 | 305 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
306 | |
307 | set_cfg_value(is_max, total, 1); | |
308 | set_cfg_value(is_max, read, 1); | |
d5851089 | 309 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 310 | |
1588ab5d | 311 | throttle_config_init(&cfg); |
f17cfe81 BC |
312 | set_cfg_value(is_max, total, 1); |
313 | set_cfg_value(is_max, write, 1); | |
d5851089 | 314 | g_assert(!throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 315 | |
1588ab5d | 316 | throttle_config_init(&cfg); |
f17cfe81 BC |
317 | set_cfg_value(is_max, total, 1); |
318 | set_cfg_value(is_max, read, 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 | 323 | set_cfg_value(is_max, total, 1); |
d5851089 | 324 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 | 325 | |
1588ab5d | 326 | throttle_config_init(&cfg); |
f17cfe81 BC |
327 | set_cfg_value(is_max, read, 1); |
328 | set_cfg_value(is_max, write, 1); | |
d5851089 | 329 | g_assert(throttle_is_valid(&cfg, NULL)); |
f17cfe81 BC |
330 | } |
331 | ||
332 | static void test_conflicting_config(void) | |
333 | { | |
334 | /* bps average conflicts */ | |
335 | test_conflicts_for_one_set(false, | |
336 | THROTTLE_BPS_TOTAL, | |
337 | THROTTLE_BPS_READ, | |
338 | THROTTLE_BPS_WRITE); | |
339 | ||
340 | /* ops average conflicts */ | |
341 | test_conflicts_for_one_set(false, | |
342 | THROTTLE_OPS_TOTAL, | |
343 | THROTTLE_OPS_READ, | |
344 | THROTTLE_OPS_WRITE); | |
345 | ||
346 | /* bps average conflicts */ | |
347 | test_conflicts_for_one_set(true, | |
348 | THROTTLE_BPS_TOTAL, | |
349 | THROTTLE_BPS_READ, | |
350 | THROTTLE_BPS_WRITE); | |
351 | /* ops average conflicts */ | |
352 | test_conflicts_for_one_set(true, | |
353 | THROTTLE_OPS_TOTAL, | |
354 | THROTTLE_OPS_READ, | |
355 | THROTTLE_OPS_WRITE); | |
356 | } | |
357 | /* functions to test the throttle_is_valid function */ | |
358 | static void test_is_valid_for_value(int value, bool should_be_valid) | |
359 | { | |
360 | int is_max, index; | |
361 | for (is_max = 0; is_max < 2; is_max++) { | |
362 | for (index = 0; index < BUCKETS_COUNT; index++) { | |
1588ab5d | 363 | throttle_config_init(&cfg); |
f17cfe81 | 364 | set_cfg_value(is_max, index, value); |
03ba36c8 | 365 | g_assert(throttle_is_valid(&cfg, NULL) == should_be_valid); |
f17cfe81 BC |
366 | } |
367 | } | |
368 | } | |
369 | ||
370 | static void test_is_valid(void) | |
371 | { | |
372 | /* negative number are invalid */ | |
373 | test_is_valid_for_value(-1, false); | |
374 | /* zero are valids */ | |
375 | test_is_valid_for_value(0, true); | |
376 | /* positives numers are valids */ | |
377 | test_is_valid_for_value(1, true); | |
378 | } | |
379 | ||
92e11a17 SH |
380 | static void test_max_is_missing_limit(void) |
381 | { | |
382 | int i; | |
383 | ||
384 | for (i = 0; i < BUCKETS_COUNT; i++) { | |
1588ab5d | 385 | throttle_config_init(&cfg); |
92e11a17 SH |
386 | cfg.buckets[i].max = 100; |
387 | cfg.buckets[i].avg = 0; | |
d5851089 | 388 | g_assert(!throttle_is_valid(&cfg, NULL)); |
92e11a17 SH |
389 | |
390 | cfg.buckets[i].max = 0; | |
391 | cfg.buckets[i].avg = 0; | |
d5851089 | 392 | g_assert(throttle_is_valid(&cfg, NULL)); |
92e11a17 SH |
393 | |
394 | cfg.buckets[i].max = 0; | |
395 | cfg.buckets[i].avg = 100; | |
d5851089 | 396 | g_assert(throttle_is_valid(&cfg, NULL)); |
92e11a17 SH |
397 | } |
398 | } | |
399 | ||
8860eabd SH |
400 | static void test_iops_size_is_missing_limit(void) |
401 | { | |
402 | /* A total/read/write iops limit is required */ | |
403 | throttle_config_init(&cfg); | |
404 | cfg.op_size = 4096; | |
405 | g_assert(!throttle_is_valid(&cfg, NULL)); | |
406 | } | |
407 | ||
f17cfe81 BC |
408 | static void test_have_timer(void) |
409 | { | |
0e5b0a2d | 410 | /* zero structures */ |
f17cfe81 | 411 | memset(&ts, 0, sizeof(ts)); |
0e5b0a2d | 412 | memset(&tt, 0, sizeof(tt)); |
f17cfe81 | 413 | |
73f395fa | 414 | /* no timer set should return false */ |
0e5b0a2d | 415 | g_assert(!throttle_timers_are_initialized(&tt)); |
f17cfe81 | 416 | |
0e5b0a2d BC |
417 | /* init structures */ |
418 | throttle_init(&ts); | |
419 | throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL, | |
420 | read_timer_cb, write_timer_cb, &ts); | |
f17cfe81 BC |
421 | |
422 | /* timer set by init should return true */ | |
0e5b0a2d | 423 | g_assert(throttle_timers_are_initialized(&tt)); |
f17cfe81 | 424 | |
0e5b0a2d | 425 | throttle_timers_destroy(&tt); |
f17cfe81 BC |
426 | } |
427 | ||
22524f72 SH |
428 | static void test_detach_attach(void) |
429 | { | |
0e5b0a2d | 430 | /* zero structures */ |
22524f72 | 431 | memset(&ts, 0, sizeof(ts)); |
0e5b0a2d | 432 | memset(&tt, 0, sizeof(tt)); |
22524f72 SH |
433 | |
434 | /* init the structure */ | |
0e5b0a2d BC |
435 | throttle_init(&ts); |
436 | throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL, | |
437 | read_timer_cb, write_timer_cb, &ts); | |
22524f72 SH |
438 | |
439 | /* timer set by init should return true */ | |
0e5b0a2d | 440 | g_assert(throttle_timers_are_initialized(&tt)); |
22524f72 SH |
441 | |
442 | /* timer should no longer exist after detaching */ | |
0e5b0a2d BC |
443 | throttle_timers_detach_aio_context(&tt); |
444 | g_assert(!throttle_timers_are_initialized(&tt)); | |
22524f72 SH |
445 | |
446 | /* timer should exist again after attaching */ | |
0e5b0a2d BC |
447 | throttle_timers_attach_aio_context(&tt, ctx); |
448 | g_assert(throttle_timers_are_initialized(&tt)); | |
22524f72 | 449 | |
0e5b0a2d | 450 | throttle_timers_destroy(&tt); |
22524f72 SH |
451 | } |
452 | ||
f17cfe81 BC |
453 | static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */ |
454 | int size, /* size of the operation to do */ | |
455 | double avg, /* io limit */ | |
456 | uint64_t op_size, /* ideal size of an io */ | |
457 | double total_result, | |
458 | double read_result, | |
459 | double write_result) | |
460 | { | |
461 | BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL, | |
462 | THROTTLE_BPS_READ, | |
463 | THROTTLE_BPS_WRITE, }, | |
464 | { THROTTLE_OPS_TOTAL, | |
465 | THROTTLE_OPS_READ, | |
466 | THROTTLE_OPS_WRITE, } }; | |
467 | ThrottleConfig cfg; | |
468 | BucketType index; | |
469 | int i; | |
470 | ||
471 | for (i = 0; i < 3; i++) { | |
472 | BucketType index = to_test[is_ops][i]; | |
473 | cfg.buckets[index].avg = avg; | |
474 | } | |
475 | ||
476 | cfg.op_size = op_size; | |
477 | ||
0e5b0a2d BC |
478 | throttle_init(&ts); |
479 | throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL, | |
480 | read_timer_cb, write_timer_cb, &ts); | |
481 | throttle_config(&ts, &tt, &cfg); | |
f17cfe81 BC |
482 | |
483 | /* account a read */ | |
484 | throttle_account(&ts, false, size); | |
485 | /* account a write */ | |
486 | throttle_account(&ts, true, size); | |
487 | ||
488 | /* check total result */ | |
489 | index = to_test[is_ops][0]; | |
490 | if (!double_cmp(ts.cfg.buckets[index].level, total_result)) { | |
491 | return false; | |
492 | } | |
493 | ||
494 | /* check read result */ | |
495 | index = to_test[is_ops][1]; | |
496 | if (!double_cmp(ts.cfg.buckets[index].level, read_result)) { | |
497 | return false; | |
498 | } | |
499 | ||
500 | /* check write result */ | |
501 | index = to_test[is_ops][2]; | |
502 | if (!double_cmp(ts.cfg.buckets[index].level, write_result)) { | |
503 | return false; | |
504 | } | |
505 | ||
0e5b0a2d | 506 | throttle_timers_destroy(&tt); |
f17cfe81 BC |
507 | |
508 | return true; | |
509 | } | |
510 | ||
511 | static void test_accounting(void) | |
512 | { | |
513 | /* tests for bps */ | |
514 | ||
515 | /* op of size 1 */ | |
516 | g_assert(do_test_accounting(false, | |
517 | 1 * 512, | |
518 | 150, | |
519 | 0, | |
520 | 1024, | |
521 | 512, | |
522 | 512)); | |
523 | ||
524 | /* op of size 2 */ | |
525 | g_assert(do_test_accounting(false, | |
526 | 2 * 512, | |
527 | 150, | |
528 | 0, | |
529 | 2048, | |
530 | 1024, | |
531 | 1024)); | |
532 | ||
533 | /* op of size 2 and orthogonal parameter change */ | |
534 | g_assert(do_test_accounting(false, | |
535 | 2 * 512, | |
536 | 150, | |
537 | 17, | |
538 | 2048, | |
539 | 1024, | |
540 | 1024)); | |
541 | ||
542 | ||
543 | /* tests for ops */ | |
544 | ||
545 | /* op of size 1 */ | |
546 | g_assert(do_test_accounting(true, | |
547 | 1 * 512, | |
548 | 150, | |
549 | 0, | |
550 | 2, | |
551 | 1, | |
552 | 1)); | |
553 | ||
554 | /* op of size 2 */ | |
555 | g_assert(do_test_accounting(true, | |
556 | 2 * 512, | |
557 | 150, | |
558 | 0, | |
559 | 2, | |
560 | 1, | |
561 | 1)); | |
562 | ||
563 | /* jumbo op accounting fragmentation : size 64 with op size of 13 units */ | |
564 | g_assert(do_test_accounting(true, | |
565 | 64 * 512, | |
566 | 150, | |
567 | 13 * 512, | |
568 | (64.0 * 2) / 13, | |
569 | (64.0 / 13), | |
570 | (64.0 / 13))); | |
571 | ||
572 | /* same with orthogonal parameters changes */ | |
573 | g_assert(do_test_accounting(true, | |
574 | 64 * 512, | |
575 | 300, | |
576 | 13 * 512, | |
577 | (64.0 * 2) / 13, | |
578 | (64.0 / 13), | |
579 | (64.0 / 13))); | |
580 | } | |
581 | ||
1fee955f AG |
582 | static void test_groups(void) |
583 | { | |
584 | ThrottleConfig cfg1, cfg2; | |
a5614993 | 585 | BlockBackend *blk1, *blk2, *blk3; |
27ccdd52 | 586 | BlockBackendPublic *blkp1, *blkp2, *blkp3; |
1fee955f | 587 | |
109525ad HR |
588 | blk1 = blk_new(); |
589 | blk2 = blk_new(); | |
590 | blk3 = blk_new(); | |
a5614993 | 591 | |
27ccdd52 KW |
592 | blkp1 = blk_get_public(blk1); |
593 | blkp2 = blk_get_public(blk2); | |
594 | blkp3 = blk_get_public(blk3); | |
595 | ||
596 | g_assert(blkp1->throttle_state == NULL); | |
597 | g_assert(blkp2->throttle_state == NULL); | |
598 | g_assert(blkp3->throttle_state == NULL); | |
1fee955f | 599 | |
31dce3cc KW |
600 | throttle_group_register_blk(blk1, "bar"); |
601 | throttle_group_register_blk(blk2, "foo"); | |
602 | throttle_group_register_blk(blk3, "bar"); | |
1fee955f | 603 | |
27ccdd52 KW |
604 | g_assert(blkp1->throttle_state != NULL); |
605 | g_assert(blkp2->throttle_state != NULL); | |
606 | g_assert(blkp3->throttle_state != NULL); | |
1fee955f | 607 | |
49d2165d KW |
608 | g_assert(!strcmp(throttle_group_get_name(blk1), "bar")); |
609 | g_assert(!strcmp(throttle_group_get_name(blk2), "foo")); | |
27ccdd52 | 610 | g_assert(blkp1->throttle_state == blkp3->throttle_state); |
1fee955f AG |
611 | |
612 | /* Setting the config of a group member affects the whole group */ | |
1588ab5d | 613 | throttle_config_init(&cfg1); |
1fee955f AG |
614 | cfg1.buckets[THROTTLE_BPS_READ].avg = 500000; |
615 | cfg1.buckets[THROTTLE_BPS_WRITE].avg = 285000; | |
616 | cfg1.buckets[THROTTLE_OPS_READ].avg = 20000; | |
617 | cfg1.buckets[THROTTLE_OPS_WRITE].avg = 12000; | |
97148076 | 618 | throttle_group_config(blk1, &cfg1); |
1fee955f | 619 | |
97148076 KW |
620 | throttle_group_get_config(blk1, &cfg1); |
621 | throttle_group_get_config(blk3, &cfg2); | |
1fee955f AG |
622 | g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1))); |
623 | ||
624 | cfg2.buckets[THROTTLE_BPS_READ].avg = 4547; | |
625 | cfg2.buckets[THROTTLE_BPS_WRITE].avg = 1349; | |
626 | cfg2.buckets[THROTTLE_OPS_READ].avg = 123; | |
627 | cfg2.buckets[THROTTLE_OPS_WRITE].avg = 86; | |
97148076 | 628 | throttle_group_config(blk3, &cfg1); |
1fee955f | 629 | |
97148076 KW |
630 | throttle_group_get_config(blk1, &cfg1); |
631 | throttle_group_get_config(blk3, &cfg2); | |
1fee955f AG |
632 | g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1))); |
633 | ||
31dce3cc KW |
634 | throttle_group_unregister_blk(blk1); |
635 | throttle_group_unregister_blk(blk2); | |
636 | throttle_group_unregister_blk(blk3); | |
1fee955f | 637 | |
27ccdd52 KW |
638 | g_assert(blkp1->throttle_state == NULL); |
639 | g_assert(blkp2->throttle_state == NULL); | |
640 | g_assert(blkp3->throttle_state == NULL); | |
1fee955f AG |
641 | } |
642 | ||
f17cfe81 BC |
643 | int main(int argc, char **argv) |
644 | { | |
73eaa047 | 645 | qemu_init_main_loop(&error_fatal); |
1fee955f | 646 | ctx = qemu_get_aio_context(); |
1fee955f | 647 | bdrv_init(); |
13af91eb | 648 | |
f17cfe81 BC |
649 | do {} while (g_main_context_iteration(NULL, false)); |
650 | ||
651 | /* tests in the same order as the header function declarations */ | |
652 | g_test_init(&argc, &argv, NULL); | |
653 | g_test_add_func("/throttle/leak_bucket", test_leak_bucket); | |
654 | g_test_add_func("/throttle/compute_wait", test_compute_wait); | |
655 | g_test_add_func("/throttle/init", test_init); | |
656 | g_test_add_func("/throttle/destroy", test_destroy); | |
657 | g_test_add_func("/throttle/have_timer", test_have_timer); | |
22524f72 | 658 | g_test_add_func("/throttle/detach_attach", test_detach_attach); |
f17cfe81 BC |
659 | g_test_add_func("/throttle/config/enabled", test_enabled); |
660 | g_test_add_func("/throttle/config/conflicting", test_conflicting_config); | |
661 | g_test_add_func("/throttle/config/is_valid", test_is_valid); | |
92e11a17 | 662 | g_test_add_func("/throttle/config/max", test_max_is_missing_limit); |
8860eabd SH |
663 | g_test_add_func("/throttle/config/iops_size", |
664 | test_iops_size_is_missing_limit); | |
f17cfe81 BC |
665 | g_test_add_func("/throttle/config_functions", test_config_functions); |
666 | g_test_add_func("/throttle/accounting", test_accounting); | |
1fee955f | 667 | g_test_add_func("/throttle/groups", test_groups); |
f17cfe81 BC |
668 | return g_test_run(); |
669 | } | |
670 |