Skip to content

Commit

Permalink
Merge pull request autotest#2548 from PaulYuuu/cpu_hotplug_check_b2
Browse files Browse the repository at this point in the history
Modify the logic of checking vcpu before and after plug/unplug [BATCH2]
  • Loading branch information
PaulYuuu authored Jan 14, 2021
2 parents a77b675 + 62d285a commit 89d8177
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 60 deletions.
14 changes: 14 additions & 0 deletions provider/cpu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,17 @@ def check_if_vm_vcpu_match(vcpu_desire, vm):
return False
logging.info("CPU quantity matched: %s" % vcpu_actual)
return True


def check_if_vm_vcpus_match_qemu(vm):
vcpus_count = vm.params.get_numeric("vcpus_count", 1)
vcpu_devices = vm.params.objects("vcpu_devices")
enabled_vcpu_devices = []

for vcpu_device in vcpu_devices:
vcpu_params = vm.params.object_params(vcpu_device)
if vcpu_params.get_boolean("vcpu_enable"):
enabled_vcpu_devices.append(vcpu_device)
enabled_count = vm.cpuinfo.smp + (len(enabled_vcpu_devices) * vcpus_count)

return check_if_vm_vcpu_match(enabled_count, vm)
22 changes: 7 additions & 15 deletions qemu/tests/cpu_device_hotplug_during_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from virttest import error_context

from provider import cpu_utils


@error_context.context_aware
def run(test, params, env):
Expand All @@ -27,8 +29,6 @@ def run(test, params, env):

vm = env.get_vm(params["main_vm"])
vm.verify_alive()
smp = vm.cpuinfo.smp
maxcpus = vm.cpuinfo.maxcpus

error_context.base_context("Hotplug vCPU devices during boot stage.",
logging.info)
Expand All @@ -42,12 +42,8 @@ def run(test, params, env):
vm.wait_for_login().close()

error_context.context("Check number of CPU inside guest.", logging.info)
current_guest_cpus = vm.get_cpu_count()
if current_guest_cpus != maxcpus:
test.fail("Actual number of guest CPUs(%s) is not equal to"
" expected(%s) after hotplug." % (current_guest_cpus,
maxcpus))
logging.info("CPU quantity(%s) in guest is correct.", current_guest_cpus)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.fail("Actual number of guest CPUs is not equal to expected")

if unplug_during_boot:
# 1) vm.reboot() will return a new session, which is not what we want.
Expand All @@ -70,10 +66,6 @@ def run(test, params, env):

error_context.context("Check number of CPU inside guest after unplug.",
logging.info)
current_guest_cpus = vm.get_cpu_count()
if current_guest_cpus != smp:
test.fail("Actual number of guest CPUs(%s) is not equal to "
"expected(%s) after hotunplug." % (current_guest_cpus,
smp))
logging.info("CPU quantity(%s) in guest is correct.",
current_guest_cpus)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.fail("Actual number of guest CPUs is not equal to expected "
"after hotunplug.")
21 changes: 12 additions & 9 deletions qemu/tests/cpu_device_hotplug_maximum.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def run(test, params, env):
reboot_timeout = params.get_numeric("reboot_timeout")
offline_vcpu_after_hotplug = params.get_boolean("offline_vcpu_after_hotplug")
mismatch_text = "Actual number of guest CPUs is not equal to the expected"
not_equal_text = "CPU quantity mismatched! Guest got %s but expected is %s"
# Many vCPUs will be plugged, it takes some time to bring them online.
verify_wait_timeout = params.get_numeric("verify_wait_timeout", 300)
qemu_binary = utils_misc.get_qemu_binary(params)
Expand Down Expand Up @@ -75,16 +74,21 @@ def run(test, params, env):
smp = cpuinfo.smp
vcpus_count = vm.params.get_numeric("vcpus_count")

error_context.context("Check the number of guest CPUs after startup",
logging.info)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.error("The number of guest CPUs is not equal to the qemu command "
"line configuration")

error_context.context("Hotplug all vCPU devices", logging.info)
for vcpu_device in vcpu_devices:
vm.hotplug_vcpu_device(vcpu_device)

