migration: introduce icount field for snapshots
[qemu/ar7.git] / tests / qemu-iotests / 199
blob58fad872a12ce90b2e1c3857304582b772ac7b3d
1 #!/usr/bin/env python3
3 # Tests for dirty bitmaps postcopy migration.
5 # Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
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/>.
21 import os
22 import iotests
23 from iotests import qemu_img
25 debug = False
27 disk_a = os.path.join(iotests.test_dir, 'disk_a')
28 disk_b = os.path.join(iotests.test_dir, 'disk_b')
29 size = '256G'
30 fifo = os.path.join(iotests.test_dir, 'mig_fifo')
32 granularity = 512
33 nb_bitmaps = 15
35 GiB = 1024 * 1024 * 1024
37 discards1 = (
38     (0, GiB),
39     (2 * GiB + 512 * 5, 512),
40     (3 * GiB + 512 * 5, 512),
41     (100 * GiB, GiB)
44 discards2 = (
45     (3 * GiB + 512 * 8, 512),
46     (4 * GiB + 512 * 8, 512),
47     (50 * GiB, GiB),
48     (100 * GiB + GiB // 2, GiB)
52 def apply_discards(vm, discards):
53     for d in discards:
54         vm.hmp_qemu_io('drive0', 'discard {} {}'.format(*d))
57 def event_seconds(event):
58     return event['timestamp']['seconds'] + \
59         event['timestamp']['microseconds'] / 1000000.0
62 def event_dist(e1, e2):
63     return event_seconds(e2) - event_seconds(e1)
66 def check_bitmaps(vm, count):
67     result = vm.qmp('query-block')
69     if count == 0:
70         assert 'dirty-bitmaps' not in result['return'][0]
71     else:
72         assert len(result['return'][0]['dirty-bitmaps']) == count
75 class TestDirtyBitmapPostcopyMigration(iotests.QMPTestCase):
76     def tearDown(self):
77         if debug:
78             self.vm_a_events += self.vm_a.get_qmp_events()
79             self.vm_b_events += self.vm_b.get_qmp_events()
80             for e in self.vm_a_events:
81                 e['vm'] = 'SRC'
82             for e in self.vm_b_events:
83                 e['vm'] = 'DST'
84             events = (self.vm_a_events + self.vm_b_events)
85             events = [(e['timestamp']['seconds'],
86                        e['timestamp']['microseconds'],
87                        e['vm'],
88                        e['event'],
89                        e.get('data', '')) for e in events]
90             for e in sorted(events):
91                 print('{}.{:06} {} {} {}'.format(*e))
93         self.vm_a.shutdown()
94         self.vm_b.shutdown()
95         os.remove(disk_a)
96         os.remove(disk_b)
97         os.remove(fifo)
99     def setUp(self):
100         os.mkfifo(fifo)
101         qemu_img('create', '-f', iotests.imgfmt, disk_a, size)
102         qemu_img('create', '-f', iotests.imgfmt, disk_b, size)
103         self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a,
104                                                           'discard=unmap')
105         self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b,
106                                                           'discard=unmap')
107         self.vm_b.add_incoming("exec: cat '" + fifo + "'")
108         self.vm_a.launch()
109         self.vm_b.launch()
111         # collect received events for debug
112         self.vm_a_events = []
113         self.vm_b_events = []
115     def start_postcopy(self):
116         """ Run migration until RESUME event on target. Return this event. """
117         for i in range(nb_bitmaps):
118             result = self.vm_a.qmp('block-dirty-bitmap-add', node='drive0',
119                                    name='bitmap{}'.format(i),
120                                    granularity=granularity,
121                                    persistent=True)
122             self.assert_qmp(result, 'return', {})
124         result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
125                                node='drive0', name='bitmap0')
126         empty_sha256 = result['return']['sha256']
128         apply_discards(self.vm_a, discards1)
130         result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
131                                node='drive0', name='bitmap0')
132         self.discards1_sha256 = result['return']['sha256']
134         # Check, that updating the bitmap by discards works
135         assert self.discards1_sha256 != empty_sha256
137         # We want to calculate resulting sha256. Do it in bitmap0, so, disable
138         # other bitmaps
139         for i in range(1, nb_bitmaps):
140             result = self.vm_a.qmp('block-dirty-bitmap-disable', node='drive0',
141                                    name='bitmap{}'.format(i))
142             self.assert_qmp(result, 'return', {})
144         apply_discards(self.vm_a, discards2)
146         result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
147                                node='drive0', name='bitmap0')
148         self.all_discards_sha256 = result['return']['sha256']
150         # Now, enable some bitmaps, to be updated during migration
151         for i in range(2, nb_bitmaps, 2):
152             result = self.vm_a.qmp('block-dirty-bitmap-enable', node='drive0',
153                                    name='bitmap{}'.format(i))
154             self.assert_qmp(result, 'return', {})
156         caps = [{'capability': 'dirty-bitmaps', 'state': True},
157                 {'capability': 'events', 'state': True}]
159         result = self.vm_a.qmp('migrate-set-capabilities', capabilities=caps)
160         self.assert_qmp(result, 'return', {})
162         result = self.vm_b.qmp('migrate-set-capabilities', capabilities=caps)
163         self.assert_qmp(result, 'return', {})
165         result = self.vm_a.qmp('migrate', uri='exec:cat>' + fifo)
166         self.assert_qmp(result, 'return', {})
168         result = self.vm_a.qmp('migrate-start-postcopy')
169         self.assert_qmp(result, 'return', {})
171         event_resume = self.vm_b.event_wait('RESUME')
172         self.vm_b_events.append(event_resume)
173         return event_resume
175     def test_postcopy_success(self):
176         event_resume = self.start_postcopy()
178         # enabled bitmaps should be updated
179         apply_discards(self.vm_b, discards2)
181         match = {'data': {'status': 'completed'}}
182         event_complete = self.vm_b.event_wait('MIGRATION', match=match)
183         self.vm_b_events.append(event_complete)
185         # take queued event, should already been happened
186         event_stop = self.vm_a.event_wait('STOP')
187         self.vm_a_events.append(event_stop)
189         downtime = event_dist(event_stop, event_resume)
190         postcopy_time = event_dist(event_resume, event_complete)
192         assert downtime * 10 < postcopy_time
193         if debug:
194             print('downtime:', downtime)
195             print('postcopy_time:', postcopy_time)
197         # check that there are no bitmaps stored on source
198         self.vm_a_events += self.vm_a.get_qmp_events()
199         self.vm_a.shutdown()
200         self.vm_a.launch()
201         check_bitmaps(self.vm_a, 0)
203         # check that bitmaps are migrated and persistence works
204         check_bitmaps(self.vm_b, nb_bitmaps)
205         self.vm_b.shutdown()
206         # recreate vm_b, so there is no incoming option, which prevents
207         # loading bitmaps from disk
208         self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b)
209         self.vm_b.launch()
210         check_bitmaps(self.vm_b, nb_bitmaps)
212         # Check content of migrated bitmaps. Still, don't waste time checking
213         # every bitmap
214         for i in range(0, nb_bitmaps, 5):
215             result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
216                                    node='drive0', name='bitmap{}'.format(i))
217             sha = self.discards1_sha256 if i % 2 else self.all_discards_sha256
218             self.assert_qmp(result, 'return/sha256', sha)
220     def test_early_shutdown_destination(self):
221         self.start_postcopy()
223         self.vm_b_events += self.vm_b.get_qmp_events()
224         self.vm_b.shutdown()
225         # recreate vm_b, so there is no incoming option, which prevents
226         # loading bitmaps from disk
227         self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b)
228         self.vm_b.launch()
229         check_bitmaps(self.vm_b, 0)
231         # Bitmaps will be lost if we just shutdown the vm, as they are marked
232         # to skip storing to disk when prepared for migration. And that's
233         # correct, as actual data may be modified in target vm, so we play
234         # safe.
235         # Still, this mark would be taken away if we do 'cont', and bitmaps
236         # become persistent again. (see iotest 169 for such behavior case)
237         result = self.vm_a.qmp('query-status')
238         assert not result['return']['running']
239         self.vm_a_events += self.vm_a.get_qmp_events()
240         self.vm_a.shutdown()
241         self.vm_a.launch()
242         check_bitmaps(self.vm_a, 0)
244     def test_early_kill_source(self):
245         self.start_postcopy()
247         self.vm_a_events = self.vm_a.get_qmp_events()
248         self.vm_a.kill()
250         self.vm_a.launch()
252         match = {'data': {'status': 'completed'}}
253         e_complete = self.vm_b.event_wait('MIGRATION', match=match)
254         self.vm_b_events.append(e_complete)
256         check_bitmaps(self.vm_a, 0)
257         check_bitmaps(self.vm_b, 0)
260 if __name__ == '__main__':
261     iotests.main(supported_fmts=['qcow2'])