Add qemu_savevm_state_complete_postcopy
[qemu/cris-port.git] / block / throttle-groups.c
blob3419af7d96f4b29277e2257a87c0bf4dba710146
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 * 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
45 * any time.
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 */
58 ThrottleState ts;
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 */
64 unsigned refcount;
65 QTAILQ_ENTRY(ThrottleGroup) list;
66 } ThrottleGroup;
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
75 * created.
77 * @name: the name of the ThrottleGroup
78 * @ret: the ThrottleState member of the ThrottleGroup
80 ThrottleState *throttle_group_incref(const char *name)
82 ThrottleGroup *tg = NULL;
83 ThrottleGroup *iter;
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)) {
90 tg = iter;
91 break;
95 /* Create a new one if not found */
96 if (!tg) {
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);
106 tg->refcount++;
108 qemu_mutex_unlock(&throttle_groups_lock);
110 return &tg->ts;
113 /* Decrease the reference count of a ThrottleGroup.
115 * When the reference count reaches zero the ThrottleGroup is
116 * destroyed.
118 * @ts: The ThrottleGroup to unref, given by its ThrottleState member
120 void throttle_group_unref(ThrottleState *ts)
122 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
124 qemu_mutex_lock(&throttle_groups_lock);
125 if (--tg->refcount == 0) {
126 QTAILQ_REMOVE(&throttle_groups, tg, list);
127 qemu_mutex_destroy(&tg->lock);
128 g_free(tg->name);
129 g_free(tg);
131 qemu_mutex_unlock(&throttle_groups_lock);
134 /* Get the name from a BlockDriverState's ThrottleGroup. The name (and
135 * the pointer) is guaranteed to remain constant during the lifetime
136 * of the group.
138 * @bs: a BlockDriverState that is member of a throttling group
139 * @ret: the name of the group.
141 const char *throttle_group_get_name(BlockDriverState *bs)
143 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
144 return tg->name;
147 /* Return the next BlockDriverState in the round-robin sequence,
148 * simulating a circular list.
150 * This assumes that tg->lock is held.
152 * @bs: the current BlockDriverState
153 * @ret: the next BlockDriverState in the sequence
155 static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
157 ThrottleState *ts = bs->throttle_state;
158 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
159 BlockDriverState *next = QLIST_NEXT(bs, round_robin);
161 if (!next) {
162 return QLIST_FIRST(&tg->head);
165 return next;
168 /* Return the next BlockDriverState in the round-robin sequence with
169 * pending I/O requests.
171 * This assumes that tg->lock is held.
173 * @bs: the current BlockDriverState
174 * @is_write: the type of operation (read/write)
175 * @ret: the next BlockDriverState with pending requests, or bs
176 * if there is none.
178 static BlockDriverState *next_throttle_token(BlockDriverState *bs,
179 bool is_write)
181 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
182 BlockDriverState *token, *start;
184 start = token = tg->tokens[is_write];
186 /* get next bs round in round robin style */
187 token = throttle_group_next_bs(token);
188 while (token != start && !token->pending_reqs[is_write]) {
189 token = throttle_group_next_bs(token);
192 /* If no IO are queued for scheduling on the next round robin token
193 * then decide the token is the current bs because chances are
194 * the current bs get the current request queued.
196 if (token == start && !token->pending_reqs[is_write]) {
197 token = bs;
200 return token;
203 /* Check if the next I/O request for a BlockDriverState needs to be
204 * throttled or not. If there's no timer set in this group, set one
205 * and update the token accordingly.
207 * This assumes that tg->lock is held.
209 * @bs: the current BlockDriverState
210 * @is_write: the type of operation (read/write)
211 * @ret: whether the I/O request needs to be throttled or not
213 static bool throttle_group_schedule_timer(BlockDriverState *bs,
214 bool is_write)
216 ThrottleState *ts = bs->throttle_state;
217 ThrottleTimers *tt = &bs->throttle_timers;
218 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
219 bool must_wait;
221 /* Check if any of the timers in this group is already armed */
222 if (tg->any_timer_armed[is_write]) {
223 return true;
226 must_wait = throttle_schedule_timer(ts, tt, is_write);
228 /* If a timer just got armed, set bs as the current token */
229 if (must_wait) {
230 tg->tokens[is_write] = bs;
231 tg->any_timer_armed[is_write] = true;
234 return must_wait;
237 /* Look for the next pending I/O request and schedule it.
239 * This assumes that tg->lock is held.
241 * @bs: the current BlockDriverState
242 * @is_write: the type of operation (read/write)
244 static void schedule_next_request(BlockDriverState *bs, bool is_write)
246 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
247 bool must_wait;
248 BlockDriverState *token;
250 /* Check if there's any pending request to schedule next */
251 token = next_throttle_token(bs, is_write);
252 if (!token->pending_reqs[is_write]) {
253 return;
256 /* Set a timer for the request if it needs to be throttled */
257 must_wait = throttle_group_schedule_timer(token, is_write);
259 /* If it doesn't have to wait, queue it for immediate execution */
260 if (!must_wait) {
261 /* Give preference to requests from the current bs */
262 if (qemu_in_coroutine() &&
263 qemu_co_queue_next(&bs->throttled_reqs[is_write])) {
264 token = bs;
265 } else {
266 ThrottleTimers *tt = &token->throttle_timers;
267 int64_t now = qemu_clock_get_ns(tt->clock_type);
268 timer_mod(tt->timers[is_write], now + 1);
269 tg->any_timer_armed[is_write] = true;
271 tg->tokens[is_write] = token;
275 /* Check if an I/O request needs to be throttled, wait and set a timer
276 * if necessary, and schedule the next request using a round robin
277 * algorithm.
279 * @bs: the current BlockDriverState
280 * @bytes: the number of bytes for this I/O
281 * @is_write: the type of operation (read/write)
283 void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
284 unsigned int bytes,
285 bool is_write)
287 bool must_wait;
288 BlockDriverState *token;
290 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
291 qemu_mutex_lock(&tg->lock);
293 /* First we check if this I/O has to be throttled. */
294 token = next_throttle_token(bs, is_write);
295 must_wait = throttle_group_schedule_timer(token, is_write);
297 /* Wait if there's a timer set or queued requests of this type */
298 if (must_wait || bs->pending_reqs[is_write]) {
299 bs->pending_reqs[is_write]++;
300 qemu_mutex_unlock(&tg->lock);
301 qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
302 qemu_mutex_lock(&tg->lock);
303 bs->pending_reqs[is_write]--;
306 /* The I/O will be executed, so do the accounting */
307 throttle_account(bs->throttle_state, is_write, bytes);
309 /* Schedule the next request */
310 schedule_next_request(bs, is_write);
312 qemu_mutex_unlock(&tg->lock);
315 /* Update the throttle configuration for a particular group. Similar
316 * to throttle_config(), but guarantees atomicity within the
317 * throttling group.
319 * @bs: a BlockDriverState that is member of the group
320 * @cfg: the configuration to set
322 void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg)
324 ThrottleTimers *tt = &bs->throttle_timers;
325 ThrottleState *ts = bs->throttle_state;
326 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
327 qemu_mutex_lock(&tg->lock);
328 /* throttle_config() cancels the timers */
329 if (timer_pending(tt->timers[0])) {
330 tg->any_timer_armed[0] = false;
332 if (timer_pending(tt->timers[1])) {
333 tg->any_timer_armed[1] = false;
335 throttle_config(ts, tt, cfg);
336 qemu_mutex_unlock(&tg->lock);
339 /* Get the throttle configuration from a particular group. Similar to
340 * throttle_get_config(), but guarantees atomicity within the
341 * throttling group.
343 * @bs: a BlockDriverState that is member of the group
344 * @cfg: the configuration will be written here
346 void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
348 ThrottleState *ts = bs->throttle_state;
349 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
350 qemu_mutex_lock(&tg->lock);
351 throttle_get_config(ts, cfg);
352 qemu_mutex_unlock(&tg->lock);
355 /* ThrottleTimers callback. This wakes up a request that was waiting
356 * because it had been throttled.
358 * @bs: the BlockDriverState whose request had been throttled
359 * @is_write: the type of operation (read/write)
361 static void timer_cb(BlockDriverState *bs, bool is_write)
363 ThrottleState *ts = bs->throttle_state;
364 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
365 bool empty_queue;
367 /* The timer has just been fired, so we can update the flag */
368 qemu_mutex_lock(&tg->lock);
369 tg->any_timer_armed[is_write] = false;
370 qemu_mutex_unlock(&tg->lock);
372 /* Run the request that was waiting for this timer */
373 empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);
375 /* If the request queue was empty then we have to take care of
376 * scheduling the next one */
377 if (empty_queue) {
378 qemu_mutex_lock(&tg->lock);
379 schedule_next_request(bs, is_write);
380 qemu_mutex_unlock(&tg->lock);
384 static void read_timer_cb(void *opaque)
386 timer_cb(opaque, false);
389 static void write_timer_cb(void *opaque)
391 timer_cb(opaque, true);
394 /* Register a BlockDriverState in the throttling group, also
395 * initializing its timers and updating its throttle_state pointer to
396 * point to it. If a throttling group with that name does not exist
397 * yet, it will be created.
399 * @bs: the BlockDriverState to insert
400 * @groupname: the name of the group
402 void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
404 int i;
405 ThrottleState *ts = throttle_group_incref(groupname);
406 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
407 int clock_type = QEMU_CLOCK_REALTIME;
409 if (qtest_enabled()) {
410 /* For testing block IO throttling only */
411 clock_type = QEMU_CLOCK_VIRTUAL;
414 bs->throttle_state = ts;
416 qemu_mutex_lock(&tg->lock);
417 /* If the ThrottleGroup is new set this BlockDriverState as the token */
418 for (i = 0; i < 2; i++) {
419 if (!tg->tokens[i]) {
420 tg->tokens[i] = bs;
424 QLIST_INSERT_HEAD(&tg->head, bs, round_robin);
426 throttle_timers_init(&bs->throttle_timers,
427 bdrv_get_aio_context(bs),
428 clock_type,
429 read_timer_cb,
430 write_timer_cb,
431 bs);
433 qemu_mutex_unlock(&tg->lock);
436 /* Unregister a BlockDriverState from its group, removing it from the
437 * list, destroying the timers and setting the throttle_state pointer
438 * to NULL.
440 * The group will be destroyed if it's empty after this operation.
442 * @bs: the BlockDriverState to remove
444 void throttle_group_unregister_bs(BlockDriverState *bs)
446 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
447 int i;
449 qemu_mutex_lock(&tg->lock);
450 for (i = 0; i < 2; i++) {
451 if (tg->tokens[i] == bs) {
452 BlockDriverState *token = throttle_group_next_bs(bs);
453 /* Take care of the case where this is the last bs in the group */
454 if (token == bs) {
455 token = NULL;
457 tg->tokens[i] = token;
461 /* remove the current bs from the list */
462 QLIST_REMOVE(bs, round_robin);
463 throttle_timers_destroy(&bs->throttle_timers);
464 qemu_mutex_unlock(&tg->lock);
466 throttle_group_unref(&tg->ts);
467 bs->throttle_state = NULL;
470 static void throttle_groups_init(void)
472 qemu_mutex_init(&throttle_groups_lock);
475 block_init(throttle_groups_init);