| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of LocalService |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
use Exception; |
| 12 |
|
| 13 |
class LocalService |
| 14 |
{ |
| 15 |
public function getNumberOfProcessorCores(): int |
| 16 |
{ |
| 17 |
$ncpu = 1; // Default to 1 processor |
| 18 |
|
| 19 |
// Check for Linux |
| 20 |
if (@is_file('/proc/cpuinfo')) { |
| 21 |
$cpuinfo = file_get_contents('/proc/cpuinfo'); |
| 22 |
preg_match_all('/^processor/m', $cpuinfo, $matches); |
| 23 |
$ncpu = count($matches[0]); |
| 24 |
} |
| 25 |
// Check for Windows |
| 26 |
elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
| 27 |
try { |
| 28 |
if (class_exists('COM')) { |
| 29 |
$wmi = new \COM('winmgmts://./root/cimv2'); |
| 30 |
$processors = $wmi->ExecQuery("SELECT NumberOfLogicalProcessors FROM Win32_Processor"); |
| 31 |
foreach ($processors as $processor) { |
| 32 |
$ncpu = (int)$processor->NumberOfLogicalProcessors; |
| 33 |
} |
| 34 |
} |
| 35 |
} catch (Exception $e) { |
| 36 |
a2wl_error_log( |
| 37 |
"LocalService::getNumberOfProcessors - could not retrieve processor count: " . $e->getMessage() |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $ncpu; |
| 43 |
} |
| 44 |
|
| 45 |
public function getSystemLoadAverage(): array |
| 46 |
{ |
| 47 |
if (!function_exists('sys_getloadavg')) { |
| 48 |
return []; |
| 49 |
} |
| 50 |
|
| 51 |
$load = \sys_getloadavg(); |
| 52 |
|
| 53 |
if ($load === false) { |
| 54 |
return []; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ($load as &$item) { |
| 58 |
$item = number_format((float)$item, 2, '.', ''); |
| 59 |
} |
| 60 |
|
| 61 |
return $load; |
| 62 |
} |
| 63 |
|
| 64 |
public function getMemoryUsageInBytes(): int |
| 65 |
{ |
| 66 |
return memory_get_usage(true); |
| 67 |
} |
| 68 |
|
| 69 |
} |