Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ echo System::isX86(); // bool
Utopia Framework requires PHP 7.4 or later. We recommend using the latest PHP version whenever possible.

## Supported Methods
| | getCPUCores | getCPUUtilisation | getMemoryTotal | getMemoryFree | getDiskTotal | getDiskFree | getIOUsage | getNetworkUsage |
| | getCPUCores | getCPUUsage | getMemoryTotal | getMemoryFree | getDiskTotal | getDiskFree | getIOUsage | getNetworkUsage |
|---------|-------------|-------------------|----------------|---------------|--------------|-------------|------------|-----------------|
| Windows | ✅ | | | | ✅ | ✅ | | |
| MacOS | ✅ | | ✅ | ✅ | ✅ | ✅ | | |
Expand Down
46 changes: 18 additions & 28 deletions src/System/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,47 +273,37 @@ private static function getProcStatData(): array
}

/**
* Gets the current usage of a core as a percentage. Passing 0 will return the usage of all cores combined.
* Get percentage CPU usage (between 0 and 100)
* Reference for formula: https://stackoverflow.com/a/23376195/17300412
*
* @param int $core
* @return int
* @param int $duration
* @return float
*
* @throws Exception
*/
public static function getCPUUtilisation(int $id = 0): int
public static function getCPUUsage(int $duration = 1): float
{
switch (self::getOS()) {
case 'Linux':
$cpuNow = self::getProcStatData();
$i = 0;

$data = [];

foreach ($cpuNow as $cpu) {
// Check if this is the total CPU
$cpuTotal = $cpu['user'] + $cpu['nice'] + $cpu['system'] + $cpu['idle'] + $cpu['iowait'] + $cpu['irq'] + $cpu['softirq'] + $cpu['steal'];

$cpuIdle = $cpu['idle'];
$startCpu = self::getProcStatData()['total'];
\sleep($duration);
$endCpu = self::getProcStatData()['total'];

$idleDelta = $cpuIdle - (isset($lastData[$i]) ? $lastData[$i]['idle'] : 0);
$prevIdle = $startCpu['idle'] + $startCpu['iowait'];
$idle = $endCpu['idle'] + $endCpu['iowait'];

$totalDelta = $cpuTotal - (isset($lastData[$i]) ? $lastData[$i]['total'] : 0);
$prevNonIdle = $startCpu['user'] + $startCpu['nice'] + $startCpu['system'] + $startCpu['irq'] + $startCpu['softirq'] + $startCpu['steal'];
$nonIdle = $endCpu['user'] + $endCpu['nice'] + $endCpu['system'] + $endCpu['irq'] + $endCpu['softirq'] + $endCpu['steal'];

$lastData[$i]['total'] = $cpuTotal;
$lastData[$i]['idle'] = $cpuIdle;
$prevTotal = $prevIdle + $prevNonIdle;
$total = $idle + $nonIdle;

$result = (1.0 - ($idleDelta / $totalDelta)) * 100;
$totalDiff = $total - $prevTotal;
$idleDiff = $idle - $prevIdle;

$data[$i] = $result;
$percentage = ($totalDiff - $idleDiff) / $totalDiff;

$i++;
}

if ($id === 0) {
return intval(array_sum($data));
} else {
return $data[$id];
}
return $percentage * 100;
default:
throw new Exception(self::getOS().' not supported.');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/System/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public function testGetDiskFree()
}

// Methods only implemented for Linux
public function testGetCPUUtilisation()
public function testGetCPUUsage()
{
if (System::getOS() === 'Linux') {
$this->assertIsInt(System::getCPUUtilisation());
$this->assertIsNumeric(System::getCPUUsage(5));
} else {
$this->expectException('Exception');
System::getCPUUtilisation();
System::getCPUUsage(5);
}
}

Expand Down