Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate FidryCpuCoreCounter #8833

Merged
merged 4 commits into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down
48 changes: 8 additions & 40 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
theofidry marked this conversation as resolved.
Show resolved Hide resolved
*/
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
Expand All @@ -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();
}

/**
Expand Down