throttle: acquire the ThrottleGroup lock in bdrv_swap()
[qemu/ar7.git] / block / throttle-groups.c
blobefc462fbc556678118760e47b4ec29e92049a48f
1 /*
2 * QEMU block throttling group infrastructure
4 * Copyright (C) Nodalink, EURL. 2014
5 * Copyright (C) Igalia, S.L. 2015
7 * Authors:
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
33 * its own locking.
35 * This locking is however handled internally in this file, so it's
36 * mostly transparent to outside users (but see the documentation in
37 * throttle_groups_lock()).
39 * The whole ThrottleGroup structure is private and invisible to
40 * outside users, that only use it through its ThrottleState.
42 * In addition to the ThrottleGroup structure, BlockDriverState has
43 * fields that need to be accessed by other members of the group and
44 * therefore also need to be protected by this lock. Once a BDS is
45 * registered in a group those fields can be accessed by other threads
46 * any time.
48 * Again, all this is handled internally and is mostly transparent to
49 * the outside. The 'throttle_timers' field however has an additional
50 * constraint because it may be temporarily invalid (see for example
51 * bdrv_set_aio_context()). Therefore in this file a thread will
52 * access some other BDS's timers only after verifying that that BDS
53 * has throttled requests in the queue.
55 typedef struct ThrottleGroup {
56 char *name; /* This is constant during the lifetime of the group */
58 QemuMutex lock; /* This lock protects the following four fields */
59 ThrottleState ts;
60 QLIST_HEAD(, BlockDriverState) head;
61 BlockDriverState *tokens[2];
62 bool any_timer_armed[2];
64 /* These two are protected by the global throttle_groups_lock */
65 unsigned refcount;
66 QTAILQ_ENTRY(ThrottleGroup) list;
67 } ThrottleGroup;
69 static QemuMutex throttle_groups_lock;
70 static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
71 QTAILQ_HEAD_INITIALIZER(throttle_groups);
73 /* Increments the reference count of a ThrottleGroup given its name.
75 * If no ThrottleGroup is found with the given name a new one is
76 * created.
78 * @name: the name of the ThrottleGroup
79 * @ret: the ThrottleGroup
81 static ThrottleGroup *throttle_group_incref(const char *name)
83 ThrottleGroup *tg = NULL;
84 ThrottleGroup *iter;
86 qemu_mutex_lock(&throttle_groups_lock);
88 /* Look for an existing group with that name */
89 QTAILQ_FOREACH(iter, &throttle_groups, list) {
90 if (!strcmp(name, iter->name)) {
91 tg = iter;
92 break;
96 /* Create a new one if not found */
97 if (!tg) {
98 tg = g_new0(ThrottleGroup, 1);
99 tg->name = g_strdup(name);
100 qemu_mutex_init(&tg->lock);
101 throttle_init(&tg->ts);
102 QLIST_INIT(&tg->head);
104 QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
107 tg->refcount++;
109 qemu_mutex_unlock(&throttle_groups_lock);
111 return tg;
114 /* Decrease the reference count of a ThrottleGroup.
116 * When the reference count reaches zero the ThrottleGroup is
117 * destroyed.
119 * @tg: The ThrottleGroup to unref
121 static void throttle_group_unref(ThrottleGroup *tg)
123 qemu_mutex_lock(&throttle_groups_lock);
124 if (--tg->refcount == 0) {
125 QTAILQ_REMOVE(&throttle_groups, tg, list);
126 qemu_mutex_destroy(&tg->lock);
127 g_free(tg->name);
128 g_free(tg);
130 qemu_mutex_unlock(&throttle_groups_lock);
133 /* Get the name from a BlockDriverState's ThrottleGroup. The name (and
134 * the pointer) is guaranteed to remain constant during the lifetime
135 * of the group.
137 * @bs: a BlockDriverState that is member of a throttling group
138 * @ret: the name of the group.
140 const char *throttle_group_get_name(BlockDriverState *bs)
142 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
143 return tg->name;
146 /* Return the next BlockDriverState in the round-robin sequence,
147 * simulating a circular list.
149 * This assumes that tg->lock is held.
151 * @bs: the current BlockDriverState
152 * @ret: the next BlockDriverState in the sequence
154 static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
156 ThrottleState *ts = bs->throttle_state;
157 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
158 BlockDriverState *next = QLIST_NEXT(bs, round_robin);
160 if (!next) {
161 return QLIST_FIRST(&tg->head);
164 return next;
167 /* Return the next BlockDriverState in the round-robin sequence with
168 * pending I/O requests.
170 * This assumes that tg->lock is held.
172 * @bs: the current BlockDriverState
173 * @is_write: the type of operation (read/write)
174 * @ret: the next BlockDriverState with pending requests, or bs
175 * if there is none.
177 static BlockDriverState *next_throttle_token(BlockDriverState *bs,
178 bool is_write)
180 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
181 BlockDriverState *token, *start;
183 start = token = tg->tokens[is_write];
185 /* get next bs round in round robin style */
186 token = throttle_group_next_bs(token);
187 while (token != start && !token->pending_reqs[is_write]) {
188 token = throttle_group_next_bs(token);
191 /* If no IO are queued for scheduling on the next round robin token
192 * then decide the token is the current bs because chances are
193 * the current bs get the current request queued.
195 if (token == start && !token->pending_reqs[is_write]) {
196 token = bs;
199 return token;
202 /* Check if the next I/O request for a BlockDriverState needs to be
203 * throttled or not. If there's no timer set in this group, set one
204 * and update the token accordingly.
206 * This assumes that tg->lock is held.
208 * @bs: the current BlockDriverState
209 * @is_write: the type of operation (read/write)
210 * @ret: whether the I/O request needs to be throttled or not
212 static bool throttle_group_schedule_timer(BlockDriverState *bs,
213 bool is_write)
215 ThrottleState *ts = bs->throttle_state;
216 ThrottleTimers *tt = &bs->throttle_timers;
217 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
218 bool must_wait;
220 /* Check if any of the timers in this group is already armed */
221 if (tg->any_timer_armed[is_write]) {
222 return true;
225 must_wait = throttle_schedule_timer(ts, tt, is_write);
227 /* If a timer just got armed, set bs as the current token */
228 if (must_wait) {
229 tg->tokens[is_write] = bs;
230 tg->any_timer_armed[is_write] = true;
233 return must_wait;
236 /* Look for the next pending I/O request and schedule it.
238 * This assumes that tg->lock is held.
240 * @bs: the current BlockDriverState
241 * @is_write: the type of operation (read/write)
243 static void schedule_next_request(BlockDriverState *bs, bool is_write)
245 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
246 bool must_wait;
247 BlockDriverState *token;
249 /* Check if there's any pending request to schedule next */
250 token = next_throttle_token(bs, is_write);
251 if (!token->pending_reqs[is_write]) {
252 return;
255 /* Set a timer for the request if it needs to be throttled */
256 must_wait = throttle_group_schedule_timer(token, is_write);
258 /* If it doesn't have to wait, queue it for immediate execution */
259 if (!must_wait) {
260 /* Give preference to requests from the current bs */
261 if (qemu_in_coroutine() &&
262 qemu_co_queue_next(&bs->throttled_reqs[is_write])) {
263 token = bs;
264 } else {
265 ThrottleTimers *tt = &token->throttle_timers;
266 int64_t now = qemu_clock_get_ns(tt->clock_type);
267 timer_mod(tt->timers[is_write], now + 1);
268 tg->any_timer_armed[is_write] = true;
270 tg->tokens[is_write] = token;
274 /* Check if an I/O request needs to be throttled, wait and set a timer
275 * if necessary, and schedule the next request using a round robin
276 * algorithm.
278 * @bs: the current BlockDriverState
279 * @bytes: the number of bytes for this I/O
280 * @is_write: the type of operation (read/write)
282 void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
283 unsigned int bytes,
284 bool is_write)
286 bool must_wait;
287 BlockDriverState *token;
289 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
290 qemu_mutex_lock(&tg->lock);
292 /* First we check if this I/O has to be throttled. */
293 token = next_throttle_token(bs, is_write);
294 must_wait = throttle_group_schedule_timer(token, is_write);
296 /* Wait if there's a timer set or queued requests of this type */
297 if (must_wait || bs->pending_reqs[is_write]) {
298 bs->pending_reqs[is_write]++;
299 qemu_mutex_unlock(&tg->lock);
300 qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
301 qemu_mutex_lock(&tg->lock);
302 bs->pending_reqs[is_write]--;
305 /* The I/O will be executed, so do the accounting */
306 throttle_account(bs->throttle_state, is_write, bytes);
308 /* Schedule the next request */
309 schedule_next_request(bs, is_write);
311 qemu_mutex_unlock(&tg->lock);
314 /* Update the throttle configuration for a particular group. Similar
315 * to throttle_config(), but guarantees atomicity within the
316 * throttling group.
318 * @bs: a BlockDriverState that is member of the group
319 * @cfg: the configuration to set
321 void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg)
323 ThrottleTimers *tt = &bs->throttle_timers;
324 ThrottleState *ts = bs->throttle_state;
325 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
326 qemu_mutex_lock(&tg->lock);
327 throttle_config(ts, tt, cfg);
328 /* throttle_config() cancels the timers */
329 tg->any_timer_armed[0] = tg->any_timer_armed[1] = false;
330 qemu_mutex_unlock(&tg->lock);
333 /* Get the throttle configuration from a particular group. Similar to
334 * throttle_get_config(), but guarantees atomicity within the
335 * throttling group.
337 * @bs: a BlockDriverState that is member of the group
338 * @cfg: the configuration will be written here
340 void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
342 ThrottleState *ts = bs->throttle_state;
343 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
344 qemu_mutex_lock(&tg->lock);
345 throttle_get_config(ts, cfg);
346 qemu_mutex_unlock(&tg->lock);
349 /* ThrottleTimers callback. This wakes up a request that was waiting
350 * because it had been throttled.
352 * @bs: the BlockDriverState whose request had been throttled
353 * @is_write: the type of operation (read/write)
355 static void timer_cb(BlockDriverState *bs, bool is_write)
357 ThrottleState *ts = bs->throttle_state;
358 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
359 bool empty_queue;
361 /* The timer has just been fired, so we can update the flag */
362 qemu_mutex_lock(&tg->lock);
363 tg->any_timer_armed[is_write] = false;
364 qemu_mutex_unlock(&tg->lock);
366 /* Run the request that was waiting for this timer */
367 empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);
369 /* If the request queue was empty then we have to take care of
370 * scheduling the next one */
371 if (empty_queue) {
372 qemu_mutex_lock(&tg->lock);
373 schedule_next_request(bs, is_write);
374 qemu_mutex_unlock(&tg->lock);
378 static void read_timer_cb(void *opaque)
380 timer_cb(opaque, false);
383 static void write_timer_cb(void *opaque)
385 timer_cb(opaque, true);
388 /* Register a BlockDriverState in the throttling group, also
389 * initializing its timers and updating its throttle_state pointer to
390 * point to it. If a throttling group with that name does not exist
391 * yet, it will be created.
393 * @bs: the BlockDriverState to insert
394 * @groupname: the name of the group
396 void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
398 int i;
399 ThrottleGroup *tg = throttle_group_incref(groupname);
400 int clock_type = QEMU_CLOCK_REALTIME;
402 if (qtest_enabled()) {
403 /* For testing block IO throttling only */
404 clock_type = QEMU_CLOCK_VIRTUAL;
407 bs->throttle_state = &tg->ts;
409 qemu_mutex_lock(&tg->lock);
410 /* If the ThrottleGroup is new set this BlockDriverState as the token */
411 for (i = 0; i < 2; i++) {
412 if (!tg->tokens[i]) {
413 tg->tokens[i] = bs;
417 QLIST_INSERT_HEAD(&tg->head, bs, round_robin);
419 throttle_timers_init(&bs->throttle_timers,
420 bdrv_get_aio_context(bs),
421 clock_type,
422 read_timer_cb,
423 write_timer_cb,
424 bs);
426 qemu_mutex_unlock(&tg->lock);
429 /* Unregister a BlockDriverState from its group, removing it from the
430 * list, destroying the timers and setting the throttle_state pointer
431 * to NULL.
433 * The group will be destroyed if it's empty after this operation.
435 * @bs: the BlockDriverState to remove
437 void throttle_group_unregister_bs(BlockDriverState *bs)
439 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
440 int i;
442 qemu_mutex_lock(&tg->lock);
443 for (i = 0; i < 2; i++) {
444 if (tg->tokens[i] == bs) {
445 BlockDriverState *token = throttle_group_next_bs(bs);
446 /* Take care of the case where this is the last bs in the group */
447 if (token == bs) {
448 token = NULL;
450 tg->tokens[i] = token;
454 /* remove the current bs from the list */
455 QLIST_REMOVE(bs, round_robin);
456 throttle_timers_destroy(&bs->throttle_timers);
457 qemu_mutex_unlock(&tg->lock);
459 throttle_group_unref(tg);
460 bs->throttle_state = NULL;
463 /* Acquire the lock of this throttling group.
465 * You won't normally need to use this. None of the functions from the
466 * ThrottleGroup API require you to acquire the lock since all of them
467 * deal with it internally.
469 * This should only be used in exceptional cases when you want to
470 * access the protected fields of a BlockDriverState directly
471 * (e.g. bdrv_swap()).
473 * @bs: a BlockDriverState that is member of the group
475 void throttle_group_lock(BlockDriverState *bs)
477 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
478 qemu_mutex_lock(&tg->lock);
481 /* Release the lock of this throttling group.
483 * See the comments in throttle_group_lock().
485 void throttle_group_unlock(BlockDriverState *bs)
487 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
488 qemu_mutex_unlock(&tg->lock);
491 static void throttle_groups_init(void)
493 qemu_mutex_init(&throttle_groups_lock);
496 block_init(throttle_groups_init);