Skip to content

Commit

Permalink
[202012][sonic_installer] consider existing swap when setting up swap…
Browse files Browse the repository at this point in the history
… mem (#2360)

* consider swap checking memory in installer

Signed-off-by: Xichen Lin <lukelin0907@gmail.com>

* update unit tests for swap allocator

Signed-off-by: Xichen Lin <lukelin0907@gmail.com>

Signed-off-by: Xichen Lin <lukelin0907@gmail.com>
Co-authored-by: Jing Kan <672454911@qq.com>
  • Loading branch information
Xichen96 and Blueve authored Sep 20, 2022
1 parent 7b8a62f commit b21e2ce
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sonic_installer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ def __enter__(self):
meminfo = self.read_from_meminfo()
mem_total_in_bytes = meminfo["MemTotal"] * SWAPAllocator.KiB_TO_BYTES_FACTOR
mem_avail_in_bytes = meminfo["MemAvailable"] * SWAPAllocator.KiB_TO_BYTES_FACTOR
if (mem_total_in_bytes < self.total_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR
or mem_avail_in_bytes < self.available_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR):
swap_total_in_bytes = meminfo["SwapTotal"] * SWAPAllocator.KiB_TO_BYTES_FACTOR
swap_free_in_bytes = meminfo["SwapFree"] * SWAPAllocator.KiB_TO_BYTES_FACTOR
if (mem_total_in_bytes + swap_total_in_bytes < self.total_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR
or mem_avail_in_bytes + swap_free_in_bytes < self.available_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR):
echo_and_log("Setup SWAP memory")
swapfile = SWAPAllocator.SWAP_FILE_PATH
if os.path.exists(swapfile):
Expand Down
64 changes: 64 additions & 0 deletions tests/swap_allocator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ def test_read_from_meminfo(self):
proc_meminfo_lines = [
"MemTotal: 32859496 kB",
"MemFree: 16275512 kB",
"SwapTotal: 2000000 kB",
"SwapFree: 1000000 kB",
"HugePages_Total: 0",
"HugePages_Free: 0",
]

read_meminfo_expected_return = {
"MemTotal": 32859496,
"MemFree": 16275512,
"SwapTotal": 2000000,
"SwapFree": 1000000,
"HugePages_Total": 0,
"HugePages_Free": 0
}
Expand Down Expand Up @@ -113,6 +117,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory(se
mock_meminfo.return_value = {
"MemTotal": 2000000,
"MemAvailable": 1900000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand All @@ -135,6 +141,56 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_available_memor
mock_meminfo.return_value = {
"MemTotal": 3000000,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

swap_allocator = SWAPAllocator(allocate=True)
try:
swap_allocator.__enter__()
except Exception as detail:
pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail))
mock_setup.assert_called_once()
mock_remove.assert_not_called()
assert swap_allocator.is_allocated is True

def test_swap_allocator_context_enter_allocate_true_insufficient_total_memory_plus_swap(self):
with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \
mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \
mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \
mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \
mock.patch("os.path.exists") as mock_exists:
mock_disk_free.return_value = 10 * 1024 * 1024 * 1024
mock_meminfo.return_value = {
"MemTotal": 1000000,
"MemAvailable": 900000,
"SwapTotal": 1000000,
"SwapFree": 1000000,
}
mock_exists.return_value = False

swap_allocator = SWAPAllocator(allocate=True)
try:
swap_allocator.__enter__()
except Exception as detail:
pytest.fail("SWAPAllocator context manager should not raise exception %s" % repr(detail))
mock_setup.assert_called_once()
mock_remove.assert_not_called()
assert swap_allocator.is_allocated is True

def test_swap_allocator_context_enter_allocate_true_insufficient_available_memory_plus_swap(self):
with mock.patch("sonic_installer.main.SWAPAllocator.get_disk_freespace") as mock_disk_free, \
mock.patch("sonic_installer.main.SWAPAllocator.read_from_meminfo") as mock_meminfo, \
mock.patch("sonic_installer.main.SWAPAllocator.setup_swapmem") as mock_setup, \
mock.patch("sonic_installer.main.SWAPAllocator.remove_swapmem") as mock_remove, \
mock.patch("os.path.exists") as mock_exists:
mock_disk_free.return_value = 10 * 1024 * 1024 * 1024
mock_meminfo.return_value = {
"MemTotal": 2000000,
"MemAvailable": 500000,
"SwapTotal": 1000000,
"SwapFree": 500000,
}
mock_exists.return_value = False

Expand All @@ -157,6 +213,8 @@ def test_swap_allocator_context_enter_allocate_true_insufficient_disk_space(self
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 16275512,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand All @@ -179,6 +237,8 @@ def test_swap_allocator_context_enter_allocate_true_swapfile_present(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = True

Expand All @@ -201,6 +261,8 @@ def test_swap_allocator_context_enter_setup_error(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False
expected_err_str = "Pseudo Error"
Expand All @@ -225,6 +287,8 @@ def test_swap_allocator_context_enter_allocate_false(self):
mock_meminfo.return_value = {
"MemTotal": 32859496,
"MemAvailable": 1000000,
"SwapTotal": 0,
"SwapFree": 0,
}
mock_exists.return_value = False

Expand Down

0 comments on commit b21e2ce

Please sign in to comment.