2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, SARL. 2013
7 * BenoƮt Canet <benoit.canet@irqsave.net>
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"
25 #include "block/aio.h"
27 /* This function make a bucket leak
29 * @bkt: the bucket to make leak
30 * @delta_ns: the time delta
32 void throttle_leak_bucket(LeakyBucket
*bkt
, int64_t delta_ns
)
36 /* compute how much to leak */
37 leak
= (bkt
->avg
* (double) delta_ns
) / NANOSECONDS_PER_SECOND
;
39 /* make the bucket leak */
40 bkt
->level
= MAX(bkt
->level
- leak
, 0);
43 /* Calculate the time delta since last leak and make proportionals leaks
45 * @now: the current timestamp in ns
47 static void throttle_do_leak(ThrottleState
*ts
, int64_t now
)
49 /* compute the time elapsed since the last leak */
50 int64_t delta_ns
= now
- ts
->previous_leak
;
53 ts
->previous_leak
= now
;
59 /* make each bucket leak */
60 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
61 throttle_leak_bucket(&ts
->cfg
.buckets
[i
], delta_ns
);
65 /* do the real job of computing the time to wait
67 * @limit: the throttling limit
68 * @extra: the number of operation to delay
69 * @ret: the time to wait in ns
71 static int64_t throttle_do_compute_wait(double limit
, double extra
)
73 double wait
= extra
* NANOSECONDS_PER_SECOND
;
78 /* This function compute the wait time in ns that a leaky bucket should trigger
80 * @bkt: the leaky bucket we operate on
81 * @ret: the resulting wait time in ns or 0 if the operation can go through
83 int64_t throttle_compute_wait(LeakyBucket
*bkt
)
85 double extra
; /* the number of extra units blocking the io */
91 extra
= bkt
->level
- bkt
->max
;
97 return throttle_do_compute_wait(bkt
->avg
, extra
);
100 /* This function compute the time that must be waited while this IO
102 * @is_write: true if the current IO is a write, false if it's a read
105 static int64_t throttle_compute_wait_for(ThrottleState
*ts
,
108 BucketType to_check
[2][4] = { {THROTTLE_BPS_TOTAL
,
115 THROTTLE_OPS_WRITE
}, };
116 int64_t wait
, max_wait
= 0;
119 for (i
= 0; i
< 4; i
++) {
120 BucketType index
= to_check
[is_write
][i
];
121 wait
= throttle_compute_wait(&ts
->cfg
.buckets
[index
]);
122 if (wait
> max_wait
) {
130 /* compute the timer for this type of operation
132 * @is_write: the type of operation
133 * @now: the current clock timestamp
134 * @next_timestamp: the resulting timer
135 * @ret: true if a timer must be set
137 bool throttle_compute_timer(ThrottleState
*ts
,
140 int64_t *next_timestamp
)
144 /* leak proportionally to the time elapsed */
145 throttle_do_leak(ts
, now
);
147 /* compute the wait time if any */
148 wait
= throttle_compute_wait_for(ts
, is_write
);
150 /* if the code must wait compute when the next timer should fire */
152 *next_timestamp
= now
+ wait
;
156 /* else no need to wait at all */
157 *next_timestamp
= now
;
161 /* Add timers to event loop */
162 void throttle_attach_aio_context(ThrottleState
*ts
, AioContext
*new_context
)
164 ts
->timers
[0] = aio_timer_new(new_context
, ts
->clock_type
, SCALE_NS
,
165 ts
->read_timer_cb
, ts
->timer_opaque
);
166 ts
->timers
[1] = aio_timer_new(new_context
, ts
->clock_type
, SCALE_NS
,
167 ts
->write_timer_cb
, ts
->timer_opaque
);
170 /* To be called first on the ThrottleState */
171 void throttle_init(ThrottleState
*ts
,
172 AioContext
*aio_context
,
173 QEMUClockType clock_type
,
174 QEMUTimerCB
*read_timer_cb
,
175 QEMUTimerCB
*write_timer_cb
,
178 memset(ts
, 0, sizeof(ThrottleState
));
180 ts
->clock_type
= clock_type
;
181 ts
->read_timer_cb
= read_timer_cb
;
182 ts
->write_timer_cb
= write_timer_cb
;
183 ts
->timer_opaque
= timer_opaque
;
184 throttle_attach_aio_context(ts
, aio_context
);
187 /* destroy a timer */
188 static void throttle_timer_destroy(QEMUTimer
**timer
)
190 assert(*timer
!= NULL
);
197 /* Remove timers from event loop */
198 void throttle_detach_aio_context(ThrottleState
*ts
)
202 for (i
= 0; i
< 2; i
++) {
203 throttle_timer_destroy(&ts
->timers
[i
]);
207 /* To be called last on the ThrottleState */
208 void throttle_destroy(ThrottleState
*ts
)
210 throttle_detach_aio_context(ts
);
213 /* is any throttling timer configured */
214 bool throttle_have_timer(ThrottleState
*ts
)
223 /* Does any throttling must be done
225 * @cfg: the throttling configuration to inspect
226 * @ret: true if throttling must be done else false
228 bool throttle_enabled(ThrottleConfig
*cfg
)
232 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
233 if (cfg
->buckets
[i
].avg
> 0) {
241 /* return true if any two throttling parameters conflicts
243 * @cfg: the throttling configuration to inspect
244 * @ret: true if any conflict detected else false
246 bool throttle_conflicting(ThrottleConfig
*cfg
)
248 bool bps_flag
, ops_flag
;
249 bool bps_max_flag
, ops_max_flag
;
251 bps_flag
= cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
&&
252 (cfg
->buckets
[THROTTLE_BPS_READ
].avg
||
253 cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
);
255 ops_flag
= cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
&&
256 (cfg
->buckets
[THROTTLE_OPS_READ
].avg
||
257 cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
);
259 bps_max_flag
= cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
&&
260 (cfg
->buckets
[THROTTLE_BPS_READ
].max
||
261 cfg
->buckets
[THROTTLE_BPS_WRITE
].max
);
263 ops_max_flag
= cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
&&
264 (cfg
->buckets
[THROTTLE_OPS_READ
].max
||
265 cfg
->buckets
[THROTTLE_OPS_WRITE
].max
);
267 return bps_flag
|| ops_flag
|| bps_max_flag
|| ops_max_flag
;
270 /* check if a throttling configuration is valid
271 * @cfg: the throttling configuration to inspect
272 * @ret: true if valid else false
274 bool throttle_is_valid(ThrottleConfig
*cfg
)
276 bool invalid
= false;
279 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
280 if (cfg
->buckets
[i
].avg
< 0) {
285 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
286 if (cfg
->buckets
[i
].max
< 0) {
294 /* fix bucket parameters */
295 static void throttle_fix_bucket(LeakyBucket
*bkt
)
299 /* zero bucket level */
302 /* The following is done to cope with the Linux CFQ block scheduler
303 * which regroup reads and writes by block of 100ms in the guest.
304 * When they are two process one making reads and one making writes cfq
305 * make a pattern looking like the following:
306 * WWWWWWWWWWWRRRRRRRRRRRRRRWWWWWWWWWWWWWwRRRRRRRRRRRRRRRRR
307 * Having a max burst value of 100ms of the average will help smooth the
311 if (bkt
->avg
&& !bkt
->max
) {
316 /* take care of canceling a timer */
317 static void throttle_cancel_timer(QEMUTimer
*timer
)
319 assert(timer
!= NULL
);
324 /* Used to configure the throttle
326 * @ts: the throttle state we are working on
327 * @cfg: the config to set
329 void throttle_config(ThrottleState
*ts
, ThrottleConfig
*cfg
)
335 for (i
= 0; i
< BUCKETS_COUNT
; i
++) {
336 throttle_fix_bucket(&ts
->cfg
.buckets
[i
]);
339 ts
->previous_leak
= qemu_clock_get_ns(ts
->clock_type
);
341 for (i
= 0; i
< 2; i
++) {
342 throttle_cancel_timer(ts
->timers
[i
]);
346 /* used to get config
348 * @ts: the throttle state we are working on
349 * @cfg: the config to write
351 void throttle_get_config(ThrottleState
*ts
, ThrottleConfig
*cfg
)
357 /* Schedule the read or write timer if needed
359 * NOTE: this function is not unit tested due to it's usage of timer_mod
361 * @is_write: the type of operation (read/write)
362 * @ret: true if the timer has been scheduled else false
364 bool throttle_schedule_timer(ThrottleState
*ts
, bool is_write
)
366 int64_t now
= qemu_clock_get_ns(ts
->clock_type
);
367 int64_t next_timestamp
;
370 must_wait
= throttle_compute_timer(ts
,
375 /* request not throttled */
380 /* request throttled and timer pending -> do nothing */
381 if (timer_pending(ts
->timers
[is_write
])) {
385 /* request throttled and timer not pending -> arm timer */
386 timer_mod(ts
->timers
[is_write
], next_timestamp
);
390 /* do the accounting for this operation
392 * @is_write: the type of operation (read/write)
393 * @size: the size of the operation
395 void throttle_account(ThrottleState
*ts
, bool is_write
, uint64_t size
)
399 /* if cfg.op_size is defined and smaller than size we compute unit count */
400 if (ts
->cfg
.op_size
&& size
> ts
->cfg
.op_size
) {
401 units
= (double) size
/ ts
->cfg
.op_size
;
404 ts
->cfg
.buckets
[THROTTLE_BPS_TOTAL
].level
+= size
;
405 ts
->cfg
.buckets
[THROTTLE_OPS_TOTAL
].level
+= units
;
408 ts
->cfg
.buckets
[THROTTLE_BPS_WRITE
].level
+= size
;
409 ts
->cfg
.buckets
[THROTTLE_OPS_WRITE
].level
+= units
;
411 ts
->cfg
.buckets
[THROTTLE_BPS_READ
].level
+= size
;
412 ts
->cfg
.buckets
[THROTTLE_OPS_READ
].level
+= units
;