2 * QEMU block throttling group infrastructure
4 * Copyright (C) Nodalink, EURL. 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 "block/throttle-groups.h"
26 #include "qemu/queue.h"
27 #include "qemu/thread.h"
28 #include "sysemu/qtest.h"
30 /* The ThrottleGroup structure (with its ThrottleState) is shared
31 * among different BlockDriverState and it's independent from
32 * AioContext, so in order to use it from different threads it needs
35 * This locking is however handled internally in this file, so it's
36 * transparent to outside users.
38 * The whole ThrottleGroup structure is private and invisible to
39 * outside users, that only use it through its ThrottleState.
41 * In addition to the ThrottleGroup structure, BlockDriverState has
42 * fields that need to be accessed by other members of the group and
43 * therefore also need to be protected by this lock. Once a BDS is
44 * registered in a group those fields can be accessed by other threads
47 * Again, all this is handled internally and is mostly transparent to
48 * the outside. The 'throttle_timers' field however has an additional
49 * constraint because it may be temporarily invalid (see for example
50 * bdrv_set_aio_context()). Therefore in this file a thread will
51 * access some other BDS's timers only after verifying that that BDS
52 * has throttled requests in the queue.
54 typedef struct ThrottleGroup
{
55 char *name
; /* This is constant during the lifetime of the group */
57 QemuMutex lock
; /* This lock protects the following four fields */
59 QLIST_HEAD(, BlockDriverState
) head
;
60 BlockDriverState
*tokens
[2];
61 bool any_timer_armed
[2];
63 /* These two are protected by the global throttle_groups_lock */
65 QTAILQ_ENTRY(ThrottleGroup
) list
;
68 static QemuMutex throttle_groups_lock
;
69 static QTAILQ_HEAD(, ThrottleGroup
) throttle_groups
=
70 QTAILQ_HEAD_INITIALIZER(throttle_groups
);
72 /* Increments the reference count of a ThrottleGroup given its name.
74 * If no ThrottleGroup is found with the given name a new one is
77 * @name: the name of the ThrottleGroup
78 * @ret: the ThrottleGroup
80 static ThrottleGroup
*throttle_group_incref(const char *name
)
82 ThrottleGroup
*tg
= NULL
;
85 qemu_mutex_lock(&throttle_groups_lock
);
87 /* Look for an existing group with that name */
88 QTAILQ_FOREACH(iter
, &throttle_groups
, list
) {
89 if (!strcmp(name
, iter
->name
)) {
95 /* Create a new one if not found */
97 tg
= g_new0(ThrottleGroup
, 1);
98 tg
->name
= g_strdup(name
);
99 qemu_mutex_init(&tg
->lock
);
100 throttle_init(&tg
->ts
);
101 QLIST_INIT(&tg
->head
);
103 QTAILQ_INSERT_TAIL(&throttle_groups
, tg
, list
);
108 qemu_mutex_unlock(&throttle_groups_lock
);
113 /* Decrease the reference count of a ThrottleGroup.
115 * When the reference count reaches zero the ThrottleGroup is
118 * @tg: The ThrottleGroup to unref
120 static void throttle_group_unref(ThrottleGroup
*tg
)
122 qemu_mutex_lock(&throttle_groups_lock
);
123 if (--tg
->refcount
== 0) {
124 QTAILQ_REMOVE(&throttle_groups
, tg
, list
);
125 qemu_mutex_destroy(&tg
->lock
);
129 qemu_mutex_unlock(&throttle_groups_lock
);
132 /* Get the name from a BlockDriverState's ThrottleGroup. The name (and
133 * the pointer) is guaranteed to remain constant during the lifetime
136 * @bs: a BlockDriverState that is member of a throttling group
137 * @ret: the name of the group.
139 const char *throttle_group_get_name(BlockDriverState
*bs
)
141 ThrottleGroup
*tg
= container_of(bs
->throttle_state
, ThrottleGroup
, ts
);
145 /* Return the next BlockDriverState in the round-robin sequence,
146 * simulating a circular list.
148 * This assumes that tg->lock is held.
150 * @bs: the current BlockDriverState
151 * @ret: the next BlockDriverState in the sequence
153 static BlockDriverState
*throttle_group_next_bs(BlockDriverState
*bs
)
155 ThrottleState
*ts
= bs
->throttle_state
;
156 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
157 BlockDriverState
*next
= QLIST_NEXT(bs
, round_robin
);
160 return QLIST_FIRST(&tg
->head
);
166 /* Return the next BlockDriverState in the round-robin sequence with
167 * pending I/O requests.
169 * This assumes that tg->lock is held.
171 * @bs: the current BlockDriverState
172 * @is_write: the type of operation (read/write)
173 * @ret: the next BlockDriverState with pending requests, or bs
176 static BlockDriverState
*next_throttle_token(BlockDriverState
*bs
,
179 ThrottleGroup
*tg
= container_of(bs
->throttle_state
, ThrottleGroup
, ts
);
180 BlockDriverState
*token
, *start
;
182 start
= token
= tg
->tokens
[is_write
];
184 /* get next bs round in round robin style */
185 token
= throttle_group_next_bs(token
);
186 while (token
!= start
&& !token
->pending_reqs
[is_write
]) {
187 token
= throttle_group_next_bs(token
);
190 /* If no IO are queued for scheduling on the next round robin token
191 * then decide the token is the current bs because chances are
192 * the current bs get the current request queued.
194 if (token
== start
&& !token
->pending_reqs
[is_write
]) {
201 /* Check if the next I/O request for a BlockDriverState needs to be
202 * throttled or not. If there's no timer set in this group, set one
203 * and update the token accordingly.
205 * This assumes that tg->lock is held.
207 * @bs: the current BlockDriverState
208 * @is_write: the type of operation (read/write)
209 * @ret: whether the I/O request needs to be throttled or not
211 static bool throttle_group_schedule_timer(BlockDriverState
*bs
,
214 ThrottleState
*ts
= bs
->throttle_state
;
215 ThrottleTimers
*tt
= &bs
->throttle_timers
;
216 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
219 /* Check if any of the timers in this group is already armed */
220 if (tg
->any_timer_armed
[is_write
]) {
224 must_wait
= throttle_schedule_timer(ts
, tt
, is_write
);
226 /* If a timer just got armed, set bs as the current token */
228 tg
->tokens
[is_write
] = bs
;
229 tg
->any_timer_armed
[is_write
] = true;
235 /* Look for the next pending I/O request and schedule it.
237 * This assumes that tg->lock is held.
239 * @bs: the current BlockDriverState
240 * @is_write: the type of operation (read/write)
242 static void schedule_next_request(BlockDriverState
*bs
, bool is_write
)
244 ThrottleGroup
*tg
= container_of(bs
->throttle_state
, ThrottleGroup
, ts
);
246 BlockDriverState
*token
;
248 /* Check if there's any pending request to schedule next */
249 token
= next_throttle_token(bs
, is_write
);
250 if (!token
->pending_reqs
[is_write
]) {
254 /* Set a timer for the request if it needs to be throttled */
255 must_wait
= throttle_group_schedule_timer(token
, is_write
);
257 /* If it doesn't have to wait, queue it for immediate execution */
259 /* Give preference to requests from the current bs */
260 if (qemu_in_coroutine() &&
261 qemu_co_queue_next(&bs
->throttled_reqs
[is_write
])) {
264 ThrottleTimers
*tt
= &token
->throttle_timers
;
265 int64_t now
= qemu_clock_get_ns(tt
->clock_type
);
266 timer_mod(tt
->timers
[is_write
], now
+ 1);
267 tg
->any_timer_armed
[is_write
] = true;
269 tg
->tokens
[is_write
] = token
;
273 /* Check if an I/O request needs to be throttled, wait and set a timer
274 * if necessary, and schedule the next request using a round robin
277 * @bs: the current BlockDriverState
278 * @bytes: the number of bytes for this I/O
279 * @is_write: the type of operation (read/write)
281 void coroutine_fn
throttle_group_co_io_limits_intercept(BlockDriverState
*bs
,
286 BlockDriverState
*token
;
288 ThrottleGroup
*tg
= container_of(bs
->throttle_state
, ThrottleGroup
, ts
);
289 qemu_mutex_lock(&tg
->lock
);
291 /* First we check if this I/O has to be throttled. */
292 token
= next_throttle_token(bs
, is_write
);
293 must_wait
= throttle_group_schedule_timer(token
, is_write
);
295 /* Wait if there's a timer set or queued requests of this type */
296 if (must_wait
|| bs
->pending_reqs
[is_write
]) {
297 bs
->pending_reqs
[is_write
]++;
298 qemu_mutex_unlock(&tg
->lock
);
299 qemu_co_queue_wait(&bs
->throttled_reqs
[is_write
]);
300 qemu_mutex_lock(&tg
->lock
);
301 bs
->pending_reqs
[is_write
]--;
304 /* The I/O will be executed, so do the accounting */
305 throttle_account(bs
->throttle_state
, is_write
, bytes
);
307 /* Schedule the next request */
308 schedule_next_request(bs
, is_write
);
310 qemu_mutex_unlock(&tg
->lock
);
313 /* Update the throttle configuration for a particular group. Similar
314 * to throttle_config(), but guarantees atomicity within the
317 * @bs: a BlockDriverState that is member of the group
318 * @cfg: the configuration to set
320 void throttle_group_config(BlockDriverState
*bs
, ThrottleConfig
*cfg
)
322 ThrottleTimers
*tt
= &bs
->throttle_timers
;
323 ThrottleState
*ts
= bs
->throttle_state
;
324 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
325 qemu_mutex_lock(&tg
->lock
);
326 throttle_config(ts
, tt
, cfg
);
327 /* throttle_config() cancels the timers */
328 tg
->any_timer_armed
[0] = tg
->any_timer_armed
[1] = false;
329 qemu_mutex_unlock(&tg
->lock
);
332 /* Get the throttle configuration from a particular group. Similar to
333 * throttle_get_config(), but guarantees atomicity within the
336 * @bs: a BlockDriverState that is member of the group
337 * @cfg: the configuration will be written here
339 void throttle_group_get_config(BlockDriverState
*bs
, ThrottleConfig
*cfg
)
341 ThrottleState
*ts
= bs
->throttle_state
;
342 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
343 qemu_mutex_lock(&tg
->lock
);
344 throttle_get_config(ts
, cfg
);
345 qemu_mutex_unlock(&tg
->lock
);
348 /* ThrottleTimers callback. This wakes up a request that was waiting
349 * because it had been throttled.
351 * @bs: the BlockDriverState whose request had been throttled
352 * @is_write: the type of operation (read/write)
354 static void timer_cb(BlockDriverState
*bs
, bool is_write
)
356 ThrottleState
*ts
= bs
->throttle_state
;
357 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
360 /* The timer has just been fired, so we can update the flag */
361 qemu_mutex_lock(&tg
->lock
);
362 tg
->any_timer_armed
[is_write
] = false;
363 qemu_mutex_unlock(&tg
->lock
);
365 /* Run the request that was waiting for this timer */
366 empty_queue
= !qemu_co_enter_next(&bs
->throttled_reqs
[is_write
]);
368 /* If the request queue was empty then we have to take care of
369 * scheduling the next one */
371 qemu_mutex_lock(&tg
->lock
);
372 schedule_next_request(bs
, is_write
);
373 qemu_mutex_unlock(&tg
->lock
);
377 static void read_timer_cb(void *opaque
)
379 timer_cb(opaque
, false);
382 static void write_timer_cb(void *opaque
)
384 timer_cb(opaque
, true);
387 /* Register a BlockDriverState in the throttling group, also
388 * initializing its timers and updating its throttle_state pointer to
389 * point to it. If a throttling group with that name does not exist
390 * yet, it will be created.
392 * @bs: the BlockDriverState to insert
393 * @groupname: the name of the group
395 void throttle_group_register_bs(BlockDriverState
*bs
, const char *groupname
)
398 ThrottleGroup
*tg
= throttle_group_incref(groupname
);
399 int clock_type
= QEMU_CLOCK_REALTIME
;
401 if (qtest_enabled()) {
402 /* For testing block IO throttling only */
403 clock_type
= QEMU_CLOCK_VIRTUAL
;
406 bs
->throttle_state
= &tg
->ts
;
408 qemu_mutex_lock(&tg
->lock
);
409 /* If the ThrottleGroup is new set this BlockDriverState as the token */
410 for (i
= 0; i
< 2; i
++) {
411 if (!tg
->tokens
[i
]) {
416 QLIST_INSERT_HEAD(&tg
->head
, bs
, round_robin
);
418 throttle_timers_init(&bs
->throttle_timers
,
419 bdrv_get_aio_context(bs
),
425 qemu_mutex_unlock(&tg
->lock
);
428 /* Unregister a BlockDriverState from its group, removing it from the
429 * list, destroying the timers and setting the throttle_state pointer
432 * The group will be destroyed if it's empty after this operation.
434 * @bs: the BlockDriverState to remove
436 void throttle_group_unregister_bs(BlockDriverState
*bs
)
438 ThrottleGroup
*tg
= container_of(bs
->throttle_state
, ThrottleGroup
, ts
);
441 qemu_mutex_lock(&tg
->lock
);
442 for (i
= 0; i
< 2; i
++) {
443 if (tg
->tokens
[i
] == bs
) {
444 BlockDriverState
*token
= throttle_group_next_bs(bs
);
445 /* Take care of the case where this is the last bs in the group */
449 tg
->tokens
[i
] = token
;
453 /* remove the current bs from the list */
454 QLIST_REMOVE(bs
, round_robin
);
455 throttle_timers_destroy(&bs
->throttle_timers
);
456 qemu_mutex_unlock(&tg
->lock
);
458 throttle_group_unref(tg
);
459 bs
->throttle_state
= NULL
;
462 static void throttle_groups_init(void)
464 qemu_mutex_init(&throttle_groups_lock
);
467 block_init(throttle_groups_init
);