Skip to content

Commit 6c18a5e

Browse files
committed
memory limit only for linux when no swap.
removed mac that has dynamic swap limit
1 parent 48967e6 commit 6c18a5e

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

codeflash/verification/pytest_plugin.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sys
1212
import time
1313
import warnings
14+
from pathlib import Path
1415
from typing import TYPE_CHECKING, Any, Callable
1516
from unittest import TestCase
1617

@@ -37,17 +38,25 @@ class UnexpectedError(Exception):
3738
pass
3839

3940

40-
if platform.system() == "Linux" or platform.system() == "Darwin":
41+
if platform.system() == "Linux":
4142
import resource
4243

43-
# Get total system memory
44-
total_memory = os.sysconf("SC_PAGE_SIZE") * os.sysconf(
45-
"SC_PHYS_PAGES"
46-
) # Set memory limit to 80% of total system memory
47-
memory_limit = int(total_memory * 0.8)
48-
49-
# Set both soft and hard limits
50-
resource.setrlimit(resource.RLIMIT_AS, (memory_limit, memory_limit))
44+
# We set the memory limit to 85% of total system memory when no swap exists
45+
swap_file_path = Path("/proc/swaps")
46+
swap_exists = swap_file_path.is_file()
47+
if swap_exists:
48+
with swap_file_path.open("r") as f:
49+
swap_exists = len(f.readlines()) > 1 # First line is header
50+
51+
if not swap_exists:
52+
# Get total system memory
53+
total_memory = os.sysconf("SC_PAGE_SIZE") * os.sysconf(
54+
"SC_PHYS_PAGES"
55+
) # Set the memory limit to 85% of total system memory
56+
memory_limit = int(total_memory * 0.85)
57+
58+
# Set both soft and hard limits
59+
resource.setrlimit(resource.RLIMIT_AS, (memory_limit, memory_limit))
5160

5261

5362
def pytest_addoption(parser: Parser) -> None:

0 commit comments

Comments
 (0)