Skip to content

Commit 9dca539

Browse files
Stephen ChengstephenchengCloud
authored andcommitted
CA-375347: Add API to get crash kernel memory by version
During RPU, XC needs to know the crash kernel memory size change. Signed-off-by: Stephen Cheng <stephen.cheng@citrix.com>
1 parent f6b180f commit 9dca539

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

tests/test_dom0.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from mock import patch, Mock
33

4-
from xcp.dom0 import default_memory, parse_mem, default_vcpus
4+
from xcp.dom0 import default_memory, crash_kernel_memory, parse_mem, default_vcpus
55

66

77
# pylint: disable=invalid-name
@@ -50,6 +50,29 @@ def mock_version(open_mock, version):
5050

5151
open_mock.assert_called_with("/etc/xensource-inventory")
5252

53+
def test_crash_kernel_memory(self):
54+
def mock_version(open_mock, version):
55+
file_mock = Mock()
56+
file_mock.readlines.return_value = iter(["PLATFORM_VERSION='%s'\n" % (version,)])
57+
open_mock.return_value.__enter__.return_value = file_mock
58+
59+
test_values = [
60+
('2.9.0', 192 * 1024), # Old version
61+
('3.0.49', 192 * 1024), # Just below 3.0.50
62+
('3.0.50', 256 * 1024), # Threshold for 256 MiB
63+
('3.4.0', 256 * 1024), # Between thresholds
64+
('3.99.50', 256 * 1024), # Below 3.99.90
65+
('3.99.90', 512 * 1024), # Threshold for 512 MiB
66+
('4.0.0', 512 * 1024), # Above threshold
67+
]
68+
69+
with patch("xcp.dom0.open_with_codec_handling") as open_mock:
70+
for ver, expected_kib in test_values:
71+
mock_version(open_mock, ver)
72+
self.assertEqual(crash_kernel_memory(), expected_kib)
73+
74+
open_mock.assert_called_with("/etc/xensource-inventory")
75+
5376
def test_parse_mem_arg(self):
5477
k = 1024
5578
M = 1024*1024

xcp/dom0.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,36 @@ def default_memory_for_version(host_mem_kib, platform_version):
7373
else:
7474
return default_memory_v3(host_mem_kib)
7575

76-
def default_memory(host_mem_kib):
77-
"""Return the default for the amount of dom0 memory for the
78-
specified amount of host memory for the current platform version"""
79-
80-
# read current host version
81-
platform_version = None
76+
def crash_kernel_memory_for_version(platform_version):
77+
"""Return the default crash kernel memory size in KiB for the given
78+
platform version."""
79+
# Need to update this if we change the crash kernel memory in the future version
80+
if platform_version >= version.Version([3, 99, 90]):
81+
return 512 * 1024
82+
if platform_version >= version.Version([3, 0, 50]):
83+
return 256 * 1024
84+
# Earlier versions are far past EOL, so don't consider them
85+
return 192 * 1024
86+
87+
def _read_platform_version():
88+
"""Read PLATFORM_VERSION from /etc/xensource-inventory."""
8289
with open_with_codec_handling("/etc/xensource-inventory") as f:
8390
for l in f.readlines():
8491
line = l.strip()
8592
if line.startswith('PLATFORM_VERSION='):
86-
platform_version = version.Version.from_string(
87-
line.split('=', 1)[1].strip("'"))
88-
break
93+
return version.Version.from_string(
94+
line.split('=', 1)[1].strip("'"))
95+
raise RuntimeError('Could not find PLATFORM_VERSION from inventory.')
8996

90-
if not platform_version:
91-
raise RuntimeError('Could not find PLATFORM_VERSION from inventory.')
97+
def default_memory(host_mem_kib):
98+
"""Return the default for the amount of dom0 memory for the
99+
specified amount of host memory for the current platform version"""
100+
return default_memory_for_version(host_mem_kib, _read_platform_version())
92101

93-
return default_memory_for_version(host_mem_kib, platform_version)
102+
def crash_kernel_memory():
103+
"""Return the default crash kernel memory size in KiB for the
104+
current platform version."""
105+
return crash_kernel_memory_for_version(_read_platform_version())
94106

95107

96108
_size_and_unit_re = re.compile(r"^(-?\d+)([bkmg]?)$", re.IGNORECASE)

0 commit comments

Comments
 (0)