error_context.context("Check Number of vCPU in guest", logging.info)
if not utils_misc.wait_for(lambda: vm.get_cpu_count() == supported_maxcpus,
verify_wait_timeout, first=5, step=10):
logging.error(not_equal_text, vm.get_cpu_count(), supported_maxcpus)
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm),
verify_wait_timeout, first=5, step=10):
test.fail(mismatch_text)
logging.info("CPU quantity is as expected: %s", supported_maxcpus)

error_context.context("Check CPU topology of guest", logging.info)
if not cpu_utils.check_guest_cpu_topology(session, os_type, cpuinfo):
Expand All @@ -104,9 +108,8 @@ def run(test, params, env):
test.error("Failed to offline all hotplugged vCPU.")
for vcpu_device in reversed(vcpu_devices):
vm.hotunplug_vcpu_device(vcpu_device, 10 * vcpus_count)
if not utils_misc.wait_for(lambda: vm.get_cpu_count() == smp,
verify_wait_timeout, first=5, step=10):
logging.error(not_equal_text, vm.get_cpu_count(), smp)
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm),
verify_wait_timeout, first=5, step=10):
test.fail(mismatch_text)
logging.info("CPU quantity is as expected after hotunplug: %s", smp)
session.close()
41 changes: 20 additions & 21 deletions qemu/tests/cpu_device_hotpluggable.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging

from provider import cpu_utils

from aexpect import ShellCmdError

from virttest import error_context
from virttest import utils_misc
from virttest.virt_vm import VMDeviceCheckError

from provider import cpu_utils


