-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kernel module handler for kvm hugepage support
Handle cases where kvm module is loaded without hugepage support 1. Add class to help reload and restore Linux kernel module configuration. KernelModuleHandler will only reload if current module configuration doesn't contain some of the requested parameter settings. It will only restore backed up configuration if it changed the current module configuration. 2. Hook activating hugepages with config into env_process a. Introduce variable `kvm_module_force_load` to force loading the module always. Don't force by default. b. Introduce variable `kvm_module_parameters` to define custom module parameters on load. c. Use convenience method `reload` to delegate decision if to load to new python module. d. Sample log output i. where reload is not necessary ```bash 2020-02-20 14:44:31,300 utils_kernel_mod L0089 DEBUG| Backed up kvm module state (was_loaded, params)=(True, halt_poll_ns_shrink=0 hpage=1 halt_poll_ns=50000 halt_poll_ns_grow_start=10000 halt_poll_ns_grow=2 nested=0 halt_poll_max_steal=10) 2020-02-20 14:44:31,301 utils_kernel_mod L0038 DEBUG| Not reloading module. Current module configuration for kvm already contains all requested parameters. Requested: hpage=1. Current: halt_poll_ns_shrink=0 hpage=1 halt_poll_ns=50000 halt_poll_ns_grow_start=10000 halt_poll_ns_grow=2 nested=0 halt_poll_max_steal=10. ``` ii. where reload is necessary ```bash 2020-02-20 14:30:16,509 utils_kernel_mod L0089 DEBUG| Backed up kvm module state (was_loaded, params)=(True, halt_poll_ns_shrink=0 hpage=0 halt_poll_ns=50000 halt_poll_ns_grow_start=10000 halt_poll_ns_grow=2 nested=0 halt_poll_max_steal=10) 2020-02-20 14:30:16,510 utils_kernel_mod L0049 DEBUG| Loading module: rmmod kvm; modprobe kvm hpage=1 2020-02-20 14:35:13,839 utils_kernel_mod L0069 DEBUG| Restoring module state: rmmod kvm; modprobe kvm halt_poll_ns_shrink=0 hpage=0 halt_poll_ns=50000 halt_poll_ns_grow_start=10000 halt_poll_ns_grow=2 nested=0 halt_poll_max_steal=10 ``` Signed-off-by: Sebastian Mitterle <[email protected]> Signed-off-by: Lukas Doktor <[email protected]> Signed-off-by: Sebastian Mitterle <[email protected]>
- Loading branch information
Showing
3 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import unittest | ||
|
||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
from virttest import utils_kernel_module | ||
|
||
# Test values | ||
some_module_name = 'skvm' | ||
some_module_param = 'force_emulation_prefix' | ||
some_module_val = 'N' | ||
some_module_config = {some_module_param: some_module_val, | ||
'halt_poll_ns_shrink': '0'} | ||
some_module_params = "%s=%s" % (some_module_param, some_module_val) | ||
|
||
# Mocks | ||
getstatusoutput_ok = mock.Mock(return_value=(0, "")) | ||
|
||
|
||
@mock.patch.object(utils_kernel_module.os, 'listdir', return_value=[some_module_param]) | ||
@mock.patch.object(utils_kernel_module, 'open', mock.mock_open(read_data=some_module_val + '\n')) | ||
@mock.patch.object(utils_kernel_module.process, 'getstatusoutput', getstatusoutput_ok) | ||
class TestReloadModule(unittest.TestCase): | ||
""" | ||
Tests the reload_module method | ||
""" | ||
|
||
def tearDown(self): | ||
getstatusoutput_ok.reset_mock() | ||
|
||
def assertLoadedWith(self, params): | ||
self.assertTrue(getstatusoutput_ok.called) | ||
cmd = getstatusoutput_ok.call_args[0][0] | ||
if params != "": | ||
self.assertTrue(params in cmd) | ||
else: | ||
self.assertTrue(cmd.endswith(some_module_name)) | ||
|
||
def assertNotLoaded(self): | ||
getstatusoutput_ok.assert_not_called() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=False) | ||
def test_tc1(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=True, params=some_module_params) | ||
self.assertLoadedWith(some_module_params) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=False) | ||
def test_tc2(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=False, params=some_module_params) | ||
self.assertNotLoaded() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc3(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=True, params=some_module_params) | ||
self.assertLoadedWith(some_module_params) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc3_empty(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=True, params="") | ||
self.assertLoadedWith("") | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc4(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=True, params="key=value") | ||
self.assertLoadedWith("key=value") | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc5(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=False, params=some_module_params) | ||
self.assertNotLoaded() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc5_empty(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=False, params="") | ||
self.assertNotLoaded() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc6(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(force=False, params="key=value") | ||
self.assertLoadedWith("key=value") | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc6_partial(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.assertEqual(some_module_params, self.handler.config_backup) | ||
self.handler.reload_module(force=False, params=some_module_params + " key=value") | ||
self.assertLoadedWith("key=value") | ||
|
||
|
||
@mock.patch.object(utils_kernel_module.process, 'getstatusoutput', getstatusoutput_ok) | ||
@mock.patch.object(utils_kernel_module.os, 'listdir', return_value=[some_module_param]) | ||
@mock.patch.object(utils_kernel_module, 'open', mock.mock_open(read_data=some_module_val + '\n')) | ||
class TestInit(unittest.TestCase): | ||
""" | ||
Tests if module status is backed up correctly | ||
""" | ||
|
||
def tearDown(self): | ||
getstatusoutput_ok.reset_mock() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_reloaded(self, *mocks): | ||
handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.assertTrue(handler.was_loaded) | ||
self.assertEqual(some_module_params, handler.config_backup) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=False) | ||
def test_not_reloaded(self, *mocks): | ||
handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.assertFalse(handler.was_loaded) | ||
self.assertIsNone(handler.config_backup) | ||
|
||
|
||
@mock.patch.object(utils_kernel_module.os, 'listdir', return_value=[some_module_param]) | ||
@mock.patch.object(utils_kernel_module, 'open', mock.mock_open(read_data=some_module_val + '\n')) | ||
@mock.patch.object(utils_kernel_module.process, 'getstatusoutput', getstatusoutput_ok) | ||
class TestRestore(unittest.TestCase): | ||
""" | ||
Tests the restore method | ||
""" | ||
|
||
def tearDown(self): | ||
getstatusoutput_ok.reset_mock() | ||
|
||
def assertRestored(self, params): | ||
self.assertTrue(getstatusoutput_ok.called) | ||
cmd = getstatusoutput_ok.call_args[0][0] | ||
self.assertTrue(params in cmd) | ||
|
||
def assertNoRestore(self): | ||
self.assertFalse(getstatusoutput_ok.called) | ||
|
||
def assertUnloaded(self): | ||
self.assertTrue(getstatusoutput_ok.called) | ||
cmd = getstatusoutput_ok.call_args[0][0] | ||
self.assertTrue("modprobe -r" in cmd) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc1(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
orig_config = self.handler.config_backup | ||
self.handler.reload_module(True, "key=value") | ||
self.handler.restore() | ||
self.assertRestored(orig_config) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc1_reload_twice(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
orig_config = self.handler.config_backup | ||
self.handler.reload_module(True, "key=value") | ||
self.handler.reload_module(True, "key1=value1") | ||
self.handler.restore() | ||
self.assertRestored(orig_config) | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=True) | ||
def test_tc2(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.restore() | ||
self.assertNoRestore() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=False) | ||
def test_tc3(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.reload_module(True, "key=value") | ||
self.handler.restore() | ||
self.assertUnloaded() | ||
|
||
@mock.patch.object(utils_kernel_module.os.path, 'exists', return_value=False) | ||
def test_tc4(self, *mocks): | ||
self.handler = utils_kernel_module.KernelModuleHandler(some_module_name) | ||
self.handler.restore() | ||
self.assertNoRestore() | ||
|
||
|
||
@mock.patch.object(utils_kernel_module.KernelModuleHandler, '__init__', return_value=None) | ||
@mock.patch.object(utils_kernel_module.KernelModuleHandler, 'reload_module', return_value=None) | ||
class TestReload(unittest.TestCase): | ||
""" | ||
Tests the module global reload method | ||
""" | ||
|
||
def test_empty_params_1(self, *mocks): | ||
self.assertIsNone(utils_kernel_module.reload(some_module_name, force=False, params="")) | ||
|
||
def test_empty_params_2(self, *mocks): | ||
self.assertIsNotNone(utils_kernel_module.reload(some_module_name, force=True, params="")) | ||
|
||
def test_non_empty_params_1(self, *mocks): | ||
self.assertIsNotNone(utils_kernel_module.reload(some_module_name, force=False, params=some_module_params)) | ||
|
||
def test_non_empty_params_2(self, *mocks): | ||
self.assertIsNotNone(utils_kernel_module.reload(some_module_name, force=True, params=some_module_params)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.