2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr.
65 * Header file for scheduler.
69 #ifndef _KERN_SCHED_H_
70 #define _KERN_SCHED_H_
72 #include <stat_time.h>
74 #include <mach/policy.h>
75 #include <kern/kern_types.h>
76 #include <kern/queue.h>
77 #include <kern/lock.h>
78 #include <kern/macro_help.h>
79 #include <kern/timer_call.h>
82 #define NRQS 128 /* 128 levels per run queue */
83 #define NRQBM (NRQS / 32) /* number of words per bit map */
85 #define MAXPRI (NRQS-1)
86 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
87 #define IDLEPRI 0 /* idle thread priority */
88 #define DEPRESSPRI MINPRI /* depress priority */
91 * High-level priority assignments
93 *************************************************************************
94 * 127 Reserved (real-time)
100 * 96 Reserved (real-time)
101 * 95 Kernel mode only
107 * 80 Kernel mode only
108 * 79 System high priority
114 * 64 System high priority
115 * 63 Elevated priorities
121 * 52 Elevated priorities
122 * 51 Elevated priorities (incl. BSD +nice)
128 * 32 Elevated priorities (incl. BSD +nice)
129 * 31 Default (default base for threads)
130 * 30 Lowered priorities (incl. BSD -nice)
136 * 11 Lowered priorities (incl. BSD -nice)
137 * 10 Lowered priorities (aged pri's)
143 * 0 Lowered priorities (aged pri's / idle)
144 *************************************************************************
147 #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
148 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
150 #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
151 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
152 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
153 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
155 #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
156 #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
158 #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
159 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
160 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
161 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
162 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
163 #define MINPRI_USER MINPRI /* 0 */
166 * Macro to check for invalid priorities.
168 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
171 int highq
; /* highest runnable queue */
172 int bitmap
[NRQBM
]; /* run queue bitmap array */
173 int count
; /* # of threads total */
174 int urgency
; /* level of preemption urgency */
175 queue_head_t queues
[NRQS
]; /* one for each priority */
178 typedef struct run_queue
*run_queue_t
;
179 #define RUN_QUEUE_NULL ((run_queue_t) 0)
181 #define first_timeslice(processor) ((processor)->timeslice > 0)
183 #define processor_timeslice_setup(processor, thread) \
185 (processor)->timeslice = \
186 ((thread)->sched_mode & TH_MODE_TIMESHARE)? \
187 (processor)->processor_set->timeshare_quanta: 1; \
190 #define thread_quantum_init(thread) \
192 (thread)->current_quantum = \
193 ((thread)->sched_mode & TH_MODE_REALTIME)? \
194 (thread)->realtime.computation: std_quantum; \
197 /* Invoked at splsched by a thread on itself */
198 #define csw_needed(thread, processor) ( \
199 ((thread)->state & TH_SUSP) || \
200 (first_timeslice(processor)? \
201 ((processor)->runq.highq > (thread)->sched_pri || \
202 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
203 ((processor)->runq.highq >= (thread)->sched_pri || \
204 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
207 * Scheduler routines.
210 /* Remove thread from its run queue */
211 extern run_queue_t
run_queue_remove(
214 /* Handle quantum expiration for an executing thread */
215 extern void thread_quantum_expire(
216 timer_call_param_t processor
,
217 timer_call_param_t thread
);
219 /* Called at splsched by a thread on itself */
220 extern ast_t
csw_check(
222 processor_t processor
);
224 extern uint32_t std_quantum
, min_std_quantum
;
225 extern uint32_t std_quantum_us
;
227 extern uint32_t max_rt_quantum
, min_rt_quantum
;
229 extern uint32_t sched_cswtime
;
232 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
234 #define SCHED_TICK_SHIFT 3
236 extern unsigned sched_tick
;
237 extern uint32_t sched_tick_interval
;
239 /* Periodic computation of various averages */
240 extern void compute_averages(void);
242 extern void compute_averunnable(
245 extern void compute_stack_target(
249 * Conversion factor from usage
252 extern uint32_t sched_pri_shift
;
255 * Scaling factor for usage
258 extern int8_t sched_load_shifts
[NRQS
];
260 extern int32_t sched_poll_yield_shift
;
261 extern uint32_t sched_safe_duration
;
263 extern uint64_t max_unsafe_computation
;
264 extern uint64_t max_poll_computation
;
266 extern uint32_t avenrun
[3], mach_factor
[3];
269 * thread_timer_delta macro takes care of both thread timers.
271 #define thread_timer_delta(thread, delta) \
273 (delta) = timer_delta(&(thread)->system_timer, \
274 &(thread)->system_timer_save); \
275 (delta) += timer_delta(&(thread)->user_timer, \
276 &(thread)->user_timer_save); \
279 #endif /* _KERN_SCHED_H_ */