master
text 163 lines 6.2 KB
Raw
1 #!/usr/bin/env python3
2 # group: quick
3 #
4 # Test bitmap merges.
5 #
6 # Copyright (c) 2018 John Snow for Red Hat, Inc.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21 # owner=jsnow@redhat.com
22
23 import iotests
24 from iotests import log
25
26 iotests.script_initialize(supported_fmts=['generic'],
27 require_hmp=True)
28 size = 64 * 1024 * 1024
29 granularity = 64 * 1024
30
31 patterns = [("0x5d", "0", "64k"),
32 ("0xd5", "1M", "64k"),
33 ("0xdc", "32M", "64k"),
34 ("0xcd", "0x3ff0000", "64k")] # 64M - 64K
35
36 overwrite = [("0xab", "0", "64k"), # Full overwrite
37 ("0xad", "0x00f8000", "64k"), # Partial-left (1M-32K)
38 ("0x1d", "0x2008000", "64k"), # Partial-right (32M+32K)
39 ("0xea", "0x3fe0000", "64k")] # Adjacent-left (64M - 128K)
40
41 def query_bitmaps(vm):
42 res = vm.qmp("query-block")
43 return { "bitmaps": { device['device']: device.get('inserted', {}).get('dirty-bitmaps', []) for
44 device in res['return'] } }
45
46 with iotests.FilePath('img') as img_path, \
47 iotests.VM() as vm:
48
49 log('--- Preparing image & VM ---\n')
50 iotests.qemu_img_create('-f', iotests.imgfmt, img_path, str(size))
51 vm.add_drive(img_path)
52 vm.launch()
53
54 log('\n--- Adding preliminary bitmaps A & B ---\n')
55 vm.qmp_log("block-dirty-bitmap-add", node="drive0",
56 name="bitmapA", granularity=granularity)
57 vm.qmp_log("block-dirty-bitmap-add", node="drive0",
58 name="bitmapB", granularity=granularity)
59
60 # Dirties 4 clusters. count=262144
61 log('\n--- Emulating writes ---\n')
62 for p in patterns:
63 cmd = "write -P%s %s %s" % p
64 log(cmd)
65 log(vm.hmp_qemu_io("drive0", cmd))
66
67 log(query_bitmaps(vm), indent=2)
68
69 log('\n--- Submitting & Aborting Transaction ---\n')
70 vm.qmp_log("transaction", indent=2, actions=[
71 { "type": "block-dirty-bitmap-disable",
72 "data": { "node": "drive0", "name": "bitmapB" }},
73 { "type": "block-dirty-bitmap-add",
74 "data": { "node": "drive0", "name": "bitmapC",
75 "granularity": granularity }},
76 { "type": "block-dirty-bitmap-clear",
77 "data": { "node": "drive0", "name": "bitmapA" }},
78 { "type": "abort", "data": {}}
79 ])
80 log(query_bitmaps(vm), indent=2)
81
82 log('\n--- Disabling B & Adding C ---\n')
83 vm.qmp_log("transaction", indent=2, actions=[
84 { "type": "block-dirty-bitmap-disable",
85 "data": { "node": "drive0", "name": "bitmapB" }},
86 { "type": "block-dirty-bitmap-add",
87 "data": { "node": "drive0", "name": "bitmapC",
88 "granularity": granularity }},
89 # Purely extraneous, but test that it works:
90 { "type": "block-dirty-bitmap-disable",
91 "data": { "node": "drive0", "name": "bitmapC" }},
92 { "type": "block-dirty-bitmap-enable",
93 "data": { "node": "drive0", "name": "bitmapC" }},
94 ])
95
96 log('\n--- Emulating further writes ---\n')
97 # Dirties 6 clusters, 3 of which are new in contrast to "A".
98 # A = 64 * 1024 * (4 + 3) = 458752
99 # C = 64 * 1024 * 6 = 393216
100 for p in overwrite:
101 cmd = "write -P%s %s %s" % p
102 log(cmd)
103 log(vm.hmp_qemu_io("drive0", cmd))
104
105 log('\n--- Disabling A & C ---\n')
106 vm.qmp_log("transaction", indent=2, actions=[
107 { "type": "block-dirty-bitmap-disable",
108 "data": { "node": "drive0", "name": "bitmapA" }},
109 { "type": "block-dirty-bitmap-disable",
110 "data": { "node": "drive0", "name": "bitmapC" }}
111 ])
112
113 # A: 7 clusters
114 # B: 4 clusters
115 # C: 6 clusters
116 log(query_bitmaps(vm), indent=2)
117
118 log('\n--- Submitting & Aborting Merge Transaction ---\n')
119 vm.qmp_log("transaction", indent=2, actions=[
120 { "type": "block-dirty-bitmap-add",
121 "data": { "node": "drive0", "name": "bitmapD",
122 "disabled": True, "granularity": granularity }},
123 { "type": "block-dirty-bitmap-merge",
124 "data": { "node": "drive0", "target": "bitmapD",
125 "bitmaps": ["bitmapB", "bitmapC"] }},
126 { "type": "abort", "data": {}}
127 ])
128 log(query_bitmaps(vm), indent=2)
129
130 log('\n--- Creating D as a merge of B & C ---\n')
131 # Good hygiene: create a disabled bitmap as a merge target.
132 vm.qmp_log("transaction", indent=2, actions=[
133 { "type": "block-dirty-bitmap-add",
134 "data": { "node": "drive0", "name": "bitmapD",
135 "disabled": True, "granularity": granularity }},
136 { "type": "block-dirty-bitmap-merge",
137 "data": { "node": "drive0", "target": "bitmapD",
138 "bitmaps": ["bitmapB", "bitmapC"] }}
139 ])
140
141 # A and D should now both have 7 clusters apiece.
142 # B and C remain unchanged with 4 and 6 respectively.
143 log(query_bitmaps(vm), indent=2)
144
145 # A and D should be equivalent.
146 # Some formats round the size of the disk, so don't print the checksums.
147 check_a = vm.qmp('x-debug-block-dirty-bitmap-sha256',
148 node="drive0", name="bitmapA")['return']['sha256']
149 check_d = vm.qmp('x-debug-block-dirty-bitmap-sha256',
150 node="drive0", name="bitmapD")['return']['sha256']
151 assert(check_a == check_d)
152
153 log('\n--- Removing bitmaps A, B, C, and D ---\n')
154 vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="bitmapA")
155 vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="bitmapB")
156 vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="bitmapC")
157 vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="bitmapD")
158
159 log('\n--- Final Query ---\n')
160 log(query_bitmaps(vm), indent=2)
161
162 log('\n--- Done ---\n')
163 vm.shutdown()