Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4c7421

Browse files
standardize memory check and support docker environments
1 parent b282dd7 commit c4c7421

File tree

5 files changed

+66
-96
lines changed

5 files changed

+66
-96
lines changed

‎Zend/tests/bug55509.phpt‎

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,10 @@ if ($zend_mm_enabled === "0") {
1212
}
1313

1414
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
15-
// check the available memory
16-
if (PHP_OS == 'Linux') {
17-
$lines = file('/proc/meminfo');
18-
$infos = array();
19-
foreach ($lines as $line) {
20-
$tmp = explode(":", $line);
21-
$index = strtolower($tmp[0]);
22-
$value = (int)ltrim($tmp[1], "")*1024;
23-
$infos[$index] = $value;
24-
}
25-
$freeMemory = $infos['memfree']+$infos['buffers']+$infos['cached'];
26-
if ($freeMemory < 2100*1024*1024) {
27-
die('skip Not enough memory.');
28-
}
29-
}
30-
elseif (PHP_OS == 'FreeBSD') {
31-
$lines = explode("\n",`sysctl -a`);
32-
$infos = array();
33-
foreach ($lines as $line) {
34-
if (!$line){
35-
continue;
36-
}
37-
$tmp = explode(":", $line);
38-
if (count($tmp) < 2) {
39-
continue;
40-
}
41-
$index = strtolower($tmp[0]);
42-
$value = trim($tmp[1], "");
43-
$infos[$index] = $value;
44-
}
45-
$freeMemory = ($infos['vm.stats.vm.v_inactive_count']*$infos['hw.pagesize'])
46-
+($infos['vm.stats.vm.v_cache_count']*$infos['hw.pagesize'])
47-
+($infos['vm.stats.vm.v_free_count']*$infos['hw.pagesize']);
48-
if ($freeMemory < 2100*1024*1024) {
49-
die('skip Not enough memory.');
50-
}
51-
} elseif (PHP_OS == "WINNT") {
52-
$s = trim(shell_exec("wmic OS get FreeVirtualMemory /Value 2>nul"));
53-
$freeMemory = explode('=', $s)[1]*1;
5415

55-
if ($freeMemory < 2.1*1024*1024) {
56-
die('skip Not enough memory.');
57-
}
16+
include(__DIR__.'/../../tests/utils.php');
17+
if(!has_enough_memory(2.1 * 1024 * 1024 * 1024)) {
18+
die('skip Reason: Insufficient RAM (should be 2.1GB)');
5819
}
5920
?>
6021
--INI--

‎ext/gmp/tests/gmp_setbit_long.phpt‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ gmp_setbit() with large index
33
--EXTENSIONS--
44
gmp
55
--SKIPIF--
6-
<?php if (getenv("TRAVIS") === "true") die("skip not suitable for Travis-CI"); ?>
7-
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
8-
<?php if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); ?>
96
<?php
10-
/* This test requires about 8G RAM which likely not to be present on an arbitrary CI. */
11-
if (!file_exists("/proc/meminfo")) {
12-
die("skip cannot determine free memory amount.");
13-
}
14-
$s = file_get_contents("/proc/meminfo");
15-
$free = 0;
16-
if (preg_match(",MemFree:\s+(\d+)\s+kB,", $s, $m)) {
17-
/* Got amount in kb. */
18-
$free = $m[1]/1024/1024;
19-
}
20-
if ($free < 8) {
21-
die("skip not enough free RAM.");
22-
}
7+
if (getenv("TRAVIS") === "true") die("skip not suitable for Travis-CI");
8+
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
9+
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
10+
/* This test requires about 8G RAM which likely not to be present on an arbitrary CI. */
11+
include(__DIR__.'/../../../tests/utils.php');
12+
if(!has_enough_memory(8 * 1024 * 1024 * 1024)) {
13+
die('skip Reason: Insufficient RAM (should be 8GB)');
14+
}
2315
?>
2416
--FILE--
2517
<?php

