1*9018f13eSnjoly.\" $NetBSD: sched.3,v 1.18 2016/09/15 07:53:59 njoly Exp $ 22ac12931Srmind.\" 3f87fbe2fSchristos.\" Copyright (c) 2008, 2016 The NetBSD Foundation, Inc. 42ac12931Srmind.\" All rights reserved. 52ac12931Srmind.\" 62ac12931Srmind.\" This code is derived from software contributed to The NetBSD Foundation 72ac12931Srmind.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>. 82ac12931Srmind.\" 92ac12931Srmind.\" Redistribution and use in source and binary forms, with or without 102ac12931Srmind.\" modification, are permitted provided that the following conditions 112ac12931Srmind.\" are met: 122ac12931Srmind.\" 1. Redistributions of source code must retain the above copyright 132ac12931Srmind.\" notice, this list of conditions and the following disclaimer. 142ac12931Srmind.\" 2. Redistributions in binary form must reproduce the above copyright 152ac12931Srmind.\" notice, this list of conditions and the following disclaimer in the 162ac12931Srmind.\" documentation and/or other materials provided with the distribution. 172ac12931Srmind.\" 182ac12931Srmind.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 192ac12931Srmind.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 202ac12931Srmind.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 212ac12931Srmind.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 222ac12931Srmind.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 232ac12931Srmind.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 242ac12931Srmind.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 252ac12931Srmind.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 262ac12931Srmind.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 272ac12931Srmind.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 282ac12931Srmind.\" POSSIBILITY OF SUCH DAMAGE. 292ac12931Srmind.\" 30f87fbe2fSchristos.Dd August 5, 2016 312ac12931Srmind.Dt SCHED 3 322ac12931Srmind.Os 332ac12931Srmind.Sh NAME 342ac12931Srmind.Nm sched_setparam , 352ac12931Srmind.Nm sched_getparam , 362ac12931Srmind.Nm sched_setscheduler , 372ac12931Srmind.Nm sched_getscheduler , 382ac12931Srmind.Nm sched_get_priority_max , 392ac12931Srmind.Nm sched_get_priority_min , 402ac12931Srmind.Nm sched_rr_get_interval , 411f9c8df1Schristos.Nm sched_yield , 42f87fbe2fSchristos.Nm sched_protect , 43f87fbe2fSchristos.Nm sched_setaffinity_np , 44f87fbe2fSchristos.Nm sched_getaffinity_np 452ac12931Srmind.Nd process scheduling 462ac12931Srmind.Sh LIBRARY 472ac12931Srmind.Lb librt 482ac12931Srmind.Sh SYNOPSIS 492ac12931Srmind.In sched.h 502ac12931Srmind.Ft int 512ac12931Srmind.Fn sched_setparam "pid_t pid" "const struct sched_param *param" 522ac12931Srmind.Ft int 532ac12931Srmind.Fn sched_getparam "pid_t pid" "struct sched_param *param" 542ac12931Srmind.Ft int 552ac12931Srmind.Fn sched_setscheduler "pid_t pid" "int policy" "const struct sched_param *param" 562ac12931Srmind.Ft int 572ac12931Srmind.Fn sched_getscheduler "pid_t pid" 582ac12931Srmind.Ft int 592ac12931Srmind.Fn sched_get_priority_max "int policy" 602ac12931Srmind.Ft int 612ac12931Srmind.Fn sched_get_priority_min "int policy" 622ac12931Srmind.Ft int 632ac12931Srmind.Fn sched_rr_get_interval "pid_t pid" "struct timespec *interval" 642ac12931Srmind.Ft int 652ac12931Srmind.Fn sched_yield "void" 661f9c8df1Schristos.Ft int 67*9018f13eSnjoly.Fn sched_setaffinity_np "pid_t pid" "size_t size" "cpuset_t *cpuset" 68f87fbe2fSchristos.Ft int 69*9018f13eSnjoly.Fn sched_getaffinity_np "pid_t pid" "size_t size" "cpuset_t *cpuset" 70f87fbe2fSchristos.Ft int 711f9c8df1Schristos.Fn sched_protect "int priority" 722ac12931Srmind.Sh DESCRIPTION 73c65dcf2dSsnjThis section describes the functions used to get scheduling information 74c65dcf2dSsnjabout processes, and control the scheduling of processes. 752ac12931Srmind.Pp 762ac12931SrmindAvailable scheduling policies (classes) are: 772ac12931Srmind.Bl -tag -width SCHED_OTHER 782ac12931Srmind.It Dv SCHED_OTHER 792ac12931SrmindTime-sharing (TS) scheduling policy. 802ac12931SrmindThe default policy in 812ac12931Srmind.Nx . 822ac12931Srmind.It Dv SCHED_FIFO 832ac12931SrmindFirst in, first out (FIFO) scheduling policy. 842ac12931Srmind.It Dv SCHED_RR 852ac12931SrmindRound robin scheduling policy. 862ac12931Srmind.El 872ac12931Srmind.Pp 882ac12931SrmindThe 892ac12931Srmind.Fa struct sched_param 902ac12931Srmindcontains at least one member: 912ac12931Srmind.Bl -tag -width flags 922ac12931Srmind.It Fa sched_priority 932ac12931SrmindSpecifies the priority of the process. 94301aaf75SriastradhFor 95301aaf75Sriastradh.Dv SCHED_OTHER , 96301aaf75Sriastradhmust be 97301aaf75Sriastradh.Dv PRI_NONE ; 98301aaf75Sriastradhthe kernel will dynamically assign priorities to the thread based on 99301aaf75SriastradhCPU load. 1002ac12931Srmind.El 101a2d73f82Srmind.Sh FUNCTIONS 1022ac12931Srmind.Bl -tag -width compact 1032ac12931Srmind.It Fn sched_setparam pid param 1042ac12931SrmindSets the scheduling parameters for the process specified by 1052ac12931Srmind.Fa pid 1062ac12931Srmindto 1072ac12931Srmind.Fa param . 1082ac12931SrmindIf the value of 1092ac12931Srmind.Fa pid 1102ac12931Srmindis equal to zero, then the calling process is used. 1112ac12931Srmind.It Fn sched_getparam pid param 1122ac12931SrmindGets the scheduling parameters of the process specified by 1132ac12931Srmind.Fa pid 1142ac12931Srmindinto the structure 1152ac12931Srmind.Fa param . 1162ac12931SrmindIf the value of 1172ac12931Srmind.Fa pid 1182ac12931Srmindis equal to zero, then the calling process is used. 1192ac12931Srmind.It Fn sched_setscheduler pid policy param 1202ac12931SrmindSet the scheduling policy and parameters for the process specified by 1212ac12931Srmind.Fa pid . 1222ac12931SrmindIf the value of 1232ac12931Srmind.Fa pid 1242ac12931Srmindis equal to zero, then the calling process is used. 1252ac12931Srmind.It Fn sched_getscheduler pid 1262ac12931SrmindReturns the scheduling policy of the process specified by 1272ac12931Srmind.Fa pid . 1282ac12931SrmindIf the value of 1292ac12931Srmind.Fa pid 1302ac12931Srmindis equal to zero, then the calling process is used. 1312ac12931Srmind.It Fn sched_get_priority_max policy 1322ac12931SrmindReturns the maximal priority which may be used for the scheduling policy 1332ac12931Srmindspecified by 1342ac12931Srmind.Fa policy . 1352ac12931Srmind.It Fn sched_get_priority_min policy 1362ac12931SrmindReturns the minimal priority which may be used for the scheduling policy 1372ac12931Srmindspecified by 1382ac12931Srmind.Fa policy . 1392ac12931Srmind.It Fn sched_rr_get_interval pid interval 1402ac12931SrmindReturns the time quantum into the structure 1412ac12931Srmind.Fa interval 1422ac12931Srmindof the process specified by 1432ac12931Srmind.Fa pid . 1442ac12931SrmindIf the value of 1452ac12931Srmind.Fa pid 1462ac12931Srmindis equal to zero, then the calling process is used. 1472ac12931SrmindThe process must be running at 1482ac12931Srmind.Fa SCHED_RR 1492ac12931Srmindscheduling policy. 1502ac12931Srmind.It Fn sched_yield 1512ac12931SrmindYields a processor voluntarily and gives other threads a chance to run 1522ac12931Srmindwithout waiting for an involuntary preemptive switch. 153a2d73f82Srmind.It Fn sched_setaffinity_np pid size cpuset 154a2d73f82SrmindSet the affinity mask specified by 155a2d73f82Srmind.Fa cpuset 156a2d73f82Srmindfor the process specified by 157a2d73f82Srmind.Fa pid . 158a2d73f82SrmindAt least one valid CPU must be set in the mask. 159a2d73f82Srmind.It Fn sched_getaffinity_np pid size cpuset 160a2d73f82SrmindGet the affinity mask of the process specified by 161a2d73f82Srmind.Fa pid 162a2d73f82Srmindinto the 163a2d73f82Srmind.Fa cpuset . 1641f9c8df1Schristos.It Fn sched_protect priority 16568df3d57SchristosPerforms priority protection using the 166ed940b32Swiz.Dv PTHREAD_PRIO_PROTECT 167ed940b32Swizprotocol. 16868df3d57SchristosThis function will increase the protected priority of the caller thread to 16968df3d57Schristos.Fa priority 17068df3d57Schristosif the current thread's protected priority is smaller than 1711f9c8df1Schristos.Fa priority . 17268df3d57SchristosMultiple calls to 17368df3d57Schristos.Fn sched_protect 17468df3d57Schristoswith a positive priority will 17568df3d57Schristos.Dq push 17668df3d57Schristosa priority level to the current thread, whereas calling 17768df3d57Schristos.Fn sched_protect 17868df3d57Schristoswith a 17968df3d57Schristos.Fa priority 18068df3d57Schristoslevel of 18168df3d57Schristos.Dv \-1 18268df3d57Schristoswill 18368df3d57Schristos.Dq pop 18468df3d57Schristosa priority level. 18568df3d57SchristosWhen the level reaches 18668df3d57Schristos.Dv 0 18768df3d57Schristos(the same number of 18868df3d57Schristos.Dq pushes 18968df3d57Schristosand 19068df3d57Schristos.Dq pops 19168df3d57Schristoshave been issued) the original thread priority will be restored. 1922ac12931Srmind.El 1938973a7a7Swiz.Sh IMPLEMENTATION NOTES 194f3effc5eSjymSetting CPU 195f3effc5eSjym.Xr affinity 3 196f3effc5eSjymrequires super-user privileges. 197f3effc5eSjymOrdinary users can be allowed to control CPU affinity 198f3effc5eSjymof their threads via the 199f3effc5eSjym.Pa security.models.extensions.user_set_cpu_affinity 200f3effc5eSjym.Xr sysctl 7 . 201f3effc5eSjymSee 202f3effc5eSjym.Xr secmodel_extensions 9 . 203f3effc5eSjym.Pp 2048973a7a7SwizPortable applications should not use the 2058973a7a7Swiz.Fn sched_setaffinity_np 2068973a7a7Swizand 2078973a7a7Swiz.Fn sched_getaffinity_np 2088973a7a7Swizfunctions. 2092ac12931Srmind.Sh RETURN VALUES 2101f9c8df1Schristos.Fn sched_protect , 2112ac12931Srmind.Fn sched_setparam , 2122ac12931Srmind.Fn sched_getparam , 2132ac12931Srmind.Fn sched_rr_get_interval , 2142ac12931Srmindand 2152ac12931Srmind.Fn sched_yield 2162ac12931Srmindreturn 0 on success. 2172ac12931SrmindOtherwise, \-1 is returned and 2182ac12931Srmind.Va errno 2192ac12931Srmindis set to indicate the error. 2202ac12931Srmind.Pp 2212ac12931Srmind.Fn sched_setscheduler 2222ac12931Srmindreturns the previously used scheduling policy on success. 2232ac12931SrmindOtherwise, \-1 is returned and 2242ac12931Srmind.Va errno 2252ac12931Srmindis set to indicate the error. 2262ac12931Srmind.Pp 2272ac12931Srmind.Fn sched_getscheduler 2282ac12931Srmindreturns the scheduling policy on success. 2292ac12931SrmindOtherwise, \-1 is returned and 2302ac12931Srmind.Va errno 2312ac12931Srmindis set to indicate the error. 2322ac12931Srmind.Pp 2332ac12931Srmind.Fn sched_get_priority_max 2342ac12931Srmindand 2352ac12931Srmind.Fn sched_get_priority_min 2362ac12931Srmindreturn the maximal/minimal priority value on success. 2372ac12931SrmindOtherwise, \-1 is returned and 2382ac12931Srmind.Va errno 2392ac12931Srmindis set to indicate the error. 240a2d73f82Srmind.Pp 241a2d73f82Srmind.Fn sched_setaffinity_np 242a2d73f82Srmindand 243a2d73f82Srmind.Fn sched_getaffinity_np 244a2d73f82Srmindreturn 0 on success. 245a2d73f82SrmindOtherwise, \-1 is returned and 246a2d73f82Srmind.Va errno 247a2d73f82Srmindis set to indicate the error. 2482ac12931Srmind.Sh ERRORS 2492ac12931SrmindThe 2502ac12931Srmind.Fn sched_setparam 2512ac12931Srmindand 2522ac12931Srmind.Fn sched_setscheduler 2532ac12931Srmindfunctions fail if: 2542ac12931Srmind.Bl -tag -width Er 2552ac12931Srmind.It Bq Er EINVAL 2562ac12931SrmindAt least one of the specified scheduling parameters was invalid. 2572ac12931Srmind.It Bq Er EPERM 2582ac12931SrmindThe calling process has no appropriate privileges to perform the operation. 2592ac12931Srmind.It Bq Er ESRCH 2600927a616SwizNo process can be found corresponding to the PID specified by 2612ac12931Srmind.Fa pid , 262c65dcf2dSsnjand the value of 2632ac12931Srmind.Fa pid 2642ac12931Srmindis not zero. 2652ac12931Srmind.El 2662ac12931Srmind.Pp 2672ac12931SrmindThe 2682ac12931Srmind.Fn sched_getparam 2692ac12931Srmindand 2702ac12931Srmind.Fn sched_getscheduler 2712ac12931Srmindfunctions fail if: 2722ac12931Srmind.Bl -tag -width Er 2732ac12931Srmind.It Bq Er EPERM 2742ac12931SrmindThe calling process is not a super-user and its effective user id does not 2752ac12931Srmindmatch the effective user-id of the specified process. 2762ac12931Srmind.It Bq Er ESRCH 2772ac12931SrmindNo process can be found corresponding to that specified by 2782ac12931Srmind.Fa pid , 279c65dcf2dSsnjand the value of 2802ac12931Srmind.Fa pid 2812ac12931Srmindis not zero. 2822ac12931Srmind.El 2832ac12931Srmind.Pp 2842ac12931SrmindThe 2852ac12931Srmind.Fn sched_get_priority_max 2862ac12931Srmindand 2872ac12931Srmind.Fn sched_get_priority_min 2882ac12931Srmindfunctions fail if: 2892ac12931Srmind.Bl -tag -width Er 2902ac12931Srmind.It Bq Er EINVAL 2912ac12931SrmindThe specified scheduling policy is invalid. 2922ac12931Srmind.El 2932ac12931Srmind.Pp 2942ac12931SrmindThe 2952ac12931Srmind.Fn sched_rr_get_interval 2962ac12931Srmindfunction fails if: 2972ac12931Srmind.Bl -tag -width Er 2982ac12931Srmind.It Bq Er ESRCH 2992ac12931SrmindNo process can be found corresponding to that specified by 3002ac12931Srmind.Fa pid , 301c65dcf2dSsnjand the value of 3022ac12931Srmind.Fa pid 3032ac12931Srmindis not zero. 3042ac12931Srmind.El 30568df3d57Schristos.Pp 30668df3d57SchristosThe 30768df3d57Schristos.Fn sched_protect 30868df3d57Schristosfunction fails if: 30968df3d57Schristos.Bl -tag -width Er 31068df3d57Schristos.It Bq Er EINVAL 31168df3d57SchristosThe thread was not priority protected. 31268df3d57Schristos.It Bq Er EPERM 31368df3d57SchristosThe 31468df3d57Schristos.Fa priority 31568df3d57Schristosparameter was out of range (not in the range between 31668df3d57Schristos.Dv SCHED_PRIO_MIN 31768df3d57Schristosand 31868df3d57Schristos.Dv SCHED_PRIO_MAX ) . 31968df3d57Schristos.El 3202ac12931Srmind.Sh SEE ALSO 3214e4303fbSrmind.Xr affinity 3 , 3224e4303fbSrmind.Xr cpuset 3 , 3232ac12931Srmind.Xr pset 3 , 3242ac12931Srmind.Xr schedctl 8 3252ac12931Srmind.Sh STANDARDS 326a2d73f82SrmindThese functions, except 327a2d73f82Srmind.Fn sched_setaffinity_np 328a2d73f82Srmindand 329a2d73f82Srmind.Fn sched_getaffinity_np , 330a2d73f82Srmindare expected to conform the 3312ac12931Srmind.St -p1003.1-2001 3322ac12931Srmindstandard. 3332ac12931Srmind.Sh HISTORY 3342ac12931SrmindThe scheduling functions appeared in 3352ac12931Srmind.Nx 5.0 . 336