master
py 126 lines 3.78 KB
Raw
1 #!/usr/bin/env python3
2 #
3 # Render Qemu Block Graph
4 #
5 # Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import os
22 import sys
23 import subprocess
24 import json
25 from graphviz import Digraph
26
27 try:
28 from qemu.qmp import QMPError
29 from qemu.qmp.legacy import QEMUMonitorProtocol
30 except ModuleNotFoundError as exc:
31 print(f"Module '{exc.name}' not found.", file=sys.stderr)
32 print(f"Try $builddir/run {' '.join(sys.argv)}", file=sys.stderr)
33 sys.exit(1)
34
35
36 def perm(arr):
37 s = 'w' if 'write' in arr else '_'
38 s += 'r' if 'consistent-read' in arr else '_'
39 s += 'u' if 'write-unchanged' in arr else '_'
40 s += 's' if 'resize' in arr else '_'
41 return s
42
43
44 def render_block_graph(qmp, filename, format='png'):
45 '''
46 Render graph in text (dot) representation into "@filename" and
47 representation in @format into "@filename.@format"
48 '''
49
50 bds_nodes = qmp.cmd('query-named-block-nodes')
51 bds_nodes = {n['node-name']: n for n in bds_nodes}
52
53 job_nodes = qmp.cmd('query-block-jobs')
54 job_nodes = {n['device']: n for n in job_nodes}
55
56 block_graph = qmp.cmd('x-debug-query-block-graph')
57
58 graph = Digraph(comment='Block Nodes Graph')
59 graph.format = format
60 graph.node('permission symbols:\l'
61 ' w - Write\l'
62 ' r - consistent-Read\l'
63 ' u - write - Unchanged\l'
64 ' g - Graph-mod\l'
65 ' s - reSize\l'
66 'edge label scheme:\l'
67 ' <child type>\l'
68 ' <perm>\l'
69 ' <shared_perm>\l', shape='none')
70
71 for n in block_graph['nodes']:
72 if n['type'] == 'block-driver':
73 info = bds_nodes[n['name']]
74 label = n['name'] + ' [' + info['drv'] + ']'
75 if info['drv'] == 'file':
76 label += '\n' + os.path.basename(info['file'])
77 shape = 'ellipse'
78 elif n['type'] == 'block-job':
79 info = job_nodes[n['name']]
80 label = info['type'] + ' job (' + n['name'] + ')'
81 shape = 'box'
82 else:
83 assert n['type'] == 'block-backend'
84 label = n['name'] if n['name'] else 'unnamed blk'
85 shape = 'box'
86
87 graph.node(str(n['id']), label, shape=shape)
88
89 for e in block_graph['edges']:
90 label = '%s\l%s\l%s\l' % (e['name'], perm(e['perm']),
91 perm(e['shared-perm']))
92 graph.edge(str(e['parent']), str(e['child']), label=label)
93
94 graph.render(filename)
95
96
97 class LibvirtGuest():
98 def __init__(self, name):
99 self.name = name
100
101 def cmd(self, cmd):
102 # only supports qmp commands without parameters
103 m = {'execute': cmd}
104 ar = ['virsh', 'qemu-monitor-command', self.name, json.dumps(m)]
105
106 reply = json.loads(subprocess.check_output(ar))
107
108 if 'error' in reply:
109 raise QMPError(reply)
110
111 return reply['return']
112
113
114 if __name__ == '__main__':
115 obj = sys.argv[1]
116 out = sys.argv[2]
117
118 if os.path.exists(obj):
119 # assume unix socket
120 qmp = QEMUMonitorProtocol(obj)
121 qmp.connect()
122 else:
123 # assume libvirt guest name
124 qmp = LibvirtGuest(obj)
125
126 render_block_graph(qmp, out)