blob: 7adb5e6652c94bda8238e520f72fb82c0a8afc24 [file] [log] [blame]
Benoît Canetf17cfe82013-09-02 14:14:38 +02001/*
2 * Throttle infrastructure tests
3 *
Alberto Garcia1fee9552015-06-08 18:17:43 +02004 * Copyright Nodalink, EURL. 2013-2014
5 * Copyright Igalia, S.L. 2015
Benoît Canetf17cfe82013-09-02 14:14:38 +02006 *
7 * Authors:
Alberto Garcia1fee9552015-06-08 18:17:43 +02008 * Benoît Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
Benoît Canetf17cfe82013-09-02 14:14:38 +020010 *
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
Peter Maydell681c28a2016-02-08 18:08:51 +000015#include "qemu/osdep.h"
Benoît Canetf17cfe82013-09-02 14:14:38 +020016#include <math.h>
Stefan Hajnoczi13af91e2014-05-14 16:22:45 +020017#include "block/aio.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Benoît Canetf17cfe82013-09-02 14:14:38 +020019#include "qemu/throttle.h"
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030020#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020021#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020022#include "qemu/module.h"
Alberto Garcia1fee9552015-06-08 18:17:43 +020023#include "block/throttle-groups.h"
Kevin Wolf31dce3c2016-03-21 11:30:57 +010024#include "sysemu/block-backend.h"
Benoît Canetf17cfe82013-09-02 14:14:38 +020025
Stefan Weil748bfb42014-07-07 21:03:38 +020026static AioContext *ctx;
27static LeakyBucket bkt;
28static ThrottleConfig cfg;
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +030029static ThrottleGroupMember tgm;
Stefan Weil748bfb42014-07-07 21:03:38 +020030static ThrottleState ts;
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +030031static ThrottleTimers *tt;
Benoît Canetf17cfe82013-09-02 14:14:38 +020032
Stefan Weil73f395f2013-10-10 20:53:40 +020033/* useful function */
Benoît Canetf17cfe82013-09-02 14:14:38 +020034static bool double_cmp(double x, double y)
35{
36 return fabsl(x - y) < 1e-6;
37}
38
39/* tests for single bucket operations */
40static void test_leak_bucket(void)
41{
Alberto Garcia1588ab52016-02-18 12:27:00 +020042 throttle_config_init(&cfg);
43 bkt = cfg.buckets[THROTTLE_BPS_TOTAL];
44
Benoît Canetf17cfe82013-09-02 14:14:38 +020045 /* set initial value */
46 bkt.avg = 150;
47 bkt.max = 15;
48 bkt.level = 1.5;
49
50 /* leak an op work of time */
Stefan Hajnoczi13566fe2015-07-08 15:10:09 +010051 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
Benoît Canetf17cfe82013-09-02 14:14:38 +020052 g_assert(bkt.avg == 150);
53 g_assert(bkt.max == 15);
54 g_assert(double_cmp(bkt.level, 0.5));
55
56 /* leak again emptying the bucket */
Stefan Hajnoczi13566fe2015-07-08 15:10:09 +010057 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
Benoît Canetf17cfe82013-09-02 14:14:38 +020058 g_assert(bkt.avg == 150);
59 g_assert(bkt.max == 15);
60 g_assert(double_cmp(bkt.level, 0));
61
62 /* check that the bucket level won't go lower */
Stefan Hajnoczi13566fe2015-07-08 15:10:09 +010063 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 150);
Benoît Canetf17cfe82013-09-02 14:14:38 +020064 g_assert(bkt.avg == 150);
65 g_assert(bkt.max == 15);
66 g_assert(double_cmp(bkt.level, 0));
Alberto Garciaeb8a1a12016-02-18 12:27:05 +020067
68 /* check that burst_level leaks correctly */
69 bkt.burst_level = 6;
70 bkt.max = 250;
71 bkt.burst_length = 2; /* otherwise burst_level will not leak */
72 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
73 g_assert(double_cmp(bkt.burst_level, 3.5));
74
75 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
76 g_assert(double_cmp(bkt.burst_level, 1));
77
78 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
79 g_assert(double_cmp(bkt.burst_level, 0));
80
81 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 100);
82 g_assert(double_cmp(bkt.burst_level, 0));
Benoît Canetf17cfe82013-09-02 14:14:38 +020083}
84
85static void test_compute_wait(void)
86{
Alberto Garciaf9d05882016-02-18 12:27:06 +020087 unsigned i;
Benoît Canetf17cfe82013-09-02 14:14:38 +020088 int64_t wait;
89 int64_t result;
90
Alberto Garcia1588ab52016-02-18 12:27:00 +020091 throttle_config_init(&cfg);
92 bkt = cfg.buckets[THROTTLE_BPS_TOTAL];
93
Benoît Canetf17cfe82013-09-02 14:14:38 +020094 /* no operation limit set */
95 bkt.avg = 0;
96 bkt.max = 15;
97 bkt.level = 1.5;
98 wait = throttle_compute_wait(&bkt);
99 g_assert(!wait);
100
101 /* zero delta */
102 bkt.avg = 150;
103 bkt.max = 15;
104 bkt.level = 15;
105 wait = throttle_compute_wait(&bkt);
106 g_assert(!wait);
107
108 /* below zero delta */
109 bkt.avg = 150;
110 bkt.max = 15;
111 bkt.level = 9;
112 wait = throttle_compute_wait(&bkt);
113 g_assert(!wait);
114
115 /* half an operation above max */
116 bkt.avg = 150;
117 bkt.max = 15;
118 bkt.level = 15.5;
119 wait = throttle_compute_wait(&bkt);
120 /* time required to do half an operation */
Stefan Hajnoczi13566fe2015-07-08 15:10:09 +0100121 result = (int64_t) NANOSECONDS_PER_SECOND / 150 / 2;
Benoît Canetf17cfe82013-09-02 14:14:38 +0200122 g_assert(wait == result);
Alberto Garciaf9d05882016-02-18 12:27:06 +0200123
124 /* Perform I/O for 2.2 seconds at a rate of bkt.max */
125 bkt.burst_length = 2;
126 bkt.level = 0;
127 bkt.avg = 10;
128 bkt.max = 200;
129 for (i = 0; i < 22; i++) {
130 double units = bkt.max / 10;
131 bkt.level += units;
132 bkt.burst_level += units;
133 throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 10);
134 wait = throttle_compute_wait(&bkt);
135 g_assert(double_cmp(bkt.burst_level, 0));
136 g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10));
137 /* We can do bursts for the 2 seconds we have configured in
138 * burst_length. We have 100 extra miliseconds of burst
139 * because bkt.level has been leaking during this time.
140 * After that, we have to wait. */
141 result = i < 21 ? 0 : 1.8 * NANOSECONDS_PER_SECOND;
142 g_assert(wait == result);
143 }
Benoît Canetf17cfe82013-09-02 14:14:38 +0200144}
145
146/* functions to test ThrottleState initialization/destroy methods */
147static void read_timer_cb(void *opaque)
148{
149}
150
151static void write_timer_cb(void *opaque)
152{
153}
154
155static void test_init(void)
156{
157 int i;
158
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300159 tt = &tgm.throttle_timers;
160
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200161 /* fill the structures with crap */
Benoît Canetf17cfe82013-09-02 14:14:38 +0200162 memset(&ts, 1, sizeof(ts));
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300163 memset(tt, 1, sizeof(*tt));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200164
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200165 /* init structures */
166 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300167 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200168 read_timer_cb, write_timer_cb, &ts);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200169
170 /* check initialized fields */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300171 g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL);
172 g_assert(tt->timers[0]);
173 g_assert(tt->timers[1]);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200174
175 /* check other fields where cleared */
176 g_assert(!ts.previous_leak);
177 g_assert(!ts.cfg.op_size);
178 for (i = 0; i < BUCKETS_COUNT; i++) {
179 g_assert(!ts.cfg.buckets[i].avg);
180 g_assert(!ts.cfg.buckets[i].max);
181 g_assert(!ts.cfg.buckets[i].level);
182 }
183
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300184 throttle_timers_destroy(tt);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200185}
186
187static void test_destroy(void)
188{
189 int i;
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200190 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300191 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200192 read_timer_cb, write_timer_cb, &ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300193 throttle_timers_destroy(tt);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200194 for (i = 0; i < 2; i++) {
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300195 g_assert(!tt->timers[i]);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200196 }
197}
198
199/* function to test throttle_config and throttle_get_config */
200static void test_config_functions(void)
201{
202 int i;
203 ThrottleConfig orig_cfg, final_cfg;
204
205 orig_cfg.buckets[THROTTLE_BPS_TOTAL].avg = 153;
206 orig_cfg.buckets[THROTTLE_BPS_READ].avg = 56;
207 orig_cfg.buckets[THROTTLE_BPS_WRITE].avg = 1;
208
209 orig_cfg.buckets[THROTTLE_OPS_TOTAL].avg = 150;
210 orig_cfg.buckets[THROTTLE_OPS_READ].avg = 69;
211 orig_cfg.buckets[THROTTLE_OPS_WRITE].avg = 23;
212
Stefan Hajnoczid72915c2017-03-01 11:50:26 +0000213 orig_cfg.buckets[THROTTLE_BPS_TOTAL].max = 0;
214 orig_cfg.buckets[THROTTLE_BPS_READ].max = 56;
Benoît Canetf17cfe82013-09-02 14:14:38 +0200215 orig_cfg.buckets[THROTTLE_BPS_WRITE].max = 120;
216
217 orig_cfg.buckets[THROTTLE_OPS_TOTAL].max = 150;
218 orig_cfg.buckets[THROTTLE_OPS_READ].max = 400;
219 orig_cfg.buckets[THROTTLE_OPS_WRITE].max = 500;
220
221 orig_cfg.buckets[THROTTLE_BPS_TOTAL].level = 45;
222 orig_cfg.buckets[THROTTLE_BPS_READ].level = 65;
223 orig_cfg.buckets[THROTTLE_BPS_WRITE].level = 23;
224
225 orig_cfg.buckets[THROTTLE_OPS_TOTAL].level = 1;
226 orig_cfg.buckets[THROTTLE_OPS_READ].level = 90;
227 orig_cfg.buckets[THROTTLE_OPS_WRITE].level = 75;
228
229 orig_cfg.op_size = 1;
230
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200231 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300232 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200233 read_timer_cb, write_timer_cb, &ts);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200234 /* structure reset by throttle_init previous_leak should be null */
235 g_assert(!ts.previous_leak);
Manos Pitsidianakis27e4cf12017-07-02 13:06:46 +0300236 throttle_config(&ts, QEMU_CLOCK_VIRTUAL, &orig_cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200237
238 /* has previous leak been initialized by throttle_config ? */
239 g_assert(ts.previous_leak);
240
241 /* get back the fixed configuration */
242 throttle_get_config(&ts, &final_cfg);
243
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300244 throttle_timers_destroy(tt);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200245
246 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153);
247 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg == 56);
248 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].avg == 1);
249
250 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].avg == 150);
251 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].avg == 69);
252 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].avg == 23);
253
Stefan Hajnoczid72915c2017-03-01 11:50:26 +0000254 g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].max == 0);
255 g_assert(final_cfg.buckets[THROTTLE_BPS_READ].max == 56);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200256 g_assert(final_cfg.buckets[THROTTLE_BPS_WRITE].max == 120);
257
258 g_assert(final_cfg.buckets[THROTTLE_OPS_TOTAL].max == 150);
259 g_assert(final_cfg.buckets[THROTTLE_OPS_READ].max == 400);
260 g_assert(final_cfg.buckets[THROTTLE_OPS_WRITE].max == 500);
261
262 g_assert(final_cfg.op_size == 1);
263
264 /* check bucket have been cleared */
265 for (i = 0; i < BUCKETS_COUNT; i++) {
266 g_assert(!final_cfg.buckets[i].level);
267 }
268}
269
270/* functions to test is throttle is enabled by a config */
271static void set_cfg_value(bool is_max, int index, int value)
272{
273 if (is_max) {
274 cfg.buckets[index].max = value;
Alberto Garcia6f9b6d52016-02-18 12:26:58 +0200275 /* If max is set, avg should never be 0 */
276 cfg.buckets[index].avg = MAX(cfg.buckets[index].avg, 1);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200277 } else {
278 cfg.buckets[index].avg = value;
279 }
280}
281
282static void test_enabled(void)
283{
284 int i;
285
Alberto Garcia1588ab52016-02-18 12:27:00 +0200286 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200287 g_assert(!throttle_enabled(&cfg));
288
289 for (i = 0; i < BUCKETS_COUNT; i++) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200290 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200291 set_cfg_value(false, i, 150);
Alberto Garciad00e6922017-08-24 16:24:47 +0300292 g_assert(throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200293 g_assert(throttle_enabled(&cfg));
294 }
295
296 for (i = 0; i < BUCKETS_COUNT; i++) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200297 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200298 set_cfg_value(false, i, -150);
Alberto Garciad00e6922017-08-24 16:24:47 +0300299 g_assert(!throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200300 }
301}
302
303/* tests functions for throttle_conflicting */
304
305static void test_conflicts_for_one_set(bool is_max,
306 int total,
307 int read,
308 int write)
309{
Alberto Garcia1588ab52016-02-18 12:27:00 +0200310 throttle_config_init(&cfg);
Alberto Garciad5851082016-02-18 12:26:59 +0200311 g_assert(throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200312
313 set_cfg_value(is_max, total, 1);
314 set_cfg_value(is_max, read, 1);
Alberto Garciad5851082016-02-18 12:26:59 +0200315 g_assert(!throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200316
Alberto Garcia1588ab52016-02-18 12:27:00 +0200317 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200318 set_cfg_value(is_max, total, 1);
319 set_cfg_value(is_max, write, 1);
Alberto Garciad5851082016-02-18 12:26:59 +0200320 g_assert(!throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200321
Alberto Garcia1588ab52016-02-18 12:27:00 +0200322 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200323 set_cfg_value(is_max, total, 1);
324 set_cfg_value(is_max, read, 1);
325 set_cfg_value(is_max, write, 1);
Alberto Garciad5851082016-02-18 12:26:59 +0200326 g_assert(!throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200327
Alberto Garcia1588ab52016-02-18 12:27:00 +0200328 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200329 set_cfg_value(is_max, total, 1);
Alberto Garciad5851082016-02-18 12:26:59 +0200330 g_assert(throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200331
Alberto Garcia1588ab52016-02-18 12:27:00 +0200332 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200333 set_cfg_value(is_max, read, 1);
334 set_cfg_value(is_max, write, 1);
Alberto Garciad5851082016-02-18 12:26:59 +0200335 g_assert(throttle_is_valid(&cfg, NULL));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200336}
337
338static void test_conflicting_config(void)
339{
340 /* bps average conflicts */
341 test_conflicts_for_one_set(false,
342 THROTTLE_BPS_TOTAL,
343 THROTTLE_BPS_READ,
344 THROTTLE_BPS_WRITE);
345
346 /* ops average conflicts */
347 test_conflicts_for_one_set(false,
348 THROTTLE_OPS_TOTAL,
349 THROTTLE_OPS_READ,
350 THROTTLE_OPS_WRITE);
351
352 /* bps average conflicts */
353 test_conflicts_for_one_set(true,
354 THROTTLE_BPS_TOTAL,
355 THROTTLE_BPS_READ,
356 THROTTLE_BPS_WRITE);
357 /* ops average conflicts */
358 test_conflicts_for_one_set(true,
359 THROTTLE_OPS_TOTAL,
360 THROTTLE_OPS_READ,
361 THROTTLE_OPS_WRITE);
362}
363/* functions to test the throttle_is_valid function */
364static void test_is_valid_for_value(int value, bool should_be_valid)
365{
366 int is_max, index;
367 for (is_max = 0; is_max < 2; is_max++) {
368 for (index = 0; index < BUCKETS_COUNT; index++) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200369 throttle_config_init(&cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200370 set_cfg_value(is_max, index, value);
Alberto Garcia03ba36c2016-02-18 12:26:57 +0200371 g_assert(throttle_is_valid(&cfg, NULL) == should_be_valid);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200372 }
373 }
374}
375
376static void test_is_valid(void)
377{
378 /* negative number are invalid */
379 test_is_valid_for_value(-1, false);
380 /* zero are valids */
381 test_is_valid_for_value(0, true);
382 /* positives numers are valids */
383 test_is_valid_for_value(1, true);
384}
385
Alberto Garciad942fee2017-08-24 16:24:49 +0300386static void test_ranges(void)
387{
388 int i;
389
390 for (i = 0; i < BUCKETS_COUNT; i++) {
391 LeakyBucket *b = &cfg.buckets[i];
392 throttle_config_init(&cfg);
393
394 /* avg = 0 means throttling is disabled, but the config is valid */
395 b->avg = 0;
396 g_assert(throttle_is_valid(&cfg, NULL));
397 g_assert(!throttle_enabled(&cfg));
398
399 /* These are valid configurations (values <= THROTTLE_VALUE_MAX) */
400 b->avg = 1;
401 g_assert(throttle_is_valid(&cfg, NULL));
402
403 b->avg = THROTTLE_VALUE_MAX;
404 g_assert(throttle_is_valid(&cfg, NULL));
405
406 b->avg = THROTTLE_VALUE_MAX;
407 b->max = THROTTLE_VALUE_MAX;
408 g_assert(throttle_is_valid(&cfg, NULL));
409
410 /* Values over THROTTLE_VALUE_MAX are not allowed */
411 b->avg = THROTTLE_VALUE_MAX + 1;
412 g_assert(!throttle_is_valid(&cfg, NULL));
413
414 b->avg = THROTTLE_VALUE_MAX;
415 b->max = THROTTLE_VALUE_MAX + 1;
416 g_assert(!throttle_is_valid(&cfg, NULL));
417
418 /* burst_length must be between 1 and THROTTLE_VALUE_MAX */
419 b->avg = 1;
420 b->max = 1;
421 b->burst_length = 0;
422 g_assert(!throttle_is_valid(&cfg, NULL));
423
424 b->avg = 1;
425 b->max = 1;
426 b->burst_length = 1;
427 g_assert(throttle_is_valid(&cfg, NULL));
428
429 b->avg = 1;
430 b->max = 1;
431 b->burst_length = THROTTLE_VALUE_MAX;
432 g_assert(throttle_is_valid(&cfg, NULL));
433
434 b->avg = 1;
435 b->max = 1;
436 b->burst_length = THROTTLE_VALUE_MAX + 1;
437 g_assert(!throttle_is_valid(&cfg, NULL));
438
439 /* burst_length * max cannot exceed THROTTLE_VALUE_MAX */
440 b->avg = 1;
441 b->max = 2;
442 b->burst_length = THROTTLE_VALUE_MAX / 2;
443 g_assert(throttle_is_valid(&cfg, NULL));
444
445 b->avg = 1;
446 b->max = 3;
447 b->burst_length = THROTTLE_VALUE_MAX / 2;
448 g_assert(!throttle_is_valid(&cfg, NULL));
449
450 b->avg = 1;
451 b->max = THROTTLE_VALUE_MAX;
452 b->burst_length = 1;
453 g_assert(throttle_is_valid(&cfg, NULL));
454
455 b->avg = 1;
456 b->max = THROTTLE_VALUE_MAX;
457 b->burst_length = 2;
458 g_assert(!throttle_is_valid(&cfg, NULL));
459 }
460}
461
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100462static void test_max_is_missing_limit(void)
463{
464 int i;
465
466 for (i = 0; i < BUCKETS_COUNT; i++) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200467 throttle_config_init(&cfg);
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100468 cfg.buckets[i].max = 100;
469 cfg.buckets[i].avg = 0;
Alberto Garciad5851082016-02-18 12:26:59 +0200470 g_assert(!throttle_is_valid(&cfg, NULL));
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100471
472 cfg.buckets[i].max = 0;
473 cfg.buckets[i].avg = 0;
Alberto Garciad5851082016-02-18 12:26:59 +0200474 g_assert(throttle_is_valid(&cfg, NULL));
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100475
476 cfg.buckets[i].max = 0;
477 cfg.buckets[i].avg = 100;
Alberto Garciad5851082016-02-18 12:26:59 +0200478 g_assert(throttle_is_valid(&cfg, NULL));
Alberto Garcia5fc8c052016-07-28 11:08:13 +0300479
480 cfg.buckets[i].max = 30;
481 cfg.buckets[i].avg = 100;
482 g_assert(!throttle_is_valid(&cfg, NULL));
483
484 cfg.buckets[i].max = 100;
485 cfg.buckets[i].avg = 100;
486 g_assert(throttle_is_valid(&cfg, NULL));
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100487 }
488}
489
Stefan Hajnoczi8860eab2016-06-01 17:40:31 -0700490static void test_iops_size_is_missing_limit(void)
491{
492 /* A total/read/write iops limit is required */
493 throttle_config_init(&cfg);
494 cfg.op_size = 4096;
495 g_assert(!throttle_is_valid(&cfg, NULL));
496}
497
Benoît Canetf17cfe82013-09-02 14:14:38 +0200498static void test_have_timer(void)
499{
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200500 /* zero structures */
Benoît Canetf17cfe82013-09-02 14:14:38 +0200501 memset(&ts, 0, sizeof(ts));
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300502 memset(tt, 0, sizeof(*tt));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200503
Stefan Weil73f395f2013-10-10 20:53:40 +0200504 /* no timer set should return false */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300505 g_assert(!throttle_timers_are_initialized(tt));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200506
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200507 /* init structures */
508 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300509 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200510 read_timer_cb, write_timer_cb, &ts);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200511
512 /* timer set by init should return true */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300513 g_assert(throttle_timers_are_initialized(tt));
Benoît Canetf17cfe82013-09-02 14:14:38 +0200514
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300515 throttle_timers_destroy(tt);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200516}
517
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200518static void test_detach_attach(void)
519{
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200520 /* zero structures */
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200521 memset(&ts, 0, sizeof(ts));
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300522 memset(tt, 0, sizeof(*tt));
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200523
524 /* init the structure */
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200525 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300526 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200527 read_timer_cb, write_timer_cb, &ts);
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200528
529 /* timer set by init should return true */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300530 g_assert(throttle_timers_are_initialized(tt));
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200531
532 /* timer should no longer exist after detaching */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300533 throttle_timers_detach_aio_context(tt);
534 g_assert(!throttle_timers_are_initialized(tt));
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200535
536 /* timer should exist again after attaching */
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300537 throttle_timers_attach_aio_context(tt, ctx);
538 g_assert(throttle_timers_are_initialized(tt));
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200539
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300540 throttle_timers_destroy(tt);
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200541}
542
Benoît Canetf17cfe82013-09-02 14:14:38 +0200543static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
544 int size, /* size of the operation to do */
545 double avg, /* io limit */
546 uint64_t op_size, /* ideal size of an io */
547 double total_result,
548 double read_result,
549 double write_result)
550{
551 BucketType to_test[2][3] = { { THROTTLE_BPS_TOTAL,
552 THROTTLE_BPS_READ,
553 THROTTLE_BPS_WRITE, },
554 { THROTTLE_OPS_TOTAL,
555 THROTTLE_OPS_READ,
556 THROTTLE_OPS_WRITE, } };
557 ThrottleConfig cfg;
558 BucketType index;
559 int i;
560
Andrey Shinkevichef0aa6a2019-07-30 19:01:36 +0300561 throttle_config_init(&cfg);
562
Benoît Canetf17cfe82013-09-02 14:14:38 +0200563 for (i = 0; i < 3; i++) {
564 BucketType index = to_test[is_ops][i];
565 cfg.buckets[index].avg = avg;
566 }
567
568 cfg.op_size = op_size;
569
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200570 throttle_init(&ts);
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300571 throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
Benoît Canet0e5b0a22015-06-08 18:17:41 +0200572 read_timer_cb, write_timer_cb, &ts);
Manos Pitsidianakis27e4cf12017-07-02 13:06:46 +0300573 throttle_config(&ts, QEMU_CLOCK_VIRTUAL, &cfg);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200574
575 /* account a read */
576 throttle_account(&ts, false, size);
577 /* account a write */
578 throttle_account(&ts, true, size);
579
580 /* check total result */
581 index = to_test[is_ops][0];
582 if (!double_cmp(ts.cfg.buckets[index].level, total_result)) {
583 return false;
584 }
585
586 /* check read result */
587 index = to_test[is_ops][1];
588 if (!double_cmp(ts.cfg.buckets[index].level, read_result)) {
589 return false;
590 }
591
592 /* check write result */
593 index = to_test[is_ops][2];
594 if (!double_cmp(ts.cfg.buckets[index].level, write_result)) {
595 return false;
596 }
597
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300598 throttle_timers_destroy(tt);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200599
600 return true;
601}
602
603static void test_accounting(void)
604{
605 /* tests for bps */
606
607 /* op of size 1 */
608 g_assert(do_test_accounting(false,
609 1 * 512,
610 150,
611 0,
612 1024,
613 512,
614 512));
615
616 /* op of size 2 */
617 g_assert(do_test_accounting(false,
618 2 * 512,
619 150,
620 0,
621 2048,
622 1024,
623 1024));
624
625 /* op of size 2 and orthogonal parameter change */
626 g_assert(do_test_accounting(false,
627 2 * 512,
628 150,
629 17,
630 2048,
631 1024,
632 1024));
633
634
635 /* tests for ops */
636
637 /* op of size 1 */
638 g_assert(do_test_accounting(true,
639 1 * 512,
640 150,
641 0,
642 2,
643 1,
644 1));
645
646 /* op of size 2 */
647 g_assert(do_test_accounting(true,
648 2 * 512,
649 150,
650 0,
651 2,
652 1,
653 1));
654
655 /* jumbo op accounting fragmentation : size 64 with op size of 13 units */
656 g_assert(do_test_accounting(true,
657 64 * 512,
658 150,
659 13 * 512,
660 (64.0 * 2) / 13,
661 (64.0 / 13),
662 (64.0 / 13)));
663
664 /* same with orthogonal parameters changes */
665 g_assert(do_test_accounting(true,
666 64 * 512,
667 300,
668 13 * 512,
669 (64.0 * 2) / 13,
670 (64.0 / 13),
671 (64.0 / 13)));
672}
673
Alberto Garcia1fee9552015-06-08 18:17:43 +0200674static void test_groups(void)
675{
676 ThrottleConfig cfg1, cfg2;
Kevin Wolfa5614992016-03-21 10:49:51 +0100677 BlockBackend *blk1, *blk2, *blk3;
Kevin Wolf27ccdd52016-03-21 12:56:44 +0100678 BlockBackendPublic *blkp1, *blkp2, *blkp3;
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300679 ThrottleGroupMember *tgm1, *tgm2, *tgm3;
Alberto Garcia1fee9552015-06-08 18:17:43 +0200680
Kevin Wolf2807c0c2017-02-09 15:48:04 +0100681 /* No actual I/O is performed on these devices */
Kevin Wolfd861ab32019-04-25 14:25:10 +0200682 blk1 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
683 blk2 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
684 blk3 = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
Kevin Wolfa5614992016-03-21 10:49:51 +0100685
Kevin Wolf27ccdd52016-03-21 12:56:44 +0100686 blkp1 = blk_get_public(blk1);
687 blkp2 = blk_get_public(blk2);
688 blkp3 = blk_get_public(blk3);
689
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300690 tgm1 = &blkp1->throttle_group_member;
691 tgm2 = &blkp2->throttle_group_member;
692 tgm3 = &blkp3->throttle_group_member;
Alberto Garcia1fee9552015-06-08 18:17:43 +0200693
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300694 g_assert(tgm1->throttle_state == NULL);
695 g_assert(tgm2->throttle_state == NULL);
696 g_assert(tgm3->throttle_state == NULL);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200697
Manos Pitsidianakisc61791f2017-08-25 16:20:24 +0300698 throttle_group_register_tgm(tgm1, "bar", blk_get_aio_context(blk1));
699 throttle_group_register_tgm(tgm2, "foo", blk_get_aio_context(blk2));
700 throttle_group_register_tgm(tgm3, "bar", blk_get_aio_context(blk3));
Alberto Garcia1fee9552015-06-08 18:17:43 +0200701
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300702 g_assert(tgm1->throttle_state != NULL);
703 g_assert(tgm2->throttle_state != NULL);
704 g_assert(tgm3->throttle_state != NULL);
705
706 g_assert(!strcmp(throttle_group_get_name(tgm1), "bar"));
707 g_assert(!strcmp(throttle_group_get_name(tgm2), "foo"));
708 g_assert(tgm1->throttle_state == tgm3->throttle_state);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200709
710 /* Setting the config of a group member affects the whole group */
Alberto Garcia1588ab52016-02-18 12:27:00 +0200711 throttle_config_init(&cfg1);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200712 cfg1.buckets[THROTTLE_BPS_READ].avg = 500000;
713 cfg1.buckets[THROTTLE_BPS_WRITE].avg = 285000;
714 cfg1.buckets[THROTTLE_OPS_READ].avg = 20000;
715 cfg1.buckets[THROTTLE_OPS_WRITE].avg = 12000;
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300716 throttle_group_config(tgm1, &cfg1);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200717
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300718 throttle_group_get_config(tgm1, &cfg1);
719 throttle_group_get_config(tgm3, &cfg2);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200720 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
721
722 cfg2.buckets[THROTTLE_BPS_READ].avg = 4547;
723 cfg2.buckets[THROTTLE_BPS_WRITE].avg = 1349;
724 cfg2.buckets[THROTTLE_OPS_READ].avg = 123;
725 cfg2.buckets[THROTTLE_OPS_WRITE].avg = 86;
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300726 throttle_group_config(tgm3, &cfg1);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200727
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300728 throttle_group_get_config(tgm1, &cfg1);
729 throttle_group_get_config(tgm3, &cfg2);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200730 g_assert(!memcmp(&cfg1, &cfg2, sizeof(cfg1)));
731
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300732 throttle_group_unregister_tgm(tgm1);
733 throttle_group_unregister_tgm(tgm2);
734 throttle_group_unregister_tgm(tgm3);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200735
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300736 g_assert(tgm1->throttle_state == NULL);
737 g_assert(tgm2->throttle_state == NULL);
738 g_assert(tgm3->throttle_state == NULL);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200739}
740
Benoît Canetf17cfe82013-09-02 14:14:38 +0200741int main(int argc, char **argv)
742{
Markus Armbruster73eaa042015-12-18 16:35:11 +0100743 qemu_init_main_loop(&error_fatal);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200744 ctx = qemu_get_aio_context();
Alberto Garcia1fee9552015-06-08 18:17:43 +0200745 bdrv_init();
Manos Pitsidianakis432d8892017-08-25 16:20:26 +0300746 module_call_init(MODULE_INIT_QOM);
Stefan Hajnoczi13af91e2014-05-14 16:22:45 +0200747
Benoît Canetf17cfe82013-09-02 14:14:38 +0200748 do {} while (g_main_context_iteration(NULL, false));
749
750 /* tests in the same order as the header function declarations */
751 g_test_init(&argc, &argv, NULL);
752 g_test_add_func("/throttle/leak_bucket", test_leak_bucket);
753 g_test_add_func("/throttle/compute_wait", test_compute_wait);
754 g_test_add_func("/throttle/init", test_init);
755 g_test_add_func("/throttle/destroy", test_destroy);
756 g_test_add_func("/throttle/have_timer", test_have_timer);
Stefan Hajnoczi22524f72014-05-14 16:22:46 +0200757 g_test_add_func("/throttle/detach_attach", test_detach_attach);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200758 g_test_add_func("/throttle/config/enabled", test_enabled);
759 g_test_add_func("/throttle/config/conflicting", test_conflicting_config);
760 g_test_add_func("/throttle/config/is_valid", test_is_valid);
Alberto Garciad942fee2017-08-24 16:24:49 +0300761 g_test_add_func("/throttle/config/ranges", test_ranges);
Stefan Hajnoczi92e11a12015-08-04 11:22:13 +0100762 g_test_add_func("/throttle/config/max", test_max_is_missing_limit);
Stefan Hajnoczi8860eab2016-06-01 17:40:31 -0700763 g_test_add_func("/throttle/config/iops_size",
764 test_iops_size_is_missing_limit);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200765 g_test_add_func("/throttle/config_functions", test_config_functions);
766 g_test_add_func("/throttle/accounting", test_accounting);
Alberto Garcia1fee9552015-06-08 18:17:43 +0200767 g_test_add_func("/throttle/groups", test_groups);
Benoît Canetf17cfe82013-09-02 14:14:38 +0200768 return g_test_run();
769}
770