Skip to content

Commit b4616ee

Browse files
committed
Determine the number of cores and run make with --jobs option
1 parent 0f0b0b6 commit b4616ee

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/Building/UnixBuild.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __invoke(
5252
$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
5353
$output->writeln('<info>Configure complete</info>' . $optionsOutput);
5454

55-
$makeOutput = $this->make($targetPlatform, $downloadedPackage);
55+
$makeOutput = $this->make($targetPlatform, $downloadedPackage, $output);
5656
if ($output->isVeryVerbose()) {
5757
$output->writeln($makeOutput);
5858
}
@@ -93,10 +93,19 @@ private function configure(DownloadedPackage $downloadedPackage, array $configur
9393
);
9494
}
9595

96-
private function make(TargetPlatform $targetPlatform, DownloadedPackage $downloadedPackage): string
96+
private function make(TargetPlatform $targetPlatform, DownloadedPackage $downloadedPackage, OutputInterface $output): string
9797
{
98+
$makeCommand = ['make'];
99+
100+
if ($targetPlatform->makeParallelJobs === 1) {
101+
$output->writeln('Running make without parallelization - try providing -jN to PIE where N is the number of cores you have.');
102+
} else {
103+
$makeCommand[] = '--jobs';
104+
$makeCommand[] = $targetPlatform->makeParallelJobs;
105+
}
106+
98107
return Process::run(
99-
['make', '--jobs', $targetPlatform->makeParallelJobs],
108+
$makeCommand,
100109
$downloadedPackage->extractedSourcePath,
101110
self::MAKE_TIMEOUT_SECS,
102111
);

src/Platform/TargetPlatform.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
88

9+
use Php\Pie\Util\NumberOfCores;
910
use function array_key_exists;
1011
use function curl_version;
1112
use function explode;
@@ -124,10 +125,9 @@ public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null
124125
}
125126

126127
if ($makeParallelJobs === null) {
127-
$makeParallelJobs = 16; // @todo detect
128+
$makeParallelJobs = NumberOfCores::determine();
128129
}
129130

130-
131131
return new self(
132132
$os,
133133
$phpBinaryPath,

src/Util/NumberOfCores.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Pie\Util;
6+
7+
use Symfony\Component\Process\Process;
8+
9+
final class NumberOfCores
10+
{
11+
/** @psalm-suppress UnusedConstructor */
12+
private function __construct()
13+
{
14+
}
15+
16+
/**
17+
* @param list<string> $params
18+
*
19+
* @return positive-int
20+
*/
21+
private static function tryCommand(string $command, array $params): int|null
22+
{
23+
$whichProcess = new Process(['which', $command]);
24+
if ($whichProcess->run() === 0) {
25+
$commandPath = trim($whichProcess->getOutput());
26+
27+
$commandProcess = new Process([$commandPath, ...$params]);
28+
if ($commandProcess->run() === 0) {
29+
$commandResult = trim($commandProcess->getOutput());
30+
if (is_numeric($commandResult)) {
31+
$commandNumericResult = (int) $commandResult;
32+
33+
if ($commandNumericResult > 0) {
34+
return $commandNumericResult;
35+
}
36+
}
37+
}
38+
}
39+
40+
return null;
41+
}
42+
43+
/**
44+
* Try a few different ways of determining number of CPUs. If we can't guess
45+
* how many CPUs someone has, then default to 1 for safety.
46+
*
47+
* @return positive-int
48+
*/
49+
public static function determine(): int
50+
{
51+
$nproc = self::tryCommand('nproc', ['--all']);
52+
if ($nproc !== null) {
53+
return $nproc;
54+
}
55+
56+
$getconf = self::tryCommand('getconf', ['_NPROCESSORS_ONLN']);
57+
if ($getconf !== null) {
58+
return $getconf;
59+
}
60+
61+
$sysctl = self::tryCommand('sysctl', ['-n', 'hw.ncpu']);
62+
if ($sysctl !== null) {
63+
return $sysctl;
64+
}
65+
66+
// Default to 1 for safety
67+
return 1;
68+
}
69+
}

0 commit comments

Comments
 (0)