1 /* $OpenBSD: sched.h,v 1.16 2007/05/18 14:41:55 art 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 1982, 1986, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
74 */
75
76 #ifndef _SYS_SCHED_H_
77 #define _SYS_SCHED_H_
78
79 /*
80 * Posix defines a <sched.h> which may want to include <sys/sched.h>
81 */
82
83 /*
84 * CPU states.
85 * XXX Not really scheduler state, but no other good place to put
86 * it right now, and it really is per-CPU.
87 */
88 #define CP_USER 0
89 #define CP_NICE 1
90 #define CP_SYS 2
91 #define CP_INTR 3
92 #define CP_IDLE 4
93 #define CPUSTATES 5
94
95 /*
96 * Per-CPU scheduler state.
97 * XXX - expose to userland for now.
98 */
99 struct schedstate_percpu {
100 struct timeval spc_runtime; /* time curproc started running */
101 __volatile int spc_schedflags; /* flags; see below */
102 u_int spc_schedticks; /* ticks for schedclock() */
103 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */
104 u_char spc_curpriority; /* usrpri of curproc */
105 int spc_rrticks; /* ticks until roundrobin() */
106 int spc_pscnt; /* prof/stat counter */
107 int spc_psdiv; /* prof/stat divisor */
108 };
109
110 #ifdef _KERNEL
111
112 /* spc_flags */
113 #define SPCF_SEENRR 0x0001 /* process has seen roundrobin() */
114 #define SPCF_SHOULDYIELD 0x0002 /* process should yield the CPU */
115 #define SPCF_SWITCHCLEAR (SPCF_SEENRR|SPCF_SHOULDYIELD)
116
117 #define PPQ (128 / NQS) /* priorities per queue */
118 #define NICE_WEIGHT 2 /* priorities per nice level */
119 #define ESTCPULIM(e) min((e), NICE_WEIGHT * PRIO_MAX - PPQ)
120
121 extern int schedhz; /* ideally: 16 */
122 extern int rrticks_init; /* ticks per roundrobin() */
123
124 struct proc;
125 void schedclock(struct proc *);
126 struct cpu_info;
127 void roundrobin(struct cpu_info *);
128
129 #define sched_is_idle() (whichqs == 0)
130
131 /* Inherit the parent's scheduler history */
132 #define scheduler_fork_hook(parent, child) do { \
133 (child)->p_estcpu = (parent)->p_estcpu; \
134 } while (0)
135
136 /* Chargeback parents for the sins of their children. */
137 #define scheduler_wait_hook(parent, child) do { \
138 (parent)->p_estcpu = ESTCPULIM((parent)->p_estcpu + (child)->p_estcpu);\
139 } while (0)
140
141 #ifndef IPL_SCHED
142 #define IPL_SCHED IPL_HIGH
143 #endif
144
145 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
146 #include <sys/lock.h>
147
148 /*
149 * XXX Instead of using struct lock for the kernel lock and thus requiring us
150 * XXX to implement simplelocks, causing all sorts of fine-grained locks all
151 * XXX over our tree getting activated consuming both time and potentially
152 * XXX introducing locking protocol bugs.
153 */
154 extern struct __mp_lock sched_lock;
155
156 #define SCHED_ASSERT_LOCKED() KASSERT(__mp_lock_held(&sched_lock))
157 #define SCHED_ASSERT_UNLOCKED() KASSERT(__mp_lock_held(&sched_lock) == 0)
158
159 #define SCHED_LOCK(s) \
160 do { \
161 s = splsched(); \
162 __mp_lock(&sched_lock); \
163 } while (/* CONSTCOND */ 0)
164
165 #define SCHED_UNLOCK(s) \
166 do { \
167 __mp_unlock(&sched_lock); \
168 splx(s); \
169 } while (/* CONSTCOND */ 0)
170
171
172 void sched_lock_idle(void);
173 void sched_unlock_idle(void);
174
175 #else /* ! MULTIPROCESSOR || LOCKDEBUG */
176
177 #define SCHED_ASSERT_LOCKED() splassert(IPL_SCHED);
178 #define SCHED_ASSERT_UNLOCKED() /* nothing */
179
180 #define SCHED_LOCK(s) s = splsched()
181 #define SCHED_UNLOCK(s) splx(s)
182
183 #endif /* MULTIPROCESSOR || LOCKDEBUG */
184
185 #endif /* _KERNEL */
186 #endif /* _SYS_SCHED_H_ */