replay_linux.py 6.6 KB

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