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