add throttle block driver filter
v7 cover add throttle block driver filter
This series adds a throttle block driver filter. Currently throttling is done at the BlockBackend level. Using block driver interfaces we can move the throttling to any point in the BDS graph using a throttle node which uses the existing throttling code. This allows for potentially more complex configurations (throttling at any point in the graph, chained filters)
v7:
remove anonymous groups
error on missing throttle-group option on block/throttle.c change switch case format in block/throttle-groups.c (berto)
v6:
don't allow named group limit configuration in block/throttle.c; allow either
-drive driver=throttle,file.filename=foo.qcow2, \
limits.iops-total=...
or
-drive driver=throttle,file.filename=foo.qcow2,throttle-group=barfixes suggested by berto
v5:
fix crash in 'add aio_context field in ThrottleGroupMember' fix suggestions in block/throttle.c
v4:
fix suggestions in block/throttle.c
fix suggestions in block/throttle_groups.c
add doc note in BlockDevOptionsThrottle
v3:
fix style error in 'add aio_context field in ThrottleGroupMember'
v2:
change QOM throttle group object name
print valid ranges for uint on error
move frees in throttle_group_obj_finalize()
split throttle_group_{set,get}()
add throttle_recurse_is_first_non_filter()
Manos Pitsidianakis (6):
block: move ThrottleGroup membership to ThrottleGroupMember
block: add aio_context field in ThrottleGroupMember
block: tidy ThrottleGroupMember initializations
block: convert ThrottleGroup to object with QOM
block: add throttle block filter driver
qemu-iotests: add 184 for throttle filter driver
qapi/block-core.json | 66 +++- include/block/throttle-groups.h | 48 ++- include/qemu/throttle-options.h | 60 ++-- include/qemu/throttle.h | 3 + include/sysemu/block-backend.h | 20 +- block/block-backend.c | 62 ++-- block/qapi.c | 8 +- block/throttle-groups.c | 737 +++++++++++++++++++++++++++++----------- block/throttle.c | 250 ++++++++++++++ blockdev.c | 4 +- tests/test-throttle.c | 111 +++--- util/throttle.c | 151 ++++++++ block/Makefile.objs | 1 + tests/qemu-iotests/184 | 205 +++++++++++ tests/qemu-iotests/184.out | 300 ++++++++++++++++ tests/qemu-iotests/group | 1 + 16 files changed, 1706 insertions(+), 321 deletions(-) create mode 100644 block/throttle.c create mode 100755 tests/qemu-iotests/184 create mode 100644 tests/qemu-iotests/184.out
Signature · 1 line
2.11.0
PATCH v7 1/6 block: move ThrottleGroup membership to ThrottleGroupMember
This commit eliminates the 1:1 relationship between BlockBackend and throttle group state. Users will be able to create multiple throttle nodes, each with its own throttle group state, in the future. The throttle group state cannot be per-BlockBackend anymore, it must be per-throttle node. This is done by gathering ThrottleGroup membership details from BlockBackendPublic into ThrottleGroupMember and refactoring existing code to use the structure.
--- include/block/throttle-groups.h | 39 +++++- include/sysemu/block-backend.h | 20 +-- block/block-backend.c | 66 +++++---- block/qapi.c | 8 +- block/throttle-groups.c | 288 ++++++++++++++++++++-------------------- blockdev.c | 4 +- tests/test-throttle.c | 53 ++++---- 7 files changed, 252 insertions(+), 226 deletions(-)
include/block/throttle-groups.h +32 -7
@@ -28,19 +28,44 @@#include "qemu/throttle.h" #include "block/block_int.h" -const char *throttle_group_get_name (BlockBackend *blk); +/* The ThrottleGroupMember structure indicates membership in a ThrottleGroup +* and holds related data. +*/ + +typedef struct ThrottleGroupMember { +/* throttled_reqs_lock protects the CoQueues for throttled requests. */ +CoMutex throttled_reqs_lock; +CoQueue throttled_reqs[2 ]; + +/* Nonzero if the I/O limits are currently being ignored; generally +* it is zero. Accessed with atomic operations. +*/ +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 ; + +const char *throttle_group_get_name (ThrottleGroupMember *tgm);ThrottleState *throttle_group_incref (const char *name);void throttle_group_unref (ThrottleState *ts); -void throttle_group_config (BlockBackend *blk,ThrottleConfig *cfg); -void throttle_group_get_config (BlockBackend *blk,ThrottleConfig *cfg); +void throttle_group_config (ThrottleGroupMember *tgm,ThrottleConfig *cfg); +void throttle_group_get_config (ThrottleGroupMember *tgm,ThrottleConfig *cfg); -void throttle_group_register_blk (BlockBackend *blk,const char *groupname); -void throttle_group_unregister_blk (BlockBackend *blk); -void throttle_group_restart_blk (BlockBackend *blk); +void throttle_group_register_tgm (ThrottleGroupMember *tgm, +const char *groupname); +void throttle_group_unregister_tgm (ThrottleGroupMember *tgm); +void throttle_group_restart_tgm (ThrottleGroupMember *tgm); -void coroutine_fn throttle_group_co_io_limits_intercept(BlockBackend *blk, +void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm,unsigned int bytes,bool is_write);
include/sysemu/block-backend.h +3 -17
@@ -70,24 +70,10 @@ typedef struct BlockDevOps {/* This struct is embedded in (the private) BlockBackend struct and contains * fields that must be public. This is in particular for QLIST_ENTRY() and -* friends so that BlockBackends can be kept in lists outside block-backend.c */ +* friends so that BlockBackends can be kept in lists outside block-backend.c +* */ typedef struct BlockBackendPublic { -/* throttled_reqs_lock protects the CoQueues for throttled requests. */ -CoMutex throttled_reqs_lock; -CoQueue throttled_reqs[2 ]; - -/* Nonzero if the I/O limits are currently being ignored; generally -* it is zero. Accessed with atomic operations. -*/ -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 (BlockBackendPublic) round_robin; +ThrottleGroupMember throttle_group_member; }BlockBackendPublic ;BlockBackend *blk_new (uint64_t perm,uint64_t shared_perm);
block/block-backend.c +37 -29
153 lines not on this page · raw message
block/qapi.c +5 -3
@@ -66,10 +66,11 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, info->detect_zeroes = bs->detect_zeroes; -if (blk &&blk_get_public (blk)->throttle_state) { +if (blk &&blk_get_public (blk)->throttle_group_member.throttle_state) {ThrottleConfig cfg; +BlockBackendPublic *blkp =blk_get_public (blk); -throttle_group_get_config (blk, &cfg); +throttle_group_get_config (&blkp->throttle_group_member, &cfg); info->bps = cfg.buckets[THROTTLE_BPS_TOTAL ].avg; info->bps_rd = cfg.buckets[THROTTLE_BPS_READ ].avg;
@@ -117,7 +118,8 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, info->iops_size = cfg.op_size; info->has_group = true; - info->group =g_strdup (throttle_group_get_name (blk)); + info->group = +g_strdup (throttle_group_get_name (&blkp->throttle_group_member)); } info->write_threshold =bdrv_write_threshold_get (bs);
block/throttle-groups.c +144 -144
564 lines not on this page · raw message
blockdev.c +2 -2
@@ -2688,7 +2688,7 @@ void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) if (throttle_enabled(&cfg)) { /* Enable I/O limits if they're not enabled yet, otherwise * just update the throttling group. */ - if (!blk_get_public(blk)->throttle_state) { + if (!blk_get_public(blk)->throttle_group_member.throttle_state) { blk_io_limits_enable(blk, arg->has_group ? arg->group : arg->has_device ? arg->device :
@@ -2698,7 +2698,7 @@ void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) }/* Set the new throttling configuration */ blk_set_io_limits (blk, &cfg); - }else if (blk_get_public (blk)->throttle_state) { + }else if (blk_get_public (blk)->throttle_group_member.throttle_state) {/* If all throttling settings are set to 0, disable I/O limits */ blk_io_limits_disable (blk); }
tests/test-throttle.c +29 -24
85 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v7 2/6 block: add aio_context field in ThrottleGroupMember
In reply to Manos Pitsidianakistimer_cb() needs to know about the current Aio context of the throttle request that is woken up. In order to make ThrottleGroupMember backend agnostic, this information is stored in an aio_context field instead of accessing it from BlockBackend.
--- include/block/throttle-groups.h | 7 ++++- block/block-backend.c | 15 ++++------ block/throttle-groups.c | 38 ++++++++++++++++--------- tests/test-throttle.c | 63 +++++++++++++++++++++-------------------- 4 files changed, 69 insertions(+), 54 deletions(-)
include/block/throttle-groups.h +6 -1
@@ -33,6 +33,7 @@*/ typedef struct ThrottleGroupMember { + AioContext *aio_context; /* throttled_reqs_lock protects the CoQueues for throttled requests. */ CoMutex throttled_reqs_lock; CoQueue throttled_reqs[2];
@@ -61,12 +62,16 @@ void throttle_group_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg);void throttle_group_get_config (ThrottleGroupMember *tgm,ThrottleConfig *cfg);void throttle_group_register_tgm (ThrottleGroupMember *tgm, -const char *groupname); +const char *groupname, +AioContext *ctx);void throttle_group_unregister_tgm (ThrottleGroupMember *tgm);void throttle_group_restart_tgm (ThrottleGroupMember *tgm); void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm,unsigned int bytes,bool is_write); +void throttle_group_attach_aio_context (ThrottleGroupMember *tgm, +AioContext *new_context); +void throttle_group_detach_aio_context (ThrottleGroupMember *tgm);#endif
block/block-backend.c +6 -9
@@ -1726,18 +1726,14 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)void blk_set_aio_context (BlockBackend *blk,AioContext *new_context) {BlockDriverState *bs =blk_bs (blk); -ThrottleTimers *tt; +ThrottleGroupMember *tgm = &blk->public.throttle_group_member;if (bs) { -if (blk->public.throttle_group_member.throttle_state) { - tt = &blk->public.throttle_group_member.throttle_timers; -throttle_timers_detach_aio_context (tt); +if (tgm->throttle_state) { +throttle_group_detach_aio_context (tgm); +throttle_group_attach_aio_context (tgm, new_context); }bdrv_set_aio_context (bs, new_context); -if (blk->public.throttle_group_member.throttle_state) { - tt = &blk->public.throttle_group_member.throttle_timers; -throttle_timers_attach_aio_context (tt, new_context); - } } }
@@ -1970,7 +1966,8 @@ void blk_io_limits_disable(BlockBackend *blk)void blk_io_limits_enable (BlockBackend *blk,const char *group) {assert (!blk->public.throttle_group_member.throttle_state); -throttle_group_register_tgm (&blk->public.throttle_group_member, group); +throttle_group_register_tgm (&blk->public.throttle_group_member, + group,blk_get_aio_context (blk)); }void blk_io_limits_update_group (BlockBackend *blk,const char *group)
block/throttle-groups.c +24 -14
@@ -391,9 +391,6 @@ static void coroutine_fn throttle_group_restart_queue_entry(void *opaque)static void throttle_group_restart_queue (ThrottleGroupMember *tgm,bool is_write) { -BlockBackendPublic *blkp =container_of (tgm, BlockBackendPublic, - throttle_group_member); -BlockBackend *blk =blk_by_public (blkp);Coroutine *co;RestartData rd = { .tgm = tgm,
@@ -401,7 +398,7 @@ static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write }; co =qemu_coroutine_create (throttle_group_restart_queue_entry, &rd); -aio_co_enter (blk_get_aio_context (blk), co); +aio_co_enter (tgm->aio_context, co); }void throttle_group_restart_tgm (ThrottleGroupMember *tgm)
@@ -449,13 +446,11 @@ void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg)/* ThrottleTimers callback. This wakes up a request that was waiting * because it had been throttled. * -* @blk: the BlockBackend whose request had been throttled +* @tgm: the ThrottleGroupMember whose request had been throttled * @is_write: the type of operation (read/write) */ -static void timer_cb (BlockBackend *blk,bool is_write) +static void timer_cb (ThrottleGroupMember *tgm,bool is_write) { -BlockBackendPublic *blkp =blk_get_public (blk); -ThrottleGroupMember *tgm = &blkp->throttle_group_member;ThrottleState *ts = tgm->throttle_state;ThrottleGroup *tg =container_of (ts, ThrottleGroup, ts);
@@ -484,18 +479,18 @@ static void write_timer_cb(void *opaque)* * @tgm: the ThrottleGroupMember to insert * @groupname: the name of the group +* @ctx: the AioContext to use */ void throttle_group_register_tgm (ThrottleGroupMember *tgm, -const char *groupname) +const char *groupname, +AioContext *ctx) {int i; -BlockBackendPublic *blkp =container_of (tgm, BlockBackendPublic, - throttle_group_member); -BlockBackend *blk =blk_by_public (blkp);ThrottleState *ts =throttle_group_incref (groupname);ThrottleGroup *tg =container_of (ts, ThrottleGroup, ts); tgm->throttle_state = ts; + tgm->aio_context = ctx;qemu_mutex_lock (&tg->lock);/* If the ThrottleGroup is new set this ThrottleGroupMember as the token */
@@ -508,11 +503,11 @@ void throttle_group_register_tgm(ThrottleGroupMember *tgm,QLIST_INSERT_HEAD (&tg->head, tgm, round_robin);throttle_timers_init (&tgm->throttle_timers, -blk_get_aio_context (blk), + tgm->aio_context, tg->clock_type, read_timer_cb, write_timer_cb, - blk); + tgm);qemu_mutex_unlock (&tg->lock); }
@@ -559,6 +554,21 @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm) tgm->throttle_state =NULL ; } +void throttle_group_attach_aio_context (ThrottleGroupMember *tgm, +AioContext *new_context) +{ +ThrottleTimers *tt = &tgm->throttle_timers; +throttle_timers_attach_aio_context (tt, new_context); + tgm->aio_context = new_context; +} + +void throttle_group_detach_aio_context (ThrottleGroupMember *tgm) +{ +ThrottleTimers *tt = &tgm->throttle_timers; +throttle_timers_detach_aio_context (tt); + tgm->aio_context =NULL ; +} +static void throttle_groups_init (void ) {qemu_mutex_init (&throttle_groups_lock);
tests/test-throttle.c +33 -30
160 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v7 3/6 block: tidy ThrottleGroupMember initializations
In reply to Manos PitsidianakisMove the CoMutex and CoQueue inits inside throttle_group_register_tgm() which is called whenever a ThrottleGroupMember is initialized. There's no need for them to be separate.
--- block/block-backend.c | 3 --- block/throttle-groups.c | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-)
block/block-backend.c +0 -3
@@ -252,9 +252,6 @@ BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm) blk->shared_perm = shared_perm;blk_set_enable_write_cache (blk, true); -qemu_co_mutex_init (&blk->public.throttle_group_member.throttled_reqs_lock); -qemu_co_queue_init (&blk->public.throttle_group_member.throttled_reqs[0 ]); -qemu_co_queue_init (&blk->public.throttle_group_member.throttled_reqs[1 ]);block_acct_init (&blk->stats);notifier_list_init (&blk->remove_bs_notifiers);
block/throttle-groups.c +3 -0
@@ -508,6 +508,9 @@ void throttle_group_register_tgm(ThrottleGroupMember *tgm, read_timer_cb, write_timer_cb, tgm); +qemu_co_mutex_init (&tgm->throttled_reqs_lock); +qemu_co_queue_init (&tgm->throttled_reqs[0 ]); +qemu_co_queue_init (&tgm->throttled_reqs[1 ]);qemu_mutex_unlock (&tg->lock); }
Signature · 1 line
2.11.0
PATCH v7 4/6 block: convert ThrottleGroup to object with QOM
In reply to Manos PitsidianakisThrottleGroup is converted to an object. This will allow the future throttle block filter drive easy creation and configuration of throttle groups in QMP and cli.
A new QAPI struct, ThrottleLimits, is introduced to provide a shared struct for all throttle configuration needs in QMP.
ThrottleGroups can be created via CLI as
-object throttle-group,id=foo,x-iops-total=100,x-..
where x-* are individual limit properties. Since we can't add non-scalar properties in -object this interface must be used instead. However, setting these properties must be disabled after initialization because certain combinations of limits are forbidden and thus configuration changes should be done in one transaction. The individual properties will go away when support for non-scalar values in CLI is implemented and thus are marked as experimental.
ThrottleGroup also has a limits property that uses the ThrottleLimits struct. It can be used to create ThrottleGroups or set the configuration in existing groups as follows:
{ "execute": "object-add",
"arguments": {
"qom-type": "throttle-group",
"id": "foo",
"props" : {
"limits": {
"iops-total": 100
}
}
}
}
{ "execute" : "qom-set",
"arguments" : {
"path" : "foo",
"property" : "limits",
"value" : {
"iops-total" : 99
}
}
}This also means a group's configuration can be fetched with qom-get.
--- qapi/block-core.json | 48 +++++ include/block/throttle-groups.h | 3 + include/qemu/throttle-options.h | 59 ++++-- include/qemu/throttle.h | 3 + block/throttle-groups.c | 419 ++++++++++++++++++++++++++++++++++++---- tests/test-throttle.c | 1 + util/throttle.c | 151 +++++++++++++++ 7 files changed, 624 insertions(+), 60 deletions(-)
qapi/block-core.json +48 -0
@@ -1905,6 +1905,54 @@ '*iops_size': 'int', '*group': 'str' } } ## +# @ThrottleLimits: +# +# Limit parameters for throttling. +# Since some limit combinations are illegal, limits should always be set in one +# transaction. All fields are optional. When setting limits, if a field is +# missing the current value is not changed. +# +# @iops-total: limit total I/O operations per second +# @iops-total-max: I/O operations burst +# @iops-total-max-length: length of the iops-total-max burst period, in seconds +# It must only be set if @iops-total-max is set as well. +# @iops-read: limit read operations per second +# @iops-read-max: I/O operations read burst +# @iops-read-max-length: length of the iops-read-max burst period, in seconds +# It must only be set if @iops-read-max is set as well. +# @iops-write: limit write operations per second +# @iops-write-max: I/O operations write burst +# @iops-write-max-length: length of the iops-write-max burst period, in seconds +# It must only be set if @iops-write-max is set as well. +# @bps-total: limit total bytes per second +# @bps-total-max: total bytes burst +# @bps-total-max-length: length of the bps-total-max burst period, in seconds. +# It must only be set if @bps-total-max is set as well. +# @bps-read: limit read bytes per second +# @bps-read-max: total bytes read burst +# @bps-read-max-length: length of the bps-read-max burst period, in seconds +# It must only be set if @bps-read-max is set as well. +# @bps-write: limit write bytes per second +# @bps-write-max: total bytes write burst +# @bps-write-max-length: length of the bps-write-max burst period, in seconds +# It must only be set if @bps-write-max is set as well. +# @iops-size: when limiting by iops max size of an I/O in bytes +# +# Since: 2.11 +## +{ 'struct': 'ThrottleLimits', + 'data': { '*iops-total' : 'int', '*iops-total-max' : 'int', + '*iops-total-max-length' : 'int', '*iops-read' : 'int', + '*iops-read-max' : 'int', '*iops-read-max-length' : 'int', + '*iops-write' : 'int', '*iops-write-max' : 'int', + '*iops-write-max-length' : 'int', '*bps-total' : 'int', + '*bps-total-max' : 'int', '*bps-total-max-length' : 'int', + '*bps-read' : 'int', '*bps-read-max' : 'int', + '*bps-read-max-length' : 'int', '*bps-write' : 'int', + '*bps-write-max' : 'int', '*bps-write-max-length' : 'int', + '*iops-size' : 'int' } } + +## # @block-stream: # # Copy data from a backing file into a block device.
include/block/throttle-groups.h +3 -0
@@ -53,6 +53,9 @@ typedef struct ThrottleGroupMember { } ThrottleGroupMember; +#define TYPE_THROTTLE_GROUP "throttle-group" +#define THROTTLE_GROUP (obj) OBJECT_CHECK(ThrottleGroup, (obj), TYPE_THROTTLE_GROUP) +const char *throttle_group_get_name (ThrottleGroupMember *tgm);ThrottleState *throttle_group_incref (const char *name);
include/qemu/throttle-options.h +40 -19
121 lines not on this page · raw message
include/qemu/throttle.h +3 -0
@@ -152,5 +152,8 @@ bool throttle_schedule_timer(ThrottleState *ts, bool is_write);void throttle_account (ThrottleState *ts,bool is_write,uint64_t size); +void throttle_limits_to_config (ThrottleLimits *arg,ThrottleConfig *cfg, +Error **errp); +void throttle_config_to_limits (ThrottleConfig *cfg,ThrottleLimits *var);#endif
block/throttle-groups.c +378 -41
487 lines not on this page · raw message
tests/test-throttle.c +1 -0
@@ -662,6 +662,7 @@ int main(int argc, char **argv)qemu_init_main_loop (&error_fatal); ctx =qemu_get_aio_context ();bdrv_init (); +module_call_init (MODULE_INIT_QOM );do {}while (g_main_context_iteration (NULL , false));
util/throttle.c +151 -0
154 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v7 4/6] block: convert ThrottleGroup to object with QOM
Quoted 8 lines · Manos Pitsidianakis
block/throttle-groups.c +8
-0+static ThrottleGroup *throttle_group_by_name (const char *name) +{ +ThrottleGroup *iter; + +/* Look for an existing group with that name */ + QTAILQ_FOREACH(iter, &throttle_groups, list) { +if (!g_strcmp0 (name, iter->name)) { +return iter;
block/throttle-groups.c typedef struct ThrottleGroup { · line 107
Quoted 10 lines
block/throttle-groups.c +10
-0+ } + } + +return NULL ; +} + +static bool throttle_group_exists (const char *name) +{ +return throttle_group_by_name (name) ? true : false; +}
"foo ? true : false" is redundant, it means
"if foo is true, then true; if foo is false, then false"
I'd leave it as 'throttle_group_by_name(name) != NULL'.
Or you can simply remove the function and put that expression in throttle_group_obj_complete(), where you call it.
Quoted 4 lines
block/throttle-groups.c +4
-0+#undef THROTTLE_OPT_PREFIX +#define THROTTLE_OPT_PREFIX "x-" + +/* Helper struct and array for QOM property setter/getter */
block/throttle-groups.c void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) · line 604
Quoted 10 lines
block/throttle-groups.c +10
-0+typedef struct { +const char *name; +BucketType type; +enum LeakyBucketField { +AVG , +MAX , +BURST_LENGTH , +IOPS_SIZE , + } category; +}ThrottleParamInfo ;
I think it looks much nicer now :) I don't think you need to name the enum (LeakyBucketField) by the way.
Quoted 15 lines
block/throttle-groups.c +15
-0+/* This function edits throttle_groups and must be called under the global +* mutex */ +static void throttle_group_obj_complete (UserCreatable *obj,Error **errp) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; + +/* set group name to object id if it exists */ +if (!tg->name && tg->parent_obj.parent) { + tg->name =object_get_canonical_path_component (OBJECT (obj)); + } +/* We must have a group name at this point */ +assert (tg->name); + +/* error if name is duplicate */
block/throttle-groups.c void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) · line 726
Quoted 10 lines
block/throttle-groups.c +10
-0+if (throttle_group_exists (tg->name)) { +error_setg (errp,"A group with this name already exists" ); +return ; + } + +/* check validity */ +throttle_get_config (&tg->ts, &cfg); +if (!throttle_is_valid (&cfg, errp)) { +return ; + }
My fault here because I suggested its removal, but I actually think we still need to call throttle_config() here so bkt->max is initialized in throttle_fix_bucket(). I'll take care of updating this call once my patches to remove throttle_fix_bucket() are applied, but for now we still need it.
Quoted 11 lines
block/throttle-groups.c +11
-0+ cfg = tg->ts.cfg; +switch (info->category) { +case AVG : + cfg.buckets[info->type].avg = value; +break ; +case MAX : + cfg.buckets[info->type].max = value; +break ; +case BURST_LENGTH : +if (value >UINT_MAX ) { + error_setg(&local_err, "%s value must be in the"
block/throttle-groups.c void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) · line 791
Quoted 10 lines
block/throttle-groups.c +10
-0+ "range [0, %u]", info->name, UINT_MAX); + goto ret; + } + cfg.buckets[info->type].burst_length = value; +break ; +case IOPS_SIZE : + cfg.op_size = value; + } + + tg->ts.cfg = cfg;
I like how this looks now. Perhaps we can get rid of the local cfg variable and use a pointer instead, there's no need to copy the config back and forth.
Berto
Re: [Qemu-devel] [PATCH v7 4/6] block: convert ThrottleGroup to object with QOM
Quoted 23 lines · Alberto Garcia
Quoted 23 lines
block/throttle-groups.c +23
-0+/* This function edits throttle_groups and must be called under the global +* mutex */ +static void throttle_group_obj_complete (UserCreatable *obj,Error **errp) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; + +/* set group name to object id if it exists */ +if (!tg->name && tg->parent_obj.parent) { + tg->name =object_get_canonical_path_component (OBJECT (obj)); + } +/* We must have a group name at this point */ +assert (tg->name); + +/* error if name is duplicate */ +if (throttle_group_exists (tg->name)) { +error_setg (errp,"A group with this name already exists" ); +return ; + } + +/* check validity */ +throttle_get_config (&tg->ts, &cfg); +if (!throttle_is_valid (&cfg, errp)) {
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return; + }My fault here because I suggested its removal, but I actually think we still need to call
throttle_config()here sobkt->maxis initialized inthrottle_fix_bucket(). I'll take care of updating this call once my patches to removethrottle_fix_bucket()are applied, but for now we still need it.
Oh yes, that is the reason I originally had done it but forgot why.
Re: [Qemu-devel] [Qemu-block] [PATCH v7 4/6] block: convert ThrottleGroup to object with QOM
In reply to Manos PitsidianakisQuoted 38 lines · Manos Pitsidianakis
/* Increments the reference count of a ThrottleGroup given its name.
*
* If no ThrottleGroup is found with the given name a new one is
* created.
*
+ * This function editsthrottle_groupsand must be called under the global
+ * mutex.
+ *
* @name: the name of the ThrottleGroup
* @ret: the ThrottleState member of the ThrottleGroup
*/
ThrottleState *throttle_group_incref(const char *name){ ThrottleGroup *tg = NULL;block/throttle-groups.c +7
-13-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 = iter; -break ; - } - } + tg =throttle_group_by_name (name); -/* Create a new one if not found */ -if (!tg) { - tg =g_new0 (ThrottleGroup,1 ); +if (tg) { +object_ref (OBJECT (tg)); + }else { +/* Create a new one if not found */ +/* new ThrottleGroup obj will have a refcnt = 1 */ + tg =THROTTLE_GROUP (object_new (TYPE_THROTTLE_GROUP )); tg->name =g_strdup (name); - tg->clock_type =QEMU_CLOCK_REALTIME ;
block/throttle-groups.c typedef struct ThrottleGroup { · line 112
Quoted 10 lines
block/throttle-groups.c +0
-10- -if (qtest_enabled ()) { -/* For testing block IO throttling only */ - tg->clock_type =QEMU_CLOCK_VIRTUAL ; - } -qemu_mutex_init (&tg->lock); -throttle_init (&tg->ts); -QLIST_INIT (&tg->head); - -QTAILQ_INSERT_TAIL (&throttle_groups, tg, list);
This is where the ThrottleGroup needs to be added to the QOM tree:
object_property_add_child(object_get_objects_root(),
id, obj, &local_err);Quoted 1 line
+
throttle_group_obj_complete((UserCreatable *)tg, &error_abort);
This cast is risky - if UserCreatable ever adds fields it can break. Please use a QOM cast: USER_CREATABLE(tg). It uses the type information and finds the interface.
Quoted 1 line
+static ThrottleParamInfo properties[] = {
This array can be const.
Quoted 1 line
+{
Missing indentation. Seems unusual but I guess you're trying to avoid reaching 80 chars? If checkpatch.pl doesn't complain then you can get away with it but normal indentation would be best.
Quoted 8 lines
block/throttle-groups.c +8
-0+switch (info->category) { +case AVG : + cfg.buckets[info->type].avg = value; +break ; +case MAX : + cfg.buckets[info->type].max = value; +break ; +case BURST_LENGTH :
block/throttle-groups.c void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) · line 789
Quoted 10 lines
block/throttle-groups.c +10
-0+if (value >UINT_MAX ) { +error_setg (&local_err,"%s value must be in the" +"range [0, %u]" , info->name,UINT_MAX ); + goto ret; + } + cfg.buckets[info->type].burst_length = value; +break ; +case IOPS_SIZE : + cfg.op_size = value; + }
Please use break statements even for the last case. If another case is added later it might accidentally fall through without a break statement.
PATCH v7 5/6 block: add throttle block filter driver
In reply to Manos Pitsidianakisblock/throttle.c uses existing I/O throttle infrastructure inside a block filter driver. I/O operations are intercepted in the filter's read/write coroutines, and referred to block/throttle-groups.c
The driver can be used with the syntax
-drive driver=throttle,file.filename=foo.qcow2,throttle-group=bar
which registers the throttle filter node with the ThrottleGroup 'bar'. The given group must be created beforehand with object-add or -object.
--- qapi/block-core.json | 18 ++- include/block/throttle-groups.h | 1 + include/qemu/throttle-options.h | 1 + block/throttle-groups.c | 7 +- block/throttle.c | 250 ++++++++++++++++++++++++++++++++++++++++ block/Makefile.objs | 1 + 6 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 block/throttle.c
qapi/block-core.json +17 -1
@@ -2222,6 +2222,7 @@ # Drivers that are supported in block device operations. # # @vxhs: Since 2.10 +# @throttle: Since 2.11 # # Since: 2.9 ##
@@ -2231,7 +2232,7 @@ 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'parallels', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh', - 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] } + 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] } ## # @BlockdevOptionsFile:
@@ -3095,6 +3096,20 @@ '*tls-creds': 'str' } } ## +# @BlockdevOptionsThrottle: +# +# Driver specific block device options for the throttle driver +# +# @throttle-group: the name of the throttle-group object to use. It +# must already exist. +# @file: reference to or definition of the data source block device +# Since: 2.11 +## +{ 'struct': 'BlockdevOptionsThrottle', + 'data': { '*throttle-group': 'str', + 'file' : 'BlockdevRef' + } } +## # @BlockdevOptions: # # Options for creating a block device. Many options are available for all
@@ -3155,6 +3170,7 @@ 'replication':'BlockdevOptionsReplication', 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', + 'throttle': 'BlockdevOptionsThrottle', 'vdi': 'BlockdevOptionsGenericFormat', 'vhdx': 'BlockdevOptionsGenericFormat', 'vmdk': 'BlockdevOptionsGenericCOWFormat',
include/block/throttle-groups.h +1 -0
@@ -76,5 +76,6 @@ void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgmvoid throttle_group_attach_aio_context (ThrottleGroupMember *tgm,AioContext *new_context);void throttle_group_detach_aio_context (ThrottleGroupMember *tgm); +bool throttle_group_exists (const char *name);#endif
include/qemu/throttle-options.h +1 -0
@@ -29,6 +29,7 @@#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"#define THROTTLE_OPT_PREFIX "throttling."#define THROTTLE_OPTS \
block/throttle-groups.c +6 -1
@@ -101,7 +101,7 @@ static ThrottleGroup *throttle_group_by_name(const char *name)return NULL ; } -static bool throttle_group_exists (const char *name) +bool throttle_group_exists (const char *name) {return throttle_group_by_name (name) ? true : false; }
@@ -548,6 +548,11 @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm)ThrottleGroupMember *token;int i; +if (!ts) { +/* Discard uninitialized tgm */ +return ; + } +assert (tgm->pending_reqs[0 ] ==0 && tgm->pending_reqs[1 ] ==0 );assert (qemu_co_queue_empty (&tgm->throttled_reqs[0 ]));assert (qemu_co_queue_empty (&tgm->throttled_reqs[1 ]));
block/throttle.c new file +250 -0
250 lines not on this page · raw message
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
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v7 5/6] block: add throttle block filter driver
Quoted 5 lines · Manos Pitsidianakis
##
qapi/block-core.json +4
-0+# @BlockdevOptionsThrottle: +# +# Driver specific block device options for the throttle driver +#
qapi/block-core.json line 3112
Quoted 10 lines
qapi/block-core.json +10
-0+# @throttle-group: the name of the throttle-group object to use. It +# must already exist. +# @file: reference to or definition of the data source block device +# Since: 2.11 +## +{ 'struct': 'BlockdevOptionsThrottle', + 'data': { '*throttle-group': 'str', + 'file' : 'BlockdevRef' + } } +##
I guess throttle-group is not optional here anymore ?
block/throttle-groups.c void throttle_group_unregister_tgm(ThrottleGroupMember *tgm) · line 555
Quoted 9 lines
block/throttle-groups.c +5
-0@@ -548,6 +548,11 @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm)ThrottleGroupMember *token;int i; +if (!ts) { +/* Discard uninitialized tgm */ +return ; + } +
Is this change needed in case throttle_configure_tgm() fails?
Quoted 16 lines
block/throttle.c new file +16
-0+static int throttle_configure_tgm (BlockDriverState *bs, +ThrottleGroupMember *tgm, +QDict *options,Error **errp) +{ +int ret; +const char *group_name; +Error *local_err =NULL ; +QemuOpts *opts =qemu_opts_create (&throttle_opts,NULL ,0 , &error_abort); + +qemu_opts_absorb_qdict (opts, options, &local_err); +if (local_err) { +error_propagate (errp, local_err); + ret = -EINVAL ; + goto fin; + } +
block/throttle.c line 63
Quoted 10 lines
block/throttle.c new file +10
-0+ group_name =qemu_opt_get (opts,QEMU_OPT_THROTTLE_GROUP_NAME ); +if (!group_name) { +error_setg (errp,"Please specify a throttle group." ); + ret =EINVAL ; + goto fin; + }else if (!throttle_group_exists (group_name)) { +error_setg (errp,"Throttle group '%s' does not exist." , group_name); + ret =EINVAL ; + goto fin; + }
It should be -EINVAL (negative value) in both cases.
Berto
Re: [Qemu-devel] [PATCH v7 5/6] block: add throttle block filter driver
Quoted 5 lines · Alberto Garcia
Quoted 4 lines · Manos Pitsidianakis
block/throttle-groups.c +0
-0@@ -548,6 +548,11 @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm)ThrottleGroupMember *token;int i;
Quoted 7 lines
block/throttle-groups.c void throttle_group_unregister_tgm(ThrottleGroupMember *tgm) · line 555
Quoted 5 lines
block/throttle-groups.c +5
-0+if (!ts) { +/* Discard uninitialized tgm */ +return ; + } +Is this change needed in case
throttle_configure_tgm()fails?
Yes, now all errors are before throttle_group_register_tgm(), therefore the unregister part in throttle_close() will not have valid data.
Re: [Qemu-devel] [Qemu-block] [PATCH v7 5/6] block: add throttle block filter driver
In reply to Manos PitsidianakisQuoted 5 lines · Manos Pitsidianakis
qapi/block-core.json +1
-0@@ -3095,6 +3096,20 @@ '*tls-creds': 'str' } } ## +# @BlockdevOptionsThrottle:
qapi/block-core.json line 3109
Quoted 10 lines
qapi/block-core.json +10
-0+# +# Driver specific block device options for the throttle driver +# +# @throttle-group: the name of the throttle-group object to use. It +# must already exist. +# @file: reference to or definition of the data source block device +# Since: 2.11 +## +{ 'struct': 'BlockdevOptionsThrottle', + 'data': { '*throttle-group': 'str',
This field is optional but:
group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
if (!group_name) {
error_setg(errp, "Please specify a throttle group.");
ret = EINVAL;
goto fin;Should it be mandatory?
Quoted 42 lines
qapi/block-core.json +4
-0+ 'file' : 'BlockdevRef' + } } +## # @BlockdevOptions: # # Options for creating a block device. Many options are available for all@@ -3155,6 +3170,7 @@ 'replication':'BlockdevOptionsReplication', 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', + 'throttle': 'BlockdevOptionsThrottle', 'vdi': 'BlockdevOptionsGenericFormat', 'vhdx': 'BlockdevOptionsGenericFormat', 'vmdk': 'BlockdevOptionsGenericCOWFormat',include/block/throttle-groups.h +1
-0@@ -76,5 +76,6 @@ void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgmvoid throttle_group_attach_aio_context (ThrottleGroupMember *tgm,AioContext *new_context);void throttle_group_detach_aio_context (ThrottleGroupMember *tgm); +bool throttle_group_exists (const char *name);#endif include/qemu/throttle-options.h +1
-0@@ -29,6 +29,7 @@#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"#define THROTTLE_OPT_PREFIX "throttling."#define THROTTLE_OPTS \block/throttle-groups.c +0
-0
block/throttle-groups.c static ThrottleGroup *throttle_group_by_name(const char *name) · line 103
Quoted 3 lines
block/throttle-groups.c +0
-0@@ -101,7 +101,7 @@ static ThrottleGroup *throttle_group_by_name(const char *name)return NULL ; }
/* Must be called with the global mutex held */
Quoted 5 lines
block/throttle-groups.c +1
-1-static bool throttle_group_exists (const char *name) +bool throttle_group_exists (const char *name) {return throttle_group_by_name (name) ? true : false; }
PATCH v7 6/6 qemu-iotests: add 184 for throttle filter driver
In reply to Manos Pitsidianakis--- tests/qemu-iotests/184 | 205 +++++++++++++++++++++++++++++++ tests/qemu-iotests/184.out | 300 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 506 insertions(+) create mode 100755 tests/qemu-iotests/184 create mode 100644 tests/qemu-iotests/184.out
tests/qemu-iotests/184 new file +205 -0
@@ -0,0 +1,205 @@ +#!/bin/bash +# +# Test I/O throttle block filter driver interface +# +# Copyright (C) 2017 Manos Pitsidianakis +# +# 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 of the License, or +# (at your option) any later version. +# +# 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 <http://www.gnu.org/licenses/>. +# + +# creator +owner="Manos Pitsidianakis" + +seq=`basename $0` +echo "QA output created by $seq" + +here=`pwd` +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux + +function do_run_qemu() +{ + echo Testing: "$@" | _filter_imgfmt + $QEMU -nographic -qmp-pretty stdio -serial none "$@" + echo +} + +function run_qemu() +{ + do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qemu | _filter_qmp\ + | _filter_qemu_io | _filter_generated_node_ids +} + +_make_test_img 64M +test_throttle=$($QEMU_IMG --help|grep throttle) +[ "$test_throttle" = "" ] && _supported_fmt throttle + +echo +echo "== checking interface ==" + +run_qemu <<EOF +{ "execute": "qmp_capabilities" } +{ "execute": "blockdev-add", + "arguments": { + "driver": "$IMGFMT", + "node-name": "disk0", + "file": { + "driver": "file", + "filename": "$TEST_IMG" + } + } +} +{ "execute": "object-add", + "arguments": { + "qom-type": "throttle-group", + "id": "group0", + "props": { + "limits" : { + "iops-total": 1000 + } + } + } +} +{ "execute": "blockdev-add", + "arguments": { + "driver": "throttle", + "node-name": "throttle0", + "throttle-group": "group0", + "file": "disk0" + } +} +{ "execute": "query-named-block-nodes" } +{ "execute": "query-block" } +{ "execute": "quit" } +EOF + +echo +echo "== property changes in ThrottleGroup ==" + +run_qemu <<EOF +{ "execute": "qmp_capabilities" } +{ "execute": "object-add", + "arguments": { + "qom-type": "throttle-group", + "id": "group0", + "props" : { + "limits": { + "iops-total": 1000 + } + } + } +} +{ "execute" : "qom-get", + "arguments" : { + "path" : "group0", + "property" : "limits" + } +} +{ "execute" : "qom-set", + "arguments" : { + "path" : "group0", + "property" : "limits", + "value" : { + "iops-total" : 0 + } + } +} +{ "execute" : "qom-get", + "arguments" : { + "path" : "group0", + "property" : "limits" + } +} +{ "execute": "quit" } +EOF + +echo +echo "== object creation/set errors ==" + +run_qemu <<EOF +{ "execute": "qmp_capabilities" } +{ "execute": "object-add", + "arguments": { + "qom-type": "throttle-group", + "id": "group0", + "props" : { + "limits": { + "iops-total": 1000 + } + } + } +} +{ "execute" : "qom-set", + "arguments" : { + "path" : "group0", + "property" : "x-iops-total", + "value" : 0 + } +} +{ "execute" : "qom-set", + "arguments" : { + "path" : "group0", + "property" : "limits", + "value" : { + "iops-total" : 10, + "iops-read" : 10 + } + } +} +{ "execute": "quit" } +EOF + +echo +echo "== don't specify group ==" + +run_qemu <<EOF +{ "execute": "qmp_capabilities" } +{ "execute": "blockdev-add", + "arguments": { + "driver": "$IMGFMT", + "node-name": "disk0", + "file": { + "driver": "file", + "filename": "$TEST_IMG" + } + } +} +{ "execute": "blockdev-add", + "arguments": { + "driver": "throttle", + "node-name": "throttle0", + "file": "disk0" + } +} +{ "execute": "quit" } +EOF + +echo +# success, all done +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/184.out new file +300 -0
300 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -180,6 +180,7 @@ 181 rw auto migration 182 rw auto quick 183 rw auto migration +184 rw auto quick 185 rw auto 186 rw auto 187 rw auto
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v7 6/6] qemu-iotests: add 184 for throttle filter driver
Quoted 1 line · Manos Pitsidianakis
Signed-off-by Manos Pitsidianakis <[email protected]>
Berto
Re: [Qemu-devel] [PATCH v7 6/6] qemu-iotests: add 184 for throttle filter driver
In reply to Manos PitsidianakisQuoted 1 line · Manos Pitsidianakis
+_supported_fmt qcow2
What makes this test qcow2-specific? With additional filtering for IMGFMT it should be possible to run this on any image format.
Re: [Qemu-devel] [PATCH v7 6/6] qemu-iotests: add 184 for throttle filter driver
Quoted 5 lines · Stefan Hajnoczi
Quoted 1 line · Manos Pitsidianakis
+_supported_fmt qcow2
What makes this test qcow2-specific? With additional filtering for IMGFMT it should be possible to run this on any image format.
I think none of the available filters hide all of the image details in query-block and company. _filter_imgfmt hides the image extensions but not the image specific fields (eg virtual-size, actual-size and format-specific). Perhaps a new filter that hides everything but "format" and "filename" in the "image" subdict can be introduced later.
Re: [Qemu-devel] [PATCH v7 6/6] qemu-iotests: add 184 for throttle filter driver
Quoted 11 lines · Manos Pitsidianakis
Quoted 8 lines · Stefan Hajnoczi
Quoted 2 lines · Manos Pitsidianakis
+_supported_fmt qcow2
What makes this test qcow2-specific? With additional filtering for IMGFMT it should be possible to run this on any image format.
Quoted 5 lines
I think none of the available filters hide all of the image details in query-block and company.
_filter_imgfmthides the image extensions but not the image specific fields (eg virtual-size, actual-size and format-specific). Perhaps a new filter that hides everything but "format" and "filename" in the "image" subdict can be introduced later.
It's a little cumbersome to filter JSON in shell. The filters do not use jq(1) at the moment.
Tests written in Python have an easier time of filtering certain fields. I did that recently for QMP Event objects in 194.
Anyway, it's okay to keep this test qcow2-only for now.
Stefan