mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Integrate FidryCpuCoreCounter
This commit is contained in:
parent
83ba661135
commit
a444f286bb
@ -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",
|
||||
|
@ -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 @@ class ProjectAnalyzer
|
||||
* 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 @@ class ProjectAnalyzer
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user