Skip to content

Instantly share code, notes, and snippets.

@boutell
Created October 4, 2012 14:12
Show Gist options
  • Save boutell/3833761 to your computer and use it in GitHub Desktop.
Save boutell/3833761 to your computer and use it in GitHub Desktop.
Check distribution of fastcgi processes over the hyperthreads and cores of your server
<?php
// Are your fastcgi processes spread out evenly over the available hyperthreads on your server?
// This script will tell you.
//
// If your php-cgi process is not /usr/local/bin/php-cgi, tweak accordingly.
//
// In my experience they do tend to distribute pretty well over time, but see also
// assignhyperthreads.php.
//
// tom@punkave.com, @boutell, punkave.com
$pids = glob("/proc/*");
$hyperthreadsTotal = count(preg_grep('/^processor/', file('/proc/cpuinfo')));
echo("Total hyperthreads: $hyperthreadsTotal\n");
for ($i = 0; ($i < $hyperthreadsTotal); $i++)
{
$cpus[$i] = 0;
}
foreach ($pids as $pid)
{
$pid = basename($pid);
if (preg_match('/^\d+$/', $pid))
{
$cmd = file_get_contents("/proc/$pid/cmdline");
$elements = explode("\x00", $cmd);
if (count($elements))
{
if ($elements[0] === '/usr/local/bin/php-cgi')
{
$stat = file_get_contents("/proc/$pid/stat");
$columns = explode(' ', $stat);
$cpu = $columns[38];
$cpus[$cpu]++;
$total++;
}
}
}
}
echo("Total fastcgi processes: $total\n");
for ($i = 0; ($i < $hyperthreadsTotal); $i++)
{
echo($cpus[$i] . "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment