From 73521f1b3bf4fc015715d655db506275dee80be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Oct 2022 09:33:41 +0000 Subject: [PATCH 1/5] Implement getCPUUsage --- src/System/System.php | 36 ++++++++++++++++++++++++++++++++++++ tests/System/SystemTest.php | 11 +++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/System/System.php b/src/System/System.php index 66455dd..2236f30 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -319,6 +319,42 @@ public static function getCPUUtilisation(int $id = 0): int } } + /** + * Get percentage CPU usage (between 0 and 100) + * + * @param int $duration + * @return float + * + * @throws Exception + */ + public static function getCPUUsage(int $duration = 1): float + { + switch (self::getOS()) { + case 'Linux': + $startCpu = self::getProcStatData()['total']; + \sleep($duration); + $endCpu = self::getProcStatData()['total']; + + $prevIdle = $startCpu['idle'] + $startCpu['iowait']; + $idle = $endCpu['idle'] + $endCpu['iowait']; + + $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']; + + $prevTotal = $prevIdle + $prevNonIdle; + $total = $idle + $nonIdle; + + $totalDiff = $total - $prevTotal; + $idleDiff = $idle - $prevIdle; + + $percentage = ($totalDiff - $idleDiff) / $totalDiff; + + return $percentage * 100; + default: + throw new Exception(self::getOS().' not supported.'); + } + } + /** * Returns the total amount of RAM available on the system as Megabytes. * diff --git a/tests/System/SystemTest.php b/tests/System/SystemTest.php index 3566d91..f54b0c5 100644 --- a/tests/System/SystemTest.php +++ b/tests/System/SystemTest.php @@ -69,6 +69,17 @@ public function testGetCPUUtilisation() } } + // Methods only implemented for Linux + public function testGetCPUUsage() + { + if (System::getOS() === 'Linux') { + $this->assertIsNumeric(System::getCPUUsage(5)); + } else { + $this->expectException('Exception'); + System::getCPUUtilisation(); + } + } + public function testGetMemoryTotal() { if (System::getOS() === 'Linux') { From b184bc4f4d4c45150c621d674cfd9dc37dff49dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Oct 2022 09:36:13 +0000 Subject: [PATCH 2/5] Linter fix --- src/System/System.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System/System.php b/src/System/System.php index 2236f30..66224ea 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -322,7 +322,7 @@ public static function getCPUUtilisation(int $id = 0): int /** * Get percentage CPU usage (between 0 and 100) * - * @param int $duration + * @param int $duration * @return float * * @throws Exception From 9847eb53d5fc6ddc008909cacdd39509e9966eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Oct 2022 06:19:09 +0000 Subject: [PATCH 3/5] Improve docs --- src/System/System.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System/System.php b/src/System/System.php index 66224ea..8fb967b 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -321,6 +321,7 @@ public static function getCPUUtilisation(int $id = 0): int /** * Get percentage CPU usage (between 0 and 100) + * Refference for formula: https://stackoverflow.com/a/23376195/17300412 * * @param int $duration * @return float From 70b1154f25fade657684532b92a4c2f4127a2171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 7 Nov 2022 12:47:01 +0100 Subject: [PATCH 4/5] Update src/System/System.php Co-authored-by: Christy Jacob --- src/System/System.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System/System.php b/src/System/System.php index 8fb967b..808096f 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -321,7 +321,7 @@ public static function getCPUUtilisation(int $id = 0): int /** * Get percentage CPU usage (between 0 and 100) - * Refference for formula: https://stackoverflow.com/a/23376195/17300412 + * Reference for formula: https://stackoverflow.com/a/23376195/17300412 * * @param int $duration * @return float From d42af6dea171c0bff3e0bac94852e1fd2e04d78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 7 Nov 2022 11:55:20 +0000 Subject: [PATCH 5/5] Remove getCPUUtilisation --- README.md | 2 +- src/System/System.php | 47 ------------------------------------- tests/System/SystemTest.php | 13 +--------- 3 files changed, 2 insertions(+), 60 deletions(-) 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 808096f..445aec3 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -272,53 +272,6 @@ private static function getProcStatData(): array return $data; } - /** - * Gets the current usage of a core as a percentage. Passing 0 will return the usage of all cores combined. - * - * @param int $core - * @return int - * - * @throws Exception - */ - public static function getCPUUtilisation(int $id = 0): int - { - 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']; - - $idleDelta = $cpuIdle - (isset($lastData[$i]) ? $lastData[$i]['idle'] : 0); - - $totalDelta = $cpuTotal - (isset($lastData[$i]) ? $lastData[$i]['total'] : 0); - - $lastData[$i]['total'] = $cpuTotal; - $lastData[$i]['idle'] = $cpuIdle; - - $result = (1.0 - ($idleDelta / $totalDelta)) * 100; - - $data[$i] = $result; - - $i++; - } - - if ($id === 0) { - return intval(array_sum($data)); - } else { - return $data[$id]; - } - default: - throw new Exception(self::getOS().' not supported.'); - } - } - /** * Get percentage CPU usage (between 0 and 100) * Reference for formula: https://stackoverflow.com/a/23376195/17300412 diff --git a/tests/System/SystemTest.php b/tests/System/SystemTest.php index f54b0c5..15f0959 100644 --- a/tests/System/SystemTest.php +++ b/tests/System/SystemTest.php @@ -58,17 +58,6 @@ public function testGetDiskFree() $this->assertIsInt(System::getDiskFree()); } - // Methods only implemented for Linux - public function testGetCPUUtilisation() - { - if (System::getOS() === 'Linux') { - $this->assertIsInt(System::getCPUUtilisation()); - } else { - $this->expectException('Exception'); - System::getCPUUtilisation(); - } - } - // Methods only implemented for Linux public function testGetCPUUsage() { @@ -76,7 +65,7 @@ public function testGetCPUUsage() $this->assertIsNumeric(System::getCPUUsage(5)); } else { $this->expectException('Exception'); - System::getCPUUtilisation(); + System::getCPUUsage(5); } }