qemu-iotests/199: better catch postcopy time
[qemu/ar7.git] / tests / qemu-iotests / 199
blobdd6044768c7645be4c1eba010eff4a091280ffb1
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')
33 def event_seconds(event):
34     return event['timestamp']['seconds'] + \
35         event['timestamp']['microseconds'] / 1000000.0
38 def event_dist(e1, e2):
39     return event_seconds(e2) - event_seconds(e1)
42 class TestDirtyBitmapPostcopyMigration(iotests.QMPTestCase):
43     def tearDown(self):
44         if debug:
45             self.vm_a_events += self.vm_a.get_qmp_events()
46             self.vm_b_events += self.vm_b.get_qmp_events()
47             for e in self.vm_a_events:
48                 e['vm'] = 'SRC'
49             for e in self.vm_b_events:
50                 e['vm'] = 'DST'
51             events = (self.vm_a_events + self.vm_b_events)
52             events = [(e['timestamp']['seconds'],
53                        e['timestamp']['microseconds'],
54                        e['vm'],
55                        e['event'],
56                        e.get('data', '')) for e in events]
57             for e in sorted(events):
58                 print('{}.{:06} {} {} {}'.format(*e))
60         self.vm_a.shutdown()
61         self.vm_b.shutdown()
62         os.remove(disk_a)
63         os.remove(disk_b)
64         os.remove(fifo)
66     def setUp(self):
67         os.mkfifo(fifo)
68         qemu_img('create', '-f', iotests.imgfmt, disk_a, size)
69         qemu_img('create', '-f', iotests.imgfmt, disk_b, size)
70         self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a)
71         self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b)
72         self.vm_b.add_incoming("exec: cat '" + fifo + "'")
73         self.vm_a.launch()
74         self.vm_b.launch()
76         # collect received events for debug
77         self.vm_a_events = []
78         self.vm_b_events = []
80     def test_postcopy(self):
81         write_size = 0x40000000
82         granularity = 512
83         chunk = 4096
85         result = self.vm_a.qmp('block-dirty-bitmap-add', node='drive0',
86                                name='bitmap', granularity=granularity)
87         self.assert_qmp(result, 'return', {})
89         s = 0
90         while s < write_size:
91             self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % (s, chunk))
92             s += 0x10000
93         s = 0x8000
94         while s < write_size:
95             self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % (s, chunk))
96             s += 0x10000
98         result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
99                                node='drive0', name='bitmap')
100         sha256 = result['return']['sha256']
102         result = self.vm_a.qmp('block-dirty-bitmap-clear', node='drive0',
103                                name='bitmap')
104         self.assert_qmp(result, 'return', {})
105         s = 0
106         while s < write_size:
107             self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % (s, chunk))
108             s += 0x10000
110         caps = [{'capability': 'dirty-bitmaps', 'state': True},
111                 {'capability': 'events', 'state': True}]
113         result = self.vm_a.qmp('migrate-set-capabilities', capabilities=caps)
114         self.assert_qmp(result, 'return', {})
116         result = self.vm_b.qmp('migrate-set-capabilities', capabilities=caps)
117         self.assert_qmp(result, 'return', {})
119         result = self.vm_a.qmp('migrate', uri='exec:cat>' + fifo)
120         self.assert_qmp(result, 'return', {})
122         result = self.vm_a.qmp('migrate-start-postcopy')
123         self.assert_qmp(result, 'return', {})
125         event_resume = self.vm_b.event_wait('RESUME')
126         self.vm_b_events.append(event_resume)
128         s = 0x8000
129         while s < write_size:
130             self.vm_b.hmp_qemu_io('drive0', 'write %d %d' % (s, chunk))
131             s += 0x10000
133         match = {'data': {'status': 'completed'}}
134         event_complete = self.vm_b.event_wait('MIGRATION', match=match)
135         self.vm_b_events.append(event_complete)
137         # take queued event, should already been happened
138         event_stop = self.vm_a.event_wait('STOP')
139         self.vm_a_events.append(event_stop)
141         downtime = event_dist(event_stop, event_resume)
142         postcopy_time = event_dist(event_resume, event_complete)
144         # TODO: assert downtime * 10 < postcopy_time
145         if debug:
146             print('downtime:', downtime)
147             print('postcopy_time:', postcopy_time)
149         # Assert that bitmap migration is finished (check that successor bitmap
150         # is removed)
151         result = self.vm_b.qmp('query-block')
152         assert len(result['return'][0]['dirty-bitmaps']) == 1
154         # Check content of migrated (and updated by new writes) bitmap
155         result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
156                                node='drive0', name='bitmap')
157         self.assert_qmp(result, 'return/sha256', sha256)
160 if __name__ == '__main__':
161     iotests.main(supported_fmts=['qcow2'])