Number Of Available CPU Cores In PHP
It is simple enough to check and then load the number of available processing cores on the current machine into a PHP variable. You can do so with the code below, which uses shell commands to grab data from /proc/cpuinfo and then removes unneeded data with PHP.
<?php
/*How to check how many processors are available on the current machine.
by gordon@incero.com http://www.Incero.com. Feel free to use this code,
but keep this header. June 30th, 2010.*/
$numberOfProcessors=`cat /proc/cpuinfo | grep processor | tail -1`;
$numberOfProcessors=preg_replace('/\s+/', '',$numberOfProcessors);
$numberOfProcessors=str_replace(":","", $numberOfProcessors);
$numberOfProcessors=str_replace("processor","", $numberOfProcessors);
$numberOfProcessors++;
echo "Number of processors on this machine is $numberOfProcessors!";
?>
We use this code to help our programs assign the most efficient parameters to others processes, such as FFMpeg video conversions, that we spawn from PHP across various machines.
Tested on CentOS 5.4, should work on most Linux distributions.
Tags: cores, cpu, cpus, php, processor
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply