Skip to content

Instantly share code, notes, and snippets.

@boutell
Created October 4, 2012 14:31
Show Gist options
  • Save boutell/3833886 to your computer and use it in GitHub Desktop.
Save boutell/3833886 to your computer and use it in GitHub Desktop.
Spread out your fastcgi processes among the hyperthreads and cores of your CPU
<?php
// This script will spread out your php-cgi fastcgi processes
// evenly over the hyperthreads of your server.
// If your php-cgi process is not /usr/local/bin/php-cgi,
// tweak accordingly.
//
// [email protected], @boutell, punkave.com
$hyperthreadsTotal = count(preg_grep('/^processor/', file('/proc/cpuinfo')));
echo("Total hyperthreads: $hyperthreadsTotal\n");
$pids = glob("/proc/*");
$hyperthread = 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')
{
system("taskset -p -c $hyperthread $pid");
$total++;
$hyperthread++;
$hyperthread %= $hyperthreadsTotal;
}
}
}
}
echo("Total fastcgi processes assigned to hyperthreads: $total\n\n");
echo("*** YOU WILL NOT SEE A DIFFERENCE IN checkhyperthreads.php\n");
echo("*** until these processes ACTUALLY RESPOND TO REQUESTS.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment