Skip to content

Commit

Permalink
Merge pull request #21 from utopia-php/feat-cpu-usage
Browse files Browse the repository at this point in the history
Feat: getCPUUsage
  • Loading branch information
christyjacob4 authored Nov 7, 2022
2 parents 183aa96 + d42af6d commit 289c432
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
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

0 comments on commit 289c432

Please sign in to comment.