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