]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sched.h
xnu-2782.40.9.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>
1c79356b 73#include <kern/macro_help.h>
0b4e3aa0 74#include <kern/timer_call.h>
9bccf70c 75#include <kern/ast.h>
1c79356b 76
9bccf70c
A
77#define NRQS 128 /* 128 levels per run queue */
78#define NRQBM (NRQS / 32) /* number of words per bit map */
1c79356b
A
79
80#define MAXPRI (NRQS-1)
81#define MINPRI IDLEPRI /* lowest legal priority schedulable */
82#define IDLEPRI 0 /* idle thread priority */
1c79356b
A
83
84/*
85 * High-level priority assignments
86 *
87 *************************************************************************
88 * 127 Reserved (real-time)
89 * A
90 * +
91 * (32 levels)
92 * +
93 * V
94 * 96 Reserved (real-time)
95 * 95 Kernel mode only
96 * A
97 * +
98 * (16 levels)
99 * +
100 * V
101 * 80 Kernel mode only
0b4e3aa0 102 * 79 System high priority
1c79356b
A
103 * A
104 * +
105 * (16 levels)
106 * +
107 * V
0b4e3aa0 108 * 64 System high priority
1c79356b
A
109 * 63 Elevated priorities
110 * A
111 * +
112 * (12 levels)
113 * +
114 * V
115 * 52 Elevated priorities
116 * 51 Elevated priorities (incl. BSD +nice)
117 * A
118 * +
119 * (20 levels)
120 * +
121 * V
122 * 32 Elevated priorities (incl. BSD +nice)
123 * 31 Default (default base for threads)
124 * 30 Lowered priorities (incl. BSD -nice)
125 * A
126 * +
127 * (20 levels)
128 * +
129 * V
130 * 11 Lowered priorities (incl. BSD -nice)
131 * 10 Lowered priorities (aged pri's)
132 * A
133 * +
134 * (11 levels)
135 * +
136 * V
137 * 0 Lowered priorities (aged pri's / idle)
138 *************************************************************************
139 */
140
55e303ae 141#define BASEPRI_RTQUEUES (BASEPRI_REALTIME + 1) /* 97 */
1c79356b
A
142#define BASEPRI_REALTIME (MAXPRI - (NRQS / 4) + 1) /* 96 */
143
91447636 144#define MAXPRI_KERNEL (BASEPRI_REALTIME - 1) /* 95 */
0b4e3aa0 145#define BASEPRI_PREEMPT (MAXPRI_KERNEL - 2) /* 93 */
55e303ae 146#define BASEPRI_KERNEL (MINPRI_KERNEL + 1) /* 81 */
0b4e3aa0 147#define MINPRI_KERNEL (MAXPRI_KERNEL - (NRQS / 8) + 1) /* 80 */
1c79356b 148
91447636 149#define MAXPRI_RESERVED (MINPRI_KERNEL - 1) /* 79 */
39236c6e 150#define BASEPRI_GRAPHICS (MAXPRI_RESERVED - 3) /* 76 */
91447636 151#define MINPRI_RESERVED (MAXPRI_RESERVED - (NRQS / 8) + 1) /* 64 */
1c79356b 152
91447636 153#define MAXPRI_USER (MINPRI_RESERVED - 1) /* 63 */
9bccf70c
A
154#define BASEPRI_CONTROL (BASEPRI_DEFAULT + 17) /* 48 */
155#define BASEPRI_FOREGROUND (BASEPRI_DEFAULT + 16) /* 47 */
156#define BASEPRI_BACKGROUND (BASEPRI_DEFAULT + 15) /* 46 */
fe8ab488 157#define BASEPRI_USER_INITIATED (BASEPRI_DEFAULT + 6) /* 37 */
0b4e3aa0 158#define BASEPRI_DEFAULT (MAXPRI_USER - (NRQS / 4)) /* 31 */
39236c6e 159#define MAXPRI_SUPPRESSED (BASEPRI_DEFAULT - 3) /* 28 */
fe8ab488 160#define BASEPRI_UTILITY (BASEPRI_DEFAULT - 11) /* 20 */
b7266188 161#define MAXPRI_THROTTLE (MINPRI + 4) /* 4 */
0b4e3aa0 162#define MINPRI_USER MINPRI /* 0 */
1c79356b 163
d1ecb069 164#define DEPRESSPRI MINPRI /* depress priority */
fe8ab488 165#define MAXPRI_PROMOTE (MAXPRI_KERNEL) /* ceiling for mutex promotion */
d1ecb069 166
6d2010ae
A
167/* Type used for thread->sched_mode and saved_mode */
168typedef enum {
169 TH_MODE_NONE = 0, /* unassigned, usually for saved_mode only */
170 TH_MODE_REALTIME, /* time constraints supplied */
171 TH_MODE_FIXED, /* use fixed priorities, no decay */
172 TH_MODE_TIMESHARE, /* use timesharing algorithm */
173 TH_MODE_FAIRSHARE /* use fair-share scheduling */
174} sched_mode_t;
175
1c79356b
A
176/*
177 * Macro to check for invalid priorities.
178 */
179#define invalid_pri(pri) ((pri) < MINPRI || (pri) > MAXPRI)
180
6d2010ae
A
181struct runq_stats {
182 uint64_t count_sum;
183 uint64_t last_change_timestamp;
184};
185
fe8ab488 186#if defined(CONFIG_SCHED_TIMESHARE_CORE) || defined(CONFIG_SCHED_PROTO)
6d2010ae 187
1c79356b 188struct run_queue {
1c79356b 189 int highq; /* highest runnable queue */
55e303ae
A
190 int bitmap[NRQBM]; /* run queue bitmap array */
191 int count; /* # of threads total */
9bccf70c 192 int urgency; /* level of preemption urgency */
55e303ae 193 queue_head_t queues[NRQS]; /* one for each priority */
6d2010ae
A
194
195 struct runq_stats runq_stats;
1c79356b
A
196};
197
fe8ab488 198#endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) || defined(CONFIG_SCHED_PROTO) */
1c79356b 199
6d2010ae
A
200struct rt_queue {
201 int count; /* # of threads total */
202 queue_head_t queue; /* all runnable RT threads */
55e303ae 203
6d2010ae
A
204 struct runq_stats runq_stats;
205};
206
fe8ab488 207#if defined(CONFIG_SCHED_FAIRSHARE_CORE)
6d2010ae
A
208struct fairshare_queue {
209 int count; /* # of threads total */
210 queue_head_t queue; /* all runnable threads demoted to fairshare scheduling */
211
212 struct runq_stats runq_stats;
213};
fe8ab488 214#endif /* CONFIG_SCHED_FAIRSHARE_CORE */
0b4e3aa0 215
6d2010ae 216#if defined(CONFIG_SCHED_GRRR_CORE)
1c79356b
A
217
218/*
6d2010ae
A
219 * We map standard Mach priorities to an abstract scale that more properly
220 * indicates how we want processor time allocated under contention.
1c79356b 221 */
6d2010ae
A
222typedef uint8_t grrr_proportional_priority_t;
223typedef uint8_t grrr_group_index_t;
224
225#define NUM_GRRR_PROPORTIONAL_PRIORITIES 256
226#define MAX_GRRR_PROPORTIONAL_PRIORITY ((grrr_proportional_priority_t)255)
227
228#if 0
229#define NUM_GRRR_GROUPS 8 /* log(256) */
230#endif
231
232#define NUM_GRRR_GROUPS 64 /* 256/4 */
233
234struct grrr_group {
235 queue_chain_t priority_order; /* next greatest weight group */
236 grrr_proportional_priority_t minpriority;
237 grrr_group_index_t index;
238
239 queue_head_t clients;
240 int count;
241 uint32_t weight;
242#if 0
243 uint32_t deferred_removal_weight;
244#endif
245 uint32_t work;
246 thread_t current_client;
247};
248
249struct grrr_run_queue {
250 int count;
251 uint32_t last_rescale_tick;
252 struct grrr_group groups[NUM_GRRR_GROUPS];
253 queue_head_t sorted_group_list;
254 uint32_t weight;
255 grrr_group_t current_group;
256
257 struct runq_stats runq_stats;
258};
259
260#endif /* defined(CONFIG_SCHED_GRRR_CORE) */
261
262#define first_timeslice(processor) ((processor)->timeslice > 0)
1c79356b 263
6d2010ae
A
264extern struct rt_queue rt_runq;
265
fe8ab488
A
266#if defined(CONFIG_SCHED_MULTIQ)
267sched_group_t sched_group_create(void);
268void sched_group_destroy(sched_group_t sched_group);
269
270extern boolean_t sched_groups_enabled;
271
272#endif /* defined(CONFIG_SCHED_MULTIQ) */
273
6d2010ae
A
274/*
275 * Scheduler routines.
276 */
1c79356b 277
0b4e3aa0
A
278/* Handle quantum expiration for an executing thread */
279extern void thread_quantum_expire(
280 timer_call_param_t processor,
281 timer_call_param_t thread);
282
c910b4d9 283/* Context switch check for current processor */
fe8ab488
A
284extern ast_t csw_check(processor_t processor,
285 ast_t check_reason);
9bccf70c 286
fe8ab488 287#if defined(CONFIG_SCHED_TIMESHARE_CORE)
0b4e3aa0
A
288extern uint32_t std_quantum, min_std_quantum;
289extern uint32_t std_quantum_us;
fe8ab488 290#endif /* CONFIG_SCHED_TIMESHARE_CORE */
6d2010ae
A
291
292extern uint32_t thread_depress_time;
293extern uint32_t default_timeshare_computation;
294extern uint32_t default_timeshare_constraint;
1c79356b 295
0b4e3aa0 296extern uint32_t max_rt_quantum, min_rt_quantum;
1c79356b 297
316670eb
A
298extern int default_preemption_rate;
299extern int default_bg_preemption_rate;
91447636 300
fe8ab488 301#if defined(CONFIG_SCHED_TIMESHARE_CORE)
6d2010ae 302
1c79356b 303/*
39236c6e
A
304 * Age usage at approximately (1 << SCHED_TICK_SHIFT) times per second
305 * Aging may be deferred during periods where all processors are idle
306 * and cumulatively applied during periods of activity.
1c79356b 307 */
91447636 308#define SCHED_TICK_SHIFT 3
39236c6e 309#define SCHED_TICK_MAX_DELTA (8)
91447636
A
310
311extern unsigned sched_tick;
312extern uint32_t sched_tick_interval;
313
fe8ab488 314#endif /* CONFIG_SCHED_TIMESHARE_CORE */
6d2010ae
A
315
316extern uint64_t sched_one_second_interval;
317
91447636 318/* Periodic computation of various averages */
39236c6e 319extern void compute_averages(uint64_t);
1c79356b 320
91447636
A
321extern void compute_averunnable(
322 void *nrun);
323
324extern void compute_stack_target(
325 void *arg);
1c79356b 326
b0d623f7
A
327extern void compute_memory_pressure(
328 void *arg);
329
6d2010ae
A
330extern void compute_zone_gc_throttle(
331 void *arg);
332
316670eb
A
333extern void compute_pageout_gc_throttle(
334 void *arg);
335
6d2010ae
A
336extern void compute_pmap_gc_throttle(
337 void *arg);
338
1c79356b 339/*
91447636
A
340 * Conversion factor from usage
341 * to priority.
342 */
fe8ab488 343#if defined(CONFIG_SCHED_TIMESHARE_CORE)
91447636 344extern uint32_t sched_pri_shift;
39236c6e
A
345extern uint32_t sched_background_pri_shift;
346extern uint32_t sched_combined_fgbg_pri_shift;
2d21ac55 347extern uint32_t sched_fixed_shift;
91447636 348extern int8_t sched_load_shifts[NRQS];
39236c6e
A
349extern uint32_t sched_decay_usage_age_factor;
350extern uint32_t sched_use_combined_fgbg_decay;
351void sched_traditional_consider_maintenance(uint64_t);
fe8ab488 352#endif /* CONFIG_SCHED_TIMESHARE_CORE */
1c79356b 353
91447636 354extern int32_t sched_poll_yield_shift;
6d2010ae 355extern uint64_t sched_safe_duration;
1c79356b 356
39236c6e 357extern uint32_t sched_run_count, sched_share_count, sched_background_count;
2d21ac55
A
358extern uint32_t sched_load_average, sched_mach_factor;
359
360extern uint32_t avenrun[3], mach_factor[3];
361
91447636
A
362extern uint64_t max_unsafe_computation;
363extern uint64_t max_poll_computation;
1c79356b 364
fe8ab488
A
365/* TH_RUN & !TH_IDLE controls whether a thread has a run count */
366#define sched_run_incr(th) \
367 hw_atomic_add(&sched_run_count, 1) \
368
369#define sched_run_decr(th) \
370 hw_atomic_sub(&sched_run_count, 1) \
371
372#if MACH_ASSERT
373extern void sched_share_incr(thread_t thread);
374extern void sched_share_decr(thread_t thread);
375extern void sched_background_incr(thread_t thread);
376extern void sched_background_decr(thread_t thread);
2d21ac55 377
fe8ab488
A
378extern void assert_thread_sched_count(thread_t thread);
379
380#else /* MACH_ASSERT */
381/* sched_mode == TH_MODE_TIMESHARE controls whether a thread has a timeshare count when it has a run count */
382#define sched_share_incr(th) \
383MACRO_BEGIN \
384 (void)hw_atomic_add(&sched_share_count, 1); \
c910b4d9 385MACRO_END
2d21ac55 386
fe8ab488
A
387#define sched_share_decr(th) \
388MACRO_BEGIN \
389 (void)hw_atomic_sub(&sched_share_count, 1); \
c910b4d9 390MACRO_END
2d21ac55 391
fe8ab488
A
392/* TH_SFLAG_THROTTLED controls whether a thread has a background count when it has a run count and a share count */
393#define sched_background_incr(th) \
394MACRO_BEGIN \
395 hw_atomic_add(&sched_background_count, 1); \
c910b4d9 396MACRO_END
1c79356b 397
fe8ab488
A
398#define sched_background_decr(th) \
399MACRO_BEGIN \
400 hw_atomic_sub(&sched_background_count, 1); \
39236c6e
A
401MACRO_END
402
fe8ab488
A
403#define assert_thread_sched_count(th) \
404MACRO_BEGIN \
39236c6e
A
405MACRO_END
406
fe8ab488
A
407#endif /* !MACH_ASSERT */
408
1c79356b
A
409/*
410 * thread_timer_delta macro takes care of both thread timers.
411 */
91447636 412#define thread_timer_delta(thread, delta) \
1c79356b 413MACRO_BEGIN \
b0d623f7 414 (delta) = (typeof(delta))timer_delta(&(thread)->system_timer, \
91447636 415 &(thread)->system_timer_save); \
b0d623f7 416 (delta) += (typeof(delta))timer_delta(&(thread)->user_timer, \
91447636 417 &(thread)->user_timer_save); \
1c79356b
A
418MACRO_END
419
1c79356b 420#endif /* _KERN_SCHED_H_ */