CPUSET(2)
β¨ cpuset, cpuset_getid, cpuset_setid β manage CPU affinity sets β¨
okay so bestie, welcome to CPUSET(2), straight out of the System Calls Manual (that's section 2, the syscall girlies π ). this is the page where we learn how to tell your processes and threads exactly which CPUs and memory domains they're allowed to vibe on. it's giving scheduler sleepover party, and you're the one handing out the invites π
π LIBRARY π
Standard C Library (libc, -lc) β yeah, she's already linked in, no extra work required π«Ά
πΈ SYNOPSIS πΈ
#include <sys/param.h>
#include <sys/cpuset.h>
int
cpuset(cpusetid_t *setid);
int
cpuset_setid(cpuwhich_t which, id_t id, cpusetid_t setid);
int
cpuset_getid(cpulevel_t level, cpuwhich_t which, id_t id,
cpusetid_t *setid);
π¦ DESCRIPTION π¦
so the cpuset family of system calls lets your app control sets of processors and memory domains and then assign processes and threads to those sets. think of it like making little friend groups, but the friend groups are CPUs π
a processor set contains lists of CPUs and domains that its members are allowed to run on, and it exists only as long as some process is a member of the set. no members? she's gone, ghosted, poof π
every single process in the system has an assigned set. the default set for all processes in the system is the set numbered 1. threads belong to the same set as the process that contains them β however (and this is the fun part), they can further restrict their set with the anonymous per-thread mask to bind to a specific CPU or a subset of CPUs and memory domains. narrowing down your circle, iconic behavior β¨
sets are referenced by a number of type cpuset_id_t. every thread has three things:
- a root set
- an assigned set
- an anonymous mask
only the root and assigned sets are numbered. the root set is the set of all CPUs and memory domains available in the system, or in the system partition the thread is running in. the assigned set is a subset of the root set and is administratively assignable on a per-process basis. many processes and threads may be members of one numbered set β it's a group chat, not a DM π¬
the anonymous set is a further thread-specific refinement on top of the assigned set. the intended division of labor is:
- administrators manipulate numbered sets using
cpuset(1) - application developers manipulate anonymous sets using
cpuset_setaffinity(2)andcpuset_setdomain(2)
to select the correct set you pass a value of type cpulevel_t. the following values for level are supported:
CPU_LEVEL_ROOTβ root setCPU_LEVEL_CPUSETβ assigned setCPU_LEVEL_WHICHβ the set specified by the which argument
the which argument determines how the value of id gets interpreted, and it's of type cpuwhich_t. which may have the following values:
CPU_WHICH_TIDβ id islwpid_t(thread id)CPU_WHICH_PIDβ id ispid_t(process id)CPU_WHICH_JAILβ id isjid(jail id)CPU_WHICH_CPUSETβ id is acpusetid_t(cpuset id)CPU_WHICH_IRQβ id is an irq numberCPU_WHICH_INTRHANDLERβ id is an irq number for an interrupt handlerCPU_WHICH_ITHREADβ id is an irq number for an ithreadCPU_WHICH_DOMAINβ id is a NUMA domain
an id of '-1' may be used with a which of CPU_WHICH_TID, CPU_WHICH_PID, or CPU_WHICH_CPUSET to mean the current thread, process, or current thread's cpuset. all cpuset syscalls allow this usage. so convenient, we love a shortcut π«Ά
a level argument of CPU_LEVEL_WHICH combined with a which argument other than CPU_WHICH_CPUSET refers to the anonymous mask of the object. this mask does not have an id and may only be manipulated with cpuset_setaffinity(2).
π« the three calls, one by one π«
cpuset() β creates a new set containing the same CPUs as the root set of the current process, and stores its id in the space provided by setid. on successful completion the calling process joins the set and is the only member. children inherit this set after a call to fork(2). it's giving "new group chat, just me and my CPUs" π
cpuset_setid() β attempts to set the id of the object specified by the which argument. currently CPU_WHICH_PID is the only acceptable value for which, because threads don't have an id distinct from their process and the API does not permit changing the id of an existing set. upon successful completion, all of the threads in the target process will be running on CPUs permitted by the set.
cpuset_getid() β retrieves a set id from the object indicated by which and stores it in the space pointed to by setid. the retrieved id may be that of either the root or the assigned set depending on the value of level. level should be CPU_LEVEL_CPUSET or CPU_LEVEL_ROOT to get the set id from the process or thread specified by the id argument. specifying CPU_LEVEL_WHICH with a process or thread is unsupported, since this references the unnumbered anonymous mask. no id, no entry bestie π
the actual contents of the sets may be retrieved or manipulated using cpuset_getaffinity(2), cpuset_setaffinity(2), cpuset_getdomain(2), and cpuset_setdomain(2). go read those manual pages for more detail, they're the real main characters of this saga πβ¨
β RETURN VALUES β
upon successful completion, the value 0 is returned. otherwise, the value -1 is returned and the global variable errno is set to indicate the error. classic. slay when it works, scream when it doesn't π
π ERRORS π
the following error codes may be set in errno:
[EINVAL]β the which or level argument was not a valid value.[EDEADLK]β thecpuset_setid()call would leave a thread without a valid CPU to run on because the set does not overlap with the thread's anonymous mask.[EFAULT]β the setid pointer passed tocpuset_getid()orcpuset()was invalid.[ESRCH]β the object specified by the id and which arguments could not be found.[EPERM]β the calling process did not have the credentials required to complete the operation.[ENFILE]β there was no freecpusetid_tfor allocation.
ok real talk, that [EDEADLK] one? that's the "you just locked your thread out of every CPU it could run on" error. dramatic, but deserved. don't do that π
π SEE ALSO π
cpuset(1)cpuset_getaffinity(2)cpuset_setaffinity(2)cpuset_getdomain(2)cpuset_setdomain(2)pthread_affinity_np(3)pthread_attr_affinity_np(3)cpuset(9)
π°οΈ HISTORY π°οΈ
the cpuset family of system calls first appeared in FreeBSD 7.1. a legend was born β¨
π AUTHORS π
Jeffrey Roberson <jeff@FreeBSD.org> β thank you for your service, king π«‘π
and that's the whole page, bestie!! this cutie comes to you from Debian, dated May 3, 2017 β go forth and pin your threads to exactly the right CPU, you've got this π π¦ππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2008 Christian Brueffer
- Copyright (c) 2008 Jeffrey Roberson
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms