PHPackages                             fidry/cpu-core-counter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fidry/cpu-core-counter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fidry/cpu-core-counter
======================

Tiny utility to get the number of CPU cores.

1.3.0(9mo ago)238148.2M—5.6%5[4 issues](https://github.com/theofidry/cpu-core-counter/issues)19MITPHPPHP ^7.2 || ^8.0CI passing

Since Nov 30Pushed today2 watchersCompare

[ Source](https://github.com/theofidry/cpu-core-counter)[ Packagist](https://packagist.org/packages/fidry/cpu-core-counter)[ GitHub Sponsors](https://github.com/theofidry)[ RSS](/packages/fidry-cpu-core-counter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (19)Used By (19)

CPU Core Counter
================

[](#cpu-core-counter)

This package is a tiny utility to get the number of CPU cores.

```
composer require fidry/cpu-core-counter
```

Usage
-----

[](#usage)

```
use Fidry\CpuCoreCounter\CpuCoreCounter;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;

$counter = new CpuCoreCounter();

// For knowing the number of cores you can use for launching parallel processes:
$counter->getAvailableForParallelisation()->availableCpus;

// Get the number of CPU cores (by default it will use the logical cores count):
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
]);

// A type-safe alternative form:
$counter->getCountWithFallback(1);

// Note that the result is memoized.
$counter->getCount();   // e.g. 8
```

Advanced usage
--------------

[](#advanced-usage)

### Changing the finders

[](#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

```
// Remove WindowsWmicFinder
$finders = array_filter(
    CpuCoreCounter::getDefaultFinders(),
    static fn (CpuCoreFinder $finder) => !($finder instanceof WindowsWmicFinder)
);

$cores = (new CpuCoreCounter($finders))->getCount();
```

```
// 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

[](#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

[](#checks-what-finders-find-what-on-your-system)

You have three scrips available that provides insight about what the finders can find:

```
# Checks what each given finder will find on your system with details about the
# information it had.
make diagnose                                     # From this repository
./vendor/fidry/cpu-core-counter/bin/diagnose.php  # From the library
```

And:

```
# Execute all finders and display the result they found.
make execute                                     # From this repository
./vendor/fidry/cpu-core-counter/bin/execute.php  # From the library
```

### Debug the results found

[](#debug-the-results-found)

You have 3 methods available to help you find out what happened:

1. If you are using the default configuration of finder registries, you can check the previous section which will provide plenty of information.
2. If what you are interested in is how many CPU cores were found, you can use the `CpuCoreCounter::trace()` method.
3. If what you are interested in is how the calculation of CPU cores available for parallelisation was done, you can inspect the values of `ParallelisationResult`returned by `CpuCoreCounter::getAvailableForParallelisation()`.

Backward Compatibility Promise (BCP)
------------------------------------

[](#backward-compatibility-promise-bcp)

The policy is for the major part following the same as [Symfony's one](https://symfony.com/doc/current/contributing/code/bc.html). 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
-------

[](#license)

This package is licensed using the MIT License.

Please have a look at [`LICENSE.md`](LICENSE.md).

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance81

Actively maintained with recent releases

Popularity70

Solid adoption and visibility

Community27

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~89 days

Recently: every ~241 days

Total

12

Last Release

278d ago

Major Versions

0.5.1 → 1.0.02023-09-17

PHP version history (2 changes)0.1.0PHP ^7.4 || ^8.0

0.3.0PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6fbf458a1f05f8ac5a3c8f6c5426abe81084b7077b24955747b09c35f6c983be?d=identicon)[theofidry](/maintainers/theofidry)

---

Top Contributors

[![theofidry](https://avatars.githubusercontent.com/u/5175937?v=4)](https://github.com/theofidry "theofidry (126 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (29 commits)")[![Chris53897](https://avatars.githubusercontent.com/u/7104259?v=4)](https://github.com/Chris53897 "Chris53897 (1 commits)")[![czukowski](https://avatars.githubusercontent.com/u/186792?v=4)](https://github.com/czukowski "czukowski (1 commits)")[![thgs](https://avatars.githubusercontent.com/u/8940963?v=4)](https://github.com/thgs "thgs (1 commits)")

---

Tags

coreCPU

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/fidry-cpu-core-counter/health.svg)

```
[![Health](https://phpackages.com/badges/fidry-cpu-core-counter/health.svg)](https://phpackages.com/packages/fidry-cpu-core-counter)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[typo3/cms-composer-installers

TYPO3 CMS Installers

6113.7M52](/packages/typo3-cms-composer-installers)[compwright/codeigniter-installers

Composer installers for CodeIgniter

2982.0k4](/packages/compwright-codeigniter-installers)[gilbertsoft/typo3-core-patches

Easily manage TYPO3 Core patches with Composer.

1453.1k](/packages/gilbertsoft-typo3-core-patches)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