@error_context.context_aware
def run(test, params, env):
Expand All @@ -24,15 +25,11 @@ def run(test, params, env):
:param params: Dictionary with the test parameters.
:param env: Dictionary with test environment.
"""
def check_guest_cpu_count(expected_count):
def check_guest_cpu_count():
if not utils_misc.wait_for(
lambda: vm.get_cpu_count() == expected_count,
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm),
verify_wait_timeout, first=sleep_after_change):
logging.error("CPU quantity mismatched! Guest got %s but the"
" expected is %s", vm.get_cpu_count(),
expected_count)
test.fail("Actual number of guest CPUs is not equal to expected")
logging.info("CPU quantity matched: %s", expected_count)

def sub_hotunplug():
error_context.context("Hotunplug vcpu devices after vcpu %s"
Expand All @@ -46,6 +43,7 @@ def sub_hotunplug():
logging.warning("%s can not be unplugged directly because "
"guest is paused, will check again after "
"resume", plugged_dev)
vm.params["vcpu_enable_%s" % plugged_dev] = "no"

def sub_reboot():
error_context.context("Reboot guest after vcpu %s"
Expand Down Expand Up @@ -78,7 +76,7 @@ def sub_migrate():
vm.verify_alive()
sub_test_after_migrate = params.objects("sub_test_after_migrate")
while sub_test_after_migrate:
check_guest_cpu_count(expected_vcpus)
check_guest_cpu_count()
sub_test = sub_test_after_migrate.pop(0)
error_context.context("%s after migration completed" % sub_test)
eval("sub_migrate_%s" % sub_test)()
Expand All @@ -92,7 +90,9 @@ def sub_online_offline():
try:
for cpu_id in cpu_ids[::-1]:
session.cmd(cmd % (0, cpu_id))
check_guest_cpu_count(smp)
if not cpu_utils.check_if_vm_vcpu_match(cpu_count_before_test, vm):
test.fail(
"Actual number of guest CPUs is not equal to expected")
for cpu_id in cpu_ids:
session.cmd(cmd % (1, cpu_id))
except ShellCmdError as err:
Expand All @@ -118,19 +118,21 @@ def sub_pause_resume():
vm = env.get_vm(params["main_vm"])
vm.verify_alive()
session = vm.wait_for_login(timeout=login_timeout)
smp = vm.cpuinfo.smp
cpu_count_before_test = vm.get_cpu_count()
maxcpus = vm.cpuinfo.maxcpus
vcpus_count = vm.params.get_numeric("vcpus_count", 1)
vcpu_devices = params.objects("vcpu_devices")
guest_cpu_ids = cpu_utils.get_guest_cpu_ids(session, os_type)

error_context.context("Check the number of guest CPUs after startup",
logging.info)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.error("The number of guest CPUs is not equal to the qemu command "
"line configuration")

if hotpluggable_test == "hotplug":
pluggable_vcpu_dev = vcpu_devices
pluggable_vcpu = vcpus_count * len(pluggable_vcpu_dev)
else:
pluggable_vcpu_dev = vcpu_devices[::-1]
pluggable_vcpu = -(vcpus_count * len(pluggable_vcpu_dev))
expected_vcpus = vm.get_cpu_count() + pluggable_vcpu

if params.get("pause_vm_before_hotplug", "no") == "yes":
error_context.context("Pause guest before %s" % hotpluggable_test,
Expand All @@ -145,22 +147,19 @@ def sub_pause_resume():
logging.info)
vm.resume()

check_guest_cpu_count(expected_vcpus)
check_guest_cpu_count()
current_guest_cpu_ids = cpu_utils.get_guest_cpu_ids(session, os_type)

if sub_test_type:
eval("sub_%s" % sub_test_type)()
# Close old session since guest maybe dead/reboot
if session:
session.close()
if ("hotunplug" in params.objects("sub_test_after_migrate")
or sub_test_type == "pause_resume"):
expected_vcpus -= pluggable_vcpu

if vm.is_alive():
session = vm.wait_for_login(timeout=login_timeout)
check_guest_cpu_count(expected_vcpus)
if expected_vcpus == maxcpus and check_cpu_topology:
check_guest_cpu_count()
if vm.get_cpu_count() == maxcpus and check_cpu_topology:
if not cpu_utils.check_guest_cpu_topology(session, os_type,
vm.cpuinfo):
session.close()
Expand Down
23 changes: 18 additions & 5 deletions qemu/tests/cpu_device_hotpluggable_with_numa.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re
import logging

from virttest import utils_misc
from virttest import error_context
from virttest import utils_package

from provider import cpu_utils


@error_context.context_aware
def run(test, params, env):
Expand Down Expand Up @@ -49,6 +52,7 @@ def get_guest_numa_cpus_info():
os_type = params["os_type"]
machine = params["machine_type"]
vcpu_devices = params.objects("vcpu_devices")
vcpus_count = params.get_numeric("vcpus_count", 1)
login_timeout = params.get_numeric("login_timeout", 360)
vm = env.get_vm(params["main_vm"])
vm.create()
Expand All @@ -70,6 +74,14 @@ def get_guest_numa_cpus_info():
logging.info)
vm.create(params=params)
session = vm.wait_for_login(timeout=login_timeout)

error_context.context("Check the number of guest CPUs after startup",
logging.info)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.error("The number of guest CPUs is not equal to the qemu command "
"line configuration")

cpu_count_before_test = vm.get_cpu_count()
if os_type == "linux" and not utils_package.package_install("numactl",
session):
test.cancel("Please install numactl to proceed")
Expand All @@ -78,8 +90,9 @@ def get_guest_numa_cpus_info():
error_context.context("hotplug vcpu device: %s" % vcpu_dev,
logging.info)
vm.hotplug_vcpu_device(vcpu_dev)
if vm.get_cpu_count() != maxcpus:
test.fail("Actual number of guest CPUs is not equal to expected.")
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm), 10):
test.fail("Actual number of guest CPUs is not equal to expected")

if os_type == "linux":
error_context.context("Check the CPU information of each numa node",
Expand All @@ -100,9 +113,9 @@ def get_guest_numa_cpus_info():
error_context.context("hotunplug vcpu device: %s" % vcpu_dev,
logging.info)
vm.hotunplug_vcpu_device(vcpu_dev)
if vm.get_cpu_count() != vm.cpuinfo.smp:
test.fail("Actual number of guest CPUs is not equal to the "
"expected.")
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm), 10):
test.fail("Actual number of guest CPUs is not equal to expected")
if get_guest_numa_cpus_info() != numa_before_plug:
logging.debug("Current guest numa info:\n%s",
session.cmd_output("numactl -H"))
Expand Down
15 changes: 7 additions & 8 deletions qemu/tests/cpu_device_hotpluggable_with_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,24 @@ def heavyload_install():
stress_duration = params.get_numeric("stress_duration", 180)
verify_wait_timeout = params.get_numeric("verify_wait_timeout", 60)
vcpu_devices = params.objects("vcpu_devices")
vcpus_count = params.get_numeric("vcpus_count", 1)
pluggable_count = len(vcpu_devices) * vcpus_count

vm = env.get_vm(params["main_vm"])
vm.verify_alive()
session = vm.wait_for_login(timeout=login_timeout)
if not cpu_utils.check_if_vm_vcpu_match(vm.cpuinfo.smp, vm):

error_context.context("Check the number of guest CPUs after startup",
logging.info)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.error("The number of guest CPUs is not equal to the qemu command "
"line configuration")

cpu_count_before_test = vm.get_cpu_count()
expected_count = pluggable_count + cpu_count_before_test
guest_cpu_ids = cpu_utils.get_guest_cpu_ids(session, os_type)
for vcpu_dev in vcpu_devices:
error_context.context("Hotplug vcpu device: %s" % vcpu_dev,
logging.info)
vm.hotplug_vcpu_device(vcpu_dev)
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpu_match(expected_count, vm),
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm),
verify_wait_timeout):
test.fail("Actual number of guest CPUs is not equal to expected")

Expand Down Expand Up @@ -103,7 +102,7 @@ def heavyload_install():
vm.hotunplug_vcpu_device(vcpu_dev)
# Drift the running stress task to other vCPUs
time.sleep(random.randint(5, 10))
if not cpu_utils.check_if_vm_vcpu_match(cpu_count_before_test, vm):
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.fail("Actual number of guest CPUs is not equal to "
"expected")
stress_tool.unload_stress()
Expand All @@ -114,7 +113,7 @@ def heavyload_install():
heavyload_install()
error_context.context("Run heavyload inside guest.", logging.info)
heavyload_bin = r'"%s\heavyload.exe" ' % install_path
heavyload_options = ["/CPU %d" % expected_count,
heavyload_options = ["/CPU %d" % vm.get_cpu_count(),
"/DURATION %d" % (stress_duration // 60),
"/AUTOEXIT",
"/START"]
Expand Down
15 changes: 13 additions & 2 deletions qemu/tests/invalid_cpu_device_hotplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import logging

from virttest import arch
from virttest import utils_misc
from virttest import error_context
from virttest.qemu_monitor import QMPCmdError

from provider import cpu_utils


@error_context.context_aware
def run(test, params, env):
Expand Down Expand Up @@ -51,8 +54,9 @@ def hotplug_inuse_vcpu():
error_context.context("hotplug vcpu device: %s" % vcpu_device_id,
logging.info)
vm.hotplug_vcpu_device(vcpu_device_id)
if vm.get_cpu_count() != maxcpus:
test.fail("Actual number of guest CPUs is not equal to expected.")
if not utils_misc.wait_for(
lambda: cpu_utils.check_if_vm_vcpus_match_qemu(vm), 10):
test.fail("Actual number of guest CPUs is not equal to expected")

# Duplicate vCPU
duplicate_vcpu_params = vm.devices.get_by_qid(
Expand Down Expand Up @@ -119,6 +123,13 @@ def hotplug_outofrange_vcpu():
vm = env.get_vm(params["main_vm"])
vm.verify_alive()
vm.wait_for_login()

error_context.context("Check the number of guest CPUs after startup",
logging.info)
if not cpu_utils.check_if_vm_vcpus_match_qemu(vm):
test.error("The number of guest CPUs is not equal to the qemu command "
"line configuration")

vcpu_bus = vm.devices.get_buses({'aobject': 'vcpu'})[0]
vcpu_props = vcpu_bus.addr_items
maxcpus = vm.cpuinfo.maxcpus
Expand Down

0 comments on commit 89d8177

Please sign in to comment.