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
99 if (!bkt->avg) {
100 return 0;
101 }
102
103
104 extra = bkt->level - bkt->max * bkt->burst_length;
105 if (extra > 0) {
106 return throttle_do_compute_wait(bkt->avg, extra);
107 }
108
109
110
111 if (bkt->burst_length > 1) {
112
113
114 extra = bkt->burst_level - bkt->max / 10;
115 if (extra > 0) {
116 return throttle_do_compute_wait(bkt->max, extra);
117 }
118 }
119
120 return 0;
121}
122
123
124
125
126
127
128static int64_t throttle_compute_wait_for(ThrottleState *ts,
129 bool is_write)
130{
131 BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
132 THROTTLE_OPS_TOTAL,
133 THROTTLE_BPS_READ,
134 THROTTLE_OPS_READ},
135 {THROTTLE_BPS_TOTAL,
136 THROTTLE_OPS_TOTAL,
137 THROTTLE_BPS_WRITE,
138 THROTTLE_OPS_WRITE}, };
139 int64_t wait, max_wait = 0;
140 int i;
141
142 for (i = 0; i < 4; i++) {
143 BucketType index = to_check[is_write][i];
144 wait = throttle_compute_wait(&ts->cfg.buckets[index]);
145 if (wait > max_wait) {
146 max_wait = wait;
147 }
148 }
149
150 return max_wait;
151}
152
153
154
155
156
157
158
159
160static bool throttle_compute_timer(ThrottleState *ts,
161 bool is_write,
162 int64_t now,
163 int64_t *next_timestamp)
164{
165 int64_t wait;
166
167
168 throttle_do_leak(ts, now);
169
170
171 wait = throttle_compute_wait_for(ts, is_write);
172
173
174 if (wait) {
175 *next_timestamp = now + wait;
176 return true;
177 }
178
179
180 *next_timestamp = now;
181 return false;
182}
183
184
185void throttle_timers_attach_aio_context(ThrottleTimers *tt,
186 AioContext *new_context)
187{
188 tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
189 tt->read_timer_cb, tt->timer_opaque);
190 tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
191 tt->write_timer_cb, tt->timer_opaque);
192}
193
194
195
196
197
198void throttle_config_init(ThrottleConfig *cfg)
199{
200 unsigned i;
201 memset(cfg, 0, sizeof(*cfg));
202 for (i = 0; i < BUCKETS_COUNT; i++) {
203 cfg->buckets[i].burst_length = 1;
204 }
205}
206
207
208void throttle_init(ThrottleState *ts)
209{
210 memset(ts, 0, sizeof(ThrottleState));
211 throttle_config_init(&ts->cfg);
212}
213
214
215void throttle_timers_init(ThrottleTimers *tt,
216 AioContext *aio_context,
217 QEMUClockType clock_type,
218 QEMUTimerCB *read_timer_cb,
219 QEMUTimerCB *write_timer_cb,
220 void *timer_opaque)
221{
222 memset(tt, 0, sizeof(ThrottleTimers));
223
224 tt->clock_type = clock_type;
225 tt->read_timer_cb = read_timer_cb;
226 tt->write_timer_cb = write_timer_cb;
227 tt->timer_opaque = timer_opaque;
228 throttle_timers_attach_aio_context(tt, aio_context);
229}
230
231
232static void throttle_timer_destroy(QEMUTimer **timer)
233{
234 assert(*timer != NULL);
235
236 timer_del(*timer);
237 timer_free(*timer);
238 *timer = NULL;
239}
240
241
242void throttle_timers_detach_aio_context(ThrottleTimers *tt)
243{
244 int i;
245
246 for (i = 0; i < 2; i++) {
247 throttle_timer_destroy(&tt->timers[i]);
248 }
249}
250
251
252void throttle_timers_destroy(ThrottleTimers *tt)
253{
254 throttle_timers_detach_aio_context(tt);
255}
256
257
258bool throttle_timers_are_initialized(ThrottleTimers *tt)
259{
260 if (tt->timers[0]) {
261 return true;
262 }
263
264 return false;
265}
266
267
268
269
270
271
272bool throttle_enabled(ThrottleConfig *cfg)
273{
274 int i;
275
276 for (i = 0; i < BUCKETS_COUNT; i++) {
277 if (cfg->buckets[i].avg > 0) {
278 return true;
279 }
280 }
281
282 return false;
283}
284
285
286
287
288
289
290bool throttle_is_valid(ThrottleConfig *cfg, Error **errp)
291{
292 int i;
293 bool bps_flag, ops_flag;
294 bool bps_max_flag, ops_max_flag;
295
296 bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
297 (cfg->buckets[THROTTLE_BPS_READ].avg ||
298 cfg->buckets[THROTTLE_BPS_WRITE].avg);
299
300 ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
301 (cfg->buckets[THROTTLE_OPS_READ].avg ||
302 cfg->buckets[THROTTLE_OPS_WRITE].avg);
303
304 bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
305 (cfg->buckets[THROTTLE_BPS_READ].max ||
306 cfg->buckets[THROTTLE_BPS_WRITE].max);
307
308 ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
309 (cfg->buckets[THROTTLE_OPS_READ].max ||
310 cfg->buckets[THROTTLE_OPS_WRITE].max);
311
312 if (bps_flag || ops_flag || bps_max_flag || ops_max_flag) {
313 error_setg(errp, "bps/iops/max total values and read/write values"
314 " cannot be used at the same time");
315 return false;
316 }
317
318 for (i = 0; i < BUCKETS_COUNT; i++) {
319 if (cfg->buckets[i].avg < 0 ||
320 cfg->buckets[i].max < 0 ||
321 cfg->buckets[i].avg > THROTTLE_VALUE_MAX ||
322 cfg->buckets[i].max > THROTTLE_VALUE_MAX) {
323 error_setg(errp, "bps/iops/max values must be within [0, %lld]",
324 THROTTLE_VALUE_MAX);
325 return false;
326 }
327
328 if (!cfg->buckets[i].burst_length) {
329 error_setg(errp, "the burst length cannot be 0");
330 return false;
331 }
332
333 if (cfg->buckets[i].burst_length > 1 && !cfg->buckets[i].max) {
334 error_setg(errp, "burst length set without burst rate");
335 return false;
336 }
337
338 if (cfg->buckets[i].max && !cfg->buckets[i].avg) {
339 error_setg(errp, "bps_max/iops_max require corresponding"
340 " bps/iops values");
341 return false;
342 }
343 }
344
345 return true;
346}
347
348
349static void throttle_fix_bucket(LeakyBucket *bkt)
350{
351 double min;
352
353
354 bkt->level = bkt->burst_level = 0;
355
356
357
358
359
360
361
362
363
364 min = bkt->avg / 10;
365 if (bkt->avg && !bkt->max) {
366 bkt->max = min;
367 }
368}
369
370
371static void throttle_cancel_timer(QEMUTimer *timer)
372{
373 assert(timer != NULL);
374
375 timer_del(timer);
376}
377
378
379
380
381
382
383
384void throttle_config(ThrottleState *ts,
385 ThrottleTimers *tt,
386 ThrottleConfig *cfg)
387{
388 int i;
389
390 ts->cfg = *cfg;
391
392 for (i = 0; i < BUCKETS_COUNT; i++) {
393 throttle_fix_bucket(&ts->cfg.buckets[i]);
394 }
395
396 ts->previous_leak = qemu_clock_get_ns(tt->clock_type);
397
398 for (i = 0; i < 2; i++) {
399 throttle_cancel_timer(tt->timers[i]);
400 }
401}
402
403
404
405
406
407
408void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
409{
410 *cfg = ts->cfg;
411}
412
413
414
415
416
417
418
419
420
421
422bool throttle_schedule_timer(ThrottleState *ts,
423 ThrottleTimers *tt,
424 bool is_write)
425{
426 int64_t now = qemu_clock_get_ns(tt->clock_type);
427 int64_t next_timestamp;
428 bool must_wait;
429
430 must_wait = throttle_compute_timer(ts,
431 is_write,
432 now,
433 &next_timestamp);
434
435
436 if (!must_wait) {
437 return false;
438 }
439
440
441 if (timer_pending(tt->timers[is_write])) {
442 return true;
443 }
444
445
446 timer_mod(tt->timers[is_write], next_timestamp);
447 return true;
448}
449
450
451
452
453
454
455void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
456{
457 const BucketType bucket_types_size[2][2] = {
458 { THROTTLE_BPS_TOTAL, THROTTLE_BPS_READ },
459 { THROTTLE_BPS_TOTAL, THROTTLE_BPS_WRITE }
460 };
461 const BucketType bucket_types_units[2][2] = {
462 { THROTTLE_OPS_TOTAL, THROTTLE_OPS_READ },
463 { THROTTLE_OPS_TOTAL, THROTTLE_OPS_WRITE }
464 };
465 double units = 1.0;
466 unsigned i;
467
468
469 if (ts->cfg.op_size && size > ts->cfg.op_size) {
470 units = (double) size / ts->cfg.op_size;
471 }
472
473 for (i = 0; i < 2; i++) {
474 LeakyBucket *bkt;
475
476 bkt = &ts->cfg.buckets[bucket_types_size[is_write][i]];
477 bkt->level += size;
478 if (bkt->burst_length > 1) {
479 bkt->burst_level += size;
480 }
481
482 bkt = &ts->cfg.buckets[bucket_types_units[is_write][i]];
483 bkt->level += units;
484 if (bkt->burst_length > 1) {
485 bkt->burst_level += units;
486 }
487 }
488}
489
490