From a444f286bbeeafb4b6c62f10f56790f4309fc0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 4 Dec 2022 20:24:18 +0100 Subject: [PATCH 1/3] Integrate FidryCpuCoreCounter --- composer.json | 1 + .../Internal/Analyzer/ProjectAnalyzer.php | 48 ++++--------------- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/composer.json b/composer.json index 420faad7441..9f16336aecb 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "dnoegel/php-xdg-base-dir": "^0.1.1", "felixfbecker/advanced-json-rpc": "^3.1", "felixfbecker/language-server-protocol": "^1.5.2", + "fidry/cpu-core-counter": "^0.2.0", "netresearch/jsonmapper": "^1.0 || ^2.0 || ^3.0 || ^4.0", "nikic/php-parser": "^4.13", "openlss/lib-array2xml": "^1.0", diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 906961ec8b5..923d10c59a5 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -3,6 +3,8 @@ namespace Psalm\Internal\Analyzer; use Amp\Loop; +use Fidry\CpuCounter\CpuCoreCounter; +use Fidry\CpuCounter\NumberOfCpuCoreNotFound; use InvalidArgumentException; use LogicException; use Psalm\Codebase; @@ -1468,23 +1470,17 @@ public function getFunctionLikeAnalyzer( * Adapted from https://gist.github.com/divinity76/01ef9ca99c111565a72d3a8a6e42f7fb * returns number of cpu cores * Copyleft 2018, license: WTFPL - * @throws RuntimeException - * @throws LogicException + * @throws NumberOfCpuCoreNotFound * @psalm-suppress ForbiddenCode */ public static function getCpuCount(): int { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { - /* - $str = trim((string) shell_exec('wmic cpu get NumberOfCores 2>&1')); - if (!preg_match('/(\d+)/', $str, $matches)) { - throw new RuntimeException('wmic failed to get number of cpu cores on windows!'); - } - return ((int) $matches [1]); - */ + // No support desired for Windows at the moment return 1; } + // PHP 7.3 with JIT on OSX is screwed for multi-threads if (ini_get('pcre.jit') === '1' && PHP_OS === 'Darwin' && version_compare(PHP_VERSION, '7.3.0') >= 0 @@ -1493,40 +1489,12 @@ public static function getCpuCount(): int return 1; } - if (!extension_loaded('pcntl') || !function_exists('shell_exec')) { + if (!extension_loaded('pcntl')) { + // Psalm requires pcntl for multi-threads support return 1; } - $has_nproc = trim((string) @shell_exec('command -v nproc')); - if ($has_nproc) { - $ret = @shell_exec('nproc'); - if (is_string($ret)) { - $ret = trim($ret); - $tmp = filter_var($ret, FILTER_VALIDATE_INT); - if (is_int($tmp)) { - return $tmp; - } - } - } - - $ret = @shell_exec('sysctl -n hw.ncpu'); - if (is_string($ret)) { - $ret = trim($ret); - $tmp = filter_var($ret, FILTER_VALIDATE_INT); - if (is_int($tmp)) { - return $tmp; - } - } - - if (is_readable('/proc/cpuinfo')) { - $cpuinfo = file_get_contents('/proc/cpuinfo'); - $count = substr_count($cpuinfo, 'processor'); - if ($count > 0) { - return $count; - } - } - - throw new LogicException('failed to detect number of CPUs!'); + return (new CpuCoreCounter())->getCount(); } /** From ba40e34947e5e113ecb56bf67fcc93d043a14ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 8 Dec 2022 12:17:26 +0100 Subject: [PATCH 2/3] Update to stable --- composer.json | 2 +- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9f16336aecb..bfe359c5b86 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "dnoegel/php-xdg-base-dir": "^0.1.1", "felixfbecker/advanced-json-rpc": "^3.1", "felixfbecker/language-server-protocol": "^1.5.2", - "fidry/cpu-core-counter": "^0.2.0", + "fidry/cpu-core-counter": "^0.3.0", "netresearch/jsonmapper": "^1.0 || ^2.0 || ^3.0 || ^4.0", "nikic/php-parser": "^4.13", "openlss/lib-array2xml": "^1.0", diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 923d10c59a5..bbb085de5cd 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -1471,7 +1471,6 @@ public function getFunctionLikeAnalyzer( * returns number of cpu cores * Copyleft 2018, license: WTFPL * @throws NumberOfCpuCoreNotFound - * @psalm-suppress ForbiddenCode */ public static function getCpuCount(): int { From 5d2b739a4e11a19e2575bb2498324b209e17eecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 8 Dec 2022 12:27:11 +0100 Subject: [PATCH 3/3] Fix CS --- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index bbb085de5cd..b9bed7eb41e 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -3,10 +3,9 @@ namespace Psalm\Internal\Analyzer; use Amp\Loop; -use Fidry\CpuCounter\CpuCoreCounter; -use Fidry\CpuCounter\NumberOfCpuCoreNotFound; +use Fidry\CpuCoreCounter\CpuCoreCounter; +use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound; use InvalidArgumentException; -use LogicException; use Psalm\Codebase; use Psalm\Config; use Psalm\Context; @@ -55,7 +54,6 @@ use Psalm\Report\ReportOptions; use Psalm\Type; use ReflectionProperty; -use RuntimeException; use UnexpectedValueException; use function array_combine; @@ -75,25 +73,18 @@ use function explode; use function extension_loaded; use function file_exists; -use function file_get_contents; -use function filter_var; -use function function_exists; use function fwrite; use function implode; use function in_array; use function ini_get; use function is_dir; use function is_file; -use function is_int; -use function is_readable; -use function is_string; use function microtime; use function mkdir; use function number_format; use function pcntl_fork; use function preg_match; use function rename; -use function shell_exec; use function stream_set_blocking; use function stream_socket_accept; use function stream_socket_client; @@ -102,12 +93,9 @@ use function strpos; use function strtolower; use function substr; -use function substr_count; -use function trim; use function usort; use function version_compare; -use const FILTER_VALIDATE_INT; use const PHP_EOL; use const PHP_OS; use const PHP_VERSION;