How to calculate number of processor cores in PHP script (linux)?

php get cpu usage
linux cpu count
how to increase cpu cores in linux
cpu number command
how to check number of cores in linux
linux command line cores
linux bash number of processors
how to calculate cpu utilization in linux

I'm trying to use pthreads for multithreading. I'm creating pool with constructor. First parameter is number of Workers.

$pool = new Pool(8, 'WebWorker');

I want to detect count of processor cores automatically. Something like this:

$pool = new Pool(get_processor_cores_number(), 'WebWorker');

How is it possible with PHP?

You can do something like that, of course your server should be in linux:

function get_processor_cores_number() {
    $command = "cat /proc/cpuinfo | grep processor | wc -l";

    return  (int) shell_exec($command);

You will execute a shell command then cast it to int.

Function to detect number of CPUs in PHP · GitHub, Function to detect number of CPUs in PHP. num-cpus.php CPU cores. *. * Should work for Linux, Windows, Mac & BSD $numCpus = count($matches[0]);. }. Check how many CPUs are there in Linux system; Linux Find If Processor (CPU) is 64 bit / 32 bit [long mode ~ lm] Linux Find Out CPU Architecture Information; Linux Hotplug a CPU and Disable CPU Cores At Run Time; Linux Command To Find the System Configuration And Hardware Information

If the server is a Linux machine you can do it with the following snippet:

$ncpu = 1;

if(is_file('/proc/cpuinfo')) {
    $cpuinfo = file_get_contents('/proc/cpuinfo');
    preg_match_all('/^processor/m', $cpuinfo, $matches);
    $ncpu = count($matches[0]);

[PHP]How to get the number of CPU cores in Fedora, Ubuntu, and , In short, there is no library or function to do it. Fortunately, we can do it via shell command. In the other words, the following methods are not  In my own library, I have a function for this among other things. You can easily modify it where it uses nonstandard PHP functions. For instance, I cache the result so you can ignore that.

Here is an extension exposing sysconf: krakjoe/sysconf

$cpusConfigured = sysconf(SYSCONF_NPROCESSORS_CONF);
$cpusOnline     = sysconf(SYSCONF_NPROCESSORS_ONLN);

Most applications only care about the number configured.

Linux Display CPU Information Number of CPUs and Their Speed , Explains how to determine the number of CPUs (sockets), cores, and threads on Linux using lscpu, /proc/cpuinfo and other commands. Each core number position on a numerology chart represents energy that has a high likelihood of substantial influence and effect. Unless a specialized reading is the purpose, numerology readings are based on the core numbers. They are the foundation. Without the core, the rest would have little foundation upon which to be expressed.

In my own library, I have a function for this among other things. You can easily modify it where it uses nonstandard PHP functions.

For instance, I cache the result so you can ignore that. Then I have functions to check if we are running on Linux, Mac or Windows. You could insert a similar check of your own there. For executing the actual system specific check I use my own Process class which allows things such as reconnecting to running processes on subsequent requests to check status, output etc. But you can change that and just use exec.

public static function getNumberOfLogicalCPUCores() {
    $numCores = CacheManager::getInstance()->fetch('System_NumberOfLogicalCPUCores');
    if(!$numCores) {
        if(System::isLinux() || System::isMac()) {
            $getNumCoresProcess = new Process("grep -c ^processor /proc/cpuinfo");
            $numCores = (int)$getNumCoresProcess->getStdOut();
        else if(System::isWindows()) {
            // Extract system information
            $getNumCoresProcess = new Process("wmic computersystem get NumberOfLogicalProcessors");
            $output = $getNumCoresProcess->getStdOut();
            // Lazy way to avoid doing a regular expression since there is only one integer in the output on line 2. 
            // Since line 1 is only text "NumberOfLogicalProcessors" that will equal 0.
            // Thus, 0 + CORE_COUNT, which equals CORE_COUNT, will be retrieved.
            $numCores = array_sum($getNumCoresProcess->getStdOutAsArray());
        else {
            // Assume one core if this is some unkown OS
            $numCores = 1;
        CacheManager::getInstance()->store('System_NumberOfLogicalCPUCores', $numCores);

    return $numCores;

How to determine number of CPUs on Linux using command line , an 8-core Apple Mac Pro or about 15 min with a 16 node/32 processor Linux An example script for running a CYANA calculation with the automated NOE on the CYANA homepage (http:// ← SQL Server – Backup Verification Script – Based on conditions T-SQL Script to Check the Native Compressed backup file size and Backup Compression Ratio → 4 thoughts on “ Powershell Script to Check – Number of Cores and Logical Processors ” Gerard January 7, 2016 at 11:08 am. Great input! Thanks. jake June 9, 2016 at 5:47 pm.

Fragment Based Drug Design: Tools, Practical Approaches, and Examples, Can I run a multi-threaded program and specify how many cores should be used: 1, 2, This is another way you can map it if you know your CPU architecture well. the value of t from the command line, you can specify the number of threads to be used Answer : To display the number of virtual processor(cpu) on linux vps, you have to run any of this command : 1. This command will display exact number of virt Skip to content

Is there a way to specify how many cores a program should run, Method-1: How to find the number Of CPUs on Linux using the “/proc/cpuinfo” file You can only get a number of CPUs by using the grep command as follows.

14 Methods To Check the Number Of CPUs in Linux, a controller node, a management network (switch) and at least two compute nodes. For controller nodes, each server blade is equipped with processor Xeon including 16 cores (2.93 GHz), 24GB Linux is installed for all servers as OS. uses non-shared file systems [16] with transferring image via SSH for test purpose.

  • PHP doesnt have any build-in capability for this purpose?
  • I don't know any native php function able to achieve that.
  • cat /proc/cpuinfo | grep processor | wc -l is utter ugly. you can do the same with a single process instead of three and two pipes: grep -c processor /proc/cpuinfo
  • this should be faster: $ncpu = substr_count((string)@file_get_contents('/proc/cpuinfo'),"\nprocessor")+1; - first off substr_count is faster than preg_match_all, and we only do a single open() instead of 2, and if the file is unreadable, substr_count returns 0 and the +1 turns 0 into 1, but when the file is readable, substr_count will skip the first processor because it does not start with a newline, and the +1 accounts for that too :)
  • for linux, return (int)(rtrim(shell_exec('nproc'))); should suffice
  • @hanshenrik: Is that a standard command I can trust to be available in all flavors?
  • yes, pretty much all linux systems has this, because it's a part of GNU Coreutils, and all mainstream linux distros i can think of ship with GNU Coreutils (probably not Slitaz Linux Floppy, but that's like the only exception i can think of..)
  • nproc, being part of coreutils, is more likely to be available than grep, because grep is not part of coreutils.
  • @hanshenrik: Thanx, great. I'll update my library to use this approach instead then.