Skip to content

Commit

Permalink
fix cgroup v1 vs v2
Browse files Browse the repository at this point in the history
  • Loading branch information
realFlowControl committed Aug 1, 2024
1 parent b282dd7 commit 962218e
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,36 @@ if (PHP_INT_SIZE < 5) {
if (getenv('SKIP_SLOW_TESTS')) {
die('skip slow test');
}
function get_system_memory(): int|float|false
function has_enough_memory(int $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;
return (((int)trim($output[1])) * 1024) >= $bytes;
}
} else {
// 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; // Convert to bytes
return ($matches[1] * 1024) >= $bytes; // Convert to bytes
}
}
return false;
}
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
die('skip Reason: Insufficient RAM (less than 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";
$tmpfileh = fopen($tmpfile, "wb");
Expand Down

0 comments on commit 962218e

Please sign in to comment.