block: add throttle block filter driver
RFC PATCH 1/1 block: add throttle block filter driver
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in bdrv_co_pwrite_zeros and bdrv_co_pdiscard refers to sectors or bytes. Should this be handled differently?
The driver can only be instantiated in the command line for now like in the following examples:
-drive driver=throttle,file.filename=foo.qcow2,iops-total=100, iops-total-max=2000,iops-total-max-length=60,throttle-group=foobar -drive driver=throttle,file.filename=foo.qcow2,iops-total=100, throttle-group=foobar
This commit copies throttle group infrastructure from block/throttle-groups.c to block/throttle.c to be used as a block filter driver.
--- block/Makefile.objs | 1 + block/throttle.c | 747 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/throttle.h | 59 ++++ 3 files changed, 807 insertions(+) create mode 100644 block/throttle.c create mode 100644 include/block/throttle.h
block/Makefile.objs +1 -0
@@ -25,6 +25,7 @@ block-obj-y += accounting.o dirty-bitmap.o block-obj-y += write-threshold.o block-obj-y += backup.o block-obj-$(CONFIG_REPLICATION) += replication.o +block-obj-y += throttle.o block-obj-y += crypto.o
block/throttle.c new file +747 -0
747 lines not on this page · raw message
include/block/throttle.h new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.11.0
Quoted 4 lines · Manos Pitsidianakis
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in
bdrv_co_pwrite_zerosandbdrv_co_pdiscardrefers to sectors or bytes.
Both refer to byte counts. (We're trying to get rid of as many sector-based interfaces as possible, but it's slow going to make sure the conversions are still accurate).
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 9 lines · Eric Blake
Quoted 4 lines · Manos Pitsidianakis
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in
bdrv_co_pwrite_zerosandbdrv_co_pdiscardrefers to sectors or bytes.Both refer to byte counts. (We're trying to get rid of as many sector-based interfaces as possible, but it's slow going to make sure the conversions are still accurate).
I see. Would changing the names in the BlockDriverState function signatures to reflect that be okay?
Quoted 7 lines · Manos Pitsidianakis
Quoted 6 lines · Eric Blake
Quoted 4 lines · Manos Pitsidianakis
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in
bdrv_co_pwrite_zerosandbdrv_co_pdiscardrefers to sectors or bytes.
Quoted 6 lines
Quoted 3 lines
Both refer to byte counts. (We're trying to get rid of as many sector-based interfaces as possible, but it's slow going to make sure the conversions are still accurate).
I see. Would changing the names in the BlockDriverState function signatures to reflect that be okay?
What names need changing?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 8 lines · Eric Blake
Quoted 7 lines · Manos Pitsidianakis
Quoted 6 lines · Eric Blake
Quoted 4 lines · Manos Pitsidianakis
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in
bdrv_co_pwrite_zerosandbdrv_co_pdiscardrefers to sectors or bytes.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Both refer to byte counts. (We're trying to get rid of as many sector-based interfaces as possible, but it's slow going to make sure the conversions are still accurate).
I see. Would changing the names in the BlockDriverState function signatures to reflect that be okay?
What names need changing?
I meant, change "int count" to "int bytes" in the bdrv_co_pwrite_zeros and bdrv_co_pdiscard signatures inside struct BlockDriverState.
Quoted 5 lines
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 13 lines · Manos Pitsidianakis
Quoted 12 lines · Eric Blake
Quoted 11 lines · Manos Pitsidianakis
Quoted 9 lines · Eric Blake
Quoted 4 lines · Manos Pitsidianakis
This is part of my work for my GSOC project this summer.
I am not sure if the count parameter in
bdrv_co_pwrite_zerosandbdrv_co_pdiscardrefers to sectors or bytes.Both refer to byte counts. (We're trying to get rid of as many sector-based interfaces as possible, but it's slow going to make sure the conversions are still accurate).
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I see. Would changing the names in the BlockDriverState function signatures to reflect that be okay?
What names need changing?
I meant, change "int count" to "int bytes" in the
bdrv_co_pwrite_zerosandbdrv_co_pdiscardsignatures inside struct BlockDriverState.
Sure, a patch to change parameter names to something that aids legibility would be welcome.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 10 lines · Manos Pitsidianakis
block/throttle.c new file +10
-0+#define QEMU_OPT_IOPS_TOTAL "iops-total" +#define QEMU_OPT_IOPS_TOTAL_MAX "iops-total-max" +#define QEMU_OPT_IOPS_TOTAL_MAX_LENGTH "iops-total-max-length" +#define QEMU_OPT_IOPS_READ "iops-read" +#define QEMU_OPT_IOPS_READ_MAX "iops-read-max" +#define QEMU_OPT_IOPS_READ_MAX_LENGTH "iops-read-max-length" +#define QEMU_OPT_IOPS_WRITE "iops-write" +#define QEMU_OPT_IOPS_WRITE_MAX "iops-write-max" +#define QEMU_OPT_IOPS_WRITE_MAX_LENGTH "iops-write-max-length" +#define QEMU_OPT_BPS_TOTAL "bps-total"
block/throttle.c line 55
Quoted 10 lines
block/throttle.c new file +10
-0+#define QEMU_OPT_BPS_TOTAL_MAX "bps-total-max" +#define QEMU_OPT_BPS_TOTAL_MAX_LENGTH "bps-total-max-length" +#define QEMU_OPT_BPS_READ "bps-read" +#define QEMU_OPT_BPS_READ_MAX "bps-read-max" +#define QEMU_OPT_BPS_READ_MAX_LENGTH "bps-read-max-length" +#define QEMU_OPT_BPS_WRITE "bps-write" +#define QEMU_OPT_BPS_WRITE_MAX "bps-write-max" +#define QEMU_OPT_BPS_WRITE_MAX_LENGTH "bps-write-max-length" +#define QEMU_OPT_IOPS_SIZE "iops-size" +#define QEMU_OPT_THROTTLE_GROUP_NAME "throttle-group"
Please reuse include/qemu/throttle-options.h.
block/throttle.c line 60
Quoted 3 lines
block/throttle.c new file +3
-0+static QemuMutex throttle_groups_lock; +static QTAILQ_HEAD(, ThrottleGroup) throttle_groups = +QTAILQ_HEAD_INITIALIZER (throttle_groups);
Please reuse block/throttle-groups.c. I think throttle-groups.c doesn't actually need BlockBackend so you can make the code more general.
Define the necessary state:
typedef struct ThrottleGroupMember {
/* Protected by AioContext lock. */
CoQueue throttled_reqs[2]; /* Nonzero if the I/O limits are currently being ignored; generally
* it is zero. */
unsigned int io_limits_disabled;
/* The following fields are protected by the ThrottleGroup lock.
* See the ThrottleGroup documentation for details.
* throttle_state tells us if I/O limits are configured. */
ThrottleState *throttle_state;
ThrottleTimers throttle_timers;
unsigned pending_reqs[2];
QLIST_ENTRY(ThrottleGroupMember) round_robin;} ThrottleGroupMember;
Then refactor throttle-groups.c to use ThrottleGroupMember instead of BlockBackend(Public).
include/block/throttle.h
Quoted 5 lines
include/block/throttle.h new file +0
-0
What is the purpose of this header?
Header files in include/* are "public" APIs that other parts of QEMU can use. There is no user other than block/throttle.c so please keep it private in the .c file.
Quoted 63 lines
include/block/throttle.h new file +59
-0@@ -0,0 +1,59 @@ +/* The ThrottleGroup structure (with its ThrottleState) is shared +* among different BlockDriverStates 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 block/throttle.c 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 +* BlockDriverState is registered in a group those fields can be accessed +* by other threads any time. +* +* Again, all this is handled internally in block/throttle.c 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 block/throttle.c will access some +* other BlockDriverState'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(, BDRVThrottleNodeState) head; +struct BDRVThrottleNodeState *tokens[2 ]; +bool any_timer_armed[2 ]; + +/* These two are protected by the global throttle_groups_lock */ +unsigned refcount; +QTAILQ_ENTRY (ThrottleGroup) list; +}ThrottleGroup ; + +typedef struct BDRVThrottleNodeState { +ThrottleGroup *throttle_group; + +/* I/O throttling has its own locking, but also some fields are +* protected by the AioContext lock. +*/ + +/* Protected by AioContext lock. */ +CoQueue throttled_reqs[2 ]; + +/* Nonzero if the I/O limits are currently being ignored; generally +* it is zero. */ +unsigned int io_limits_disabled; + +ThrottleState *throttle_state; +ThrottleTimers throttle_timers; +unsigned pending_reqs[2 ]; +QLIST_ENTRY (BDRVThrottleNodeState) round_robin; + +}BDRVThrottleNodeState ;Signature · 1 line
2.11.0
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Manos Pitsidianakis
block/throttle.c new file +14
-0+#define QEMU_OPT_IOPS_TOTAL "iops-total" +#define QEMU_OPT_IOPS_TOTAL_MAX "iops-total-max" +#define QEMU_OPT_IOPS_TOTAL_MAX_LENGTH "iops-total-max-length" +#define QEMU_OPT_IOPS_READ "iops-read" +#define QEMU_OPT_IOPS_READ_MAX "iops-read-max" +#define QEMU_OPT_IOPS_READ_MAX_LENGTH "iops-read-max-length" +#define QEMU_OPT_IOPS_WRITE "iops-write" +#define QEMU_OPT_IOPS_WRITE_MAX "iops-write-max" +#define QEMU_OPT_IOPS_WRITE_MAX_LENGTH "iops-write-max-length" +#define QEMU_OPT_BPS_TOTAL "bps-total" +#define QEMU_OPT_BPS_TOTAL_MAX "bps-total-max" +#define QEMU_OPT_BPS_TOTAL_MAX_LENGTH "bps-total-max-length" +#define QEMU_OPT_BPS_READ "bps-read" +#define QEMU_OPT_BPS_READ_MAX "bps-read-max"
Quoted 8 lines
block/throttle.c line 55
Quoted 6 lines
block/throttle.c new file +6
-0+#define QEMU_OPT_BPS_READ_MAX_LENGTH "bps-read-max-length" +#define QEMU_OPT_BPS_WRITE "bps-write" +#define QEMU_OPT_BPS_WRITE_MAX "bps-write-max" +#define QEMU_OPT_BPS_WRITE_MAX_LENGTH "bps-write-max-length" +#define QEMU_OPT_IOPS_SIZE "iops-size" +#define QEMU_OPT_THROTTLE_GROUP_NAME "throttle-group"Please reuse
include/qemu/throttle-options.h.
This would make the options too verbose, ie -drive driver=throttle,file=..,throttling.iops-total=..,throttling. Eventually of course throttle-options.h will be replaced.
However, this can't be done at this point anyway, since the throttling.[...] options are consumed by the already existing throttling code, and are unavailable for the driver.
Quoted 25 lines
block/throttle.c line 60
Quoted 4 lines
block/throttle.c new file +3
-0+static QemuMutex throttle_groups_lock; +static QTAILQ_HEAD(, ThrottleGroup) throttle_groups = +QTAILQ_HEAD_INITIALIZER (throttle_groups);Please reuse
block/throttle-groups.c. I thinkthrottle-groups.cdoesn't actually need BlockBackend so you can make the code more general.Define the necessary state:
typedef struct ThrottleGroupMember { /* Protected by AioContext lock. */ CoQueue throttled_reqs[2]; /* Nonzero if the I/O limits are currently being ignored; generally * it is zero. */ unsigned int io_limits_disabled; /* The following fields are protected by the ThrottleGroup lock. * See the ThrottleGroup documentation for details. * throttle_state tells us if I/O limits are configured. */ ThrottleState *throttle_state; ThrottleTimers throttle_timers; unsigned pending_reqs[2]; QLIST_ENTRY(ThrottleGroupMember) round_robin;
Quoted 4 lines
} ThrottleGroupMember;
Then refactor
throttle-groups.cto use ThrottleGroupMember instead of BlockBackend(Public).
I've already done this, it was just in a different branch.
Quoted 9 lines
include/block/throttle.h
Quoted 6 lines
include/block/throttle.h new file +0
-0What is the purpose of this header?
Quoted 3 lines
Header files in
include/*are "public" APIs that other parts of QEMU can use. There is no user other thanblock/throttle.cso please keep it private in the .c file.
Eventually the qmp/hmp/qom related stuff will be in a header file, and this is why I put them from the beginning separately. But yes, in this case it is not needed. Also, these probably should go to include/qemu/throttle.h instead of a separate header file.
Quoted 64 lines
Quoted 64 lines
include/block/throttle.h new file +59
-1@@ -0,0 +1,59 @@ +/* The ThrottleGroup structure (with its ThrottleState) is shared +* among different BlockDriverStates 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 block/throttle.c 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 +* BlockDriverState is registered in a group those fields can be accessed +* by other threads any time. +* +* Again, all this is handled internally in block/throttle.c 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 block/throttle.c will access some +* other BlockDriverState'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(, BDRVThrottleNodeState) head; +struct BDRVThrottleNodeState *tokens[2 ]; +bool any_timer_armed[2 ]; + +/* These two are protected by the global throttle_groups_lock */ +unsigned refcount; +QTAILQ_ENTRY (ThrottleGroup) list; +}ThrottleGroup ; + +typedef struct BDRVThrottleNodeState { +ThrottleGroup *throttle_group; + +/* I/O throttling has its own locking, but also some fields are +* protected by the AioContext lock. +*/ + +/* Protected by AioContext lock. */ +CoQueue throttled_reqs[2 ]; + +/* Nonzero if the I/O limits are currently being ignored; generally +* it is zero. */ +unsigned int io_limits_disabled; + +ThrottleState *throttle_state; +ThrottleTimers throttle_timers; +unsigned pending_reqs[2 ]; +QLIST_ENTRY (BDRVThrottleNodeState) round_robin; + +}BDRVThrottleNodeState ; --2.11.0
Quoted 25 lines · Manos Pitsidianakis
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Manos Pitsidianakis
block/throttle.c new file +14
-0+#define QEMU_OPT_IOPS_TOTAL "iops-total" +#define QEMU_OPT_IOPS_TOTAL_MAX "iops-total-max" +#define QEMU_OPT_IOPS_TOTAL_MAX_LENGTH "iops-total-max-length" +#define QEMU_OPT_IOPS_READ "iops-read" +#define QEMU_OPT_IOPS_READ_MAX "iops-read-max" +#define QEMU_OPT_IOPS_READ_MAX_LENGTH "iops-read-max-length" +#define QEMU_OPT_IOPS_WRITE "iops-write" +#define QEMU_OPT_IOPS_WRITE_MAX "iops-write-max" +#define QEMU_OPT_IOPS_WRITE_MAX_LENGTH "iops-write-max-length" +#define QEMU_OPT_BPS_TOTAL "bps-total" +#define QEMU_OPT_BPS_TOTAL_MAX "bps-total-max" +#define QEMU_OPT_BPS_TOTAL_MAX_LENGTH "bps-total-max-length" +#define QEMU_OPT_BPS_READ "bps-read" +#define QEMU_OPT_BPS_READ_MAX "bps-read-max"Quoted 8 lines
block/throttle.c line 55
Quoted 6 lines
block/throttle.c new file +6
-0+#define QEMU_OPT_BPS_READ_MAX_LENGTH "bps-read-max-length" +#define QEMU_OPT_BPS_WRITE "bps-write" +#define QEMU_OPT_BPS_WRITE_MAX "bps-write-max" +#define QEMU_OPT_BPS_WRITE_MAX_LENGTH "bps-write-max-length" +#define QEMU_OPT_IOPS_SIZE "iops-size" +#define QEMU_OPT_THROTTLE_GROUP_NAME "throttle-group"Please reuse
include/qemu/throttle-options.h.
Quoted 7 lines
This would make the options too verbose, ie -drive driver=throttle,file=..,throttling.iops-total=..,throttling. Eventually of course
throttle-options.hwill be replaced.However, this can't be done at this point anyway, since the throttling.[...] options are consumed by the already existing throttling code, and are unavailable for the driver.
There are ways of reusing include/qemu/throttle-options.h, I'm just not sure if they are worth it.
In block/block-backend.c:
#define THROTTLE_OPTION_PREFIX "throttling." #include "qemu/throttle-options.h"
In block/throttle.c:
#include "qemu/throttle-options.h"
In include/qemu/throttle-options.h:
#define THROTTLE_OPTION_IOPS_TOTAL (THROTTLE_OPTION_PREFIX "iops-total")
#define THROTTLE_OPTS \
{ \
.name = THROTTLE_OPTION_IOPS_TOTAL,\
.type = QEMU_OPT_NUMBER,\
.help = "limit total I/O operations per second",\
},{ \to generate either "throttling.iops-total" or "iops-total", depending on the environment in which the file was included.
This reuses the same .h file but it's also a little ugly because of the C macro (ab)use.
Quoted 8 lines
Quoted 8 lines
include/block/throttle.h
Quoted 5 lines
include/block/throttle.h new file +0
-0What is the purpose of this header?
Quoted 8 lines
Quoted 3 lines
Header files in
include/*are "public" APIs that other parts of QEMU can use. There is no user other thanblock/throttle.cso please keep it private in the .c file.Eventually the qmp/hmp/qom related stuff will be in a header file, and this is why I put them from the beginning separately. But yes, in this case it is not needed. Also, these probably should go to
include/qemu/throttle.hinstead of a separate header file.
Patches must be self-contained because reviewers don't necessarily know future plans and if those future patches are never merged then the source tree is in an incomplete state.
There are rare cases where it's really necessary for a patch to add something that is unused. In that case the intention should be clear from the patch or commit description (e.g. "let's change this now so the next patch can take advantage of it").