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 Jul 10, 2024
1 parent e2ac3a8 commit da1e3d9
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +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 {
// check if running in docker
$cgroups = @file_get_contents("/proc/self/cgroup");
if ($cgroups && str_contains($cgroups, "docker")) {
$limit = @file_get_contents('/sys/fs/cgroup/memory/memory.limit_in_bytes');
if ($limit) {
return (int) $limit;
}
} 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
}
// 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;
}
$memAvail = get_system_memory();
if ($memAvail < 10 * 1024 * 1024 * 1024) {
die('skip Reason: Insufficient RAM (should be 10GB, is '.$memAvail.' bytes)');
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 da1e3d9

Please sign in to comment.