Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env_process: Refactor huge pages setup/cleanup steps #4054

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from virttest.test_setup.gcov import ResetQemuGCov
from virttest.test_setup.kernel import ReloadKVMModules
from virttest.test_setup.libvirt_setup import LibvirtdDebugLogConfig
from virttest.test_setup.memory import HugePagesSetup
from virttest.test_setup.migration import MigrationEnvSetup
from virttest.test_setup.networking import (
BridgeConfig,
Expand Down Expand Up @@ -91,11 +92,6 @@

_setup_manager = SetupManager()

# default num of surplus hugepage, order to compare the values before and after
# the test when 'setup_hugepages = yes'
_pre_hugepages_surp = 0
_post_hugepages_surp = 0

#: Hooks to use for own customization stages of the virtual machines with
#: test, params. and env as supplied arguments
preprocess_vm_off_hook = None
Expand Down Expand Up @@ -1023,6 +1019,7 @@ def preprocess(test, params, env):
_setup_manager.register(CheckVirtioWinVersion)
_setup_manager.register(CheckLibvirtVersion)
_setup_manager.register(LogVersionInfo)
_setup_manager.register(HugePagesSetup)
_setup_manager.do_setup()

vm_type = params.get("vm_type")
Expand All @@ -1031,24 +1028,6 @@ def preprocess(test, params, env):

libvirtd_inst = None

# If guest is configured to be backed by hugepages, setup hugepages in host
if params.get("hugepage") == "yes":
params["setup_hugepages"] = "yes"

if params.get("setup_hugepages") == "yes":
global _pre_hugepages_surp
h = test_setup.HugePageConfig(params)
_pre_hugepages_surp = h.ext_hugepages_surp
suggest_mem = h.setup()
if suggest_mem is not None:
params["mem"] = suggest_mem
if not params.get("hugepage_path"):
params["hugepage_path"] = h.hugepage_path
if vm_type == "libvirt":
if libvirtd_inst is None:
libvirtd_inst = utils_libvirtd.Libvirtd()
libvirtd_inst.restart()

if params.get("setup_thp") == "yes":
thp = test_setup.TransparentHugePageConfig(test, params, env)
thp.setup()
Expand Down Expand Up @@ -1534,21 +1513,6 @@ def postprocess(test, params, env):
libvirtd_inst = None
vm_type = params.get("vm_type")

if params.get("setup_hugepages") == "yes":
global _post_hugepages_surp
try:
h = test_setup.HugePageConfig(params)
h.cleanup()
if vm_type == "libvirt":
if libvirtd_inst is None:
libvirtd_inst = utils_libvirtd.Libvirtd()
libvirtd_inst.restart()
except Exception as details:
err += "\nHP cleanup: %s" % str(details).replace("\\n", "\n ")
LOG.error(details)
else:
_post_hugepages_surp = h.ext_hugepages_surp

if params.get("setup_thp") == "yes":
try:
thp = test_setup.TransparentHugePageConfig(test, params, env)
Expand Down Expand Up @@ -1607,9 +1571,6 @@ def postprocess(test, params, env):

if err:
raise RuntimeError("Failures occurred while postprocess:\n%s" % err)
elif _post_hugepages_surp > _pre_hugepages_surp:
leak_num = _post_hugepages_surp - _pre_hugepages_surp
raise exceptions.TestFail("%d huge pages leaked!" % leak_num)


def postprocess_on_error(test, params, env):
Expand Down
8 changes: 8 additions & 0 deletions virttest/test_setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class THPKhugepagedError(THPError):
pass


class HugePagesLeakError(Exception):
"""
Thrown when huge pages are leaked after cleanup.
"""

pass


class PolkitConfigError(Exception):

"""
Expand Down
36 changes: 36 additions & 0 deletions virttest/test_setup/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from virttest import test_setup, utils_libvirtd
from virttest.test_setup.core import Setuper


class HugePagesSetup(Setuper):
def __init__(self, test, params, env):
super().__init__(test, params, env)
# default num of surplus hugepages, in order to compare the values
# before and after the test when 'setup_hugepages = yes'
self._pre_hugepages_surp = 0

def setup(self):
# If guest is configured to be backed by hugepages, setup hugepages in host
if self.params.get("hugepage") == "yes":
self.params["setup_hugepages"] = "yes"
if self.params.get("setup_hugepages") == "yes":
h = test_setup.HugePageConfig(self.params)
self._pre_hugepages_surp = h.ext_hugepages_surp
suggest_mem = h.setup()
if suggest_mem is not None:
self.params["mem"] = suggest_mem
if not self.params.get("hugepage_path"):
self.params["hugepage_path"] = h.hugepage_path
if self.params.get("vm_type") == "libvirt":
utils_libvirtd.Libvirtd().restart()

def cleanup(self):
if self.params.get("setup_hugepages") == "yes":
h = test_setup.HugePageConfig(self.params)
h.cleanup()
if self.params.get("vm_type") == "libvirt":
utils_libvirtd.Libvirtd().restart()
post_hugepages_surp = h.ext_hugepages_surp
if post_hugepages_surp > self._pre_hugepages_surp:
leak_num = post_hugepages_surp - self._pre_hugepages_surp
raise test_setup.HugePagesLeakError("%d huge pages leaked!" % leak_num)
Loading