-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for limiting the number of CPU cores calcualted for parallelisation #129
Changes from all commits
e8e98a6
7d84d0f
5c5a0ea
cd8bf50
d3601d4
ac46d48
dda7f9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
namespace Fidry\CpuCoreCounter; | ||
|
||
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; | ||
use Fidry\CpuCoreCounter\Finder\EnvVariableFinder; | ||
use Fidry\CpuCoreCounter\Finder\FinderRegistry; | ||
|
||
final class CpuCoreCounter | ||
|
@@ -37,15 +38,27 @@ public function __construct(?array $finders = null) | |
} | ||
|
||
/** | ||
* @param positive-int $reservedCpus | ||
* @param positive-int $reservedCpus | ||
* @param positive-int|null $limit If no limit is given, all available CPUs will be returned. | ||
* | ||
* @return positive-int | ||
*/ | ||
public function getAvailableForParallelisation(int $reservedCpus = 1): int | ||
{ | ||
public function getAvailableForParallelisation( | ||
int $reservedCpus = 1, | ||
?int $limit = null | ||
): int { | ||
$limit = null === $limit | ||
? self::getKubernetesLimit() | ||
: $limit; | ||
|
||
$count = $this->getCountWithFallback(1); | ||
|
||
$availableCpus = $count - $reservedCpus; | ||
|
||
if (null !== $limit && $availableCpus > $limit) { | ||
$availableCpus = $limit; | ||
} | ||
|
||
return max(1, $availableCpus); | ||
} | ||
|
||
|
@@ -113,4 +126,11 @@ public function getFinderAndCores(): array | |
|
||
throw NumberOfCpuCoreNotFound::create(); | ||
} | ||
|
||
public static function getKubernetesLimit(): ?int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is Kubernetes the only environment with this issue? Maybe other infra orchestrators also apply here, so maybe more generic name like Anyway, I've just entered debug mode on our CI runner in Gitlab (which is run on Kubernetes) and I don't see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: being inside container on k8s it's possible to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
so far yes: I didn't see any other, and worst case you can always pass the
I will introduce such a method if there is more coming, meanwhile the current one is strictly about kubernetes. The new one would likely leverage the existing kubernetes one and potentially more I guess.
You tell me 😅 . I've never used kubernetes... so I don't know. It was my understanding from the reported issue that it was available as an environment variable as per the following quote:
|
||
{ | ||
$finder = new EnvVariableFinder('KUBERNETES_CPU_LIMIT'); | ||
|
||
return $finder->find(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?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; | ||
|
||
use function getenv; | ||
use function preg_match; | ||
use function sprintf; | ||
use function var_export; | ||
|
||
final class EnvVariableFinder implements CpuCoreFinder | ||
{ | ||
/** @var string */ | ||
private $environmentVariableName; | ||
|
||
public function __construct(string $environmentVariableName) | ||
{ | ||
$this->environmentVariableName = $environmentVariableName; | ||
} | ||
|
||
public function diagnose(): string | ||
{ | ||
$value = getenv($this->environmentVariableName); | ||
|
||
return sprintf( | ||
'parse(getenv(%s)=%s)=%s', | ||
$this->environmentVariableName, | ||
var_export($value, true), | ||
self::isPositiveInteger($value) ? $value : 'null' | ||
); | ||
} | ||
|
||
public function find(): ?int | ||
{ | ||
$value = getenv($this->environmentVariableName); | ||
|
||
return self::isPositiveInteger($value) | ||
? (int) $value | ||
: null; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'getenv(%s)', | ||
$this->environmentVariableName | ||
); | ||
} | ||
|
||
/** | ||
* @param string|false $value | ||
*/ | ||
private static function isPositiveInteger($value): bool | ||
{ | ||
return false !== $value | ||
&& 1 === preg_match('/^\d+$/', $value) | ||
&& (int) $value > 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.