From eec45d854e9d28d42343a85dc28465e3a418c251 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Wed, 10 Jul 2024 12:29:58 +0200 Subject: [PATCH 1/2] standardize memory check and support docker environments --- Zend/tests/bug55509.phpt | 45 ++--------------- ext/gmp/tests/gmp_setbit_long.phpt | 24 +++------ ...le_get_contents_file_put_contents_5gb.phpt | 23 ++------- sapi/cli/tests/upload_2G.phpt | 21 ++------ tests/utils.php | 49 +++++++++++++++++++ 5 files changed, 66 insertions(+), 96 deletions(-) create mode 100644 tests/utils.php diff --git a/Zend/tests/bug55509.phpt b/Zend/tests/bug55509.phpt index 1ae5b04f8a0df..cb2477094b809 100644 --- a/Zend/tests/bug55509.phpt +++ b/Zend/tests/bug55509.phpt @@ -12,49 +12,10 @@ if ($zend_mm_enabled === "0") { } if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); -// check the available memory -if (PHP_OS == 'Linux') { - $lines = file('/proc/meminfo'); - $infos = array(); - foreach ($lines as $line) { - $tmp = explode(":", $line); - $index = strtolower($tmp[0]); - $value = (int)ltrim($tmp[1], " ")*1024; - $infos[$index] = $value; - } - $freeMemory = $infos['memfree']+$infos['buffers']+$infos['cached']; - if ($freeMemory < 2100*1024*1024) { - die('skip Not enough memory.'); - } -} -elseif (PHP_OS == 'FreeBSD') { - $lines = explode("\n",`sysctl -a`); - $infos = array(); - foreach ($lines as $line) { - if (!$line){ - continue; - } - $tmp = explode(":", $line); - if (count($tmp) < 2) { - continue; - } - $index = strtolower($tmp[0]); - $value = trim($tmp[1], " "); - $infos[$index] = $value; - } - $freeMemory = ($infos['vm.stats.vm.v_inactive_count']*$infos['hw.pagesize']) - +($infos['vm.stats.vm.v_cache_count']*$infos['hw.pagesize']) - +($infos['vm.stats.vm.v_free_count']*$infos['hw.pagesize']); - if ($freeMemory < 2100*1024*1024) { - die('skip Not enough memory.'); - } -} elseif (PHP_OS == "WINNT") { - $s = trim(shell_exec("wmic OS get FreeVirtualMemory /Value 2>nul")); - $freeMemory = explode('=', $s)[1]*1; - if ($freeMemory < 2.1*1024*1024) { - die('skip Not enough memory.'); - } +include(__DIR__.'/../../tests/utils.php'); +if (!has_enough_memory(2.1 * 1024 * 1024 * 1024)) { + die('skip Reason: Insufficient RAM (should be 2.1GB)'); } ?> --INI-- diff --git a/ext/gmp/tests/gmp_setbit_long.phpt b/ext/gmp/tests/gmp_setbit_long.phpt index 08927e9a2ee7f..08002a1bbe5b4 100644 --- a/ext/gmp/tests/gmp_setbit_long.phpt +++ b/ext/gmp/tests/gmp_setbit_long.phpt @@ -3,23 +3,15 @@ gmp_setbit() with large index --EXTENSIONS-- gmp --SKIPIF-- - - - --FILE-- 3) { - $enough_free_ram = true; - } - } - } -} - -if (empty($enough_free_ram)) { - die(sprintf("skip need +3G free RAM, but only %01.2f available", $free_ram)); +include(__DIR__.'/../../../tests/utils.php'); +if(!has_enough_memory(3 * 1024 * 1024 * 1024)) { // 3 GB + die('skip Reason: Insufficient RAM (should be 3GB)'); } if (getenv('TRAVIS')) { diff --git a/tests/utils.php b/tests/utils.php new file mode 100644 index 0000000000000..690000f0bc885 --- /dev/null +++ b/tests/utils.php @@ -0,0 +1,49 @@ += $bytes; + } + } elseif (PHP_OS == 'FreeBSD') { + $lines = explode("\n", `sysctl -a`); + $infos = array(); + foreach ($lines as $line) { + if (!$line) { + continue; + } + $tmp = explode(":", $line); + if (count($tmp) < 2) { + continue; + } + $index = strtolower($tmp[0]); + $value = trim($tmp[1], " "); + $infos[$index] = $value; + } + $freeMemory = ($infos['vm.stats.vm.v_inactive_count'] * $infos['hw.pagesize']) + + ($infos['vm.stats.vm.v_cache_count'] * $infos['hw.pagesize']) + + ($infos['vm.stats.vm.v_free_count'] * $infos['hw.pagesize']); + return $freeMemory >= $bytes; + } else { // Linux + // 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) >= $bytes; // Convert to bytes + } + } + return false; +} From 3fe9da443184231abbc531953260073bf35f83ee Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Mon, 26 Aug 2024 09:40:30 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Jakub Zelenka --- ext/gmp/tests/gmp_setbit_long.phpt | 2 +- .../tests/file/file_get_contents_file_put_contents_5gb.phpt | 2 +- sapi/cli/tests/upload_2G.phpt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/gmp/tests/gmp_setbit_long.phpt b/ext/gmp/tests/gmp_setbit_long.phpt index 08002a1bbe5b4..9247de6bf3071 100644 --- a/ext/gmp/tests/gmp_setbit_long.phpt +++ b/ext/gmp/tests/gmp_setbit_long.phpt @@ -9,7 +9,7 @@ if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); /* This test requires about 8G RAM which likely not to be present on an arbitrary CI. */ include(__DIR__.'/../../../tests/utils.php'); -if(!has_enough_memory(8 * 1024 * 1024 * 1024)) { +if (!has_enough_memory(8 * 1024 * 1024 * 1024)) { die('skip Reason: Insufficient RAM (should be 8GB)'); } ?> 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 90e82701f35f2..cbb7de6d24a01 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 @@ -10,7 +10,7 @@ if (getenv('SKIP_SLOW_TESTS')) { die('skip slow test'); } include(__DIR__.'/../../../../tests/utils.php'); -if(!has_enough_memory(10 * 1024 * 1024 * 1024)) { // 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"; diff --git a/sapi/cli/tests/upload_2G.phpt b/sapi/cli/tests/upload_2G.phpt index f5c9138a77b2d..819ba6a5cec60 100644 --- a/sapi/cli/tests/upload_2G.phpt +++ b/sapi/cli/tests/upload_2G.phpt @@ -13,7 +13,7 @@ if (disk_free_space(sys_get_temp_dir()) < 2300000000) { } include(__DIR__.'/../../../tests/utils.php'); -if(!has_enough_memory(3 * 1024 * 1024 * 1024)) { // 3 GB +if (!has_enough_memory(3 * 1024 * 1024 * 1024)) { // 3 GB die('skip Reason: Insufficient RAM (should be 3GB)'); }