diff mbox series

[3/4] block: add dbg_dump_block_layer()

Message ID 20201119131634.14009-4-vsementsov@virtuozzo.com (mailing list archive)
State New, archived
Headers show
Series Add function to dump block layer for debugging | expand

Commit Message

Vladimir Sementsov-Ogievskiy Nov. 19, 2020, 1:16 p.m. UTC
Add function for debugging: we already have x-debug-query-block-graph
qmp command and scripts/render_block_graph.py which can dump
block-layer graph for running vm using qmp command. But when debug
block layer code, its often needed to dump some intermediate state
during some operation, so separate qmp command doesn't help.

So, let's introduce a function which can dump needed information into
json file.

In next commit we'll update scripts/render_block_graph.py so that it
will be able to parse json files.

For new function to not crash if we have mirror_top filter node not yet
bdrv_append()ed (to debug why it can't be appended), make
bdrv_get_allocated_file_size() and bdrv_refresh_filename() not crash on
filters without a child.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 qapi/block-core.json  | 13 +++++++++++
 include/block/block.h |  3 +++
 block.c               | 51 ++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 66 insertions(+), 1 deletion(-)

Comments

Eric Blake Nov. 19, 2020, 5:02 p.m. UTC | #1
On 11/19/20 7:16 AM, Vladimir Sementsov-Ogievskiy wrote:
> Add function for debugging: we already have x-debug-query-block-graph
> qmp command and scripts/render_block_graph.py which can dump
> block-layer graph for running vm using qmp command. But when debug
> block layer code, its often needed to dump some intermediate state
> during some operation, so separate qmp command doesn't help.
> 
> So, let's introduce a function which can dump needed information into
> json file.
> 
> In next commit we'll update scripts/render_block_graph.py so that it
> will be able to parse json files.
> 
> For new function to not crash if we have mirror_top filter node not yet
> bdrv_append()ed (to debug why it can't be appended), make
> bdrv_get_allocated_file_size() and bdrv_refresh_filename() not crash on
> filters without a child.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---

> +++ b/qapi/block-core.json
> @@ -1908,6 +1908,19 @@
>  ##
>  { 'command': 'x-debug-query-block-graph', 'returns': 'XDbgBlockGraph' }
>  
> +##
> +# @XDbgBlockLayerDump:
> +#
> +# Unite query-named-block-nodes, query-block-jobs and
> +# x-debug-query-block-graph results into one structure for block_layer_dump()
> +# function.
> +#
> +# Since: 5.3

The next release will be numbered 6.0, not 5.3.
diff mbox series

Patch

diff --git a/qapi/block-core.json b/qapi/block-core.json
index 04ad80bc1e..3f8e5a3822 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -1908,6 +1908,19 @@ 
 ##
 { 'command': 'x-debug-query-block-graph', 'returns': 'XDbgBlockGraph' }
 
+##
+# @XDbgBlockLayerDump:
+#
+# Unite query-named-block-nodes, query-block-jobs and
+# x-debug-query-block-graph results into one structure for block_layer_dump()
+# function.
+#
+# Since: 5.3
+##
+{ 'struct': 'XDbgBlockLayerDump',
+  'data': { 'nodes': ['BlockDeviceInfo'], 'jobs': ['BlockJobInfo'],
+            'graph': 'XDbgBlockGraph' } }
+
 ##
 # @drive-mirror:
 #
diff --git a/include/block/block.h b/include/block/block.h
index d17151abd7..6a75bbc84c 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -836,4 +836,7 @@  int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
                                     BdrvChild *dst, uint64_t dst_offset,
                                     uint64_t bytes, BdrvRequestFlags read_flags,
                                     BdrvRequestFlags write_flags);
+
+int dbg_dump_block_layer(const char *filename, Error **errp);
+
 #endif
diff --git a/block.c b/block.c
index eca16a5e29..82f847a88e 100644
--- a/block.c
+++ b/block.c
@@ -5035,7 +5035,8 @@  int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
         return -ENOTSUP;
     } else if (drv->is_filter) {
         /* Filter drivers default to the size of their filtered child */
-        return bdrv_get_allocated_file_size(bdrv_filter_bs(bs));
+        return bdrv_filter_child(bs) ?
+            bdrv_get_allocated_file_size(bdrv_filter_bs(bs)) : -ENOMEDIUM;
     } else {
         /* Other drivers default to summing their children's sizes */
         return bdrv_sum_allocated_file_size(bs);
@@ -5382,6 +5383,51 @@  XDbgBlockGraph *bdrv_get_xdbg_block_graph(void)
     return xdbg_graph_finalize(gr);
 }
 
+int dbg_dump_block_layer(const char *filename, Error **errp)
+{
+    ERRP_GUARD();
+    Visitor *v;
+    g_autoptr(XDbgBlockLayerDump) dump = g_new0(XDbgBlockLayerDump, 1);
+    QObject *obj = NULL;
+    QString *json;
+    FILE *f;
+
+    dump->nodes = bdrv_named_nodes_list(false, errp);
+    if (*errp) {
+        return -EINVAL;
+    }
+    dump->jobs = block_jobs_info_list(errp);
+    if (*errp) {
+        return -EINVAL;
+    }
+    dump->graph = bdrv_get_xdbg_block_graph();
+
+    v = qobject_output_visitor_new(&obj);
+    if (visit_type_XDbgBlockLayerDump(v, "unused", &dump, errp)) {
+        visit_complete(v, &obj);
+    }
+    visit_free(v);
+    if (*errp) {
+        return -EINVAL;
+    }
+
+    json = qobject_to_json_pretty(obj);
+    qobject_unref(obj);
+
+    f = fopen(filename, "w");
+    if (!f) {
+        error_setg_errno(errp, errno, "Can't open file '%s'", filename);
+        qobject_unref(json);
+        return -EINVAL;
+    }
+
+    fputs(qstring_get_str(json), f);
+    fclose(f);
+    qobject_unref(json);
+
+    return 0;
+}
+
 BlockDriverState *bdrv_lookup_bs(const char *device,
                                  const char *node_name,
                                  Error **errp)
@@ -6885,6 +6931,9 @@  void bdrv_refresh_filename(BlockDriverState *bs)
     if (bs->implicit) {
         /* For implicit nodes, just copy everything from the single child */
         child = QLIST_FIRST(&bs->children);
+        if (!child) {
+            return;
+        }
         assert(QLIST_NEXT(child, next) == NULL);
 
         pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),