xref: /openbmc/qemu/block/throttle-groups.c (revision b8fe1694)
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 
25 #include "block/throttle-groups.h"
26 #include "qemu/queue.h"
27 #include "qemu/thread.h"
28 #include "sysemu/qtest.h"
29 
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.
34  *
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()).
38  *
39  * The whole ThrottleGroup structure is private and invisible to
40  * outside users, that only use it through its ThrottleState.
41  *
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.
47  *
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.
54  */
55 typedef struct ThrottleGroup {
56     char *name; /* This is constant during the lifetime of the group */
57 
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];
63 
64     /* These two are protected by the global throttle_groups_lock */
65     unsigned refcount;
66     QTAILQ_ENTRY(ThrottleGroup) list;
67 } ThrottleGroup;
68 
69 static QemuMutex throttle_groups_lock;
70 static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
71     QTAILQ_HEAD_INITIALIZER(throttle_groups);
72 
73 /* Increments the reference count of a ThrottleGroup given its name.
74  *
75  * If no ThrottleGroup is found with the given name a new one is
76  * created.
77  *
78  * @name: the name of the ThrottleGroup
79  * @ret:  the ThrottleGroup
80  */
81 static ThrottleGroup *throttle_group_incref(const char *name)
82 {
83     ThrottleGroup *tg = NULL;
84     ThrottleGroup *iter;
85 
86     qemu_mutex_lock(&throttle_groups_lock);
87 
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;
93         }
94     }
95 
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);
103 
104         QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
105     }
106 
107     tg->refcount++;
108 
109     qemu_mutex_unlock(&throttle_groups_lock);
110 
111     return tg;
112 }
113 
114 /* Decrease the reference count of a ThrottleGroup.
115  *
116  * When the reference count reaches zero the ThrottleGroup is
117  * destroyed.
118  *
119  * @tg:  The ThrottleGroup to unref
120  */
121 static void throttle_group_unref(ThrottleGroup *tg)
122 {
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);
129     }
130     qemu_mutex_unlock(&throttle_groups_lock);
131 }
132 
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.
136  *
137  * @bs:   a BlockDriverState that is member of a throttling group
138  * @ret:  the name of the group.
139  */
140 const char *throttle_group_get_name(BlockDriverState *bs)
141 {
142     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
143     return tg->name;
144 }
145 
146 /* Return the next BlockDriverState in the round-robin sequence,
147  * simulating a circular list.
148  *
149  * This assumes that tg->lock is held.
150  *
151  * @bs:  the current BlockDriverState
152  * @ret: the next BlockDriverState in the sequence
153  */
154 static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
155 {
156     ThrottleState *ts = bs->throttle_state;
157     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
158     BlockDriverState *next = QLIST_NEXT(bs, round_robin);
159 
160     if (!next) {
161         return QLIST_FIRST(&tg->head);
162     }
163 
164     return next;
165 }
166 
167 /* Return the next BlockDriverState in the round-robin sequence with
168  * pending I/O requests.
169  *
170  * This assumes that tg->lock is held.
171  *
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.
176  */
177 static BlockDriverState *next_throttle_token(BlockDriverState *bs,
178                                              bool is_write)
179 {
180     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
181     BlockDriverState *token, *start;
182 
183     start = token = tg->tokens[is_write];
184 
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);
189     }
190 
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.
194      */
195     if (token == start && !token->pending_reqs[is_write]) {
196         token = bs;
197     }
198 
199     return token;
200 }
201 
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.
205  *
206  * This assumes that tg->lock is held.
207  *
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
211  */
212 static bool throttle_group_schedule_timer(BlockDriverState *bs,
213                                           bool is_write)
214 {
215     ThrottleState *ts = bs->throttle_state;
216     ThrottleTimers *tt = &bs->throttle_timers;
217     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
218     bool must_wait;
219 
220     /* Check if any of the timers in this group is already armed */
221     if (tg->any_timer_armed[is_write]) {
222         return true;
223     }
224 
225     must_wait = throttle_schedule_timer(ts, tt, is_write);
226 
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;
231     }
232 
233     return must_wait;
234 }
235 
236 /* Look for the next pending I/O request and schedule it.
237  *
238  * This assumes that tg->lock is held.
239  *
240  * @bs:        the current BlockDriverState
241  * @is_write:  the type of operation (read/write)
242  */
243 static void schedule_next_request(BlockDriverState *bs, bool is_write)
244 {
245     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
246     bool must_wait;
247     BlockDriverState *token;
248 
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;
253     }
254 
255     /* Set a timer for the request if it needs to be throttled */
256     must_wait = throttle_group_schedule_timer(token, is_write);
257 
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;
269         }
270         tg->tokens[is_write] = token;
271     }
272 }
273 
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.
277  *
278  * @bs:        the current BlockDriverState
279  * @bytes:     the number of bytes for this I/O
280  * @is_write:  the type of operation (read/write)
281  */
282 void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
283                                                         unsigned int bytes,
284                                                         bool is_write)
285 {
286     bool must_wait;
287     BlockDriverState *token;
288 
289     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
290     qemu_mutex_lock(&tg->lock);
291 
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);
295 
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]--;
303     }
304 
305     /* The I/O will be executed, so do the accounting */
306     throttle_account(bs->throttle_state, is_write, bytes);
307 
308     /* Schedule the next request */
309     schedule_next_request(bs, is_write);
310 
311     qemu_mutex_unlock(&tg->lock);
312 }
313 
314 /* Update the throttle configuration for a particular group. Similar
315  * to throttle_config(), but guarantees atomicity within the
316  * throttling group.
317  *
318  * @bs:  a BlockDriverState that is member of the group
319  * @cfg: the configuration to set
320  */
321 void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg)
322 {
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);
331 }
332 
333 /* Get the throttle configuration from a particular group. Similar to
334  * throttle_get_config(), but guarantees atomicity within the
335  * throttling group.
336  *
337  * @bs:  a BlockDriverState that is member of the group
338  * @cfg: the configuration will be written here
339  */
340 void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
341 {
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);
347 }
348 
349 /* ThrottleTimers callback. This wakes up a request that was waiting
350  * because it had been throttled.
351  *
352  * @bs:        the BlockDriverState whose request had been throttled
353  * @is_write:  the type of operation (read/write)
354  */
355 static void timer_cb(BlockDriverState *bs, bool is_write)
356 {
357     ThrottleState *ts = bs->throttle_state;
358     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
359     bool empty_queue;
360 
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);
365 
366     /* Run the request that was waiting for this timer */
367     empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);
368 
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);
375     }
376 }
377 
378 static void read_timer_cb(void *opaque)
379 {
380     timer_cb(opaque, false);
381 }
382 
383 static void write_timer_cb(void *opaque)
384 {
385     timer_cb(opaque, true);
386 }
387 
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.
392  *
393  * @bs:        the BlockDriverState to insert
394  * @groupname: the name of the group
395  */
396 void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
397 {
398     int i;
399     ThrottleGroup *tg = throttle_group_incref(groupname);
400     int clock_type = QEMU_CLOCK_REALTIME;
401 
402     if (qtest_enabled()) {
403         /* For testing block IO throttling only */
404         clock_type = QEMU_CLOCK_VIRTUAL;
405     }
406 
407     bs->throttle_state = &tg->ts;
408 
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;
414         }
415     }
416 
417     QLIST_INSERT_HEAD(&tg->head, bs, round_robin);
418 
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);
425 
426     qemu_mutex_unlock(&tg->lock);
427 }
428 
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.
432  *
433  * The group will be destroyed if it's empty after this operation.
434  *
435  * @bs: the BlockDriverState to remove
436  */
437 void throttle_group_unregister_bs(BlockDriverState *bs)
438 {
439     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
440     int i;
441 
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;
449             }
450             tg->tokens[i] = token;
451         }
452     }
453 
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);
458 
459     throttle_group_unref(tg);
460     bs->throttle_state = NULL;
461 }
462 
463 /* Acquire the lock of this throttling group.
464  *
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.
468  *
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()).
472  *
473  * @bs: a BlockDriverState that is member of the group
474  */
475 void throttle_group_lock(BlockDriverState *bs)
476 {
477     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
478     qemu_mutex_lock(&tg->lock);
479 }
480 
481 /* Release the lock of this throttling group.
482  *
483  * See the comments in throttle_group_lock().
484  */
485 void throttle_group_unlock(BlockDriverState *bs)
486 {
487     ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
488     qemu_mutex_unlock(&tg->lock);
489 }
490 
491 static void throttle_groups_init(void)
492 {
493     qemu_mutex_init(&throttle_groups_lock);
494 }
495 
496 block_init(throttle_groups_init);
497