]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched.h
6f5c9d398bd32c0932ddb9555152541ed2194e4b
[apple/xnu.git] / osfmk / kern / sched.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: sched.h
57 * Author: Avadis Tevanian, Jr.
58 * Date: 1985
59 *
60 * Header file for scheduler.
61 *
62 */
63
64 #ifndef _KERN_SCHED_H_
65 #define _KERN_SCHED_H_
66
67 #include <simple_clock.h>
68 #include <stat_time.h>
69
70 #include <mach/policy.h>
71 #include <kern/kern_types.h>
72 #include <kern/queue.h>
73 #include <kern/lock.h>
74 #include <kern/macro_help.h>
75 #include <kern/timer_call.h>
76 #include <kern/ast.h>
77
78 #if STAT_TIME
79
80 /*
81 * Statistical timing uses microseconds as timer units.
82 */
83 #define PRI_SHIFT (16 - SCHED_TICK_SHIFT)
84
85 #else /* STAT_TIME */
86
87 /*
88 * Otherwise machine provides shift(s) based on time units it uses.
89 */
90 #include <machine/sched_param.h>
91
92 #endif /* STAT_TIME */
93
94 #define NRQS 128 /* 128 levels per run queue */
95 #define NRQBM (NRQS / 32) /* number of words per bit map */
96
97 #define MAXPRI (NRQS-1)
98 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
99 #define IDLEPRI 0 /* idle thread priority */
100 #define DEPRESSPRI MINPRI /* depress priority */
101
102 /*
103 * High-level priority assignments
104 *
105 *************************************************************************
106 * 127 Reserved (real-time)
107 * A
108 * +
109 * (32 levels)
110 * +
111 * V
112 * 96 Reserved (real-time)
113 * 95 Kernel mode only
114 * A
115 * +
116 * (16 levels)
117 * +
118 * V
119 * 80 Kernel mode only
120 * 79 System high priority
121 * A
122 * +
123 * (16 levels)
124 * +
125 * V
126 * 64 System high priority
127 * 63 Elevated priorities
128 * A
129 * +
130 * (12 levels)
131 * +
132 * V
133 * 52 Elevated priorities
134 * 51 Elevated priorities (incl. BSD +nice)
135 * A
136 * +
137 * (20 levels)
138 * +
139 * V
140 * 32 Elevated priorities (incl. BSD +nice)
141 * 31 Default (default base for threads)
142 * 30 Lowered priorities (incl. BSD -nice)
143 * A
144 * +
145 * (20 levels)
146 * +
147 * V
148 * 11 Lowered priorities (incl. BSD -nice)
149 * 10 Lowered priorities (aged pri's)
150 * A
151 * +
152 * (11 levels)
153 * +
154 * V
155 * 0 Lowered priorities (aged pri's / idle)
156 *************************************************************************
157 */
158
159 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
160
161 #define MAXPRI_STANDARD (BASEPRI_REALTIME - 1) /* 95 */
162
163 #define MAXPRI_KERNEL MAXPRI_STANDARD /* 95 */
164 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
165 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
166
167 #define MAXPRI_SYSTEM (MINPRI_KERNEL - 1) /* 79 */
168 #define MINPRI_SYSTEM (MAXPRI_SYSTEM - (NRQS / 8) + 1) /* 64 */
169
170 #define MAXPRI_USER (MINPRI_SYSTEM - 1) /* 63 */
171 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
172 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
173 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
174 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
175 #define MINPRI_USER MINPRI /* 0 */
176
177 #define MINPRI_STANDARD MINPRI_USER /* 0 */
178
179 /*
180 * Macro to check for invalid priorities.
181 */
182 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
183
184 struct run_queue {
185 queue_head_t queues[NRQS]; /* one for each priority */
186 decl_simple_lock_data(,lock) /* one lock for all queues */
187 int bitmap[NRQBM]; /* run queue bitmap array */
188 int highq; /* highest runnable queue */
189 int urgency; /* level of preemption urgency */
190 int count; /* # of threads in queue */
191 };
192
193 typedef struct run_queue *run_queue_t;
194 #define RUN_QUEUE_NULL ((run_queue_t) 0)
195
196 #define first_quantum(processor) ((processor)->slice_quanta > 0)
197
198 /* Invoked at splsched by a thread on itself */
199 #define csw_needed(thread, processor) ( \
200 ((thread)->state & TH_SUSP) || \
201 (first_quantum(processor)? \
202 ((processor)->runq.highq > (thread)->sched_pri || \
203 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
204 ((processor)->runq.highq >= (thread)->sched_pri || \
205 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
206
207 /*
208 * Scheduler routines.
209 */
210
211 /* Remove thread from its run queue */
212 extern run_queue_t rem_runq(
213 thread_t thread);
214
215 /* Periodic computation of load factors */
216 extern void compute_mach_factor(void);
217
218 /* Handle quantum expiration for an executing thread */
219 extern void thread_quantum_expire(
220 timer_call_param_t processor,
221 timer_call_param_t thread);
222
223 /* Called at splsched by a thread on itself */
224 extern ast_t csw_check(
225 thread_t thread,
226 processor_t processor);
227
228 extern uint32_t std_quantum, min_std_quantum;
229 extern uint32_t std_quantum_us;
230
231 extern uint32_t max_rt_quantum, min_rt_quantum;
232
233 /*
234 * Shift structures for holding update shifts. Actual computation
235 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
236 * +/- is determined by the sign of shift 2.
237 */
238 struct shift {
239 int shift1;
240 int shift2;
241 };
242
243 typedef struct shift *shift_t, shift_data_t;
244
245 /*
246 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
247 */
248
249 extern unsigned sched_tick;
250
251 #define SCHED_TICK_SHIFT 3
252
253 #define SCHED_SCALE 128
254 #define SCHED_SHIFT 7
255
256 /*
257 * thread_timer_delta macro takes care of both thread timers.
258 */
259 #define thread_timer_delta(thread) \
260 MACRO_BEGIN \
261 register uint32_t delta; \
262 \
263 delta = 0; \
264 TIMER_DELTA((thread)->system_timer, \
265 (thread)->system_timer_save, delta); \
266 TIMER_DELTA((thread)->user_timer, \
267 (thread)->user_timer_save, delta); \
268 (thread)->cpu_delta += delta; \
269 (thread)->sched_delta += (delta * \
270 (thread)->processor_set->sched_load); \
271 MACRO_END
272
273 #if SIMPLE_CLOCK
274 /*
275 * sched_usec is an exponential average of number of microseconds
276 * in a second for clock drift compensation.
277 */
278
279 extern int sched_usec;
280 #endif /* SIMPLE_CLOCK */
281
282 #endif /* _KERN_SCHED_H_ */