FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Get Number of Running Proccesses with PHP

Recently I had to setup a script to curl 10k urls, but it could only do 500 requests at any one time. 501 requests would cause a 503 server error, or even a 500. In order to work under that limit, I created a function that returns the number of currently running processes on the machine in an extremely fast and efficient way.

How it works

Apache Httpd Number Processes

It gets the number of running processes in the most efficient way possible, by doing a simple stat on the /proc directory for the number of hard links. In unix, each hard link in the /proc directory corresponds to a running process. So /proc/1234 would be a hard link for the process id 1234. Since each process has a unique process ID it has a corresponding hard link.

Pros and Cons

The upside to this method is that it is incredibly fast and efficient. The downside is it won't tell you how many of those processes are php, httpd, exim, sshd, etc.

Equivalent Unix Command

This command will give you the same thing from the command line, such as from the Bash shell.

$ stat -c '%h' /proc

Get Process Count

Here is the function. Note it is for php 5.3.0, though if you are running less than that it is no big deal in terms of performance, this baby is super quick.

/** askapache_get_process_count()
 * Returns the number of running processes
 *
 * @version 1.4
 *
 * @return int
 */
function askapache_get_process_count() {
  static $ver, $runs = 0;
  
  // check if php version supports clearstatcache params, but only check once
  if ( is_null( $ver ) )
    $ver = version_compare( PHP_VERSION, '5.3.0', '>=' );

  // Only call clearstatcache() if function called more than once */
  if ( $runs++ > 0 ) { // checks if $runs > 0, then increments $runs by one.
    
    // if php version is >= 5.3.0
    if ( $ver ) {
      clearstatcache( true, '/proc' );
    } else {
      // if php version is < 5.3.0
      clearstatcache();
    }
  }
  
  $stat = stat( '/proc' );

  // if stat succeeds and nlink value is present return it, otherwise return 0
  return ( ( false !== $stat && isset( $stat[3] ) ) ? $stat[3] : 0 );
}

Example - Sleep 5 seconds until processes less than max

This example will continually sleep for 5 seconds until the process count is less than the max_procs.

$max_procs = 200;
$proc_count = askapache_get_process_count();

do {
    $proc_count = askapache_get_process_count();
    error_log( "ALERT!! procs > max_procs:  {$proc_count} > {$max_procs}.. SLEEP FOR 5 SECS " );
    sleep( 5 );
} while ( $proc_count > $max_procs );

clearstatcache - Clears file status cache

void clearstatcache ([ bool $clear_realpath_cache = false [, string $filename ]] )

Description

clearstatcache

When you use stat(), lstat(), or any of the other functions listed in the affected functions list (below), PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. For instance, if the same file is being checked multiple times within a single script, and that file is in danger of being removed or changed during that script's operation, you may elect to clear the status cache. In these cases, you can use the clearstatcache() function to clear the information that PHP caches about a file.

You should also note that PHP doesn't cache information about non-existent files. So, if you call file_exists() on a file that doesn't exist, it will return FALSE until you create the file. If you create the file, it will return TRUE even if you then delete the file. However unlink() clears the cache automatically.

Note:
This function caches information about specific filenames, so you only need to call clearstatcache() if you are performing multiple operations on the same filename and require the information about that particular file to not be cached.

Parameters

clear_realpath_cache
Whether to clear the realpath cache or not.
filename
Clear the realpath and the stat cache for a specific filename only; only used if clear_realpath_cache is TRUE.

Return Values

No value is returned.

stat - Gives information about a file

array stat ( string $filename )

Description

stat

Gathers the statistics of the file named by filename. If filename is a symbolic link, statistics are from the file itself, not the symlink.

lstat() is identical to stat() except it would instead be based off the symlinks status.

Parameters

filename
Path to the file.

Return Values

NumericAssociative (since PHP 4.0.6)Description
0devdevice number
1inoinode number *
2modeinode protection mode
3nlinknumber of links
4uiduserid of owner *
5gidgroupid of owner *
6rdevdevice type, if inode device
7sizesize in bytes
8atimetime of last access (Unix timestamp)
9mtimetime of last modification (Unix timestamp)
10ctimetime of last inode change (Unix timestamp)
11blksizeblocksize of filesystem IO **
12blocksnumber of 512-byte blocks allocated **

* On Windows this will always be 0.
** Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1.

In case of error, stat() returns FALSE.

Note:
Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

PHP /proc clearstatcache Hard link PHP Process stat version_compare

 

 

Comments