]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_prim.h
xnu-3248.30.4.tar.gz
[apple/xnu.git] / osfmk / kern / sched_prim.h
1 /*
2 * Copyright (c) 2000-2012 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_prim.h
60 * Author: David Golub
61 *
62 * Scheduling primitive definitions file
63 *
64 */
65
66 #ifndef _KERN_SCHED_PRIM_H_
67 #define _KERN_SCHED_PRIM_H_
68
69 #include <mach/boolean.h>
70 #include <mach/machine/vm_types.h>
71 #include <mach/kern_return.h>
72 #include <kern/clock.h>
73 #include <kern/kern_types.h>
74 #include <kern/thread.h>
75 #include <sys/cdefs.h>
76
77 #ifdef MACH_KERNEL_PRIVATE
78
79 #include <mach/branch_predicates.h>
80
81 /* Initialization */
82 extern void sched_init(void);
83
84 extern void sched_startup(void);
85
86 extern void sched_timebase_init(void);
87
88 /* Force a preemption point for a thread and wait for it to stop running */
89 extern boolean_t thread_stop(
90 thread_t thread,
91 boolean_t until_not_runnable);
92
93 /* Release a previous stop request */
94 extern void thread_unstop(
95 thread_t thread);
96
97 /* Wait for a thread to stop running */
98 extern void thread_wait(
99 thread_t thread,
100 boolean_t until_not_runnable);
101
102 /* Unblock thread on wake up */
103 extern boolean_t thread_unblock(
104 thread_t thread,
105 wait_result_t wresult);
106
107 /* Unblock and dispatch thread */
108 extern kern_return_t thread_go(
109 thread_t thread,
110 wait_result_t wresult);
111
112 /* Handle threads at context switch */
113 extern void thread_dispatch(
114 thread_t old_thread,
115 thread_t new_thread);
116
117 /* Switch directly to a particular thread */
118 extern int thread_run(
119 thread_t self,
120 thread_continue_t continuation,
121 void *parameter,
122 thread_t new_thread);
123
124 /* Resume thread with new stack */
125 extern void thread_continue(
126 thread_t old_thread);
127
128 /* Invoke continuation */
129 extern void call_continuation(
130 thread_continue_t continuation,
131 void *parameter,
132 wait_result_t wresult);
133
134 /* Set the current scheduled priority */
135 extern void set_sched_pri(
136 thread_t thread,
137 int priority);
138
139 /* Set base priority of the specified thread */
140 extern void sched_set_thread_base_priority(
141 thread_t thread,
142 int priority);
143
144 /* Set the thread to be categorized as 'background' */
145 extern void sched_set_thread_throttled(thread_t thread,
146 boolean_t wants_throttle);
147
148 /* Set the thread's true scheduling mode */
149 extern void sched_set_thread_mode(thread_t thread,
150 sched_mode_t mode);
151 /* Demote the true scheduler mode */
152 extern void sched_thread_mode_demote(thread_t thread,
153 uint32_t reason);
154 /* Un-demote the true scheduler mode */
155 extern void sched_thread_mode_undemote(thread_t thread,
156 uint32_t reason);
157
158 /* Re-evaluate base priority of thread (thread locked) */
159 void thread_recompute_priority(thread_t thread);
160
161 /* Re-evaluate base priority of thread (thread unlocked) */
162 void thread_recompute_qos(thread_t thread);
163
164 /* Reset scheduled priority of thread */
165 extern void thread_recompute_sched_pri(
166 thread_t thread,
167 boolean_t override_depress);
168
169 /* Periodic scheduler activity */
170 extern void sched_init_thread(void (*)(void));
171
172 /* Perform sched_tick housekeeping activities */
173 extern boolean_t can_update_priority(
174 thread_t thread);
175
176 extern void update_priority(
177 thread_t thread);
178
179 extern void lightweight_update_priority(
180 thread_t thread);
181
182 extern void sched_default_quantum_expire(thread_t thread);
183
184 /* Idle processor thread */
185 extern void idle_thread(void);
186
187 extern kern_return_t idle_thread_create(
188 processor_t processor);
189
190 /* Continuation return from syscall */
191 extern void thread_syscall_return(
192 kern_return_t ret);
193
194 /* Context switch */
195 extern wait_result_t thread_block_reason(
196 thread_continue_t continuation,
197 void *parameter,
198 ast_t reason);
199
200 /* Reschedule thread for execution */
201 extern void thread_setrun(
202 thread_t thread,
203 integer_t options);
204
205 #define SCHED_TAILQ 1
206 #define SCHED_HEADQ 2
207 #define SCHED_PREEMPT 4
208
209 extern uintptr_t sched_thread_on_rt_queue;
210 #define THREAD_ON_RT_RUNQ ((processor_t)(uintptr_t)&sched_thread_on_rt_queue)
211
212 extern processor_set_t task_choose_pset(
213 task_t task);
214
215 /* Bind the current thread to a particular processor */
216 extern processor_t thread_bind(
217 processor_t processor);
218
219 /* Choose the best processor to run a thread */
220 extern processor_t choose_processor(
221 processor_set_t pset,
222 processor_t processor,
223 thread_t thread);
224
225
226 extern void thread_quantum_init(
227 thread_t thread);
228
229 extern void run_queue_init(
230 run_queue_t runq);
231
232 extern thread_t run_queue_dequeue(
233 run_queue_t runq,
234 integer_t options);
235
236 extern boolean_t run_queue_enqueue(
237 run_queue_t runq,
238 thread_t thread,
239 integer_t options);
240
241 extern void run_queue_remove(
242 run_queue_t runq,
243 thread_t thread);
244
245 struct sched_update_scan_context
246 {
247 uint64_t earliest_bg_make_runnable_time;
248 uint64_t earliest_normal_make_runnable_time;
249 uint64_t earliest_rt_make_runnable_time;
250 };
251 typedef struct sched_update_scan_context *sched_update_scan_context_t;
252
253 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
254
255 extern boolean_t thread_update_add_thread(thread_t thread);
256 extern void thread_update_process_threads(void);
257 extern boolean_t runq_scan(run_queue_t runq, sched_update_scan_context_t scan_context);
258
259 extern void sched_timeshare_init(void);
260 extern void sched_timeshare_timebase_init(void);
261 extern void sched_timeshare_maintenance_continue(void);
262
263 extern boolean_t priority_is_urgent(int priority);
264 extern uint32_t sched_timeshare_initial_quantum_size(thread_t thread);
265
266 extern int sched_compute_timeshare_priority(thread_t thread);
267
268 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
269
270 extern void rt_runq_scan(sched_update_scan_context_t scan_context);
271
272 /* Remove thread from its run queue */
273 extern boolean_t thread_run_queue_remove(thread_t thread);
274 thread_t thread_run_queue_remove_for_handoff(thread_t thread);
275
276 /* Put a thread back in the run queue after being yanked */
277 extern void thread_run_queue_reinsert(thread_t thread, integer_t options);
278
279 extern void thread_timer_expire(
280 void *thread,
281 void *p1);
282
283 extern boolean_t thread_eager_preemption(
284 thread_t thread);
285
286 extern boolean_t sched_generic_direct_dispatch_to_idle_processors;
287
288 /* Set the maximum interrupt level for the thread */
289 __private_extern__ wait_interrupt_t thread_interrupt_level(
290 wait_interrupt_t interruptible);
291
292 __private_extern__ wait_result_t thread_mark_wait_locked(
293 thread_t thread,
294 wait_interrupt_t interruptible);
295
296 /* Wake up locked thread directly, passing result */
297 __private_extern__ kern_return_t clear_wait_internal(
298 thread_t thread,
299 wait_result_t result);
300
301 extern void sched_stats_handle_csw(
302 processor_t processor,
303 int reasons,
304 int selfpri,
305 int otherpri);
306
307 extern void sched_stats_handle_runq_change(
308 struct runq_stats *stats,
309 int old_count);
310
311
312
313 #define SCHED_STATS_CSW(processor, reasons, selfpri, otherpri) \
314 do { \
315 if (__builtin_expect(sched_stats_active, 0)) { \
316 sched_stats_handle_csw((processor), \
317 (reasons), (selfpri), (otherpri)); \
318 } \
319 } while (0)
320
321
322 #define SCHED_STATS_RUNQ_CHANGE(stats, old_count) \
323 do { \
324 if (__builtin_expect(sched_stats_active, 0)) { \
325 sched_stats_handle_runq_change((stats), \
326 (old_count)); \
327 } \
328 } while (0)
329
330 extern uint32_t sched_debug_flags;
331 #define SCHED_DEBUG_FLAG_PLATFORM_TRACEPOINTS 0x00000001
332 #define SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS 0x00000002
333
334 #define SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(...) do { \
335 if (__improbable(sched_debug_flags & SCHED_DEBUG_FLAG_PLATFORM_TRACEPOINTS)) { \
336 KERNEL_DEBUG_CONSTANT(__VA_ARGS__); \
337 } \
338 } while(0)
339
340 #define SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(...) do { \
341 if (__improbable(sched_debug_flags & SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS)) { \
342 KERNEL_DEBUG_CONSTANT(__VA_ARGS__); \
343 } \
344 } while(0)
345
346 #define THREAD_URGENCY_NONE 0 /* indicates that there is no currently runnable */
347 #define THREAD_URGENCY_BACKGROUND 1 /* indicates that the thread is marked as a "background" thread */
348 #define THREAD_URGENCY_NORMAL 2 /* indicates that the thread is marked as a "normal" thread */
349 #define THREAD_URGENCY_REAL_TIME 3 /* indicates that the thread is marked as a "real-time" or urgent thread */
350 #define THREAD_URGENCY_MAX 4 /* Marker */
351 /* Returns the "urgency" of a thread (provided by scheduler) */
352 extern int thread_get_urgency(
353 thread_t thread,
354 uint64_t *rt_period,
355 uint64_t *rt_deadline);
356
357 /* Tells the "urgency" of the just scheduled thread (provided by CPU PM) */
358 extern void thread_tell_urgency(
359 int urgency,
360 uint64_t rt_period,
361 uint64_t rt_deadline,
362 uint64_t sched_latency,
363 thread_t nthread);
364
365 /* Tells if there are "active" RT threads in the system (provided by CPU PM) */
366 extern void active_rt_threads(
367 boolean_t active);
368
369 #endif /* MACH_KERNEL_PRIVATE */
370
371 __BEGIN_DECLS
372
373 #ifdef XNU_KERNEL_PRIVATE
374
375 extern boolean_t assert_wait_possible(void);
376
377 /* Toggles a global override to turn off CPU Throttling */
378 #define CPU_THROTTLE_DISABLE 0
379 #define CPU_THROTTLE_ENABLE 1
380 extern void sys_override_cpu_throttle(int flag);
381
382 /*
383 ****************** Only exported until BSD stops using ********************
384 */
385
386 extern void thread_vm_bind_group_add(void);
387
388 /* Wake up thread directly, passing result */
389 extern kern_return_t clear_wait(
390 thread_t thread,
391 wait_result_t result);
392
393 /* Start thread running */
394 extern void thread_bootstrap_return(void);
395
396 /* Return from exception (BSD-visible interface) */
397 extern void thread_exception_return(void) __dead2;
398
399 #define SCHED_STRING_MAX_LENGTH (48)
400 /* String declaring the name of the current scheduler */
401 extern char sched_string[SCHED_STRING_MAX_LENGTH];
402
403 extern kern_return_t sched_work_interval_notify(thread_t thread, uint64_t work_interval_id, uint64_t start, uint64_t finish, uint64_t deadline, uint64_t next_start, uint32_t flags);
404
405 #endif /* XNU_KERNEL_PRIVATE */
406
407 /* Context switch */
408 extern wait_result_t thread_block(
409 thread_continue_t continuation);
410
411 extern wait_result_t thread_block_parameter(
412 thread_continue_t continuation,
413 void *parameter);
414
415 /* Declare thread will wait on a particular event */
416 extern wait_result_t assert_wait(
417 event_t event,
418 wait_interrupt_t interruptible);
419
420 /* Assert that the thread intends to wait with a timeout */
421 extern wait_result_t assert_wait_timeout(
422 event_t event,
423 wait_interrupt_t interruptible,
424 uint32_t interval,
425 uint32_t scale_factor);
426
427 /* Assert that the thread intends to wait with an urgency, timeout and leeway */
428 extern wait_result_t assert_wait_timeout_with_leeway(
429 event_t event,
430 wait_interrupt_t interruptible,
431 wait_timeout_urgency_t urgency,
432 uint32_t interval,
433 uint32_t leeway,
434 uint32_t scale_factor);
435
436 extern wait_result_t assert_wait_deadline(
437 event_t event,
438 wait_interrupt_t interruptible,
439 uint64_t deadline);
440
441 /* Assert that the thread intends to wait with an urgency, deadline, and leeway */
442 extern wait_result_t assert_wait_deadline_with_leeway(
443 event_t event,
444 wait_interrupt_t interruptible,
445 wait_timeout_urgency_t urgency,
446 uint64_t deadline,
447 uint64_t leeway);
448
449 /* Wake up thread (or threads) waiting on a particular event */
450 extern kern_return_t thread_wakeup_prim(
451 event_t event,
452 boolean_t one_thread,
453 wait_result_t result);
454
455 extern kern_return_t thread_wakeup_prim_internal(
456 event_t event,
457 boolean_t one_thread,
458 wait_result_t result,
459 int priority);
460
461
462 #define thread_wakeup(x) \
463 thread_wakeup_prim((x), FALSE, THREAD_AWAKENED)
464 #define thread_wakeup_with_result(x, z) \
465 thread_wakeup_prim((x), FALSE, (z))
466 #define thread_wakeup_one(x) \
467 thread_wakeup_prim((x), TRUE, THREAD_AWAKENED)
468
469 #ifdef MACH_KERNEL_PRIVATE
470 #define thread_wakeup_one_with_pri(x, pri) \
471 thread_wakeup_prim_internal((x), TRUE, THREAD_AWAKENED, pri)
472 #endif
473
474 extern boolean_t preemption_enabled(void);
475
476 #ifdef MACH_KERNEL_PRIVATE
477
478 /*
479 * Scheduler algorithm indirection. If only one algorithm is
480 * enabled at compile-time, a direction function call is used.
481 * If more than one is enabled, calls are dispatched through
482 * a function pointer table.
483 */
484
485 #if !defined(CONFIG_SCHED_TRADITIONAL) && !defined(CONFIG_SCHED_PROTO) && !defined(CONFIG_SCHED_GRRR) && !defined(CONFIG_SCHED_MULTIQ)
486 #error Enable at least one scheduler algorithm in osfmk/conf/MASTER.XXX
487 #endif
488
489 #define SCHED(f) (sched_current_dispatch->f)
490
491 struct sched_dispatch_table {
492 const char *sched_name;
493 void (*init)(void); /* Init global state */
494 void (*timebase_init)(void); /* Timebase-dependent initialization */
495 void (*processor_init)(processor_t processor); /* Per-processor scheduler init */
496 void (*pset_init)(processor_set_t pset); /* Per-processor set scheduler init */
497
498 void (*maintenance_continuation)(void); /* Function called regularly */
499
500 /*
501 * Choose a thread of greater or equal priority from the per-processor
502 * runqueue for timeshare/fixed threads
503 */
504 thread_t (*choose_thread)(
505 processor_t processor,
506 int priority,
507 ast_t reason);
508
509 /* True if scheduler supports stealing threads */
510 boolean_t steal_thread_enabled;
511
512 /*
513 * Steal a thread from another processor in the pset so that it can run
514 * immediately
515 */
516 thread_t (*steal_thread)(
517 processor_set_t pset);
518
519 /*
520 * Compute priority for a timeshare thread based on base priority.
521 */
522 int (*compute_timeshare_priority)(thread_t thread);
523
524 /*
525 * Pick the best processor for a thread (any kind of thread) to run on.
526 */
527 processor_t (*choose_processor)(
528 processor_set_t pset,
529 processor_t processor,
530 thread_t thread);
531 /*
532 * Enqueue a timeshare or fixed priority thread onto the per-processor
533 * runqueue
534 */
535 boolean_t (*processor_enqueue)(
536 processor_t processor,
537 thread_t thread,
538 integer_t options);
539
540 /* Migrate threads away in preparation for processor shutdown */
541 void (*processor_queue_shutdown)(
542 processor_t processor);
543
544 /* Remove the specific thread from the per-processor runqueue */
545 boolean_t (*processor_queue_remove)(
546 processor_t processor,
547 thread_t thread);
548
549 /*
550 * Does the per-processor runqueue have any timeshare or fixed priority
551 * threads on it? Called without pset lock held, so should
552 * not assume immutability while executing.
553 */
554 boolean_t (*processor_queue_empty)(processor_t processor);
555
556 /*
557 * Would this priority trigger an urgent preemption if it's sitting
558 * on the per-processor runqueue?
559 */
560 boolean_t (*priority_is_urgent)(int priority);
561
562 /*
563 * Does the per-processor runqueue contain runnable threads that
564 * should cause the currently-running thread to be preempted?
565 */
566 ast_t (*processor_csw_check)(processor_t processor);
567
568 /*
569 * Does the per-processor runqueue contain a runnable thread
570 * of > or >= priority, as a preflight for choose_thread() or other
571 * thread selection
572 */
573 boolean_t (*processor_queue_has_priority)(processor_t processor,
574 int priority,
575 boolean_t gte);
576
577 /* Quantum size for the specified non-realtime thread. */
578 uint32_t (*initial_quantum_size)(thread_t thread);
579
580 /* Scheduler mode for a new thread */
581 sched_mode_t (*initial_thread_sched_mode)(task_t parent_task);
582
583 /*
584 * Is it safe to call update_priority, which may change a thread's
585 * runqueue or other state. This can be used to throttle changes
586 * to dynamic priority.
587 */
588 boolean_t (*can_update_priority)(thread_t thread);
589
590 /*
591 * Update both scheduled priority and other persistent state.
592 * Side effects may including migration to another processor's runqueue.
593 */
594 void (*update_priority)(thread_t thread);
595
596 /* Lower overhead update to scheduled priority and state. */
597 void (*lightweight_update_priority)(thread_t thread);
598
599 /* Callback for non-realtime threads when the quantum timer fires */
600 void (*quantum_expire)(thread_t thread);
601
602 /*
603 * Runnable threads on per-processor runqueue. Should only
604 * be used for relative comparisons of load between processors.
605 */
606 int (*processor_runq_count)(processor_t processor);
607
608 /* Aggregate runcount statistics for per-processor runqueue */
609 uint64_t (*processor_runq_stats_count_sum)(processor_t processor);
610
611 boolean_t (*processor_bound_count)(processor_t processor);
612
613 void (*thread_update_scan)(sched_update_scan_context_t scan_context);
614
615 /*
616 * Use processor->next_thread to pin a thread to an idle
617 * processor. If FALSE, threads are enqueued and can
618 * be stolen by other processors.
619 */
620 boolean_t direct_dispatch_to_idle_processors;
621
622 /* Supports more than one pset */
623 boolean_t multiple_psets_enabled;
624 /* Supports scheduler groups */
625 boolean_t sched_groups_enabled;
626 };
627
628 #if defined(CONFIG_SCHED_TRADITIONAL)
629 extern const struct sched_dispatch_table sched_traditional_dispatch;
630 extern const struct sched_dispatch_table sched_traditional_with_pset_runqueue_dispatch;
631 #endif
632
633 #if defined(CONFIG_SCHED_MULTIQ)
634 extern const struct sched_dispatch_table sched_multiq_dispatch;
635 extern const struct sched_dispatch_table sched_dualq_dispatch;
636 #endif
637
638 #if defined(CONFIG_SCHED_PROTO)
639 extern const struct sched_dispatch_table sched_proto_dispatch;
640 #endif
641
642 #if defined(CONFIG_SCHED_GRRR)
643 extern const struct sched_dispatch_table sched_grrr_dispatch;
644 #endif
645
646 /*
647 * It is an error to invoke any scheduler-related code
648 * before this is set up
649 */
650 extern const struct sched_dispatch_table *sched_current_dispatch;
651
652 #endif /* MACH_KERNEL_PRIVATE */
653
654 __END_DECLS
655
656 #endif /* _KERN_SCHED_PRIM_H_ */