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