# Copyright (c) 2013, Citrix Inc.# All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are met:## 1. Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer.# 2. Redistributions in binary form must reproduce the above copyright notice,# this list of conditions and the following disclaimer in the documentation# and/or other materials provided with the distribution.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.from__future__importabsolute_importfrom__future__importdivisionimportrefrom.importversionfrom.compatimportopen_with_codec_handling
[docs]defdefault_memory_v2(host_mem_kib):"""Return the default for the amount of dom0 memory for the specified amount of host memory for platform versions < 2.9.0."""## The host memory reported by Xen is a bit less than the physical# RAM installed in the machine since it doesn't include the memory# used by Xen etc.## Add a bit extra to account for this.#gb=(host_mem_kib+256*1024)//1024//1024ifgb<24:return752*1024ifgb<48:return2*1024*1024ifgb<64:return3*1024*1024return4*1024*1024
[docs]defdefault_memory_v3(host_mem_kib):"""Return the default for the amount of dom0 memory for the specified amount of host memory for platform versions >= 2.9.0."""## The host memory reported by Xen is a bit less than the physical# RAM installed in the machine since it doesn't include the memory# used by Xen etc.## Add a bit extra to account for this.#mb=(host_mem_kib+256*1024)//1024# Give dom0 1 GiB + 5% of host memory, rounded to 16 MiB, limited to 8 GiBreturnmin(1024+int(mb*0.05)&~0xF,8192)*1024
[docs]defdefault_memory_for_version(host_mem_kib,platform_version):"""Return the default for the amount of dom0 memory for the specified amount of host memory for the given platform version."""ifplatform_version<version.Version([2,9,0]):returndefault_memory_v2(host_mem_kib)else:returndefault_memory_v3(host_mem_kib)
[docs]defdefault_memory(host_mem_kib):"""Return the default for the amount of dom0 memory for the specified amount of host memory for the current platform version"""# read current host versionplatform_version=Nonewithopen_with_codec_handling("/etc/xensource-inventory")asf:forlinf.readlines():line=l.strip()ifline.startswith('PLATFORM_VERSION='):platform_version=version.Version.from_string(line.split('=',1)[1].strip("'"))breakifnotplatform_version:raiseRuntimeError('Could not find PLATFORM_VERSION from inventory.')returndefault_memory_for_version(host_mem_kib,platform_version)
[docs]defparse_mem(arg):"""Parse Xen's dom0_mem command line option. Return tuple of (amount, min, max) memory in bytes from a string in the following format: dom0_mem=[min:<min_amt>,][max:<max_amt>,][<amt>] See also Xen's docs/txt/misc/xen-command-line.txt."""t=arg.split("=")iflen(t)<2ort[0]!="dom0_mem":return(None,None,None)dom0_mem=Nonedom0_mem_min=Nonedom0_mem_max=None## This is an equivalent to the parse_dom0_mem() call in# xen/arch/x86/domain_build.c#forsint[1].split(","):ifs.startswith("min:"):dom0_mem_min=_parse_size_and_unit(s[4:])elifs.startswith("max:"):dom0_mem_max=_parse_size_and_unit(s[4:])else:dom0_mem=_parse_size_and_unit(s)return(dom0_mem,dom0_mem_min,dom0_mem_max)
[docs]defdefault_vcpus(host_pcpus,dom0_mem_mb=None):"""Return the default number of dom0 vcpus for the specified number of host pcpus and the amount of dom0 memory."""max_vcpus=16# Calculate max number of vCPUs# based on the amount of available memoryifdom0_mem_mbisnotNone:ifdom0_mem_mb<2*1024:max_vcpus=4elifdom0_mem_mb<4*1024:max_vcpus=8# Special case (minimum)ifhost_pcpus==0:return1# vCPUs = host_pcpus for host pcpus <= 16ifhost_pcpus<=16:returnmin(host_pcpus,max_vcpus)# 16 for anything greater than 16returnmin(16,max_vcpus)