Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions nodescraper/plugins/inband/memory/memory_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def analyze_data(
def _bytes_to_gb(n: float) -> float:
return n / (1024**3)

free_memory = convert_to_bytes(data.mem_free)
available_memory = convert_to_bytes(data.mem_available or data.mem_free)
total_memory = convert_to_bytes(data.mem_total)
used_memory = total_memory - free_memory
used_memory = total_memory - available_memory

threshold_bytes = convert_to_bytes(args.memory_threshold)

Expand Down Expand Up @@ -93,7 +93,8 @@ def _bytes_to_gb(n: float) -> float:
"used_memory": used_memory,
"max_allowed_used_mem": max_allowed_used_mem,
"total_memory": total_memory,
"free_memory": free_memory,
"available_memory": available_memory,
"mem_free": convert_to_bytes(data.mem_free),
},
)

Expand Down
17 changes: 12 additions & 5 deletions nodescraper/plugins/inband/memory/memory_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[MemoryDataModel]
Returns:
tuple[TaskResult, Optional[MemoryDataModel]]: tuple containing the task result and memory data model or None if data is not available.
"""
mem_free, mem_total = None, None
mem_free, mem_total, mem_available = None, None, None
if self.system_info.os_family == OSFamily.WINDOWS:
os_memory_cmd = self._run_sut_cmd(self.CMD_WINDOWS)
if os_memory_cmd.exit_code == 0:
Expand All @@ -71,9 +71,12 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[MemoryDataModel]
else:
os_memory_cmd = self._run_sut_cmd(self.CMD)
if os_memory_cmd.exit_code == 0:
pattern = r"Mem:\s+(\d\.?\d*\w+)\s+\d\.?\d*\w+\s+(\d\.?\d*\w+)"
mem_free = re.search(pattern, os_memory_cmd.stdout).group(2)
mem_total = re.search(pattern, os_memory_cmd.stdout).group(1)
pattern = r"Mem:\s+(\S+)\s+\S+\s+(\S+)(?:\s+\S+\s+\S+\s+(\S+))?"
match = re.search(pattern, os_memory_cmd.stdout)
if match:
mem_total = match.group(1)
mem_free = match.group(2)
mem_available = match.group(3)

if os_memory_cmd.exit_code != 0:
self._log_event(
Expand Down Expand Up @@ -163,6 +166,7 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[MemoryDataModel]
mem_data = MemoryDataModel(
mem_free=mem_free,
mem_total=mem_total,
mem_available=mem_available,
lsmem_data=lsmem_data,
numa_topology=numa_topology,
)
Expand All @@ -172,7 +176,10 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[MemoryDataModel]
data=mem_data.model_dump(),
priority=EventPriority.INFO,
)
self.result.message = f"Memory: mem_free={mem_free}, mem_total={mem_total}"
if mem_available:
self.result.message = f"Memory: mem_total={mem_total}, mem_available={mem_available}, mem_free={mem_free}"
else:
self.result.message = f"Memory: mem_free={mem_free}, mem_total={mem_total}"
self.result.status = ExecutionStatus.OK
else:
mem_data = None
Expand Down
1 change: 1 addition & 0 deletions nodescraper/plugins/inband/memory/memorydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ class MemoryDataModel(DataModel):

mem_free: str
mem_total: str
mem_available: Optional[str] = None
lsmem_data: Optional[LsmemData] = None
numa_topology: Optional[NumaTopology] = None
22 changes: 22 additions & 0 deletions test/unit/plugin/test_memory_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ def test_config_provided(analyzer, model_obj):
assert result.status == ExecutionStatus.OK


def test_linux_low_free_high_available_passes(analyzer):
"""MemFree alone looks tight; MemAvailable reflects reclaimable cache."""
model = MemoryDataModel(
mem_total="128Gi",
mem_free="2Gi",
mem_available="100Gi",
)
args = MemoryAnalyzerArgs(memory_threshold="128Gi", ratio=0.66)
result = analyzer.analyze_data(model, args)
assert result.status == ExecutionStatus.OK


def test_linux_low_free_high_available_fails_without_mem_available(analyzer):
model = MemoryDataModel(
mem_total="128Gi",
mem_free="2Gi",
)
args = MemoryAnalyzerArgs(memory_threshold="128Gi", ratio=0.66)
result = analyzer.analyze_data(model, args)
assert result.status == ExecutionStatus.ERROR


def test_windows_like_memory(analyzer):
model = MemoryDataModel(mem_free="751720910848", mem_total="1013310287872")
result = analyzer.analyze_data(model)
Expand Down
3 changes: 2 additions & 1 deletion test/unit/plugin/test_memory_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def mock_run_command(command, **kwargs):

assert result.status == ExecutionStatus.OK
assert data.mem_free == "2097459761152"
assert data.mem_available == "2122320150528"
assert data.mem_total == "2164113772544"
assert data.lsmem_data is not None
assert len(data.lsmem_data.memory_blocks) == 2
Expand Down Expand Up @@ -165,8 +166,8 @@ def mock_run_command(command, **kwargs):

assert result.status == ExecutionStatus.OK
assert data.mem_free == "2097459761152"
assert data.mem_available == "2122320150528"
assert data.mem_total == "2164113772544"
assert data.lsmem_data is None
assert data.numa_topology is None
lsmem_events = [e for e in result.events if "lsmem" in e.description]
assert len(lsmem_events) > 0
Expand Down
Loading