forked from xenserver/python-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom0.py
More file actions
182 lines (149 loc) · 6.25 KB
/
dom0.py
File metadata and controls
182 lines (149 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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__ import absolute_import
from __future__ import division
import re
from . import version
from .compat import open_with_codec_handling
def default_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 // 1024
if gb < 24:
return 752 * 1024
if gb < 48:
return 2 * 1024 * 1024
if gb < 64:
return 3 * 1024 * 1024
return 4 * 1024 * 1024
def default_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 GiB
return min(1024 + int(mb * 0.05) & ~0xF, 8192) * 1024
def default_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."""
if platform_version < version.Version([2, 9, 0]):
return default_memory_v2(host_mem_kib)
else:
return default_memory_v3(host_mem_kib)
def crash_kernel_memory_for_version(platform_version):
"""Return the default crash kernel memory size in KiB for the given
platform version."""
# Need to update this if we change the crash kernel memory in the future version
if platform_version >= version.Version([3, 99, 90]):
return 512 * 1024
if platform_version >= version.Version([3, 0, 50]):
return 256 * 1024
# Earlier versions are far past EOL, so don't consider them
return 192 * 1024
def _read_platform_version():
"""Read PLATFORM_VERSION from /etc/xensource-inventory."""
with open_with_codec_handling("/etc/xensource-inventory") as f:
for l in f.readlines():
line = l.strip()
if line.startswith('PLATFORM_VERSION='):
return version.Version.from_string(
line.split('=', 1)[1].strip("'"))
raise RuntimeError('Could not find PLATFORM_VERSION from inventory.')
def default_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"""
return default_memory_for_version(host_mem_kib, _read_platform_version())
def crash_kernel_memory():
"""Return the default crash kernel memory size in KiB for the
current platform version."""
return crash_kernel_memory_for_version(_read_platform_version())
_size_and_unit_re = re.compile(r"^(-?\d+)([bkmg]?)$", re.IGNORECASE)
def _parse_size_and_unit(s):
m = _size_and_unit_re.match(s)
if not m:
return None
val = int(m.group(1))
unit = m.group(2).lower()
if unit == "g":
val *= 1024*1024*1024
elif unit == "m":
val *= 1024*1024
elif unit in ("k", ""):
val *= 1024
return val
def parse_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("=")
if len(t) < 2 or t[0] != "dom0_mem":
return (None, None, None)
dom0_mem = None
dom0_mem_min = None
dom0_mem_max = None
#
# This is an equivalent to the parse_dom0_mem() call in
# xen/arch/x86/domain_build.c
#
for s in t[1].split(","):
if s.startswith("min:"):
dom0_mem_min = _parse_size_and_unit(s[4:])
elif s.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)
def default_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 memory
if dom0_mem_mb is not None:
if dom0_mem_mb < 2 * 1024:
max_vcpus = 4
elif dom0_mem_mb < 4 * 1024:
max_vcpus = 8
# Special case (minimum)
if host_pcpus == 0:
return 1
# vCPUs = host_pcpus for host pcpus <= 16
if host_pcpus <= 16:
return min(host_pcpus, max_vcpus)
# 16 for anything greater than 16
return min(16, max_vcpus)