CPUSET_GETAFFINITY(2)
π CPUSET_GETAFFINITY(2) β the CPU affinity girlies' manual π
okay so bestie, welcome to the System Calls Manual corner of the internet, where we talk about cpuset_getaffinity and cpuset_setaffinity β the two functions that literally decide which CPUs your little guys get to run on. no cap. let's get into it π¦
β¨ NAME β¨
cpuset_getaffinity, cpuset_setaffinity β manage CPU affinity π
π LIBRARY π
Standard C Library (libc, -lc) β she's built in, you don't gotta link anything extra π«Ά
πΈ SYNOPSIS πΈ
#include <sys/param.h>
#include <sys/cpuset.h>
int
cpuset_getaffinity(cpulevel_t level, cpuwhich_t which, id_t id,
size_t setsize, cpuset_t *mask);
int
cpuset_setaffinity(cpulevel_t level, cpuwhich_t which, id_t id,
size_t setsize, const cpuset_t *mask);
like. look at those signatures. const on the setter's mask because she would NEVER modify your mask, she's respectful π
π« DESCRIPTION π«
okay so cpuset_getaffinity() and cpuset_setaffinity() let you mess with sets of CPUs available to processes, threads, interrupts, jails and other resources. iconic range honestly. these functions can manipulate sets of CPUs that hold many processes, OR per-object anonymous masks that only affect a single object. both vibes are valid π
the valid values for the level and which arguments live over in cpuset(2) β go read her, she's the source of truth. those two arguments are basically telling the kernel which object and which set of that object we're talking about. not all possible combinations are valid tho!! for example: only processes may belong to a numbered set accessed by a level argument of CPU_LEVEL_CPUSET. but all resources have a mask that may be manipulated with CPU_LEVEL_WHICH. periodt.
masks of type cpuset_t get composed using the CPU_SET macros. the kernel is chill and tolerates large sets β as long as all the CPUs specified in the set actually exist. but sets smaller than the kernel uses? yeah that generates an error on calls to cpuset_getaffinity(), even if the result set would fit inside the set you supplied. rude? a little. calls to cpuset_setaffinity() on the other hand tolerate small sets with no restrictions, so she's the forgiving sister π
the suppliedmaskshould have a size ofsetsizebytes. this size is usually provided by callingsizeof(mask), which is ultimately determined by the value ofCPU_SETSIZEas defined in<sys/cpuset.h>. don't skip this bestie, the size matters fr fr.
cpuset_getaffinity()β retrieves the mask from the object specified bylevel,whichandid, and stores it in the space provided bymask. it's giving "read the room, then report back" πcpuset_setaffinity()β attempts to set the mask for the object specified bylevel,whichandidto the value inmask. she's giving main character energy, making decisions π
π₯ 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.
π ERRORS π
the following error codes may be set in errno:
[EINVAL]β thelevelorwhichargument was not a valid value. π[EINVAL]β themaskargument specified when callingcpuset_setaffinity()was not a valid value.[EDEADLK]β thecpuset_setaffinity()call would leave a thread without a valid CPU to run on, because the set does not overlap with the thread's anonymous mask. bestie. that's a whole crisis. do not do that.[EFAULT]β themaskpointer passed was invalid. π«[ESRCH]β the object specified by theidandwhicharguments could not be found. she's not here, she ghosted.[ERANGE]β thecpusetsizewas either preposterously large or smaller than the kernel set size. preposterously is the actual word and i'm obsessed with it.[EPERM]β the calling process did not have the credentials required to complete the operation. no permission, no party π ββοΈ[ECAPMODE]β the calling process attempted to act on a process other than itself, while in capability mode. seecapsicum(4). that's a boundary violation and we don't do those here.
π SEE ALSO π
capsicum(4), cpuset(1), cpuset(2), cpuset_getid(2), cpuset_setid(2), cpuset_getdomain(2), cpuset_setdomain(2), pthread_affinity_np(3), pthread_attr_affinity_np(3), cpuset(9) β the whole friend group, go say hi π«Ά
π HISTORY π
the cpuset_getaffinity family of system calls first appeared in FreeBSD 7.1. an elder. a legend. respect her π
π AUTHORS π
Jeffrey Roberson <jeff@FreeBSD.org> β thank you for your service king π«‘
and that's the whole page bestie, no notes. this was the Debian build of the CPUSET_GETAFFINITY(2) page, dated May 23, 2017, and honestly? she ate. go set some affinities. love u, bye πβ¨π¦
π¦ 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