2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, SARL. 2013
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 or
12 * (at your option) version 3 of the License.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "qemu/throttle.h"
24 #include "qemu/timer.h"
26 /* This function make a bucket leak
28 * @bkt: the bucket to make leak
29 * @delta_ns: the time delta
31 void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta_ns)
35 /* compute how much to leak */
36 leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
38 /* make the bucket leak */
39 bkt->level = MAX(bkt->level - leak, 0);
42 /* Calculate the time delta since last leak and make proportionals leaks
44 * @now: the current timestamp in ns
46 static void throttle_do_leak(ThrottleState *ts, int64_t now)
48 /* compute the time elapsed since the last leak */
49 int64_t delta_ns = now - ts->previous_leak;
52 ts->previous_leak = now;
58 /* make each bucket leak */
59 for (i = 0; i < BUCKETS_COUNT; i++) {
60 throttle_leak_bucket(&ts->cfg.buckets[i], delta_ns);
64 /* do the real job of computing the time to wait
66 * @limit: the throttling limit
67 * @extra: the number of operation to delay
68 * @ret: the time to wait in ns
70 static int64_t throttle_do_compute_wait(double limit, double extra)
72 double wait = extra * NANOSECONDS_PER_SECOND;
77 /* This function compute the wait time in ns that a leaky bucket should trigger
79 * @bkt: the leaky bucket we operate on
80 * @ret: the resulting wait time in ns or 0 if the operation can go through
82 int64_t throttle_compute_wait(LeakyBucket *bkt)
84 double extra; /* the number of extra units blocking the io */
90 extra = bkt->level - bkt->max;
96 return throttle_do_compute_wait(bkt->avg, extra);
99 /* This function compute the time that must be waited while this IO
101 * @is_write: true if the current IO is a write, false if it's a read
104 static int64_t throttle_compute_wait_for(ThrottleState *ts,
107 BucketType to_check[2][4] = { {THROTTLE_BPS_TOTAL,
114 THROTTLE_OPS_WRITE}, };
115 int64_t wait, max_wait = 0;
118 for (i = 0; i < 4; i++) {
119 BucketType index = to_check[is_write][i];
120 wait = throttle_compute_wait(&ts->cfg.buckets[index]);
121 if (wait > max_wait) {
129 /* compute the timer for this type of operation
131 * @is_write: the type of operation
132 * @now: the current clock timestamp
133 * @next_timestamp: the resulting timer
134 * @ret: true if a timer must be set
136 bool throttle_compute_timer(ThrottleState *ts,
139 int64_t *next_timestamp)
143 /* leak proportionally to the time elapsed */
144 throttle_do_leak(ts, now);
146 /* compute the wait time if any */
147 wait = throttle_compute_wait_for(ts, is_write);
149 /* if the code must wait compute when the next timer should fire */
151 *next_timestamp = now + wait;
155 /* else no need to wait at all */
156 *next_timestamp = now;
160 /* To be called first on the ThrottleState */
161 void throttle_init(ThrottleState *ts,
162 QEMUClockType clock_type,
163 QEMUTimerCB *read_timer_cb,
164 QEMUTimerCB *write_timer_cb,
167 memset(ts, 0, sizeof(ThrottleState));
169 ts->clock_type = clock_type;
170 ts->timers[0] = timer_new_ns(clock_type, read_timer_cb, timer_opaque);
171 ts->timers[1] = timer_new_ns(clock_type, write_timer_cb, timer_opaque);
174 /* destroy a timer */
175 static void throttle_timer_destroy(QEMUTimer **timer)
177 assert(*timer != NULL);
184 /* To be called last on the ThrottleState */
185 void throttle_destroy(ThrottleState *ts)
189 for (i = 0; i < 2; i++) {
190 throttle_timer_destroy(&ts->timers[i]);
194 /* is any throttling timer configured */
195 bool throttle_have_timer(ThrottleState *ts)
204 /* Does any throttling must be done
206 * @cfg: the throttling configuration to inspect
207 * @ret: true if throttling must be done else false
209 bool throttle_enabled(ThrottleConfig *cfg)
213 for (i = 0; i < BUCKETS_COUNT; i++) {
214 if (cfg->buckets[i].avg > 0) {
222 /* return true if any two throttling parameters conflicts
224 * @cfg: the throttling configuration to inspect
225 * @ret: true if any conflict detected else false
227 bool throttle_conflicting(ThrottleConfig *cfg)
229 bool bps_flag, ops_flag;
230 bool bps_max_flag, ops_max_flag;
232 bps_flag = cfg->buckets[THROTTLE_BPS_TOTAL].avg &&
233 (cfg->buckets[THROTTLE_BPS_READ].avg ||
234 cfg->buckets[THROTTLE_BPS_WRITE].avg);
236 ops_flag = cfg->buckets[THROTTLE_OPS_TOTAL].avg &&
237 (cfg->buckets[THROTTLE_OPS_READ].avg ||
238 cfg->buckets[THROTTLE_OPS_WRITE].avg);
240 bps_max_flag = cfg->buckets[THROTTLE_BPS_TOTAL].max &&
241 (cfg->buckets[THROTTLE_BPS_READ].max ||
242 cfg->buckets[THROTTLE_BPS_WRITE].max);
244 ops_max_flag = cfg->buckets[THROTTLE_OPS_TOTAL].max &&
245 (cfg->buckets[THROTTLE_OPS_READ].max ||
246 cfg->buckets[THROTTLE_OPS_WRITE].max);
248 return bps_flag || ops_flag || bps_max_flag || ops_max_flag;
251 /* check if a throttling configuration is valid
252 * @cfg: the throttling configuration to inspect
253 * @ret: true if valid else false
255 bool throttle_is_valid(ThrottleConfig *cfg)
257 bool invalid = false;
260 for (i = 0; i < BUCKETS_COUNT; i++) {
261 if (cfg->buckets[i].avg < 0) {
266 for (i = 0; i < BUCKETS_COUNT; i++) {
267 if (cfg->buckets[i].max < 0) {
275 /* fix bucket parameters */
276 static void throttle_fix_bucket(LeakyBucket *bkt)
280 /* zero bucket level */
283 /* The following is done to cope with the Linux CFQ block scheduler
284 * which regroup reads and writes by block of 100ms in the guest.
285 * When they are two process one making reads and one making writes cfq
286 * make a pattern looking like the following:
287 * WWWWWWWWWWWRRRRRRRRRRRRRRWWWWWWWWWWWWWwRRRRRRRRRRRRRRRRR
288 * Having a max burst value of 100ms of the average will help smooth the
292 if (bkt->avg && !bkt->max) {
297 /* take care of canceling a timer */
298 static void throttle_cancel_timer(QEMUTimer *timer)
300 assert(timer != NULL);
305 /* Used to configure the throttle
307 * @ts: the throttle state we are working on
308 * @cfg: the config to set
310 void throttle_config(ThrottleState *ts, ThrottleConfig *cfg)
316 for (i = 0; i < BUCKETS_COUNT; i++) {
317 throttle_fix_bucket(&ts->cfg.buckets[i]);
320 ts->previous_leak = qemu_clock_get_ns(ts->clock_type);
322 for (i = 0; i < 2; i++) {
323 throttle_cancel_timer(ts->timers[i]);
327 /* used to get config
329 * @ts: the throttle state we are working on
330 * @cfg: the config to write
332 void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg)
338 /* Schedule the read or write timer if needed
340 * NOTE: this function is not unit tested due to it's usage of timer_mod
342 * @is_write: the type of operation (read/write)
343 * @ret: true if the timer has been scheduled else false
345 bool throttle_schedule_timer(ThrottleState *ts, bool is_write)
347 int64_t now = qemu_clock_get_ns(ts->clock_type);
348 int64_t next_timestamp;
351 must_wait = throttle_compute_timer(ts,
356 /* request not throttled */
361 /* request throttled and timer pending -> do nothing */
362 if (timer_pending(ts->timers[is_write])) {
366 /* request throttled and timer not pending -> arm timer */
367 timer_mod(ts->timers[is_write], next_timestamp);
371 /* do the accounting for this operation
373 * @is_write: the type of operation (read/write)
374 * @size: the size of the operation
376 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size)
380 /* if cfg.op_size is defined and smaller than size we compute unit count */
381 if (ts->cfg.op_size && size > ts->cfg.op_size) {
382 units = (double) size / ts->cfg.op_size;
385 ts->cfg.buckets[THROTTLE_BPS_TOTAL].level += size;
386 ts->cfg.buckets[THROTTLE_OPS_TOTAL].level += units;
389 ts->cfg.buckets[THROTTLE_BPS_WRITE].level += size;
390 ts->cfg.buckets[THROTTLE_OPS_WRITE].level += units;
392 ts->cfg.buckets[THROTTLE_BPS_READ].level += size;
393 ts->cfg.buckets[THROTTLE_OPS_READ].level += units;