|
| 1 | +# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for verifying the PVTime device behavior under contention and across snapshots.""" |
| 5 | + |
| 6 | +import time |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from framework.properties import global_props |
| 11 | + |
| 12 | + |
| 13 | +def get_steal_time_ms(vm): |
| 14 | + """Returns total steal time of vCPUs in VM in milliseconds""" |
| 15 | + _, out, _ = vm.ssh.run("grep -w '^cpu' /proc/stat") |
| 16 | + steal_time_tck = int(out.strip().split()[8]) |
| 17 | + clk_tck = int(vm.ssh.run("getconf CLK_TCK").stdout) |
| 18 | + return steal_time_tck / clk_tck * 1000 |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.skipif( |
| 22 | + global_props.cpu_architecture != "aarch64", reason="Only run in aarch64" |
| 23 | +) |
| 24 | +def test_guest_has_pvtime_enabled(uvm_plain): |
| 25 | + """ |
| 26 | + Check that the guest kernel has enabled PV steal time. |
| 27 | + """ |
| 28 | + vm = uvm_plain |
| 29 | + vm.spawn() |
| 30 | + vm.basic_config() |
| 31 | + vm.add_net_iface() |
| 32 | + vm.start() |
| 33 | + |
| 34 | + _, stdout, _ = vm.ssh.run("dmesg | grep 'stolen time PV'") |
| 35 | + assert ( |
| 36 | + "stolen time PV" in stdout |
| 37 | + ), "Guest kernel did not report PV steal time enabled" |
| 38 | + |
| 39 | + |
| 40 | +def test_pvtime_steal_time_increases(uvm_plain): |
| 41 | + """ |
| 42 | + Test that PVTime steal time increases when both vCPUs are contended on the same pCPU. |
| 43 | + """ |
| 44 | + vm = uvm_plain |
| 45 | + vm.spawn() |
| 46 | + vm.basic_config() |
| 47 | + vm.add_net_iface() |
| 48 | + vm.start() |
| 49 | + |
| 50 | + # Pin both vCPUs to the same physical CPU to induce contention |
| 51 | + vm.pin_vcpu(0, 0) |
| 52 | + vm.pin_vcpu(1, 0) |
| 53 | + |
| 54 | + # Start two infinite loops to hog CPU time |
| 55 | + hog_cmd = "nohup bash -c 'while true; do :; done' >/dev/null 2>&1 &" |
| 56 | + vm.ssh.run(hog_cmd) |
| 57 | + vm.ssh.run(hog_cmd) |
| 58 | + |
| 59 | + # Measure before and after steal time |
| 60 | + steal_before = get_steal_time_ms(vm) |
| 61 | + time.sleep(2) |
| 62 | + steal_after = get_steal_time_ms(vm) |
| 63 | + |
| 64 | + # Require increase in steal time |
| 65 | + assert ( |
| 66 | + steal_after > steal_before |
| 67 | + ), f"Steal time did not increase as expected. Before: {steal_before}, After: {steal_after}" |
| 68 | + |
| 69 | + |
| 70 | +def test_pvtime_snapshot(uvm_plain, microvm_factory): |
| 71 | + """ |
| 72 | + Test that PVTime steal time is preserved across snapshot/restore |
| 73 | + and continues increasing post-resume. |
| 74 | + """ |
| 75 | + vm = uvm_plain |
| 76 | + vm.spawn() |
| 77 | + vm.basic_config() |
| 78 | + vm.add_net_iface() |
| 79 | + vm.start() |
| 80 | + |
| 81 | + vm.pin_vcpu(0, 0) |
| 82 | + vm.pin_vcpu(1, 0) |
| 83 | + |
| 84 | + hog_cmd = "nohup bash -c 'while true; do :; done' >/dev/null 2>&1 &" |
| 85 | + vm.ssh.run(hog_cmd) |
| 86 | + vm.ssh.run(hog_cmd) |
| 87 | + |
| 88 | + # Snapshot pre-steal time |
| 89 | + steal_before = get_steal_time_ms(vm) |
| 90 | + |
| 91 | + snapshot = vm.snapshot_full() |
| 92 | + vm.kill() |
| 93 | + |
| 94 | + # Restore microVM from snapshot and resume |
| 95 | + restored_vm = microvm_factory.build() |
| 96 | + restored_vm.spawn() |
| 97 | + restored_vm.restore_from_snapshot(snapshot, resume=False) |
| 98 | + snapshot.delete() |
| 99 | + |
| 100 | + restored_vm.pin_vcpu(0, 0) |
| 101 | + restored_vm.pin_vcpu(1, 0) |
| 102 | + restored_vm.resume() |
| 103 | + |
| 104 | + # Steal time just after restoring |
| 105 | + steal_after_snap = get_steal_time_ms(restored_vm) |
| 106 | + |
| 107 | + time.sleep(2) |
| 108 | + |
| 109 | + # Steal time after running resumed VM |
| 110 | + steal_after_resume = get_steal_time_ms(restored_vm) |
| 111 | + |
| 112 | + # Ensure steal time persisted and continued increasing |
| 113 | + tolerance = 2000 # 2.0 seconds tolerance for persistence check |
| 114 | + persisted = ( |
| 115 | + steal_before < steal_after_snap and steal_after_snap - steal_before < tolerance |
| 116 | + ) |
| 117 | + increased = steal_after_resume > steal_after_snap |
| 118 | + |
| 119 | + assert ( |
| 120 | + persisted and increased |
| 121 | + ), "Steal time did not persist through snapshot or failed to increase after resume" |
0 commit comments