diff mbox series

[4/4] scripts/render_block_graph.py: add ability to parse json files

Message ID 20201119131634.14009-5-vsementsov@virtuozzo.com
State New
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 an option to use json file (generated by dbg_dump_block_layer())
as input.

Also, add myself as maintainer of the script.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 MAINTAINERS                   |  5 ++++
 scripts/render_block_graph.py | 53 ++++++++++++++++++++++-------------
 2 files changed, 39 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 2e018a0c1d..e780c89e0e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2408,6 +2408,11 @@  M: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
 S: Maintained
 F: scripts/simplebench/
 
+Block graph visualization
+M: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
+S: Maintained
+F: scripts/render_block_graph.py
+
 QAPI
 M: Markus Armbruster <armbru@redhat.com>
 M: Michael Roth <mdroth@linux.vnet.ibm.com>
diff --git a/scripts/render_block_graph.py b/scripts/render_block_graph.py
index da6acf050d..4a11aba697 100755
--- a/scripts/render_block_graph.py
+++ b/scripts/render_block_graph.py
@@ -2,7 +2,7 @@ 
 #
 # Render Qemu Block Graph
 #
-# Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
+# Copyright (c) 2018-2020 Virtuozzo International GmbH.
 #
 # 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
@@ -40,19 +40,14 @@  def perm(arr):
     return s
 
 
-def render_block_graph(qmp, filename, format='png'):
+def do_render_block_graph(nodes, jobs, block_graph, filename, format='png'):
     '''
     Render graph in text (dot) representation into "@filename" and
     representation in @format into "@filename.@format"
     '''
+    bds_nodes = {n['node-name']: n for n in nodes}
 
-    bds_nodes = qmp.command('query-named-block-nodes')
-    bds_nodes = {n['node-name']: n for n in bds_nodes}
-
-    job_nodes = qmp.command('query-block-jobs')
-    job_nodes = {n['device']: n for n in job_nodes}
-
-    block_graph = qmp.command('x-debug-query-block-graph')
+    job_nodes = {n['device']: n for n in jobs}
 
     graph = Digraph(comment='Block Nodes Graph')
     graph.format = format
@@ -93,6 +88,19 @@  def render_block_graph(qmp, filename, format='png'):
     graph.render(filename)
 
 
+def render_block_graph(qmp, filename, format='png'):
+    '''
+    Render graph in text (dot) representation into "@filename" and
+    representation in @format into "@filename.@format"
+    '''
+
+    nodes = qmp.command('query-named-block-nodes')
+    jobs = qmp.command('query-block-jobs')
+    graph = qmp.command('x-debug-query-block-graph')
+
+    do_render_block_graph(nodes, jobs, graph, filename, format)
+
+
 class LibvirtGuest():
     def __init__(self, name):
         self.name = name
@@ -111,15 +119,22 @@  class LibvirtGuest():
 
 
 if __name__ == '__main__':
-    obj = sys.argv[1]
-    out = sys.argv[2]
-
-    if os.path.exists(obj):
-        # assume unix socket
-        qmp = QEMUMonitorProtocol(obj)
-        qmp.connect()
+    if sys.argv[1] == '--json':
+        json_file = sys.argv[2]
+        out = sys.argv[3]
+        with open(json_file) as f:
+            dump = json.load(f)
+        do_render_block_graph(dump['nodes'], dump['jobs'], dump['graph'], out)
     else:
-        # assume libvirt guest name
-        qmp = LibvirtGuest(obj)
+        obj = sys.argv[1]
+        out = sys.argv[2]
+
+        if os.path.exists(obj):
+            # assume unix socket
+            qmp = QEMUMonitorProtocol(obj)
+            qmp.connect()
+        else:
+            # assume libvirt guest name
+            qmp = LibvirtGuest(obj)
 
-    render_block_graph(qmp, out)
+        render_block_graph(qmp, out)