diff --git a/README.md b/README.md index 10a27af..f32c9ce 100644 --- a/README.md +++ b/README.md @@ -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 | ✅ | | ✅ | ✅ | ✅ | ✅ | | | diff --git a/src/System/System.php b/src/System/System.php index 66455dd..445aec3 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -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.'); } diff --git a/tests/System/SystemTest.php b/tests/System/SystemTest.php index 3566d91..15f0959 100644 --- a/tests/System/SystemTest.php +++ b/tests/System/SystemTest.php @@ -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); } }