2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * Header file for scheduler.
67 #ifndef _KERN_SCHED_H_
68 #define _KERN_SCHED_H_
70 #include <mach/policy.h>
71 #include <kern/kern_types.h>
72 #include <kern/queue.h>
73 #include <kern/lock.h>
74 #include <kern/macro_help.h>
75 #include <kern/timer_call.h>
78 #define NRQS 128 /* 128 levels per run queue */
79 #define NRQBM (NRQS / 32) /* number of words per bit map */
81 #define MAXPRI (NRQS-1)
82 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
83 #define IDLEPRI 0 /* idle thread priority */
86 * High-level priority assignments
88 *************************************************************************
89 * 127 Reserved (real-time)
95 * 96 Reserved (real-time)
102 * 80 Kernel mode only
103 * 79 System high priority
109 * 64 System high priority
110 * 63 Elevated priorities
116 * 52 Elevated priorities
117 * 51 Elevated priorities (incl. BSD +nice)
123 * 32 Elevated priorities (incl. BSD +nice)
124 * 31 Default (default base for threads)
125 * 30 Lowered priorities (incl. BSD -nice)
131 * 11 Lowered priorities (incl. BSD -nice)
132 * 10 Lowered priorities (aged pri's)
138 * 0 Lowered priorities (aged pri's / idle)
139 *************************************************************************
142 #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
143 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
145 #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
146 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
147 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
148 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
150 #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
151 #define BASEPRI_GRAPHICS (MAXPRI_RESERVED - 3) /* 76 */
152 #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
154 #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
155 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
156 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
157 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
158 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
159 #define MAXPRI_SUPPRESSED (BASEPRI_DEFAULT - 3) /* 28 */
160 #define MAXPRI_THROTTLE (MINPRI + 4) /* 4 */
161 #define MINPRI_USER MINPRI /* 0 */
163 #define DEPRESSPRI MINPRI /* depress priority */
165 /* Type used for thread->sched_mode and saved_mode */
167 TH_MODE_NONE
= 0, /* unassigned, usually for saved_mode only */
168 TH_MODE_REALTIME
, /* time constraints supplied */
169 TH_MODE_FIXED
, /* use fixed priorities, no decay */
170 TH_MODE_TIMESHARE
, /* use timesharing algorithm */
171 TH_MODE_FAIRSHARE
/* use fair-share scheduling */
175 * Macro to check for invalid priorities.
177 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
181 uint64_t last_change_timestamp
;
184 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY)
187 int highq
; /* highest runnable queue */
188 int bitmap
[NRQBM
]; /* run queue bitmap array */
189 int count
; /* # of threads total */
190 int urgency
; /* level of preemption urgency */
191 queue_head_t queues
[NRQS
]; /* one for each priority */
193 struct runq_stats runq_stats
;
196 #endif /* defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY) */
199 int count
; /* # of threads total */
200 queue_head_t queue
; /* all runnable RT threads */
202 struct runq_stats runq_stats
;
205 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY)
206 struct fairshare_queue
{
207 int count
; /* # of threads total */
208 queue_head_t queue
; /* all runnable threads demoted to fairshare scheduling */
210 struct runq_stats runq_stats
;
214 #if defined(CONFIG_SCHED_GRRR_CORE)
217 * We map standard Mach priorities to an abstract scale that more properly
218 * indicates how we want processor time allocated under contention.
220 typedef uint8_t grrr_proportional_priority_t
;
221 typedef uint8_t grrr_group_index_t
;
223 #define NUM_GRRR_PROPORTIONAL_PRIORITIES 256
224 #define MAX_GRRR_PROPORTIONAL_PRIORITY ((grrr_proportional_priority_t)255)
227 #define NUM_GRRR_GROUPS 8 /* log(256) */
230 #define NUM_GRRR_GROUPS 64 /* 256/4 */
233 queue_chain_t priority_order
; /* next greatest weight group */
234 grrr_proportional_priority_t minpriority
;
235 grrr_group_index_t index
;
237 queue_head_t clients
;
241 uint32_t deferred_removal_weight
;
244 thread_t current_client
;
247 struct grrr_run_queue
{
249 uint32_t last_rescale_tick
;
250 struct grrr_group groups
[NUM_GRRR_GROUPS
];
251 queue_head_t sorted_group_list
;
253 grrr_group_t current_group
;
255 struct runq_stats runq_stats
;
258 #endif /* defined(CONFIG_SCHED_GRRR_CORE) */
260 #define first_timeslice(processor) ((processor)->timeslice > 0)
262 extern struct rt_queue rt_runq
;
265 * Scheduler routines.
268 /* Handle quantum expiration for an executing thread */
269 extern void thread_quantum_expire(
270 timer_call_param_t processor
,
271 timer_call_param_t thread
);
273 /* Context switch check for current processor */
274 extern ast_t
csw_check(processor_t processor
);
276 #if defined(CONFIG_SCHED_TRADITIONAL)
277 extern uint32_t std_quantum
, min_std_quantum
;
278 extern uint32_t std_quantum_us
;
281 extern uint32_t thread_depress_time
;
282 extern uint32_t default_timeshare_computation
;
283 extern uint32_t default_timeshare_constraint
;
285 extern uint32_t max_rt_quantum
, min_rt_quantum
;
287 extern int default_preemption_rate
;
288 extern int default_bg_preemption_rate
;
290 #if defined(CONFIG_SCHED_TRADITIONAL)
293 * Age usage at approximately (1 << SCHED_TICK_SHIFT) times per second
294 * Aging may be deferred during periods where all processors are idle
295 * and cumulatively applied during periods of activity.
297 #define SCHED_TICK_SHIFT 3
298 #define SCHED_TICK_MAX_DELTA (8)
300 extern unsigned sched_tick
;
301 extern uint32_t sched_tick_interval
;
303 #endif /* CONFIG_SCHED_TRADITIONAL */
305 extern uint64_t sched_one_second_interval
;
307 /* Periodic computation of various averages */
308 extern void compute_averages(uint64_t);
310 extern void compute_averunnable(
313 extern void compute_stack_target(
316 extern void compute_memory_pressure(
319 extern void compute_zone_gc_throttle(
322 extern void compute_pageout_gc_throttle(
325 extern void compute_pmap_gc_throttle(
329 * Conversion factor from usage
332 #if defined(CONFIG_SCHED_TRADITIONAL)
333 extern uint32_t sched_pri_shift
;
334 extern uint32_t sched_background_pri_shift
;
335 extern uint32_t sched_combined_fgbg_pri_shift
;
336 extern uint32_t sched_fixed_shift
;
337 extern int8_t sched_load_shifts
[NRQS
];
338 extern uint32_t sched_decay_usage_age_factor
;
339 extern uint32_t sched_use_combined_fgbg_decay
;
340 void sched_traditional_consider_maintenance(uint64_t);
343 extern int32_t sched_poll_yield_shift
;
344 extern uint64_t sched_safe_duration
;
346 extern uint32_t sched_run_count
, sched_share_count
, sched_background_count
;
347 extern uint32_t sched_load_average
, sched_mach_factor
;
349 extern uint32_t avenrun
[3], mach_factor
[3];
351 extern uint64_t max_unsafe_computation
;
352 extern uint64_t max_poll_computation
;
354 #define sched_run_incr() \
356 hw_atomic_add(&sched_run_count, 1); \
359 #define sched_run_decr() \
361 hw_atomic_sub(&sched_run_count, 1); \
364 #define sched_share_incr() \
366 (void)hw_atomic_add(&sched_share_count, 1); \
369 #define sched_share_decr() \
371 (void)hw_atomic_sub(&sched_share_count, 1); \
374 #define sched_background_incr() \
376 (void)hw_atomic_add(&sched_background_count, 1); \
379 #define sched_background_decr() \
381 (void)hw_atomic_sub(&sched_background_count, 1); \
385 * thread_timer_delta macro takes care of both thread timers.
387 #define thread_timer_delta(thread, delta) \
389 (delta) = (typeof(delta))timer_delta(&(thread)->system_timer, \
390 &(thread)->system_timer_save); \
391 (delta) += (typeof(delta))timer_delta(&(thread)->user_timer, \
392 &(thread)->user_timer_save); \
395 #endif /* _KERN_SCHED_H_ */