add throttle block driver filter
v4 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)
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 (7):
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
block: add BlockDevOptionsThrottle to QAPI
block: add throttle block filter driver interface tests
block/Makefile.objs | 1 + block/block-backend.c | 62 ++-- block/qapi.c | 8 +- block/throttle-groups.c | 730 +++++++++++++++++++++++++++++----------- block/throttle.c | 315 +++++++++++++++++ blockdev.c | 4 +- include/block/throttle-groups.h | 47 ++- include/qemu/throttle-options.h | 60 ++-- include/qemu/throttle.h | 3 + include/sysemu/block-backend.h | 20 +- qapi/block-core.json | 70 +++- tests/qemu-iotests/184 | 310 +++++++++++++++++ tests/qemu-iotests/184.out | 422 +++++++++++++++++++++++ tests/qemu-iotests/group | 1 + tests/test-throttle.c | 111 +++--- util/throttle.c | 151 +++++++++ 16 files changed, 1995 insertions(+), 320 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 v4 1/7 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.
--- block/block-backend.c | 66 +++++---- block/qapi.c | 8 +- block/throttle-groups.c | 288 ++++++++++++++++++++-------------------- blockdev.c | 4 +- include/block/throttle-groups.h | 39 +++++- include/sysemu/block-backend.h | 20 +-- tests/test-throttle.c | 53 ++++---- 7 files changed, 252 insertions(+), 226 deletions(-)
block/block-backend.c +37 -29
@@ -215,9 +215,9 @@ 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.throttled_reqs_lock); -qemu_co_queue_init (&blk->public.throttled_reqs[0 ]); -qemu_co_queue_init (&blk->public.throttled_reqs[1 ]); +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);
@@ -285,7 +285,7 @@ static void blk_delete(BlockBackend *blk)assert (!blk->refcnt);assert (!blk->name);assert (!blk->dev); -if (blk->public.throttle_state) { +if (blk->public.throttle_group_member.throttle_state) {blk_io_limits_disable (blk); }if (blk->root) {
@@ -596,9 +596,12 @@ BlockBackend *blk_by_public(BlockBackendPublic *public)*/ void blk_remove_bs (BlockBackend *blk) { +ThrottleTimers *tt; +notifier_list_notify (&blk->remove_bs_notifiers, blk); -if (blk->public.throttle_state) { -throttle_timers_detach_aio_context (&blk->public.throttle_timers); +if (blk->public.throttle_group_member.throttle_state) { + tt = &blk->public.throttle_group_member.throttle_timers; +throttle_timers_detach_aio_context (tt); }blk_update_root_state (blk);
@@ -620,9 +623,10 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)bdrv_ref (bs);notifier_list_notify (&blk->insert_bs_notifiers, blk); -if (blk->public.throttle_state) { +if (blk->public.throttle_group_member.throttle_state) {throttle_timers_attach_aio_context ( - &blk->public.throttle_timers,bdrv_get_aio_context (bs)); + &blk->public.throttle_group_member.throttle_timers, +bdrv_get_aio_context (bs)); }return 0 ;
@@ -984,8 +988,9 @@ int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,bdrv_inc_in_flight (bs);/* throttling disk I/O */ -if (blk->public.throttle_state) { -throttle_group_co_io_limits_intercept (blk, bytes, false); +if (blk->public.throttle_group_member.throttle_state) { +throttle_group_co_io_limits_intercept (&blk->public.throttle_group_member, + bytes, false); } ret =bdrv_co_preadv (blk->root, offset, bytes, qiov, flags);
@@ -1008,10 +1013,10 @@ int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset, }bdrv_inc_in_flight (bs); -/* throttling disk I/O */ -if (blk->public.throttle_state) { -throttle_group_co_io_limits_intercept (blk, bytes, true); +if (blk->public.throttle_group_member.throttle_state) { +throttle_group_co_io_limits_intercept (&blk->public.throttle_group_member, + bytes, true); }if (!blk->enable_write_cache) {
@@ -1680,15 +1685,17 @@ 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;if (bs) { -if (blk->public.throttle_state) { -throttle_timers_detach_aio_context (&blk->public.throttle_timers); +if (blk->public.throttle_group_member.throttle_state) { + tt = &blk->public.throttle_group_member.throttle_timers; +throttle_timers_detach_aio_context (tt); }bdrv_set_aio_context (bs, new_context); -if (blk->public.throttle_state) { -throttle_timers_attach_aio_context (&blk->public.throttle_timers, - 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); } } }
@@ -1907,33 +1914,34 @@ int blk_commit_all(void)/* throttling disk I/O limits */ void blk_set_io_limits (BlockBackend *blk,ThrottleConfig *cfg) { -throttle_group_config (blk, cfg); +throttle_group_config (&blk->public.throttle_group_member, cfg); }void blk_io_limits_disable (BlockBackend *blk) { -assert (blk->public.throttle_state); +assert (blk->public.throttle_group_member.throttle_state);bdrv_drained_begin (blk_bs (blk)); -throttle_group_unregister_blk (blk); +throttle_group_unregister_tgm (&blk->public.throttle_group_member);bdrv_drained_end (blk_bs (blk)); }/* should be called before blk_set_io_limits if a limit is set */ void blk_io_limits_enable (BlockBackend *blk,const char *group) { -assert (!blk->public.throttle_state); -throttle_group_register_blk (blk, group); +assert (!blk->public.throttle_group_member.throttle_state); +throttle_group_register_tgm (&blk->public.throttle_group_member, group); }void blk_io_limits_update_group (BlockBackend *blk,const char *group) {/* this BB is not part of any group */ -if (!blk->public.throttle_state) { +if (!blk->public.throttle_group_member.throttle_state) {return ; }/* this BB is a part of the same group than the one we want */ -if (!g_strcmp0 (throttle_group_get_name (blk), group)) { +if (!g_strcmp0 (throttle_group_get_name (&blk->public.throttle_group_member), + group)) {return ; }
@@ -1955,8 +1963,8 @@ static void blk_root_drained_begin(BdrvChild *child)/* Note that blk->root may not be accessible here yet if we are just * attaching to a BlockDriverState that is drained. Use child instead. */ -if (atomic_fetch_inc (&blk->public.io_limits_disabled) ==0 ) { -throttle_group_restart_blk (blk); +if (atomic_fetch_inc (&blk->public.throttle_group_member.io_limits_disabled) ==0 ) { +throttle_group_restart_tgm (&blk->public.throttle_group_member); } }
@@ -1965,8 +1973,8 @@ static void blk_root_drained_end(BdrvChild *child)BlockBackend *blk = child->opaque;assert (blk->quiesce_counter); -assert (blk->public.io_limits_disabled); -atomic_dec (&blk->public.io_limits_disabled); +assert (blk->public.throttle_group_member.io_limits_disabled); +atomic_dec (&blk->public.throttle_group_member.io_limits_disabled);if (--blk->quiesce_counter ==0 ) {if (blk->dev_ops && blk->dev_ops->drained_end) {
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); }
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);
tests/test-throttle.c +29 -24
85 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v4 2/7 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.
--- block/block-backend.c | 15 +++++----- block/throttle-groups.c | 38 ++++++++++++++++--------- include/block/throttle-groups.h | 7 ++++- tests/test-throttle.c | 63 +++++++++++++++++++++-------------------- 4 files changed, 70 insertions(+), 53 deletions(-)
block/block-backend.c +7 -8
@@ -1685,17 +1685,15 @@ 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); }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); +if (tgm->throttle_state) { +throttle_group_attach_aio_context (tgm, new_context); } } }
@@ -1929,7 +1927,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);
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
tests/test-throttle.c +33 -30
160 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v4 3/7 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
@@ -215,9 +215,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 v4 4/7 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.
ThrottleGroups can be anonymous which means they can't get accessed by other users ie they will always be units instead of group (Because they have one ThrottleGroupMember).
---
v4 major changes:
removed throttle_groups_lock
changed reference counting of ThrottleGroup objects
Notes:
Note: I tested Markus Armbruster's patch in <[email protected]> on master and I can use this syntax successfuly:
-object '{ "qom-type" : "throttle-group", "id" : "foo", "props" : { "limits" \
: { "iops-total" : 1000 } } }'
If this gets merged using -object will be a little more verbose but at least we won't have seperate properties, which is a good thing, so the x-* should be dropped.
block/throttle-groups.c | 421 ++++++++++++++++++++++++++++++++++++---- include/block/throttle-groups.h | 3 + include/qemu/throttle-options.h | 59 ++++-- include/qemu/throttle.h | 3 + qapi/block-core.json | 48 +++++ tests/test-throttle.c | 1 + util/throttle.c | 151 ++++++++++++++ 7 files changed, 626 insertions(+), 60 deletions(-)
block/throttle-groups.c +380 -41
@@ -25,9 +25,17 @@#include "qemu/osdep.h" #include "sysemu/block-backend.h" #include "block/throttle-groups.h" +#include "qemu/throttle-options.h" #include "qemu/queue.h" #include "qemu/thread.h" #include "sysemu/qtest.h" +#include "qapi/error.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qom/object_interfaces.h" + +static void throttle_group_obj_init (Object *obj); +static void throttle_group_obj_complete (UserCreatable *obj,Error **errp);/* The ThrottleGroup structure (with its ThrottleState) is shared * among different ThrottleGroupMembers and it's independent from
@@ -54,6 +62,10 @@* that ThrottleGroupMember has throttled requests in the queue. */ typedef struct ThrottleGroup { + Object parent_obj; + + /* refuse individual property change if initialization is complete */ + bool is_initialized; char *name; /* This is constant during the lifetime of the group */ QemuMutex lock; /* This lock protects the following four fields */
@@ -63,12 +75,11 @@ typedef struct ThrottleGroup {bool any_timer_armed[2 ];QEMUClockType clock_type; -/* These two are protected by the global throttle_groups_lock */ -unsigned refcount; +/* This field is protected by the global QEMU mutex */ QTAILQ_ENTRY (ThrottleGroup) list; } ThrottleGroup; -static QemuMutex throttle_groups_lock; +/* This is protected by the global QEMU mutex */ static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =QTAILQ_HEAD_INITIALIZER (throttle_groups);
@@ -77,7 +88,11 @@ static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =* If no ThrottleGroup is found with the given name a new one is * created. * -* @name: the name of the ThrottleGroup +* This function edits throttle_groups and must be called under the global +* mutex. +* +* @name: the name of the ThrottleGroup, NULL means a new anonymous group will +* be created. * @ret: the ThrottleState member of the ThrottleGroup */ ThrottleState *throttle_group_incref (const char *name)
@@ -85,37 +100,26 @@ ThrottleState *throttle_group_incref(const char *name)ThrottleGroup *tg =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 = iter; -break ; +if (name) { +/* Look for an existing group with that name */ + QTAILQ_FOREACH(iter, &throttle_groups, list) { +if (!g_strcmp0 (name, iter->name)) { + tg = iter; +break ; + } } } -/* 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 ; - -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); +throttle_group_obj_complete ((UserCreatable *)tg, &error_abort); } - tg->refcount++; - -qemu_mutex_unlock (&throttle_groups_lock); -return &tg->ts; }
@@ -124,20 +128,15 @@ ThrottleState *throttle_group_incref(const char *name)* When the reference count reaches zero the ThrottleGroup is * destroyed. * +* This function edits throttle_groups and must be called under the global +* mutex. +* * @ts: The ThrottleGroup to unref, given by its ThrottleState member */ void throttle_group_unref (ThrottleState *ts) {ThrottleGroup *tg =container_of (ts, ThrottleGroup, ts); - -qemu_mutex_lock (&throttle_groups_lock); -if (--tg->refcount ==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); +object_unref (OBJECT (tg)); }/* Get the name from a ThrottleGroupMember's group. The name (and the pointer)
@@ -477,8 +476,11 @@ static void write_timer_cb(void *opaque)* its timers and updating its throttle_state pointer to point to it. If a * throttling group with that name does not exist yet, it will be created. * +* This function edits throttle_groups and must be called under the global +* mutex. +* * @tgm: the ThrottleGroupMember to insert -* @groupname: the name of the group +* @groupname: the name of the group. If NULL, an anonymous group is created. * @ctx: the AioContext to use */ void throttle_group_register_tgm(ThrottleGroupMember *tgm,
@@ -572,9 +574,346 @@ void throttle_group_detach_aio_context(ThrottleGroupMember *tgm)throttle_timers_detach_aio_context (tt); } +#undef THROTTLE_OPT_PREFIX +#define THROTTLE_OPT_PREFIX "x-" +#define DOUBLE 0 +#define UINT64 1 +#define UNSIGNED 2 + +/* Helper struct and array for QOM property setter/getter */ +typedef struct { +const char *name; +BucketType type; +int data_type; +const ptrdiff_t offset;/* offset in LeakyBucket struct. */ +}ThrottleParamInfo ; + +static ThrottleParamInfo properties[] = { +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL, +THROTTLE_OPS_TOTAL ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX, +THROTTLE_OPS_TOTAL ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX_LENGTH, +THROTTLE_OPS_TOTAL ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ, +THROTTLE_OPS_READ ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX, +THROTTLE_OPS_READ ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX_LENGTH, +THROTTLE_OPS_READ ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE, +THROTTLE_OPS_WRITE ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX, +THROTTLE_OPS_WRITE ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX_LENGTH, +THROTTLE_OPS_WRITE ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL, +THROTTLE_BPS_TOTAL ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX, +THROTTLE_BPS_TOTAL ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX_LENGTH, +THROTTLE_BPS_TOTAL ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ, +THROTTLE_BPS_READ ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX, +THROTTLE_BPS_READ ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX_LENGTH, +THROTTLE_BPS_READ ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE, +THROTTLE_BPS_WRITE ,DOUBLE , offsetof(LeakyBucket , avg), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX, +THROTTLE_BPS_WRITE ,DOUBLE , offsetof(LeakyBucket , max), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX_LENGTH, +THROTTLE_BPS_WRITE ,UNSIGNED , offsetof(LeakyBucket , burst_length), +}, +{ + THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_SIZE, +0 ,UINT64 , offsetof(ThrottleConfig , op_size), +} +}; + +/* This function edits throttle_groups and must be called under the global +* mutex */ +static void throttle_group_obj_init (Object *obj) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); + + tg->clock_type =QEMU_CLOCK_REALTIME ; +if (qtest_enabled ()) { +/* For testing block IO throttling only */ + tg->clock_type =QEMU_CLOCK_VIRTUAL ; + } + tg->is_initialized = false; +QTAILQ_INSERT_TAIL (&throttle_groups, tg, list); +qemu_mutex_init (&tg->lock); +throttle_init (&tg->ts); +QLIST_INIT (&tg->head); +} + +/* 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), *iter; +ThrottleConfig *cfg = &tg->ts.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)); + } + +if (tg->name) { +/* error if name is duplicate */ + QTAILQ_FOREACH(iter, &throttle_groups, list) { +if (!g_strcmp0 (tg->name, iter->name) && tg != iter) { +error_setg (errp,"A group with this name already exists" ); +return ; + } + } + } + +/* unfix buckets to check validity */ +throttle_get_config (&tg->ts, cfg); +if (!throttle_is_valid (cfg, errp)) { +return ; + } +/* fix buckets again */ +throttle_config (&tg->ts, tg->clock_type, cfg); + + tg->is_initialized = true; +} + +/* This function edits throttle_groups and must be called under the global +* mutex */ +static void throttle_group_obj_finalize (Object *obj) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +QTAILQ_REMOVE (&throttle_groups, tg, list); +qemu_mutex_destroy (&tg->lock); +g_free (tg->name); +} + +static void throttle_group_set (Object *obj,Visitor *v,const char * name, +void *opaque,Error **errp) + +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; +ThrottleParamInfo *info = opaque; +Error *local_err =NULL ; +int64_t value; + +/* If we have finished initialization, don't accept individual property +* changes through QOM. Throttle configuration limits must be set in one +* transaction, as certain combinations are invalid. +*/ +if (tg->is_initialized) { +error_setg (&local_err,"Property cannot be set after initialization" ); + goto ret; + } + +visit_type_int64 (v, name, &value, &local_err); +if (local_err) { + goto ret; + } +if (value <0 ) { +error_setg (&local_err,"Property values cannot be negative" ); + goto ret; + } + + cfg = tg->ts.cfg; +switch (info->data_type) { +case UINT64 : + { +uint64_t *field = (void *)&cfg.buckets[info->type] + info->offset; + *field = value; + } +break ; +case DOUBLE : + { +double *field = (void *)&cfg.buckets[info->type] + info->offset; + *field = value; + } +break ; +case UNSIGNED : + { +if (value >UINT_MAX ) { +error_setg (&local_err,"%s value must be in the" +"range [0, %u]" , info->name,UINT_MAX ); + goto ret; + } +unsigned *field = (void *)&cfg.buckets[info->type] + info->offset; + *field = value; + } + } + + tg->ts.cfg = cfg; + +ret: +error_propagate (errp, local_err); +return ; + +} + +static void throttle_group_get (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; +ThrottleParamInfo *info = opaque; +int64_t value; + + cfg = tg->ts.cfg; +switch (info->data_type) { +case UINT64 : + { +uint64_t *field = (void *)&cfg.buckets[info->type] + info->offset; + value = *field; + } +break ; +case DOUBLE : + { +double *field = (void *)&cfg.buckets[info->type] + info->offset; + value = *field; + } +break ; +case UNSIGNED : + { +unsigned *field = (void *)&cfg.buckets[info->type] + info->offset; + value = *field; + } + } + +visit_type_int64 (v, name, &value, errp); +} + +static void throttle_group_set_limits (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) + +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; +ThrottleLimits *arg =NULL ; +Error *local_err =NULL ; + + arg =g_new0 (ThrottleLimits,1 ); +visit_type_ThrottleLimits (v, name, &arg, &local_err); +if (local_err) { + goto ret; + } +qemu_mutex_lock (&tg->lock); +throttle_get_config (&tg->ts, &cfg); +throttle_limits_to_config (arg, &cfg, &local_err); +if (local_err) { + goto unlock; + } +throttle_config (&tg->ts, tg->clock_type, &cfg); + +unlock: +qemu_mutex_unlock (&tg->lock); +ret: +g_free (arg); +error_propagate (errp, local_err); +return ; +} + +static void throttle_group_get_limits (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +ThrottleGroup *tg =THROTTLE_GROUP (obj); +ThrottleConfig cfg; +ThrottleLimits *arg =NULL ; + +qemu_mutex_lock (&tg->lock); +throttle_get_config (&tg->ts, &cfg); +qemu_mutex_unlock (&tg->lock); + + arg =g_new0 (ThrottleLimits,1 ); +throttle_config_to_limits (&cfg, arg); + +visit_type_ThrottleLimits (v, name, &arg, errp); +g_free (arg); +} + +static void throttle_group_obj_class_init (ObjectClass *klass,void *class_data) +{ +size_t i =0 ; +UserCreatableClass *ucc =USER_CREATABLE_CLASS (klass); + + ucc->complete = throttle_group_obj_complete; +/* individual properties */ +for (i =0 ; i <sizeof (properties) /sizeof (ThrottleParamInfo); i++) { +object_class_property_add (klass, + properties[i].name, +"int" , + throttle_group_get, + throttle_group_set, +NULL , &properties[i], + &error_abort); + } + +/* ThrottleLimits */ +object_class_property_add (klass, +"limits" ,"ThrottleLimits" , + throttle_group_get_limits, + throttle_group_set_limits, +NULL ,NULL , + &error_abort); +} + +static const TypeInfo throttle_group_info = { + .name =TYPE_THROTTLE_GROUP , + .parent =TYPE_OBJECT , + .class_init = throttle_group_obj_class_init, + .instance_size =sizeof (ThrottleGroup), + .instance_init = throttle_group_obj_init, + .instance_finalize = throttle_group_obj_finalize, + .interfaces = (InterfaceInfo []) { + {TYPE_USER_CREATABLE }, + { } + }, +}; +static void throttle_groups_init (void ) { -qemu_mutex_init (&throttle_groups_lock); +type_register_static (&throttle_group_info); } -block_init (throttle_groups_init); +type_init (throttle_groups_init);
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
qapi/block-core.json +48 -0
54 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 v4 4/7] block: convert ThrottleGroup to object with QOM
block/throttle-groups.c void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) · line 716
Quoted 7 lines · Manos Pitsidianakis
block/throttle-groups.c +7
-0+/* unfix buckets to check validity */ +throttle_get_config (&tg->ts, cfg); +if (!throttle_is_valid (cfg, errp)) { +return ; + } +/* fix buckets again */ +throttle_config (&tg->ts, tg->clock_type, cfg);
Ugh, this whole fix/unfix buckets should be transparent to outside users of throttle.c.
You can leave this here for now but I'll think of a way to eliminate the need to do this.
Berto
PATCH v4 5/7 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, \
limits.iops-total=...,throttle-group=bar
The configuration flags and their semantics are identical to the hardcoded throttling ones.
A node can be created referring to an existing group, and will overwrite its limits if any are specified, otherwise they are retained.
--- block/Makefile.objs | 1 + block/throttle.c | 315 ++++++++++++++++++++++++++++++++++++++++ include/qemu/throttle-options.h | 1 + 3 files changed, 317 insertions(+) create mode 100644 block/throttle.c
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 +315 -0
315 lines not on this page · raw message
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 \
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v4 5/7] block: add throttle block filter driver
Quoted 9 lines · Manos Pitsidianakis
block/throttle.c new file +9
-0+/* Extract ThrottleConfig options. Assumes cfg is initialized and will be +* checked for validity. +* +* Returns -1 and sets errp if a burst_length value is over UINT_MAX. +*/ +static int throttle_extract_options (QemuOpts *opts,ThrottleConfig *cfg, +Error **errp) +{ +#define IF_OPT_SET (rvalue, opt_name) \
block/throttle.c line 59
Quoted 10 lines
block/throttle.c new file +10
-0+ if (qemu_opt_get(opts, THROTTLE_OPT_PREFIX opt_name)) { \ + rvalue = qemu_opt_get_number(opts, THROTTLE_OPT_PREFIX opt_name, 0); } + +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_TOTAL ].avg,QEMU_OPT_BPS_TOTAL ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_READ ].avg,QEMU_OPT_BPS_READ ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_WRITE ].avg,QEMU_OPT_BPS_WRITE ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_TOTAL ].avg,QEMU_OPT_IOPS_TOTAL ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_READ ].avg,QEMU_OPT_IOPS_READ ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_WRITE ].avg,QEMU_OPT_IOPS_WRITE ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_TOTAL ].max,QEMU_OPT_BPS_TOTAL_MAX );
[...]
This is all the code that I was saying that we'd save if we don't allow setting limits here.
block/throttle.c line 100
Quoted 7 lines
block/throttle.c new file +7
-0+static int throttle_configure_tgm (BlockDriverState *bs, +ThrottleGroupMember *tgm, +QDict *options,Error **errp) +{ +int ret; +ThrottleConfig cfg; +const char *group_name =NULL ;
No need to set it to NULL here.
Quoted 16 lines
block/throttle.c new file +16
-0+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); + goto err; + } + +/* If group_name is NULL, an anonymous group will be created */ + group_name =qemu_opt_get (opts,QEMU_OPT_THROTTLE_GROUP_NAME ); + +/* Register membership to group with name group_name */ +throttle_group_register_tgm (tgm, group_name,bdrv_get_aio_context (bs)); + +/* Copy previous configuration */
block/throttle.c line 126
Quoted 10 lines
block/throttle.c new file +10
-0+throttle_group_get_config (tgm, &cfg); + +/* Change limits if user has specified them */ +if (throttle_extract_options (opts, &cfg, errp) || + !throttle_is_valid (&cfg, errp)) { +throttle_group_unregister_tgm (tgm); + goto err; + } +/* Update group configuration */ +throttle_group_config (tgm, &cfg);
We'd also spare this, and this function would remain much simpler.
block/throttle.c line 136
Quoted 10 lines
block/throttle.c new file +10
-0+ + ret =0 ; + goto fin; + +err: + ret = -EINVAL ; +fin: +qemu_opts_del (opts); +return ret; +}
If you set ret = -EINVAL before calling goto err you can simplify this part as well, but feel free to ignore this suggestion.
Quoted 4 lines
block/throttle.c new file +4
-0+static int throttle_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue,Error **errp) +{ +ThrottleGroupMember *tgm =NULL ;
block/throttle.c line 238
Quoted 10 lines
block/throttle.c new file +10
-0+ +assert (reopen_state !=NULL ); +assert (reopen_state->bs !=NULL ); + + reopen_state->opaque =g_new0 (ThrottleGroupMember,1 ); + tgm = reopen_state->opaque; + +return throttle_configure_tgm (reopen_state->bs, tgm, reopen_state->options, + errp); +}
I would rename 'reopen_state' as 'state' for consistency with the other two functions.
block/throttle.c line 248
Quoted 9 lines
block/throttle.c new file +9
-0+static void throttle_reopen_commit (BDRVReopenState *state) +{ +ThrottleGroupMember *tgm = state->bs->opaque; + +throttle_group_unregister_tgm (tgm); +g_free (state->bs->opaque); + state->bs->opaque = state->opaque; + state->opaque =NULL ; +}
I also find the mixing of state->bs->opaque and tgm a bit confusing. Here's a suggestion, but feel free to ignore it:
ThrottleGroupMember *old_tgm = state->bs->opaque;
ThrottleGroupMember *new_tgm = state->opaque;
throttle_group_unregister_tgm(old_tgm);
g_free(old_tgm);
state->bs->opaque = new_tgm;
state->opaque = NULL;block/throttle.c line 257
Quoted 8 lines
block/throttle.c new file +8
-0+static void throttle_reopen_abort (BDRVReopenState *state) +{ +ThrottleGroupMember *tgm = state->opaque; + +throttle_group_unregister_tgm (tgm); +g_free (state->opaque); + state->opaque =NULL ; +}
Similar problem here (this one is simpler though).
Apart from the general question of whether we're parsing the limits in this driver, the rest are just comments about the style. Otherwise the code looks good, thanks!
Berto
Re: [Qemu-devel] [PATCH v4 5/7] block: add throttle block filter driver
Quoted 25 lines · Alberto Garcia
Quoted 9 lines · Manos Pitsidianakis
block/throttle.c new file +9
-0+/* Extract ThrottleConfig options. Assumes cfg is initialized and will be +* checked for validity. +* +* Returns -1 and sets errp if a burst_length value is over UINT_MAX. +*/ +static int throttle_extract_options (QemuOpts *opts,ThrottleConfig *cfg, +Error **errp) +{ +#define IF_OPT_SET (rvalue, opt_name) \block/throttle.c line 59
Quoted 10 lines
block/throttle.c new file +10
-0+ if (qemu_opt_get(opts, THROTTLE_OPT_PREFIX opt_name)) { \ + rvalue = qemu_opt_get_number(opts, THROTTLE_OPT_PREFIX opt_name, 0); } + +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_TOTAL ].avg,QEMU_OPT_BPS_TOTAL ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_READ ].avg,QEMU_OPT_BPS_READ ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_WRITE ].avg,QEMU_OPT_BPS_WRITE ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_TOTAL ].avg,QEMU_OPT_IOPS_TOTAL ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_READ ].avg,QEMU_OPT_IOPS_READ ); +IF_OPT_SET (cfg->buckets[THROTTLE_OPS_WRITE ].avg,QEMU_OPT_IOPS_WRITE ); +IF_OPT_SET (cfg->buckets[THROTTLE_BPS_TOTAL ].max,QEMU_OPT_BPS_TOTAL_MAX );[...]
This is all the code that I was saying that we'd save if we don't allow setting limits here.
Quoted 9 lines
block/throttle.c line 100
Quoted 7 lines
block/throttle.c new file +7
-0+static int throttle_configure_tgm (BlockDriverState *bs, +ThrottleGroupMember *tgm, +QDict *options,Error **errp) +{ +int ret; +ThrottleConfig cfg; +const char *group_name =NULL ;No need to set it to NULL here.
I know, I do it out of habit!
Quoted 54 lines
Quoted 18 lines
(quoted diff) +2
-0+ Error *local_err = NULL; + QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0,
&error_abort);block/throttle.c new file +14
-0+ +qemu_opts_absorb_qdict (opts, options, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto err; + } + +/* If group_name is NULL, an anonymous group will be created */ + group_name =qemu_opt_get (opts,QEMU_OPT_THROTTLE_GROUP_NAME ); + +/* Register membership to group with name group_name */ +throttle_group_register_tgm (tgm, group_name,bdrv_get_aio_context (bs)); + +/* Copy previous configuration */ block/throttle.c line 126
Quoted 10 lines
block/throttle.c new file +10
-0+throttle_group_get_config (tgm, &cfg); + +/* Change limits if user has specified them */ +if (throttle_extract_options (opts, &cfg, errp) || + !throttle_is_valid (&cfg, errp)) { +throttle_group_unregister_tgm (tgm); + goto err; + } +/* Update group configuration */ +throttle_group_config (tgm, &cfg);We'd also spare this, and this function would remain much simpler.
block/throttle.c line 136
Quoted 10 lines
block/throttle.c new file +10
-0+ + ret =0 ; + goto fin; + +err: + ret = -EINVAL ; +fin: +qemu_opts_del (opts); +return ret; +}If you set ret =
-EINVALbefore calling goto err you can simplify this part as well, but feel free to ignore this suggestion.Quoted 9 lines
block/throttle.c new file +9
-0+static int throttle_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue,Error **errp) +{ +ThrottleGroupMember *tgm =NULL ; + +assert (reopen_state !=NULL ); +assert (reopen_state->bs !=NULL ); + + reopen_state->opaque =g_new0 (ThrottleGroupMember,1 );
Quoted 8 lines
block/throttle.c line 238
Quoted 5 lines
block/throttle.c new file +5
-0+ tgm = reopen_state->opaque; + +return throttle_configure_tgm (reopen_state->bs, tgm, reopen_state->options, + errp); +}I would rename '
reopen_state' as 'state' for consistency with the other two functions.
The function signatures in block_int.h have reopen_state, so maybe for consistency I should change the other two to reopen_state as well, instead.
Quoted 13 lines
block/throttle.c line 248
Quoted 10 lines
block/throttle.c new file +9
-0+static void throttle_reopen_commit (BDRVReopenState *state) +{ +ThrottleGroupMember *tgm = state->bs->opaque; + +throttle_group_unregister_tgm (tgm); +g_free (state->bs->opaque); + state->bs->opaque = state->opaque; + state->opaque =NULL ; +}I also find the mixing of
state->bs->opaqueand tgm a bit confusing. Here's a suggestion, but feel free to ignore it:
You're right, though it's only a few lines it might require a second read. I will rewrite those more clearly, too.
Re: [Qemu-devel] [PATCH v4 5/7] block: add throttle block filter driver
In reply to Manos PitsidianakisQuoted 9 lines · Manos Pitsidianakis
block/throttle.c new file +9
-0+static int coroutine_fn throttle_co_preadv(BlockDriverState *bs, +uint64_t offset,uint64_t bytes, +QEMUIOVector *qiov,int flags) +{ + +ThrottleGroupMember *tgm = bs->opaque; +throttle_group_co_io_limits_intercept (tgm, bytes, false); + +return bdrv_co_preadv (bs->file, offset, bytes, qiov, flags);
block/throttle.c line 185
Quoted 10 lines
block/throttle.c new file +10
-0+} + +static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs, +uint64_t offset,uint64_t bytes, +QEMUIOVector *qiov,int flags) +{ +ThrottleGroupMember *tgm = bs->opaque; +throttle_group_co_io_limits_intercept (tgm, bytes, true); + +return bdrv_co_preadv (bs->file, offset, bytes, qiov, flags);
--------------------^
Tried some write throttling testing, noticed this. If anyone wants to test this iteration, change this to bdrv_co_pwritev(), I will correct this in the next version. (let's pretend this never happened!)
PATCH v4 6/7 block: add BlockDevOptionsThrottle to QAPI
In reply to Manos PitsidianakisThis is needed to configure throttle filter driver nodes with QAPI.
--- qapi/block-core.json | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
qapi/block-core.json +21 -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,24 @@ '*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 will be +# created if it doesn't already exist. If not specified, an +# anonymous group will be created, which cannot be +# referenced by other throttle nodes. +# @file: reference to or definition of the data source block device +# @limits: ThrottleLimits options +# Since: 2.11 +## +{ 'struct': 'BlockdevOptionsThrottle', + 'data': { '*throttle-group': 'str', + 'file' : 'BlockdevRef', + '*limits' : 'ThrottleLimits' + } } +## # @BlockdevOptions: # # Options for creating a block device. Many options are available for all
@@ -3155,6 +3174,7 @@ 'replication':'BlockdevOptionsReplication', 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', + 'throttle': 'BlockdevOptionsThrottle', 'vdi': 'BlockdevOptionsGenericFormat', 'vhdx': 'BlockdevOptionsGenericFormat', 'vmdk': 'BlockdevOptionsGenericCOWFormat',
Signature · 1 line
2.11.0
PATCH v4 7/7 block: add throttle block filter driver interface tests
In reply to Manos Pitsidianakis--- tests/qemu-iotests/184 | 310 +++++++++++++++++++++++++++++++++ tests/qemu-iotests/184.out | 422 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 733 insertions(+) create mode 100755 tests/qemu-iotests/184 create mode 100644 tests/qemu-iotests/184.out
tests/qemu-iotests/184 new file +310 -0
@@ -0,0 +1,310 @@ +#!/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 "== create anonymous 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": "throttle-anonymous", + "limits" : { + "iops-total" : 1000 + }, + "file": "disk0" + } +} +{ "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": "blockdev-add", + "arguments": { + "driver": "$IMGFMT", + "node-name": "disk0", + "file": { + "driver": "file", + "filename": "$TEST_IMG" + } + } +} +{ "execute": "blockdev-add", + "arguments": { + "driver": "throttle", + "node-name": "throttle0", + "throttle-group": "group0", + "limits" : { + "iops-total" : 128, + "iops-total-max" : 256, + "iops-total-max-length" : 60 + }, + "file": "disk0" + } +} +{ "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 "== specify both limits and group ==" + +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", + "limits" : { + "iops-total" : 256, + "iops-total-max": 2048, + "iops-total-max-length": 120 + }, + "file": "disk0" + } +} +{ "execute" : "qom-get", + "arguments" : { + "path" : "group0", + "property" : "limits" + } +} +{ "execute": "quit" } +EOF + +echo +echo "== specify neither limits nor 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 +422 -0
422 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 188 rw auto quick
Signature · 1 line
2.11.0