Skip to content

Commit

Permalink
Add lscpu Finder (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
thgs authored Dec 24, 2022
1 parent 040127e commit 3ae77c8
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 0 deletions.
2 changes: 2 additions & 0 deletions e2e/expected-output-osx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CpuInfoFinder: F
DummyCpuCoreFinder(value=1): .
HwLogicalFinder: .
HwPhysicalFinder: .
LscpuLogicalFinder: F
LscpuPhysicalFinder: F
_NProcessorFinder: .
NProcessorFinder: .
NProcFinder(all=true): F
Expand Down
2 changes: 2 additions & 0 deletions e2e/expected-output-ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CpuInfoFinder: .
DummyCpuCoreFinder(value=1): .
HwLogicalFinder: F
HwPhysicalFinder: F
LscpuLogicalFinder: .
LscpuPhysicalFinder: .
_NProcessorFinder: .
NProcessorFinder: F
NProcFinder(all=true): .
Expand Down
2 changes: 2 additions & 0 deletions e2e/expected-output-ubuntu-restricted
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CpuInfoFinder: F
DummyCpuCoreFinder(value=1): .
HwLogicalFinder: F
HwPhysicalFinder: F
LscpuLogicalFinder: F
LscpuPhysicalFinder: F
_NProcessorFinder: F
NProcessorFinder: F
NProcFinder(all=true): F
Expand Down
2 changes: 2 additions & 0 deletions e2e/expected-output-windows
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CpuInfoFinder: F
DummyCpuCoreFinder(value=1): .
HwLogicalFinder: F
HwPhysicalFinder: F
LscpuLogicalFinder: F
LscpuPhysicalFinder: F
_NProcessorFinder: .
NProcessorFinder: F
NProcFinder(all=true): F
Expand Down
4 changes: 4 additions & 0 deletions src/Finder/FinderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static function getAllVariants(): array
new DummyCpuCoreFinder(1),
new HwLogicalFinder(),
new HwPhysicalFinder(),
new LscpuLogicalFinder(),
new LscpuPhysicalFinder(),
new _NProcessorFinder(),
new NProcessorFinder(),
new NProcFinder(true),
Expand Down Expand Up @@ -53,6 +55,7 @@ public static function getDefaultLogicalFinders(): array
new _NProcessorFinder(),
new NProcessorFinder(),
new CpuInfoFinder(),
new LscpuLogicalFinder(),
];
}

Expand All @@ -64,6 +67,7 @@ public static function getDefaultPhysicalFinders(): array
return [
OnlyOnOSFamilyFinder::forWindows(new WmicPhysicalFinder()),
new HwPhysicalFinder(),
new LscpuPhysicalFinder(),
];
}

Expand Down
47 changes: 47 additions & 0 deletions src/Finder/LscpuLogicalFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* 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;

/**
* 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 $lscpu): ?int
{
$lines = explode(PHP_EOL, $lscpu);

$actualLines = preg_grep('/^[0-9]+\,/', $lines);

if (false === $actualLines) {
return null;
}

$count = count($actualLines);

return 0 === $count ? null : $count;
}

public function toString(): string
{
return 'LscpuLogicalFinder';
}
}
61 changes: 61 additions & 0 deletions src/Finder/LscpuPhysicalFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* 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;

/**
* 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 $lscpu): ?int
{
$lines = explode(PHP_EOL, $lscpu);

$actualLines = preg_grep('/^[0-9]+\,/', $lines);

if (false === $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;
}
}
125 changes: 125 additions & 0 deletions tests/Finder/LscpuLogicalFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* 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\Test\Finder;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder;
use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder
*
* @internal
*/
final class LscpuLogicalFinderTest extends TestCase
{
/**
* @var DummyExecutor
*/
protected $executor;

protected function setUp(): void
{
$this->executor = new DummyExecutor();
}

protected function tearDown(): void
{
unset($this->executor);
}

/**
* @dataProvider finderProvider
*/
public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void
{
$actual = $finder->toString();

self::assertSame($expected, $actual);
}

public static function finderProvider(): iterable
{
yield [
new LscpuLogicalFinder(),
FinderShortClassName::get(new LscpuLogicalFinder())
];
}

/**
* @dataProvider lscpuProvider
*/
public function test_it_can_count_the_number_of_cpu_cores(
?array $processResult,
?int $expected
): void {
$finder = new LscpuLogicalFinder($this->executor);
$this->executor->setOutput($processResult);

$actual = $finder->find();

self::assertSame($expected, $actual);
}

public static function lscpuProvider(): iterable
{
yield 'example with four logical but two physical' => [
[
<<<'EOF'
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core,Socket,Node,,L1d,L1i,L2,L3
0,0,0,0,,0,0,0,0
1,1,0,0,,1,1,1,0
2,0,0,0,,0,0,0,0
3,1,0,0,,1,1,1,0

EOF
,
''
],
4
];

yield 'example with two cores' => [
[
<<<'EOF'
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core,Socket,Node,,L1d,L1i,L2
0,0,0,0,,0,0,0
1,1,0,0,,1,1,1

EOF
,
''
],
2
];

yield 'handling lscpu failure' => [
[
'',
<<<'EOF'
lscpu: failed to determine number of CPUs: /sys/devices/system/cpu/possible: No such file or directory

EOF
],
null
];
}
}
Loading

0 comments on commit 3ae77c8

Please sign in to comment.