2 * Copyright (c) 2000-2007 Apple Computer, 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 <stat_time.h>
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>
77 #include <kern/timer_call.h>
80 #define NRQS 128 /* 128 levels per run queue */
81 #define NRQBM (NRQS / 32) /* number of words per bit map */
83 #define MAXPRI (NRQS-1)
84 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
85 #define IDLEPRI 0 /* idle thread priority */
86 #define DEPRESSPRI MINPRI /* depress priority */
89 * High-level priority assignments
91 *************************************************************************
92 * 127 Reserved (real-time)
98 * 96 Reserved (real-time)
105 * 80 Kernel mode only
106 * 79 System high priority
112 * 64 System high priority
113 * 63 Elevated priorities
119 * 52 Elevated priorities
120 * 51 Elevated priorities (incl. BSD +nice)
126 * 32 Elevated priorities (incl. BSD +nice)
127 * 31 Default (default base for threads)
128 * 30 Lowered priorities (incl. BSD -nice)
134 * 11 Lowered priorities (incl. BSD -nice)
135 * 10 Lowered priorities (aged pri's)
141 * 0 Lowered priorities (aged pri's / idle)
142 *************************************************************************
145 #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
146 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
148 #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
149 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
150 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
151 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
153 #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
154 #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
156 #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
157 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
158 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
159 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
160 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
161 #define MINPRI_USER MINPRI /* 0 */
164 * Macro to check for invalid priorities.
166 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
169 int highq
; /* highest runnable queue */
170 int bitmap
[NRQBM
]; /* run queue bitmap array */
171 int count
; /* # of threads total */
172 int urgency
; /* level of preemption urgency */
173 queue_head_t queues
[NRQS
]; /* one for each priority */
176 typedef struct run_queue
*run_queue_t
;
177 #define RUN_QUEUE_NULL ((run_queue_t) 0)
179 #define first_timeslice(processor) ((processor)->timeslice > 0)
181 #define thread_quantum_init(thread) \
183 (thread)->current_quantum = \
184 ((thread)->sched_mode & TH_MODE_REALTIME)? \
185 (thread)->realtime.computation: std_quantum; \
188 /* Invoked at splsched by a thread on itself */
189 #define csw_needed(thread, processor) ( \
190 ((thread)->state & TH_SUSP) || \
191 (first_timeslice(processor)? \
192 ((processor)->runq.highq > (thread)->sched_pri || \
193 rt_runq.highq > (thread)->sched_pri) : \
194 ((processor)->runq.highq >= (thread)->sched_pri || \
195 rt_runq.highq >= (thread)->sched_pri)) )
197 extern struct run_queue rt_runq
;
200 * Scheduler routines.
203 /* Remove thread from its run queue */
204 extern boolean_t
run_queue_remove(
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
);
212 /* Called at splsched by a thread on itself */
213 extern ast_t
csw_check(
215 processor_t processor
);
217 extern uint32_t std_quantum
, min_std_quantum
;
218 extern uint32_t std_quantum_us
;
220 extern uint32_t max_rt_quantum
, min_rt_quantum
;
222 extern uint32_t sched_cswtime
;
225 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
227 #define SCHED_TICK_SHIFT 3
229 extern unsigned sched_tick
;
230 extern uint32_t sched_tick_interval
;
232 /* Periodic computation of various averages */
233 extern void compute_averages(void);
235 extern void compute_averunnable(
238 extern void compute_stack_target(
242 * Conversion factor from usage
245 extern uint32_t sched_pri_shift
;
246 extern uint32_t sched_fixed_shift
;
247 extern int8_t sched_load_shifts
[NRQS
];
249 extern int32_t sched_poll_yield_shift
;
250 extern uint32_t sched_safe_duration
;
252 extern uint32_t sched_run_count
, sched_share_count
;
253 extern uint32_t sched_load_average
, sched_mach_factor
;
255 extern uint32_t avenrun
[3], mach_factor
[3];
257 extern uint64_t max_unsafe_computation
;
258 extern uint64_t max_poll_computation
;
260 #define sched_run_incr() \
261 (void)hw_atomic_add(&sched_run_count, 1)
263 #define sched_run_decr() \
264 (void)hw_atomic_sub(&sched_run_count, 1)
266 #define sched_share_incr() \
267 (void)hw_atomic_add(&sched_share_count, 1)
269 #define sched_share_decr() \
270 (void)hw_atomic_sub(&sched_share_count, 1)
273 * thread_timer_delta macro takes care of both thread timers.
275 #define thread_timer_delta(thread, delta) \
277 (delta) = timer_delta(&(thread)->system_timer, \
278 &(thread)->system_timer_save); \
279 (delta) += timer_delta(&(thread)->user_timer, \
280 &(thread)->user_timer_save); \
283 #endif /* _KERN_SCHED_H_ */