1#!/usr/bin/env python3 2# 3# virtio-balloon tests 4# 5# This work is licensed under the terms of the GNU GPL, version 2 or 6# later. See the COPYING file in the top-level directory. 7 8import time 9 10from qemu_test import QemuSystemTest, Asset 11from qemu_test import wait_for_console_pattern 12from qemu_test import exec_command_and_wait_for_pattern 13 14UNSET_STATS_VALUE = 18446744073709551615 15 16 17class VirtioBalloonx86(QemuSystemTest): 18 19 ASSET_KERNEL = Asset( 20 ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' 21 '/31/Server/x86_64/os/images/pxeboot/vmlinuz'), 22 'd4738d03dbbe083ca610d0821d0a8f1488bebbdccef54ce33e3adb35fda00129') 23 24 ASSET_INITRD = Asset( 25 ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' 26 '/31/Server/x86_64/os/images/pxeboot/initrd.img'), 27 '277cd6c7adf77c7e63d73bbb2cded8ef9e2d3a2f100000e92ff1f8396513cd8b') 28 29 ASSET_DISKIMAGE = Asset( 30 ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' 31 '/31/Cloud/x86_64/images/Fedora-Cloud-Base-31-1.9.x86_64.qcow2'), 32 'e3c1b309d9203604922d6e255c2c5d098a309c2d46215d8fc026954f3c5c27a0') 33 34 DEFAULT_KERNEL_PARAMS = ('root=/dev/vda1 console=ttyS0 net.ifnames=0 ' 35 'rd.rescue') 36 37 def wait_for_console_pattern(self, success_message, vm=None): 38 wait_for_console_pattern( 39 self, 40 success_message, 41 failure_message="Kernel panic - not syncing", 42 vm=vm, 43 ) 44 45 def mount_root(self): 46 self.wait_for_console_pattern('Entering emergency mode.') 47 prompt = '# ' 48 self.wait_for_console_pattern(prompt) 49 50 exec_command_and_wait_for_pattern(self, 'mount /dev/vda1 /sysroot', 51 prompt) 52 exec_command_and_wait_for_pattern(self, 'chroot /sysroot', 53 prompt) 54 exec_command_and_wait_for_pattern(self, "modprobe virtio-balloon", 55 prompt) 56 57 def assert_initial_stats(self): 58 ret = self.vm.qmp('qom-get', 59 {'path': '/machine/peripheral/balloon', 60 'property': 'guest-stats'})['return'] 61 when = ret.get('last-update') 62 assert when == 0 63 stats = ret.get('stats') 64 for name, val in stats.items(): 65 assert val == UNSET_STATS_VALUE 66 67 def assert_running_stats(self, then): 68 ret = self.vm.qmp('qom-get', 69 {'path': '/machine/peripheral/balloon', 70 'property': 'guest-stats'})['return'] 71 when = ret.get('last-update') 72 now = time.time() 73 74 assert when > then and when < now 75 stats = ret.get('stats') 76 # Stat we expect this particular Kernel to have set 77 expectData = [ 78 "stat-available-memory", 79 "stat-disk-caches", 80 "stat-free-memory", 81 "stat-htlb-pgalloc", 82 "stat-htlb-pgfail", 83 "stat-major-faults", 84 "stat-minor-faults", 85 "stat-swap-in", 86 "stat-swap-out", 87 "stat-total-memory", 88 ] 89 for name, val in stats.items(): 90 if name in expectData: 91 assert val != UNSET_STATS_VALUE 92 else: 93 assert val == UNSET_STATS_VALUE 94 95 def test_virtio_balloon_stats(self): 96 self.set_machine('q35') 97 kernel_path = self.ASSET_KERNEL.fetch() 98 initrd_path = self.ASSET_INITRD.fetch() 99 diskimage_path = self.ASSET_DISKIMAGE.fetch() 100 101 self.vm.set_console() 102 self.vm.add_args("-S") 103 self.vm.add_args("-cpu", "max") 104 self.vm.add_args("-m", "2G") 105 # Slow down BIOS phase with boot menu, so that after a system 106 # reset, we can reliably catch the clean stats again in BIOS 107 # phase before the guest OS launches 108 self.vm.add_args("-boot", "menu=on") 109 self.vm.add_args("-machine", "q35,accel=kvm:tcg") 110 self.vm.add_args("-device", "virtio-balloon,id=balloon") 111 self.vm.add_args('-drive', 112 f'file={diskimage_path},if=none,id=drv0,snapshot=on') 113 self.vm.add_args('-device', 'virtio-blk-pci,bus=pcie.0,' + 114 'drive=drv0,id=virtio-disk0,bootindex=1') 115 116 self.vm.add_args( 117 "-kernel", 118 kernel_path, 119 "-initrd", 120 initrd_path, 121 "-append", 122 self.DEFAULT_KERNEL_PARAMS 123 ) 124 self.vm.launch() 125 126 # Poll stats at 100ms 127 self.vm.qmp('qom-set', 128 {'path': '/machine/peripheral/balloon', 129 'property': 'guest-stats-polling-interval', 130 'value': 100 }) 131 132 # We've not run any guest code yet, neither BIOS or guest, 133 # so stats should be all default values 134 self.assert_initial_stats() 135 136 self.vm.qmp('cont') 137 138 then = time.time() 139 self.mount_root() 140 self.assert_running_stats(then) 141 142 # Race window between these two commands, where we 143 # rely on '-boot menu=on' to (hopefully) ensure we're 144 # still executing the BIOS when QEMU processes the 145 # 'stop', and thus have not loaded the virtio-balloon 146 # driver in the guest 147 self.vm.qmp('system_reset') 148 self.vm.qmp('stop') 149 150 # If the above assumption held, we're in BIOS now and 151 # stats should be all back at their default values 152 self.assert_initial_stats() 153 self.vm.qmp('cont') 154 155 then = time.time() 156 self.mount_root() 157 self.assert_running_stats(then) 158 159 160if __name__ == '__main__': 161 QemuSystemTest.main() 162