Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust memory limit check in docker env #14895

Open
wants to merge 2 commits into
base: PHP-8.2
Choose a base branch
from
Open
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
45 changes: 3 additions & 42 deletions Zend/tests/bug55509.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand Down
24 changes: 8 additions & 16 deletions ext/gmp/tests/gmp_setbit_long.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ gmp_setbit() with large index
--EXTENSIONS--
gmp
--SKIPIF--
<?php if (getenv("TRAVIS") === "true") die("skip not suitable for Travis-CI"); ?>
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
<?php if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); ?>
<?php
/* This test requires about 8G RAM which likely not to be present on an arbitrary CI. */
if (!file_exists("/proc/meminfo")) {
die("skip cannot determine free memory amount.");
}
$s = file_get_contents("/proc/meminfo");
$free = 0;
if (preg_match(",MemFree:\s+(\d+)\s+kB,", $s, $m)) {
/* Got amount in kb. */
$free = $m[1]/1024/1024;
}
if ($free < 8) {
die("skip not enough free RAM.");
}
if (getenv("TRAVIS") === "true") die("skip not suitable for Travis-CI");
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)) {
die('skip Reason: Insufficient RAM (should be 8GB)');
}
?>
--FILE--
<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,9 @@ if (PHP_INT_SIZE < 5) {
if (getenv('SKIP_SLOW_TESTS')) {
die('skip slow test');
}
function get_system_memory(): int|float|false
{
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;
}
} else {
// 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 false;
}
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
die('skip Reason: Insufficient RAM (less than 10GB)');
include(__DIR__.'/../../../../tests/utils.php');
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");
Expand Down
21 changes: 3 additions & 18 deletions sapi/cli/tests/upload_2G.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,9 @@ if (disk_free_space(sys_get_temp_dir()) < 2300000000) {
die("skip need more than 2.15G of free disk space for the uploaded file");
}

if (!file_exists('/proc/meminfo')) {
die('skip Cannot check free RAM from /proc/meminfo on this platform');
}

$free_ram = 0;
if ($f = fopen("/proc/meminfo","r")) {
while (!feof($f)) {
if (preg_match('/MemFree[^\d]*(\d+)/i', fgets($f), $m)) {
$free_ram = max($free_ram, $m[1]/1024/1024);
if ($free_ram > 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')) {
Expand Down
49 changes: 49 additions & 0 deletions tests/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

function has_enough_memory(int|float $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) >= $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;
}
Loading