FreeBSD/Linux Kernel Cross Reference
sys/sys/sched.h
1 /* $NetBSD: sched.h,v 1.19 2003/08/07 16:34:12 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ross Harvey, Jason R. Thorpe, and Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1982, 1986, 1991, 1993
41 * The Regents of the University of California. All rights reserved.
42 * (c) UNIX System Laboratories, Inc.
43 * All or some portions of this file are derived from material licensed
44 * to the University of California by American Telephone and Telegraph
45 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46 * the permission of UNIX System Laboratories, Inc.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
73 */
74
75 #ifndef _SYS_SCHED_H_
76 #define _SYS_SCHED_H_
77
78 #include <sys/featuretest.h>
79
80 #if defined(_KERNEL_OPT)
81 #include "opt_multiprocessor.h"
82 #include "opt_lockdebug.h"
83 #endif
84
85 struct sched_param {
86 int sched_priority;
87 };
88
89 /*
90 * Scheduling policies required by IEEE Std 1003.1-2001
91 */
92 #define SCHED_OTHER 0 /* Behavior can be FIFO or RR, or not */
93 #define SCHED_FIFO 1
94 #define SCHED_RR 2
95
96 /* Other nonstandard policies: */
97
98 #if defined(_NETBSD_SOURCE)
99
100 #include <sys/time.h>
101
102 /*
103 * Sleep queues.
104 *
105 * We're only looking at 7 bits of the address; everything is
106 * aligned to 4, lots of things are aligned to greater powers
107 * of 2. Shift right by 8, i.e. drop the bottom 256 worth.
108 */
109 #define SLPQUE_TABLESIZE 128
110 #define SLPQUE_LOOKUP(x) (((u_long)(x) >> 8) & (SLPQUE_TABLESIZE - 1))
111 struct slpque {
112 struct lwp *sq_head;
113 struct lwp **sq_tailp;
114 };
115
116 /*
117 * Run queues.
118 *
119 * We have 32 run queues in descending priority of 0..31. We maintain
120 * a bitmask of non-empty queues in order speed up finding the first
121 * runnable process. The bitmask is maintained only by machine-dependent
122 * code, allowing the most efficient instructions to be used to find the
123 * first non-empty queue.
124 */
125 #define RUNQUE_NQS 32
126 struct prochd {
127 struct lwp *ph_link;
128 struct lwp *ph_rlink;
129 };
130
131 /*
132 * CPU states.
133 * XXX Not really scheduler state, but no other good place to put
134 * it right now, and it really is per-CPU.
135 */
136 #define CP_USER 0
137 #define CP_NICE 1
138 #define CP_SYS 2
139 #define CP_INTR 3
140 #define CP_IDLE 4
141 #define CPUSTATES 5
142
143 /*
144 * Per-CPU scheduler state.
145 */
146 struct schedstate_percpu {
147 struct timeval spc_runtime; /* time curproc started running */
148 __volatile int spc_flags; /* flags; see below */
149 u_int spc_schedticks; /* ticks for schedclock() */
150 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
151 u_char spc_curpriority; /* usrpri of curproc */
152 int spc_rrticks; /* ticks until roundrobin() */
153 int spc_pscnt; /* prof/stat counter */
154 int spc_psdiv; /* prof/stat divisor */
155 };
156
157 /* spc_flags */
158 #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
159 #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
160
161 #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
162
163 /*
164 * Flags passed to the Linux-compatible __clone(2) system call.
165 */
166 #define CLONE_CSIGNAL 0x000000ff /* signal to be sent at exit */
167 #define CLONE_VM 0x00000100 /* share address space */
168 #define CLONE_FS 0x00000200 /* share "file system" info */
169 #define CLONE_FILES 0x00000400 /* share file descriptors */
170 #define CLONE_SIGHAND 0x00000800 /* share signal actions */
171 #define CLONE_PID 0x00001000 /* share process ID */
172 #define CLONE_PTRACE 0x00002000 /* ptrace(2) continues on
173 child */
174 #define CLONE_VFORK 0x00004000 /* parent blocks until child
175 exits */
176
177 #endif /* !_POSIX_SOURCE && !_XOPEN_SOURCE && !_ANSI_SOURCE */
178
179 #ifdef _KERNEL
180
181 #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
182 #define NICE_WEIGHT 2 /* priorities per nice level */
183 #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - PPQ)
184
185 extern int schedhz; /* ideally: 16 */
186 extern int rrticks; /* ticks per roundrobin() */
187
188 /*
189 * Global scheduler state. We would like to group these all together
190 * in a single structure to make them easier to find, but leaving
191 * whichqs and qs as independent globals makes for more efficient
192 * assembly language in the low-level context switch code. So we
193 * simply give them meaningful names; the globals are actually declared
194 * in kern/kern_synch.c.
195 */
196 extern struct prochd sched_qs[];
197 extern struct slpque sched_slpque[];
198 extern __volatile u_int32_t sched_whichqs;
199
200 #define SLPQUE(ident) (&sched_slpque[SLPQUE_LOOKUP(ident)])
201
202 struct proc;
203 struct cpu_info;
204
205 void schedclock(struct lwp *);
206 void sched_wakeup(const void *);
207 void roundrobin(struct cpu_info *);
208
209 /*
210 * scheduler_fork_hook:
211 *
212 * Inherit the parent's scheduler history.
213 */
214 #define scheduler_fork_hook(parent, child) \
215 do { \
216 (child)->p_estcpu = (parent)->p_estcpu; \
217 } while (/* CONSTCOND */ 0)
218
219 /*
220 * scheduler_wait_hook:
221 *
222 * Chargeback parents for the sins of their children.
223 */
224 #define scheduler_wait_hook(parent, child) \
225 do { \
226 /* XXX Only if parent != init?? */ \
227 (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + \
228 (child)->p_estcpu); \
229 } while (/* CONSTCOND */ 0)
230
231 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
232 #include <sys/lock.h>
233
234 extern struct simplelock sched_lock;
235
236 #define SCHED_ASSERT_LOCKED() LOCK_ASSERT(simple_lock_held(&sched_lock))
237 #define SCHED_ASSERT_UNLOCKED() LOCK_ASSERT(simple_lock_held(&sched_lock) == 0)
238
239 #define SCHED_LOCK(s) \
240 do { \
241 s = splsched(); \
242 simple_lock(&sched_lock); \
243 } while (/* CONSTCOND */ 0)
244
245 #define SCHED_UNLOCK(s) \
246 do { \
247 simple_unlock(&sched_lock); \
248 splx(s); \
249 } while (/* CONSTCOND */ 0)
250
251 void sched_lock_idle(void);
252 void sched_unlock_idle(void);
253
254 #else /* ! MULTIPROCESSOR || LOCKDEBUG */
255
256 #define SCHED_ASSERT_LOCKED() /* nothing */
257 #define SCHED_ASSERT_UNLOCKED() /* nothing */
258
259 #define SCHED_LOCK(s) s = splsched()
260 #define SCHED_UNLOCK(s) splx(s)
261
262 #endif /* MULTIPROCESSOR || LOCKDEBUG */
263
264 #endif /* _KERNEL */
265 #endif /* _SYS_SCHED_H_ */
Cache object: 9a09a8216eac6d3a824bdab123083c99
|