Install psalm as dev, sync scripts updates
This commit is contained in:
16
vendor/fidry/cpu-core-counter/LICENSE.md
vendored
Normal file
16
vendor/fidry/cpu-core-counter/LICENSE.md
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2022 Théo FIDRY <theo.fidry@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
||||
documentation files (the _Software_), to deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED **AS IS**, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
||||
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
114
vendor/fidry/cpu-core-counter/README.md
vendored
Normal file
114
vendor/fidry/cpu-core-counter/README.md
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
# CPU Core Counter
|
||||
|
||||
This package is a tiny utility to get the number of CPU cores.
|
||||
|
||||
```sh
|
||||
composer require fidry/cpu-core-counter
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```php
|
||||
use Fidry\CpuCoreCounter\CpuCoreCounter;
|
||||
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
|
||||
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
|
||||
|
||||
$counter = new CpuCoreCounter();
|
||||
|
||||
try {
|
||||
$counter->getCount(); // e.g. 8
|
||||
} catch (NumberOfCpuCoreNotFound) {
|
||||
return 1; // Fallback value
|
||||
}
|
||||
|
||||
// An alternative form where we not want to catch the exception:
|
||||
|
||||
$counter = new CpuCoreCounter([
|
||||
...CpuCoreCounter::getDefaultFinders(),
|
||||
new DummyCpuCoreFinder(1), // Fallback value
|
||||
]);
|
||||
|
||||
$counter->getCount(); // e.g. 8
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Advanced usage
|
||||
|
||||
### Changing the finders
|
||||
|
||||
When creating `CpuCoreCounter`, you may want to change the order of the finders
|
||||
used or disable a specific finder. You can easily do so by passing the finders
|
||||
you want
|
||||
|
||||
```php
|
||||
// Remove WindowsWmicFinder
|
||||
$finders = array_filter(
|
||||
CpuCoreCounter::getDefaultFinders(),
|
||||
static fn (CpuCoreFinder $finder) => !($finder instanceof WindowsWmicFinder)
|
||||
);
|
||||
|
||||
$cores = (new CpuCoreCounter($finders))->getCount();
|
||||
```
|
||||
|
||||
```php
|
||||
// Use CPUInfo first & don't use Nproc
|
||||
$finders = [
|
||||
new CpuInfoFinder(),
|
||||
new WindowsWmicFinder(),
|
||||
new HwLogicalFinder(),
|
||||
];
|
||||
|
||||
$cores = (new CpuCoreCounter($finders))->getCount();
|
||||
```
|
||||
|
||||
### Choosing only logical or physical finders
|
||||
|
||||
`FinderRegistry` provides two helpful entries:
|
||||
|
||||
- `::getDefaultLogicalFinders()`: gives an ordered list of finders that will
|
||||
look for the _logical_ CPU cores count
|
||||
- `::getDefaultPhysicalFinders()`: gives an ordered list of finders that will
|
||||
look for the _physical_ CPU cores count
|
||||
|
||||
By default when using `CpuCoreCounter`, it will use the logical finders since
|
||||
it is more likely what you are looking for and is what is used by PHP source to
|
||||
build the PHP binary.
|
||||
|
||||
|
||||
### Checks what finders find what on your system
|
||||
|
||||
You have two commands available that provides insight about what the finders
|
||||
can find:
|
||||
|
||||
```
|
||||
$ make diagnosis # From this repository
|
||||
$ ./vendor/fidry/cpu-core-counter/bin/diagnose.php # From the library
|
||||
```
|
||||
|
||||
And:
|
||||
```
|
||||
$ make execute # From this repository
|
||||
$ ./vendor/fidry/cpu-core-counter/bin/execute.php # From the library
|
||||
```
|
||||
|
||||
|
||||
## Backward Compatibility Promise (BCP)
|
||||
|
||||
The policy is for the major part following the same as [Symfony's one][symfony-bc-policy].
|
||||
Note that the code marked as `@private` or `@internal` are excluded from the BCP.
|
||||
|
||||
The following elements are also excluded:
|
||||
|
||||
- The `diagnose` and `execute` commands: those are for debugging/inspection purposes only
|
||||
- `FinderRegistry::get*Finders()`: new finders may be added or the order of finders changed at any time
|
||||
|
||||
|
||||
## License
|
||||
|
||||
This package is licensed using the MIT License.
|
||||
|
||||
Please have a look at [`LICENSE.md`](LICENSE.md).
|
||||
|
||||
[symfony-bc-policy]: https://symfony.com/doc/current/contributing/code/bc.html
|
||||
21
vendor/fidry/cpu-core-counter/bin/diagnose.php
vendored
Executable file
21
vendor/fidry/cpu-core-counter/bin/diagnose.php
vendored
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Fidry\CpuCoreCounter\Diagnoser;
|
||||
use Fidry\CpuCoreCounter\Finder\FinderRegistry;
|
||||
|
||||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
echo 'Running diagnosis...'.PHP_EOL.PHP_EOL;
|
||||
echo Diagnoser::diagnose(FinderRegistry::getAllVariants()).PHP_EOL;
|
||||
21
vendor/fidry/cpu-core-counter/bin/execute.php
vendored
Executable file
21
vendor/fidry/cpu-core-counter/bin/execute.php
vendored
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Fidry\CpuCoreCounter\Diagnoser;
|
||||
use Fidry\CpuCoreCounter\Finder\FinderRegistry;
|
||||
|
||||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
echo 'Executing finders...'.PHP_EOL.PHP_EOL;
|
||||
echo Diagnoser::execute(FinderRegistry::getAllVariants()).PHP_EOL;
|
||||
48
vendor/fidry/cpu-core-counter/composer.json
vendored
Normal file
48
vendor/fidry/cpu-core-counter/composer.json
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "fidry/cpu-core-counter",
|
||||
"description": "Tiny utility to get the number of CPU cores.",
|
||||
"license": "MIT",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"cpu",
|
||||
"core"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Théo FIDRY",
|
||||
"email": "theo.fidry@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2 || ^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fidry/makefile": "^0.2.0",
|
||||
"phpstan/extension-installer": "^1.2.0",
|
||||
"phpstan/phpstan": "^1.9.2",
|
||||
"phpstan/phpstan-deprecation-rules": "^1.0.0",
|
||||
"phpstan/phpstan-phpunit": "^1.2.2",
|
||||
"phpstan/phpstan-strict-rules": "^1.4.4",
|
||||
"phpunit/phpunit": "^9.5.26 || ^8.5.31",
|
||||
"theofidry/php-cs-fixer-config": "^1.0",
|
||||
"webmozarts/strict-phpunit": "^7.5"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Fidry\\CpuCoreCounter\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Fidry\\CpuCoreCounter\\Test\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"ergebnis/composer-normalize": true,
|
||||
"infection/extension-installer": true,
|
||||
"phpstan/extension-installer": true
|
||||
},
|
||||
"sort-packages": true
|
||||
}
|
||||
}
|
||||
89
vendor/fidry/cpu-core-counter/src/CpuCoreCounter.php
vendored
Normal file
89
vendor/fidry/cpu-core-counter/src/CpuCoreCounter.php
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter;
|
||||
|
||||
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
|
||||
use Fidry\CpuCoreCounter\Finder\FinderRegistry;
|
||||
|
||||
final class CpuCoreCounter
|
||||
{
|
||||
/**
|
||||
* @var list<CpuCoreFinder>
|
||||
*/
|
||||
private $finders;
|
||||
|
||||
/**
|
||||
* @var positive-int|null
|
||||
*/
|
||||
private $count;
|
||||
|
||||
/**
|
||||
* @param list<CpuCoreFinder>|null $finders
|
||||
*/
|
||||
public function __construct(?array $finders = null)
|
||||
{
|
||||
$this->finders = $finders ?? FinderRegistry::getDefaultLogicalFinders();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NumberOfCpuCoreNotFound
|
||||
*
|
||||
* @return positive-int
|
||||
*/
|
||||
public function getCount(): int
|
||||
{
|
||||
// Memoize result
|
||||
if (null === $this->count) {
|
||||
$this->count = $this->findCount();
|
||||
}
|
||||
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NumberOfCpuCoreNotFound
|
||||
*
|
||||
* @return positive-int
|
||||
*/
|
||||
private function findCount(): int
|
||||
{
|
||||
foreach ($this->finders as $finder) {
|
||||
$cores = $finder->find();
|
||||
|
||||
if (null !== $cores) {
|
||||
return $cores;
|
||||
}
|
||||
}
|
||||
|
||||
throw NumberOfCpuCoreNotFound::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NumberOfCpuCoreNotFound
|
||||
*
|
||||
* @return array{CpuCoreFinder, positive-int}
|
||||
*/
|
||||
public function getFinderAndCores(): array
|
||||
{
|
||||
foreach ($this->finders as $finder) {
|
||||
$cores = $finder->find();
|
||||
|
||||
if (null !== $cores) {
|
||||
return [$finder, $cores];
|
||||
}
|
||||
}
|
||||
|
||||
throw NumberOfCpuCoreNotFound::create();
|
||||
}
|
||||
}
|
||||
101
vendor/fidry/cpu-core-counter/src/Diagnoser.php
vendored
Normal file
101
vendor/fidry/cpu-core-counter/src/Diagnoser.php
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter;
|
||||
|
||||
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
|
||||
use function array_map;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function max;
|
||||
use function str_repeat;
|
||||
use const PHP_EOL;
|
||||
|
||||
/**
|
||||
* Utility to debug.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
final class Diagnoser
|
||||
{
|
||||
/**
|
||||
* Provides an aggregated diagnosis based on each finders diagnosis.
|
||||
*
|
||||
* @param list<CpuCoreFinder> $finders
|
||||
*/
|
||||
public static function diagnose(array $finders): string
|
||||
{
|
||||
$diagnoses = array_map(
|
||||
static function (CpuCoreFinder $finder): string {
|
||||
return self::diagnoseFinder($finder);
|
||||
},
|
||||
$finders
|
||||
);
|
||||
|
||||
return implode(PHP_EOL, $diagnoses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes each finders.
|
||||
*
|
||||
* @param list<CpuCoreFinder> $finders
|
||||
*/
|
||||
public static function execute(array $finders): string
|
||||
{
|
||||
$diagnoses = array_map(
|
||||
static function (CpuCoreFinder $finder): string {
|
||||
$coresCount = $finder->find();
|
||||
|
||||
return implode(
|
||||
'',
|
||||
[
|
||||
$finder->toString(),
|
||||
': ',
|
||||
null === $coresCount ? 'NULL' : $coresCount,
|
||||
]
|
||||
);
|
||||
},
|
||||
$finders
|
||||
);
|
||||
|
||||
return implode(PHP_EOL, $diagnoses);
|
||||
}
|
||||
|
||||
private static function diagnoseFinder(CpuCoreFinder $finder): string
|
||||
{
|
||||
$diagnosis = $finder->diagnose();
|
||||
|
||||
$maxLineLength = max(
|
||||
array_map(
|
||||
'strlen',
|
||||
explode(PHP_EOL, $diagnosis)
|
||||
)
|
||||
);
|
||||
|
||||
$separator = str_repeat('-', $maxLineLength);
|
||||
|
||||
return implode(
|
||||
'',
|
||||
[
|
||||
$finder->toString().':'.PHP_EOL,
|
||||
$separator.PHP_EOL,
|
||||
$diagnosis.PHP_EOL,
|
||||
$separator.PHP_EOL,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
||||
56
vendor/fidry/cpu-core-counter/src/Executor/ProcOpenExecutor.php
vendored
Normal file
56
vendor/fidry/cpu-core-counter/src/Executor/ProcOpenExecutor.php
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Executor;
|
||||
|
||||
use function fclose;
|
||||
use function function_exists;
|
||||
use function is_resource;
|
||||
use function proc_close;
|
||||
use function proc_open;
|
||||
use function stream_get_contents;
|
||||
|
||||
final class ProcOpenExecutor implements ProcessExecutor
|
||||
{
|
||||
public function execute(string $command): ?array
|
||||
{
|
||||
if (!function_exists('proc_open')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pipes = [];
|
||||
|
||||
$process = @proc_open(
|
||||
$command,
|
||||
[
|
||||
['pipe', 'rb'],
|
||||
['pipe', 'wb'], // stdout
|
||||
['pipe', 'wb'], // stderr
|
||||
],
|
||||
$pipes
|
||||
);
|
||||
|
||||
if (!is_resource($process)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
fclose($pipes[0]);
|
||||
|
||||
$stdout = (string) stream_get_contents($pipes[1]);
|
||||
$stderr = (string) stream_get_contents($pipes[2]);
|
||||
|
||||
proc_close($process);
|
||||
|
||||
return [$stdout, $stderr];
|
||||
}
|
||||
}
|
||||
22
vendor/fidry/cpu-core-counter/src/Executor/ProcessExecutor.php
vendored
Normal file
22
vendor/fidry/cpu-core-counter/src/Executor/ProcessExecutor.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Executor;
|
||||
|
||||
interface ProcessExecutor
|
||||
{
|
||||
/**
|
||||
* @return array{string, string}|null STDOUT & STDERR tuple
|
||||
*/
|
||||
public function execute(string $command): ?array;
|
||||
}
|
||||
37
vendor/fidry/cpu-core-counter/src/Finder/CpuCoreFinder.php
vendored
Normal file
37
vendor/fidry/cpu-core-counter/src/Finder/CpuCoreFinder.php
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
interface CpuCoreFinder
|
||||
{
|
||||
/**
|
||||
* Provides an explanation which may offer some insight as to what the finder
|
||||
* will be able to find.
|
||||
*
|
||||
* This is practical to have an idea of what each finder will find collect
|
||||
* information for the unit tests, since integration tests are quite complicated
|
||||
* as dependent on complex infrastructures.
|
||||
*/
|
||||
public function diagnose(): string;
|
||||
|
||||
/**
|
||||
* Find the number of CPU cores. If it could not find it, returns null. The
|
||||
* means used to find the cores are at the implementation discretion.
|
||||
*
|
||||
* @return positive-int|null
|
||||
*/
|
||||
public function find(): ?int;
|
||||
|
||||
public function toString(): string;
|
||||
}
|
||||
98
vendor/fidry/cpu-core-counter/src/Finder/CpuInfoFinder.php
vendored
Normal file
98
vendor/fidry/cpu-core-counter/src/Finder/CpuInfoFinder.php
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function file_get_contents;
|
||||
use function is_file;
|
||||
use function sprintf;
|
||||
use function substr_count;
|
||||
use const PHP_EOL;
|
||||
|
||||
/**
|
||||
* Find the number of CPU cores looking up at the cpuinfo file which is available
|
||||
* on Linux systems and Windows systems with a Linux sub-system.
|
||||
*
|
||||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
|
||||
* @see https://unix.stackexchange.com/questions/146051/number-of-processors-in-proc-cpuinfo
|
||||
*/
|
||||
final class CpuInfoFinder implements CpuCoreFinder
|
||||
{
|
||||
private const CPU_INFO_PATH = '/proc/cpuinfo';
|
||||
|
||||
public function diagnose(): string
|
||||
{
|
||||
if (!is_file(self::CPU_INFO_PATH)) {
|
||||
return sprintf(
|
||||
'The file "%s" could not be found.',
|
||||
self::CPU_INFO_PATH
|
||||
);
|
||||
}
|
||||
|
||||
$cpuInfo = file_get_contents(self::CPU_INFO_PATH);
|
||||
|
||||
if (false === $cpuInfo) {
|
||||
return sprintf(
|
||||
'Could not get the content of the file "%s".',
|
||||
self::CPU_INFO_PATH
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'Found the file "%s" with the content:%s%s',
|
||||
self::CPU_INFO_PATH,
|
||||
PHP_EOL,
|
||||
$cpuInfo
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return positive-int|null
|
||||
*/
|
||||
public function find(): ?int
|
||||
{
|
||||
$cpuInfo = self::getCpuInfo();
|
||||
|
||||
return null === $cpuInfo ? null : self::countCpuCores($cpuInfo);
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'CpuInfoFinder';
|
||||
}
|
||||
|
||||
private static function getCpuInfo(): ?string
|
||||
{
|
||||
if (!@is_file(self::CPU_INFO_PATH)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$cpuInfo = @file_get_contents(self::CPU_INFO_PATH);
|
||||
|
||||
return false === $cpuInfo
|
||||
? null
|
||||
: $cpuInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @return positive-int|null
|
||||
*/
|
||||
public static function countCpuCores(string $cpuInfo): ?int
|
||||
{
|
||||
$processorCount = substr_count($cpuInfo, 'processor');
|
||||
|
||||
return $processorCount > 0 ? $processorCount : null;
|
||||
}
|
||||
}
|
||||
57
vendor/fidry/cpu-core-counter/src/Finder/DummyCpuCoreFinder.php
vendored
Normal file
57
vendor/fidry/cpu-core-counter/src/Finder/DummyCpuCoreFinder.php
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* This finder returns whatever value you gave to it. This is useful for testing
|
||||
* or as a fallback to avoid to catch the NumberOfCpuCoreNotFound exception.
|
||||
*/
|
||||
final class DummyCpuCoreFinder implements CpuCoreFinder
|
||||
{
|
||||
/**
|
||||
* @var positive-int
|
||||
*/
|
||||
private $count;
|
||||
|
||||
public function diagnose(): string
|
||||
{
|
||||
return sprintf(
|
||||
'Will return "%d".',
|
||||
$this->count
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param positive-int $count
|
||||
*/
|
||||
public function __construct(int $count)
|
||||
{
|
||||
$this->count = $count;
|
||||
}
|
||||
|
||||
public function find(): ?int
|
||||
{
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'DummyCpuCoreFinder(value=%d)',
|
||||
$this->count
|
||||
);
|
||||
}
|
||||
}
|
||||
77
vendor/fidry/cpu-core-counter/src/Finder/FinderRegistry.php
vendored
Normal file
77
vendor/fidry/cpu-core-counter/src/Finder/FinderRegistry.php
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
final class FinderRegistry
|
||||
{
|
||||
/**
|
||||
* @return list<CpuCoreFinder> List of all the known finders with all their variants.
|
||||
*/
|
||||
public static function getAllVariants(): array
|
||||
{
|
||||
return [
|
||||
new CpuInfoFinder(),
|
||||
new DummyCpuCoreFinder(1),
|
||||
new HwLogicalFinder(),
|
||||
new HwPhysicalFinder(),
|
||||
new LscpuLogicalFinder(),
|
||||
new LscpuPhysicalFinder(),
|
||||
new _NProcessorFinder(),
|
||||
new NProcessorFinder(),
|
||||
new NProcFinder(true),
|
||||
new NProcFinder(false),
|
||||
new NullCpuCoreFinder(),
|
||||
SkipOnOSFamilyFinder::forWindows(
|
||||
new DummyCpuCoreFinder(1)
|
||||
),
|
||||
OnlyOnOSFamilyFinder::forWindows(
|
||||
new DummyCpuCoreFinder(1)
|
||||
),
|
||||
new WmicPhysicalFinder(),
|
||||
new WmicLogicalFinder(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<CpuCoreFinder>
|
||||
*/
|
||||
public static function getDefaultLogicalFinders(): array
|
||||
{
|
||||
return [
|
||||
OnlyOnOSFamilyFinder::forWindows(new WmicLogicalFinder()),
|
||||
new NProcFinder(),
|
||||
new HwLogicalFinder(),
|
||||
new _NProcessorFinder(),
|
||||
new NProcessorFinder(),
|
||||
new LscpuLogicalFinder(),
|
||||
new CpuInfoFinder(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<CpuCoreFinder>
|
||||
*/
|
||||
public static function getDefaultPhysicalFinders(): array
|
||||
{
|
||||
return [
|
||||
OnlyOnOSFamilyFinder::forWindows(new WmicPhysicalFinder()),
|
||||
new HwPhysicalFinder(),
|
||||
new LscpuPhysicalFinder(),
|
||||
];
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
||||
33
vendor/fidry/cpu-core-counter/src/Finder/HwLogicalFinder.php
vendored
Normal file
33
vendor/fidry/cpu-core-counter/src/Finder/HwLogicalFinder.php
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
/**
|
||||
* Find the number of logical CPU cores for Linux, BSD and OSX.
|
||||
*
|
||||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
|
||||
* @see https://opensource.apple.com/source/xnu/xnu-792.2.4/libkern/libkern/sysctl.h.auto.html
|
||||
*/
|
||||
final class HwLogicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'sysctl -n hw.logicalcpu';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'HwLogicalFinder';
|
||||
}
|
||||
}
|
||||
33
vendor/fidry/cpu-core-counter/src/Finder/HwPhysicalFinder.php
vendored
Normal file
33
vendor/fidry/cpu-core-counter/src/Finder/HwPhysicalFinder.php
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
/**
|
||||
* Find the number of physical CPU cores for Linux, BSD and OSX.
|
||||
*
|
||||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
|
||||
* @see https://opensource.apple.com/source/xnu/xnu-792.2.4/libkern/libkern/sysctl.h.auto.html
|
||||
*/
|
||||
final class HwPhysicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'sysctl -n hw.physicalcpu';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'HwPhysicalFinder';
|
||||
}
|
||||
}
|
||||
52
vendor/fidry/cpu-core-counter/src/Finder/LscpuLogicalFinder.php
vendored
Normal file
52
vendor/fidry/cpu-core-counter/src/Finder/LscpuLogicalFinder.php
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function count;
|
||||
use function explode;
|
||||
use function is_array;
|
||||
use function preg_grep;
|
||||
use const PHP_EOL;
|
||||
|
||||
/**
|
||||
* The number of logical cores.
|
||||
*
|
||||
* @see https://stackoverflow.com/a/23378780/5846754
|
||||
*/
|
||||
final class LscpuLogicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
public function getCommand(): string
|
||||
{
|
||||
return 'lscpu -p';
|
||||
}
|
||||
|
||||
protected function countCpuCores(string $process): ?int
|
||||
{
|
||||
$lines = explode(PHP_EOL, $process);
|
||||
$actualLines = preg_grep('/^\d+,/', $lines);
|
||||
|
||||
if (!is_array($actualLines)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$count = count($actualLines);
|
||||
|
||||
return 0 === $count ? null : $count;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'LscpuLogicalFinder';
|
||||
}
|
||||
}
|
||||
66
vendor/fidry/cpu-core-counter/src/Finder/LscpuPhysicalFinder.php
vendored
Normal file
66
vendor/fidry/cpu-core-counter/src/Finder/LscpuPhysicalFinder.php
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function count;
|
||||
use function explode;
|
||||
use function is_array;
|
||||
use function preg_grep;
|
||||
use function strtok;
|
||||
use const PHP_EOL;
|
||||
|
||||
/**
|
||||
* The number of physical processors.
|
||||
*
|
||||
* @see https://stackoverflow.com/a/23378780/5846754
|
||||
*/
|
||||
final class LscpuPhysicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
public function toString(): string
|
||||
{
|
||||
return 'LscpuPhysicalFinder';
|
||||
}
|
||||
|
||||
public function getCommand(): string
|
||||
{
|
||||
return 'lscpu -p';
|
||||
}
|
||||
|
||||
protected function countCpuCores(string $process): ?int
|
||||
{
|
||||
$lines = explode(PHP_EOL, $process);
|
||||
$actualLines = preg_grep('/^\d+/', $lines);
|
||||
|
||||
if (!is_array($actualLines)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$cores = [];
|
||||
foreach ($actualLines as $line) {
|
||||
strtok($line, ',');
|
||||
$core = strtok(',');
|
||||
|
||||
if (false === $core) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cores[$core] = true;
|
||||
}
|
||||
unset($cores['-']);
|
||||
|
||||
$count = count($cores);
|
||||
|
||||
return 0 === $count ? null : $count;
|
||||
}
|
||||
}
|
||||
56
vendor/fidry/cpu-core-counter/src/Finder/NProcFinder.php
vendored
Normal file
56
vendor/fidry/cpu-core-counter/src/Finder/NProcFinder.php
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* The number of (logical) cores.
|
||||
*
|
||||
* @see https://github.com/infection/infection/blob/fbd8c44/src/Resource/Processor/CpuCoresCountProvider.php#L69-L82
|
||||
* @see https://unix.stackexchange.com/questions/146051/number-of-processors-in-proc-cpuinfo
|
||||
*/
|
||||
final class NProcFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $all;
|
||||
|
||||
/**
|
||||
* @param bool $all If disabled will give the number of cores available for the current process only.
|
||||
*/
|
||||
public function __construct(
|
||||
bool $all = true,
|
||||
?ProcessExecutor $executor = null
|
||||
) {
|
||||
parent::__construct($executor);
|
||||
|
||||
$this->all = $all;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'NProcFinder(all=%s)',
|
||||
$this->all ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'nproc'.($this->all ? ' --all' : '');
|
||||
}
|
||||
}
|
||||
32
vendor/fidry/cpu-core-counter/src/Finder/NProcessorFinder.php
vendored
Normal file
32
vendor/fidry/cpu-core-counter/src/Finder/NProcessorFinder.php
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
/**
|
||||
* Find the number of logical CPU cores for FreeSBD, Solaris and the likes.
|
||||
*
|
||||
* @see https://twitter.com/freebsdfrau/status/1052016199452700678?s=20&t=M2pHkRqmmna-UF68lfL2hw
|
||||
*/
|
||||
final class NProcessorFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'getconf NPROCESSORS_ONLN';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'NProcessorFinder';
|
||||
}
|
||||
}
|
||||
35
vendor/fidry/cpu-core-counter/src/Finder/NullCpuCoreFinder.php
vendored
Normal file
35
vendor/fidry/cpu-core-counter/src/Finder/NullCpuCoreFinder.php
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
/**
|
||||
* This finder returns whatever value you gave to it. This is useful for testing.
|
||||
*/
|
||||
final class NullCpuCoreFinder implements CpuCoreFinder
|
||||
{
|
||||
public function diagnose(): string
|
||||
{
|
||||
return 'Will return "null".';
|
||||
}
|
||||
|
||||
public function find(): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'NullCpuCoreFinder';
|
||||
}
|
||||
}
|
||||
113
vendor/fidry/cpu-core-counter/src/Finder/OnlyOnOSFamilyFinder.php
vendored
Normal file
113
vendor/fidry/cpu-core-counter/src/Finder/OnlyOnOSFamilyFinder.php
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function implode;
|
||||
use function sprintf;
|
||||
use const PHP_OS_FAMILY;
|
||||
|
||||
final class OnlyOnOSFamilyFinder implements CpuCoreFinder
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private $skippedOSFamilies;
|
||||
|
||||
/**
|
||||
* @var CpuCoreFinder
|
||||
*/
|
||||
private $decoratedFinder;
|
||||
|
||||
/**
|
||||
* @param string|list<string> $skippedOSFamilyOrFamilies
|
||||
*/
|
||||
public function __construct(
|
||||
$skippedOSFamilyOrFamilies,
|
||||
CpuCoreFinder $decoratedFinder
|
||||
) {
|
||||
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
|
||||
$this->decoratedFinder = $decoratedFinder;
|
||||
}
|
||||
|
||||
public static function forWindows(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Windows',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forBSD(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'BSD',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forDarwin(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Darwin',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forSolaris(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Solaris',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forLinux(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Linux',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public function diagnose(): string
|
||||
{
|
||||
return $this->skip()
|
||||
? sprintf(
|
||||
'Skipped platform detected ("%s").',
|
||||
PHP_OS_FAMILY
|
||||
)
|
||||
: $this->decoratedFinder->diagnose();
|
||||
}
|
||||
|
||||
public function find(): ?int
|
||||
{
|
||||
return $this->skip()
|
||||
? null
|
||||
: $this->decoratedFinder->find();
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'OnlyOnOSFamilyFinder(only=(%s),%s)',
|
||||
implode(',', $this->skippedOSFamilies),
|
||||
$this->decoratedFinder->toString()
|
||||
);
|
||||
}
|
||||
|
||||
private function skip(): bool
|
||||
{
|
||||
return !in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
|
||||
}
|
||||
}
|
||||
104
vendor/fidry/cpu-core-counter/src/Finder/ProcOpenBasedFinder.php
vendored
Normal file
104
vendor/fidry/cpu-core-counter/src/Finder/ProcOpenBasedFinder.php
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
|
||||
use Fidry\CpuCoreCounter\Executor\ProcOpenExecutor;
|
||||
use function filter_var;
|
||||
use function function_exists;
|
||||
use function is_int;
|
||||
use function sprintf;
|
||||
use function trim;
|
||||
use const FILTER_VALIDATE_INT;
|
||||
use const PHP_EOL;
|
||||
|
||||
abstract class ProcOpenBasedFinder implements CpuCoreFinder
|
||||
{
|
||||
/**
|
||||
* @var ProcessExecutor
|
||||
*/
|
||||
private $executor;
|
||||
|
||||
public function __construct(?ProcessExecutor $executor = null)
|
||||
{
|
||||
$this->executor = $executor ?? new ProcOpenExecutor();
|
||||
}
|
||||
|
||||
public function diagnose(): string
|
||||
{
|
||||
if (!function_exists('proc_open')) {
|
||||
return 'The function "proc_open" is not available.';
|
||||
}
|
||||
|
||||
$command = $this->getCommand();
|
||||
$output = $this->executor->execute($command);
|
||||
|
||||
if (null === $output) {
|
||||
return sprintf(
|
||||
'Failed to execute the command "%s".',
|
||||
$command
|
||||
);
|
||||
}
|
||||
|
||||
[$stdout, $stderr] = $output;
|
||||
$failed = '' !== trim($stderr);
|
||||
|
||||
return $failed
|
||||
? sprintf(
|
||||
'Executed the command "%s" which wrote the following output to the STDERR:%s%s',
|
||||
$command,
|
||||
PHP_EOL,
|
||||
$stderr
|
||||
)
|
||||
: sprintf(
|
||||
'Executed the command "%s" and got the following (STDOUT) output:%s%s',
|
||||
$command,
|
||||
PHP_EOL,
|
||||
$stdout
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return positive-int|null
|
||||
*/
|
||||
public function find(): ?int
|
||||
{
|
||||
$output = $this->executor->execute($this->getCommand());
|
||||
|
||||
if (null === $output) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$stdout, $stderr] = $output;
|
||||
$failed = '' !== trim($stderr);
|
||||
|
||||
return $failed
|
||||
? null
|
||||
: $this->countCpuCores($stdout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @return positive-int|null
|
||||
*/
|
||||
protected function countCpuCores(string $process): ?int
|
||||
{
|
||||
$cpuCount = filter_var($process, FILTER_VALIDATE_INT);
|
||||
|
||||
return is_int($cpuCount) && $cpuCount > 0 ? $cpuCount : null;
|
||||
}
|
||||
|
||||
abstract protected function getCommand(): string;
|
||||
}
|
||||
113
vendor/fidry/cpu-core-counter/src/Finder/SkipOnOSFamilyFinder.php
vendored
Normal file
113
vendor/fidry/cpu-core-counter/src/Finder/SkipOnOSFamilyFinder.php
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
|
||||
final class SkipOnOSFamilyFinder implements CpuCoreFinder
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private $skippedOSFamilies;
|
||||
|
||||
/**
|
||||
* @var CpuCoreFinder
|
||||
*/
|
||||
private $decoratedFinder;
|
||||
|
||||
/**
|
||||
* @param string|list<string> $skippedOSFamilyOrFamilies
|
||||
*/
|
||||
public function __construct(
|
||||
$skippedOSFamilyOrFamilies,
|
||||
CpuCoreFinder $decoratedFinder
|
||||
) {
|
||||
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
|
||||
$this->decoratedFinder = $decoratedFinder;
|
||||
}
|
||||
|
||||
public static function forWindows(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Windows',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forBSD(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'BSD',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forDarwin(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Darwin',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forSolaris(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Solaris',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public static function forLinux(CpuCoreFinder $decoratedFinder): self
|
||||
{
|
||||
return new self(
|
||||
'Linux',
|
||||
$decoratedFinder
|
||||
);
|
||||
}
|
||||
|
||||
public function diagnose(): string
|
||||
{
|
||||
return $this->skip()
|
||||
? sprintf(
|
||||
'Skipped platform detected ("%s").',
|
||||
PHP_OS_FAMILY
|
||||
)
|
||||
: $this->decoratedFinder->diagnose();
|
||||
}
|
||||
|
||||
public function find(): ?int
|
||||
{
|
||||
return $this->skip()
|
||||
? null
|
||||
: $this->decoratedFinder->find();
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'SkipOnOSFamilyFinder(skip=(%s),%s)',
|
||||
implode(',', $this->skippedOSFamilies),
|
||||
$this->decoratedFinder->toString()
|
||||
);
|
||||
}
|
||||
|
||||
private function skip(): bool
|
||||
{
|
||||
return in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
|
||||
}
|
||||
}
|
||||
47
vendor/fidry/cpu-core-counter/src/Finder/WmicLogicalFinder.php
vendored
Normal file
47
vendor/fidry/cpu-core-counter/src/Finder/WmicLogicalFinder.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Find the number of logical CPU cores for Windows.
|
||||
*
|
||||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
|
||||
*/
|
||||
final class WmicLogicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
private const CPU_CORE_COUNT_REGEX = '/NumberOfLogicalProcessors[\s\n]+(?<count>\d+)/';
|
||||
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'wmic cpu get NumberOfLogicalProcessors';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'WmicLogicalFinder';
|
||||
}
|
||||
|
||||
protected function countCpuCores(string $process): ?int
|
||||
{
|
||||
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
|
||||
return parent::countCpuCores($process);
|
||||
}
|
||||
|
||||
$count = $matches['count'];
|
||||
|
||||
return parent::countCpuCores($count);
|
||||
}
|
||||
}
|
||||
47
vendor/fidry/cpu-core-counter/src/Finder/WmicPhysicalFinder.php
vendored
Normal file
47
vendor/fidry/cpu-core-counter/src/Finder/WmicPhysicalFinder.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Find the number of physical CPU cores for Windows.
|
||||
*
|
||||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
|
||||
*/
|
||||
final class WmicPhysicalFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
private const CPU_CORE_COUNT_REGEX = '/NumberOfCores[\s\n]+(?<count>\d+)/';
|
||||
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'wmic cpu get NumberOfCores';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return 'WmicPhysicalFinder';
|
||||
}
|
||||
|
||||
protected function countCpuCores(string $process): ?int
|
||||
{
|
||||
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
|
||||
return parent::countCpuCores($process);
|
||||
}
|
||||
|
||||
$count = $matches['count'];
|
||||
|
||||
return parent::countCpuCores($count);
|
||||
}
|
||||
}
|
||||
32
vendor/fidry/cpu-core-counter/src/Finder/_NProcessorFinder.php
vendored
Normal file
32
vendor/fidry/cpu-core-counter/src/Finder/_NProcessorFinder.php
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter\Finder;
|
||||
|
||||
/**
|
||||
* Find the number of logical CPU cores for Linux and the likes.
|
||||
*
|
||||
* @see https://twitter.com/freebsdfrau/status/1052016199452700678?s=20&t=M2pHkRqmmna-UF68lfL2hw
|
||||
*/
|
||||
final class _NProcessorFinder extends ProcOpenBasedFinder
|
||||
{
|
||||
protected function getCommand(): string
|
||||
{
|
||||
return 'getconf _NPROCESSORS_ONLN';
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return '_NProcessorFinder';
|
||||
}
|
||||
}
|
||||
26
vendor/fidry/cpu-core-counter/src/NumberOfCpuCoreNotFound.php
vendored
Normal file
26
vendor/fidry/cpu-core-counter/src/NumberOfCpuCoreNotFound.php
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fidry CPUCounter Config package.
|
||||
*
|
||||
* (c) Théo FIDRY <theo.fidry@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fidry\CpuCoreCounter;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
final class NumberOfCpuCoreNotFound extends RuntimeException
|
||||
{
|
||||
public static function create(): self
|
||||
{
|
||||
return new self(
|
||||
'Could not find the number of CPU cores available.'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user