CPU Core Counter for PHP

repository·main·Indexed 42 days ago

https://github.com/theofidry/cpu-core-counter

A lightweight PHP utility for detecting the number of logical or physical CPU cores on a system. It provides methods to retrieve core counts, determine cores available for parallelisation via ParallelisationResult, and supports custom finders and CLI diagnostic tools.

Tokens
1.4K
Snippets
6
Records
7
Agent score
4%

What's inside CPU Core Counter

  1. How logical and physical finders work

    main

    The FinderRegistry provides two ways to retrieve pre-configured lists of finders:

    • CpuCoreCounter::getDefaultLogicalFinders(): Returns an ordered list of finders that look for logical CPU cores (the default behavior).
    • CpuCoreCounter::getDefaultPhysicalFinders(): Returns an ordered list of finders that look for physical CPU cores.
    // Use physical finders instead of logical ones
    $finders = CpuCoreCounter::getDefaultPhysicalFinders();
    $counter = new CpuCoreCounter($finders);
    $physicalCores = $counter->getCount();
  2. Debug CPU core detection results

    main

    If the core count is unexpected, use these three methods to debug:

    1. CLI Tools: Use diagnose.php or execute.php (see Diagnose and execute finders via CLI) to see detailed finder output.
    2. Tracing: Use $counter->trace() to see the internal trace of the detection process.
    3. Parallelisation Inspection: Inspect the values returned by $counter->getAvailableForParallelisation() to understand how the parallelisation calculation was derived.
  3. Get CPU cores available for parallelisation

    main

    To determine how many cores are suitable for launching parallel processes, use getAvailableForParallelisation(). This returns a ParallelisationResult object containing the availableCpus property.

    $counter = new CpuCoreCounter();
    $availableCpus = $counter->getAvailableForParallelisation()->availableCpus;
  4. Get the number of CPU cores

    main

    Use the CpuCoreCounter class to retrieve the CPU core count. By default, it returns the count of logical cores. The result is memoized after the first call.

    There are three ways to handle potential failures (when no finder can determine the core count):

    1. Catching exceptions: Use getCount() and catch Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound.
    2. Custom finders: Pass a DummyCpuCoreFinder in the constructor to provide a fallback.
    3. Type-safe fallback: Use getCountWithFallback(int $fallback).
    use Fidry\CpuCoreCounter\CpuCoreCounter;
    use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
    use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
    
    $counter = new CpuCoreCounter();
    
    // 1. Catching exceptions
    try {
        $count = $counter->getCount();
    } catch (NumberOfCpuCoreNotFound) {
        $count = 1; // Fallback
    }
    
    // 2. Using a Dummy finder in constructor
    $counterWithFallback = new CpuCoreCounter([
        ...CpuCoreCounter::getDefaultFinders(),
        new DummyCpuCoreFinder(1),
    ]);
    $count = $counterWithFallback->getCount();
    
    // 3. Type-safe fallback method
    $count = $counter->getCountWithFallback(1);
  5. Configure custom finders

    main

    You can customize the CpuCoreCounter by passing a specific array of finders to the constructor. This allows you to change the search order or disable specific finders (e.g., removing WindowsWmicFinder).

    use Fidry\CpuCoreCounter\CpuCoreCounter;
    use Fidry\CpuCoreCounter\Finder\WindowsWmicFinder;
    use Fidry\CpuCoreCounter\Finder\CpuInfoFinder;
    use Fidry\CpuCoreCounter\Finder\HwLogicalFinder;
    
    // Example: Remove a specific finder
    $finders = array_filter(
        CpuCoreCounter::getDefaultFinders(),
        static fn ($finder) => !($finder instanceof WindowsWmicFinder)
    );
    $cores = (new CpuCoreCounter($finders))->getCount();
    
    // Example: Define a specific order
    $finders = [
        new CpuInfoFinder(),
        new WindowsWmicFinder(),
        new HwLogicalFinder(),
    ];
    $cores = (new CpuCoreCounter($finders))->getCount();
  6. Diagnose and execute finders via CLI

    main

    If you need to inspect how finders behave on your specific system, use the provided binary scripts located in your vendor directory.

    • Diagnose: Checks what each given finder will find on your system with details about the information it had.
    • Execute: Executes all finders and displays the result they found.
    # Diagnose the system
    ./vendor/fidry/cpu-core-counter/bin/diagnose.php
    
    # Execute all finders
    ./vendor/fidry/cpu-core-counter/bin/execute.php