function provision_count_cpus

7.x-3.x provision.drush.inc provision_count_cpus()

determine the number of CPU on the machine

This tries a best guess at the number of CPUs running on the system. This is useful for calculating sane load threshold.

On Linux, this parses /proc/cpuinfo and looks for lines like this:

processor : 0 ... processor : 1 processor : n

The number of CPUs on the system is n+1, we just count the number of lines.

Other systems remain to be implemented, and would be best implemetend through a PECL (or similar) extension that would use the POSIX sysconf interface, as such:

ncpus = sysconf(_SC_NPROCESSORS_ONLN);

If no method can be found to figure out the number of CPUs, this will return FALSE.

People wishing to extend this to other platforms should look at suggestions at:

http://groups.google.com/group/sage-devel/browse_thread/thread/d65209f7a...

@todo implement for other systems than Linux

See also

provision_load_critical()

Related topics

1 call to provision_count_cpus()
provision_load_critical in ./provision.drush.inc
determine if overall load of the machine is critical

File

./provision.drush.inc, line 409
Provision Drush commands.

Code

function provision_count_cpus() {
  $ncpus = FALSE;
  if (is_readable("/data/all/cpuinfo")) {
    # this should work on BOA with a /data/all/cpuinfo generated daily
    $cpuinfo = (int) file_get_contents("/data/all/cpuinfo");
    if ($cpuinfo !== FALSE && is_numeric($cpuinfo)) {
      $ncpus = $cpuinfo;
    }
  }
  elseif (is_readable("/proc/cpuinfo")) {
    # this should work on Linux with a /proc filesystem
    $cpuinfo = file_get_contents("/proc/cpuinfo");
    if ($cpuinfo !== FALSE) {
      if (preg_match_all("/^processor.*:.*[0-9]+$/m", $cpuinfo, $matches)) {
        $ncpus = count(array_pop($matches));
      }
    }
  }
  return $ncpus;
}