1 /* $OpenBSD: sched.h,v 1.73 2024/07/08 14:46:47 mpi Exp $ */ 2 /* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */ 3 4 /*- 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Ross Harvey. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * (c) UNIX System Laboratories, Inc. 37 * All or some portions of this file are derived from material licensed 38 * to the University of California by American Telephone and Telegraph 39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 40 * the permission of UNIX System Laboratories, Inc. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 67 */ 68 69 #ifndef _SYS_SCHED_H_ 70 #define _SYS_SCHED_H_ 71 72 /* 73 * Posix defines a <sched.h> which may want to include <sys/sched.h> 74 */ 75 76 /* 77 * CPU states. 78 * XXX Not really scheduler state, but no other good place to put 79 * it right now, and it really is per-CPU. 80 */ 81 #define CP_USER 0 82 #define CP_NICE 1 83 #define CP_SYS 2 84 #define CP_SPIN 3 85 #define CP_INTR 4 86 #define CP_IDLE 5 87 #define CPUSTATES 6 88 89 struct cpustats { 90 uint64_t cs_time[CPUSTATES]; /* CPU state statistics */ 91 uint64_t cs_flags; /* see below */ 92 }; 93 94 #define CPUSTATS_ONLINE 0x0001 /* CPU is schedulable */ 95 96 #ifdef _KERNEL 97 98 #include <sys/clockintr.h> 99 #include <sys/queue.h> 100 101 #define SCHED_NQS 32 /* 32 run queues. */ 102 103 struct smr_entry; 104 105 /* 106 * Per-CPU scheduler state. 107 */ 108 struct schedstate_percpu { 109 struct proc *spc_idleproc; /* idle proc for this cpu */ 110 TAILQ_HEAD(prochead, proc) spc_qs[SCHED_NQS]; 111 LIST_HEAD(,proc) spc_deadproc; 112 struct timespec spc_runtime; /* time curproc started running */ 113 volatile int spc_schedflags; /* flags; see below */ 114 u_int spc_schedticks; /* ticks for schedclock() */ 115 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */ 116 u_char spc_curpriority; /* usrpri of curproc */ 117 118 struct clockintr spc_itimer; /* [o] itimer_update handle */ 119 struct clockintr spc_profclock; /* [o] profclock handle */ 120 struct clockintr spc_roundrobin;/* [o] roundrobin handle */ 121 struct clockintr spc_statclock; /* [o] statclock handle */ 122 123 u_int spc_nrun; /* procs on the run queues */ 124 125 volatile uint32_t spc_whichqs; 126 volatile u_int spc_spinning; /* this cpu is currently spinning */ 127 128 SIMPLEQ_HEAD(, smr_entry) spc_deferred; /* deferred smr calls */ 129 u_int spc_ndeferred; /* number of deferred smr calls */ 130 u_int spc_smrdepth; /* level of smr nesting */ 131 u_char spc_smrexpedite; /* if set, dispatch smr entries 132 * without delay */ 133 u_char spc_smrgp; /* this CPU's view of grace period */ 134 }; 135 136 /* spc_flags */ 137 #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */ 138 #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */ 139 #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD) 140 #define SPCF_SHOULDHALT 0x0004 /* CPU should be vacated */ 141 #define SPCF_HALTED 0x0008 /* CPU has been halted */ 142 #define SPCF_PROFCLOCK 0x0010 /* profclock() was started */ 143 #define SPCF_ITIMER 0x0020 /* itimer_update() was started */ 144 145 #define SCHED_PPQ (128 / SCHED_NQS) /* priorities per queue */ 146 #define NICE_WEIGHT 2 /* priorities per nice level */ 147 #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - SCHED_PPQ) 148 149 extern uint64_t roundrobin_period; 150 151 struct proc; 152 void schedclock(struct proc *); 153 struct clockrequest; 154 void roundrobin(struct clockrequest *, void *, void *); 155 void scheduler_start(void); 156 void userret(struct proc *p); 157 158 struct cpu_info; 159 void sched_init_cpu(struct cpu_info *); 160 void sched_idle(void *); 161 void sched_exit(struct proc *); 162 void sched_toidle(void); 163 void mi_switch(void); 164 void cpu_switchto(struct proc *, struct proc *); 165 struct proc *sched_chooseproc(void); 166 struct cpu_info *sched_choosecpu(struct proc *); 167 struct cpu_info *sched_choosecpu_fork(struct proc *parent, int); 168 void cpu_idle_enter(void); 169 void cpu_idle_cycle(void); 170 void cpu_idle_leave(void); 171 void sched_peg_curproc(struct cpu_info *ci); 172 void sched_unpeg_curproc(void); 173 void sched_barrier(struct cpu_info *ci); 174 175 int sysctl_hwsetperf(void *, size_t *, void *, size_t); 176 int sysctl_hwperfpolicy(void *, size_t *, void *, size_t); 177 int sysctl_hwsmt(void *, size_t *, void *, size_t); 178 int sysctl_hwncpuonline(void); 179 180 #ifdef MULTIPROCESSOR 181 void sched_start_secondary_cpus(void); 182 void sched_stop_secondary_cpus(void); 183 #endif 184 185 #define cpu_is_idle(ci) ((ci)->ci_schedstate.spc_whichqs == 0) 186 int cpu_is_online(struct cpu_info *); 187 188 void sched_init_runqueues(void); 189 void setrunqueue(struct cpu_info *, struct proc *, uint8_t); 190 void remrunqueue(struct proc *); 191 192 /* Chargeback parents for the sins of their children. */ 193 #define scheduler_wait_hook(parent, child) do { \ 194 (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + (child)->p_estcpu);\ 195 } while (0) 196 197 /* Allow other processes to progress */ 198 #define sched_pause(func) do { \ 199 if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) \ 200 func(); \ 201 } while (0) 202 203 extern struct mutex sched_lock; 204 205 #define SCHED_ASSERT_LOCKED() MUTEX_ASSERT_LOCKED(&sched_lock) 206 #define SCHED_ASSERT_UNLOCKED() MUTEX_ASSERT_UNLOCKED(&sched_lock) 207 208 #define SCHED_LOCK_INIT() mtx_init(&sched_lock, IPL_SCHED) 209 #define SCHED_LOCK() mtx_enter(&sched_lock) 210 #define SCHED_UNLOCK() mtx_leave(&sched_lock) 211 212 #endif /* _KERNEL */ 213 #endif /* _SYS_SCHED_H_ */ 214