4

Pretty straightforward question - is there a way to set the CPU affinity via PHP? Any of the following will do:

  • Setting the affinity of the current process via a PHP call.
  • Setting the affinity of a specific PID via a PHP call.
  • As a last resort, setting the affinity of a specific PID via a command-line tool.

The only option I've found so far is the last one, using a tool named schedutils, and it seems it may only be supported on more recent kernels.

Schedutils

Fabio
  • 23,183
  • 12
  • 55
  • 64
rinogo
  • 8,491
  • 12
  • 61
  • 102
  • 3
    Why would you want to do that? – Flexo Jun 09 '13 at 17:54
  • 2
    Why should PHP be screwing around with the web server in this way? – Ignacio Vazquez-Abrams Jun 09 '13 at 18:03
  • I've found that certain long-running tasks on our server become resource hogs. I'm exploring different options for effectively "niceing" a PHP script (beyond "nice" and CLI PHP, obviously :) ). If you have other ideas, I'd love to hear them! – rinogo Jun 10 '13 at 02:38

1 Answers1

9

The way to set the CPU affinity is with the sched_setaffinity C function. It's not available through the standard PHP API so you probably would have to write an extension.

An alternative is running taskset program through system. For example this binds the PHP process to cores 0 and 1:

system('taskset -cp 0,1 '.getmypid());
Joni
  • 108,737
  • 14
  • 143
  • 193