2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr.
57 * Header file for scheduler.
61 #ifndef _KERN_SCHED_H_
62 #define _KERN_SCHED_H_
64 #include <stat_time.h>
66 #include <mach/policy.h>
67 #include <kern/kern_types.h>
68 #include <kern/queue.h>
69 #include <kern/lock.h>
70 #include <kern/macro_help.h>
71 #include <kern/timer_call.h>
74 #define NRQS 128 /* 128 levels per run queue */
75 #define NRQBM (NRQS / 32) /* number of words per bit map */
77 #define MAXPRI (NRQS-1)
78 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
79 #define IDLEPRI 0 /* idle thread priority */
80 #define DEPRESSPRI MINPRI /* depress priority */
83 * High-level priority assignments
85 *************************************************************************
86 * 127 Reserved (real-time)
92 * 96 Reserved (real-time)
100 * 79 System high priority
106 * 64 System high priority
107 * 63 Elevated priorities
113 * 52 Elevated priorities
114 * 51 Elevated priorities (incl. BSD +nice)
120 * 32 Elevated priorities (incl. BSD +nice)
121 * 31 Default (default base for threads)
122 * 30 Lowered priorities (incl. BSD -nice)
128 * 11 Lowered priorities (incl. BSD -nice)
129 * 10 Lowered priorities (aged pri's)
135 * 0 Lowered priorities (aged pri's / idle)
136 *************************************************************************
139 #define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
140 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
142 #define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
143 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
144 #define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
145 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
147 #define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
148 #define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
150 #define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
151 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
152 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
153 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
154 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
155 #define MINPRI_USER MINPRI /* 0 */
158 * Macro to check for invalid priorities.
160 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
163 int highq
; /* highest runnable queue */
164 int bitmap
[NRQBM
]; /* run queue bitmap array */
165 int count
; /* # of threads total */
166 int urgency
; /* level of preemption urgency */
167 queue_head_t queues
[NRQS
]; /* one for each priority */
170 typedef struct run_queue
*run_queue_t
;
171 #define RUN_QUEUE_NULL ((run_queue_t) 0)
173 #define first_timeslice(processor) ((processor)->timeslice > 0)
175 #define processor_timeslice_setup(processor, thread) \
177 (processor)->timeslice = \
178 ((thread)->sched_mode & TH_MODE_TIMESHARE)? \
179 (processor)->processor_set->timeshare_quanta: 1; \
182 #define thread_quantum_init(thread) \
184 (thread)->current_quantum = \
185 ((thread)->sched_mode & TH_MODE_REALTIME)? \
186 (thread)->realtime.computation: std_quantum; \
189 /* Invoked at splsched by a thread on itself */
190 #define csw_needed(thread, processor) ( \
191 ((thread)->state & TH_SUSP) || \
192 (first_timeslice(processor)? \
193 ((processor)->runq.highq > (thread)->sched_pri || \
194 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
195 ((processor)->runq.highq >= (thread)->sched_pri || \
196 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
199 * Scheduler routines.
202 /* Remove thread from its run queue */
203 extern run_queue_t
run_queue_remove(
206 /* Handle quantum expiration for an executing thread */
207 extern void thread_quantum_expire(
208 timer_call_param_t processor
,
209 timer_call_param_t thread
);
211 /* Called at splsched by a thread on itself */
212 extern ast_t
csw_check(
214 processor_t processor
);
216 extern uint32_t std_quantum
, min_std_quantum
;
217 extern uint32_t std_quantum_us
;
219 extern uint32_t max_rt_quantum
, min_rt_quantum
;
221 extern uint32_t sched_cswtime
;
224 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
226 #define SCHED_TICK_SHIFT 3
228 extern unsigned sched_tick
;
229 extern uint32_t sched_tick_interval
;
231 /* Periodic computation of various averages */
232 extern void compute_averages(void);
234 extern void compute_averunnable(
237 extern void compute_stack_target(
241 * Conversion factor from usage
244 extern uint32_t sched_pri_shift
;
247 * Scaling factor for usage
250 extern int8_t sched_load_shifts
[NRQS
];
252 extern int32_t sched_poll_yield_shift
;
253 extern uint32_t sched_safe_duration
;
255 extern uint64_t max_unsafe_computation
;
256 extern uint64_t max_poll_computation
;
258 extern uint32_t avenrun
[3], mach_factor
[3];
261 * thread_timer_delta macro takes care of both thread timers.
263 #define thread_timer_delta(thread, delta) \
265 (delta) = timer_delta(&(thread)->system_timer, \
266 &(thread)->system_timer_save); \
267 (delta) += timer_delta(&(thread)->user_timer, \
268 &(thread)->user_timer_save); \
271 #endif /* _KERN_SCHED_H_ */