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