Process affinity manipulation

Description

Function to get/set the process's CPU affinity mask. Affinity mask allows binding a process to a specific core(s) within the machine.

Usage

1
2
3
4
5
6
7
setaffinity(cpus = 1:ncores(), verbose = FALSE)

getaffinity_count(verbose = FALSE)

getaffinity(verbose = FALSE)

ncores()

Arguments

cpus

Which cpu cores to restrict to. Must be vector of integers between 1 and ncores.

verbose

print some C output (TRUE/FALSE)

Details

Setting a process afinity allows for restricting the process to only use certain cores in the machine. The cores are indexed by the operating system as 1 to ncores. One can lookup ncores using ncores(). Calling setaffinity with no arguments resets the process to use any of the available cores.

Note that setaffinity is different from setting r_limit values in the sense that it is not a one-way process. An unprivileged user can change the process affinity to any value. In order to 'lock' an affinity value, one would have to manipulate Linux capability value for CAP_SYS_NICE.

References

Jeroen Ooms (2013). The RAppArmor Package: Enforcing Security Policies in R Using Dynamic Sandboxing on Linux. Journal of Statistical Software, 55(7), 1-34. http://www.jstatsoft.org/v55/i07/.

Ubuntu Manpage: sched_setaffinity - set and get a process's CPU affinity mask. http://manpages.ubuntu.com/manpages/precise/man2/sched_setaffinity.2.html.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Not run: 
ncores()
getaffinity();
getaffinity_count();
setaffinity(1); #restricts the process to core number 1.
getaffinity();
setaffinity(); #reset
getaffinity();

## End(Not run)