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