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