The details of getting and setting process CPU affinities varies greatly from system to system. Even among the different flavors of Unix there is very little in the way of a common interface to CPU affinities. The existing tools and libraries for setting CPU affinities are not very standardized, so that a technique for setting CPU affinities on one system may not work on another system with the same architecture.

This module seeks to do one thing and do it well: manipulate CPU affinities through a common interface on as many systems as possible, by any means necessary.

The module is composed of several subroutines, each one implementing a different technique to perform a CPU affinity operation. A technique might try to import a Perl module, run an external program that might be installed on your system, or invoke some C code to access your system libraries. Usually, a technique is applicable to only a single or small group of operating systems, and on any particular system, most of the techniques would fail. Regardless of your particular system and configuration, it is hoped that at least one of the techniques will work and you will be able to get and set the CPU affinities of your processes.

It is important that your PATH variable is set correctly so that this module can find any external programs on your system that can help it to manipulate CPU affinities (for example, taskset on Linux, cpuset on FreeBSD).

If $ENV{DEBUG} is set to a true value, this module will produce some output that may or may not be good for debugging.

The techniques for manipulating CPU affinities for Windows (including Cygwin) and Linux have been refined and tested pretty well. Some techniques applicable to BSD systems (particularly FreeBSD) and Solaris have been tested a little bit. The hope is that this module will include more techniques for more systems in future releases. See the "NOTE TO DEVELOPERS" below for information about how you can help.

MacOS, OpenBSD are explicitly not supported, as there does not appear to be any public interface for specifying the CPU affinity of a process directly on those platforms.

On NetBSD, getting and setting CPU affinity is supported only for the calling process, and, AFAICT, only when run as the super-user. Which is to say, you can do this:

Retrieves the current CPU affinity for the process with the specified process ID. In scalar context, returns a bit-mask of the CPUs that the process has affinity for, with the least significant bit denoting CPU #0. The return value is actually a Math::BigInt value, so it can store a bit mask on systems with an arbitrarily high number of CPUs.

In list context, returns a list of integers indicating the indices of the CPU that the process has affinity for.

So for example, if a process in an 8 core machine had affinity for cores # 2, 6, and 7, then in scalar context, getAffinity() would return

(1 << 2) | (1 << 6) | (1 << 7) ==> 196

and in list context, it would return

(2, 6, 7)

A return value of 0 or undef indicates an error such as an invalid process ID.

Sets the CPU affinity of a process to the specified processors. First argument is the process ID. The second argument is either a bitmask of the desired processors to assign to the PID, or an array reference with the index values of processors to assign to the PID.

As a special case, using a $bitmask value of -1 will clear the CPU affinities of a process -- setting the affinity to all available processors.

On some platforms, notably AIX and Irix, it is only possible to bind a process to a single CPU. If the processor mask argument to setAffinity specifies more than one processor (but less than the total number of processors in your system), then this function might only bind the process one of the specified processors.

None known, but they are likely to arise as this module makes a lot of assumptions about how to provide input and interpret output for many different system utilities on many different platforms. Please report a bug if you suspect this module of misusing any system utilities.

This module seeks to work for as many systems in as many configurations as possible. If you know of a tool, a function, a technique to set CPU affinities on a system -- any system, -- then let's include it in this module.