‎ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,9 @@ if (PHP_INT_SIZE < 5) {
99
if (getenv('SKIP_SLOW_TESTS')) {
1010
die('skip slow test');
1111
}
12-
function get_system_memory(): int|float|false
13-
{
14-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
15-
// Windows-based memory check
16-
@exec('wmic OS get FreePhysicalMemory', $output);
17-
if (isset($output[1])) {
18-
return ((int)trim($output[1])) * 1024;
19-
}
20-
} else {
21-
// Unix/Linux-based memory check
22-
$memInfo = @file_get_contents("/proc/meminfo");
23-
if ($memInfo) {
24-
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
25-
return $matches[1] * 1024; // Convert to bytes
26-
}
27-
}
28-
return false;
29-
}
30-
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
31-
die('skip Reason: Insufficient RAM (less than 10GB)');
12+
include(__DIR__.'/../../../../tests/utils.php');
13+
if(!has_enough_memory(10 * 1024 * 1024 * 1024)) { // 10GB
14+
die('skip Reason: Insufficient RAM (should be 10GB)');
3215
}
3316
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
3417
$tmpfileh = fopen($tmpfile, "wb");

‎sapi/cli/tests/upload_2G.phpt‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,9 @@ if (disk_free_space(sys_get_temp_dir()) < 2300000000) {
1212
die("skip need more than 2.15G of free disk space for the uploaded file");
1313
}
1414

15-
if (!file_exists('/proc/meminfo')) {
16-
die('skip Cannot check free RAM from /proc/meminfo on this platform');
17-
}
18-
19-
$free_ram = 0;
20-
if ($f = fopen("/proc/meminfo","r")) {
21-
while (!feof($f)) {
22-
if (preg_match('/MemFree[^\d]*(\d+)/i', fgets($f), $m)) {
23-
$free_ram = max($free_ram, $m[1]/1024/1024);
24-
if ($free_ram > 3) {
25-
$enough_free_ram = true;
26-
}
27-
}
28-
}
29-
}
30-
31-
if (empty($enough_free_ram)) {
32-
die(sprintf("skip need +3G free RAM, but only %01.2f available", $free_ram));
15+
include(__DIR__.'/../../../tests/utils.php');
16+
if(!has_enough_memory(3 * 1024 * 1024 * 1024)) { // 3 GB
17+
die('skip Reason: Insufficient RAM (should be 3GB)');
3318
}
3419

3520
if (getenv('TRAVIS')) {

‎tests/utils.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
function has_enough_memory(int|float $bytes): bool
4+
{
5+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
6+
// Windows-based memory check
7+
@exec('wmic OS get FreePhysicalMemory', $output);
8+
if (isset($output[1])) {
9+
return (((int)trim($output[1])) * 1024) >= $bytes;
10+
}
11+
} elseif (PHP_OS == 'FreeBSD') {
12+
$lines = explode("\n", `sysctl -a`);
13+
$infos = array();
14+
foreach ($lines as $line) {
15+
if (!$line) {
16+
continue;
17+
}
18+
$tmp = explode(":", $line);
19+
if (count($tmp) < 2) {
20+
continue;
21+
}
22+
$index = strtolower($tmp[0]);
23+
$value = trim($tmp[1], "");
24+
$infos[$index] = $value;
25+
}
26+
$freeMemory = ($infos['vm.stats.vm.v_inactive_count'] * $infos['hw.pagesize'])
27+
+ ($infos['vm.stats.vm.v_cache_count'] * $infos['hw.pagesize'])
28+
+ ($infos['vm.stats.vm.v_free_count'] * $infos['hw.pagesize']);
29+
return $freeMemory >= $bytes;
30+
} else { // Linux
31+
// cgroup v1
32+
$limit = @file_get_contents('/sys/fs/cgroup/memory/memory.limit_in_bytes');
33+
if ($limit && (int)$limit < $bytes) {
34+
return false;
35+
}
36+
// cgroup v2
37+
$limit = @file_get_contents('/sys/fs/cgroup/memory.max');
38+
if ($limit && !str_contains($limit, 'max') && (int)$limit < $bytes) {
39+
return false;
40+
}
41+
// Unix/Linux-based memory check
42+
$memInfo = @file_get_contents("/proc/meminfo");
43+
if ($memInfo) {
44+
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
45+
return ($matches[1] * 1024) >= $bytes; // Convert to bytes
46+
}
47+
}
48+
return false;
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /