提交 a990e7f1 编写于 作者: Y Yiqiao Pu 提交者: Lucas Meneghel Rodrigues

virttest.test_setup: Add KSM setup class

This class will setup and clean KSM config in different hosts.
You can use setup_ksm to configure it. There are several params
that can be used for ksm configure:

ksm_pages_to_scan: pages ksmd will scan in one loop
ksm_sleep_ms: million secs ksmd will sleep between two scan
ksm_run: start ksmd or not
ksm_module: if ksm is not compiled into kerenl need this parameter
            tell scripts which module should it load
disable_ksmtuned: if we need idsable ksmtuned during our test

This class also can be used to disable KSM in some special case with
set setup_ksm to yes and ksm_run to 0 or no.
Signed-off-by: NYiqiao Pu <ypu@redhat.com>
上级 27bb1c4b
......@@ -60,7 +60,7 @@ variants:
ksm_base:
status_query_cmd = "ksmctl info"
setup_cmd = "ksmctl start 5000 50"
query_cmd = "top -p `pidof qemu` -n 1"
query_cmd = "top -p QEMU_PID -n 1"
query_regex = "(\d+.\d+\w)\s+.\s+\d+\.\d+\s+\d+\.\d+\s+\d+:\d+\.\d+\s+qemu"
ksm_module = "ksm"
cpuinfo_query.qmachine_type:
......@@ -88,8 +88,6 @@ variants:
host_kernel_ver_str += ".6"
netdev_peer_re = "\s{2,}(.*?):.*?peer=(.*?)\s"
ksm_base:
status_query_cmd = "cat /sys/kernel/mm/ksm/run"
setup_cmd = "echo 1 > /sys/kernel/mm/ksm/run"
query_cmd = "cat /sys/kernel/mm/ksm/pages_sharing"
virtio_net:
vhost = "vhost=on"
......@@ -127,8 +125,6 @@ variants:
block_reopen_cmd = "block-job-complete"
check_event = yes
ksm_base:
status_query_cmd = "cat /sys/kernel/mm/ksm/run"
setup_cmd = "echo 1 > /sys/kernel/mm/ksm/run"
query_cmd = "cat /sys/kernel/mm/ksm/pages_sharing"
flag_check:
pattern = r"flags:\s+(.*)\s+(.*)\s+(.*)\s+(.*)"
......
......@@ -2,8 +2,7 @@
only Linux
type = ksm_base
virt_test_type = qemu
status_query_cmd = "cat /sys/kernel/mm/ksm/run"
setup_cmd = "echo 1 > /sys/kernel/mm/ksm/run"
setup_ksm = yes
shared_mem = 1024
query_cmd = "cat /sys/kernel/mm/ksm/pages_sharing"
split = "yes"
......
......@@ -16,6 +16,7 @@
# Host memory reserve (default - best fit for used mem)
# ksm_host_reserve = 512
# ksm_guest_reserve = 1024
setup_ksm = yes
variants:
- ksm_serial:
ksm_mode = "serial"
......
......@@ -3,6 +3,7 @@ from autotest.client.shared import error
from virttest import aexpect, utils_test, data_dir
@error.context_aware
def run_ksm_base(test, params, env):
"""
Test how KSM (Kernel Shared Memory) act when more than physical memory is
......@@ -63,127 +64,92 @@ def run_ksm_base(test, params, env):
vm.verify_alive()
session = vm.wait_for_login(timeout=timeout)
try:
logging.info("Start to prepare env in host")
# Check ksm module status
ksm_module = params.get("ksm_module")
if ksm_module:
module_status, _ = commands.getstatusoutput("lsmod |grep %s" %
ksm_module)
if module_status == 256:
s, _ = commands.getstatusoutput("modprobe %s" % ksm_module)
if s != 0:
raise error.TestError("Can not insert ksm module in host")
# Prepare env for KSM
s, ksmtuned_id = commands.getstatusoutput("ps -C ksmtuned -o pid=")
if ksmtuned_id:
logging.info("Turn off ksmtuned in host")
s, o = commands.getstatusoutput("kill -9 %s" % ksmtuned_id)
status_query_cmd = params.get("status_query_cmd")
setup_cmd = params.get("setup_cmd")
s, status = commands.getstatusoutput(status_query_cmd)
if int(re.findall("\d+", status)[0]) == 0:
s, o = commands.getstatusoutput(setup_cmd)
if s != 0:
raise error.TestError("Can not setup KSM: %s" % o)
# Prepare work in guest
logging.info("Turn off swap in guest")
session.cmd_status_output("swapoff -a")
script_file_path = os.path.join(data_dir.get_root_dir(),
"shared/scripts/ksm_overcommit_guest.py")
vm.copy_files_to(script_file_path, "/tmp")
test_type = params.get("test_type")
shared_mem = params.get("shared_mem")
get_free_mem_cmd = params.get("get_free_mem_cmd",
"grep MemFree /proc/meminfo")
free_mem = vm.get_memory_size(get_free_mem_cmd)
# Keep test from OOM killer
if free_mem < shared_mem:
shared_mem = free_mem
fill_timeout = int(shared_mem) / 10
query_cmd = params.get("query_cmd")
query_regex = params.get("query_regex")
random_bits = params.get("random_bits")
seed = random.randint(0, 255)
s, sharing_page_0 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_0 = re.findall(query_regex, sharing_page_0)[0]
_start_allocator(vm, session, 60)
mem_fill = "mem = MemFill(%s, 0, %s)" % (shared_mem, seed)
_execute_allocator(mem_fill, vm, session, fill_timeout)
cmd = "mem.value_fill()"
_execute_allocator(cmd, vm, session, fill_timeout)
time.sleep(120)
s, sharing_page_1 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_1 = re.findall(query_regex, sharing_page_1)[0]
split = params.get("split")
if split == "yes":
if test_type == "negative":
cmd = "mem.static_random_fill(%s)" % random_bits
else:
cmd = "mem.static_random_fill()"
_execute_allocator(cmd, vm, session, fill_timeout)
time.sleep(120)
s, sharing_page_2 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_2 = re.findall(query_regex, sharing_page_2)[0]
sharing_page = [sharing_page_0, sharing_page_1, sharing_page_2]
for i in sharing_page:
if re.findall("[A-Za-z]", i):
data = i[0:-1]
unit = i[-1]
index = sharing_page.index(i)
if unit == "g":
sharing_page[index] = utils_test.aton(data) * 1024
else:
sharing_page[index] = utils_test.aton(data)
fail_type = 0
if test_type == "disable":
if int(sharing_page[0]) != 0 and int(sharing_page[1]) != 0:
fail_type += 1
# Prepare work in guest
error.context("Turn off swap in guest", logging.info)
session.cmd_status_output("swapoff -a")
script_file_path = os.path.join(data_dir.get_root_dir(),
"shared/scripts/ksm_overcommit_guest.py")
vm.copy_files_to(script_file_path, "/tmp")
test_type = params.get("test_type")
shared_mem = params.get("shared_mem")
get_free_mem_cmd = params.get("get_free_mem_cmd",
"grep MemFree /proc/meminfo")
free_mem = vm.get_memory_size(get_free_mem_cmd)
# Keep test from OOM killer
if free_mem < shared_mem:
shared_mem = free_mem
fill_timeout = int(shared_mem) / 10
query_cmd = params.get("query_cmd")
query_regex = params.get("query_regex")
random_bits = params.get("random_bits")
seed = random.randint(0, 255)
query_cmd = re.sub("QEMU_PID", str(vm.process.get_pid()), query_cmd)
s, sharing_page_0 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_0 = re.findall(query_regex, sharing_page_0)[0]
error.context("Start to allocate pages inside guest", logging.info)
_start_allocator(vm, session, 60)
error.context("Turn off swap in guest", logging.info)
mem_fill = "mem = MemFill(%s, 0, %s)" % (shared_mem, seed)
_execute_allocator(mem_fill, vm, session, fill_timeout)
cmd = "mem.value_fill()"
_execute_allocator(cmd, vm, session, fill_timeout)
time.sleep(120)
s, sharing_page_1 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_1 = re.findall(query_regex, sharing_page_1)[0]
error.context("Start to fill memory with random value in guest",
logging.info)
split = params.get("split")
if split == "yes":
if test_type == "negative":
cmd = "mem.static_random_fill(%s)" % random_bits
else:
if int(sharing_page[0]) >= int(sharing_page[1]):
fail_type += 2
if int(sharing_page[1]) <= int(sharing_page[2]):
fail_type += 4
fail = ["Sharing page increased abnormally",
"Sharing page didn't increase", "Sharing page didn't split"]
if fail_type != 0:
turns = 0
while (fail_type > 0):
if fail_type % 2 == 1:
logging.error(fail[turns])
fail_type = fail_type / 2
turns += 1
raise error.TestFail("KSM test failed: %s %s %s" %
(sharing_page_0, sharing_page_1,
sharing_page_2))
finally:
if ksm_module:
if module_status == 256:
logging.info("unload ksm module")
s, o = commands.getstatusoutput("modprobe -r %s" % ksm_module)
if ksmtuned_id:
logging.info("Restart ksmtuned in host")
os.system("/bin/bash /usr/sbin/ksmtuned")
if int(re.findall("\d+", status)[0]) == 0:
logging.info("Reset the ksm env in host")
if re.findall("ksmctl", setup_cmd):
cmd = "ksmctl stop"
cmd = "mem.static_random_fill()"
_execute_allocator(cmd, vm, session, fill_timeout)
time.sleep(120)
s, sharing_page_2 = commands.getstatusoutput(query_cmd)
if query_regex:
sharing_page_2 = re.findall(query_regex, sharing_page_2)[0]
sharing_page = [sharing_page_0, sharing_page_1, sharing_page_2]
for i in sharing_page:
if re.findall("[A-Za-z]", i):
data = i[0:-1]
unit = i[-1]
index = sharing_page.index(i)
if unit == "g":
sharing_page[index] = utils_test.aton(data) * 1024
else:
cmd = "echo 0 > %s" % re.split(">", setup_cmd)[-1]
s, o = commands.getstatusoutput(cmd)
session.close()
sharing_page[index] = utils_test.aton(data)
fail_type = 0
if test_type == "disable":
if int(sharing_page[0]) != 0 and int(sharing_page[1]) != 0:
fail_type += 1
else:
if int(sharing_page[0]) >= int(sharing_page[1]):
fail_type += 2
if int(sharing_page[1]) <= int(sharing_page[2]):
fail_type += 4
fail = ["Sharing page increased abnormally",
"Sharing page didn't increase", "Sharing page didn't split"]
if fail_type != 0:
turns = 0
while (fail_type > 0):
if fail_type % 2 == 1:
logging.error(fail[turns])
fail_type = fail_type / 2
turns += 1
raise error.TestFail("KSM test failed: %s %s %s" %
(sharing_page_0, sharing_page_1,
sharing_page_2))
session.close()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册