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