From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58935) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3QPQ-0008CS-Jy for qemu-devel@nongnu.org; Fri, 12 Jun 2015 10:58:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z3QPK-0004tz-Tf for qemu-devel@nongnu.org; Fri, 12 Jun 2015 10:58:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50194) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3QPK-0004tn-L3 for qemu-devel@nongnu.org; Fri, 12 Jun 2015 10:58:10 -0400 From: Stefan Hajnoczi Date: Fri, 12 Jun 2015 15:57:52 +0100 Message-Id: <1434121078-15776-5-git-send-email-stefanha@redhat.com> In-Reply-To: <1434121078-15776-1-git-send-email-stefanha@redhat.com> References: <1434121078-15776-1-git-send-email-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 04/10] throttle: Add throttle group infrastructure List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Peter Maydell , Alberto Garcia , Stefan Hajnoczi From: Alberto Garcia Signed-off-by: Alberto Garcia Reviewed-by: Stefan Hajnoczi Message-id: 2fdb4de17210b733a13eb472c33cd08b45f8fd21.1433779731.git.berto= @igalia.com Signed-off-by: Stefan Hajnoczi --- block/Makefile.objs | 1 + block/throttle-groups.c | 261 ++++++++++++++++++++++++++++++++++= ++++++ include/block/block_int.h | 1 + include/block/throttle-groups.h | 39 ++++++ 4 files changed, 302 insertions(+) create mode 100644 block/throttle-groups.c create mode 100644 include/block/throttle-groups.h diff --git a/block/Makefile.objs b/block/Makefile.objs index 0d8c2a4..c34fd7c 100644 --- a/block/Makefile.objs +++ b/block/Makefile.objs @@ -10,6 +10,7 @@ block-obj-$(CONFIG_WIN32) +=3D raw-win32.o win32-aio.o block-obj-$(CONFIG_POSIX) +=3D raw-posix.o block-obj-$(CONFIG_LINUX_AIO) +=3D linux-aio.o block-obj-y +=3D null.o mirror.o io.o +block-obj-y +=3D throttle-groups.o =20 block-obj-y +=3D nbd.o nbd-client.o sheepdog.o block-obj-$(CONFIG_LIBISCSI) +=3D iscsi.o diff --git a/block/throttle-groups.c b/block/throttle-groups.c new file mode 100644 index 0000000..352077f --- /dev/null +++ b/block/throttle-groups.c @@ -0,0 +1,261 @@ +/* + * QEMU block throttling group infrastructure + * + * Copyright (C) Nodalink, EURL. 2014 + * Copyright (C) Igalia, S.L. 2015 + * + * Authors: + * Beno=C3=AEt Canet + * Alberto Garcia + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 or + * (at your option) version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "block/throttle-groups.h" + +/* The ThrottleGroup structure (with its ThrottleState) is shared + * among different BlockDriverState and it's independent from + * AioContext, so in order to use it from different threads it needs + * its own locking. + * + * This locking is however handled internally in this file, so it's + * transparent to outside users. + * + * The whole ThrottleGroup structure is private and invisible to + * outside users, that only use it through its ThrottleState. + * + * In addition to the ThrottleGroup structure, BlockDriverState has + * fields that need to be accessed by other members of the group and + * therefore also need to be protected by this lock. Once a BDS is + * registered in a group those fields can be accessed by other threads + * any time. + * + * Again, all this is handled internally and is mostly transparent to + * the outside. The 'throttle_timers' field however has an additional + * constraint because it may be temporarily invalid (see for example + * bdrv_set_aio_context()). Therefore in this file a thread will + * access some other BDS's timers only after verifying that that BDS + * has throttled requests in the queue. + */ +typedef struct ThrottleGroup { + char *name; /* This is constant during the lifetime of the group */ + + QemuMutex lock; /* This lock protects the following four fields */ + ThrottleState ts; + QLIST_HEAD(, BlockDriverState) head; + BlockDriverState *tokens[2]; + bool any_timer_armed[2]; + + /* These two are protected by the global throttle_groups_lock */ + unsigned refcount; + QTAILQ_ENTRY(ThrottleGroup) list; +} ThrottleGroup; + +static QemuMutex throttle_groups_lock; +static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =3D + QTAILQ_HEAD_INITIALIZER(throttle_groups); + +/* Increments the reference count of a ThrottleGroup given its name. + * + * If no ThrottleGroup is found with the given name a new one is + * created. + * + * @name: the name of the ThrottleGroup + * @ret: the ThrottleGroup + */ +static ThrottleGroup *throttle_group_incref(const char *name) +{ + ThrottleGroup *tg =3D NULL; + ThrottleGroup *iter; + + qemu_mutex_lock(&throttle_groups_lock); + + /* Look for an existing group with that name */ + QTAILQ_FOREACH(iter, &throttle_groups, list) { + if (!strcmp(name, iter->name)) { + tg =3D iter; + break; + } + } + + /* Create a new one if not found */ + if (!tg) { + tg =3D g_new0(ThrottleGroup, 1); + tg->name =3D g_strdup(name); + qemu_mutex_init(&tg->lock); + throttle_init(&tg->ts); + QLIST_INIT(&tg->head); + + QTAILQ_INSERT_TAIL(&throttle_groups, tg, list); + } + + tg->refcount++; + + qemu_mutex_unlock(&throttle_groups_lock); + + return tg; +} + +/* Decrease the reference count of a ThrottleGroup. + * + * When the reference count reaches zero the ThrottleGroup is + * destroyed. + * + * @tg: The ThrottleGroup to unref + */ +static void throttle_group_unref(ThrottleGroup *tg) +{ + qemu_mutex_lock(&throttle_groups_lock); + if (--tg->refcount =3D=3D 0) { + QTAILQ_REMOVE(&throttle_groups, tg, list); + qemu_mutex_destroy(&tg->lock); + g_free(tg->name); + g_free(tg); + } + qemu_mutex_unlock(&throttle_groups_lock); +} + +/* Get the name from a BlockDriverState's ThrottleGroup. The name (and + * the pointer) is guaranteed to remain constant during the lifetime + * of the group. + * + * @bs: a BlockDriverState that is member of a throttling group + * @ret: the name of the group. + */ +const char *throttle_group_get_name(BlockDriverState *bs) +{ + ThrottleGroup *tg =3D container_of(bs->throttle_state, ThrottleGroup= , ts); + return tg->name; +} + +/* Return the next BlockDriverState in the round-robin sequence, + * simulating a circular list. + * + * This assumes that tg->lock is held. + * + * @bs: the current BlockDriverState + * @ret: the next BlockDriverState in the sequence + */ +static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs) +{ + ThrottleState *ts =3D bs->throttle_state; + ThrottleGroup *tg =3D container_of(ts, ThrottleGroup, ts); + BlockDriverState *next =3D QLIST_NEXT(bs, round_robin); + + if (!next) { + return QLIST_FIRST(&tg->head); + } + + return next; +} + +/* Update the throttle configuration for a particular group. Similar + * to throttle_config(), but guarantees atomicity within the + * throttling group. + * + * @bs: a BlockDriverState that is member of the group + * @cfg: the configuration to set + */ +void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg) +{ + ThrottleTimers *tt =3D &bs->throttle_timers; + ThrottleState *ts =3D bs->throttle_state; + ThrottleGroup *tg =3D container_of(ts, ThrottleGroup, ts); + qemu_mutex_lock(&tg->lock); + throttle_config(ts, tt, cfg); + /* throttle_config() cancels the timers */ + tg->any_timer_armed[0] =3D tg->any_timer_armed[1] =3D false; + qemu_mutex_unlock(&tg->lock); +} + +/* Get the throttle configuration from a particular group. Similar to + * throttle_get_config(), but guarantees atomicity within the + * throttling group. + * + * @bs: a BlockDriverState that is member of the group + * @cfg: the configuration will be written here + */ +void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg= ) +{ + ThrottleState *ts =3D bs->throttle_state; + ThrottleGroup *tg =3D container_of(ts, ThrottleGroup, ts); + qemu_mutex_lock(&tg->lock); + throttle_get_config(ts, cfg); + qemu_mutex_unlock(&tg->lock); +} + +/* Register a BlockDriverState in the throttling group, also updating + * its throttle_state pointer to point to it. If a throttling group + * with that name does not exist yet, it will be created. + * + * @bs: the BlockDriverState to insert + * @groupname: the name of the group + */ +void throttle_group_register_bs(BlockDriverState *bs, const char *groupn= ame) +{ + int i; + ThrottleGroup *tg =3D throttle_group_incref(groupname); + + bs->throttle_state =3D &tg->ts; + + qemu_mutex_lock(&tg->lock); + /* If the ThrottleGroup is new set this BlockDriverState as the toke= n */ + for (i =3D 0; i < 2; i++) { + if (!tg->tokens[i]) { + tg->tokens[i] =3D bs; + } + } + + QLIST_INSERT_HEAD(&tg->head, bs, round_robin); + qemu_mutex_unlock(&tg->lock); +} + +/* Unregister a BlockDriverState from its group, removing it from the + * list and setting the throttle_state pointer to NULL. + * + * The group will be destroyed if it's empty after this operation. + * + * @bs: the BlockDriverState to remove + */ +void throttle_group_unregister_bs(BlockDriverState *bs) +{ + ThrottleGroup *tg =3D container_of(bs->throttle_state, ThrottleGroup= , ts); + int i; + + qemu_mutex_lock(&tg->lock); + for (i =3D 0; i < 2; i++) { + if (tg->tokens[i] =3D=3D bs) { + BlockDriverState *token =3D throttle_group_next_bs(bs); + /* Take care of the case where this is the last bs in the gr= oup */ + if (token =3D=3D bs) { + token =3D NULL; + } + tg->tokens[i] =3D token; + } + } + + /* remove the current bs from the list */ + QLIST_REMOVE(bs, round_robin); + qemu_mutex_unlock(&tg->lock); + + throttle_group_unref(tg); + bs->throttle_state =3D NULL; +} + +static void throttle_groups_init(void) +{ + qemu_mutex_init(&throttle_groups_lock); +} + +block_init(throttle_groups_init); diff --git a/include/block/block_int.h b/include/block/block_int.h index 02b312f..8d15c7b 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -383,6 +383,7 @@ struct BlockDriverState { ThrottleTimers throttle_timers; CoQueue throttled_reqs[2]; bool io_limits_enabled; + QLIST_ENTRY(BlockDriverState) round_robin; =20 /* I/O stats (display with "info blockstats"). */ BlockAcctStats stats; diff --git a/include/block/throttle-groups.h b/include/block/throttle-gro= ups.h new file mode 100644 index 0000000..b966ec7 --- /dev/null +++ b/include/block/throttle-groups.h @@ -0,0 +1,39 @@ +/* + * QEMU block throttling group infrastructure + * + * Copyright (C) Nodalink, EURL. 2014 + * Copyright (C) Igalia, S.L. 2015 + * + * Authors: + * Beno=C3=AEt Canet + * Alberto Garcia + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 or + * (at your option) version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef THROTTLE_GROUPS_H +#define THROTTLE_GROUPS_H + +#include "qemu/throttle.h" +#include "block/block_int.h" + +const char *throttle_group_get_name(BlockDriverState *bs); + +void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg); +void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg= ); + +void throttle_group_register_bs(BlockDriverState *bs, const char *groupn= ame); +void throttle_group_unregister_bs(BlockDriverState *bs); + +#endif --=20 2.4.2