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