From 962218e9995f23013375a6a035a4ae412c266cc5 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Wed, 10 Jul 2024 12:29:58 +0200 Subject: [PATCH] fix cgroup v1 vs v2 --- ...le_get_contents_file_put_contents_5gb.phpt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt b/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt index 854996a6481e7..e9226c72c5c06 100644 --- a/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt +++ b/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt @@ -9,26 +9,36 @@ if (PHP_INT_SIZE < 5) { if (getenv('SKIP_SLOW_TESTS')) { die('skip slow test'); } -function get_system_memory(): int|float|false +function has_enough_memory(int $bytes): bool { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // Windows-based memory check @exec('wmic OS get FreePhysicalMemory', $output); if (isset($output[1])) { - return ((int)trim($output[1])) * 1024; + return (((int)trim($output[1])) * 1024) >= $bytes; } } else { + // cgroup v1 + $limit = @file_get_contents('/sys/fs/cgroup/memory/memory.limit_in_bytes'); + if ($limit && (int)$limit < $bytes) { + return false; + } + // cgroup v2 + $limit = @file_get_contents('/sys/fs/cgroup/memory.max'); + if ($limit && !str_contains($limit, 'max') && (int)$limit < $bytes) { + return false; + } // Unix/Linux-based memory check $memInfo = @file_get_contents("/proc/meminfo"); if ($memInfo) { preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches); - return $matches[1] * 1024; // Convert to bytes + return ($matches[1] * 1024) >= $bytes; // Convert to bytes } } return false; } -if (get_system_memory() < 10 * 1024 * 1024 * 1024) { - die('skip Reason: Insufficient RAM (less than 10GB)'); +if(!has_enough_memory(10 * 1024 * 1024 * 1024)) { // 10GB + die('skip Reason: Insufficient RAM (should be 10GB)'); } $tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin"; $tmpfileh = fopen($tmpfile, "wb");