Skip to content

Commit

Permalink
Merge pull request #897 from xutian/fix_mems
Browse files Browse the repository at this point in the history
qemu_vm: add params for add memory devices
  • Loading branch information
Xu Tian authored Mar 31, 2017
2 parents a9584b8 + 683ac01 commit 720b595
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
31 changes: 5 additions & 26 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import copy
import urllib2
import math
import multiprocessing

import aexpect
Expand Down Expand Up @@ -513,35 +514,13 @@ def process(test, params, env, image_func, vm_func, vm_first=False):
:param vm_func: A function to call for each VM.
:param vm_first: Call vm_func first or not.
"""
def validate_memory_resource():
"""Validate host has enough memory to lauch VMs"""
magnification, requried_mem, count = 1.0, 0, 1
for vm_name in params.objects("vms"):
vm_params = params.object_params(vm_name)
if vm_params.get("start_vm") == "yes":
mem = "%sM" % vm_params.get("mem", 512)
requried_mem += float(
utils_misc.normalize_data_size(mem))
count += 1
if (params.get("setup_ksm") == "yes" and
params.get("ksm_run", "1") == "1"):
magnification = 1.2
free_mem = "%s KB" % memory.read_from_meminfo('MemFree')
free_mem = float(utils_misc.normalize_data_size(free_mem))
provide_mem = free_mem * magnification
# make memory size aligned to 256Mib
suggest_mem = int(provide_mem / count / 256) * 256 + 256
return (requried_mem > provide_mem, suggest_mem)

def _call_vm_func():
need_reset, suggest_mem = validate_memory_resource()
if need_reset:
# Convert 'MemFree' from KB to MB, then split it evenly
params["mem"] = suggest_mem
logging.warn("No enough free memory to launch VMs, "
"reset guest memory to %s MB" % params["mem"])
for vm_name in params.objects("vms"):
free_mem = "%s KB" % memory.read_from_meminfo('MemFree')
free_mem = float(utils_misc.normalize_data_size(free_mem))
max_mem = int(math.ceil(free_mem / 1024) * 1024)
vm_params = params.object_params(vm_name)
vm_params["max_usable_mem"] = str(max_mem)
vm_func(test, vm_params, env, vm_name)

def _call_image_func():
Expand Down
4 changes: 2 additions & 2 deletions virttest/qemu_devices/qcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,8 @@ def memory_define_by_params(self, params, name):
pc-dimm devices.
"""
devices = []
if not self.has_option("object"):
logging.warn("QOM does not support by your qemu")
if not self.has_device("pc-dimm"):
logging.warn("'PC-DIMM' does not support by your qemu")
return devices
mem = self.memory_object_define_by_params(params, name)
if mem:
Expand Down
41 changes: 24 additions & 17 deletions virttest/qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,25 +896,32 @@ def add_memorys(devices, params):
:param devices: VM devices container
"""
options = []
options, devs = [], []
params.setdefault("mem", "512")
mem_params = params.object_params("mem")
if mem_params.get("maxmem"):
mem_params.setdefault("automem", "yes")
automem = mem_params["automem"] == "yes"
if automem:
normalize_data_size = utils_misc.normalize_data_size
values = map(lambda x: "%sM" % mem_params.get(x, 512), ["maxmem", "mem"])
values = map(lambda x: int(float(normalize_data_size(x))), values)
params["mem"] = min(values)
options.append("%s" % params.get("mem", 512))
if mem_params.get("slots") and mem_params.get("maxmem"):
options.append("slots=%s" % mem_params["slots"])
options.append("maxmem=%s" % mem_params["maxmem"])

cmdline = "-m %s" % ",".join(options)
dev = StrDev("mem", cmdline=cmdline)
devices.insert(dev)
for name in mem_params.objects("mem_devs"):
memdev_params = mem_params.object_params(name)
mem_devs = devices.memory_define_by_params(memdev_params, name)
devices.insert(mem_devs)
mem_size_m = "%sM" % mem_params["mem"]
mem_size_m = int(float(normalize_data_size(mem_size_m)))
max_usable_mem_m = int(mem_params["max_usable_mem"])
if mem_size_m >= max_usable_mem_m:
logging.debug("Host no enough free memory, reset guest"
" memory size to %s MB" % max_usable_mem)
params["mem"] = max_usable_mem_m
options.append(params["mem"])
if devices.has_device("pc-dimm"):
if mem_params.get("slots") and mem_params.get("maxmem"):
options.append("slots=%s" % mem_params["slots"])
options.append("maxmem=%s" % mem_params["maxmem"])
for name in mem_params.objects("mem_devs"):
memdev_params = mem_params.object_params(name)
dev = devices.memory_define_by_params(memdev_params, name)
devs.extend(dev)
cmdline = "-m %s" % ",".join(map(str, options))
devs.insert(0, StrDev("mem", cmdline=cmdline))
devices.insert(devs)
return devices

def add_spice_rhel5(devices, spice_params, port_range=(3100, 3199)):
Expand Down

0 comments on commit 720b595

Please sign in to comment.