]> git.proxmox.com Git - mirror_qemu.git/blame - block/throttle-groups.c
vfio: spapr: Add DMA memory preregistering (SPAPR IOMMU v2)
[mirror_qemu.git] / block / throttle-groups.c
CommitLineData
2ff1f2e3
AG
1/*
2 * QEMU block throttling group infrastructure
3 *
4 * Copyright (C) Nodalink, EURL. 2014
5 * Copyright (C) Igalia, S.L. 2015
6 *
7 * Authors:
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
10 *
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.
15 *
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.
20 *
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/>.
23 */
24
80c71a24 25#include "qemu/osdep.h"
31dce3cc 26#include "sysemu/block-backend.h"
2ff1f2e3 27#include "block/throttle-groups.h"
76f4afb4
AG
28#include "qemu/queue.h"
29#include "qemu/thread.h"
30#include "sysemu/qtest.h"
2ff1f2e3
AG
31
32/* The ThrottleGroup structure (with its ThrottleState) is shared
27ccdd52 33 * among different BlockBackends and it's independent from
2ff1f2e3
AG
34 * AioContext, so in order to use it from different threads it needs
35 * its own locking.
36 *
37 * This locking is however handled internally in this file, so it's
d87d01e1 38 * transparent to outside users.
2ff1f2e3
AG
39 *
40 * The whole ThrottleGroup structure is private and invisible to
41 * outside users, that only use it through its ThrottleState.
42 *
27ccdd52 43 * In addition to the ThrottleGroup structure, BlockBackendPublic has
2ff1f2e3 44 * fields that need to be accessed by other members of the group and
27ccdd52
KW
45 * therefore also need to be protected by this lock. Once a
46 * BlockBackend is registered in a group those fields can be accessed
47 * by other threads any time.
2ff1f2e3
AG
48 *
49 * Again, all this is handled internally and is mostly transparent to
50 * the outside. The 'throttle_timers' field however has an additional
51 * constraint because it may be temporarily invalid (see for example
52 * bdrv_set_aio_context()). Therefore in this file a thread will
27ccdd52
KW
53 * access some other BlockBackend's timers only after verifying that
54 * that BlockBackend has throttled requests in the queue.
2ff1f2e3
AG
55 */
56typedef struct ThrottleGroup {
57 char *name; /* This is constant during the lifetime of the group */
58
59 QemuMutex lock; /* This lock protects the following four fields */
60 ThrottleState ts;
31dce3cc
KW
61 QLIST_HEAD(, BlockBackendPublic) head;
62 BlockBackend *tokens[2];
2ff1f2e3
AG
63 bool any_timer_armed[2];
64
65 /* These two are protected by the global throttle_groups_lock */
66 unsigned refcount;
67 QTAILQ_ENTRY(ThrottleGroup) list;
68} ThrottleGroup;
69
70static QemuMutex throttle_groups_lock;
71static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
72 QTAILQ_HEAD_INITIALIZER(throttle_groups);
73
74/* Increments the reference count of a ThrottleGroup given its name.
75 *
76 * If no ThrottleGroup is found with the given name a new one is
77 * created.
78 *
79 * @name: the name of the ThrottleGroup
973f2ddf 80 * @ret: the ThrottleState member of the ThrottleGroup
2ff1f2e3 81 */
973f2ddf 82ThrottleState *throttle_group_incref(const char *name)
2ff1f2e3
AG
83{
84 ThrottleGroup *tg = NULL;
85 ThrottleGroup *iter;
86
87 qemu_mutex_lock(&throttle_groups_lock);
88
89 /* Look for an existing group with that name */
90 QTAILQ_FOREACH(iter, &throttle_groups, list) {
91 if (!strcmp(name, iter->name)) {
92 tg = iter;
93 break;
94 }
95 }
96
97 /* Create a new one if not found */
98 if (!tg) {
99 tg = g_new0(ThrottleGroup, 1);
100 tg->name = g_strdup(name);
101 qemu_mutex_init(&tg->lock);
102 throttle_init(&tg->ts);
103 QLIST_INIT(&tg->head);
104
105 QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
106 }
107
108 tg->refcount++;
109
110 qemu_mutex_unlock(&throttle_groups_lock);
111
973f2ddf 112 return &tg->ts;
2ff1f2e3
AG
113}
114
115/* Decrease the reference count of a ThrottleGroup.
116 *
117 * When the reference count reaches zero the ThrottleGroup is
118 * destroyed.
119 *
973f2ddf 120 * @ts: The ThrottleGroup to unref, given by its ThrottleState member
2ff1f2e3 121 */
973f2ddf 122void throttle_group_unref(ThrottleState *ts)
2ff1f2e3 123{
973f2ddf
HR
124 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
125
2ff1f2e3
AG
126 qemu_mutex_lock(&throttle_groups_lock);
127 if (--tg->refcount == 0) {
128 QTAILQ_REMOVE(&throttle_groups, tg, list);
129 qemu_mutex_destroy(&tg->lock);
130 g_free(tg->name);
131 g_free(tg);
132 }
133 qemu_mutex_unlock(&throttle_groups_lock);
134}
135
49d2165d
KW
136/* Get the name from a BlockBackend's ThrottleGroup. The name (and the pointer)
137 * is guaranteed to remain constant during the lifetime of the group.
2ff1f2e3 138 *
49d2165d 139 * @blk: a BlockBackend that is member of a throttling group
2ff1f2e3
AG
140 * @ret: the name of the group.
141 */
49d2165d 142const char *throttle_group_get_name(BlockBackend *blk)
2ff1f2e3 143{
27ccdd52
KW
144 BlockBackendPublic *blkp = blk_get_public(blk);
145 ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts);
2ff1f2e3
AG
146 return tg->name;
147}
148
31dce3cc
KW
149/* Return the next BlockBackend in the round-robin sequence, simulating a
150 * circular list.
2ff1f2e3
AG
151 *
152 * This assumes that tg->lock is held.
153 *
31dce3cc
KW
154 * @blk: the current BlockBackend
155 * @ret: the next BlockBackend in the sequence
2ff1f2e3 156 */
31dce3cc 157static BlockBackend *throttle_group_next_blk(BlockBackend *blk)
2ff1f2e3 158{
27ccdd52
KW
159 BlockBackendPublic *blkp = blk_get_public(blk);
160 ThrottleState *ts = blkp->throttle_state;
2ff1f2e3 161 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
27ccdd52 162 BlockBackendPublic *next = QLIST_NEXT(blkp, round_robin);
2ff1f2e3
AG
163
164 if (!next) {
31dce3cc 165 next = QLIST_FIRST(&tg->head);
2ff1f2e3
AG
166 }
167
31dce3cc 168 return blk_by_public(next);
2ff1f2e3
AG
169}
170
31dce3cc
KW
171/* Return the next BlockBackend in the round-robin sequence with pending I/O
172 * requests.
76f4afb4
AG
173 *
174 * This assumes that tg->lock is held.
175 *
31dce3cc 176 * @blk: the current BlockBackend
76f4afb4 177 * @is_write: the type of operation (read/write)
31dce3cc
KW
178 * @ret: the next BlockBackend with pending requests, or blk if there is
179 * none.
76f4afb4 180 */
31dce3cc 181static BlockBackend *next_throttle_token(BlockBackend *blk, bool is_write)
76f4afb4 182{
27ccdd52
KW
183 BlockBackendPublic *blkp = blk_get_public(blk);
184 ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts);
31dce3cc 185 BlockBackend *token, *start;
76f4afb4
AG
186
187 start = token = tg->tokens[is_write];
188
189 /* get next bs round in round robin style */
31dce3cc 190 token = throttle_group_next_blk(token);
27ccdd52 191 while (token != start && !blkp->pending_reqs[is_write]) {
31dce3cc 192 token = throttle_group_next_blk(token);
76f4afb4
AG
193 }
194
195 /* If no IO are queued for scheduling on the next round robin token
196 * then decide the token is the current bs because chances are
197 * the current bs get the current request queued.
198 */
27ccdd52 199 if (token == start && !blkp->pending_reqs[is_write]) {
31dce3cc 200 token = blk;
76f4afb4
AG
201 }
202
203 return token;
204}
205
31dce3cc
KW
206/* Check if the next I/O request for a BlockBackend needs to be throttled or
207 * not. If there's no timer set in this group, set one and update the token
208 * accordingly.
76f4afb4
AG
209 *
210 * This assumes that tg->lock is held.
211 *
31dce3cc 212 * @blk: the current BlockBackend
76f4afb4
AG
213 * @is_write: the type of operation (read/write)
214 * @ret: whether the I/O request needs to be throttled or not
215 */
31dce3cc 216static bool throttle_group_schedule_timer(BlockBackend *blk, bool is_write)
76f4afb4 217{
27ccdd52
KW
218 BlockBackendPublic *blkp = blk_get_public(blk);
219 ThrottleState *ts = blkp->throttle_state;
220 ThrottleTimers *tt = &blkp->throttle_timers;
76f4afb4
AG
221 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
222 bool must_wait;
223
27ccdd52 224 if (blkp->io_limits_disabled) {
ce0f1412
PB
225 return false;
226 }
227
76f4afb4
AG
228 /* Check if any of the timers in this group is already armed */
229 if (tg->any_timer_armed[is_write]) {
230 return true;
231 }
232
233 must_wait = throttle_schedule_timer(ts, tt, is_write);
234
31dce3cc 235 /* If a timer just got armed, set blk as the current token */
76f4afb4 236 if (must_wait) {
31dce3cc 237 tg->tokens[is_write] = blk;
76f4afb4
AG
238 tg->any_timer_armed[is_write] = true;
239 }
240
241 return must_wait;
242}
243
244/* Look for the next pending I/O request and schedule it.
245 *
246 * This assumes that tg->lock is held.
247 *
31dce3cc 248 * @blk: the current BlockBackend
76f4afb4
AG
249 * @is_write: the type of operation (read/write)
250 */
31dce3cc 251static void schedule_next_request(BlockBackend *blk, bool is_write)
76f4afb4 252{
27ccdd52
KW
253 BlockBackendPublic *blkp = blk_get_public(blk);
254 ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts);
76f4afb4 255 bool must_wait;
31dce3cc 256 BlockBackend *token;
76f4afb4
AG
257
258 /* Check if there's any pending request to schedule next */
31dce3cc 259 token = next_throttle_token(blk, is_write);
27ccdd52 260 if (!blkp->pending_reqs[is_write]) {
76f4afb4
AG
261 return;
262 }
263
264 /* Set a timer for the request if it needs to be throttled */
265 must_wait = throttle_group_schedule_timer(token, is_write);
266
267 /* If it doesn't have to wait, queue it for immediate execution */
268 if (!must_wait) {
27ccdd52 269 /* Give preference to requests from the current blk */
76f4afb4 270 if (qemu_in_coroutine() &&
27ccdd52 271 qemu_co_queue_next(&blkp->throttled_reqs[is_write])) {
31dce3cc 272 token = blk;
76f4afb4 273 } else {
27ccdd52 274 ThrottleTimers *tt = &blkp->throttle_timers;
76f4afb4
AG
275 int64_t now = qemu_clock_get_ns(tt->clock_type);
276 timer_mod(tt->timers[is_write], now + 1);
277 tg->any_timer_armed[is_write] = true;
278 }
279 tg->tokens[is_write] = token;
280 }
281}
282
283/* Check if an I/O request needs to be throttled, wait and set a timer
284 * if necessary, and schedule the next request using a round robin
285 * algorithm.
286 *
441565b2 287 * @blk: the current BlockBackend
76f4afb4
AG
288 * @bytes: the number of bytes for this I/O
289 * @is_write: the type of operation (read/write)
290 */
441565b2 291void coroutine_fn throttle_group_co_io_limits_intercept(BlockBackend *blk,
76f4afb4
AG
292 unsigned int bytes,
293 bool is_write)
294{
295 bool must_wait;
31dce3cc 296 BlockBackend *token;
76f4afb4 297
27ccdd52
KW
298 BlockBackendPublic *blkp = blk_get_public(blk);
299 ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts);
76f4afb4
AG
300 qemu_mutex_lock(&tg->lock);
301
302 /* First we check if this I/O has to be throttled. */
27ccdd52 303 token = next_throttle_token(blk, is_write);
76f4afb4
AG
304 must_wait = throttle_group_schedule_timer(token, is_write);
305
306 /* Wait if there's a timer set or queued requests of this type */
27ccdd52
KW
307 if (must_wait || blkp->pending_reqs[is_write]) {
308 blkp->pending_reqs[is_write]++;
76f4afb4 309 qemu_mutex_unlock(&tg->lock);
27ccdd52 310 qemu_co_queue_wait(&blkp->throttled_reqs[is_write]);
76f4afb4 311 qemu_mutex_lock(&tg->lock);
27ccdd52 312 blkp->pending_reqs[is_write]--;
76f4afb4
AG
313 }
314
315 /* The I/O will be executed, so do the accounting */
27ccdd52 316 throttle_account(blkp->throttle_state, is_write, bytes);
76f4afb4
AG
317
318 /* Schedule the next request */
27ccdd52 319 schedule_next_request(blk, is_write);
76f4afb4
AG
320
321 qemu_mutex_unlock(&tg->lock);
322}
323
27ccdd52 324void throttle_group_restart_blk(BlockBackend *blk)
a72f6414 325{
27ccdd52 326 BlockBackendPublic *blkp = blk_get_public(blk);
a72f6414
PB
327 int i;
328
329 for (i = 0; i < 2; i++) {
27ccdd52 330 while (qemu_co_enter_next(&blkp->throttled_reqs[i])) {
a72f6414
PB
331 ;
332 }
333 }
334}
335
2ff1f2e3
AG
336/* Update the throttle configuration for a particular group. Similar
337 * to throttle_config(), but guarantees atomicity within the
338 * throttling group.
339 *
97148076 340 * @blk: a BlockBackend that is a member of the group
2ff1f2e3
AG
341 * @cfg: the configuration to set
342 */
97148076 343void throttle_group_config(BlockBackend *blk, ThrottleConfig *cfg)
2ff1f2e3 344{
97148076 345 BlockBackendPublic *blkp = blk_get_public(blk);
27ccdd52
KW
346 ThrottleTimers *tt = &blkp->throttle_timers;
347 ThrottleState *ts = blkp->throttle_state;
2ff1f2e3
AG
348 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
349 qemu_mutex_lock(&tg->lock);
2ff1f2e3 350 /* throttle_config() cancels the timers */
2f388b93
AG
351 if (timer_pending(tt->timers[0])) {
352 tg->any_timer_armed[0] = false;
353 }
354 if (timer_pending(tt->timers[1])) {
355 tg->any_timer_armed[1] = false;
356 }
357 throttle_config(ts, tt, cfg);
2ff1f2e3 358 qemu_mutex_unlock(&tg->lock);
a72f6414 359
27ccdd52
KW
360 qemu_co_enter_next(&blkp->throttled_reqs[0]);
361 qemu_co_enter_next(&blkp->throttled_reqs[1]);
2ff1f2e3
AG
362}
363
364/* Get the throttle configuration from a particular group. Similar to
365 * throttle_get_config(), but guarantees atomicity within the
366 * throttling group.
367 *
97148076 368 * @blk: a BlockBackend that is a member of the group
2ff1f2e3
AG
369 * @cfg: the configuration will be written here
370 */
97148076 371void throttle_group_get_config(BlockBackend *blk, ThrottleConfig *cfg)
2ff1f2e3 372{
97148076 373 BlockBackendPublic *blkp = blk_get_public(blk);
27ccdd52 374 ThrottleState *ts = blkp->throttle_state;
2ff1f2e3
AG
375 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
376 qemu_mutex_lock(&tg->lock);
377 throttle_get_config(ts, cfg);
378 qemu_mutex_unlock(&tg->lock);
379}
380
76f4afb4
AG
381/* ThrottleTimers callback. This wakes up a request that was waiting
382 * because it had been throttled.
383 *
27ccdd52 384 * @blk: the BlockBackend whose request had been throttled
76f4afb4
AG
385 * @is_write: the type of operation (read/write)
386 */
27ccdd52 387static void timer_cb(BlockBackend *blk, bool is_write)
76f4afb4 388{
27ccdd52
KW
389 BlockBackendPublic *blkp = blk_get_public(blk);
390 ThrottleState *ts = blkp->throttle_state;
76f4afb4
AG
391 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
392 bool empty_queue;
393
394 /* The timer has just been fired, so we can update the flag */
395 qemu_mutex_lock(&tg->lock);
396 tg->any_timer_armed[is_write] = false;
397 qemu_mutex_unlock(&tg->lock);
398
399 /* Run the request that was waiting for this timer */
27ccdd52 400 empty_queue = !qemu_co_enter_next(&blkp->throttled_reqs[is_write]);
76f4afb4
AG
401
402 /* If the request queue was empty then we have to take care of
403 * scheduling the next one */
404 if (empty_queue) {
405 qemu_mutex_lock(&tg->lock);
27ccdd52 406 schedule_next_request(blk, is_write);
76f4afb4
AG
407 qemu_mutex_unlock(&tg->lock);
408 }
409}
410
411static void read_timer_cb(void *opaque)
412{
413 timer_cb(opaque, false);
414}
415
416static void write_timer_cb(void *opaque)
417{
418 timer_cb(opaque, true);
419}
420
31dce3cc
KW
421/* Register a BlockBackend in the throttling group, also initializing its
422 * timers and updating its throttle_state pointer to point to it. If a
423 * throttling group with that name does not exist yet, it will be created.
2ff1f2e3 424 *
31dce3cc 425 * @blk: the BlockBackend to insert
2ff1f2e3
AG
426 * @groupname: the name of the group
427 */
31dce3cc 428void throttle_group_register_blk(BlockBackend *blk, const char *groupname)
2ff1f2e3
AG
429{
430 int i;
27ccdd52 431 BlockBackendPublic *blkp = blk_get_public(blk);
973f2ddf
HR
432 ThrottleState *ts = throttle_group_incref(groupname);
433 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
76f4afb4
AG
434 int clock_type = QEMU_CLOCK_REALTIME;
435
436 if (qtest_enabled()) {
437 /* For testing block IO throttling only */
438 clock_type = QEMU_CLOCK_VIRTUAL;
439 }
2ff1f2e3 440
27ccdd52 441 blkp->throttle_state = ts;
2ff1f2e3
AG
442
443 qemu_mutex_lock(&tg->lock);
31dce3cc 444 /* If the ThrottleGroup is new set this BlockBackend as the token */
2ff1f2e3
AG
445 for (i = 0; i < 2; i++) {
446 if (!tg->tokens[i]) {
31dce3cc 447 tg->tokens[i] = blk;
2ff1f2e3
AG
448 }
449 }
450
27ccdd52 451 QLIST_INSERT_HEAD(&tg->head, blkp, round_robin);
76f4afb4 452
27ccdd52
KW
453 throttle_timers_init(&blkp->throttle_timers,
454 blk_get_aio_context(blk),
76f4afb4
AG
455 clock_type,
456 read_timer_cb,
457 write_timer_cb,
27ccdd52 458 blk);
76f4afb4 459
2ff1f2e3
AG
460 qemu_mutex_unlock(&tg->lock);
461}
462
31dce3cc
KW
463/* Unregister a BlockBackend from its group, removing it from the list,
464 * destroying the timers and setting the throttle_state pointer to NULL.
2ff1f2e3 465 *
31dce3cc
KW
466 * The BlockBackend must not have pending throttled requests, so the caller has
467 * to drain them first.
5ac72418 468 *
2ff1f2e3
AG
469 * The group will be destroyed if it's empty after this operation.
470 *
31dce3cc 471 * @blk: the BlockBackend to remove
2ff1f2e3 472 */
31dce3cc 473void throttle_group_unregister_blk(BlockBackend *blk)
2ff1f2e3 474{
27ccdd52
KW
475 BlockBackendPublic *blkp = blk_get_public(blk);
476 ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts);
2ff1f2e3
AG
477 int i;
478
27ccdd52
KW
479 assert(blkp->pending_reqs[0] == 0 && blkp->pending_reqs[1] == 0);
480 assert(qemu_co_queue_empty(&blkp->throttled_reqs[0]));
481 assert(qemu_co_queue_empty(&blkp->throttled_reqs[1]));
5ac72418 482
2ff1f2e3
AG
483 qemu_mutex_lock(&tg->lock);
484 for (i = 0; i < 2; i++) {
31dce3cc
KW
485 if (tg->tokens[i] == blk) {
486 BlockBackend *token = throttle_group_next_blk(blk);
27ccdd52 487 /* Take care of the case where this is the last blk in the group */
31dce3cc 488 if (token == blk) {
2ff1f2e3
AG
489 token = NULL;
490 }
491 tg->tokens[i] = token;
492 }
493 }
494
27ccdd52
KW
495 /* remove the current blk from the list */
496 QLIST_REMOVE(blkp, round_robin);
497 throttle_timers_destroy(&blkp->throttle_timers);
2ff1f2e3
AG
498 qemu_mutex_unlock(&tg->lock);
499
973f2ddf 500 throttle_group_unref(&tg->ts);
27ccdd52 501 blkp->throttle_state = NULL;
2ff1f2e3
AG
502}
503
504static void throttle_groups_init(void)
505{
506 qemu_mutex_init(&throttle_groups_lock);
507}
508
509block_init(throttle_groups_init);