2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, EURL. 2013-2014
5 * Copyright (C) Igalia, S.L. 2015
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
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.
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.
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/>.
25 #include "qemu/osdep.h"
26 #include "qemu/throttle.h"
27 #include "qemu/timer.h"
28 #include "block/aio.h"
30 /* This function make a bucket leak
32 * @bkt: the bucket to make leak
33 * @delta_ns: the time delta
35 void throttle_leak_bucket(LeakyBucket
*bkt
, int64_t delta_ns
)
39 /* compute how much to leak */
40 leak
= (bkt
->avg
* (double) delta_ns
) / NANOSECONDS_PER_SECOND
;
42 /* make the bucket leak */
43 bkt
->level
= MAX(bkt
->level
- leak
, 0);
46 /* Calculate the time delta since last leak and make proportionals leaks
48 * @now: the current timestamp in ns
50 static void throttle_do_leak(ThrottleState
*ts
, int64_t now
)
52 /* compute the time elapsed since the last leak */
53 int64_t delta_ns
= now
- ts
->previous_leak
;
56 ts
->previous_leak
= now
;
62 /* make each bucket leak */
63 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
64 throttle_leak_bucket(&ts
->cfg
.buckets
[i
], delta_ns
);
68 /* do the real job of computing the time to wait
70 * @limit: the throttling limit
71 * @extra: the number of operation to delay
72 * @ret: the time to wait in ns
74 static int64_t throttle_do_compute_wait(double limit
, double extra
)
76 double wait
= extra
* NANOSECONDS_PER_SECOND
;
81 /* This function compute the wait time in ns that a leaky bucket should trigger
83 * @bkt: the leaky bucket we operate on
84 * @ret: the resulting wait time in ns or 0 if the operation can go through
86 int64_t throttle_compute_wait(LeakyBucket
*bkt
)
88 double extra
; /* the number of extra units blocking the io */
94 extra
= bkt
->level
- bkt
->max
;
100 return throttle_do_compute_wait(bkt
->avg
, extra
);
103 /* This function compute the time that must be waited while this IO
105 * @is_write: true if the current IO is a write, false if it's a read
108 static int64_t throttle_compute_wait_for(ThrottleState
*ts
,
111 BucketType to_check
[2][4] = { {THROTTLE_BPS_TOTAL
,
118 THROTTLE_OPS_WRITE
}, };
119 int64_t wait
, max_wait
= 0;
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
) {
133 /* compute the timer for this type of operation
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
140 static bool throttle_compute_timer(ThrottleState
*ts
,
143 int64_t *next_timestamp
)
147 /* leak proportionally to the time elapsed */
148 throttle_do_leak(ts
, now
);
150 /* compute the wait time if any */
151 wait
= throttle_compute_wait_for(ts
, is_write
);
153 /* if the code must wait compute when the next timer should fire */
155 *next_timestamp
= now
+ wait
;
159 /* else no need to wait at all */
160 *next_timestamp
= now
;
164 /* Add timers to event loop */
165 void throttle_timers_attach_aio_context(ThrottleTimers
*tt
,
166 AioContext
*new_context
)
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
);
174 /* To be called first on the ThrottleState */
175 void throttle_init(ThrottleState
*ts
)
177 memset(ts
, 0, sizeof(ThrottleState
));
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
,
188 memset(tt
, 0, sizeof(ThrottleTimers
));
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
);
197 /* destroy a timer */
198 static void throttle_timer_destroy(QEMUTimer
**timer
)
200 assert(*timer
!= NULL
);
207 /* Remove timers from event loop */
208 void throttle_timers_detach_aio_context(ThrottleTimers
*tt
)
212 for (i
= 0; i
< 2; i
++) {
213 throttle_timer_destroy(&tt
->timers
[i
]);
217 /* To be called last on the ThrottleTimers */
218 void throttle_timers_destroy(ThrottleTimers
*tt
)
220 throttle_timers_detach_aio_context(tt
);
223 /* is any throttling timer configured */
224 bool throttle_timers_are_initialized(ThrottleTimers
*tt
)
233 /* Does any throttling must be done
235 * @cfg: the throttling configuration to inspect
236 * @ret: true if throttling must be done else false
238 bool throttle_enabled(ThrottleConfig
*cfg
)
242 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
243 if (cfg
->buckets
[i
].avg
> 0) {
251 /* return true if any two throttling parameters conflicts
253 * @cfg: the throttling configuration to inspect
254 * @ret: true if any conflict detected else false
255 * @errp: error object
257 bool throttle_conflicting(ThrottleConfig
*cfg
, Error
**errp
)
259 bool bps_flag
, ops_flag
;
260 bool bps_max_flag
, ops_max_flag
;
262 bps_flag
= cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
&&
263 (cfg
->buckets
[THROTTLE_BPS_READ
].avg
||
264 cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
);
266 ops_flag
= cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
&&
267 (cfg
->buckets
[THROTTLE_OPS_READ
].avg
||
268 cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
);
270 bps_max_flag
= cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
&&
271 (cfg
->buckets
[THROTTLE_BPS_READ
].max
||
272 cfg
->buckets
[THROTTLE_BPS_WRITE
].max
);
274 ops_max_flag
= cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
&&
275 (cfg
->buckets
[THROTTLE_OPS_READ
].max
||
276 cfg
->buckets
[THROTTLE_OPS_WRITE
].max
);
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");
287 /* check if a throttling configuration is valid
288 * @cfg: the throttling configuration to inspect
289 * @ret: true if valid else false
290 * @errp: error object
292 bool throttle_is_valid(ThrottleConfig
*cfg
, Error
**errp
)
296 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
297 if (cfg
->buckets
[i
].avg
< 0 ||
298 cfg
->buckets
[i
].max
< 0 ||
299 cfg
->buckets
[i
].avg
> THROTTLE_VALUE_MAX
||
300 cfg
->buckets
[i
].max
> THROTTLE_VALUE_MAX
) {
301 error_setg(errp
, "bps/iops/max values must be within [0, %lld]",
310 /* check if bps_max/iops_max is used without bps/iops
311 * @cfg: the throttling configuration to inspect
312 * @errp: error object
314 bool throttle_max_is_missing_limit(ThrottleConfig
*cfg
, Error
**errp
)
318 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
319 if (cfg
->buckets
[i
].max
&& !cfg
->buckets
[i
].avg
) {
320 error_setg(errp
, "bps_max/iops_max require corresponding"
328 /* fix bucket parameters */
329 static void throttle_fix_bucket(LeakyBucket
*bkt
)
333 /* zero bucket level */
336 /* The following is done to cope with the Linux CFQ block scheduler
337 * which regroup reads and writes by block of 100ms in the guest.
338 * When they are two process one making reads and one making writes cfq
339 * make a pattern looking like the following:
340 * WWWWWWWWWWWRRRRRRRRRRRRRRWWWWWWWWWWWWWwRRRRRRRRRRRRRRRRR
341 * Having a max burst value of 100ms of the average will help smooth the
345 if (bkt
->avg
&& !bkt
->max
) {
350 /* take care of canceling a timer */
351 static void throttle_cancel_timer(QEMUTimer
*timer
)
353 assert(timer
!= NULL
);
358 /* Used to configure the throttle
360 * @ts: the throttle state we are working on
361 * @tt: the throttle timers we use in this aio context
362 * @cfg: the config to set
364 void throttle_config(ThrottleState
*ts
,
372 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
373 throttle_fix_bucket(&ts
->cfg
.buckets
[i
]);
376 ts
->previous_leak
= qemu_clock_get_ns(tt
->clock_type
);
378 for (i
= 0; i
< 2; i
++) {
379 throttle_cancel_timer(tt
->timers
[i
]);
383 /* used to get config
385 * @ts: the throttle state we are working on
386 * @cfg: the config to write
388 void throttle_get_config(ThrottleState
*ts
, ThrottleConfig
*cfg
)
394 /* Schedule the read or write timer if needed
396 * NOTE: this function is not unit tested due to it's usage of timer_mod
398 * @tt: the timers structure
399 * @is_write: the type of operation (read/write)
400 * @ret: true if the timer has been scheduled else false
402 bool throttle_schedule_timer(ThrottleState
*ts
,
406 int64_t now
= qemu_clock_get_ns(tt
->clock_type
);
407 int64_t next_timestamp
;
410 must_wait
= throttle_compute_timer(ts
,
415 /* request not throttled */
420 /* request throttled and timer pending -> do nothing */
421 if (timer_pending(tt
->timers
[is_write
])) {
425 /* request throttled and timer not pending -> arm timer */
426 timer_mod(tt
->timers
[is_write
], next_timestamp
);
430 /* do the accounting for this operation
432 * @is_write: the type of operation (read/write)
433 * @size: the size of the operation
435 void throttle_account(ThrottleState
*ts
, bool is_write
, uint64_t size
)
439 /* if cfg.op_size is defined and smaller than size we compute unit count */
440 if (ts
->cfg
.op_size
&& size
> ts
->cfg
.op_size
) {
441 units
= (double) size
/ ts
->cfg
.op_size
;
444 ts
->cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
+= size
;
445 ts
->cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
+= units
;
448 ts
->cfg
.buckets
[THROTTLE_BPS_WRITE
].level
+= size
;
449 ts
->cfg
.buckets
[THROTTLE_OPS_WRITE
].level
+= units
;
451 ts
->cfg
.buckets
[THROTTLE_BPS_READ
].level
+= size
;
452 ts
->cfg
.buckets
[THROTTLE_OPS_READ
].level
+= units
;