]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched.h
187a62b2b5469b6f24c24a3a8618a7582af35d3a
[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_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
160 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
161
162 #define MAXPRI_STANDARD (BASEPRI_REALTIME - 1) /* 95 */
163
164 #define MAXPRI_KERNEL MAXPRI_STANDARD /* 95 */
165 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
166 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
167 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
168
169 #define MAXPRI_SYSTEM (MINPRI_KERNEL - 1) /* 79 */
170 #define MINPRI_SYSTEM (MAXPRI_SYSTEM - (NRQS / 8) + 1) /* 64 */
171
172 #define MAXPRI_USER (MINPRI_SYSTEM - 1) /* 63 */
173 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
174 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
175 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
176 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
177 #define MINPRI_USER MINPRI /* 0 */
178
179 #define MINPRI_STANDARD MINPRI_USER /* 0 */
180
181 /*
182 * Macro to check for invalid priorities.
183 */
184 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
185
186 struct run_queue {
187 int highq; /* highest runnable queue */
188 int bitmap[NRQBM]; /* run queue bitmap array */
189 int count; /* # of threads total */
190 int urgency; /* level of preemption urgency */
191 queue_head_t queues[NRQS]; /* one for each priority */
192 };
193
194 typedef struct run_queue *run_queue_t;
195 #define RUN_QUEUE_NULL ((run_queue_t) 0)
196
197 #define first_timeslice(processor) ((processor)->timeslice > 0)
198
199 #define processor_timeslice_setup(processor, thread) \
200 MACRO_BEGIN \
201 (processor)->timeslice = \
202 ((thread)->sched_mode & TH_MODE_TIMESHARE)? \
203 (processor)->processor_set->timeshare_quanta: 1; \
204 MACRO_END
205
206 #define thread_quantum_init(thread) \
207 MACRO_BEGIN \
208 (thread)->current_quantum = \
209 ((thread)->sched_mode & TH_MODE_REALTIME)? \
210 (thread)->realtime.computation: std_quantum; \
211 MACRO_END
212
213 /* Invoked at splsched by a thread on itself */
214 #define csw_needed(thread, processor) ( \
215 ((thread)->state & TH_SUSP) || \
216 (first_timeslice(processor)? \
217 ((processor)->runq.highq > (thread)->sched_pri || \
218 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
219 ((processor)->runq.highq >= (thread)->sched_pri || \
220 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
221
222 /*
223 * Scheduler routines.
224 */
225
226 /* Remove thread from its run queue */
227 extern run_queue_t run_queue_remove(
228 thread_t thread);
229
230 /* Periodic computation of load factors */
231 extern void compute_mach_factor(void);
232
233 /* Handle quantum expiration for an executing thread */
234 extern void thread_quantum_expire(
235 timer_call_param_t processor,
236 timer_call_param_t thread);
237
238 /* Called at splsched by a thread on itself */
239 extern ast_t csw_check(
240 thread_t thread,
241 processor_t processor);
242
243 extern uint32_t std_quantum, min_std_quantum;
244 extern uint32_t std_quantum_us;
245
246 extern uint32_t max_rt_quantum, min_rt_quantum;
247
248 /*
249 * Shift structures for holding update shifts. Actual computation
250 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
251 * +/- is determined by the sign of shift 2.
252 */
253 struct shift {
254 int shift1;
255 int shift2;
256 };
257
258 typedef struct shift *shift_t, shift_data_t;
259
260 /*
261 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
262 */
263
264 extern unsigned sched_tick;
265
266 #define SCHED_TICK_SHIFT 3
267
268 #define SCHED_SCALE 128
269 #define SCHED_SHIFT 7
270
271 /*
272 * thread_timer_delta macro takes care of both thread timers.
273 */
274 #define thread_timer_delta(thread) \
275 MACRO_BEGIN \
276 register uint32_t delta; \
277 \
278 delta = 0; \
279 TIMER_DELTA((thread)->system_timer, \
280 (thread)->system_timer_save, delta); \
281 TIMER_DELTA((thread)->user_timer, \
282 (thread)->user_timer_save, delta); \
283 (thread)->cpu_delta += delta; \
284 (thread)->sched_delta += (delta * \
285 (thread)->processor_set->sched_load); \
286 MACRO_END
287
288 #if SIMPLE_CLOCK
289 /*
290 * sched_usec is an exponential average of number of microseconds
291 * in a second for clock drift compensation.
292 */
293
294 extern int sched_usec;
295 #endif /* SIMPLE_CLOCK */
296
297 #endif /* _KERN_SCHED_H_ */