]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
6601e61a A |
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. | |
8f6c56a5 | 11 | * |
6601e61a A |
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 | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a A |
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. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
1c79356b A |
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 | ||
1c79356b A |
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> | |
0b4e3aa0 | 71 | #include <kern/timer_call.h> |
9bccf70c | 72 | #include <kern/ast.h> |
1c79356b | 73 | |
9bccf70c A |
74 | #define NRQS 128 /* 128 levels per run queue */ |
75 | #define NRQBM (NRQS / 32) /* number of words per bit map */ | |
1c79356b A |
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 | |
0b4e3aa0 | 100 | * 79 System high priority |
1c79356b A |
101 | * A |
102 | * + | |
103 | * (16 levels) | |
104 | * + | |
105 | * V | |
0b4e3aa0 | 106 | * 64 System high priority |
1c79356b A |
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 | ||
55e303ae | 139 | #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */ |
1c79356b A |
140 | #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */ |
141 | ||
91447636 | 142 | #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */ |
0b4e3aa0 | 143 | #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */ |
55e303ae | 144 | #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */ |
0b4e3aa0 | 145 | #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */ |
1c79356b | 146 | |
91447636 A |
147 | #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */ |
148 | #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */ | |
1c79356b | 149 | |
91447636 | 150 | #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */ |
9bccf70c A |
151 | #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */ |
152 | #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */ | |
153 | #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */ | |
0b4e3aa0 A |
154 | #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */ |
155 | #define MINPRI_USER MINPRI /* 0 */ | |
1c79356b | 156 | |
1c79356b A |
157 | /* |
158 | * Macro to check for invalid priorities. | |
159 | */ | |
160 | #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI) | |
161 | ||
162 | struct run_queue { | |
1c79356b | 163 | int highq; /* highest runnable queue */ |
55e303ae A |
164 | int bitmap[NRQBM]; /* run queue bitmap array */ |
165 | int count; /* # of threads total */ | |
9bccf70c | 166 | int urgency; /* level of preemption urgency */ |
55e303ae | 167 | queue_head_t queues[NRQS]; /* one for each priority */ |
1c79356b A |
168 | }; |
169 | ||
170 | typedef struct run_queue *run_queue_t; | |
171 | #define RUN_QUEUE_NULL ((run_queue_t) 0) | |
172 | ||
55e303ae A |
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 | |
0b4e3aa0 | 188 | |
9bccf70c | 189 | /* Invoked at splsched by a thread on itself */ |
1c79356b A |
190 | #define csw_needed(thread, processor) ( \ |
191 | ((thread)->state & TH_SUSP) || \ | |
55e303ae | 192 | (first_timeslice(processor)? \ |
1c79356b A |
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 */ | |
55e303ae | 203 | extern run_queue_t run_queue_remove( |
0b4e3aa0 | 204 | thread_t thread); |
1c79356b | 205 | |
0b4e3aa0 A |
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 | ||
9bccf70c A |
211 | /* Called at splsched by a thread on itself */ |
212 | extern ast_t csw_check( | |
213 | thread_t thread, | |
214 | processor_t processor); | |
215 | ||
0b4e3aa0 A |
216 | extern uint32_t std_quantum, min_std_quantum; |
217 | extern uint32_t std_quantum_us; | |
1c79356b | 218 | |
0b4e3aa0 | 219 | extern uint32_t max_rt_quantum, min_rt_quantum; |
1c79356b | 220 | |
91447636 A |
221 | extern uint32_t sched_cswtime; |
222 | ||
1c79356b | 223 | /* |
91447636 | 224 | * Age usage (1 << SCHED_TICK_SHIFT) times per second. |
1c79356b | 225 | */ |
91447636 A |
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); | |
1c79356b | 233 | |
91447636 A |
234 | extern void compute_averunnable( |
235 | void *nrun); | |
236 | ||
237 | extern void compute_stack_target( | |
238 | void *arg); | |
1c79356b A |
239 | |
240 | /* | |
91447636 A |
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. | |
1c79356b | 249 | */ |
91447636 | 250 | extern int8_t sched_load_shifts[NRQS]; |
1c79356b | 251 | |
91447636 A |
252 | extern int32_t sched_poll_yield_shift; |
253 | extern uint32_t sched_safe_duration; | |
1c79356b | 254 | |
91447636 A |
255 | extern uint64_t max_unsafe_computation; |
256 | extern uint64_t max_poll_computation; | |
1c79356b | 257 | |
91447636 | 258 | extern uint32_t avenrun[3], mach_factor[3]; |
1c79356b A |
259 | |
260 | /* | |
261 | * thread_timer_delta macro takes care of both thread timers. | |
262 | */ | |
91447636 | 263 | #define thread_timer_delta(thread, delta) \ |
1c79356b | 264 | MACRO_BEGIN \ |
91447636 A |
265 | (delta) = timer_delta(&(thread)->system_timer, \ |
266 | &(thread)->system_timer_save); \ | |
267 | (delta) += timer_delta(&(thread)->user_timer, \ | |
268 | &(thread)->user_timer_save); \ | |
1c79356b A |
269 | MACRO_END |
270 | ||
1c79356b | 271 | #endif /* _KERN_SCHED_H_ */ |