1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1996, 1997 5 * HD Associates, Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by HD Associates, Inc 18 * and Jukka Antero Ukkonen. 19 * 4. Neither the name of the author nor the names of any co-contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 2002-2008, Jeffrey Roberson <jeff@freebsd.org> 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice unmodified, this list of conditions, and the following 45 * disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 * 61 * $FreeBSD$ 62 */ 63 64 #ifndef _SCHED_H_ 65 #define _SCHED_H_ 66 67 #ifdef _KERNEL 68 /* 69 * General scheduling info. 70 * 71 * sched_load: 72 * Total runnable non-ithread threads in the system. 73 * 74 * sched_runnable: 75 * Runnable threads for this processor. 76 */ 77 int sched_load(void); 78 int sched_rr_interval(void); 79 int sched_runnable(void); 80 81 /* 82 * Proc related scheduling hooks. 83 */ 84 void sched_exit(struct proc *p, struct thread *childtd); 85 void sched_fork(struct thread *td, struct thread *childtd); 86 void sched_fork_exit(struct thread *td); 87 void sched_class(struct thread *td, int class); 88 void sched_nice(struct proc *p, int nice); 89 90 /* 91 * Threads are switched in and out, block on resources, have temporary 92 * priorities inherited from their procs, and use up cpu time. 93 */ 94 void sched_exit_thread(struct thread *td, struct thread *child); 95 u_int sched_estcpu(struct thread *td); 96 void sched_fork_thread(struct thread *td, struct thread *child); 97 void sched_lend_prio(struct thread *td, u_char prio); 98 void sched_lend_user_prio(struct thread *td, u_char pri); 99 fixpt_t sched_pctcpu(struct thread *td); 100 void sched_prio(struct thread *td, u_char prio); 101 void sched_sleep(struct thread *td, int prio); 102 void sched_switch(struct thread *td, struct thread *newtd, int flags); 103 void sched_throw(struct thread *td); 104 void sched_unlend_prio(struct thread *td, u_char prio); 105 void sched_user_prio(struct thread *td, u_char prio); 106 void sched_userret_slowpath(struct thread *td); 107 void sched_wakeup(struct thread *td); 108 #ifdef RACCT 109 #ifdef SCHED_4BSD 110 fixpt_t sched_pctcpu_delta(struct thread *td); 111 #endif 112 #endif 113 114 static inline void 115 sched_userret(struct thread *td) 116 { 117 118 /* 119 * XXX we cheat slightly on the locking here to avoid locking in 120 * the usual case. Setting td_priority here is essentially an 121 * incomplete workaround for not setting it properly elsewhere. 122 * Now that some interrupt handlers are threads, not setting it 123 * properly elsewhere can clobber it in the window between setting 124 * it here and returning to user mode, so don't waste time setting 125 * it perfectly here. 126 */ 127 KASSERT((td->td_flags & TDF_BORROWING) == 0, 128 ("thread with borrowed priority returning to userland")); 129 if (__predict_false(td->td_priority != td->td_user_pri)) 130 sched_userret_slowpath(td); 131 } 132 133 /* 134 * Threads are moved on and off of run queues 135 */ 136 void sched_add(struct thread *td, int flags); 137 void sched_clock(struct thread *td); 138 void sched_preempt(struct thread *td); 139 void sched_rem(struct thread *td); 140 void sched_relinquish(struct thread *td); 141 struct thread *sched_choose(void); 142 void sched_idletd(void *); 143 144 /* 145 * Binding makes cpu affinity permanent while pinning is used to temporarily 146 * hold a thread on a particular CPU. 147 */ 148 void sched_bind(struct thread *td, int cpu); 149 static __inline void sched_pin(void); 150 void sched_unbind(struct thread *td); 151 static __inline void sched_unpin(void); 152 int sched_is_bound(struct thread *td); 153 void sched_affinity(struct thread *td); 154 155 /* 156 * These procedures tell the process data structure allocation code how 157 * many bytes to actually allocate. 158 */ 159 int sched_sizeof_proc(void); 160 int sched_sizeof_thread(void); 161 162 /* 163 * This routine provides a consistent thread name for use with KTR graphing 164 * functions. 165 */ 166 char *sched_tdname(struct thread *td); 167 #ifdef KTR 168 void sched_clear_tdname(struct thread *td); 169 #endif 170 171 static __inline void 172 sched_pin(void) 173 { 174 curthread->td_pinned++; 175 __compiler_membar(); 176 } 177 178 static __inline void 179 sched_unpin(void) 180 { 181 __compiler_membar(); 182 curthread->td_pinned--; 183 } 184 185 /* sched_add arguments (formerly setrunqueue) */ 186 #define SRQ_BORING 0x0000 /* No special circumstances. */ 187 #define SRQ_YIELDING 0x0001 /* We are yielding (from mi_switch). */ 188 #define SRQ_OURSELF 0x0002 /* It is ourself (from mi_switch). */ 189 #define SRQ_INTR 0x0004 /* It is probably urgent. */ 190 #define SRQ_PREEMPTED 0x0008 /* has been preempted.. be kind */ 191 #define SRQ_BORROWING 0x0010 /* Priority updated due to prio_lend */ 192 193 /* Scheduler stats. */ 194 #ifdef SCHED_STATS 195 DPCPU_DECLARE(long, sched_switch_stats[SWT_COUNT]); 196 197 #define SCHED_STAT_DEFINE_VAR(name, ptr, descr) \ 198 static void name ## _add_proc(void *dummy __unused) \ 199 { \ 200 \ 201 SYSCTL_ADD_PROC(NULL, \ 202 SYSCTL_STATIC_CHILDREN(_kern_sched_stats), OID_AUTO, \ 203 #name, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE, \ 204 ptr, 0, sysctl_dpcpu_long, "LU", descr); \ 205 } \ 206 SYSINIT(name, SI_SUB_LAST, SI_ORDER_MIDDLE, name ## _add_proc, NULL); 207 208 #define SCHED_STAT_DEFINE(name, descr) \ 209 DPCPU_DEFINE(unsigned long, name); \ 210 SCHED_STAT_DEFINE_VAR(name, &DPCPU_NAME(name), descr) 211 /* 212 * Sched stats are always incremented in critical sections so no atomic 213 * is necesssary to increment them. 214 */ 215 #define SCHED_STAT_INC(var) DPCPU_GET(var)++; 216 #else 217 #define SCHED_STAT_DEFINE_VAR(name, descr, ptr) 218 #define SCHED_STAT_DEFINE(name, descr) 219 #define SCHED_STAT_INC(var) (void)0 220 #endif 221 222 /* 223 * Fixup scheduler state for proc0 and thread0 224 */ 225 void schedinit(void); 226 #endif /* _KERNEL */ 227 228 /* POSIX 1003.1b Process Scheduling */ 229 230 /* 231 * POSIX scheduling policies 232 */ 233 #define SCHED_FIFO 1 234 #define SCHED_OTHER 2 235 #define SCHED_RR 3 236 237 struct sched_param { 238 int sched_priority; 239 }; 240 241 /* 242 * POSIX scheduling declarations for userland. 243 */ 244 #ifndef _KERNEL 245 #include <sys/cdefs.h> 246 #include <sys/_timespec.h> 247 #include <sys/_types.h> 248 249 #ifndef _PID_T_DECLARED 250 typedef __pid_t pid_t; 251 #define _PID_T_DECLARED 252 #endif 253 254 __BEGIN_DECLS 255 int sched_get_priority_max(int); 256 int sched_get_priority_min(int); 257 int sched_getparam(pid_t, struct sched_param *); 258 int sched_getscheduler(pid_t); 259 int sched_rr_get_interval(pid_t, struct timespec *); 260 int sched_setparam(pid_t, const struct sched_param *); 261 int sched_setscheduler(pid_t, int, const struct sched_param *); 262 int sched_yield(void); 263 __END_DECLS 264 265 #endif 266 #endif /* !_SCHED_H_ */ 267