]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sched.h
xnu-2422.100.13.tar.gz
[apple/xnu.git] / osfmk / kern / sched.h
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: sched.h
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1985
62 *
63 * Header file for scheduler.
64 *
65 */
66
67#ifndef _KERN_SCHED_H_
68#define _KERN_SCHED_H_
69
1c79356b
A
70#include <mach/policy.h>
71#include <kern/kern_types.h>
72#include <kern/queue.h>
73#include <kern/lock.h>
74#include <kern/macro_help.h>
0b4e3aa0 75#include <kern/timer_call.h>
9bccf70c 76#include <kern/ast.h>
1c79356b 77
9bccf70c
A
78#define NRQS 128 /* 128 levels per run queue */
79#define NRQBM (NRQS / 32) /* number of words per bit map */
1c79356b
A
80
81#define MAXPRI (NRQS-1)
82#define MINPRI IDLEPRI /* lowest legal priority schedulable */
83#define IDLEPRI 0 /* idle thread priority */
1c79356b
A
84
85/*
86 * High-level priority assignments
87 *
88 *************************************************************************
89 * 127 Reserved (real-time)
90 * A
91 * +
92 * (32 levels)
93 * +
94 * V
95 * 96 Reserved (real-time)
96 * 95 Kernel mode only
97 * A
98 * +
99 * (16 levels)
100 * +
101 * V
102 * 80 Kernel mode only
0b4e3aa0 103 * 79 System high priority
1c79356b
A
104 * A
105 * +
106 * (16 levels)
107 * +
108 * V
0b4e3aa0 109 * 64 System high priority
1c79356b
A
110 * 63 Elevated priorities
111 * A
112 * +
113 * (12 levels)
114 * +
115 * V
116 * 52 Elevated priorities
117 * 51 Elevated priorities (incl. BSD +nice)
118 * A
119 * +
120 * (20 levels)
121 * +
122 * V
123 * 32 Elevated priorities (incl. BSD +nice)
124 * 31 Default (default base for threads)
125 * 30 Lowered priorities (incl. BSD -nice)
126 * A
127 * +
128 * (20 levels)
129 * +
130 * V
131 * 11 Lowered priorities (incl. BSD -nice)
132 * 10 Lowered priorities (aged pri's)
133 * A
134 * +
135 * (11 levels)
136 * +
137 * V
138 * 0 Lowered priorities (aged pri's / idle)
139 *************************************************************************
140 */
141
55e303ae 142#define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
1c79356b
A
143#define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
144
91447636 145#define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
0b4e3aa0 146#define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
55e303ae 147#define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
0b4e3aa0 148#define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
1c79356b 149
91447636 150#define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
39236c6e 151#define BASEPRI_GRAPHICS (MAXPRI_RESERVED - 3) /* 76 */
91447636 152#define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
1c79356b 153
91447636 154#define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
9bccf70c
A
155#define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
156#define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
157#define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
0b4e3aa0 158#define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
39236c6e 159#define MAXPRI_SUPPRESSED (BASEPRI_DEFAULT - 3) /* 28 */
b7266188 160#define MAXPRI_THROTTLE (MINPRI + 4) /* 4 */
0b4e3aa0 161#define MINPRI_USER MINPRI /* 0 */
1c79356b 162
d1ecb069 163#define DEPRESSPRI MINPRI /* depress priority */
d1ecb069 164
6d2010ae
A
165/* Type used for thread->sched_mode and saved_mode */
166typedef enum {
167 TH_MODE_NONE = 0, /* unassigned, usually for saved_mode only */
168 TH_MODE_REALTIME, /* time constraints supplied */
169 TH_MODE_FIXED, /* use fixed priorities, no decay */
170 TH_MODE_TIMESHARE, /* use timesharing algorithm */
171 TH_MODE_FAIRSHARE /* use fair-share scheduling */
172} sched_mode_t;
173
1c79356b
A
174/*
175 * Macro to check for invalid priorities.
176 */
177#define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
178
6d2010ae
A
179struct runq_stats {
180 uint64_t count_sum;
181 uint64_t last_change_timestamp;
182};
183
184#if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY)
185
1c79356b 186struct run_queue {
1c79356b 187 int highq; /* highest runnable queue */
55e303ae
A
188 int bitmap[NRQBM]; /* run queue bitmap array */
189 int count; /* # of threads total */
9bccf70c 190 int urgency; /* level of preemption urgency */
55e303ae 191 queue_head_t queues[NRQS]; /* one for each priority */
6d2010ae
A
192
193 struct runq_stats runq_stats;
1c79356b
A
194};
195
6d2010ae 196#endif /* defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY) */
1c79356b 197
6d2010ae
A
198struct rt_queue {
199 int count; /* # of threads total */
200 queue_head_t queue; /* all runnable RT threads */
55e303ae 201
6d2010ae
A
202 struct runq_stats runq_stats;
203};
204
205#if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_PROTO) || defined(CONFIG_SCHED_FIXEDPRIORITY)
206struct fairshare_queue {
207 int count; /* # of threads total */
208 queue_head_t queue; /* all runnable threads demoted to fairshare scheduling */
209
210 struct runq_stats runq_stats;
211};
212#endif
0b4e3aa0 213
6d2010ae 214#if defined(CONFIG_SCHED_GRRR_CORE)
1c79356b
A
215
216/*
6d2010ae
A
217 * We map standard Mach priorities to an abstract scale that more properly
218 * indicates how we want processor time allocated under contention.
1c79356b 219 */
6d2010ae
A
220typedef uint8_t grrr_proportional_priority_t;
221typedef uint8_t grrr_group_index_t;
222
223#define NUM_GRRR_PROPORTIONAL_PRIORITIES 256
224#define MAX_GRRR_PROPORTIONAL_PRIORITY ((grrr_proportional_priority_t)255)
225
226#if 0
227#define NUM_GRRR_GROUPS 8 /* log(256) */
228#endif
229
230#define NUM_GRRR_GROUPS 64 /* 256/4 */
231
232struct grrr_group {
233 queue_chain_t priority_order; /* next greatest weight group */
234 grrr_proportional_priority_t minpriority;
235 grrr_group_index_t index;
236
237 queue_head_t clients;
238 int count;
239 uint32_t weight;
240#if 0
241 uint32_t deferred_removal_weight;
242#endif
243 uint32_t work;
244 thread_t current_client;
245};
246
247struct grrr_run_queue {
248 int count;
249 uint32_t last_rescale_tick;
250 struct grrr_group groups[NUM_GRRR_GROUPS];
251 queue_head_t sorted_group_list;
252 uint32_t weight;
253 grrr_group_t current_group;
254
255 struct runq_stats runq_stats;
256};
257
258#endif /* defined(CONFIG_SCHED_GRRR_CORE) */
259
260#define first_timeslice(processor) ((processor)->timeslice > 0)
1c79356b 261
6d2010ae
A
262extern struct rt_queue rt_runq;
263
264/*
265 * Scheduler routines.
266 */
1c79356b 267
0b4e3aa0
A
268/* Handle quantum expiration for an executing thread */
269extern void thread_quantum_expire(
270 timer_call_param_t processor,
271 timer_call_param_t thread);
272
c910b4d9
A
273/* Context switch check for current processor */
274extern ast_t csw_check(processor_t processor);
9bccf70c 275
6d2010ae 276#if defined(CONFIG_SCHED_TRADITIONAL)
0b4e3aa0
A
277extern uint32_t std_quantum, min_std_quantum;
278extern uint32_t std_quantum_us;
6d2010ae
A
279#endif
280
281extern uint32_t thread_depress_time;
282extern uint32_t default_timeshare_computation;
283extern uint32_t default_timeshare_constraint;
1c79356b 284
0b4e3aa0 285extern uint32_t max_rt_quantum, min_rt_quantum;
1c79356b 286
316670eb
A
287extern int default_preemption_rate;
288extern int default_bg_preemption_rate;
91447636 289
6d2010ae
A
290#if defined(CONFIG_SCHED_TRADITIONAL)
291
1c79356b 292/*
39236c6e
A
293 * Age usage at approximately (1 << SCHED_TICK_SHIFT) times per second
294 * Aging may be deferred during periods where all processors are idle
295 * and cumulatively applied during periods of activity.
1c79356b 296 */
91447636 297#define SCHED_TICK_SHIFT 3
39236c6e 298#define SCHED_TICK_MAX_DELTA (8)
91447636
A
299
300extern unsigned sched_tick;
301extern uint32_t sched_tick_interval;
302
6d2010ae
A
303#endif /* CONFIG_SCHED_TRADITIONAL */
304
305extern uint64_t sched_one_second_interval;
306
91447636 307/* Periodic computation of various averages */
39236c6e 308extern void compute_averages(uint64_t);
1c79356b 309
91447636
A
310extern void compute_averunnable(
311 void *nrun);
312
313extern void compute_stack_target(
314 void *arg);
1c79356b 315
b0d623f7
A
316extern void compute_memory_pressure(
317 void *arg);
318
6d2010ae
A
319extern void compute_zone_gc_throttle(
320 void *arg);
321
316670eb
A
322extern void compute_pageout_gc_throttle(
323 void *arg);
324
6d2010ae
A
325extern void compute_pmap_gc_throttle(
326 void *arg);
327
1c79356b 328/*
91447636
A
329 * Conversion factor from usage
330 * to priority.
331 */
6d2010ae 332#if defined(CONFIG_SCHED_TRADITIONAL)
91447636 333extern uint32_t sched_pri_shift;
39236c6e
A
334extern uint32_t sched_background_pri_shift;
335extern uint32_t sched_combined_fgbg_pri_shift;
2d21ac55 336extern uint32_t sched_fixed_shift;
91447636 337extern int8_t sched_load_shifts[NRQS];
39236c6e
A
338extern uint32_t sched_decay_usage_age_factor;
339extern uint32_t sched_use_combined_fgbg_decay;
340void sched_traditional_consider_maintenance(uint64_t);
6d2010ae 341#endif
1c79356b 342
91447636 343extern int32_t sched_poll_yield_shift;
6d2010ae 344extern uint64_t sched_safe_duration;
1c79356b 345
39236c6e 346extern uint32_t sched_run_count, sched_share_count, sched_background_count;
2d21ac55
A
347extern uint32_t sched_load_average, sched_mach_factor;
348
349extern uint32_t avenrun[3], mach_factor[3];
350
91447636
A
351extern uint64_t max_unsafe_computation;
352extern uint64_t max_poll_computation;
1c79356b 353
2d21ac55 354#define sched_run_incr() \
6d2010ae
A
355MACRO_BEGIN \
356 hw_atomic_add(&sched_run_count, 1); \
c910b4d9 357MACRO_END
2d21ac55
A
358
359#define sched_run_decr() \
6d2010ae
A
360MACRO_BEGIN \
361 hw_atomic_sub(&sched_run_count, 1); \
c910b4d9 362MACRO_END
2d21ac55
A
363
364#define sched_share_incr() \
c910b4d9
A
365MACRO_BEGIN \
366 (void)hw_atomic_add(&sched_share_count, 1); \
367MACRO_END
2d21ac55
A
368
369#define sched_share_decr() \
c910b4d9
A
370MACRO_BEGIN \
371 (void)hw_atomic_sub(&sched_share_count, 1); \
372MACRO_END
1c79356b 373
39236c6e
A
374#define sched_background_incr() \
375MACRO_BEGIN \
376 (void)hw_atomic_add(&sched_background_count, 1); \
377MACRO_END
378
379#define sched_background_decr() \
380MACRO_BEGIN \
381 (void)hw_atomic_sub(&sched_background_count, 1); \
382MACRO_END
383
1c79356b
A
384/*
385 * thread_timer_delta macro takes care of both thread timers.
386 */
91447636 387#define thread_timer_delta(thread, delta) \
1c79356b 388MACRO_BEGIN \
b0d623f7 389 (delta) = (typeof(delta))timer_delta(&(thread)->system_timer, \
91447636 390 &(thread)->system_timer_save); \
b0d623f7 391 (delta) += (typeof(delta))timer_delta(&(thread)->user_timer, \
91447636 392 &(thread)->user_timer_save); \
1c79356b
A
393MACRO_END
394
1c79356b 395#endif /* _KERN_SCHED_H_ */