iotests/257: add Pattern class
[qemu/ar7.git] / tests / qemu-iotests / 257
blob02f9ae06490f6acec1cc49533430db1b433fe022
1 #!/usr/bin/env python
3 # Test bitmap-sync backups (incremental, differential, and partials)
5 # Copyright (c) 2019 John Snow for Red Hat, Inc.
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.
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.
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/>.
20 # owner=jsnow@redhat.com
22 import math
23 import os
25 import iotests
26 from iotests import log, qemu_img
28 SIZE = 64 * 1024 * 1024
29 GRANULARITY = 64 * 1024
32 class Pattern:
33 def __init__(self, byte, offset, size=GRANULARITY):
34 self.byte = byte
35 self.offset = offset
36 self.size = size
38 def bits(self, granularity):
39 lower = self.offset // granularity
40 upper = (self.offset + self.size - 1) // granularity
41 return set(range(lower, upper + 1))
44 class PatternGroup:
45 """Grouping of Pattern objects. Initialize with an iterable of Patterns."""
46 def __init__(self, patterns):
47 self.patterns = patterns
49 def bits(self, granularity):
50 """Calculate the unique bits dirtied by this pattern grouping"""
51 res = set()
52 for pattern in self.patterns:
53 res |= pattern.bits(granularity)
54 return res
57 GROUPS = [
58 PatternGroup([
59 # Batch 0: 4 clusters
60 Pattern('0x49', 0x0000000),
61 Pattern('0x6c', 0x0100000), # 1M
62 Pattern('0x6f', 0x2000000), # 32M
63 Pattern('0x76', 0x3ff0000)]), # 64M - 64K
64 PatternGroup([
65 # Batch 1: 6 clusters (3 new)
66 Pattern('0x65', 0x0000000), # Full overwrite
67 Pattern('0x77', 0x00f8000), # Partial-left (1M-32K)
68 Pattern('0x72', 0x2008000), # Partial-right (32M+32K)
69 Pattern('0x69', 0x3fe0000)]), # Adjacent-left (64M - 128K)
70 PatternGroup([
71 # Batch 2: 7 clusters (3 new)
72 Pattern('0x74', 0x0010000), # Adjacent-right
73 Pattern('0x69', 0x00e8000), # Partial-left (1M-96K)
74 Pattern('0x6e', 0x2018000), # Partial-right (32M+96K)
75 Pattern('0x67', 0x3fe0000,
76 2*GRANULARITY)]), # Overwrite [(64M-128K)-64M)
77 PatternGroup([
78 # Batch 3: 8 clusters (5 new)
79 # Carefully chosen such that nothing re-dirties the one cluster
80 # that copies out successfully before failure in Group #1.
81 Pattern('0xaa', 0x0010000,
82 3*GRANULARITY), # Overwrite and 2x Adjacent-right
83 Pattern('0xbb', 0x00d8000), # Partial-left (1M-160K)
84 Pattern('0xcc', 0x2028000), # Partial-right (32M+160K)
85 Pattern('0xdd', 0x3fc0000)]), # New; leaving a gap to the right
88 class Drive:
89 """Represents, vaguely, a drive attached to a VM.
90 Includes format, graph, and device information."""
92 def __init__(self, path, vm=None):
93 self.path = path
94 self.vm = vm
95 self.fmt = None
96 self.size = None
97 self.node = None
98 self.device = None
100 @property
101 def name(self):
102 return self.node or self.device
104 def img_create(self, fmt, size):
105 self.fmt = fmt
106 self.size = size
107 iotests.qemu_img_create('-f', self.fmt, self.path, str(self.size))
109 def create_target(self, name, fmt, size):
110 basename = os.path.basename(self.path)
111 file_node_name = "file_{}".format(basename)
112 vm = self.vm
114 log(vm.command('blockdev-create', job_id='bdc-file-job',
115 options={
116 'driver': 'file',
117 'filename': self.path,
118 'size': 0,
120 vm.run_job('bdc-file-job')
121 log(vm.command('blockdev-add', driver='file',
122 node_name=file_node_name, filename=self.path))
124 log(vm.command('blockdev-create', job_id='bdc-fmt-job',
125 options={
126 'driver': fmt,
127 'file': file_node_name,
128 'size': size,
130 vm.run_job('bdc-fmt-job')
131 log(vm.command('blockdev-add', driver=fmt,
132 node_name=name,
133 file=file_node_name))
134 self.fmt = fmt
135 self.size = size
136 self.node = name
138 def query_bitmaps(vm):
139 res = vm.qmp("query-block")
140 return {"bitmaps": {device['device'] or device['qdev']:
141 device.get('dirty-bitmaps', []) for
142 device in res['return']}}
144 def get_bitmap(bitmaps, drivename, name, recording=None):
146 get a specific bitmap from the object returned by query_bitmaps.
147 :param recording: If specified, filter results by the specified value.
149 for bitmap in bitmaps['bitmaps'][drivename]:
150 if bitmap.get('name', '') == name:
151 if recording is None:
152 return bitmap
153 elif bitmap.get('recording') == recording:
154 return bitmap
155 return None
157 def reference_backup(drive, n, filepath):
158 log("--- Reference Backup #{:d} ---\n".format(n))
159 target_id = "ref_target_{:d}".format(n)
160 job_id = "ref_backup_{:d}".format(n)
161 target_drive = Drive(filepath, vm=drive.vm)
163 target_drive.create_target(target_id, drive.fmt, drive.size)
164 drive.vm.qmp_log("blockdev-backup",
165 job_id=job_id, device=drive.name,
166 target=target_id, sync="full")
167 drive.vm.run_job(job_id, auto_dismiss=True)
168 log('')
170 def bitmap_backup(drive, n, filepath, bitmap, bitmap_mode):
171 log("--- Bitmap Backup #{:d} ---\n".format(n))
172 target_id = "bitmap_target_{:d}".format(n)
173 job_id = "bitmap_backup_{:d}".format(n)
174 target_drive = Drive(filepath, vm=drive.vm)
176 target_drive.create_target(target_id, drive.fmt, drive.size)
177 drive.vm.qmp_log("blockdev-backup", job_id=job_id, device=drive.name,
178 target=target_id, sync="bitmap",
179 bitmap_mode=bitmap_mode,
180 bitmap=bitmap,
181 auto_finalize=False)
182 return job_id
184 def perform_writes(drive, n):
185 log("--- Write #{:d} ---\n".format(n))
186 for pattern in GROUPS[n].patterns:
187 cmd = "write -P{:s} 0x{:07x} 0x{:x}".format(
188 pattern.byte,
189 pattern.offset,
190 pattern.size)
191 log(cmd)
192 log(drive.vm.hmp_qemu_io(drive.name, cmd))
193 bitmaps = query_bitmaps(drive.vm)
194 log(bitmaps, indent=2)
195 log('')
196 return bitmaps
198 def calculate_bits(groups=None):
199 """Calculate how many bits we expect to see dirtied."""
200 if groups:
201 bits = set.union(*(GROUPS[group].bits(GRANULARITY) for group in groups))
202 return len(bits)
203 return 0
205 def bitmap_comparison(bitmap, groups=None, want=0):
207 Print a nice human-readable message checking that this bitmap has as
208 many bits set as we expect it to.
210 log("= Checking Bitmap {:s} =".format(bitmap.get('name', '(anonymous)')))
212 if groups:
213 want = calculate_bits(groups)
214 have = bitmap['count'] // bitmap['granularity']
216 log("expecting {:d} dirty sectors; have {:d}. {:s}".format(
217 want, have, "OK!" if want == have else "ERROR!"))
218 log('')
220 def compare_images(image, reference, baseimg=None, expected_match=True):
222 Print a nice human-readable message comparing these images.
224 expected_ret = 0 if expected_match else 1
225 if baseimg:
226 assert qemu_img("rebase", "-u", "-b", baseimg, image) == 0
227 ret = qemu_img("compare", image, reference)
228 log('qemu_img compare "{:s}" "{:s}" ==> {:s}, {:s}'.format(
229 image, reference,
230 "Identical" if ret == 0 else "Mismatch",
231 "OK!" if ret == expected_ret else "ERROR!"),
232 filters=[iotests.filter_testfiles])
234 def test_bitmap_sync(bsync_mode, failure=None):
236 Test bitmap backup routines.
238 :param bsync_mode: Is the Bitmap Sync mode, and can be any of:
239 - on-success: This is the "incremental" style mode. Bitmaps are
240 synchronized to what was copied out only on success.
241 (Partial images must be discarded.)
242 - never: This is the "differential" style mode.
243 Bitmaps are never synchronized.
244 - always: This is a "best effort" style mode.
245 Bitmaps are always synchronized, regardless of failure.
246 (Partial images must be kept.)
248 :param failure: Is the (optional) failure mode, and can be any of:
249 - None: No failure. Test the normative path. Default.
250 - simulated: Cancel the job right before it completes.
251 This also tests writes "during" the job.
252 - intermediate: This tests a job that fails mid-process and produces
253 an incomplete backup. Testing limitations prevent
254 testing competing writes.
256 with iotests.FilePaths(['img', 'bsync1', 'bsync2',
257 'fbackup0', 'fbackup1', 'fbackup2']) as \
258 (img_path, bsync1, bsync2,
259 fbackup0, fbackup1, fbackup2), \
260 iotests.VM() as vm:
262 mode = "Bitmap Sync Mode {:s}".format(bsync_mode)
263 preposition = "with" if failure else "without"
264 cond = "{:s} {:s}".format(preposition,
265 "{:s} failure".format(failure) if failure
266 else "failure")
267 log("\n=== {:s} {:s} ===\n".format(mode, cond))
269 log('--- Preparing image & VM ---\n')
270 drive0 = Drive(img_path, vm=vm)
271 drive0.img_create(iotests.imgfmt, SIZE)
272 vm.add_device("{},id=scsi0".format(iotests.get_virtio_scsi_device()))
273 vm.launch()
275 file_config = {
276 'driver': 'file',
277 'filename': drive0.path
280 if failure == 'intermediate':
281 file_config = {
282 'driver': 'blkdebug',
283 'image': file_config,
284 'set-state': [{
285 'event': 'flush_to_disk',
286 'state': 1,
287 'new_state': 2
288 }, {
289 'event': 'read_aio',
290 'state': 2,
291 'new_state': 3
293 'inject-error': [{
294 'event': 'read_aio',
295 'errno': 5,
296 'state': 3,
297 'immediately': False,
298 'once': True
302 vm.qmp_log('blockdev-add',
303 filters=[iotests.filter_qmp_testfiles],
304 node_name="drive0",
305 driver=drive0.fmt,
306 file=file_config)
307 drive0.node = 'drive0'
308 drive0.device = 'device0'
309 # Use share-rw to allow writes directly to the node;
310 # The anonymous block-backend for this configuration prevents us
311 # from using HMP's qemu-io commands to address the device.
312 vm.qmp_log("device_add", id=drive0.device,
313 drive=drive0.name, driver="scsi-hd",
314 share_rw=True)
315 log('')
317 # 0 - Writes and Reference Backup
318 perform_writes(drive0, 0)
319 reference_backup(drive0, 0, fbackup0)
320 log('--- Add Bitmap ---\n')
321 vm.qmp_log("block-dirty-bitmap-add", node=drive0.name,
322 name="bitmap0", granularity=GRANULARITY)
323 log('')
325 # 1 - Writes and Reference Backup
326 bitmaps = perform_writes(drive0, 1)
327 dirty_groups = {1}
328 bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0')
329 bitmap_comparison(bitmap, groups=dirty_groups)
330 reference_backup(drive0, 1, fbackup1)
332 # 1 - Bitmap Backup (Optional induced failure)
333 if failure == 'intermediate':
334 # Activate blkdebug induced failure for second-to-next read
335 log(vm.hmp_qemu_io(drive0.name, 'flush'))
336 log('')
337 job = bitmap_backup(drive0, 1, bsync1, "bitmap0", bsync_mode)
339 def _callback():
340 """Issue writes while the job is open to test bitmap divergence."""
341 # Note: when `failure` is 'intermediate', this isn't called.
342 log('')
343 bitmaps = perform_writes(drive0, 2)
344 # Named bitmap (static, should be unchanged)
345 bitmap_comparison(get_bitmap(bitmaps, drive0.device, 'bitmap0'),
346 groups=dirty_groups)
347 # Anonymous bitmap (dynamic, shows new writes)
348 bitmap_comparison(get_bitmap(bitmaps, drive0.device, '',
349 recording=True), groups={2})
350 dirty_groups.add(2)
352 vm.run_job(job, auto_dismiss=True, auto_finalize=False,
353 pre_finalize=_callback,
354 cancel=(failure == 'simulated'))
355 bitmaps = query_bitmaps(vm)
356 bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0')
357 log(bitmaps, indent=2)
358 log('')
360 if ((bsync_mode == 'on-success' and not failure) or
361 (bsync_mode == 'always' and failure != 'intermediate')):
362 dirty_groups.remove(1)
364 if bsync_mode == 'always' and failure == 'intermediate':
365 # We manage to copy one sector (one bit) before the error.
366 bitmap_comparison(bitmap,
367 want=calculate_bits(groups=dirty_groups) - 1)
368 else:
369 bitmap_comparison(bitmap, groups=dirty_groups)
371 # 2 - Writes and Reference Backup
372 bitmaps = perform_writes(drive0, 3)
373 dirty_groups.add(3)
374 bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0')
375 if bsync_mode == 'always' and failure == 'intermediate':
376 # We're one bit short, still.
377 bitmap_comparison(bitmap,
378 want=calculate_bits(groups=dirty_groups) - 1)
379 else:
380 bitmap_comparison(bitmap, groups=dirty_groups)
381 reference_backup(drive0, 2, fbackup2)
383 # 2 - Bitmap Backup (In failure modes, this is a recovery.)
384 job = bitmap_backup(drive0, 2, bsync2, "bitmap0", bsync_mode)
385 vm.run_job(job, auto_dismiss=True, auto_finalize=False)
386 bitmaps = query_bitmaps(vm)
387 bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0')
388 log(bitmaps, indent=2)
389 log('')
390 bitmap_comparison(bitmap, groups={}
391 if bsync_mode != 'never'
392 else dirty_groups)
394 log('--- Cleanup ---\n')
395 vm.qmp_log("block-dirty-bitmap-remove",
396 node=drive0.name, name="bitmap0")
397 log(query_bitmaps(vm), indent=2)
398 vm.shutdown()
399 log('')
401 log('--- Verification ---\n')
402 # 'simulated' failures will actually all pass here because we canceled
403 # while "pending". This is actually undefined behavior,
404 # don't rely on this to be true!
405 compare_images(bsync1, fbackup1, baseimg=fbackup0,
406 expected_match=failure != 'intermediate')
407 if not failure or bsync_mode == 'always':
408 # Always keep the last backup on success or when using 'always'
409 base = bsync1
410 else:
411 base = fbackup0
412 compare_images(bsync2, fbackup2, baseimg=base)
413 compare_images(img_path, fbackup2)
414 log('')
416 def main():
417 for bsync_mode in ("never", "on-success", "always"):
418 for failure in ("simulated", "intermediate", None):
419 test_bitmap_sync(bsync_mode, failure)
421 if __name__ == '__main__':
422 iotests.script_main(main, supported_fmts=['qcow2'])