Annotation of src/sys/sys/sched.h, Revision 1.14.10.3
1.14.10.3! thorpej 1: /* $NetBSD: sched.h,v 1.14.10.2 2002/03/10 21:05:11 thorpej Exp $ */
1.1 ross 2:
3: /*-
1.14.10.1 thorpej 4: * Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
1.1 ross 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.3 thorpej 8: * by Ross Harvey and Jason R. Thorpe.
1.1 ross 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. All advertising materials mentioning features or use of this software
57: * must display the following acknowledgement:
58: * This product includes software developed by the University of
59: * California, Berkeley and its contributors.
60: * 4. Neither the name of the University nor the names of its contributors
61: * may be used to endorse or promote products derived from this software
62: * without specific prior written permission.
63: *
64: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74: * SUCH DAMAGE.
75: *
76: * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
77: */
78:
79: #ifndef _SYS_SCHED_H_
80: #define _SYS_SCHED_H_
81:
1.13 mrg 82: #if defined(_KERNEL_OPT)
1.6 thorpej 83: #include "opt_multiprocessor.h"
84: #include "opt_lockdebug.h"
85: #endif
86:
1.1 ross 87: /*
88: * Posix defines a <sched.h> which may want to include <sys/sched.h>
89: */
90:
1.3 thorpej 91: #if !defined(_POSIX_SOURCE) && !defined(_XOPEN_SOURCE) && \
92: !defined(_ANSI_SOURCE)
1.4 sommerfe 93:
94: #include <sys/time.h>
1.14.10.1 thorpej 95: #include <sys/queue.h>
1.4 sommerfe 96:
1.3 thorpej 97: /*
98: * Sleep queues.
99: *
100: * We're only looking at 7 bits of the address; everything is
101: * aligned to 4, lots of things are aligned to greater powers
102: * of 2. Shift right by 8, i.e. drop the bottom 256 worth.
103: */
104: #define SLPQUE_TABLESIZE 128
105: #define SLPQUE_LOOKUP(x) (((u_long)(x) >> 8) & (SLPQUE_TABLESIZE - 1))
106: struct slpque {
107: struct proc *sq_head;
108: struct proc **sq_tailp;
109: };
110:
111: /*
112: * Run queues.
113: *
114: * We have 32 run queues in descending priority of 0..31. We maintain
115: * a bitmask of non-empty queues in order speed up finding the first
116: * runnable process. The bitmask is maintained only by machine-dependent
117: * code, allowing the most efficient instructions to be used to find the
118: * first non-empty queue.
119: */
120: #define RUNQUE_NQS 32
121: struct prochd {
122: struct proc *ph_link;
123: struct proc *ph_rlink;
124: };
125:
126: /*
1.5 thorpej 127: * CPU states.
128: * XXX Not really scheduler state, but no other good place to put
129: * it right now, and it really is per-CPU.
130: */
131: #define CP_USER 0
132: #define CP_NICE 1
133: #define CP_SYS 2
134: #define CP_INTR 3
135: #define CP_IDLE 4
136: #define CPUSTATES 5
137:
138: /*
1.3 thorpej 139: * Per-CPU scheduler state.
140: */
141: struct schedstate_percpu {
142: struct timeval spc_runtime; /* time curproc started running */
143: __volatile int spc_flags; /* flags; see below */
1.5 thorpej 144: u_int spc_schedticks; /* ticks for schedclock() */
145: u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
1.3 thorpej 146: u_char spc_curpriority; /* usrpri of curproc */
1.8 sommerfe 147: int spc_rrticks; /* ticks until roundrobin() */
148: int spc_pscnt; /* prof/stat counter */
149: int spc_psdiv; /* prof/stat divisor */
1.3 thorpej 150: };
151:
152: /* spc_flags */
153: #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
154: #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
155:
156: #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
157:
1.14 thorpej 158: /*
1.14.10.1 thorpej 159: * Turnstile sleep queue for kernel synchronization primitives.
160: */
161: struct turnstile {
162: LIST_ENTRY(turnstile) ts_chain; /* link on hash chain */
163: struct turnstile *ts_free; /* turnstile free list */
164: void *ts_obj; /* lock object */
165: struct slpque ts_sleepq; /* queue of waiters */
166: u_int ts_waiters; /* number of waiters on queue */
167: };
168:
169: #define TS_READER_Q 0 /* reader sleep queue */
170: #define TS_WRITER_Q 1 /* writer sleep queue */
171:
172: /*
1.14 thorpej 173: * Flags passed to the Linux-compatible __clone(2) system call.
174: */
175: #define CLONE_CSIGNAL 0x000000ff /* signal to be sent at exit */
176: #define CLONE_VM 0x00000100 /* share address space */
177: #define CLONE_FS 0x00000200 /* share "file system" info */
178: #define CLONE_FILES 0x00000400 /* share file descriptors */
179: #define CLONE_SIGHAND 0x00000800 /* share signal actions */
180: #define CLONE_PID 0x00001000 /* share process ID */
181: #define CLONE_PTRACE 0x00002000 /* ptrace(2) continues on
182: child */
183: #define CLONE_VFORK 0x00004000 /* parent blocks until child
184: exits */
185:
1.3 thorpej 186: #endif /* !_POSIX_SOURCE && !_XOPEN_SOURCE && !_ANSI_SOURCE */
187:
1.14 thorpej 188: #ifdef _KERNEL
1.1 ross 189:
1.14.10.1 thorpej 190: #include <sys/pool.h>
191:
1.3 thorpej 192: #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
1.1 ross 193: #define NICE_WEIGHT 2 /* priorities per nice level */
194: #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - PPQ)
195:
1.12 simonb 196: extern int schedhz; /* ideally: 16 */
197: extern int rrticks; /* ticks per roundrobin() */
1.1 ross 198:
1.3 thorpej 199: /*
200: * Global scheduler state. We would like to group these all together
201: * in a single structure to make them easier to find, but leaving
202: * whichqs and qs as independent globals makes for more efficient
203: * assembly language in the low-level context switch code. So we
204: * simply give them meaningful names; the globals are actually declared
205: * in kern/kern_synch.c.
206: */
207: extern struct prochd sched_qs[];
208: extern struct slpque sched_slpque[];
209: extern __volatile u_int32_t sched_whichqs;
210:
211: #define SLPQUE(ident) (&sched_slpque[SLPQUE_LOOKUP(ident)])
212:
213: struct proc;
1.10 sommerfe 214: struct cpu_info;
1.3 thorpej 215:
1.6 thorpej 216: void schedclock(struct proc *p);
217: void sched_wakeup(void *);
1.9 sommerfe 218: void roundrobin(struct cpu_info *);
1.14.10.1 thorpej 219: void awaken(struct proc *);
220:
221: void turnstile_init(void);
222:
223: struct turnstile *turnstile_lookup(void *);
224: void turnstile_exit(void *);
1.14.10.3! thorpej 225: int turnstile_block(struct turnstile *, int, int, void *);
1.14.10.1 thorpej 226: void turnstile_wakeup(struct turnstile *, int, int);
227:
1.14.10.2 thorpej 228: extern struct pool_cache turnstile_cache;
1.3 thorpej 229:
230: /*
231: * scheduler_fork_hook:
232: *
233: * Inherit the parent's scheduler history.
234: */
235: #define scheduler_fork_hook(parent, child) \
236: do { \
237: (child)->p_estcpu = (parent)->p_estcpu; \
1.11 lukem 238: } while (/* CONSTCOND */ 0)
1.3 thorpej 239:
240: /*
241: * scheduler_wait_hook:
242: *
243: * Chargeback parents for the sins of their children.
244: */
245: #define scheduler_wait_hook(parent, child) \
246: do { \
247: /* XXX Only if parent != init?? */ \
248: (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + \
249: (child)->p_estcpu); \
1.11 lukem 250: } while (/* CONSTCOND */ 0)
1.6 thorpej 251:
252: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
253: #include <sys/lock.h>
254:
255: extern struct simplelock sched_lock;
256:
257: #define SCHED_ASSERT_LOCKED() LOCK_ASSERT(simple_lock_held(&sched_lock))
258: #define SCHED_ASSERT_UNLOCKED() LOCK_ASSERT(simple_lock_held(&sched_lock) == 0)
259:
1.14.10.1 thorpej 260: #define _SCHED_LOCK simple_lock(&sched_lock)
261: #define _SCHED_UNLOCK simple_unlock(&sched_lock)
1.6 thorpej 262:
263: void sched_lock_idle(void);
264: void sched_unlock_idle(void);
265:
266: #else /* ! MULTIPROCESSOR || LOCKDEBUG */
267:
268: #define SCHED_ASSERT_LOCKED() /* nothing */
269: #define SCHED_ASSERT_UNLOCKED() /* nothing */
270:
1.14.10.1 thorpej 271: #define _SCHED_LOCK /* nothing */
272: #define _SCHED_UNLOCK /* nothing */
273:
274: #define SCHED_LOCK(s) \
275: do { \
276: s = splsched(); \
277: _SCHED_LOCK; \
278: } while (/* CONSTCOND */ 0)
279:
280: #define SCHED_UNLOCK(s) \
281: do { \
282: _SCHED_UNLOCK; \
283: splx(s); \
284: } while (/* CONSTCOND */ 0)
1.6 thorpej 285:
286: #endif /* MULTIPROCESSOR || LOCKDEBUG */
287:
1.1 ross 288: #endif /* _KERNEL */
289: #endif /* _SYS_SCHED_H_ */
CVSweb <webmaster@jp.NetBSD.org>