1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "qapi/error.h"
27#include "qemu/throttle.h"
28#include "qemu/timer.h"
29#include "block/aio.h"
30
31
32
33
34
35
36void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta_ns)
37{
38 double leak;
39
40
41 leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
42
43
44 bkt->level = MAX(bkt->level - leak, 0);
45
46
47
48
49 if (bkt->burst_length > 1) {
50 leak = (bkt->max * (double) delta_ns) / NANOSECONDS_PER_SECOND;
51 bkt->burst_level = MAX(bkt->burst_level - leak, 0);
52 }
53}
54
55
56
57
58
59static void throttle_do_leak(ThrottleState *ts, int64_t now)
60{
61
62 int64_t delta_ns = now - ts->previous_leak;
63 int i;
64
65 ts->previous_leak = now;
66
67 if (delta_ns <= 0) {
68 return;
69 }
70
71
72 for (i = 0; i < BUCKETS_COUNT; i++) {
73 throttle_leak_bucket(&ts->cfg.buckets[i], delta_ns);
74 }
75}
76
77
78
79
80
81
82
83static int64_t throttle_do_compute_wait(double limit, double extra)
84{
85 double wait = extra * NANOSECONDS_PER_SECOND;
86 wait /= limit;
87 return wait;
88}
89
90
91
92
93
94
95int64_t throttle_compute_wait(LeakyBucket *bkt)
96{
97 double extra;
98 double bucket_size;
99 double burst_bucket_size;
100
101 if (!bkt->avg) {
102 return 0;
103 }
104
105 if (!bkt->max) {
106
107
108
109 bucket_size = (double) bkt->avg / 10;
110 burst_bucket_size = 0;
111 } else {
112
113
114 bucket_size = bkt->max * bkt->burst_length;
115 burst_bucket_size = (double) bkt->max / 10;
116 }
117
118
119 extra = bkt->level - bucket_size;
120 if (extra > 0) {
121 return throttle_do_compute_wait(bkt->avg, extra);
122 }
123
124
125
126 if (bkt->burst_length > 1) {
127 assert(bkt->max > 0);
128 extra = bkt->burst_level - burst_bucket_size;
129 if (extra > 0) {
130 return throttle_do_compute_wait(bkt->max, extra);
131 }
132 }
133
134 return 0;
135}
136
137
138
139
140
141
142static int64_t throttle_compute_wait_for(ThrottleState *ts,
143 bool is_write)
144{
145 BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
146 THROTTLE_OPS_TOTAL,
147 THROTTLE_BPS_READ,
148 THROTTLE_OPS_READ},
149 {THROTTLE_BPS_TOTAL,
150 THROTTLE_OPS_TOTAL,
151 THROTTLE_BPS_WRITE,
152 THROTTLE_OPS_WRITE}, };
153 int64_t wait, max_wait = 0;
154 int i;
155
156 for (i = 0; i < 4; i++) {
157 BucketType index = to_check[is_write][i];
158 wait = throttle_compute_wait(&ts->cfg.buckets[index]);
159 if (wait > max_wait) {
160 max_wait = wait;
161 }
162 }
163
164 return max_wait;
165}
166
167
168
169
170
171
172
173
174static bool throttle_compute_timer(ThrottleState *ts,
175 bool is_write,
176 int64_t now,
177 int64_t *next_timestamp)
178{
179 int64_t wait;
180
181
182 throttle_do_leak(ts, now);
183
184
185 wait = throttle_compute_wait_for(ts, is_write);
186
187
188 if (wait) {
189 *next_timestamp = now + wait;
190 return true;
191 }
192
193
194 *next_timestamp = now;
195 return false;
196}
197
198
199void throttle_timers_attach_aio_context(ThrottleTimers *tt,
200 AioContext *new_context)
201{
202 tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
203 tt->read_timer_cb, tt->timer_opaque);
204 tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
205 tt->write_timer_cb, tt->timer_opaque);
206}
207
208
209
210
211
212void throttle_config_init(ThrottleConfig *cfg)
213{
214 unsigned i;
215 memset(cfg, 0, sizeof(*cfg));
216 for (i = 0; i < BUCKETS_COUNT; i++) {
217 cfg->buckets[i].burst_length = 1;
218 }
219}
220
221
222void throttle_init(ThrottleState *ts)
223{
224 memset(ts, 0, sizeof(ThrottleState));
225 throttle_config_init(&ts->cfg);
226}
227
228
229void throttle_timers_init(ThrottleTimers *tt,
230 AioContext *aio_context,
231 QEMUClockType clock_type,
232 QEMUTimerCB *read_timer_cb,
233 QEMUTimerCB *write_timer_cb,
234 void *timer_opaque)
235{
236 memset(tt, 0, sizeof(ThrottleTimers));
237
238 tt->clock_type = clock_type;
239 tt->read_timer_cb = read_timer_cb;
240 tt->write_timer_cb = write_timer_cb;
241 tt->timer_opaque = timer_opaque;
242 throttle_timers_attach_aio_context(tt, aio_context);
243}
244
245
246static void throttle_timer_destroy(QEMUTimer **timer)
247{
248 assert(*timer != NULL);
249
250 timer_del(*timer);
251 timer_free(*timer);
252 *timer = NULL;
253}
254
255
256void throttle_timers_detach_aio_context(ThrottleTimers *tt)
257{
258 int i;
259
260 for (i = 0; i < 2; i++) {
261 throttle_timer_destroy(&tt->timers[i]);
262 }
263}
264
265
266void throttle_timers_destroy(ThrottleTimers *tt)
267{
268 throttle_timers_detach_aio_context(tt);
269}
270
271
272bool throttle_timers_are_initialized(ThrottleTimers *tt)
273{
274 if (tt->timers[0]) {
275 return true;
276 }
277
278 return false;
279}
280
281
282
283
284
285
286bool throttle_enabled(ThrottleConfig *cfg)
287{
288 int i;
289
290 for (i = 0; i < BUCKETS_COUNT; i++) {
291 if (cfg->buckets[i].avg > 0) {
292 return true;
293 }
294 }
295
296 return false;
297}
298
299
300
301
302
303
304bool throttle_is_valid(ThrottleConfig *cfg, Error **errp)
305{
306 int i;
307 bool bps_flag, ops_flag;
308 bool bps_max_flag, ops_max_flag;
309
310 bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
311 (cfg->buckets[THROTTLE_BPS_READ].avg ||
312 cfg->buckets[THROTTLE_BPS_WRITE].avg);
313
314 ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
315 (cfg->buckets[THROTTLE_OPS_READ].avg ||
316 cfg->buckets[THROTTLE_OPS_WRITE].avg);
317
318 bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
319 (cfg->buckets[THROTTLE_BPS_READ].max ||
320 cfg->buckets[THROTTLE_BPS_WRITE].max);
321
322 ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
323 (cfg->buckets[THROTTLE_OPS_READ].max ||
324 cfg->buckets[THROTTLE_OPS_WRITE].max);
325
326 if (bps_flag || ops_flag || bps_max_flag || ops_max_flag) {
327 error_setg(errp, "bps/iops/max total values and read/write values"
328 " cannot be used at the same time");
329 return false;
330 }
331
332 if (cfg->op_size &&
333 !cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
334 !cfg->buckets[THROTTLE_OPS_READ].avg &&
335 !cfg->buckets[THROTTLE_OPS_WRITE].avg) {
336 error_setg(errp, "iops size requires an iops value to be set");
337 return false;
338 }
339
340 for (i = 0; i < BUCKETS_COUNT; i++) {
341 LeakyBucket *bkt = &cfg->buckets[i];
342 if (bkt->avg > THROTTLE_VALUE_MAX || bkt->max > THROTTLE_VALUE_MAX) {
343 error_setg(errp, "bps/iops/max values must be within [0, %lld]",
344 THROTTLE_VALUE_MAX);
345 return false;
346 }
347
348 if (!bkt->burst_length) {
349 error_setg(errp, "the burst length cannot be 0");
350 return false;
351 }
352
353 if (bkt->burst_length > 1 && !bkt->max) {
354 error_setg(errp, "burst length set without burst rate");
355 return false;
356 }
357
358 if (bkt->max && bkt->burst_length > THROTTLE_VALUE_MAX / bkt->max) {
359 error_setg(errp, "burst length too high for this burst rate");
360 return false;
361 }
362
363 if (bkt->max && !bkt->avg) {
364 error_setg(errp, "bps_max/iops_max require corresponding"
365 " bps/iops values");
366 return false;
367 }
368
369 if (bkt->max && bkt->max < bkt->avg) {
370 error_setg(errp, "bps_max/iops_max cannot be lower than bps/iops");
371 return false;
372 }
373 }
374
375 return true;
376}
377
378
379
380
381
382
383
384void throttle_config(ThrottleState *ts,
385 QEMUClockType clock_type,
386 ThrottleConfig *cfg)
387{
388 int i;
389
390 ts->cfg = *cfg;
391
392
393 for (i = 0; i < BUCKETS_COUNT; i++) {
394 ts->cfg.buckets[i].level = 0;
395 ts->cfg.buckets[i].burst_level = 0;
396 }
397
398 ts->previous_leak = qemu_clock_get_ns(clock_type);
399}
400
401
402
403
404
405
406void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
407{
408 *cfg = ts->cfg;
409}
410
411
412
413
414
415
416
417
418
419
420bool throttle_schedule_timer(ThrottleState *ts,
421 ThrottleTimers *tt,
422 bool is_write)
423{
424 int64_t now = qemu_clock_get_ns(tt->clock_type);
425 int64_t next_timestamp;
426 bool must_wait;
427
428 must_wait = throttle_compute_timer(ts,
429 is_write,
430 now,
431 &next_timestamp);
432
433
434 if (!must_wait) {
435 return false;
436 }
437
438
439 if (timer_pending(tt->timers[is_write])) {
440 return true;
441 }
442
443
444 timer_mod(tt->timers[is_write], next_timestamp);
445 return true;
446}
447
448
449
450
451
452
453void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
454{
455 const BucketType bucket_types_size[2][2] = {
456 { THROTTLE_BPS_TOTAL, THROTTLE_BPS_READ },
457 { THROTTLE_BPS_TOTAL, THROTTLE_BPS_WRITE }
458 };
459 const BucketType bucket_types_units[2][2] = {
460 { THROTTLE_OPS_TOTAL, THROTTLE_OPS_READ },
461 { THROTTLE_OPS_TOTAL, THROTTLE_OPS_WRITE }
462 };
463 double units = 1.0;
464 unsigned i;
465
466
467 if (ts->cfg.op_size && size > ts->cfg.op_size) {
468 units = (double) size / ts->cfg.op_size;
469 }
470
471 for (i = 0; i < 2; i++) {
472 LeakyBucket *bkt;
473
474 bkt = &ts->cfg.buckets[bucket_types_size[is_write][i]];
475 bkt->level += size;
476 if (bkt->burst_length > 1) {
477 bkt->burst_level += size;
478 }
479
480 bkt = &ts->cfg.buckets[bucket_types_units[is_write][i]];
481 bkt->level += units;
482 if (bkt->burst_length > 1) {
483 bkt->burst_level += units;
484 }
485 }
486}
487
488
489
490
491
492
493
494void throttle_limits_to_config(ThrottleLimits *arg, ThrottleConfig *cfg,
495 Error **errp)
496{
497 if (arg->has_bps_total) {
498 cfg->buckets[THROTTLE_BPS_TOTAL].avg = arg->bps_total;
499 }
500 if (arg->has_bps_read) {
501 cfg->buckets[THROTTLE_BPS_READ].avg = arg->bps_read;
502 }
503 if (arg->has_bps_write) {
504 cfg->buckets[THROTTLE_BPS_WRITE].avg = arg->bps_write;
505 }
506
507 if (arg->has_iops_total) {
508 cfg->buckets[THROTTLE_OPS_TOTAL].avg = arg->iops_total;
509 }
510 if (arg->has_iops_read) {
511 cfg->buckets[THROTTLE_OPS_READ].avg = arg->iops_read;
512 }
513 if (arg->has_iops_write) {
514 cfg->buckets[THROTTLE_OPS_WRITE].avg = arg->iops_write;
515 }
516
517 if (arg->has_bps_total_max) {
518 cfg->buckets[THROTTLE_BPS_TOTAL].max = arg->bps_total_max;
519 }
520 if (arg->has_bps_read_max) {
521 cfg->buckets[THROTTLE_BPS_READ].max = arg->bps_read_max;
522 }
523 if (arg->has_bps_write_max) {
524 cfg->buckets[THROTTLE_BPS_WRITE].max = arg->bps_write_max;
525 }
526 if (arg->has_iops_total_max) {
527 cfg->buckets[THROTTLE_OPS_TOTAL].max = arg->iops_total_max;
528 }
529 if (arg->has_iops_read_max) {
530 cfg->buckets[THROTTLE_OPS_READ].max = arg->iops_read_max;
531 }
532 if (arg->has_iops_write_max) {
533 cfg->buckets[THROTTLE_OPS_WRITE].max = arg->iops_write_max;
534 }
535
536 if (arg->has_bps_total_max_length) {
537 if (arg->bps_total_max_length > UINT_MAX) {
538 error_setg(errp, "bps-total-max-length value must be in"
539 " the range [0, %u]", UINT_MAX);
540 return;
541 }
542 cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_total_max_length;
543 }
544 if (arg->has_bps_read_max_length) {
545 if (arg->bps_read_max_length > UINT_MAX) {
546 error_setg(errp, "bps-read-max-length value must be in"
547 " the range [0, %u]", UINT_MAX);
548 return;
549 }
550 cfg->buckets[THROTTLE_BPS_READ].burst_length = arg->bps_read_max_length;
551 }
552 if (arg->has_bps_write_max_length) {
553 if (arg->bps_write_max_length > UINT_MAX) {
554 error_setg(errp, "bps-write-max-length value must be in"
555 " the range [0, %u]", UINT_MAX);
556 return;
557 }
558 cfg->buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_write_max_length;
559 }
560 if (arg->has_iops_total_max_length) {
561 if (arg->iops_total_max_length > UINT_MAX) {
562 error_setg(errp, "iops-total-max-length value must be in"
563 " the range [0, %u]", UINT_MAX);
564 return;
565 }
566 cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_total_max_length;
567 }
568 if (arg->has_iops_read_max_length) {
569 if (arg->iops_read_max_length > UINT_MAX) {
570 error_setg(errp, "iops-read-max-length value must be in"
571 " the range [0, %u]", UINT_MAX);
572 return;
573 }
574 cfg->buckets[THROTTLE_OPS_READ].burst_length = arg->iops_read_max_length;
575 }
576 if (arg->has_iops_write_max_length) {
577 if (arg->iops_write_max_length > UINT_MAX) {
578 error_setg(errp, "iops-write-max-length value must be in"
579 " the range [0, %u]", UINT_MAX);
580 return;
581 }
582 cfg->buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_write_max_length;
583 }
584
585 if (arg->has_iops_size) {
586 cfg->op_size = arg->iops_size;
587 }
588
589 throttle_is_valid(cfg, errp);
590}
591
592
593
594
595
596
597void throttle_config_to_limits(ThrottleConfig *cfg, ThrottleLimits *var)
598{
599 var->bps_total = cfg->buckets[THROTTLE_BPS_TOTAL].avg;
600 var->bps_read = cfg->buckets[THROTTLE_BPS_READ].avg;
601 var->bps_write = cfg->buckets[THROTTLE_BPS_WRITE].avg;
602 var->iops_total = cfg->buckets[THROTTLE_OPS_TOTAL].avg;
603 var->iops_read = cfg->buckets[THROTTLE_OPS_READ].avg;
604 var->iops_write = cfg->buckets[THROTTLE_OPS_WRITE].avg;
605 var->bps_total_max = cfg->buckets[THROTTLE_BPS_TOTAL].max;
606 var->bps_read_max = cfg->buckets[THROTTLE_BPS_READ].max;
607 var->bps_write_max = cfg->buckets[THROTTLE_BPS_WRITE].max;
608 var->iops_total_max = cfg->buckets[THROTTLE_OPS_TOTAL].max;
609 var->iops_read_max = cfg->buckets[THROTTLE_OPS_READ].max;
610 var->iops_write_max = cfg->buckets[THROTTLE_OPS_WRITE].max;
611 var->bps_total_max_length = cfg->buckets[THROTTLE_BPS_TOTAL].burst_length;
612 var->bps_read_max_length = cfg->buckets[THROTTLE_BPS_READ].burst_length;
613 var->bps_write_max_length = cfg->buckets[THROTTLE_BPS_WRITE].burst_length;
614 var->iops_total_max_length = cfg->buckets[THROTTLE_OPS_TOTAL].burst_length;
615 var->iops_read_max_length = cfg->buckets[THROTTLE_OPS_READ].burst_length;
616 var->iops_write_max_length = cfg->buckets[THROTTLE_OPS_WRITE].burst_length;
617 var->iops_size = cfg->op_size;
618
619 var->has_bps_total = true;
620 var->has_bps_read = true;
621 var->has_bps_write = true;
622 var->has_iops_total = true;
623 var->has_iops_read = true;
624 var->has_iops_write = true;
625 var->has_bps_total_max = true;
626 var->has_bps_read_max = true;
627 var->has_bps_write_max = true;
628 var->has_iops_total_max = true;
629 var->has_iops_read_max = true;
630 var->has_iops_write_max = true;
631 var->has_bps_read_max_length = true;
632 var->has_bps_total_max_length = true;
633 var->has_bps_write_max_length = true;
634 var->has_iops_total_max_length = true;
635 var->has_iops_read_max_length = true;
636 var->has_iops_write_max_length = true;
637 var->has_iops_size = true;
638}
639