xref: /qemu/tests/avocado/replay_linux.py (revision ca61e750)
1# Record/replay test that boots a complete Linux system via a cloud image
2#
3# Copyright (c) 2020 ISP RAS
4#
5# Author:
6#  Pavel Dovgalyuk <Pavel.Dovgaluk@ispras.ru>
7#
8# This work is licensed under the terms of the GNU GPL, version 2 or
9# later.  See the COPYING file in the top-level directory.
10
11import os
12import logging
13import time
14
15from avocado import skipUnless
16from avocado_qemu import BUILD_DIR
17from avocado.utils import cloudinit
18from avocado.utils import network
19from avocado.utils import vmimage
20from avocado.utils import datadrainer
21from avocado.utils.path import find_command
22from avocado_qemu import LinuxTest
23
24class ReplayLinux(LinuxTest):
25    """
26    Boots a Linux system, checking for a successful initialization
27    """
28
29    timeout = 1800
30    chksum = None
31    hdd = 'ide-hd'
32    cd = 'ide-cd'
33    bus = 'ide'
34
35    def setUp(self):
36        # LinuxTest does many replay-incompatible things, but includes
37        # useful methods. Do not setup LinuxTest here and just
38        # call some functions.
39        super(LinuxTest, self).setUp()
40        self._set_distro()
41        self.boot_path = self.download_boot()
42        self.phone_server = cloudinit.PhoneHomeServer(('0.0.0.0', 0),
43                                                      self.name)
44        ssh_pubkey, self.ssh_key = self.set_up_existing_ssh_keys()
45        self.cloudinit_path = self.prepare_cloudinit(ssh_pubkey)
46
47    def vm_add_disk(self, vm, path, id, device):
48        bus_string = ''
49        if self.bus:
50            bus_string = ',bus=%s.%d' % (self.bus, id,)
51        vm.add_args('-drive', 'file=%s,snapshot,id=disk%s,if=none' % (path, id))
52        vm.add_args('-drive',
53            'driver=blkreplay,id=disk%s-rr,if=none,image=disk%s' % (id, id))
54        vm.add_args('-device',
55            '%s,drive=disk%s-rr%s' % (device, id, bus_string))
56
57    def launch_and_wait(self, record, args, shift):
58        vm = self.get_vm()
59        vm.add_args('-smp', '1')
60        vm.add_args('-m', '1024')
61        vm.add_args('-netdev', 'user,id=vnet,hostfwd=:127.0.0.1:0-:22',
62                    '-device', 'virtio-net,netdev=vnet')
63        vm.add_args('-object', 'filter-replay,id=replay,netdev=vnet')
64        if args:
65            vm.add_args(*args)
66        self.vm_add_disk(vm, self.boot_path, 0, self.hdd)
67        self.vm_add_disk(vm, self.cloudinit_path, 1, self.cd)
68        logger = logging.getLogger('replay')
69        if record:
70            logger.info('recording the execution...')
71            mode = 'record'
72        else:
73            logger.info('replaying the execution...')
74            mode = 'replay'
75        replay_path = os.path.join(self.workdir, 'replay.bin')
76        vm.add_args('-icount', 'shift=%s,rr=%s,rrfile=%s' %
77                    (shift, mode, replay_path))
78
79        start_time = time.time()
80
81        vm.set_console()
82        vm.launch()
83        console_drainer = datadrainer.LineLogger(vm.console_socket.fileno(),
84                                    logger=self.log.getChild('console'),
85                                    stop_check=(lambda : not vm.is_running()))
86        console_drainer.start()
87        if record:
88            while not self.phone_server.instance_phoned_back:
89                self.phone_server.handle_request()
90            vm.shutdown()
91            logger.info('finished the recording with log size %s bytes'
92                % os.path.getsize(replay_path))
93        else:
94            vm.event_wait('SHUTDOWN', self.timeout)
95            vm.shutdown(True)
96            logger.info('successfully fihished the replay')
97        elapsed = time.time() - start_time
98        logger.info('elapsed time %.2f sec' % elapsed)
99        return elapsed
100
101    def run_rr(self, args=None, shift=7):
102        t1 = self.launch_and_wait(True, args, shift)
103        t2 = self.launch_and_wait(False, args, shift)
104        logger = logging.getLogger('replay')
105        logger.info('replay overhead {:.2%}'.format(t2 / t1 - 1))
106
107@skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
108class ReplayLinuxX8664(ReplayLinux):
109    """
110    :avocado: tags=arch:x86_64
111    :avocado: tags=accel:tcg
112    """
113
114    chksum = 'e3c1b309d9203604922d6e255c2c5d098a309c2d46215d8fc026954f3c5c27a0'
115
116    def test_pc_i440fx(self):
117        """
118        :avocado: tags=machine:pc
119        """
120        self.run_rr(shift=1)
121
122    def test_pc_q35(self):
123        """
124        :avocado: tags=machine:q35
125        """
126        self.run_rr(shift=3)
127
128@skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
129class ReplayLinuxX8664Virtio(ReplayLinux):
130    """
131    :avocado: tags=arch:x86_64
132    :avocado: tags=virtio
133    :avocado: tags=accel:tcg
134    """
135
136    hdd = 'virtio-blk-pci'
137    cd = 'virtio-blk-pci'
138    bus = None
139
140    chksum = 'e3c1b309d9203604922d6e255c2c5d098a309c2d46215d8fc026954f3c5c27a0'
141
142    def test_pc_i440fx(self):
143        """
144        :avocado: tags=machine:pc
145        """
146        self.run_rr(shift=1)
147
148    def test_pc_q35(self):
149        """
150        :avocado: tags=machine:q35
151        """
152        self.run_rr(shift=3)
153
154@skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
155class ReplayLinuxAarch64(ReplayLinux):
156    """
157    :avocado: tags=accel:tcg
158    :avocado: tags=arch:aarch64
159    :avocado: tags=machine:virt
160    :avocado: tags=cpu:max
161    """
162
163    chksum = '1e18d9c0cf734940c4b5d5ec592facaed2af0ad0329383d5639c997fdf16fe49'
164
165    hdd = 'virtio-blk-device'
166    cd = 'virtio-blk-device'
167    bus = None
168
169    def get_common_args(self):
170        return ('-bios',
171                os.path.join(BUILD_DIR, 'pc-bios', 'edk2-aarch64-code.fd'),
172                "-cpu", "max,lpa2=off",
173                '-device', 'virtio-rng-pci,rng=rng0',
174                '-object', 'rng-builtin,id=rng0')
175
176    def test_virt_gicv2(self):
177        """
178        :avocado: tags=machine:gic-version=2
179        """
180
181        self.run_rr(shift=3,
182                    args=(*self.get_common_args(),
183                          "-machine", "virt,gic-version=2"))
184
185    def test_virt_gicv3(self):
186        """
187        :avocado: tags=machine:gic-version=3
188        """
189
190        self.run_rr(shift=3,
191                    args=(*self.get_common_args(),
192