2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr.
58 * Header file for scheduler.
62 #ifndef _KERN_SCHED_H_
63 #define _KERN_SCHED_H_
65 #include <stat_time.h>
67 #include <mach/policy.h>
68 #include <kern/kern_types.h>
69 #include <kern/queue.h>
70 #include <kern/lock.h>
71 #include <kern/macro_help.h>
72 #include <kern/timer_call.h>
75 #define NRQS 128 /* 128 levels per run queue */
76 #define NRQBM (NRQS / 32) /* number of words per bit map */
78 #define MAXPRI (NRQS-1)
79 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
80 #define IDLEPRI 0 /* idle thread priority */
81 #define DEPRESSPRI MINPRI /* depress priority */
84 * High-level priority assignments
86 *************************************************************************
87 * 127 Reserved (real-time)
93 * 96 Reserved (real-time)
100 * 80 Kernel mode only
101 * 79 System high priority
107 * 64 System high priority
108 * 63 Elevated priorities
114 * 52 Elevated priorities
115 * 51 Elevated priorities (incl. BSD +nice)
121 * 32 Elevated priorities (incl. BSD +nice)
122 * 31 Default (default base for threads)
123 * 30 Lowered priorities (incl. BSD -nice)
129 * 11 Lowered priorities (incl. BSD -nice)
130 * 10 Lowered priorities (aged pri's)
136 * 0 Lowered priorities (aged pri's / idle)
137 *************************************************************************
140 #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
141 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
143 #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
144 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
145 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
146 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
148 #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
149 #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
151 #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
152 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
153 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
154 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
155 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
156 #define MINPRI_USER MINPRI /* 0 */
159 * Macro to check for invalid priorities.
161 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
164 int highq
; /* highest runnable queue */
165 int bitmap
[NRQBM
]; /* run queue bitmap array */
166 int count
; /* # of threads total */
167 int urgency
; /* level of preemption urgency */
168 queue_head_t queues
[NRQS
]; /* one for each priority */
171 typedef struct run_queue
*run_queue_t
;
172 #define RUN_QUEUE_NULL ((run_queue_t) 0)
174 #define first_timeslice(processor) ((processor)->timeslice > 0)
176 #define processor_timeslice_setup(processor, thread) \
178 (processor)->timeslice = \
179 ((thread)->sched_mode & TH_MODE_TIMESHARE)? \
180 (processor)->processor_set->timeshare_quanta: 1; \
183 #define thread_quantum_init(thread) \
185 (thread)->current_quantum = \
186 ((thread)->sched_mode & TH_MODE_REALTIME)? \
187 (thread)->realtime.computation: std_quantum; \
190 /* Invoked at splsched by a thread on itself */
191 #define csw_needed(thread, processor) ( \
192 ((thread)->state & TH_SUSP) || \
193 (first_timeslice(processor)? \
194 ((processor)->runq.highq > (thread)->sched_pri || \
195 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
196 ((processor)->runq.highq >= (thread)->sched_pri || \
197 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
200 * Scheduler routines.
203 /* Remove thread from its run queue */
204 extern run_queue_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
;
248 * Scaling factor for usage
251 extern int8_t sched_load_shifts
[NRQS
];
253 extern int32_t sched_poll_yield_shift
;
254 extern uint32_t sched_safe_duration
;
256 extern uint64_t max_unsafe_computation
;
257 extern uint64_t max_poll_computation
;
259 extern uint32_t avenrun
[3], mach_factor
[3];
262 * thread_timer_delta macro takes care of both thread timers.
264 #define thread_timer_delta(thread, delta) \
266 (delta) = timer_delta(&(thread)->system_timer, \
267 &(thread)->system_timer_save); \
268 (delta) += timer_delta(&(thread)->user_timer, \
269 &(thread)->user_timer_save); \
272 #endif /* _KERN_SCHED_H_ */