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