]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | ||
1c79356b A |
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> | |
0b4e3aa0 | 77 | #include <kern/timer_call.h> |
9bccf70c | 78 | #include <kern/ast.h> |
1c79356b | 79 | |
9bccf70c A |
80 | #define NRQS 128 /* 128 levels per run queue */ |
81 | #define NRQBM (NRQS / 32) /* number of words per bit map */ | |
1c79356b A |
82 | |
83 | #define MAXPRI (NRQS-1) | |
84 | #define MINPRI IDLEPRI /* lowest legal priority schedulable */ | |
85 | #define IDLEPRI 0 /* idle thread priority */ | |
1c79356b A |
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 | |
0b4e3aa0 | 105 | * 79 System high priority |
1c79356b A |
106 | * A |
107 | * + | |
108 | * (16 levels) | |
109 | * + | |
110 | * V | |
0b4e3aa0 | 111 | * 64 System high priority |
1c79356b A |
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 | ||
55e303ae | 144 | #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */ |
1c79356b A |
145 | #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */ |
146 | ||
91447636 | 147 | #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */ |
0b4e3aa0 | 148 | #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */ |
55e303ae | 149 | #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */ |
0b4e3aa0 | 150 | #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */ |
1c79356b | 151 | |
91447636 A |
152 | #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */ |
153 | #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */ | |
1c79356b | 154 | |
91447636 | 155 | #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */ |
9bccf70c A |
156 | #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */ |
157 | #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */ | |
158 | #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */ | |
0b4e3aa0 | 159 | #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */ |
b7266188 | 160 | #define MAXPRI_THROTTLE (MINPRI + 4) /* 4 */ |
0b4e3aa0 | 161 | #define MINPRI_USER MINPRI /* 0 */ |
1c79356b | 162 | |
d1ecb069 A |
163 | #ifdef CONFIG_EMBEDDED |
164 | #define DEPRESSPRI MAXPRI_THROTTLE | |
165 | #else | |
166 | #define DEPRESSPRI MINPRI /* depress priority */ | |
167 | #endif | |
168 | ||
6d2010ae A |
169 | /* Type used for thread->sched_mode and saved_mode */ |
170 | typedef enum { | |
171 | TH_MODE_NONE = 0, /* unassigned, usually for saved_mode only */ | |
172 | TH_MODE_REALTIME, /* time constraints supplied */ | |
173 | TH_MODE_FIXED, /* use fixed priorities, no decay */ | |
174 | TH_MODE_TIMESHARE, /* use timesharing algorithm */ | |
175 | TH_MODE_FAIRSHARE /* use fair-share scheduling */ | |
176 | } sched_mode_t; | |
177 | ||
1c79356b A |
178 | /* |
179 | * Macro to check for invalid priorities. | |
180 | */ | |
181 | #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI) | |
182 | ||
6d2010ae A |
183 | struct runq_stats { |
184 | uint64_t count_sum; | |
185 | uint64_t last_change_timestamp; | |
186 | }; | |
187 | ||
188 | #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY) | |
189 | ||
1c79356b | 190 | struct run_queue { |
1c79356b | 191 | int highq; /* highest runnable queue */ |
55e303ae A |
192 | int bitmap[NRQBM]; /* run queue bitmap array */ |
193 | int count; /* # of threads total */ | |
9bccf70c | 194 | int urgency; /* level of preemption urgency */ |
55e303ae | 195 | queue_head_t queues[NRQS]; /* one for each priority */ |
6d2010ae A |
196 | |
197 | struct runq_stats runq_stats; | |
1c79356b A |
198 | }; |
199 | ||
6d2010ae | 200 | #endif /* defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY) */ |
1c79356b | 201 | |
6d2010ae A |
202 | struct rt_queue { |
203 | int count; /* # of threads total */ | |
204 | queue_head_t queue; /* all runnable RT threads */ | |
55e303ae | 205 | |
6d2010ae A |
206 | struct runq_stats runq_stats; |
207 | }; | |
208 | ||
209 | #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY) | |
210 | struct fairshare_queue { | |
211 | int count; /* # of threads total */ | |
212 | queue_head_t queue; /* all runnable threads demoted to fairshare scheduling */ | |
213 | ||
214 | struct runq_stats runq_stats; | |
215 | }; | |
216 | #endif | |
0b4e3aa0 | 217 | |
6d2010ae | 218 | #if defined(CONFIG_SCHED_GRRR_CORE) |
1c79356b A |
219 | |
220 | /* | |
6d2010ae A |
221 | * We map standard Mach priorities to an abstract scale that more properly |
222 | * indicates how we want processor time allocated under contention. | |
1c79356b | 223 | */ |
6d2010ae A |
224 | typedef uint8_t grrr_proportional_priority_t; |
225 | typedef uint8_t grrr_group_index_t; | |
226 | ||
227 | #define NUM_GRRR_PROPORTIONAL_PRIORITIES 256 | |
228 | #define MAX_GRRR_PROPORTIONAL_PRIORITY ((grrr_proportional_priority_t)255) | |
229 | ||
230 | #if 0 | |
231 | #define NUM_GRRR_GROUPS 8 /* log(256) */ | |
232 | #endif | |
233 | ||
234 | #define NUM_GRRR_GROUPS 64 /* 256/4 */ | |
235 | ||
236 | struct grrr_group { | |
237 | queue_chain_t priority_order; /* next greatest weight group */ | |
238 | grrr_proportional_priority_t minpriority; | |
239 | grrr_group_index_t index; | |
240 | ||
241 | queue_head_t clients; | |
242 | int count; | |
243 | uint32_t weight; | |
244 | #if 0 | |
245 | uint32_t deferred_removal_weight; | |
246 | #endif | |
247 | uint32_t work; | |
248 | thread_t current_client; | |
249 | }; | |
250 | ||
251 | struct grrr_run_queue { | |
252 | int count; | |
253 | uint32_t last_rescale_tick; | |
254 | struct grrr_group groups[NUM_GRRR_GROUPS]; | |
255 | queue_head_t sorted_group_list; | |
256 | uint32_t weight; | |
257 | grrr_group_t current_group; | |
258 | ||
259 | struct runq_stats runq_stats; | |
260 | }; | |
261 | ||
262 | #endif /* defined(CONFIG_SCHED_GRRR_CORE) */ | |
263 | ||
264 | #define first_timeslice(processor) ((processor)->timeslice > 0) | |
1c79356b | 265 | |
6d2010ae A |
266 | extern struct rt_queue rt_runq; |
267 | ||
268 | /* | |
269 | * Scheduler routines. | |
270 | */ | |
1c79356b | 271 | |
0b4e3aa0 A |
272 | /* Handle quantum expiration for an executing thread */ |
273 | extern void thread_quantum_expire( | |
274 | timer_call_param_t processor, | |
275 | timer_call_param_t thread); | |
276 | ||
c910b4d9 A |
277 | /* Context switch check for current processor */ |
278 | extern ast_t csw_check(processor_t processor); | |
9bccf70c | 279 | |
6d2010ae | 280 | #if defined(CONFIG_SCHED_TRADITIONAL) |
0b4e3aa0 A |
281 | extern uint32_t std_quantum, min_std_quantum; |
282 | extern uint32_t std_quantum_us; | |
6d2010ae A |
283 | #endif |
284 | ||
285 | extern uint32_t thread_depress_time; | |
286 | extern uint32_t default_timeshare_computation; | |
287 | extern uint32_t default_timeshare_constraint; | |
1c79356b | 288 | |
0b4e3aa0 | 289 | extern uint32_t max_rt_quantum, min_rt_quantum; |
1c79356b | 290 | |
91447636 A |
291 | extern uint32_t sched_cswtime; |
292 | ||
6d2010ae A |
293 | #if defined(CONFIG_SCHED_TRADITIONAL) |
294 | ||
1c79356b | 295 | /* |
91447636 | 296 | * Age usage (1 << SCHED_TICK_SHIFT) times per second. |
1c79356b | 297 | */ |
91447636 A |
298 | #define SCHED_TICK_SHIFT 3 |
299 | ||
300 | extern unsigned sched_tick; | |
301 | extern uint32_t sched_tick_interval; | |
302 | ||
6d2010ae A |
303 | #endif /* CONFIG_SCHED_TRADITIONAL */ |
304 | ||
305 | extern uint64_t sched_one_second_interval; | |
306 | ||
91447636 A |
307 | /* Periodic computation of various averages */ |
308 | extern void compute_averages(void); | |
1c79356b | 309 | |
91447636 A |
310 | extern void compute_averunnable( |
311 | void *nrun); | |
312 | ||
313 | extern void compute_stack_target( | |
314 | void *arg); | |
1c79356b | 315 | |
b0d623f7 A |
316 | extern void compute_memory_pressure( |
317 | void *arg); | |
318 | ||
6d2010ae A |
319 | extern void compute_zone_gc_throttle( |
320 | void *arg); | |
321 | ||
322 | extern void compute_pmap_gc_throttle( | |
323 | void *arg); | |
324 | ||
1c79356b | 325 | /* |
91447636 A |
326 | * Conversion factor from usage |
327 | * to priority. | |
328 | */ | |
6d2010ae | 329 | #if defined(CONFIG_SCHED_TRADITIONAL) |
91447636 | 330 | extern uint32_t sched_pri_shift; |
2d21ac55 | 331 | extern uint32_t sched_fixed_shift; |
91447636 | 332 | extern int8_t sched_load_shifts[NRQS]; |
6d2010ae | 333 | #endif |
1c79356b | 334 | |
91447636 | 335 | extern int32_t sched_poll_yield_shift; |
6d2010ae | 336 | extern uint64_t sched_safe_duration; |
1c79356b | 337 | |
2d21ac55 A |
338 | extern uint32_t sched_run_count, sched_share_count; |
339 | extern uint32_t sched_load_average, sched_mach_factor; | |
340 | ||
341 | extern uint32_t avenrun[3], mach_factor[3]; | |
342 | ||
91447636 A |
343 | extern uint64_t max_unsafe_computation; |
344 | extern uint64_t max_poll_computation; | |
1c79356b | 345 | |
2d21ac55 | 346 | #define sched_run_incr() \ |
6d2010ae A |
347 | MACRO_BEGIN \ |
348 | hw_atomic_add(&sched_run_count, 1); \ | |
c910b4d9 | 349 | MACRO_END |
2d21ac55 A |
350 | |
351 | #define sched_run_decr() \ | |
6d2010ae A |
352 | MACRO_BEGIN \ |
353 | hw_atomic_sub(&sched_run_count, 1); \ | |
c910b4d9 | 354 | MACRO_END |
2d21ac55 A |
355 | |
356 | #define sched_share_incr() \ | |
c910b4d9 A |
357 | MACRO_BEGIN \ |
358 | (void)hw_atomic_add(&sched_share_count, 1); \ | |
359 | MACRO_END | |
2d21ac55 A |
360 | |
361 | #define sched_share_decr() \ | |
c910b4d9 A |
362 | MACRO_BEGIN \ |
363 | (void)hw_atomic_sub(&sched_share_count, 1); \ | |
364 | MACRO_END | |
1c79356b A |
365 | |
366 | /* | |
367 | * thread_timer_delta macro takes care of both thread timers. | |
368 | */ | |
91447636 | 369 | #define thread_timer_delta(thread, delta) \ |
1c79356b | 370 | MACRO_BEGIN \ |
b0d623f7 | 371 | (delta) = (typeof(delta))timer_delta(&(thread)->system_timer, \ |
91447636 | 372 | &(thread)->system_timer_save); \ |
b0d623f7 | 373 | (delta) += (typeof(delta))timer_delta(&(thread)->user_timer, \ |
91447636 | 374 | &(thread)->user_timer_save); \ |
1c79356b A |
375 | MACRO_END |
376 | ||
1c79356b | 377 | #endif /* _KERN_SCHED_H_ */ |