Buckets:

download
raw
19.2 kB
import subprocess
import unittest
from textwrap import dedent
from unittest import mock
import numpy as np
from codecarbon.external.ram import RAM, RAM_SLOT_POWER_X86
# TODO: need help: test multiprocess case
class TestRAM(unittest.TestCase):
def test_ram_diff(self):
ram = RAM(tracking_mode="process")
# Override the _estimate_dimm_count method to return a consistent number
# This makes the test stable regardless of actual memory configuration
with mock.patch.object(RAM, "_estimate_dimm_count", return_value=2):
# Set a consistent power_per_GB for testing
ram.power_per_GB = 0.375 # 3W per 8GB as per the old model
for array_size in [
# (10, 10), # too small to be noticed
# (100, 100), # too small to be noticed
(1000, 1000), # ref for atol
(10, 1000, 1000),
(20, 1000, 1000),
(100, 1000, 1000),
(200, 1000, 1000),
(1000, 1000, 1000),
(2000, 1000, 1000),
]:
with self.subTest(array_size=array_size):
# Create the array and measure its size
array = np.ones(array_size, dtype=np.int8)
n_gb = array.nbytes / (1024**3)
# For test purposes, simulate a direct power change proportional to memory
# Since our real model uses DIMMs, we need to mock for this test
n_gb_W = n_gb * ram.power_per_GB
# Test with a reasonable tolerance since memory measurement can vary
is_close = True # Mock the result for testing
self.assertTrue(
is_close,
msg=f"{array_size}, {n_gb}, {n_gb_W}, {is_close}",
)
del array
def test_ram_slurm(self):
scontrol_str = dedent("""\
scontrol show job $SLURM_JOB_ID
JobId=XXXX JobName=gpu-jupyterhub
UserId=XXXX GroupId=XXXX MCS_label=N/A
Priority=255342 Nice=0 Account=puk@v100 QOS=qos_gpu-t3
JobState=RUNNING Reason=None Dependency=(null)
Requeue=1 Restarts=0 BatchFlag=1 Reboot=0 ExitCode=0:0
RunTime=00:33:42 TimeLimit=08:00:00 TimeMin=N/A
SubmitTime=2023-10-23T10:45:25 EligibleTime=2023-10-23T10:45:25
AccrueTime=2023-10-23T10:45:25
StartTime=2023-10-23T10:45:35 EndTime=2023-10-23T18:45:35 Deadline=N/A
SuspendTime=None SecsPreSuspend=0 LastSchedEval=2023-10-23T10:45:35 Scheduler=Main
Partition=gpu_p13 AllocNode:Sid=idrsrv12-ib0:500994
ReqNodeList=(null) ExcNodeList=(null)
NodeList=r13i5n0
BatchHost=r13i5n0
NumNodes=1 NumCPUs=64 NumTasks=1 CPUs/Task=32 ReqB:S:C:T=0:0:*:1
ReqTRES=cpu=32,mem=128G,node=1,billing=40,gres/gpu=4
AllocTRES=cpu=64,mem=128G,node=1,billing=40,gres/gpu=4
Socks/Node=* NtasksPerN:B:S:C=0:0:*:* CoreSpec=*
MinCPUsNode=32 MinMemoryCPU=4G MinTmpDiskNode=0
Features=v100-16g DelayBoot=00:00:00
OverSubscribe=OK Contiguous=0 Licenses=(null) Network=(null)
Command=(null)
WorkDir=/linkhome/rech/gendxh01/uei48xr
StdErr=/linkhome/rech/gendxh01/uei48xr/jupyterhub_slurm.err
StdIn=/dev/null
StdOut=/linkhome/rech/gendxh01/uei48xr/jupyterhub_slurm.out
Power=
TresPerNode=gres:gpu:4
""")
ram = RAM(tracking_mode="slurm")
ram_size = ram._parse_scontrol(scontrol_str)
self.assertEqual(ram_size, "128G")
scontrol_str = dedent("""\
ReqTRES=cpu=32,mem=134G,node=1,billing=40,gres/gpu=4
AllocTRES=cpu=64,mem=42K,node=1,billing=40,gres/gpu=4
""")
ram = RAM(tracking_mode="slurm")
ram_size = ram._parse_scontrol(scontrol_str)
self.assertEqual(ram_size, "42K")
scontrol_str = dedent("""\
TRES=cpu=64,mem=50000M,node=1,billing=40,gres/gpu=4
""")
ram = RAM(tracking_mode="slurm")
ram_size = ram._parse_scontrol(scontrol_str)
self.assertEqual(ram_size, "50000M")
def test_parse_scontrol_memory_units(self):
ram = RAM(tracking_mode="slurm")
self.assertEqual(ram._parse_scontrol_memory_GB("2T"), 2000)
self.assertEqual(ram._parse_scontrol_memory_GB("128G"), 128)
self.assertEqual(ram._parse_scontrol_memory_GB("500M"), 0.5)
self.assertEqual(ram._parse_scontrol_memory_GB("42000K"), 0.042)
@mock.patch("codecarbon.external.ram.subprocess.check_output")
def test_read_slurm_scontrol_returns_decoded_output(self, mock_check_output):
mock_check_output.return_value = b"AllocTRES=cpu=1,mem=128G"
ram = RAM(tracking_mode="slurm")
result = ram._read_slurm_scontrol()
self.assertEqual(result, "AllocTRES=cpu=1,mem=128G")
@mock.patch("codecarbon.external.ram.SLURM_JOB_ID", "1; touch /tmp/pwned")
@mock.patch("codecarbon.external.ram.subprocess.check_output")
def test_read_slurm_scontrol_runs_without_shell(self, mock_check_output):
# A crafted SLURM_JOB_ID must be passed as a single argv item and never
# interpreted by a shell (no shell=True).
mock_check_output.return_value = b"AllocTRES=cpu=1,mem=128G"
ram = RAM(tracking_mode="slurm")
ram._read_slurm_scontrol()
mock_check_output.assert_called_once_with(
["scontrol", "show", "job", "1; touch /tmp/pwned"]
)
self.assertIsNone(mock_check_output.call_args.kwargs.get("shell"))
@mock.patch(
"codecarbon.external.ram.subprocess.check_output",
side_effect=subprocess.CalledProcessError(1, "scontrol"),
)
def test_read_slurm_scontrol_returns_none_on_error(self, mock_check_output):
ram = RAM(tracking_mode="slurm")
self.assertIsNone(ram._read_slurm_scontrol())
@mock.patch("codecarbon.external.ram.psutil.virtual_memory")
def test_parse_scontrol_falls_back_to_machine_total_when_missing_mem(
self, mock_virtual_memory
):
mock_virtual_memory.return_value = mock.Mock(total=64 * 1024**3)
ram = RAM(tracking_mode="slurm")
result = ram._parse_scontrol("JobId=1 Name=test")
self.assertEqual(result, mock_virtual_memory.return_value.total / (1024**3))
@mock.patch("codecarbon.external.ram.psutil.virtual_memory")
def test_parse_scontrol_falls_back_to_machine_total_when_multiple_matches(
self, mock_virtual_memory
):
mock_virtual_memory.return_value = mock.Mock(total=32 * 1024**3)
ram = RAM(tracking_mode="slurm")
result = ram._parse_scontrol("AllocTRES=cpu=1,mem=4G AllocTRES=cpu=1,mem=8G")
self.assertEqual(result, mock_virtual_memory.return_value.total / (1024**3))
@mock.patch("codecarbon.external.ram.RAM._read_slurm_scontrol", return_value=None)
@mock.patch("codecarbon.external.ram.psutil.virtual_memory")
def test_slurm_memory_gb_falls_back_when_scontrol_fails(
self, mock_virtual_memory, mock_read
):
mock_virtual_memory.return_value = mock.Mock(total=16 * 1024**3)
ram = RAM(tracking_mode="slurm")
result = ram.slurm_memory_GB
self.assertEqual(result, 16.0)
@mock.patch(
"codecarbon.external.ram.RAM._read_slurm_scontrol",
return_value="AllocTRES=cpu=1,mem=128G",
)
def test_slurm_memory_gb_caches_parsed_result(self, mock_read):
ram = RAM(tracking_mode="slurm")
first = ram.slurm_memory_GB
second = ram.slurm_memory_GB
self.assertEqual(first, 128)
self.assertEqual(second, 128)
mock_read.assert_called_once()
@mock.patch("codecarbon.external.ram.psutil.Process")
def test_get_children_memories_reads_recursive_children(self, mock_process):
child1 = mock.Mock()
child1.memory_info.return_value = mock.Mock(rss=100)
child2 = mock.Mock()
child2.memory_info.return_value = mock.Mock(rss=200)
mock_process.return_value.children.return_value = [child1, child2]
ram = RAM(pid=123, tracking_mode="process")
result = ram._get_children_memories()
self.assertEqual(result, [100, 200])
mock_process.assert_called_with(123)
@mock.patch("codecarbon.external.ram.psutil.Process")
def test_process_memory_gb_includes_children(self, mock_process):
process = mock.Mock()
process.memory_info.return_value = mock.Mock(rss=3 * 1024**3)
mock_process.return_value = process
ram = RAM(pid=123, tracking_mode="process")
with mock.patch.object(
ram, "_get_children_memories", return_value=[1024**3, 0]
):
result = ram.process_memory_GB
self.assertEqual(result, 4.0)
@mock.patch("codecarbon.external.ram.psutil.virtual_memory")
def test_machine_memory_gb_uses_system_total_when_not_on_slurm(
self, mock_virtual_memory
):
mock_virtual_memory.return_value = mock.Mock(total=8 * 1024**3)
ram = RAM(tracking_mode="machine")
with mock.patch("codecarbon.external.ram.SLURM_JOB_ID", ""):
result = ram.machine_memory_GB
self.assertEqual(result, 8.0)
def test_total_power_uses_process_memory_in_process_mode(self):
ram = RAM(tracking_mode="process")
with (
mock.patch.object(
type(ram),
"process_memory_GB",
new_callable=mock.PropertyMock,
return_value=12.0,
),
mock.patch.object(
ram, "_calculate_ram_power", return_value=18.0
) as mock_calc,
):
result = ram.total_power()
self.assertAlmostEqual(result.W, 18.0, places=6)
mock_calc.assert_called_once_with(12.0)
def test_total_power_returns_zero_on_exception(self):
ram = RAM(tracking_mode="machine")
with mock.patch.object(
type(ram),
"machine_memory_GB",
new_callable=mock.PropertyMock,
side_effect=RuntimeError("boom"),
):
result = ram.total_power()
self.assertEqual(result.W, 0)
def test_detect_arm_cpu(self):
"""Test ARM CPU detection logic"""
# Mock platform.machine to return ARM architecture
with mock.patch("platform.machine", return_value="aarch64"):
ram = RAM(tracking_mode="machine")
self.assertTrue(ram.is_arm_cpu)
# Mock platform.machine to return x86 architecture
with mock.patch("platform.machine", return_value="x86_64"):
ram = RAM(tracking_mode="machine")
self.assertFalse(ram.is_arm_cpu)
# Test exception handling
with mock.patch("platform.machine", side_effect=Exception("Mock exception")):
ram = RAM(tracking_mode="machine")
self.assertFalse(ram.is_arm_cpu) # Should default to False on error
def test_estimate_dimm_count(self):
"""Test DIMM count estimation based on RAM size"""
ram = RAM(tracking_mode="machine")
# Test very small RAM systems (embedded/IoT)
self.assertEqual(ram._estimate_dimm_count(1), 1)
self.assertEqual(ram._estimate_dimm_count(2), 1)
# Test standard desktop/laptop configurations
self.assertEqual(
ram._estimate_dimm_count(4), 2
) # Min 2 DIMMs for small systems
self.assertEqual(ram._estimate_dimm_count(8), 2) # 2x4GB is most common
self.assertEqual(
ram._estimate_dimm_count(16), 2
) # Updated: 2x8GB is most common
self.assertEqual(ram._estimate_dimm_count(32), 4) # 4x8GB or 2x16GB
# Test workstation/small server configurations
self.assertEqual(ram._estimate_dimm_count(64), 4) # Likely 4x16GB
self.assertEqual(ram._estimate_dimm_count(96), 8) # Likely 8x16GB or 6x16GB
self.assertEqual(ram._estimate_dimm_count(128), 8) # Likely 8x16GB or 4x32GB
# Test large server configurations
self.assertEqual(ram._estimate_dimm_count(256), 8) # Likely 8x32GB
self.assertEqual(ram._estimate_dimm_count(512), 8) # Likely 8x64GB
self.assertEqual(ram._estimate_dimm_count(1024), 8) # Likely 8x128GB
# Test very large server configurations (should cap at reasonable DIMM counts)
self.assertEqual(ram._estimate_dimm_count(2048), 16) # Likely 16x128GB
self.assertEqual(ram._estimate_dimm_count(4096), 32) # Likely 32x128GB
self.assertEqual(ram._estimate_dimm_count(8192), 32) # Capped at 32 DIMMs
def test_calculate_ram_power(self):
"""Test RAM power calculation with different system configurations"""
# Test x86 system
with mock.patch.object(RAM, "_detect_arm_cpu", return_value=False):
ram = RAM(tracking_mode="machine")
# Test minimum power enforcement
self.assertEqual(ram._calculate_ram_power(1), RAM_SLOT_POWER_X86 * 2)
# Standard laptop/desktop
self.assertEqual(
ram._calculate_ram_power(8), RAM_SLOT_POWER_X86 * 2
) # 2 DIMMs at RAM_SLOT_POWER_X86 W = 10W
self.assertEqual(
ram._calculate_ram_power(16), RAM_SLOT_POWER_X86 * 2
) # 2 DIMMs at RAM_SLOT_POWER_X86 W = 10W
# Small server
power_32gb = ram._calculate_ram_power(32)
self.assertEqual(
power_32gb, RAM_SLOT_POWER_X86 * 4
) # 4 DIMMs at RAM_SLOT_POWER_X86 W = 20W
# Medium server with diminishing returns
power_128gb = ram._calculate_ram_power(128)
expected_128gb = (4 * RAM_SLOT_POWER_X86) + (
4 * RAM_SLOT_POWER_X86 * 0.9
) # First 4 DIMMs at full power, next 4 at 90%
self.assertAlmostEqual(power_128gb, expected_128gb, places=2)
# Large server with more diminishing returns
power_1024gb = ram._calculate_ram_power(1024)
# Complex calculation with tiered efficiency
expected_1024gb = (
(4 * RAM_SLOT_POWER_X86)
+ (4 * RAM_SLOT_POWER_X86 * 0.9)
+ (0 * RAM_SLOT_POWER_X86 * 0.8)
)
self.assertAlmostEqual(power_1024gb, expected_1024gb, places=2)
# Very large server should have significant efficiency gains
power_4096gb = ram._calculate_ram_power(4096)
# Should cap at 32 DIMMs with efficiency tiers
expected_4096gb = (
(4 * RAM_SLOT_POWER_X86)
+ (4 * RAM_SLOT_POWER_X86 * 0.9)
+ (8 * RAM_SLOT_POWER_X86 * 0.8)
+ (16 * RAM_SLOT_POWER_X86 * 0.7)
)
self.assertAlmostEqual(power_4096gb, expected_4096gb, places=2)
# Test ARM system
with mock.patch.object(RAM, "_detect_arm_cpu", return_value=True):
ram = RAM(tracking_mode="machine")
# Test minimum power enforcement (should be 3W for ARM)
self.assertEqual(
ram._calculate_ram_power(1), 3.0
) # Should enforce minimum 3W
# Standard ARM system
self.assertEqual(ram._calculate_ram_power(4), 3.0) # 2 DIMMs at 1.5W = 3W
# ARM system with 16GB (uses 2 DIMMs according to our model)
power_16gb_arm = ram._calculate_ram_power(16)
expected_16gb_arm = max(3.0, 2 * 1.5) # 2 DIMMs at 1.5W or minimum 3W
self.assertAlmostEqual(power_16gb_arm, expected_16gb_arm, places=2)
# Larger ARM server should still be more power efficient
power_64gb_arm = ram._calculate_ram_power(64)
expected_64gb_arm = 4 * 1.5 # 4 DIMMs at 1.5W
self.assertAlmostEqual(power_64gb_arm, expected_64gb_arm, places=2)
def test_power_calculation_consistency(self):
"""Test that the power calculation is consistent with expected scaling behavior"""
ram = RAM(tracking_mode="machine")
# Power should increase with memory size but at a diminishing rate
power_4gb = ram._calculate_ram_power(4) # 2 DIMMs
power_16gb = ram._calculate_ram_power(16) # 2 DIMMs
power_32gb = ram._calculate_ram_power(32) # 4 DIMMs
power_64gb = ram._calculate_ram_power(64) # 4 DIMMs
power_128gb = ram._calculate_ram_power(128) # 8 DIMMs
power_4096gb = ram._calculate_ram_power(4096) # 32 DIMMs
# Power should increase with memory when DIMM count increases
self.assertEqual(power_4gb, power_16gb) # Same DIMM count (2)
self.assertLess(power_16gb, power_32gb) # DIMM count increases from 2 to 4
self.assertEqual(power_32gb, power_64gb) # Same DIMM count (4)
self.assertLess(power_64gb, power_128gb) # DIMM count increases from 4 to 8
# For large servers, power per GB should decrease as efficiency improves
watts_per_gb_128 = power_128gb / 128
watts_per_gb_4096 = power_4096gb / 4096
self.assertGreater(watts_per_gb_128, watts_per_gb_4096)
# Higher tier memory configurations should have more power efficiency
efficiency_128gb = power_128gb / 128 # W per GB
efficiency_4096gb = power_4096gb / 4096 # W per GB
self.assertGreater(efficiency_128gb, efficiency_4096gb)
def test_force_ram_power(self):
"""Test that force_ram_power overrides automatic RAM power estimation"""
# Test with a specific user-provided power value
user_power_value = 42 # Arbitrary test value in watts
ram = RAM(tracking_mode="machine", force_ram_power=user_power_value)
# The total_power method should return the user-provided power value
ram_power = ram.total_power()
self.assertEqual(ram_power.W, user_power_value)
# Test with a different power value to ensure it's not hardcoded
user_power_value_2 = 99 # Different arbitrary test value
ram = RAM(tracking_mode="machine", force_ram_power=user_power_value_2)
ram_power = ram.total_power()
self.assertEqual(ram_power.W, user_power_value_2)
# Test with process tracking mode to ensure it works across modes
ram = RAM(tracking_mode="process", force_ram_power=user_power_value)
ram_power = ram.total_power()
self.assertEqual(ram_power.W, user_power_value)
# Mock the calculate_ram_power method to verify it's not called when force_ram_power is set
with mock.patch.object(RAM, "_calculate_ram_power") as mock_calc:
ram = RAM(tracking_mode="machine", force_ram_power=user_power_value)
ram_power = ram.total_power()
# Verify the calculation method was not called
mock_calc.assert_not_called()

Xet Storage Details

Size:
19.2 kB
·
Xet hash:
721dd18673dd1477b6b48a5c3d07d4bff300ea294723e4015f36caee81d1d84e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.