block: enable stats-intervals for virtio-blk devs with -blockdev
PATCH 1/1 block: enable stats-intervals for virtio-blk devs with -blockdev
This patch allows the stats-intervals.* flag to be used with -blockdev. Stats collection is initialized for virtio-blk devices at their time of creation. However, it is limited to just virtio-blk devices for now.
--- block.c | 50 ++++++++++++++++++++++++++++++++ hw/block/virtio-blk.c | 6 ++++ include/block/block_int-common.h | 4 +++ qapi/block-core.json | 6 +++- 4 files changed, 65 insertions(+), 1 deletion(-)
block.c +50 -0
@@ -38,7 +38,9 @@#include "qapi/error.h" #include "qobject/qdict.h" #include "qobject/qjson.h" +#include "qobject/qlist.h" #include "qobject/qnull.h" +#include "qobject/qnum.h" #include "qobject/qstring.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qapi-visit-block-core.h"
@@ -3956,6 +3958,35 @@ out:return bs_snapshot; } +static bool bdrv_parse_stats_intervals (BlockDriverState *bs,QList *intervals, +Error **errp) +{ +unsigned i =0 ; +const QListEntry *entry; + bs->num_stats_intervals =qlist_size (intervals); + +if (bs->num_stats_intervals >0 ) { + bs->stats_intervals =g_new (uint64_t, bs->num_stats_intervals); + } + +for (entry =qlist_first (intervals); entry; entry =qlist_next (entry)) { +if (qobject_type (entry->value) ==QTYPE_QNUM ) { +uint64_t length =qnum_get_int (qobject_to (QNum, entry->value)); + +if (length >0 && length <=UINT_MAX ) { + bs->stats_intervals[i++] = length; + }else { +error_setg (errp,"Invalid interval length: %" PRId64, length); +return false; + } + }else { +error_setg (errp,"The specification of stats-intervals is invalid" ); +return false; + } + } +return true; +} +/* * Opens a disk image (raw, qcow2, vmdk, ...) *
@@ -3987,6 +4018,8 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,Error *local_err =NULL ;QDict *snapshot_options =NULL ;int snapshot_flags =0 ; +QDict *interval_dict =NULL ; +QList *interval_list =NULL ;assert (!child_class || !flags);assert (!child_class == !parent);
@@ -4205,6 +4238,19 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,g_free (child_key_dot); } +qdict_extract_subqdict (options, &interval_dict,"stats-intervals." ); +qdict_array_split (interval_dict, &interval_list); + +if (qdict_size (interval_dict) !=0 ) { +error_setg (errp,"Invalid option stats-intervals.%s" , +qdict_first (interval_dict)->key); + goto close_and_fail; + } + +if (!bdrv_parse_stats_intervals (bs, interval_list, errp)) { + goto close_and_fail; + } +/* Check if any unknown options were used */ if (qdict_size (options) !=0 ) {const QDictEntry *entry =qdict_first (options);
@@ -4261,6 +4307,8 @@ close_and_fail:bdrv_unref (bs);qobject_unref (snapshot_options);qobject_unref (options); +qobject_unref (interval_dict); +qobject_unref (interval_list);error_propagate (errp, local_err);return NULL ; }
@@ -5190,6 +5238,8 @@ static void GRAPH_UNLOCKED bdrv_close(BlockDriverState *bs) bs->full_open_options =NULL ;g_free (bs->block_status_cache); bs->block_status_cache =NULL ; +g_free (bs->stats_intervals); + bs->stats_intervals =NULL ;bdrv_release_named_dirty_bitmaps (bs);assert (QLIST_EMPTY (&bs->dirty_bitmaps));
hw/block/virtio-blk.c +6 -0
@@ -1814,6 +1814,12 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp) conf->conf.lcyls, conf->conf.lheads, conf->conf.lsecs); + +if (bs->stats_intervals) { +for (i =0 ; i < bs->num_stats_intervals; i++) { +block_acct_add_interval (blk_get_stats (s->blk), bs->stats_intervals[i]); + } + } }static void virtio_blk_device_unrealize (DeviceState *dev)
include/block/block_int-common.h +4 -0
@@ -1277,6 +1277,10 @@ struct BlockDriverState {/* array of write pointers' location of each zone in the zoned device. */ BlockZoneWps *wps; + +/* Array of intervals for collecting IO stats */ +uint64_t *stats_intervals; +unsigned int num_stats_intervals; };struct BlockBackendRootState {
qapi/block-core.json +5 -1
@@ -4771,6 +4771,9 @@ # @force-share: force share all permission on added nodes. Requires # read-only=true. (Since 2.10) # +# @stats-intervals: #optional list of intervals for collecting I/O +# statistics, in seconds (default: none) +# # Since: 2.9 ## { 'union': 'BlockdevOptions',
@@ -4782,7 +4785,8 @@ '*read-only': 'bool', '*auto-read-only': 'bool', '*force-share': 'bool', - '*detect-zeroes': 'BlockdevDetectZeroesOptions' }, + '*detect-zeroes': 'BlockdevDetectZeroesOptions', + '*stats-intervals': ['int'] }, 'discriminator': 'driver', 'data': { 'blkdebug': 'BlockdevOptionsBlkdebug',
Signature · 1 line
2.51.0
Am 30.09.2025 um 01:52 hat Chandan geschrieben:
Quoted 6 lines
This patch allows the stats-intervals.* flag to be used with -blockdev. Stats collection is initialized for virtio-blk devices at their time of creation. However, it is limited to just virtio-blk devices for now.
Signed-off-by Chandan <[email protected]>
Is it intentional that this (and the From: header) says only "Chandan" and not "Chandan Somani"?
One fundamental question I have with this patch is if statistics should be collected on the BlockBackend level (which is what we're doing today) or on the node level. This patch can't seem to decide, so you configure things on the node level, but then the actual collection happens on the BlockBackend level and setting on the node are ignored if it isn't directly used by a device.
If we keep things on the BlockBackend level, then intervals shouldn't be defined with -blockdev (which configures block nodes), but with -device (which configures the device that owns the BlockBackend). And conversely, if we think that things should be done on the node level, then we need to make sure that accounting actually works on the node level and is mostly done by the functions in block/io.c instead of the devices.
Quoted 43 lines
--- block.c | 50 ++++++++++++++++++++++++++++++++ hw/block/virtio-blk.c | 6 ++++ include/block/block_int-common.h | 4 +++ qapi/block-core.json | 6 +++- 4 files changed, 65 insertions(+), 1 deletion(-)block.c +20
-0@@ -38,7 +38,9 @@#include "qapi/error.h" #include "qobject/qdict.h" #include "qobject/qjson.h" +#include "qobject/qlist.h" #include "qobject/qnull.h" +#include "qobject/qnum.h" #include "qobject/qstring.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qapi-visit-block-core.h" @@ -3956,6 +3958,35 @@ out:return bs_snapshot; } +static bool bdrv_parse_stats_intervals (BlockDriverState *bs,QList *intervals, +Error **errp) +{ +unsigned i =0 ; +const QListEntry *entry; + bs->num_stats_intervals =qlist_size (intervals); + +if (bs->num_stats_intervals >0 ) { + bs->stats_intervals =g_new (uint64_t, bs->num_stats_intervals); + } + +for (entry =qlist_first (intervals); entry; entry =qlist_next (entry)) { +if (qobject_type (entry->value) ==QTYPE_QNUM ) { +uint64_t length =qnum_get_int (qobject_to (QNum, entry->value)); + +if (length >0 && length <=UINT_MAX ) { + bs->stats_intervals[i++] = length; + }else {
block.c out: · line 3988
Quoted 10 lines
block.c +10
-0+error_setg (errp,"Invalid interval length: %" PRId64, length); +return false; + } + }else { +error_setg (errp,"The specification of stats-intervals is invalid" ); +return false; + } + } +return true; +}
This is essentially a duplication of parse_stats_intervals() in blockdev.c. Can't we just call the existing function? Or in fact move it here and remove the existing call from blockdev_init() because -drive should be able to just use the per-node option that you're introducing here, too?
Quoted 14 lines
block.c +2
-0/* * Opens a disk image (raw, qcow2, vmdk, ...) * @@ -3987,6 +4018,8 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,Error *local_err =NULL ;QDict *snapshot_options =NULL ;int snapshot_flags =0 ; +QDict *interval_dict =NULL ; +QList *interval_list =NULL ;assert (!child_class || !flags);assert (!child_class == !parent);@@ -4205,6 +4238,19 @@ bdrv_open_inherit(const char *filename, const char *reference, QDict *options,g_free (child_key_dot);
block.c bdrv_open_inherit(const char *filename, const char *reference, QDict *options, · line 4248
Quoted 10 lines
block.c +8
-0} +qdict_extract_subqdict (options, &interval_dict,"stats-intervals." ); +qdict_array_split (interval_dict, &interval_list); + +if (qdict_size (interval_dict) !=0 ) { +error_setg (errp,"Invalid option stats-intervals.%s" , +qdict_first (interval_dict)->key); + goto close_and_fail; + }
I think I would move the above lines into bdrv_parse_stats_intervals().
Quoted 17 lines
block.c +6
-0+if (!bdrv_parse_stats_intervals (bs, interval_list, errp)) { + goto close_and_fail; + } +/* Check if any unknown options were used */ if (qdict_size (options) !=0 ) {const QDictEntry *entry =qdict_first (options);@@ -4261,6 +4307,8 @@ close_and_fail:bdrv_unref (bs);qobject_unref (snapshot_options);qobject_unref (options); +qobject_unref (interval_dict); +qobject_unref (interval_list);error_propagate (errp, local_err);return NULL ; }
block.c static void GRAPH_UNLOCKED bdrv_close(BlockDriverState *bs) · line 5242
Quoted 5 lines
block.c +2
-0@@ -5190,6 +5238,8 @@ static void GRAPH_UNLOCKED bdrv_close(BlockDriverState *bs) bs->full_open_options =NULL ;g_free (bs->block_status_cache); bs->block_status_cache =NULL ; +g_free (bs->stats_intervals); + bs->stats_intervals =NULL ;
Does this fix a preexisting memory leak for -drive?
Quoted 7 lines
block.c +0
-0bdrv_release_named_dirty_bitmaps (bs);assert (QLIST_EMPTY (&bs->dirty_bitmaps));hw/block/virtio-blk.c +0
-0
hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState *dev, Error **errp) · line 1823
Quoted 10 lines
hw/block/virtio-blk.c +6
-0@@ -1814,6 +1814,12 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp) conf->conf.lcyls, conf->conf.lheads, conf->conf.lsecs); + +if (bs->stats_intervals) { +for (i =0 ; i < bs->num_stats_intervals; i++) { +block_acct_add_interval (blk_get_stats (s->blk), bs->stats_intervals[i]); + } + } }
This could be part of block_acct_setup(), which is called by blkconf_apply_backend_options(). Then it would be available for more devices than just virtio-blk.
Quoted 21 lines
hw/block/virtio-blk.c +0
-0static void virtio_blk_device_unrealize (DeviceState *dev)include/block/block_int-common.h +4
-0@@ -1277,6 +1277,10 @@ struct BlockDriverState {/* array of write pointers' location of each zone in the zoned device. */ BlockZoneWps *wps; + +/* Array of intervals for collecting IO stats */ +uint64_t *stats_intervals; +unsigned int num_stats_intervals; };struct BlockBackendRootState {qapi/block-core.json +0
-0
qapi/block-core.json line 4775
Quoted 5 lines
qapi/block-core.json +2
-0@@ -4771,6 +4771,9 @@ # @force-share: force share all permission on added nodes. Requires # read-only=true. (Since 2.10) # +# @stats-intervals: #optional list of intervals for collecting I/O +# statistics, in seconds (default: none)
#optional is not a marker that is used anywhere else.
Can we document why there are multiple intervals and what effect they have on the statistics that you can query later?
We also need a '(Since 10.2)' note.
Quoted 5 lines
qapi/block-core.json +1
-0+# # Since: 2.9 ## { 'union': 'BlockdevOptions',
qapi/block-core.json line 4792
Quoted 9 lines
qapi/block-core.json +2
-1@@ -4782,7 +4785,8 @@ '*read-only': 'bool', '*auto-read-only': 'bool', '*force-share': 'bool', - '*detect-zeroes': 'BlockdevDetectZeroesOptions' }, + '*detect-zeroes': 'BlockdevDetectZeroesOptions', + '*stats-intervals': ['int'] }, 'discriminator': 'driver', 'data': { 'blkdebug': 'BlockdevOptionsBlkdebug',
Kevin
Quoted 18 lines · Kevin Wolf
Am 30.09.2025 um 01:52 hat Chandan geschrieben:
Quoted 6 lines
This patch allows the stats-intervals.* flag to be used with -blockdev. Stats collection is initialized for virtio-blk devices at their time of creation. However, it is limited to just virtio-blk devices for now.
Signed-off-by Chandan <[email protected]>Is it intentional that this (and the From: header) says only "Chandan" and not "Chandan Somani"?
One fundamental question I have with this patch is if statistics should be collected on the BlockBackend level (which is what we're doing today) or on the node level. This patch can't seem to decide, so you configure things on the node level, but then the actual collection happens on the BlockBackend level and setting on the node are ignored if it isn't directly used by a device.
Quoted 8 lines
If we keep things on the BlockBackend level, then intervals shouldn't be defined with -blockdev (which configures block nodes), but with -device (which configures the device that owns the BlockBackend). And conversely, if we think that things should be done on the node level, then we need to make sure that accounting actually works on the node level and is mostly done by the functions in
block/io.cinstead of the devices.
Problem I ran into was that the BlockBackends for the storage devices are hidden from block.c,
which is why I had to collect the stats in virtio_blk_device_realize() rather than bdrv_open_inherit().
I think it makes sense to keep it in the BlockBackend level because the storage devices are what we really want to collect stats for, so in v2, I add stats-intervals for storage devices with the -device option.
Quoted 136 lines
Quoted 23 lines
--- block.c | 50 ++++++++++++++++++++++++++++++++ hw/block/virtio-blk.c | 6 ++++ include/block/block_int-common.h | 4 +++ qapi/block-core.json | 6 +++- 4 files changed, 65 insertions(+), 1 deletion(-)block.c +2
-0@@ -38,7 +38,9 @@#include "qapi/error.h" #include "qobject/qdict.h" #include "qobject/qjson.h" +#include "qobject/qlist.h" #include "qobject/qnull.h" +#include "qobject/qnum.h" #include "qobject/qstring.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qapi-visit-block-core.h" block.c out: · line 3961
Quoted 4 lines
block.c +1
-0@@ -3956,6 +3958,35 @@ out:return bs_snapshot; } +static bool bdrv_parse_stats_intervals(BlockDriverState *bs, QList*intervals,
block.c out:
Quoted 11 lines
block.c +11
-0+ Error **errp) +{ + unsigned i = 0; + const QListEntry *entry; + bs->num_stats_intervals = qlist_size(intervals); + + if (bs->num_stats_intervals > 0) { + bs->stats_intervals = g_new(uint64_t, bs->num_stats_intervals); + } + + for (entry = qlist_first(intervals); entry; entry =
qlist_next(entry)) {block.c out: · line 3974
Quoted 2 lines
block.c +2
-0+if (qobject_type (entry->value) ==QTYPE_QNUM ) { + uint64_t length = qnum_get_int(qobject_to(QNum,
entry->value));block.c out:
Quoted 5 lines
block.c +5
-0+ +if (length >0 && length <=UINT_MAX ) { + bs->stats_intervals[i++] = length; + }else { + error_setg(errp, "Invalid interval length: %" PRId64,length);
block.c out:
Quoted 4 lines
block.c +4
-0+return false; + } + }else { + error_setg(errp, "The specification of stats-intervals isinvalid");
block.c out: · line 3988
Quoted 5 lines
block.c +5
-0+return false; + } + } +return true; +}This is essentially a duplication of
parse_stats_intervals()inblockdev.c. Can't we just call the existing function? Or in fact move it here and remove the existing call fromblockdev_init()because -drive should be able to just use the per-node option that you're introducing here, too?block.c bdrv_open_inherit(const char *filename, const char
Quoted 4 lines
block.c +0
-0/* * Opens a disk image (raw, qcow2, vmdk, ...) * @@ -3987,6 +4018,8 @@ bdrv_open_inherit(const char *filename, const char*reference, QDict *options,
block.c bdrv_open_inherit(const char *filename, const
Quoted 9 lines
block.c +2
-0Error *local_err =NULL ;QDict *snapshot_options =NULL ;int snapshot_flags =0 ; +QDict *interval_dict =NULL ; +QList *interval_list =NULL ;assert (!child_class || !flags);assert (!child_class == !parent);@@ -4205,6 +4238,19 @@ bdrv_open_inherit(const char *filename, constchar *reference, QDict *options,
block.c bdrv_open_inherit(const char *filename, const char *reference, QDict *options, · line 4248
Quoted 11 lines
block.c +8
-0g_free (child_key_dot); } +qdict_extract_subqdict (options, &interval_dict,"stats-intervals." ); +qdict_array_split (interval_dict, &interval_list); + +if (qdict_size (interval_dict) !=0 ) { +error_setg (errp,"Invalid option stats-intervals.%s" , +qdict_first (interval_dict)->key); + goto close_and_fail; + }I think I would move the above lines into
bdrv_parse_stats_intervals().block.c static void GRAPH_UNLOCKED
Quoted 17 lines
block.c +6
-0+if (!bdrv_parse_stats_intervals (bs, interval_list, errp)) { + goto close_and_fail; + } +/* Check if any unknown options were used */ if (qdict_size (options) !=0 ) {const QDictEntry *entry =qdict_first (options);@@ -4261,6 +4307,8 @@ close_and_fail:bdrv_unref (bs);qobject_unref (snapshot_options);qobject_unref (options); +qobject_unref (interval_dict); +qobject_unref (interval_list);error_propagate (errp, local_err);return NULL ; }@@ -5190,6 +5238,8 @@ static void GRAPH_UNLOCKED
bdrv_close(BlockDriverState *bs)block.c static void GRAPH_UNLOCKED bdrv_close(BlockDriverState *bs) · line 5242
Quoted 5 lines
block.c +2
-0bs->full_open_options =NULL ;g_free (bs->block_status_cache); bs->block_status_cache =NULL ; +g_free (bs->stats_intervals); + bs->stats_intervals =NULL ;Does this fix a preexisting memory leak for -drive?
hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState
Quoted 7 lines
block.c +0
-0bdrv_release_named_dirty_bitmaps (bs);assert (QLIST_EMPTY (&bs->dirty_bitmaps));hw/block/virtio-blk.c +0
-0@@ -1814,6 +1814,12 @@ static void virtio_blk_device_realize(DeviceState*dev, Error **errp)
hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
Quoted 7 lines
hw/block/virtio-blk.c +4
-0conf->conf.lcyls, conf->conf.lheads, conf->conf.lsecs); + +if (bs->stats_intervals) { +for (i =0 ; i < bs->num_stats_intervals; i++) { + block_acct_add_interval(blk_get_stats(s->blk),
Quoted 9 lines
bs->stats_intervals[i]);
hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState *dev, Error **errp) · line 1823
Quoted 3 lines
hw/block/virtio-blk.c +2
-0+ } + } }This could be part of
block_acct_setup(), which is called byblkconf_apply_backend_options(). Then it would be available for more devices than just virtio-blk.
Updated in v2 to do this.
Quoted 55 lines
Review
Quoted 3 lines
hw/block/virtio-blk.c +0
-0static void virtio_blk_device_unrealize (DeviceState *dev)(quoted diff) +0
-0
b/include/block/block_int-common.hinclude/block/block_int-common.h struct BlockDriverState { · line 1278
Quoted 6 lines
index
034c0634c8..1b4b7c636d100644include/block/block_int-common.h +0
-0@@ -1277,6 +1277,10 @@ struct BlockDriverState {/* array of write pointers' location of each zone in the zoned device. */
Quoted 13 lines
include/block/block_int-common.h +4
-0BlockZoneWps *wps; + +/* Array of intervals for collecting IO stats */ +uint64_t *stats_intervals; +unsigned int num_stats_intervals; };struct BlockBackendRootState {qapi/block-core.json +0
-0qapi/block-core.json line 4775
Quoted 5 lines
qapi/block-core.json +2
-0@@ -4771,6 +4771,9 @@ # @force-share: force share all permission on added nodes. Requires # read-only=true. (Since 2.10) # +# @stats-intervals: #optional list of intervals for collecting I/O +# statistics, in seconds (default: none)#optional is not a marker that is used anywhere else.
Can we document why there are multiple intervals and what effect they have on the statistics that you can query later?
We also need a '(Since 10.2)' note.
Quoted 5 lines
qapi/block-core.json +1
-0+# # Since: 2.9 ## { 'union': 'BlockdevOptions',qapi/block-core.json line 4792
Quoted 9 lines
qapi/block-core.json +2
-1@@ -4782,7 +4785,8 @@ '*read-only': 'bool', '*auto-read-only': 'bool', '*force-share': 'bool', - '*detect-zeroes': 'BlockdevDetectZeroesOptions' }, + '*detect-zeroes': 'BlockdevDetectZeroesOptions', + '*stats-intervals': ['int'] }, 'discriminator': 'driver', 'data': { 'blkdebug': 'BlockdevOptionsBlkdebug',Kevin
Quoted 7 lines · Kevin Wolf
Am 30.09.2025 um 01:52 hat Chandan geschrieben:
Quoted 6 lines
This patch allows the stats-intervals.* flag to be used with -blockdev. Stats collection is initialized for virtio-blk devices at their time of creation. However, it is limited to just virtio-blk devices for now.
Signed-off-by Chandan <[email protected]>
[...]
Quoted 5 lines
Quoted 5 lines
struct BlockBackendRootState {
qapi/block-core.json +0
-0
Quoted 8 lines
qapi/block-core.json line 4775
Quoted 6 lines
qapi/block-core.json +2
-0@@ -4771,6 +4771,9 @@ # @force-share: force share all permission on added nodes. Requires # read-only=true. (Since 2.10) # +# @stats-intervals: #optional list of intervals for collecting I/O +# statistics, in seconds (default: none)#optional is not a marker that is used anywhere else.
Because it gets rendered like this:
* stats-intervals ([int], *optional*) -- #optional list of intervals for collecting I/O statistics, in seconds (default:
none)
Drop it, please.
[...]