2
0

render_block_graph.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  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. import os
  21. import sys
  22. import subprocess
  23. import json
  24. from graphviz import Digraph
  25. sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
  26. from qemu.machine import MonitorResponseError
  27. def perm(arr):
  28. s = 'w' if 'write' in arr else '_'
  29. s += 'r' if 'consistent-read' in arr else '_'
  30. s += 'u' if 'write-unchanged' in arr else '_'
  31. s += 'g' if 'graph-mod' in arr else '_'
  32. s += 's' if 'resize' in arr else '_'
  33. return s
  34. def render_block_graph(qmp, filename, format='png'):
  35. '''
  36. Render graph in text (dot) representation into "@filename" and
  37. representation in @format into "@filename.@format"
  38. '''
  39. bds_nodes = qmp.command('query-named-block-nodes')
  40. bds_nodes = {n['node-name']: n for n in bds_nodes}
  41. job_nodes = qmp.command('query-block-jobs')
  42. job_nodes = {n['device']: n for n in job_nodes}
  43. block_graph = qmp.command('x-debug-query-block-graph')
  44. graph = Digraph(comment='Block Nodes Graph')
  45. graph.format = format
  46. graph.node('permission symbols:\l'
  47. ' w - Write\l'
  48. ' r - consistent-Read\l'
  49. ' u - write - Unchanged\l'
  50. ' g - Graph-mod\l'
  51. ' s - reSize\l'
  52. 'edge label scheme:\l'
  53. ' <child type>\l'
  54. ' <perm>\l'
  55. ' <shared_perm>\l', shape='none')
  56. for n in block_graph['nodes']:
  57. if n['type'] == 'block-driver':
  58. info = bds_nodes[n['name']]
  59. label = n['name'] + ' [' + info['drv'] + ']'
  60. if info['drv'] == 'file':
  61. label += '\n' + os.path.basename(info['file'])
  62. shape = 'ellipse'
  63. elif n['type'] == 'block-job':
  64. info = job_nodes[n['name']]
  65. label = info['type'] + ' job (' + n['name'] + ')'
  66. shape = 'box'
  67. else:
  68. assert n['type'] == 'block-backend'
  69. label = n['name'] if n['name'] else 'unnamed blk'
  70. shape = 'box'
  71. graph.node(str(n['id']), label, shape=shape)
  72. for e in block_graph['edges']:
  73. label = '%s\l%s\l%s\l' % (e['name'], perm(e['perm']),
  74. perm(e['shared-perm']))
  75. graph.edge(str(e['parent']), str(e['child']), label=label)
  76. graph.render(filename)
  77. class LibvirtGuest():
  78. def __init__(self, name):
  79. self.name = name
  80. def command(self, cmd):
  81. # only supports qmp commands without parameters
  82. m = {'execute': cmd}
  83. ar = ['virsh', 'qemu-monitor-command', self.name, json.dumps(m)]
  84. reply = json.loads(subprocess.check_output(ar))
  85. if 'error' in reply:
  86. raise MonitorResponseError(reply)
  87. return reply['return']
  88. if __name__ == '__main__':
  89. obj = sys.argv[1]
  90. out = sys.argv[2]
  91. if os.path.exists(obj):
  92. # assume unix socket
  93. qmp = QEMUMonitorProtocol(obj)
  94. qmp.connect()
  95. else:
  96. # assume libvirt guest name
  97. qmp = LibvirtGuest(obj)
  98. render_block_graph(qmp, out)