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