Skip to contentSkip to conversation

Transparent Hugepages test v2

PATCH In review Lucas Meneghel Rodrigues 2 participants 7 messages Jun 16, 2011 – Jun 17, 2011 KVM

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 virt: Add Transparent Hugepages setup v2

In reply to Lucas Meneghel Rodrigues

This class configures khugepaged to active mode, with functions to restore original guest configuration.

Changes from v1:
* Rather than a pre/post script, config is now part of the framework
* No need to store configuration in files anymore to restore host khugepaged original behavior

---
 client/virt/virt_test_setup.py |  143 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 142 insertions(+), 1 deletions(-)
client/virt/virt_test_setup.py +142 -1
index 3e1f5b5..792ffe6 100644
@@ -1,11 +1,152 @@
 """
 Library to perform pre/post test setup for KVM autotest.
 """
-import os, logging
+import os, logging, re, sre
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.bin import utils
 
 
+class THPError(Exception):
+    """
+    Base exception for Transparent Hugepage setup.
+    """
+    pass
+
+
+class THPNotSupportedError(THPError):
+    """
+    Thrown when host does not support tansparent hugepages.
+    """
+    pass
+
+
+class THPWriteConfigError(THPError):
+    """
+    Thrown when host does not support tansparent hugepages.
+    """
+    pass
+
+
+class THPKhugepagedError(THPError):
+    """
+    Thrown when khugepaged is not behaving as expected.
+    """
+    pass
+
+
+class TransparentHugePageConfig(object):
+    def __init__(self, test, params):
+        """
+        Find paths for transparent hugepages and kugepaged configuration. Also,
+        back up original host configuration so it can be restored during
+        cleanup.
+        """
+        self.params = params
+
+        RH_THP_PATH = "/sys/kernel/mm/redhat_transparent_hugepage"
+        UPSTREAM_THP_PATH = "/sys/kernel/mm/transparent_hugepage"
+        if os.path.isdir(RH_THP_PATH):
+            self.thp_path = RH_THP_PATH
+        elif os.path.isdir(UPSTREAM_THP_PATH):
+            self.thp_path = UPSTREAM_THP_PATH
+        else:
+            raise THPNotSupportedError("System doesn't support transparent "
+                                       "hugepages")
+
+        # test_cfg holds all the desired host config values we want to set
+        # before THP tests
+        test_cfg={"%s/defrag" % self.thp_path: "yes",
+                  "%s/enabled" % self.thp_path: "always",
+                  "%s/khugepaged/defrag" % self.thp_path: "yes",
+                  "%s/khugepaged/scan_sleep_millisecs" % self.thp_path: "100",
+                  "%s/khugepaged/pages_to_scan" % self.thp_path: "4096",
+                  "%s/khugepaged/alloc_sleep_millisecs" % self.thp_path: "100",
+                  "/sys/kernel/mm/ksm/run": "1",
+                  "/proc/sys/vm/nr_hugepages":"0"}
+        if os.path.isfile("%s/khugepaged/enabled" % self.thp_path):
+            test_cfg["%s/khugepaged/enabled" % self.thp_path] = "always"
+        if os.path.isfile("%s/khugepaged/max_ptes_none" % self.thp_path):
+            test_cfg["%s/khugepaged/max_ptes_none" % self.thp_path] = "511"
+            test_cfg["%s/defrag" % self.thp_path] = "always"
+
+        tmp_list = []
+        test_config = self.params.get("test_config", None)
+        if test_config is not None:
+            tmp_list = re.split(';', test_config)
+        while len(tmp_list) > 0:
+            tmp_cfg = tmp_list.pop()
+            test_cfg[re.split(":", tmp_cfg)[0]] = sre.split(":", tmp_cfg)[1]
+
+        self.original_config = {}
+        # Save host current config, so we can restore it during cleanup
+        for path in test_cfg:
+            param = open(path, 'r').read()
+            if ("enabled" in param) or ("defrag" in param):
+                param = re.split("\[|\]", param)[1] + '\n'
+            self.original_config[path] = param
+
+        self.test_config = test_cfg
+
+
+    def set_env(self):
+        """
+        Applies test configuration on the host.
+        """
+        if self.test_config:
+            for path in self.test_config.keys():
+                file(path, 'w').write(self.test_config[path])
+
+
+    def set_params(self, path_dict={}, filename="", value=""):
+        """
+        Sets the value of a config file for a given path.
+
+        @param path_dict: Dict of files' paths {path : value}
+        @param filename: Name of file to setup
+        @param value: Value set to the configuration files
+        """
+        for path in path_dict.keys():
+            if path in filename:
+                try:
+                    file(path, "w").write(value)
+                except IOError, e:
+                    raise THPWriteConfigError("Can not set %s to %s: %s" %
+                                              (value, filename, e))
+
+
+    def khugepaged_test(self):
+        """
+        Start, stop and frequency change test for khugepaged.
+        """
+        status_list = ["never", "always", "never"]
+        for status in status_list:
+            self.set_params(self.test_config, "enabled", status)
+            try:
+                utils.run('pgrep khugepaged')
+            except error.CmdError:
+                raise THPKhugepagedError("khugepaged can not be set to "
+                                         "status %s" % status)
+
+
+    def setup(self):
+        """
+        Configure host for testing. Also, check that khugepaged is working as
+        expected.
+        """
+        self.set_env()
+        self.khugepaged_test()
+
+
+    def cleanup(self):
+        """:
+        Restore the host's original configuration after test
+        """
+        for path in self.original_config:
+            p_file = open(path, 'w')
+            p_file.write(self.original_config[path])
+            p_file.close()
+
+
 class HugePageConfig(object):
     def __init__(self, params):
         """
Signature · 1 line
1.7.5.4
#5 lore raw

PATCH 2/3 KVM test: Add Transparent Hugepages subtests v2

In reply to Lucas Meneghel Rodrigues

Transparent hugepage test includes:

1) Smoke test and stress test
Smoking test is test the transparent hugepage is used by kvm and guest. Stress test test use a parallel dd to test the stability of transparent hugepages

2) Swap test
Bootup a vm and verify that it can be swapped out and swapped in correctly

3) Defrag test
Allocate hugepage for libhugetlbfs while defrag is on and off. Then compare the results

Changes from v1:
* Different paths to mount debugfs and tmpfs on
* Use of autotest API to execute commands
* Use more current guest virt API to execute commands

---
 client/tests/kvm/tests/trans_hugepage.py          |  101 +++++++++++++++++++
 client/tests/kvm/tests/trans_hugepage_defrag.py   |   85 ++++++++++++++++
 client/tests/kvm/tests/trans_hugepage_swapping.py |  109 +++++++++++++++++++++
 3 files changed, 295 insertions(+), 0 deletions(-)
 create mode 100644 client/tests/kvm/tests/trans_hugepage.py
 create mode 100644 client/tests/kvm/tests/trans_hugepage_defrag.py
 create mode 100644 client/tests/kvm/tests/trans_hugepage_swapping.py
client/tests/kvm/tests/trans_hugepage.py new file +101 -0
new file mode 100644
index 0000000..cbda16c
@@ -0,0 +1,101 @@
+import logging, time, commands, os, string, re
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib import utils
+from autotest_lib.client.virt import virt_test_utils, aexpect
+
+
[email protected]_aware
+def run_trans_hugepage(test, params, env):
+    """
+    KVM kernel hugepages user side test:
+    1) Smoke test
+    2) Stress test
+
+    @param test: KVM test object.
+    @param params: Dictionary with test parameters.
+    @param env: Dictionary with the test environment.
+    """
+    def get_mem_status(params, type):
+        if type == "host":
+            info = utils.system_output("cat /proc/meminfo")
+        else:
+            info = session.cmd("cat /proc/meminfo")
+        for h in re.split("\n+", info):
+            if h.startswith("%s" % params):
+                output = re.split('\s+', h)[1]
+        return output
+
+
+    # Check khugepage is used by guest
+    dd_timeout = float(params.get("dd_timeout", 900))
+    fail = 0
+    nr_ah = []
+    mem = params['mem']
+
+    debugfs_flag = 1
+    debugfs_path = os.path.join(test.tmpdir, 'debugfs')
+    mem_path = os.path.join("/tmp", 'thp_space')
+
+    error.context("smoke test setup")
+    if not os.path.ismount(debugfs_path):
+        if not os.path.isdir(debugfs_path):
+            os.makedirs(debugfs_path)
+        utils.run("mount -t debugfs none %s" % debugfs_path)
+
+    logging.info("Smoke test start")
+    error.context("smoke test")
+    login_timeout = float(params.get("login_timeout", "3600"))
+    vm = virt_test_utils.get_living_vm(env, params.get("main_vm"))
+    session = virt_test_utils.wait_for_login(vm, timeout=login_timeout)
+
+    nr_ah_before = get_mem_status('AnonHugePages', 'host')
+    if nr_ah_before <= 0:
+        raise error.TestFail("VM is not using transparent hugepages")
+
+    # Protect system from oom killer
+    if int(get_mem_status('MemFree', 'guest')) / 1024 < mem :
+        mem = int(get_mem_status('MemFree', 'guest')) / 1024
+
+    session.cmd("mkdir -p %s" % mem_path)
+
+    session.cmd("mount -t tmpfs -o size=%sM none %s" % (str(mem), mem_path))
+
+    count = mem / 4
+    session.cmd("dd if=/dev/zero of=%s/1 bs=4000000 count=%s" %
+                (mem_path, count), timeout=dd_timeout)
+
+    nr_ah_after = get_mem_status('AnonHugePages', 'host')
+
+    if nr_ah_after <= nr_ah_before:
+        logging.warning("VM did not use Transparent Hugepages during dd")
+
+    if debugfs_flag == 1:
+        if int(open('%s/kvm/largepages' % debugfs_path, 'r').read()) <= 0:
+            raise error.TestFail("KVM did not use Transparent Hugepages")
+
+    logging.info("Smoke test finished")
+
+    # Use parallel dd as stress for memory
+    count = count / 3
+    logging.info("Stress test start")
+    error.context("stress test")
+    output = session.cmd("for i in `seq %s`; do dd if=/dev/zero of=%s/$i "
+                         "bs=4000000 count=1& done" % (count, mem_path),
+                         timeout=dd_timeout)
+
+    if len(re.findall("No space", output)) > count * 0.05:
+        raise error.TestFail("Too many dd instances failed in guest")
+
+    try:
+        output = session.cmd('pidof dd')
+    except Exception:
+        output = None
+
+    if output is not None:
+        for i in re.split('\n+', o):
+            session.cmd('kill -9 %s' % i)
+
+    session.cmd("umount %s" % mem_path)
+    logging.info("Stress test finished")
+
+    session.close()
client/tests/kvm/tests/trans_hugepage_defrag.py new file +85 -0

85 lines not on this page · raw message

client/tests/kvm/tests/trans_hugepage_swapping.py new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#6 lore raw

PATCH 3/3 Add THP test variants to tests_base.cfg.sample v2

In reply to Lucas Meneghel Rodrigues

Changes from v1:
* No need to use pre/post scripts anymore

---
 client/tests/kvm/tests_base.cfg.sample |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
client/tests/kvm/tests_base.cfg.sample +15 -0
index bd8f884..1907fc1 100644
@@ -1076,6 +1076,21 @@ variants:
         kill_vm_gracefully = no
     # Do not define test variants below shutdown
 
+    - trans_hugepage: install setup unattended_install.cdrom
+        thp_test_config = ""
+        kill_vm = yes
+        login_timeout = 360
+        variants:
+            - base:
+                type = trans_hugepage
+                dd_timeout = 900
+            - defrag:
+                type = trans_hugepage_defrag
+            - swapping:
+                type = trans_hugepage_swapping
+                dd_timeout = 900
+                check_cmd_timeout = 900
+
 
 # NICs
 variants:
Signature · 1 line
1.7.5.4
#0 lore raw

cover Transparent Hugepages test v2

Implemented for the most part the suggestions proposed on the first review of the code. Now I ask Yiqiao to review this code and make any changes he might want to do.

Lucas Meneghel Rodrigues (3):
virt: Add Transparent Hugepages setup v2 KVM test: Add Transparent Hugepages subtests v2 Add THP test variants to tests_base.cfg.sample v2

 client/tests/kvm/tests/trans_hugepage.py          |  101 +++++++++++++++
 client/tests/kvm/tests/trans_hugepage_defrag.py   |   85 ++++++++++++
 client/tests/kvm/tests/trans_hugepage_swapping.py |  109 ++++++++++++++++
 client/tests/kvm/tests_base.cfg.sample            |   15 ++
 client/virt/virt_test_setup.py                    |  143 ++++++++++++++++++++-
 5 files changed, 452 insertions(+), 1 deletions(-)
 create mode 100644 client/tests/kvm/tests/trans_hugepage.py
 create mode 100644 client/tests/kvm/tests/trans_hugepage_defrag.py
 create mode 100644 client/tests/kvm/tests/trans_hugepage_swapping.py
Signature · 1 line
1.7.5.4
#2 lore raw

Re: [PATCH 1/3] virt: Add Transparent Hugepages setup v2

In reply to Lucas Meneghel Rodrigues

Hi Lucas,

client/virt/virt_test_setup.py line 20

Quoted 5 lines · Lucas Meneghel Rodrigues
client/virt/virt_test_setup.py +5 -0
+class THPNotSupportedError(THPError):
+    """
+    Thrown when host does not support tansparent hugepages.
+    """
+    pass

s/tansparent/transparent/

client/virt/virt_test_setup.py line 27

Quoted 5 lines
client/virt/virt_test_setup.py +5 -0
+class THPWriteConfigError(THPError):
+    """
+    Thrown when host does not support tansparent hugepages.
+    """
+    pass

same as above.

Quoted 12 lines
client/virt/virt_test_setup.py +12 -0
+class TransparentHugePageConfig(object):
+    def __init__(self, test, params):
+        """
+        Find paths for transparent hugepages and kugepaged configuration. Also,
+        back up original host configuration so it can be restored during
+        cleanup.
+        """
+        self.params = params
+
+        RH_THP_PATH = "/sys/kernel/mm/redhat_transparent_hugepage"
+        UPSTREAM_THP_PATH = "/sys/kernel/mm/transparent_hugepage"
+        if os.path.isdir(RH_THP_PATH):

client/virt/virt_test_setup.py line 58

Quoted 10 lines
client/virt/virt_test_setup.py +10 -0
+            self.thp_path = RH_THP_PATH
+        elif os.path.isdir(UPSTREAM_THP_PATH):
+            self.thp_path = UPSTREAM_THP_PATH
+        else:
+            raise THPNotSupportedError("System doesn't support transparent "
+                                       "hugepages")
+
+        # test_cfg holds all the desired host config values we want to set
+        # before THP tests
+        test_cfg={"%s/defrag" % self.thp_path: "yes",

upstream >=3.0.0 this would value would be 1 (on 2.6.38 it's still "yes").

client/virt/virt_test_setup.py line 60

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "%s/enabled" % self.thp_path: "always",
+                  "%s/khugepaged/defrag" % self.thp_path: "yes",

Upstream >=3.0.0 it's 1.

Quoted 1 line

+ "%s/khugepaged/scan_sleep_millisecs" % self.thp_path: "100",

So this is meant to increase khugepaged scan rate compared to the default.

client/virt/virt_test_setup.py line 63

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "%s/khugepaged/pages_to_scan" % self.thp_path: "4096",
+                  "%s/khugepaged/alloc_sleep_millisecs" % self.thp_path: "100",

An allocation failure is not very likely and it's generally better to keep this bigger than the scan_sleep_millisecs. I would keep a factor of 6 of difference, so you could set it to 600 if scan_sleep_millisecs is set to 100.

client/virt/virt_test_setup.py line 65

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "/sys/kernel/mm/ksm/run": "1",
+                  "/proc/sys/vm/nr_hugepages":"0"}

nr_hugepages is unlikely to matter.

Quoted 53 lines
client/virt/virt_test_setup.py +53 -0
+        if os.path.isfile("%s/khugepaged/enabled" % self.thp_path):
+            test_cfg["%s/khugepaged/enabled" % self.thp_path] = "always"
+        if os.path.isfile("%s/khugepaged/max_ptes_none" % self.thp_path):
+            test_cfg["%s/khugepaged/max_ptes_none" % self.thp_path] = "511"
+            test_cfg["%s/defrag" % self.thp_path] = "always"
+
+        tmp_list = []
+        test_config = self.params.get("test_config", None)
+        if test_config is not None:
+            tmp_list = re.split(';', test_config)
+        while len(tmp_list) > 0:
+            tmp_cfg = tmp_list.pop()
+            test_cfg[re.split(":", tmp_cfg)[0]] = sre.split(":", tmp_cfg)[1]
+
+        self.original_config = {}
+        # Save host current config, so we can restore it during cleanup
+        for path in test_cfg:
+            param = open(path, 'r').read()
+            if ("enabled" in param) or ("defrag" in param):
+                param = re.split("\[|\]", param)[1] + '\n'
+            self.original_config[path] = param
+
+        self.test_config = test_cfg
+
+
+    def set_env(self):
+        """
+        Applies test configuration on the host.
+        """
+        if self.test_config:
+            for path in self.test_config.keys():
+                file(path, 'w').write(self.test_config[path])
+
+
+    def set_params(self, path_dict={}, filename="", value=""):
+        """
+        Sets the value of a config file for a given path.
+
+        @param path_dict: Dict of files' paths {path : value}
+        @param filename: Name of file to setup
+        @param value: Value set to the configuration files
+        """
+        for path in path_dict.keys():
+            if path in filename:
+                try:
+                    file(path, "w").write(value)
+                except IOError, e:
+                    raise THPWriteConfigError("Can not set %s to %s: %s" %
+                                              (value, filename, e))
+
+
+    def khugepaged_test(self):
+        """

