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/throttle.h"
26#include "qemu/timer.h"
27#include "block/aio.h"
28
29
30
31
32
33
34void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta_ns)
35{
36 double leak;
37
38
39 leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
40
41
42 bkt->level = MAX(bkt->level - leak, 0);
43}
44
45
46
47
48
49static void throttle_do_leak(ThrottleState *ts, int64_t now)
50{
51
52 int64_t delta_ns = now - ts->previous_leak;
53 int i;
54
55 ts->previous_leak = now;
56
57 if (delta_ns <= 0) {
58 return;
59 }
60
61
62 for (i = 0; i < BUCKETS_COUNT; i++) {
63 throttle_leak_bucket(&ts->cfg.buckets[i], delta_ns);
64 }
65}
66
67
68
69
70
71
72
73static int64_t throttle_do_compute_wait(double limit, double extra)
74{
75 double wait = extra * NANOSECONDS_PER_SECOND;
76 wait /= limit;
77 return wait;
78}
79
80
81
82
83
84
85int64_t throttle_compute_wait(LeakyBucket *bkt)
86{
87 double extra;
88
89 if (!bkt->avg) {
90 return 0;
91 }
92
93 extra = bkt->level - bkt->max;
94
95 if (extra <= 0) {
96 return 0;
97 }
98
99 return throttle_do_compute_wait(bkt->avg, extra);
100}
101
102
103
104
105
106
107static int64_t throttle_compute_wait_for(ThrottleState *ts,
108 bool is_write)
109{
110 BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
111 THROTTLE_OPS_TOTAL,
112 THROTTLE_BPS_READ,
113 THROTTLE_OPS_READ},
114 {THROTTLE_BPS_TOTAL,
115 THROTTLE_OPS_TOTAL,
116 THROTTLE_BPS_WRITE,
117 THROTTLE_OPS_WRITE}, };
118 int64_t wait, max_wait = 0;
119 int i;
120
121 for (i = 0; i < 4; i++) {
122 BucketType index = to_check[is_write][i];
123 wait = throttle_compute_wait(&ts->cfg.buckets[index]);
124 if (wait > max_wait) {
125 max_wait = wait;
126 }
127 }
128
129 return max_wait;
130}
131
132
133
134
135
136
137
138
139bool throttle_compute_timer(ThrottleState *ts,
140 bool is_write,
141 int64_t now,
142 int64_t *next_timestamp)
143{
144 int64_t wait;
145
146
147 throttle_do_leak(ts, now);
148
149
150 wait = throttle_compute_wait_for(ts, is_write);
151
152
153 if (wait) {
154 *next_timestamp = now + wait;
155 return true;
156 }
157
158
159 *next_timestamp = now;
160 return false;
161}
162
163
164void throttle_timers_attach_aio_context(ThrottleTimers *tt,
165 AioContext *new_context)
166{
167 tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
168 tt->read_timer_cb, tt->timer_opaque);
169 tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
170 tt->write_timer_cb, tt->timer_opaque);
171}
172
173
174void throttle_init(ThrottleState *ts)
175{
176 memset(ts, 0, sizeof(ThrottleState));
177}
178
179
180void throttle_timers_init(ThrottleTimers *tt,
181 AioContext *aio_context,
182 QEMUClockType clock_type,
183 QEMUTimerCB *read_timer_cb,
184 QEMUTimerCB *write_timer_cb,
185 void *timer_opaque)
186{
187 memset(tt, 0, sizeof(ThrottleTimers));
188
189 tt->clock_type = clock_type;
190 tt->read_timer_cb = read_timer_cb;
191 tt->write_timer_cb = write_timer_cb;
192 tt->timer_opaque = timer_opaque;
193 throttle_timers_attach_aio_context(tt, aio_context);
194}
195
196
197static void throttle_timer_destroy(QEMUTimer **timer)
198{
199 assert(*timer != NULL);
200
201 timer_del(*timer);
202 timer_free(*timer);
203 *timer = NULL;
204}
205
206
207void throttle_timers_detach_aio_context(ThrottleTimers *tt)
208{
209 int i;
210
211 for (i = 0; i < 2; i++) {
212 throttle_timer_destroy(&tt->timers[i]);
213 }
214}
215
216
217void throttle_timers_destroy(ThrottleTimers *tt)
218{
219 throttle_timers_detach_aio_context(tt);
220}
221
222
223bool throttle_timers_are_initialized(ThrottleTimers *tt)
224{
225 if (tt->timers[0]) {
226 return true;
227 }
228
229 return false;
230}
231
232
233
234
235
236
237bool throttle_enabled(ThrottleConfig *cfg)
238{
239 int i;
240
241 for (i = 0; i < BUCKETS_COUNT; i++) {
242 if (cfg->buckets[i].avg > 0) {
243 return true;
244 }
245 }
246
247 return false;
248}
249
250
251
252
253
254
255bool throttle_conflicting(ThrottleConfig *cfg)
256{
257 bool bps_flag, ops_flag;
258 bool bps_max_flag, ops_max_flag;
259
260 bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
261 (cfg->buckets[THROTTLE_BPS_READ].avg ||
262 cfg->buckets[THROTTLE_BPS_WRITE].avg);
263
264 ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
265 (cfg->buckets[THROTTLE_OPS_READ].avg ||
266 cfg->buckets[THROTTLE_OPS_WRITE].avg);
267
268 bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
269 (cfg->buckets[THROTTLE_BPS_READ].max ||
270 cfg->buckets[THROTTLE_BPS_WRITE].max);
271
272 ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
273 (cfg->buckets[THROTTLE_OPS_READ].max ||
274 cfg->buckets[THROTTLE_OPS_WRITE].max);
275
276 return bps_flag || ops_flag || bps_max_flag || ops_max_flag;
277}
278
279
280
281
282
283bool throttle_is_valid(ThrottleConfig *cfg)
284{
285 bool invalid = false;
286 int i;
287
288 for (i = 0; i < BUCKETS_COUNT; i++) {
289 if (cfg->buckets[i].avg < 0) {
290 invalid = true;
291 }
292 }
293
294 for (i = 0; i < BUCKETS_COUNT; i++) {
295 if (cfg->buckets[i].max < 0) {
296 invalid = true;
297 }
298 }
299
300 return !invalid;
301}
302
303
304static void throttle_fix_bucket(LeakyBucket *bkt)
305{
306 double min;
307
308
309 bkt->level = 0;
310
311
312
313
314
315
316
317
318
319 min = bkt->avg / 10;
320 if (bkt->avg && !bkt->max) {
321 bkt->max = min;
322 }
323}
324
325
326static void throttle_cancel_timer(QEMUTimer *timer)
327{
328 assert(timer != NULL);
329
330 timer_del(timer);
331}
332
333
334
335
336
337
338
339void throttle_config(ThrottleState *ts,
340 ThrottleTimers *tt,
341 ThrottleConfig *cfg)
342{
343 int i;
344
345 ts->cfg = *cfg;
346
347 for (i = 0; i < BUCKETS_COUNT; i++) {
348 throttle_fix_bucket(&ts->cfg.buckets[i]);
349 }
350
351 ts->previous_leak = qemu_clock_get_ns(tt->clock_type);
352
353 for (i = 0; i < 2; i++) {
354 throttle_cancel_timer(tt->timers[i]);
355 }
356}
357
358
359
360
361
362
363void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
364{
365 *cfg = ts->cfg;
366}
367
368
369
370
371
372
373
374
375
376
377bool throttle_schedule_timer(ThrottleState *ts,
378 ThrottleTimers *tt,
379 bool is_write)
380{
381 int64_t now = qemu_clock_get_ns(tt->clock_type);
382 int64_t next_timestamp;
383 bool must_wait;
384
385 must_wait = throttle_compute_timer(ts,
386 is_write,
387 now,
388 &next_timestamp);
389
390
391 if (!must_wait) {
392 return false;
393 }
394
395
396 if (timer_pending(tt->timers[is_write])) {
397 return true;
398 }
399
400
401 timer_mod(tt->timers[is_write], next_timestamp);
402 return true;
403}
404
405
406
407
408
409
410void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
411{
412 double units = 1.0;
413
414
415 if (ts->cfg.op_size && size > ts->cfg.op_size) {
416 units = (double) size / ts->cfg.op_size;
417 }
418
419 ts->cfg.buckets[THROTTLE_BPS_TOTAL].level += size;
420 ts->cfg.buckets[THROTTLE_OPS_TOTAL].level += units;
421
422 if (is_write) {
423 ts->cfg.buckets[THROTTLE_BPS_WRITE].level += size;
424 ts->cfg.buckets[THROTTLE_OPS_WRITE].level += units;
425 } else {
426 ts->cfg.buckets[THROTTLE_BPS_READ].level += size;
427 ts->cfg.buckets[THROTTLE_OPS_READ].level += units;
428 }
429}
430
431