Skip to content

Commit 962218e

Browse files
fix cgroup v1 vs v2
1 parent b282dd7 commit 962218e

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,36 @@ 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
12+
function has_enough_memory(int $bytes): bool
1313
{
1414
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
1515
// Windows-based memory check
1616
@exec('wmic OS get FreePhysicalMemory', $output);
1717
if (isset($output[1])) {
18-
return ((int)trim($output[1])) * 1024;
18+
return (((int)trim($output[1])) * 1024) >= $bytes;
1919
}
2020
} else {
21+
// cgroup v1
22+
$limit = @file_get_contents('/sys/fs/cgroup/memory/memory.limit_in_bytes');
23+
if ($limit && (int)$limit < $bytes) {
24+
return false;
25+
}
26+
// cgroup v2
27+
$limit = @file_get_contents('/sys/fs/cgroup/memory.max');
28+
if ($limit && !str_contains($limit, 'max') && (int)$limit < $bytes) {
29+
return false;
30+
}
2131
// Unix/Linux-based memory check
2232
$memInfo = @file_get_contents("/proc/meminfo");
2333
if ($memInfo) {
2434
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
25-
return $matches[1] * 1024; // Convert to bytes
35+
return ($matches[1] * 1024) >= $bytes; // Convert to bytes
2636
}
2737
}
2838
return false;
2939
}
30-
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
31-
die('skip Reason: Insufficient RAM (less than 10GB)');
40+
if(!has_enough_memory(10 * 1024 * 1024 * 1024)) { // 10GB
41+
die('skip Reason: Insufficient RAM (should be 10GB)');
3242
}
3343
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
3444
$tmpfileh = fopen($tmpfile, "wb");

0 commit comments

Comments
 (0)