client/virt/virt_test_setup.py line 128

Quoted 10 lines
client/virt/virt_test_setup.py +10 -0
+        Start, stop and frequency change test for khugepaged.
+        """
+        status_list = ["never", "always", "never"]
+        for status in status_list:
+            self.set_params(self.test_config, "enabled", status)
+            try:
+                utils.run('pgrep khugepaged')
+            except error.CmdError:
+                raise THPKhugepagedError("khugepaged can not be set to "
+                                         "status %s" % status)

khugepaged will quit when enabled is set to never, but it's not a bug (it's to save the memory of the kernel thread stack when THP is disabled). So I'm unsure if the pgrep is going to create spurious errors.

#3 lore raw

Re: [PATCH 1/3] virt: Add Transparent Hugepages setup v2

Quoted 1 line · Andrea Arcangeli

Hi Lucas,

Hi Andrea, thanks for the review! Yiqiao is working on the patchset, v3 or v4 will contain the fixes you have pointed out.

Quoted 10 lines

client/virt/virt_test_setup.py line 58

Quoted 3 lines
client/virt/virt_test_setup.py +3 -0
+        # test_cfg holds all the desired host config values we want to set
+        # before THP tests
+        test_cfg={"%s/defrag" % self.thp_path: "yes",

upstream >=3.0.0 this would value would be 1 (on 2.6.38 it's still "yes").

client/virt/virt_test_setup.py line 60

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "%s/enabled" % self.thp_path: "always",
+                  "%s/khugepaged/defrag" % self.thp_path: "yes",

Upstream >=3.0.0 it's 1.

Ok, we'll set the values conditionally depending on the running kernel version.

Quoted 4 lines
Quoted 1 line

+ "%s/khugepaged/scan_sleep_millisecs" % self.thp_path: "100",

So this is meant to increase khugepaged scan rate compared to the default.

Yep.

Quoted 7 lines

client/virt/virt_test_setup.py line 63

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "%s/khugepaged/pages_to_scan" % self.thp_path: "4096",
+                  "%s/khugepaged/alloc_sleep_millisecs" % self.thp_path: "100",

An allocation failure is not very likely and it's generally better to keep this bigger than the scan_sleep_millisecs. I would keep a factor of 6 of difference, so you could set it to 600 if scan_sleep_millisecs is set to 100.

Ok, will do.

Quoted 4 lines

client/virt/virt_test_setup.py line 65

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "/sys/kernel/mm/ksm/run": "1",
+                  "/proc/sys/vm/nr_hugepages":"0"}

nr_hugepages is unlikely to matter.

Dropping this then.

Quoted 59 lines
Quoted 59 lines
client/virt/virt_test_setup.py +59 -0
+        if os.path.isfile("%s/khugepaged/enabled" % self.thp_path):
+            test_cfg["%s/khugepaged/enabled" % self.thp_path] = "always"
+        if os.path.isfile("%s/khugepaged/max_ptes_none" % self.thp_path):
+            test_cfg["%s/khugepaged/max_ptes_none" % self.thp_path] = "511"
+            test_cfg["%s/defrag" % self.thp_path] = "always"
+
+        tmp_list = []
+        test_config = self.params.get("test_config", None)
+        if test_config is not None:
+            tmp_list = re.split(';', test_config)
+        while len(tmp_list) > 0:
+            tmp_cfg = tmp_list.pop()
+            test_cfg[re.split(":", tmp_cfg)[0]] = sre.split(":", tmp_cfg)[1]
+
+        self.original_config = {}
+        # Save host current config, so we can restore it during cleanup
+        for path in test_cfg:
+            param = open(path, 'r').read()
+            if ("enabled" in param) or ("defrag" in param):
+                param = re.split("\[|\]", param)[1] + '\n'
+            self.original_config[path] = param
+
+        self.test_config = test_cfg
+
+
+    def set_env(self):
+        """
+        Applies test configuration on the host.
+        """
+        if self.test_config:
+            for path in self.test_config.keys():
+                file(path, 'w').write(self.test_config[path])
+
+
+    def set_params(self, path_dict={}, filename="", value=""):
+        """
+        Sets the value of a config file for a given path.
+
+        @param path_dict: Dict of files' paths {path : value}
+        @param filename: Name of file to setup
+        @param value: Value set to the configuration files
+        """
+        for path in path_dict.keys():
+            if path in filename:
+                try:
+                    file(path, "w").write(value)
+                except IOError, e:
+                    raise THPWriteConfigError("Can not set %s to %s: %s" %
+                                              (value, filename, e))
+
+
+    def khugepaged_test(self):
+        """
+        Start, stop and frequency change test for khugepaged.
+        """
+        status_list = ["never", "always", "never"]
+        for status in status_list:
+            self.set_params(self.test_config, "enabled", status)
+            try:
Quoted 8 lines

client/virt/virt_test_setup.py line 128

Quoted 4 lines
client/virt/virt_test_setup.py +4 -0
+                utils.run('pgrep khugepaged')
+            except error.CmdError:
+                raise THPKhugepagedError("khugepaged can not be set to "
+                                         "status %s" % status)

khugepaged will quit when enabled is set to never, but it's not a bug (it's to save the memory of the kernel thread stack when THP is disabled). So I'm unsure if the pgrep is going to create spurious errors.

Ok, will improve this check then, based on the expected behavior.

#4 lore raw

Re: [PATCH 1/3] virt: Add Transparent Hugepages setup v2

Quoted 5 lines · Lucas Meneghel Rodrigues
Quoted 1 line · Andrea Arcangeli

Hi Lucas,

Hi Andrea, thanks for the review! Yiqiao is working on the patchset, v3 or v4 will contain the fixes you have pointed out.

Thanks! BTW, I'm in the process of reviewing the other patches too.

Quoted 7 lines
Quoted 7 lines

client/virt/virt_test_setup.py line 58

Quoted 4 lines
client/virt/virt_test_setup.py +3 -0
+        # test_cfg holds all the desired host config values we want to set
+        # before THP tests
+        test_cfg={"%s/defrag" % self.thp_path: "yes",

upstream >=3.0.0 this would value would be 1 (on 2.6.38 it's still "yes").

Quoted 7 lines
Quoted 4 lines

client/virt/virt_test_setup.py line 60

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "%s/enabled" % self.thp_path: "always",
+                  "%s/khugepaged/defrag" % self.thp_path: "yes",

Upstream >=3.0.0 it's 1.

Ok, we'll set the values conditionally depending on the running kernel version.

Checking the kernel version may be less reliable than reading it back and seeing if it's yes|no or 0|1 (not sure how the stable kernels will work with 3.0.0 etc..), the future numbering seems a bit variable these days. Theoretically checking the version number is better, but I suspect reading it and handling both formats without regard of the kernel version is simpler in the end. But it's definitely up to you.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+ "%s/khugepaged/scan_sleep_millisecs" % self.thp_path: "100",

So this is meant to increase khugepaged scan rate compared to the default.

Yep.

Sounds good!

Quoted 6 lines
Quoted 4 lines

client/virt/virt_test_setup.py line 65

Quoted 2 lines
client/virt/virt_test_setup.py +2 -0
+                  "/sys/kernel/mm/ksm/run": "1",
+                  "/proc/sys/vm/nr_hugepages":"0"}

nr_hugepages is unlikely to matter.

Dropping this then.

I think you can drop it yes. I think this only could cause hugetlbfs allocation failures if there's some VM backed by hugetlbfs pages that could then fail allocation and they have no fallback like THP.

hugetlbfs and THP are completely separated handled, even if they share plenty code for the clear-copy and put_page/get_page/head/tail/compound logics.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
client/virt/virt_test_setup.py +12 -0
+    def khugepaged_test(self):
+        """
+        Start, stop and frequency change test for khugepaged.
+        """
+        status_list = ["never", "always", "never"]
+        for status in status_list:
+            self.set_params(self.test_config, "enabled", status)
+            try:
+                utils.run('pgrep khugepaged')
+            except error.CmdError:
+                raise THPKhugepagedError("khugepaged can not be set to "
+                                         "status %s" % status)
Quoted 5 lines
Quoted 3 lines

khugepaged will quit when enabled is set to never, but it's not a bug (it's to save the memory of the kernel thread stack when THP is disabled). So I'm unsure if the pgrep is going to create spurious errors.

Ok, will improve this check then, based on the expected behavior.

If you wait a few seconds (a few seconds should be enough even with heavy loads) khugepaged should disappear after setting enabled=never, and again in a few seconds it should appear after setting enabled=always.

So when setting enabled=never, you can wait with a timeout until pgrep throws the CmdError as expected, and print an error if it pgrep keeps succeeding. The opposite when setting enabled=always.

This is a bit kernel dependent (for example other kernel daemons always are present even when the feature is disabled, I handled the races in shutting down and turning on the daemon by optimizing it more, but it's not very important to check that the khugepaged goes away, so you may just limit the pgrep check to when enabled=always to verify khugepaged is there, but you should wait a few seconds before failing as it may take a reschedule for khugepaged to start).

Thanks,
Andrea