FreeBSD/Linux Kernel Cross Reference
sys/sys/sched.h
1 /* $OpenBSD: sched.h,v 1.57 2020/12/25 12:49:31 visa 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 #include <sys/queue.h>
73
74 /*
75 * Posix defines a <sched.h> which may want to include <sys/sched.h>
76 */
77
78 /*
79 * CPU states.
80 * XXX Not really scheduler state, but no other good place to put
81 * it right now, and it really is per-CPU.
82 */
83 #define CP_USER 0
84 #define CP_NICE 1
85 #define CP_SYS 2
86 #define CP_SPIN 3
87 #define CP_INTR 4
88 #define CP_IDLE 5
89 #define CPUSTATES 6
90
91 #define SCHED_NQS 32 /* 32 run queues. */
92
93 struct smr_entry;
94
95 /*
96 * Per-CPU scheduler state.
97 */
98 struct schedstate_percpu {
99 struct proc *spc_idleproc; /* idle proc for this cpu */
100 TAILQ_HEAD(prochead, proc) spc_qs[SCHED_NQS];
101 LIST_HEAD(,proc) spc_deadproc;
102 struct timespec spc_runtime; /* time curproc started running */
103 volatile int spc_schedflags; /* flags; see below */
104 u_int spc_schedticks; /* ticks for schedclock() */
105 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
106 u_char spc_curpriority; /* usrpri of curproc */
107 int spc_rrticks; /* ticks until roundrobin() */
108 int spc_pscnt; /* prof/stat counter */
109 int spc_psdiv; /* prof/stat divisor */
110
111 u_int spc_nrun; /* procs on the run queues */
112 fixpt_t spc_ldavg; /* shortest load avg. for this cpu */
113
114 volatile uint32_t spc_whichqs;
115 volatile u_int spc_spinning; /* this cpu is currently spinning */
116
117 SIMPLEQ_HEAD(, smr_entry) spc_deferred; /* deferred smr calls */
118 u_int spc_ndeferred; /* number of deferred smr calls */
119 u_int spc_smrdepth; /* level of smr nesting */
120 u_char spc_smrexpedite; /* if set, dispatch smr entries
121 * without delay */
122 u_char spc_smrgp; /* this CPU's view of grace period */
123 };
124
125 struct cpustats {
126 uint64_t cs_time[CPUSTATES]; /* CPU state statistics */
127 uint64_t cs_flags; /* see below */
128 };
129
130 #define CPUSTATS_ONLINE 0x0001 /* CPU is schedulable */
131
132 #ifdef _KERNEL
133
134 /* spc_flags */
135 #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
136 #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
137 #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
138 #define SPCF_SHOULDHALT 0x0004 /* CPU should be vacated */
139 #define SPCF_HALTED 0x0008 /* CPU has been halted */
140
141 #define SCHED_PPQ (128 / SCHED_NQS) /* priorities per queue */
142 #define NICE_WEIGHT 2 /* priorities per nice level */
143 #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - SCHED_PPQ)
144
145 extern int schedhz; /* ideally: 16 */
146 extern int rrticks_init; /* ticks per roundrobin() */
147
148 struct proc;
149 void schedclock(struct proc *);
150 struct cpu_info;
151 void roundrobin(struct cpu_info *);
152 void scheduler_start(void);
153 void userret(struct proc *p);
154
155 void sched_init_cpu(struct cpu_info *);
156 void sched_idle(void *);
157 void sched_exit(struct proc *);
158 void mi_switch(void);
159 void cpu_switchto(struct proc *, struct proc *);
160 struct proc *sched_chooseproc(void);
161 struct cpu_info *sched_choosecpu(struct proc *);
162 struct cpu_info *sched_choosecpu_fork(struct proc *parent, int);
163 void cpu_idle_enter(void);
164 void cpu_idle_cycle(void);
165 void cpu_idle_leave(void);
166 void sched_peg_curproc(struct cpu_info *ci);
167 void sched_barrier(struct cpu_info *ci);
168
169 int sysctl_hwsetperf(void *, size_t *, void *, size_t);
170 int sysctl_hwperfpolicy(void *, size_t *, void *, size_t);
171 int sysctl_hwsmt(void *, size_t *, void *, size_t);
172 int sysctl_hwncpuonline(void);
173
174 #ifdef MULTIPROCESSOR
175 void sched_start_secondary_cpus(void);
176 void sched_stop_secondary_cpus(void);
177 #endif
178
179 #define cpu_is_idle(ci) ((ci)->ci_schedstate.spc_whichqs == 0)
180 int cpu_is_online(struct cpu_info *);
181
182 void sched_init_runqueues(void);
183 void setrunqueue(struct cpu_info *, struct proc *, uint8_t);
184 void remrunqueue(struct proc *);
185
186 /* Chargeback parents for the sins of their children. */
187 #define scheduler_wait_hook(parent, child) do { \
188 (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + (child)->p_estcpu);\
189 } while (0)
190
191 /* Allow other processes to progress */
192 #define sched_pause(func) do { \
193 if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) \
194 func(); \
195 } while (0)
196
197 #if defined(MULTIPROCESSOR)
198 #include <sys/lock.h>
199
200 /*
201 * XXX Instead of using struct lock for the kernel lock and thus requiring us
202 * XXX to implement simplelocks, causing all sorts of fine-grained locks all
203 * XXX over our tree to be activated, the sched_lock is a different kind of
204 * XXX lock to avoid introducing locking protocol bugs.
205 */
206 extern struct __mp_lock sched_lock;
207
208 #define SCHED_ASSERT_LOCKED() \
209 do { \
210 splassert(IPL_SCHED); \
211 KASSERT(__mp_lock_held(&sched_lock, curcpu())); \
212 } while (0)
213 #define SCHED_ASSERT_UNLOCKED() \
214 do { \
215 KASSERT(__mp_lock_held(&sched_lock, curcpu()) == 0); \
216 } while (0)
217
218 #define SCHED_LOCK_INIT() __mp_lock_init(&sched_lock)
219
220 #define SCHED_LOCK(s) \
221 do { \
222 s = splsched(); \
223 __mp_lock(&sched_lock); \
224 } while (/* CONSTCOND */ 0)
225
226 #define SCHED_UNLOCK(s) \
227 do { \
228 __mp_unlock(&sched_lock); \
229 splx(s); \
230 } while (/* CONSTCOND */ 0)
231
232 #else /* ! MULTIPROCESSOR */
233
234 #define SCHED_ASSERT_LOCKED() splassert(IPL_SCHED);
235 #define SCHED_ASSERT_UNLOCKED() /* nothing */
236
237 #define SCHED_LOCK_INIT() /* nothing */
238
239 #define SCHED_LOCK(s) s = splsched()
240 #define SCHED_UNLOCK(s) splx(s)
241
242 #endif /* MULTIPROCESSOR */
243
244 #endif /* _KERNEL */
245 #endif /* _SYS_SCHED_H_ */
Cache object: 099de6b325616bc4ee7910fc20cd6bb0
|