diff --git a/lib/libsys/Makefile.sys b/lib/libsys/Makefile.sys --- a/lib/libsys/Makefile.sys +++ b/lib/libsys/Makefile.sys @@ -301,6 +301,7 @@ rtprio.2 \ sched_get_priority_max.2 \ sched_getcpu.3 \ + sched_getaffinity.3 \ sched_setparam.2 \ sched_setscheduler.2 \ sched_yield.2 \ @@ -491,6 +492,7 @@ recv.2 recvmsg.2 MLINKS+=rename.2 renameat.2 MLINKS+=rtprio.2 rtprio_thread.2 +MLINKS+=sched_getaffinity.3 sched_setaffinity.3 MLINKS+=sched_get_priority_max.2 sched_get_priority_min.2 \ sched_get_priority_max.2 sched_rr_get_interval.2 MLINKS+=sched_setparam.2 sched_getparam.2 diff --git a/lib/libsys/sched_getaffinity.3 b/lib/libsys/sched_getaffinity.3 new file mode 100644 --- /dev/null +++ b/lib/libsys/sched_getaffinity.3 @@ -0,0 +1,89 @@ +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 2025 Felix Johnson +.Dd June 30, 2025 +.Dt SCHED_GETAFFINITY 3 +.Os +.Sh NAME +.Nm sched_getaffinity , +.Nm sched_setaffinity +.Nd get or set thread to CPU affinity +.Sh SYNOPSIS +.Lb libc +.Pp +.In sched.h +.Ft int +.Fo sched_getaffinity +.Fa "pid_t pid" +.Fa "size_t cpusetsz" +.Fa "cpuset_t *cpuset" +.Fc +.Pp +.Ft int +.Fo sched_setaffinity +.Fa "pid_t pid" +.Fa "size_t cpusetsz" +.Fa "const cpuset_t *cpuset" +.Fc +.Sh DESCRIPTION +These functions are convenience wrappers around +.Xr cpuset_getaffinity 3 +and +.Xr cpuset_setaffinity 3 . +.Pp +The +.Fn sched_getaffinity +function retrieves the current CPU affinity mask for +a thread or process. +If +.Fa pid +is 0, the calling thread is targeted. +If +.Fa pid +is greater than 0, it refers to a process ID. +In this case, the returned affinity mask is the union of +the CPU masks of all threads within the specified process. +.Pp +The +.Fn sched_setaffinity +function sets the CPU affinity mask for a given thread, +specifying which CPUs it is eligible to run on. +If +.Fa pid +is 0, the calling thread is targeted. +If +.Fa pid +is non-zero, it must be the thread ID of the target thread. +This function does not affect all threads in a process; +to change the affinity of all threads, the caller must +set each thread's mask individually. +.Pp +Affinity masks are represented using the +.Vt cpuset_t +type, which encodes a set of CPUs as a bitmask. +.Sh RETURN VALUES +.Rv -std sched_getaffinity sched_setaffinity +.Sh ERRORS +See +.Xr cpuset_getaffinity 3 +and +.Xr cpuset_setaffinity 3 +for a list of possible error codes. +.Sh SEE ALSO +.Xr cpuset 2 , +.Xr cpuset_getaffinity 2 , +.Xr cpuset_setaffinity 2 +.Sh STANDARDS +The +.Fn sched_getaffinity +and +.Fn sched_setaffinity +functions originated in Linux. +This implementation aims to be source-compatible with the Linux implementation. +.Sh HISTORY +The +.Fn sched_getaffinity +and +.Fn sched_setaffinity +functions were introduced in +.Fx 13.1 .