Skip to content

Commit da1e3d9

Browse files
fix cgroup v1 vs v2
1 parent e2ac3a8 commit da1e3d9

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +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-
// check if running in docker
22-
$cgroups = @file_get_contents("/proc/self/cgroup");
23-
if ($cgroups && str_contains($cgroups, "docker")) {
24-
$limit = @file_get_contents('/sys/fs/cgroup/memory/memory.limit_in_bytes');
25-
if ($limit) {
26-
return (int) $limit;
27-
}
28-
} else {
29-
// Unix/Linux-based memory check
30-
$memInfo = @file_get_contents("/proc/meminfo");
31-
if ($memInfo) {
32-
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
33-
return $matches[1] * 1024; // Convert to bytes
34-
}
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+
}
31+
// Unix/Linux-based memory check
32+
$memInfo = @file_get_contents("/proc/meminfo");
33+
if ($memInfo) {
34+
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
35+
return ($matches[1] * 1024) >= $bytes; // Convert to bytes
3536
}
3637
}
3738
return false;
3839
}
39-
$memAvail = get_system_memory();
40-
if ($memAvail < 10 * 1024 * 1024 * 1024) {
41-
die('skip Reason: Insufficient RAM (should be 10GB, is '.$memAvail.' bytes)');
40+
if(has_enough_memory(10 * 1024 * 1024 * 1024)) { // 10GB
41+
die('skip Reason: Insufficient RAM (should be 10GB)');
4242
}
4343
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
4444
$tmpfileh = fopen($tmpfile, "wb");

0 commit comments

Comments
 (0)