Skip to content

Commit

Permalink
env_process: Refactor huge pages setup/cleanup steps
Browse files Browse the repository at this point in the history
Creating a new Setuper subclass for setting and cleaning huge pages up.
Removing the original code from virttest.env_process and replacing it
instead with the new HugePagesSetup class being registered in the
setup_manager.

_pre_hugepages_surp and _post_hugepages_surp were left in env_process.
Their goal is to provide a mechanism in env_process to raise a TestFail
in case pages were leaked during a test. If that mechanism was
refactored into the setuper, the TestFail would be masked by just an
Error due to the way setup_manager handles postprocess exceptions.
Changing the way SetupManager handles that requires bigger discussion on
how the test infrastructure should handle test status reports, which is
a way broader topic that what this patch aims to be.

This is a patch from a larger patch series refactoring the env_process
preprocess and postprocess functions. In each of these patches, a
pre/post process step is identified and replaced with a Setuper subclass
so the following can finally be met:
    - Only cleanup steps of successful setup steps are run to avoid
      possible environment corruption or hard to read errors.
    - Running setup/cleanup steps symmetrically during env pre/post
      process.
    - Reduce explicit pre/post process function code length.

Signed-off-by: Beñat Gartzia Arruabarrena <[email protected]>
  • Loading branch information
bgartzi committed Jan 17, 2025
1 parent 0812d4d commit 3fdae9b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
35 changes: 2 additions & 33 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 @@ -1023,6 +1024,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 +1033,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 +1518,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
27 changes: 27 additions & 0 deletions virttest/test_setup/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from virttest import env_process, test_setup, utils_libvirtd
from virttest.test_setup.core import Setuper


class HugePagesSetup(Setuper):
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)
env_process._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()
env_process._post_hugepages_surp = h.ext_hugepages_surp

0 comments on commit 3fdae9b

Please sign in to comment.