qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eduardo Habkost <ehabkost@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, crosa@redhat.com,
	eblake@redhat.com, armbru@redhat.com, mreitz@redhat.com,
	kwolf@redhat.com, den@openvz.org, jsnow@redhat.com,
	famz@redhat.com, stefanha@redhat.com, pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH v3 2/3] scripts: add render_block_graph function for QEMUMachine
Date: Thu, 23 Aug 2018 14:56:17 -0300	[thread overview]
Message-ID: <20180823175617.GO3778@localhost.localdomain> (raw)
In-Reply-To: <20180823154655.40188-3-vsementsov@virtuozzo.com>

On Thu, Aug 23, 2018 at 06:46:54PM +0300, Vladimir Sementsov-Ogievskiy wrote:
> Render block nodes graph with help of graphviz. This new function is
> for debugging, so there is no sense to put it into qemu.py as a method
> of QEMUMachine. Let's instead put it separately.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  scripts/render_block_graph.py | 120 ++++++++++++++++++++++++++++++++++
>  1 file changed, 120 insertions(+)
>  create mode 100755 scripts/render_block_graph.py
> 
> diff --git a/scripts/render_block_graph.py b/scripts/render_block_graph.py
> new file mode 100755
> index 0000000000..382cc769ef
> --- /dev/null
> +++ b/scripts/render_block_graph.py
> @@ -0,0 +1,120 @@
> +#!/usr/bin/env python
> +#
> +# Render Qemu Block Graph
> +#
> +# Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
> +#
> +# 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
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +#
> +
> +import os
> +import sys
> +import subprocess
> +import json
> +from graphviz import Digraph
> +from qemu import MonitorResponseError
> +
> +
> +def perm(arr):
> +    s = 'w' if 'write' in arr else '_'
> +    s += 'r' if 'consistent-read' in arr else '_'
> +    s += 'u' if 'write-unchanged' in arr else '_'
> +    s += 'g' if 'graph-mod' in arr else '_'
> +    s += 's' if 'resize' in arr else '_'
> +    return s
> +
> +
> +def render_block_graph(qmp, filename, format='png'):
> +    '''
> +    Render graph in text (dot) representation into "@filename" and
> +    representation in @format into "@filename.@format"
> +    '''
> +
> +    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')
> +
> +    graph = Digraph(comment='Block Nodes Graph')
> +    graph.format = format
> +    graph.node('permission symbols:\l'
> +               '  w - Write\l'
> +               '  r - consistent-Read\l'
> +               '  u - write - Unchanged\l'
> +               '  g - Graph-mod\l'
> +               '  s - reSize\l'
> +               'edge label scheme:\l'
> +               '  <child type>\l'
> +               '  <perm>\l'
> +               '  <shared_perm>\l', shape='none')
> +
> +    for n in block_graph['nodes']:
> +        if n['type'] == 'bds':
> +            info = bds_nodes[n['name']]
> +            label = n['name'] + ' [' + info['drv'] + ']'
> +            if info['drv'] == 'file':
> +                label += '\n' + os.path.basename(info['file'])
> +            shape = 'ellipse'
> +        elif n['type'] == 'job':
> +            info = job_nodes[n['name']]
> +            label = info['type'] + ' job (' + n['name'] + ')'
> +            shape = 'box'
> +        else:
> +            assert n['type'] == 'blk'
> +            label = n['name'] if n['name'] else 'unnamed blk'
> +            shape = 'box'
> +
> +        graph.node(str(n['id']), label, shape=shape)
> +
> +    for e in block_graph['edges']:
> +        label = '%s\l%s\l%s\l' % (e['name'], perm(e['perm']),
> +                                  perm(e['shared-perm']))
> +        graph.edge(str(e['parent']), str(e['child']), label=label)
> +
> +    graph.render(filename)
> +
> +
> +class LibvirtGuest():
> +    def __init__(self, name):
> +        self.name = name
> +
> +    def command(self, cmd):
> +        # only supports qmp commands without parameters
> +        m = {'execute': cmd}
> +        ar = ['virsh', 'qemu-monitor-command', self.name, json.dumps(m)]
> +
> +        reply = json.loads(subprocess.check_output(ar))
> +
> +        if 'error' in reply:
> +            raise MonitorResponseError(reply)
> +
> +        return reply['return']

Interesting trick.  :)

Suggestion for a follow-up patch: we could move this helper class
to qmp.py, so other scripts could use it too.

> +
> +
> +if __name__ == '__main__':
> +    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)
> -- 
> 2.18.0
> 

-- 
Eduardo

  reply	other threads:[~2018-08-23 17:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-23 15:46 [Qemu-devel] [PATCH v3 0/3] block nodes graph visualization Vladimir Sementsov-Ogievskiy
2018-08-23 15:46 ` [Qemu-devel] [PATCH v3 1/3] qapi: add x-debug-query-block-graph Vladimir Sementsov-Ogievskiy
     [not found]   ` <2a8dda25-67e8-b710-7de3-00f5db68015e@redhat.com>
2018-10-02 13:01     ` Vladimir Sementsov-Ogievskiy
2018-10-05 19:34       ` Max Reitz
2018-10-08  9:40         ` Vladimir Sementsov-Ogievskiy
2018-08-23 15:46 ` [Qemu-devel] [PATCH v3 2/3] scripts: add render_block_graph function for QEMUMachine Vladimir Sementsov-Ogievskiy
2018-08-23 17:56   ` Eduardo Habkost [this message]
2018-08-23 17:57   ` Eduardo Habkost
2018-10-01 19:15   ` Max Reitz
2018-08-23 15:46 ` [Qemu-devel] [PATCH v3 3/3] not-for-commit: example of new command usage for debugging Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180823175617.GO3778@localhost.localdomain \
    --to=ehabkost@redhat.com \
    --cc=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).