2 * Copyright (c) 2000 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 <simple_clock.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>
78 * Statistical timing uses microseconds as timer units.
80 #define PRI_SHIFT (16 - SCHED_TICK_SHIFT)
85 * Otherwise machine provides shift(s) based on time units it uses.
87 #include <machine/sched_param.h>
89 #endif /* STAT_TIME */
91 #define NRQS 128 /* 128 levels per run queue */
92 #define NRQBM (NRQS / 32) /* number of words per bit map */
94 #define MAXPRI (NRQS-1)
95 #define MINPRI IDLEPRI /* lowest legal priority schedulable */
96 #define IDLEPRI 0 /* idle thread priority */
97 #define DEPRESSPRI MINPRI /* depress priority */
100 * High-level priority assignments
102 *************************************************************************
103 * 127 Reserved (real-time)
109 * 96 Reserved (real-time)
110 * 95 Kernel mode only
116 * 80 Kernel mode only
117 * 79 System high priority
123 * 64 System high priority
124 * 63 Elevated priorities
130 * 52 Elevated priorities
131 * 51 Elevated priorities (incl. BSD +nice)
137 * 32 Elevated priorities (incl. BSD +nice)
138 * 31 Default (default base for threads)
139 * 30 Lowered priorities (incl. BSD -nice)
145 * 11 Lowered priorities (incl. BSD -nice)
146 * 10 Lowered priorities (aged pri's)
152 * 0 Lowered priorities (aged pri's / idle)
153 *************************************************************************
156 #define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
158 #define MAXPRI_STANDARD (BASEPRI_REALTIME - 1) /* 95 */
160 #define MAXPRI_KERNEL MAXPRI_STANDARD /* 95 */
161 #define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
162 #define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
164 #define MAXPRI_SYSTEM (MINPRI_KERNEL - 1) /* 79 */
165 #define MINPRI_SYSTEM (MAXPRI_SYSTEM - (NRQS / 8) + 1) /* 64 */
167 #define MAXPRI_USER (MINPRI_SYSTEM - 1) /* 63 */
168 #define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
169 #define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
170 #define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
171 #define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
172 #define MINPRI_USER MINPRI /* 0 */
174 #define MINPRI_STANDARD MINPRI_USER /* 0 */
177 * Macro to check for invalid priorities.
179 #define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
182 queue_head_t queues
[NRQS
]; /* one for each priority */
183 decl_simple_lock_data(,lock
) /* one lock for all queues */
184 int bitmap
[NRQBM
]; /* run queue bitmap array */
185 int highq
; /* highest runnable queue */
186 int urgency
; /* level of preemption urgency */
187 int count
; /* # of threads in queue */
190 typedef struct run_queue
*run_queue_t
;
191 #define RUN_QUEUE_NULL ((run_queue_t) 0)
193 #define first_quantum(processor) ((processor)->slice_quanta > 0)
195 /* Invoked at splsched by a thread on itself */
196 #define csw_needed(thread, processor) ( \
197 ((thread)->state & TH_SUSP) || \
198 (first_quantum(processor)? \
199 ((processor)->runq.highq > (thread)->sched_pri || \
200 (processor)->processor_set->runq.highq > (thread)->sched_pri) : \
201 ((processor)->runq.highq >= (thread)->sched_pri || \
202 (processor)->processor_set->runq.highq >= (thread)->sched_pri)) )
205 * Scheduler routines.
208 /* Remove thread from its run queue */
209 extern run_queue_t
rem_runq(
212 /* Periodic computation of load factors */
213 extern void compute_mach_factor(void);
215 /* Handle quantum expiration for an executing thread */
216 extern void thread_quantum_expire(
217 timer_call_param_t processor
,
218 timer_call_param_t thread
);
220 /* Called at splsched by a thread on itself */
221 extern ast_t
csw_check(
223 processor_t processor
);
225 extern uint32_t std_quantum
, min_std_quantum
;
226 extern uint32_t std_quantum_us
;
228 extern uint32_t max_rt_quantum
, min_rt_quantum
;
231 * Shift structures for holding update shifts. Actual computation
232 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
233 * +/- is determined by the sign of shift 2.
240 typedef struct shift
*shift_t
, shift_data_t
;
243 * Age usage (1 << SCHED_TICK_SHIFT) times per second.
246 extern unsigned sched_tick
;
248 #define SCHED_TICK_SHIFT 3
250 #define SCHED_SCALE 128
251 #define SCHED_SHIFT 7
254 * thread_timer_delta macro takes care of both thread timers.
256 #define thread_timer_delta(thread) \
258 register uint32_t delta; \
261 TIMER_DELTA((thread)->system_timer, \
262 (thread)->system_timer_save, delta); \
263 TIMER_DELTA((thread)->user_timer, \
264 (thread)->user_timer_save, delta); \
265 (thread)->cpu_delta += delta; \
266 (thread)->sched_delta += (delta * \
267 (thread)->processor_set->sched_load); \
272 * sched_usec is an exponential average of number of microseconds
273 * in a second for clock drift compensation.
276 extern int sched_usec
;
277 #endif /* SIMPLE_CLOCK */
279 #endif /* _KERN_SCHED_H_ */