22declare(strict_types=1);
31use
function file_get_contents;
32use
function function_exists;
36use
function memory_get_usage;
37use
function posix_kill;
38use
function preg_match;
39use
function proc_close;
40use
function proc_open;
41use
function str_starts_with;
42use
function stream_get_contents;
47 private function __construct(){
56 $reserved = memory_get_usage();
60 $status = @file_get_contents(
"/proc/self/status");
61 if($status ===
false)
throw new AssumptionFailedError(
"/proc/self/status should always be accessible");
64 if(preg_match(
"/VmRSS:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
65 $VmRSS = ((int) $matches[1]) * 1024;
68 if(preg_match(
"/VmSize:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
69 $VmSize = ((int) $matches[1]) * 1024;
76 $VmRSS = memory_get_usage();
80 $VmSize = memory_get_usage(
true);
83 return [$reserved, $VmRSS, $VmSize];
86 public static function getMemoryUsage() : int{
98 $mappings = @file(
"/proc/self/maps");
99 if($mappings ===
false)
throw new AssumptionFailedError(
"/proc/self/maps should always be accessible");
100 foreach($mappings as $line){
101 if(preg_match(
"#([a-z0-9]+)\\-([a-z0-9]+) [rwxp\\-]{4} [a-z0-9]+ [^\\[]*\\[([a-zA-z0-9]+)\\]#", trim($line), $matches) > 0){
102 if(str_starts_with($matches[3],
"heap")){
103 $heap += (int) hexdec($matches[2]) - (int) hexdec($matches[1]);
104 }elseif(str_starts_with($matches[3],
"stack")){
105 $stack += (int) hexdec($matches[2]) - (int) hexdec($matches[1]);
111 return [$heap, $stack];
114 public static function getThreadCount() : int{
116 $status = @file_get_contents(
"/proc/self/status");
117 if($status ===
false)
throw new AssumptionFailedError(
"/proc/self/status should always be accessible");
118 if(preg_match(
"/Threads:[ \t]+([0-9]+)/", $status, $matches) > 0){
119 return (
int) $matches[1];
125 return Thread::getRunningCount() + 1;
131 public static function kill(
int $pid,
bool $subprocesses =
false) : void{
134 $logger->syncFlushBuffer();
136 switch(Utils::getOS()){
137 case Utils::OS_WINDOWS:
138 exec(
"taskkill.exe /F " . ($subprocesses ?
"/T " :
"") .
"/PID $pid > NUL 2> NUL");
140 case Utils::OS_MACOS:
141 case Utils::OS_LINUX:
146 if(function_exists(
"posix_kill")){
149 exec(
"kill -9 $pid > /dev/null 2>&1");
163 public static function execute(
string $command, ?
string &$stdout =
null, ?
string &$stderr =
null) : int{
164 $process = proc_open($command, [
170 if($process ===
false){
171 $stderr =
"Failed to open process";
177 $stdout = stream_get_contents($pipes[1]);
178 $stderr = stream_get_contents($pipes[2]);
180 foreach($pipes as $p){
184 return proc_close($process);
187 public static function pid() : int{
188 $result = getmypid();
189 if($result ===
false){
190 throw new \LogicException(
"getmypid() doesn't work on this platform");
195 public static function uid() : int{
196 $result = getmyuid();
197 if($result ===
false){
198 throw new \LogicException(
"getmyuid() doesn't work on this platform");
static kill(int $pid, bool $subprocesses=false)
static execute(string $command, ?string &$stdout=null, ?string &$stderr=null)
static getRealMemoryUsage()
static getAdvancedMemoryUsage()
static getOS(bool $recalculate=false)