]> git.proxmox.com Git - mirror_qemu.git/blob - util/throttle.c
throttle: Merge all functions that check the configuration into one
[mirror_qemu.git] / util / throttle.c
1 /*
2 * QEMU throttling infrastructure
3 *
4 * Copyright (C) Nodalink, EURL. 2013-2014
5 * Copyright (C) Igalia, S.L. 2015
6 *
7 * Authors:
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 or
14 * (at your option) version 3 of the License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "qemu/osdep.h"
26 #include "qemu/throttle.h"
27 #include "qemu/timer.h"
28 #include "block/aio.h"
29
30 /* This function make a bucket leak
31 *
32 * @bkt: the bucket to make leak
33 * @delta_ns: the time delta
34 */
35 void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta_ns)
36 {
37 double leak;
38
39 /* compute how much to leak */
40 leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
41
42 /* make the bucket leak */
43 bkt->level = MAX(bkt->level - leak, 0);
44 }
45
46 /* Calculate the time delta since last leak and make proportionals leaks
47 *
48 * @now: the current timestamp in ns
49 */
50 static void throttle_do_leak(ThrottleState *ts, int64_t now)
51 {
52 /* compute the time elapsed since the last leak */
53 int64_t delta_ns = now - ts->previous_leak;
54 int i;
55
56 ts->previous_leak = now;
57
58 if (delta_ns <= 0) {
59 return;
60 }
61
62 /* make each bucket leak */
63 for (i = 0; i < BUCKETS_COUNT; i++) {
64 throttle_leak_bucket(&ts->cfg.buckets[i], delta_ns);
65 }
66 }
67
68 /* do the real job of computing the time to wait
69 *
70 * @limit: the throttling limit
71 * @extra: the number of operation to delay
72 * @ret: the time to wait in ns
73 */
74 static int64_t throttle_do_compute_wait(double limit, double extra)
75 {
76 double wait = extra * NANOSECONDS_PER_SECOND;
77 wait /= limit;
78 return wait;
79 }
80
81 /* This function compute the wait time in ns that a leaky bucket should trigger
82 *
83 * @bkt: the leaky bucket we operate on
84 * @ret: the resulting wait time in ns or 0 if the operation can go through
85 */
86 int64_t throttle_compute_wait(LeakyBucket *bkt)
87 {
88 double extra; /* the number of extra units blocking the io */
89
90 if (!bkt->avg) {
91 return 0;
92 }
93
94 extra = bkt->level - bkt->max;
95
96 if (extra <= 0) {
97 return 0;
98 }
99
100 return throttle_do_compute_wait(bkt->avg, extra);
101 }
102
103 /* This function compute the time that must be waited while this IO
104 *
105 * @is_write: true if the current IO is a write, false if it's a read
106 * @ret: time to wait
107 */
108 static int64_t throttle_compute_wait_for(ThrottleState *ts,
109 bool is_write)
110 {
111 BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
112 THROTTLE_OPS_TOTAL,
113 THROTTLE_BPS_READ,
114 THROTTLE_OPS_READ},
115 {THROTTLE_BPS_TOTAL,
116 THROTTLE_OPS_TOTAL,
117 THROTTLE_BPS_WRITE,
118 THROTTLE_OPS_WRITE}, };
119 int64_t wait, max_wait = 0;
120 int i;
121
122 for (i = 0; i < 4; i++) {
123 BucketType index = to_check[is_write][i];
124 wait = throttle_compute_wait(&ts->cfg.buckets[index]);
125 if (wait > max_wait) {
126 max_wait = wait;
127 }
128 }
129
130 return max_wait;
131 }
132
133 /* compute the timer for this type of operation
134 *
135 * @is_write: the type of operation
136 * @now: the current clock timestamp
137 * @next_timestamp: the resulting timer
138 * @ret: true if a timer must be set
139 */
140 static bool throttle_compute_timer(ThrottleState *ts,
141 bool is_write,
142 int64_t now,
143 int64_t *next_timestamp)
144 {
145 int64_t wait;
146
147 /* leak proportionally to the time elapsed */
148 throttle_do_leak(ts, now);
149
150 /* compute the wait time if any */
151 wait = throttle_compute_wait_for(ts, is_write);
152
153 /* if the code must wait compute when the next timer should fire */
154 if (wait) {
155 *next_timestamp = now + wait;
156 return true;
157 }
158
159 /* else no need to wait at all */
160 *next_timestamp = now;
161 return false;
162 }
163
164 /* Add timers to event loop */
165 void throttle_timers_attach_aio_context(ThrottleTimers *tt,
166 AioContext *new_context)
167 {
168 tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
169 tt->read_timer_cb, tt->timer_opaque);
170 tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
171 tt->write_timer_cb, tt->timer_opaque);
172 }
173
174 /* To be called first on the ThrottleState */
175 void throttle_init(ThrottleState *ts)
176 {
177 memset(ts, 0, sizeof(ThrottleState));
178 }
179
180 /* To be called first on the ThrottleTimers */
181 void throttle_timers_init(ThrottleTimers *tt,
182 AioContext *aio_context,
183 QEMUClockType clock_type,
184 QEMUTimerCB *read_timer_cb,
185 QEMUTimerCB *write_timer_cb,
186 void *timer_opaque)
187 {
188 memset(tt, 0, sizeof(ThrottleTimers));
189
190 tt->clock_type = clock_type;
191 tt->read_timer_cb = read_timer_cb;
192 tt->write_timer_cb = write_timer_cb;
193 tt->timer_opaque = timer_opaque;
194 throttle_timers_attach_aio_context(tt, aio_context);
195 }
196
197 /* destroy a timer */
198 static void throttle_timer_destroy(QEMUTimer **timer)
199 {
200 assert(*timer != NULL);
201
202 timer_del(*timer);
203 timer_free(*timer);
204 *timer = NULL;
205 }
206
207 /* Remove timers from event loop */
208 void throttle_timers_detach_aio_context(ThrottleTimers *tt)
209 {
210 int i;
211
212 for (i = 0; i < 2; i++) {
213 throttle_timer_destroy(&tt->timers[i]);
214 }
215 }
216
217 /* To be called last on the ThrottleTimers */
218 void throttle_timers_destroy(ThrottleTimers *tt)
219 {
220 throttle_timers_detach_aio_context(tt);
221 }
222
223 /* is any throttling timer configured */
224 bool throttle_timers_are_initialized(ThrottleTimers *tt)
225 {
226 if (tt->timers[0]) {
227 return true;
228 }
229
230 return false;
231 }
232
233 /* Does any throttling must be done
234 *
235 * @cfg: the throttling configuration to inspect
236 * @ret: true if throttling must be done else false
237 */
238 bool throttle_enabled(ThrottleConfig *cfg)
239 {
240 int i;
241
242 for (i = 0; i < BUCKETS_COUNT; i++) {
243 if (cfg->buckets[i].avg > 0) {
244 return true;
245 }
246 }
247
248 return false;
249 }
250
251 /* check if a throttling configuration is valid
252 * @cfg: the throttling configuration to inspect
253 * @ret: true if valid else false
254 * @errp: error object
255 */
256 bool throttle_is_valid(ThrottleConfig *cfg, Error **errp)
257 {
258 int i;
259 bool bps_flag, ops_flag;
260 bool bps_max_flag, ops_max_flag;
261
262 bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
263 (cfg->buckets[THROTTLE_BPS_READ].avg ||
264 cfg->buckets[THROTTLE_BPS_WRITE].avg);
265
266 ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
267 (cfg->buckets[THROTTLE_OPS_READ].avg ||
268 cfg->buckets[THROTTLE_OPS_WRITE].avg);
269
270 bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
271 (cfg->buckets[THROTTLE_BPS_READ].max ||
272 cfg->buckets[THROTTLE_BPS_WRITE].max);
273
274 ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
275 (cfg->buckets[THROTTLE_OPS_READ].max ||
276 cfg->buckets[THROTTLE_OPS_WRITE].max);
277
278 if (bps_flag || ops_flag || bps_max_flag || ops_max_flag) {
279 error_setg(errp, "bps/iops/max total values and read/write values"
280 " cannot be used at the same time");
281 return false;
282 }
283
284 for (i = 0; i < BUCKETS_COUNT; i++) {
285 if (cfg->buckets[i].avg < 0 ||
286 cfg->buckets[i].max < 0 ||
287 cfg->buckets[i].avg > THROTTLE_VALUE_MAX ||
288 cfg->buckets[i].max > THROTTLE_VALUE_MAX) {
289 error_setg(errp, "bps/iops/max values must be within [0, %lld]",
290 THROTTLE_VALUE_MAX);
291 return false;
292 }
293
294 if (cfg->buckets[i].max && !cfg->buckets[i].avg) {
295 error_setg(errp, "bps_max/iops_max require corresponding"
296 " bps/iops values");
297 return false;
298 }
299 }
300
301 return true;
302 }
303
304 /* fix bucket parameters */
305 static void throttle_fix_bucket(LeakyBucket *bkt)
306 {
307 double min;
308
309 /* zero bucket level */
310 bkt->level = 0;
311
312 /* The following is done to cope with the Linux CFQ block scheduler
313 * which regroup reads and writes by block of 100ms in the guest.
314 * When they are two process one making reads and one making writes cfq
315 * make a pattern looking like the following:
316 * WWWWWWWWWWWRRRRRRRRRRRRRRWWWWWWWWWWWWWwRRRRRRRRRRRRRRRRR
317 * Having a max burst value of 100ms of the average will help smooth the
318 * throttling
319 */
320 min = bkt->avg / 10;
321 if (bkt->avg && !bkt->max) {
322 bkt->max = min;
323 }
324 }
325
326 /* take care of canceling a timer */
327 static void throttle_cancel_timer(QEMUTimer *timer)
328 {
329 assert(timer != NULL);
330
331 timer_del(timer);
332 }
333
334 /* Used to configure the throttle
335 *
336 * @ts: the throttle state we are working on
337 * @tt: the throttle timers we use in this aio context
338 * @cfg: the config to set
339 */
340 void throttle_config(ThrottleState *ts,
341 ThrottleTimers *tt,
342 ThrottleConfig *cfg)
343 {
344 int i;
345
346 ts->cfg = *cfg;
347
348 for (i = 0; i < BUCKETS_COUNT; i++) {
349 throttle_fix_bucket(&ts->cfg.buckets[i]);
350 }
351
352 ts->previous_leak = qemu_clock_get_ns(tt->clock_type);
353
354 for (i = 0; i < 2; i++) {
355 throttle_cancel_timer(tt->timers[i]);
356 }
357 }
358
359 /* used to get config
360 *
361 * @ts: the throttle state we are working on
362 * @cfg: the config to write
363 */
364 void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
365 {
366 *cfg = ts->cfg;
367 }
368
369
370 /* Schedule the read or write timer if needed
371 *
372 * NOTE: this function is not unit tested due to it's usage of timer_mod
373 *
374 * @tt: the timers structure
375 * @is_write: the type of operation (read/write)
376 * @ret: true if the timer has been scheduled else false
377 */
378 bool throttle_schedule_timer(ThrottleState *ts,
379 ThrottleTimers *tt,
380 bool is_write)
381 {
382 int64_t now = qemu_clock_get_ns(tt->clock_type);
383 int64_t next_timestamp;
384 bool must_wait;
385
386 must_wait = throttle_compute_timer(ts,
387 is_write,
388 now,
389 &next_timestamp);
390
391 /* request not throttled */
392 if (!must_wait) {
393 return false;
394 }
395
396 /* request throttled and timer pending -> do nothing */
397 if (timer_pending(tt->timers[is_write])) {
398 return true;
399 }
400
401 /* request throttled and timer not pending -> arm timer */
402 timer_mod(tt->timers[is_write], next_timestamp);
403 return true;
404 }
405
406 /* do the accounting for this operation
407 *
408 * @is_write: the type of operation (read/write)
409 * @size: the size of the operation
410 */
411 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
412 {
413 double units = 1.0;
414
415 /* if cfg.op_size is defined and smaller than size we compute unit count */
416 if (ts->cfg.op_size && size > ts->cfg.op_size) {
417 units = (double) size / ts->cfg.op_size;
418 }
419
420 ts->cfg.buckets[THROTTLE_BPS_TOTAL].level += size;
421 ts->cfg.buckets[THROTTLE_OPS_TOTAL].level += units;
422
423 if (is_write) {
424 ts->cfg.buckets[THROTTLE_BPS_WRITE].level += size;
425 ts->cfg.buckets[THROTTLE_OPS_WRITE].level += units;
426 } else {
427 ts->cfg.buckets[THROTTLE_BPS_READ].level += size;
428 ts->cfg.buckets[THROTTLE_OPS_READ].level += units;
429 }
430 }
431