-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
standardize memory check and support docker environments
- Loading branch information
1 parent
b282dd7
commit 7ef1834
Showing
5 changed files
with
66 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
function has_enough_memory(int|float $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) >= $bytes; | ||
} | ||
} elseif (PHP_OS == 'FreeBSD') { | ||
$lines = explode("\n", `sysctl -a`); | ||
$infos = array(); | ||
foreach ($lines as $line) { | ||
if (!$line) { | ||
continue; | ||
} | ||
$tmp = explode(":", $line); | ||
if (count($tmp) < 2) { | ||
continue; | ||
} | ||
$index = strtolower($tmp[0]); | ||
$value = trim($tmp[1], " "); | ||
$infos[$index] = $value; | ||
} | ||
$freeMemory = ($infos['vm.stats.vm.v_inactive_count'] * $infos['hw.pagesize']) | ||
+ ($infos['vm.stats.vm.v_cache_count'] * $infos['hw.pagesize']) | ||
+ ($infos['vm.stats.vm.v_free_count'] * $infos['hw.pagesize']); | ||
return $freeMemory >= $bytes; | ||
} else { // Linux | ||
// 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; | ||
} |