1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #pragma once 30 31 /** 32 * @file sched.h 33 * @brief Thread execution scheduling. 34 */ 35 36 #include <sys/cdefs.h> 37 38 #include <bits/timespec.h> 39 #include <linux/sched.h> 40 #include <linux/sched/types.h> 41 42 __BEGIN_DECLS 43 44 /* 45 * @def SCHED_NORMAL 46 * The standard (as opposed to real-time) round-robin scheduling policy. 47 * 48 * (Linux's name for POSIX's SCHED_OTHER.) 49 * 50 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 51 */ 52 53 /* 54 * @def SCHED_FIFO 55 * The real-time first-in/first-out scheduling policy. 56 * 57 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 58 */ 59 60 /* 61 * @def SCHED_RR 62 * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.) 63 * 64 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 65 */ 66 67 /* 68 * @def SCHED_BATCH 69 * The batch scheduling policy. 70 * 71 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 72 */ 73 74 /* 75 * @def SCHED_IDLE 76 * The low priority "only when otherwise idle" scheduling priority. 77 * 78 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 79 */ 80 81 /* 82 * @def SCHED_DEADLINE 83 * The deadline scheduling policy. 84 * 85 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) 86 */ 87 88 /* 89 * The standard (as opposed to real-time) round-robin scheduling policy. 90 * 91 * (POSIX's name for Linux's SCHED_NORMAL.) 92 */ 93 #define SCHED_OTHER SCHED_NORMAL 94 95 /** 96 * See sched_getparam()/sched_setparam() and 97 * sched_getscheduler()/sched_setscheduler(). 98 */ 99 struct sched_param { 100 int sched_priority; 101 }; 102 103 /** 104 * [sched_setscheduler(2)](https://man7.org/linux/man-pages/man2/sched_setscheduler.2.html) 105 * sets the scheduling policy and associated parameters for the given thread. 106 * 107 * Returns 0 on success and returns -1 and sets `errno` on failure. 108 */ 109 int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* _Nonnull __param); 110 111 /** 112 * [sched_getscheduler(2)](https://man7.org/linux/man-pages/man2/sched_getscheduler.2) 113 * gets the scheduling policy for the given thread. 114 * 115 * Returns a non-negative thread policy on success and returns -1 and sets 116 * `errno` on failure. 117 */ 118 int sched_getscheduler(pid_t __pid); 119 120 /** 121 * [sched_yield(2)](https://man7.org/linux/man-pages/man2/sched_yield.2.html) 122 * voluntarily gives up using the CPU so that another thread can run. 123 * 124 * Returns 0 on success and returns -1 and sets `errno` on failure. 125 */ 126 int sched_yield(void); 127 128 /** 129 * [sched_get_priority_max(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_max.2.html) 130 * gets the maximum priority value allowed for the given scheduling policy. 131 * 132 * Returns a priority on success and returns -1 and sets `errno` on failure. 133 */ 134 int sched_get_priority_max(int __policy); 135 136 /** 137 * [sched_get_priority_min(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_min.2.html) 138 * gets the minimum priority value allowed for the given scheduling policy. 139 * 140 * Returns a priority on success and returns -1 and sets `errno` on failure. 141 */ 142 int sched_get_priority_min(int __policy); 143 144 /** 145 * [sched_setparam(2)](https://man7.org/linux/man-pages/man2/sched_setparam.2.html) 146 * sets the scheduling parameters for the given thread. 147 * 148 * Returns 0 on success and returns -1 and sets `errno` on failure. 149 */ 150 int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param); 151 152 /** 153 * [sched_getparam(2)](https://man7.org/linux/man-pages/man2/sched_getparam.2.html) 154 * gets the scheduling parameters for the given thread. 155 * 156 * Returns 0 on success and returns -1 and sets `errno` on failure. 157 */ 158 int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param); 159 160 /** 161 * [sched_rr_get_interval(2)](https://man7.org/linux/man-pages/man2/sched_rr_get_interval.2.html) 162 * queries the round-robin time quantum for the given thread. 163 * 164 * Returns 0 on success and returns -1 and sets `errno` on failure. 165 */ 166 int sched_rr_get_interval(pid_t __pid, struct timespec* _Nonnull __quantum); 167 168 #if defined(__USE_GNU) 169 170 /** 171 * [clone(2)](https://man7.org/linux/man-pages/man2/clone.2.html) 172 * creates a new child process. 173 * 174 * Returns the pid of the child to the caller on success and 175 * returns -1 and sets `errno` on failure. 176 */ 177 int clone(int (* __BIONIC_COMPLICATED_NULLNESS __fn)(void* __BIONIC_COMPLICATED_NULLNESS ), void* __BIONIC_COMPLICATED_NULLNESS __child_stack, int __flags, void* _Nullable __arg, ...); 178 179 /** 180 * [unshare(2)](https://man7.org/linux/man-pages/man2/unshare.2.html) 181 * disassociates part of the caller's execution context. 182 * 183 * Returns 0 on success and returns -1 and sets `errno` on failure. 184 */ 185 int unshare(int __flags); 186 187 /** 188 * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html) 189 * reassociates a thread with a different namespace. 190 * 191 * Returns 0 on success and returns -1 and sets `errno` on failure. 192 */ 193 int setns(int __fd, int __ns_type); 194 195 /** 196 * [sched_getcpu(3)](https://man7.org/linux/man-pages/man3/sched_getcpu.3.html) 197 * reports which CPU the caller is running on. 198 * 199 * Returns a non-negative CPU number on success and returns -1 and sets 200 * `errno` on failure. 201 */ 202 int sched_getcpu(void); 203 204 #ifdef __LP64__ 205 #define CPU_SETSIZE 1024 206 #else 207 #define CPU_SETSIZE 32 208 #endif 209 210 #define __CPU_BITTYPE unsigned long int /* mandated by the kernel */ 211 #define __CPU_BITS (8 * sizeof(__CPU_BITTYPE)) 212 #define __CPU_ELT(x) ((x) / __CPU_BITS) 213 #define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1))) 214 215 /** 216 * [cpu_set_t](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) is a 217 * statically-sized CPU set. See `CPU_ALLOC` for dynamically-sized CPU sets. 218 */ 219 typedef struct { 220 __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ]; 221 } cpu_set_t; 222 223 /** 224 * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html) 225 * sets the CPU affinity mask for the given thread. 226 * 227 * Returns 0 on success and returns -1 and sets `errno` on failure. 228 */ 229 int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* _Nonnull __set); 230 231 /** 232 * [sched_getaffinity(2)](https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html) 233 * gets the CPU affinity mask for the given thread. 234 * 235 * Returns 0 on success and returns -1 and sets `errno` on failure. 236 */ 237 int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* _Nonnull __set); 238 239 /** 240 * [sched_setattr(2)](https://man7.org/linux/man-pages/man2/sched_setattr.2.html) 241 * sets the scheduling attributes for the given thread. 242 * 243 * Returns 0 on success and returns -1 and sets `errno` on failure. 244 */ 245 int sched_setattr(pid_t __pid, struct sched_attr* _Nonnull __attr, unsigned __flags) __INTRODUCED_IN(37); 246 247 /** 248 * [sched_getattr(2)](https://man7.org/linux/man-pages/man2/sched_getattr.2.html) 249 * gets the scheduling attributes for the given thread. 250 * 251 * Returns 0 on success and returns -1 and sets `errno` on failure. 252 */ 253 int sched_getattr(pid_t __pid, struct sched_attr* _Nonnull __attr, unsigned __size, unsigned __flags) __INTRODUCED_IN(37); 254 255 /** 256 * [CPU_ZERO](https://man7.org/linux/man-pages/man3/CPU_ZERO.3.html) clears all 257 * bits in a static CPU set. 258 */ 259 #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set) 260 /** 261 * [CPU_ZERO_S](https://man7.org/linux/man-pages/man3/CPU_ZERO_S.3.html) clears all 262 * bits in a dynamic CPU set allocated by `CPU_ALLOC`. 263 */ 264 #define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize) 265 266 /** 267 * [CPU_SET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one 268 * bit in a static CPU set. 269 */ 270 #define CPU_SET(cpu, set) CPU_SET_S(cpu, sizeof(cpu_set_t), set) 271 /** 272 * [CPU_SET_S](https://man7.org/linux/man-pages/man3/CPU_SET_S.3.html) sets one 273 * bit in a dynamic CPU set allocated by `CPU_ALLOC`. 274 */ 275 #define CPU_SET_S(cpu, setsize, set) \ 276 do { \ 277 size_t __cpu = (cpu); \ 278 if (__cpu < 8 * (setsize)) \ 279 (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \ 280 } while (0) 281 282 /** 283 * [CPU_CLR](https://man7.org/linux/man-pages/man3/CPU_CLR.3.html) clears one 284 * bit in a static CPU set. 285 */ 286 #define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set) 287 /** 288 * [CPU_CLR_S](https://man7.org/linux/man-pages/man3/CPU_CLR_S.3.html) clears one 289 * bit in a dynamic CPU set allocated by `CPU_ALLOC`. 290 */ 291 #define CPU_CLR_S(cpu, setsize, set) \ 292 do { \ 293 size_t __cpu = (cpu); \ 294 if (__cpu < 8 * (setsize)) \ 295 (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \ 296 } while (0) 297 298 /** 299 * [CPU_ISSET](https://man7.org/linux/man-pages/man3/CPU_ISSET.3.html) tests 300 * whether the given bit is set in a static CPU set. 301 */ 302 #define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set) 303 /** 304 * [CPU_ISSET_S](https://man7.org/linux/man-pages/man3/CPU_ISSET_S.3.html) tests 305 * whether the given bit is set in a dynamic CPU set allocated by `CPU_ALLOC`. 306 */ 307 #define CPU_ISSET_S(cpu, setsize, set) \ 308 (__extension__ ({ \ 309 size_t __cpu = (cpu); \ 310 (__cpu < 8 * (setsize)) \ 311 ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \ 312 : 0; \ 313 })) 314 315 /** 316 * [CPU_COUNT](https://man7.org/linux/man-pages/man3/CPU_COUNT.3.html) counts 317 * how many bits are set in a static CPU set. 318 */ 319 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set) 320 /** 321 * [CPU_COUNT_S](https://man7.org/linux/man-pages/man3/CPU_COUNT_S.3.html) counts 322 * how many bits are set in a dynamic CPU set allocated by `CPU_ALLOC`. 323 */ 324 #define CPU_COUNT_S(setsize, set) __sched_cpucount((setsize), (set)) 325 int __sched_cpucount(size_t __set_size, const cpu_set_t* _Nonnull __set); 326 327 /** 328 * [CPU_EQUAL](https://man7.org/linux/man-pages/man3/CPU_EQUAL.3.html) tests 329 * whether two static CPU sets have the same bits set and cleared as each other. 330 */ 331 #define CPU_EQUAL(set1, set2) CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2) 332 /** 333 * [CPU_EQUAL_S](https://man7.org/linux/man-pages/man3/CPU_EQUAL_S.3.html) tests 334 * whether two dynamic CPU sets allocated by `CPU_ALLOC` have the same bits 335 * set and cleared as each other. 336 */ 337 #define CPU_EQUAL_S(setsize, set1, set2) (__builtin_memcmp(set1, set2, setsize) == 0) 338 339 /** 340 * [CPU_AND](https://man7.org/linux/man-pages/man3/CPU_AND.3.html) ands two 341 * static CPU sets. 342 */ 343 #define CPU_AND(dst, set1, set2) __CPU_OP(dst, set1, set2, &) 344 /** 345 * [CPU_AND_S](https://man7.org/linux/man-pages/man3/CPU_AND_S.3.html) ands two 346 * dynamic CPU sets allocated by `CPU_ALLOC`. 347 */ 348 #define CPU_AND_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, &) 349 350 /** 351 * [CPU_OR](https://man7.org/linux/man-pages/man3/CPU_OR.3.html) ors two 352 * static CPU sets. 353 */ 354 #define CPU_OR(dst, set1, set2) __CPU_OP(dst, set1, set2, |) 355 /** 356 * [CPU_OR_S](https://man7.org/linux/man-pages/man3/CPU_OR_S.3.html) ors two 357 * dynamic CPU sets allocated by `CPU_ALLOC`. 358 */ 359 #define CPU_OR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, |) 360 361 /** 362 * [CPU_XOR](https://man7.org/linux/man-pages/man3/CPU_XOR.3.html) 363 * exclusive-ors two static CPU sets. 364 */ 365 #define CPU_XOR(dst, set1, set2) __CPU_OP(dst, set1, set2, ^) 366 /** 367 * [CPU_XOR_S](https://man7.org/linux/man-pages/man3/CPU_XOR_S.3.html) 368 * exclusive-ors two dynamic CPU sets allocated by `CPU_ALLOC`. 369 */ 370 #define CPU_XOR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, ^) 371 372 #define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op) 373 374 #define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \ 375 do { \ 376 cpu_set_t* __dst = (dstset); \ 377 const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \ 378 const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \ 379 size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \ 380 for (; __nn < __nn_max; __nn++) \ 381 (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \ 382 } while (0) 383 384 /** 385 * [CPU_ALLOC_SIZE](https://man7.org/linux/man-pages/man3/CPU_ALLOC_SIZE.3.html) 386 * returns the size of a CPU set large enough for CPUs in the range 0..count-1. 387 */ 388 #define CPU_ALLOC_SIZE(count) \ 389 __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE) 390 391 /** 392 * [CPU_ALLOC](https://man7.org/linux/man-pages/man3/CPU_ALLOC.3.html) 393 * allocates a CPU set large enough for CPUs in the range 0..count-1. 394 */ 395 #define CPU_ALLOC(count) __sched_cpualloc((count)) 396 cpu_set_t* _Nullable __sched_cpualloc(size_t __count); 397 398 /** 399 * [CPU_FREE](https://man7.org/linux/man-pages/man3/CPU_FREE.3.html) 400 * deallocates a CPU set allocated by `CPU_ALLOC`. 401 */ 402 #define CPU_FREE(set) __sched_cpufree((set)) 403 void __sched_cpufree(cpu_set_t* _Nonnull __set); 404 405 #endif /* __USE_GNU */ 406 407 __END_DECLS 408