1#!/usr/bin/env python3
2#
3# Benchmark block jobs
4#
5# Copyright (c) 2019 Virtuozzo International GmbH.
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
22import sys
23import os
24import subprocess
25import socket
26import json
27
28sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
29from qemu.machine import QEMUMachine
30from qemu.qmp import QMPConnectError
31from qemu.aqmp import ConnectError
32
33
34def bench_block_job(cmd, cmd_args, qemu_args):
35    """Benchmark block-job
36
37    cmd       -- qmp command to run block-job (like blockdev-backup)
38    cmd_args  -- dict of qmp command arguments
39    qemu_args -- list of Qemu command line arguments, including path to Qemu
40                 binary
41
42    Returns {'seconds': int} on success and {'error': str} on failure, dict may
43    contain addional 'vm-log' field. Return value is compatible with
44    simplebench lib.
45    """
46
47    vm = QEMUMachine(qemu_args[0], args=qemu_args[1:])
48
49    try:
50        vm.launch()
51    except OSError as e:
52        return {'error': 'popen failed: ' + str(e)}
53    except (QMPConnectError, ConnectError, socket.timeout):
54        return {'error': 'qemu failed: ' + str(vm.get_log())}
55
56    try:
57        res = vm.qmp(cmd, **cmd_args)
58        if res != {'return': {}}:
59            vm.shutdown()
60            return {'error': '"{}" command failed: {}'.format(cmd, str(res))}
61
62        e = vm.event_wait('JOB_STATUS_CHANGE')
63        assert e['data']['status'] == 'created'
64        start_ms = e['timestamp']['seconds'] * 1000000 + \
65            e['timestamp']['microseconds']
66
67        e = vm.events_wait((('BLOCK_JOB_READY', None),
68                            ('BLOCK_JOB_COMPLETED', None),
69                            ('BLOCK_JOB_FAILED', None)), timeout=True)
70        if e['event'] not in ('BLOCK_JOB_READY', 'BLOCK_JOB_COMPLETED'):
71            vm.shutdown()
72            return {'error': 'block-job failed: ' + str(e),
73                    'vm-log': vm.get_log()}
74        if 'error' in e['data']:
75            vm.shutdown()
76            return {'error': 'block-job failed: ' + e['data']['error'],
77                    'vm-log': vm.get_log()}
78        end_ms = e['timestamp']['seconds'] * 1000000 + \
79            e['timestamp']['microseconds']
80    finally:
81        vm.shutdown()
82
83    return {'seconds': (end_ms - start_ms) / 1000000.0}
84
85
86def get_image_size(path):
87    out = subprocess.run(['qemu-img', 'info', '--out=json', path],
88                         stdout=subprocess.PIPE, check=True).stdout
89    return json.loads(out)['virtual-size']
90
91
92def get_blockdev_size(obj):
93    img = obj['filename'] if 'filename' in obj else obj['file']['filename']
94    return get_image_size(img)
95
96
97# Bench backup or mirror
98def bench_block_copy(qemu_binary, cmd, cmd_options, source, target):
99    """Helper to run bench_block_job() for mirror or backup"""
100    assert cmd in ('blockdev-backup', 'blockdev-mirror')
101
102    if target['driver'] == 'qcow2':
103        try:
104            os.remove(target['file']['filename'])
105        except OSError:
106            pass
107
108        subprocess.run(['qemu-img', 'create', '-f', 'qcow2',
109                        target['file']['filename'],
110                        str(get_blockdev_size(source))],
111                       stdout=subprocess.DEVNULL,
112                       stderr=subprocess.DEVNULL, check=True)
113
114    source['node-name'] = 'source'
115    target['node-name'] = 'target'
116
117    cmd_options['job-id'] = 'job0'
118    cmd_options['device'] = 'source'
119    cmd_options['target'] = 'target'
120    cmd_options['sync'] = 'full'
121
122    return bench_block_job(cmd, cmd_options,
123                           [qemu_binary,
124                            '-blockdev', json.dumps(source),
125                            '-blockdev', json.dumps(target)])
126
127
128def drv_file(filename, o_direct=True):
129    node = {'driver': 'file', 'filename': filename}
130    if o_direct:
131        node['cache'] = {'direct': True}
132        node['aio'] = 'native'
133
134    return node
135
136
137def drv_nbd(host, port):
138    return {'driver': 'nbd',
139            'server': {'type': 'inet', 'host': host, 'port': port}}
140
141
142def drv_qcow2(file):
143    return {'driver': 'qcow2', 'file': file}
144
145
146if __name__ == '__main__':
147    import sys
148
149    if len(sys.argv) < 4:
150        print('USAGE: {} <qmp block-job command name> '
151              '<json string of arguments for the command> '
152              '<qemu binary path and arguments>'.format(sys.argv[0]))
153        exit(1)
154
155    res = bench_block_job(sys.argv[1], json.loads(sys.argv[2]), sys.argv[3:])
156    if 'seconds' in res:
157        print('{:.2f}'.format(res['seconds']))
158    else:
159        print(res)
160