Skip to content

Commit ef02ded

Browse files
authored
Merge pull request #8833 from theofidry/feature/cpu-counter
Integrate FidryCpuCoreCounter
2 parents af549fa + 5d2b739 commit ef02ded

File tree

2 files changed

+9
-53
lines changed

2 files changed

+9
-53
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dnoegel/php-xdg-base-dir": "^0.1.1",
3131
"felixfbecker/advanced-json-rpc": "^3.1",
3232
"felixfbecker/language-server-protocol": "^1.5.2",
33+
"fidry/cpu-core-counter": "^0.3.0",
3334
"netresearch/jsonmapper": "^1.0 || ^2.0 || ^3.0 || ^4.0",
3435
"nikic/php-parser": "^4.13",
3536
"openlss/lib-array2xml": "^1.0",

src/Psalm/Internal/Analyzer/ProjectAnalyzer.php

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Psalm\Internal\Analyzer;
44

55
use Amp\Loop;
6+
use Fidry\CpuCoreCounter\CpuCoreCounter;
7+
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
68
use InvalidArgumentException;
7-
use LogicException;
89
use Psalm\Codebase;
910
use Psalm\Config;
1011
use Psalm\Context;
@@ -53,7 +54,6 @@
5354
use Psalm\Report\ReportOptions;
5455
use Psalm\Type;
5556
use ReflectionProperty;
56-
use RuntimeException;
5757
use UnexpectedValueException;
5858

5959
use function array_combine;
@@ -73,25 +73,18 @@
7373
use function explode;
7474
use function extension_loaded;
7575
use function file_exists;
76-
use function file_get_contents;
77-
use function filter_var;
78-
use function function_exists;
7976
use function fwrite;
8077
use function implode;
8178
use function in_array;
8279
use function ini_get;
8380
use function is_dir;
8481
use function is_file;
85-
use function is_int;
86-
use function is_readable;
87-
use function is_string;
8882
use function microtime;
8983
use function mkdir;
9084
use function number_format;
9185
use function pcntl_fork;
9286
use function preg_match;
9387
use function rename;
94-
use function shell_exec;
9588
use function stream_set_blocking;
9689
use function stream_socket_accept;
9790
use function stream_socket_client;
@@ -100,12 +93,9 @@
10093
use function strpos;
10194
use function strtolower;
10295
use function substr;
103-
use function substr_count;
104-
use function trim;
10596
use function usort;
10697
use function version_compare;
10798

108-
use const FILTER_VALIDATE_INT;
10999
use const PHP_EOL;
110100
use const PHP_OS;
111101
use const PHP_VERSION;
@@ -1468,23 +1458,16 @@ public function getFunctionLikeAnalyzer(
14681458
* Adapted from https://gist.github.com/divinity76/01ef9ca99c111565a72d3a8a6e42f7fb
14691459
* returns number of cpu cores
14701460
* Copyleft 2018, license: WTFPL
1471-
* @throws RuntimeException
1472-
* @throws LogicException
1473-
* @psalm-suppress ForbiddenCode
1461+
* @throws NumberOfCpuCoreNotFound
14741462
*/
14751463
public static function getCpuCount(): int
14761464
{
14771465
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
1478-
/*
1479-
$str = trim((string) shell_exec('wmic cpu get NumberOfCores 2>&1'));
1480-
if (!preg_match('/(\d+)/', $str, $matches)) {
1481-
throw new RuntimeException('wmic failed to get number of cpu cores on windows!');
1482-
}
1483-
return ((int) $matches [1]);
1484-
*/
1466+
// No support desired for Windows at the moment
14851467
return 1;
14861468
}
14871469

1470+
// PHP 7.3 with JIT on OSX is screwed for multi-threads
14881471
if (ini_get('pcre.jit') === '1'
14891472
&& PHP_OS === 'Darwin'
14901473
&& version_compare(PHP_VERSION, '7.3.0') >= 0
@@ -1493,40 +1476,12 @@ public static function getCpuCount(): int
14931476
return 1;
14941477
}
14951478

1496-
if (!extension_loaded('pcntl') || !function_exists('shell_exec')) {
1479+
if (!extension_loaded('pcntl')) {
1480+
// Psalm requires pcntl for multi-threads support
14971481
return 1;
14981482
}
14991483

1500-
$has_nproc = trim((string) @shell_exec('command -v nproc'));
1501-
if ($has_nproc) {
1502-
$ret = @shell_exec('nproc');
1503-
if (is_string($ret)) {
1504-
$ret = trim($ret);
1505-
$tmp = filter_var($ret, FILTER_VALIDATE_INT);
1506-
if (is_int($tmp)) {
1507-
return $tmp;
1508-
}
1509-
}
1510-
}
1511-
1512-
$ret = @shell_exec('sysctl -n hw.ncpu');
1513-
if (is_string($ret)) {
1514-
$ret = trim($ret);
1515-
$tmp = filter_var($ret, FILTER_VALIDATE_INT);
1516-
if (is_int($tmp)) {
1517-
return $tmp;
1518-
}
1519-
}
1520-
1521-
if (is_readable('/proc/cpuinfo')) {
1522-
$cpuinfo = file_get_contents('/proc/cpuinfo');
1523-
$count = substr_count($cpuinfo, 'processor');
1524-
if ($count > 0) {
1525-
return $count;
1526-
}
1527-
}
1528-
1529-
throw new LogicException('failed to detect number of CPUs!');
1484+
return (new CpuCoreCounter())->getCount();
15301485
}
15311486

15321487
/**

0 commit comments

Comments
 (0)