target-tilegx: Implement complex multiply instructions
[qemu/ar7.git] / block / throttle-groups.c
blob1abc6fcaea73690aeb5f175dfcc7122471c4061d
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() cancels the timers */
328 if (timer_pending(tt->timers[0])) {
329 tg->any_timer_armed[0] = false;
331 if (timer_pending(tt->timers[1])) {
332 tg->any_timer_armed[1] = false;
334 throttle_config(ts, tt, cfg);
335 qemu_mutex_unlock(&tg->lock);
338 /* Get the throttle configuration from a particular group. Similar to
339 * throttle_get_config(), but guarantees atomicity within the
340 * throttling group.
342 * @bs: a BlockDriverState that is member of the group
343 * @cfg: the configuration will be written here
345 void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
347 ThrottleState *ts = bs->throttle_state;
348 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
349 qemu_mutex_lock(&tg->lock);
350 throttle_get_config(ts, cfg);
351 qemu_mutex_unlock(&tg->lock);
354 /* ThrottleTimers callback. This wakes up a request that was waiting
355 * because it had been throttled.
357 * @bs: the BlockDriverState whose request had been throttled
358 * @is_write: the type of operation (read/write)
360 static void timer_cb(BlockDriverState *bs, bool is_write)
362 ThrottleState *ts = bs->throttle_state;
363 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
364 bool empty_queue;
366 /* The timer has just been fired, so we can update the flag */
367 qemu_mutex_lock(&tg->lock);
368 tg->any_timer_armed[is_write] = false;
369 qemu_mutex_unlock(&tg->lock);
371 /* Run the request that was waiting for this timer */
372 empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);
374 /* If the request queue was empty then we have to take care of
375 * scheduling the next one */
376 if (empty_queue) {
377 qemu_mutex_lock(&tg->lock);
378 schedule_next_request(bs, is_write);
379 qemu_mutex_unlock(&tg->lock);
383 static void read_timer_cb(void *opaque)
385 timer_cb(opaque, false);
388 static void write_timer_cb(void *opaque)
390 timer_cb(opaque, true);
393 /* Register a BlockDriverState in the throttling group, also
394 * initializing its timers and updating its throttle_state pointer to
395 * point to it. If a throttling group with that name does not exist
396 * yet, it will be created.
398 * @bs: the BlockDriverState to insert
399 * @groupname: the name of the group
401 void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
403 int i;
404 ThrottleGroup *tg = throttle_group_incref(groupname);
405 int clock_type = QEMU_CLOCK_REALTIME;
407 if (qtest_enabled()) {
408 /* For testing block IO throttling only */
409 clock_type = QEMU_CLOCK_VIRTUAL;
412 bs->throttle_state = &tg->ts;
414 qemu_mutex_lock(&tg->lock);
415 /* If the ThrottleGroup is new set this BlockDriverState as the token */
416 for (i = 0; i < 2; i++) {
417 if (!tg->tokens[i]) {
418 tg->tokens[i] = bs;
422 QLIST_INSERT_HEAD(&tg->head, bs, round_robin);
424 throttle_timers_init(&bs->throttle_timers,
425 bdrv_get_aio_context(bs),
426 clock_type,
427 read_timer_cb,
428 write_timer_cb,
429 bs);
431 qemu_mutex_unlock(&tg->lock);
434 /* Unregister a BlockDriverState from its group, removing it from the
435 * list, destroying the timers and setting the throttle_state pointer
436 * to NULL.
438 * The group will be destroyed if it's empty after this operation.
440 * @bs: the BlockDriverState to remove
442 void throttle_group_unregister_bs(BlockDriverState *bs)
444 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
445 int i;
447 qemu_mutex_lock(&tg->lock);
448 for (i = 0; i < 2; i++) {
449 if (tg->tokens[i] == bs) {
450 BlockDriverState *token = throttle_group_next_bs(bs);
451 /* Take care of the case where this is the last bs in the group */
452 if (token == bs) {
453 token = NULL;
455 tg->tokens[i] = token;
459 /* remove the current bs from the list */
460 QLIST_REMOVE(bs, round_robin);
461 throttle_timers_destroy(&bs->throttle_timers);
462 qemu_mutex_unlock(&tg->lock);
464 throttle_group_unref(tg);
465 bs->throttle_state = NULL;
468 /* Acquire the lock of this throttling group.
470 * You won't normally need to use this. None of the functions from the
471 * ThrottleGroup API require you to acquire the lock since all of them
472 * deal with it internally.
474 * This should only be used in exceptional cases when you want to
475 * access the protected fields of a BlockDriverState directly
476 * (e.g. bdrv_swap()).
478 * @bs: a BlockDriverState that is member of the group
480 void throttle_group_lock(BlockDriverState *bs)
482 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
483 qemu_mutex_lock(&tg->lock);
486 /* Release the lock of this throttling group.
488 * See the comments in throttle_group_lock().
490 void throttle_group_unlock(BlockDriverState *bs)
492 ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
493 qemu_mutex_unlock(&tg->lock);
496 static void throttle_groups_init(void)
498 qemu_mutex_init(&throttle_groups_lock);
501 block_init(throttle_groups_init);