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 [email protected] 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 <sys/cdefs.h> 70 #include <mach/boolean.h> 71 #include <mach/machine/vm_types.h> 72 #include <mach/kern_return.h> 73 #include <kern/clock.h> 74 #include <kern/kern_types.h> 75 #include <kern/percpu.h> 76 #include <kern/thread.h> 77 #include <kern/block_hint.h> 78 79 extern int thread_get_current_cpuid(void); 80 81 #ifdef MACH_KERNEL_PRIVATE 82 83 #include <kern/sched_urgency.h> 84 #include <kern/thread_group.h> 85 #include <kern/waitq.h> 86 87 /* Initialization */ 88 extern void sched_init(void); 89 90 extern void sched_startup(void); 91 92 extern void sched_timebase_init(void); 93 94 extern void pset_rt_init(processor_set_t pset); 95 96 extern void sched_rtlocal_init(processor_set_t pset); 97 98 extern rt_queue_t sched_rtlocal_runq(processor_set_t pset); 99 100 extern void sched_rtlocal_queue_shutdown(processor_t processor); 101 102 extern int64_t sched_rtlocal_runq_count_sum(void); 103 104 extern thread_t sched_rtlocal_steal_thread(processor_set_t stealing_pset, uint64_t earliest_deadline); 105 106 extern thread_t sched_rt_choose_thread(processor_set_t pset); 107 108 extern void sched_check_spill(processor_set_t pset, thread_t thread); 109 110 extern bool sched_thread_should_yield(processor_t processor, thread_t thread); 111 112 extern bool sched_steal_thread_DISABLED(processor_set_t pset); 113 extern bool sched_steal_thread_enabled(processor_set_t pset); 114 115 /* Force a preemption point for a thread and wait for it to stop running */ 116 extern boolean_t thread_stop( 117 thread_t thread, 118 boolean_t until_not_runnable); 119 120 /* Release a previous stop request */ 121 extern void thread_unstop( 122 thread_t thread); 123 124 /* Wait for a thread to stop running */ 125 extern void thread_wait( 126 thread_t thread, 127 boolean_t until_not_runnable); 128 129 /* Unblock thread on wake up */ 130 extern boolean_t thread_unblock( 131 thread_t thread, 132 wait_result_t wresult); 133 134 /* Unblock and dispatch thread */ 135 extern void thread_go( 136 thread_t thread, 137 wait_result_t wresult, 138 bool try_handoff); 139 140 /* Check if direct handoff is allowed */ 141 extern boolean_t 142 thread_allowed_for_handoff( 143 thread_t thread); 144 145 /* Handle threads at context switch */ 146 extern void thread_dispatch( 147 thread_t old_thread, 148 thread_t new_thread); 149 150 /* Switch directly to a particular thread */ 151 extern int thread_run( 152 thread_t self, 153 thread_continue_t continuation, 154 void *parameter, 155 thread_t new_thread); 156 157 /* Resume thread with new stack */ 158 extern __dead2 void thread_continue(thread_t old_thread); 159 160 /* Invoke continuation */ 161 extern __dead2 void call_continuation( 162 thread_continue_t continuation, 163 void *parameter, 164 wait_result_t wresult, 165 boolean_t enable_interrupts); 166 167 /* 168 * Flags that can be passed to set_sched_pri 169 * to skip side effects 170 */ 171 __options_decl(set_sched_pri_options_t, uint32_t, { 172 SETPRI_DEFAULT = 0x0, 173 SETPRI_LAZY = 0x1, /* Avoid setting AST flags or sending IPIs */ 174 }); 175 176 /* Set the current scheduled priority */ 177 extern void set_sched_pri( 178 thread_t thread, 179 int16_t priority, 180 set_sched_pri_options_t options); 181 182 /* Set base priority of the specified thread */ 183 extern void sched_set_thread_base_priority( 184 thread_t thread, 185 int priority); 186 187 /* Set absolute base priority of the specified thread */ 188 extern void sched_set_kernel_thread_priority( 189 thread_t thread, 190 int priority); 191 192 193 /* Set the thread's true scheduling mode */ 194 extern void sched_set_thread_mode(thread_t thread, 195 sched_mode_t mode); 196 197 /* 198 * Set the thread's scheduling mode taking into account that the thread may have 199 * been demoted. 200 * */ 201 extern void sched_set_thread_mode_user(thread_t thread, 202 sched_mode_t mode); 203 204 /* 205 * Get the thread's scheduling mode taking into account that the thread may have 206 * been demoted. 207 * */ 208 extern sched_mode_t sched_get_thread_mode_user(thread_t thread); 209 210 211 /* Demote the true scheduler mode */ 212 extern void sched_thread_mode_demote(thread_t thread, 213 uint32_t reason); 214 /* Un-demote the true scheduler mode */ 215 extern void sched_thread_mode_undemote(thread_t thread, 216 uint32_t reason); 217 /* Check for a specific demotion */ 218 extern bool sched_thread_mode_has_demotion(thread_t thread, 219 uint32_t reason); 220 221 extern void sched_thread_promote_reason(thread_t thread, uint32_t reason, uintptr_t trace_obj); 222 extern void sched_thread_unpromote_reason(thread_t thread, uint32_t reason, uintptr_t trace_obj); 223 224 /* Re-evaluate base priority of thread (thread locked) */ 225 void thread_recompute_priority(thread_t thread); 226 227 /* Re-evaluate scheduled priority of thread (thread locked) */ 228 extern void thread_recompute_sched_pri( 229 thread_t thread, 230 set_sched_pri_options_t options); 231 232 /* Periodic scheduler activity */ 233 extern void sched_init_thread(void); 234 235 /* Perform sched_tick housekeeping activities */ 236 extern boolean_t can_update_priority( 237 thread_t thread); 238 239 extern void update_priority( 240 thread_t thread); 241 242 extern void lightweight_update_priority( 243 thread_t thread); 244 245 extern void sched_default_quantum_expire(thread_t thread); 246 247 /* Idle processor thread continuation */ 248 extern void idle_thread( 249 void* parameter, 250 wait_result_t result); 251 252 extern void idle_thread_create( 253 processor_t processor, 254 thread_continue_t continuation); 255 256 /* Continuation return from syscall */ 257 extern void thread_syscall_return( 258 kern_return_t ret); 259 260 /* Context switch */ 261 extern wait_result_t thread_block_reason( 262 thread_continue_t continuation, 263 void *parameter, 264 ast_t reason); 265 266 __options_decl(sched_options_t, uint32_t, { 267 SCHED_NONE = 0x0, 268 SCHED_TAILQ = 0x1, 269 SCHED_HEADQ = 0x2, 270 SCHED_PREEMPT = 0x4, 271 SCHED_REBALANCE = 0x8, 272 }); 273 274 /* Reschedule thread for execution */ 275 extern void thread_setrun( 276 thread_t thread, 277 sched_options_t options); 278 279 extern processor_set_t task_choose_pset( 280 task_t task); 281 282 /* Bind the current thread to a particular processor */ 283 extern processor_t thread_bind( 284 processor_t processor); 285 286 extern void thread_bind_during_wakeup( 287 thread_t thread, 288 processor_t processor); 289 290 extern void thread_unbind_after_queue_shutdown( 291 thread_t thread, 292 processor_t processor); 293 294 extern bool pset_has_stealable_threads( 295 processor_set_t pset); 296 297 extern bool pset_has_stealable_rt_threads( 298 processor_set_t pset); 299 300 extern processor_set_t choose_starting_pset( 301 pset_node_t node, 302 thread_t thread, 303 processor_t *processor_hint); 304 305 extern int pset_available_cpu_count( 306 processor_set_t pset); 307 308 extern bool pset_is_recommended( 309 processor_set_t pset); 310 311 extern bool pset_type_is_recommended( 312 processor_set_t pset); 313 314 extern pset_node_t sched_choose_node( 315 thread_t thread); 316 317 /* Choose the best processor to run a thread */ 318 extern processor_t choose_processor( 319 processor_set_t pset, 320 processor_t processor, 321 thread_t thread); 322 323 extern bool sched_SMT_balance( 324 processor_t processor, 325 processor_set_t pset); 326 327 extern void thread_quantum_init( 328 thread_t thread, 329 uint64_t now); 330 331 332 extern void run_queue_init( 333 run_queue_t runq); 334 335 extern thread_t run_queue_dequeue( 336 run_queue_t runq, 337 sched_options_t options); 338 339 extern boolean_t run_queue_enqueue( 340 run_queue_t runq, 341 thread_t thread, 342 sched_options_t options); 343 344 extern void run_queue_remove( 345 run_queue_t runq, 346 thread_t thread); 347 348 extern thread_t run_queue_peek( 349 run_queue_t runq); 350 351 struct sched_update_scan_context { 352 uint64_t earliest_bg_make_runnable_time; 353 uint64_t earliest_normal_make_runnable_time; 354 uint64_t earliest_rt_make_runnable_time; 355 uint64_t sched_tick_last_abstime; 356 }; 357 typedef struct sched_update_scan_context *sched_update_scan_context_t; 358 359 extern void sched_rtlocal_runq_scan(sched_update_scan_context_t scan_context); 360 361 extern void sched_pset_made_schedulable( 362 processor_t processor, 363 processor_set_t pset, 364 boolean_t drop_lock); 365 366 extern void sched_cpu_init_completed(void); 367 368 /* 369 * Enum to define various events which need IPIs. The IPI policy 370 * engine decides what kind of IPI to use based on destination 371 * processor state, thread and one of the following scheduling events. 372 */ 373 typedef enum { 374 SCHED_IPI_EVENT_BOUND_THR = 0x1, 375 SCHED_IPI_EVENT_PREEMPT = 0x2, 376 SCHED_IPI_EVENT_SMT_REBAL = 0x3, 377 SCHED_IPI_EVENT_SPILL = 0x4, 378 SCHED_IPI_EVENT_REBALANCE = 0x5, 379 SCHED_IPI_EVENT_RT_PREEMPT = 0x6, 380 } sched_ipi_event_t; 381 382 383 /* Enum to define various IPI types used by the scheduler */ 384 typedef enum { 385 SCHED_IPI_NONE = 0x0, 386 SCHED_IPI_IMMEDIATE = 0x1, 387 SCHED_IPI_IDLE = 0x2, 388 SCHED_IPI_DEFERRED = 0x3, 389 } sched_ipi_type_t; 390 391 /* The IPI policy engine behaves in the following manner: 392 * - All scheduler events which need an IPI invoke sched_ipi_action() with 393 * the appropriate destination processor, thread and event. 394 * - sched_ipi_action() performs basic checks, invokes the scheduler specific 395 * ipi_policy routine and sets pending_AST bits based on the result. 396 * - Once the pset lock is dropped, the scheduler invokes sched_ipi_perform() 397 * routine which actually sends the appropriate IPI to the destination core. 398 */ 399 extern sched_ipi_type_t sched_ipi_action(processor_t dst, thread_t thread, sched_ipi_event_t event); 400 extern void sched_ipi_perform(processor_t dst, sched_ipi_type_t ipi); 401 402 /* sched_ipi_policy() is the global default IPI policy for all schedulers */ 403 extern sched_ipi_type_t sched_ipi_policy(processor_t dst, thread_t thread, 404 boolean_t dst_idle, sched_ipi_event_t event); 405 406 /* sched_ipi_deferred_policy() is the global default deferred IPI policy for all schedulers */ 407 extern sched_ipi_type_t sched_ipi_deferred_policy(processor_set_t pset, 408 processor_t dst, thread_t thread, sched_ipi_event_t event); 409 410 #if defined(CONFIG_SCHED_TIMESHARE_CORE) 411 412 extern boolean_t thread_update_add_thread(thread_t thread); 413 extern void thread_update_process_threads(void); 414 extern boolean_t runq_scan(run_queue_t runq, sched_update_scan_context_t scan_context); 415 416 #if CONFIG_SCHED_CLUTCH 417 extern boolean_t sched_clutch_timeshare_scan(queue_t thread_queue, uint16_t count, sched_update_scan_context_t scan_context); 418 #endif /* CONFIG_SCHED_CLUTCH */ 419 420 extern void sched_timeshare_init(void); 421 extern void sched_timeshare_timebase_init(void); 422 extern void sched_timeshare_maintenance_continue(void); 423 424 extern boolean_t priority_is_urgent(int priority); 425 extern uint32_t sched_timeshare_initial_quantum_size(thread_t thread); 426 427 extern int sched_compute_timeshare_priority(thread_t thread); 428 429 #endif /* CONFIG_SCHED_TIMESHARE_CORE */ 430 431 /* Remove thread from its run queue */ 432 extern boolean_t thread_run_queue_remove(thread_t thread); 433 thread_t thread_run_queue_remove_for_handoff(thread_t thread); 434 435 /* Put a thread back in the run queue after being yanked */ 436 extern void thread_run_queue_reinsert(thread_t thread, sched_options_t options); 437 438 extern void thread_timer_expire( 439 void *thread, 440 void *p1); 441 442 extern bool thread_is_eager_preempt(thread_t thread); 443 444 extern boolean_t sched_generic_direct_dispatch_to_idle_processors; 445 446 /* Set the maximum interrupt level for the thread */ 447 __private_extern__ wait_interrupt_t thread_interrupt_level( 448 wait_interrupt_t interruptible); 449 450 __private_extern__ wait_result_t thread_mark_wait_locked( 451 thread_t thread, 452 wait_interrupt_t interruptible); 453 454 /* Wake up locked thread directly, passing result */ 455 __private_extern__ kern_return_t clear_wait_internal( 456 thread_t thread, 457 wait_result_t result); 458 459 struct sched_statistics { 460 uint32_t csw_count; 461 uint32_t preempt_count; 462 uint32_t preempted_rt_count; 463 uint32_t preempted_by_rt_count; 464 uint32_t rt_sched_count; 465 uint32_t interrupt_count; 466 uint32_t ipi_count; 467 uint32_t timer_pop_count; 468 uint32_t idle_transitions; 469 uint32_t quantum_timer_expirations; 470 }; 471 PERCPU_DECL(struct sched_statistics, sched_stats); 472 extern bool sched_stats_active; 473 474 extern void sched_stats_handle_csw( 475 processor_t processor, 476 int reasons, 477 int selfpri, 478 int otherpri); 479 480 extern void sched_stats_handle_runq_change( 481 struct runq_stats *stats, 482 int old_count); 483 484 #define SCHED_STATS_INC(field) \ 485 MACRO_BEGIN \ 486 if (__improbable(sched_stats_active)) { \ 487 PERCPU_GET(sched_stats)->field++; \ 488 } \ 489 MACRO_END 490 491 #if DEBUG 492 493 #define SCHED_STATS_CSW(processor, reasons, selfpri, otherpri) \ 494 MACRO_BEGIN \ 495 if (__improbable(sched_stats_active)) { \ 496 sched_stats_handle_csw((processor), \ 497 (reasons), (selfpri), (otherpri)); \ 498 } \ 499 MACRO_END 500 501 502 #define SCHED_STATS_RUNQ_CHANGE(stats, old_count) \ 503 MACRO_BEGIN \ 504 if (__improbable(sched_stats_active)) { \ 505 sched_stats_handle_runq_change((stats), (old_count)); \ 506 } \ 507 MACRO_END 508 509 #else /* DEBUG */ 510 511 #define SCHED_STATS_CSW(processor, reasons, selfpri, otherpri) do { }while(0) 512 #define SCHED_STATS_RUNQ_CHANGE(stats, old_count) do { }while(0) 513 514 #endif /* DEBUG */ 515 516 extern uint32_t sched_debug_flags; 517 #define SCHED_DEBUG_FLAG_PLATFORM_TRACEPOINTS 0x00000001 518 #define SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS 0x00000002 519 #define SCHED_DEBUG_FLAG_AST_CHECK_TRACEPOINTS 0x00000004 520 521 #define SCHED_DEBUG_PLATFORM_KERNEL_DEBUG_CONSTANT(...) \ 522 MACRO_BEGIN \ 523 if (__improbable(sched_debug_flags & \ 524 SCHED_DEBUG_FLAG_PLATFORM_TRACEPOINTS)) { \ 525 KERNEL_DEBUG_CONSTANT(__VA_ARGS__); \ 526 } \ 527 MACRO_END 528 529 #define SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT_IST(...) \ 530 MACRO_BEGIN \ 531 if (__improbable(sched_debug_flags & \ 532 SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS)) { \ 533 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, __VA_ARGS__); \ 534 } \ 535 MACRO_END 536 537 #define SCHED_DEBUG_AST_CHECK_KDBG_RELEASE(...) \ 538 MACRO_BEGIN \ 539 if (__improbable(sched_debug_flags & \ 540 SCHED_DEBUG_FLAG_AST_CHECK_TRACEPOINTS)) { \ 541 KDBG_RELEASE(__VA_ARGS__); \ 542 } \ 543 MACRO_END 544 545 546 /* Tells if there are "active" RT threads in the system (provided by CPU PM) */ 547 extern void active_rt_threads( 548 boolean_t active); 549 550 /* Returns the perfcontrol attribute for the thread */ 551 extern perfcontrol_class_t thread_get_perfcontrol_class( 552 thread_t thread); 553 554 /* Generic routine for Non-AMP schedulers to calculate parallelism */ 555 extern uint32_t sched_qos_max_parallelism(int qos, uint64_t options); 556 557 extern void check_monotonic_time(uint64_t ctime); 558 559 #endif /* MACH_KERNEL_PRIVATE */ 560 561 __BEGIN_DECLS 562 563 #ifdef XNU_KERNEL_PRIVATE 564 565 extern void thread_bind_cluster_type(thread_t, char cluster_type, bool soft_bind); 566 567 __options_decl(thread_bind_option_t, uint64_t, { 568 /* Unbind a previously cluster bound thread */ 569 THREAD_UNBIND = 0x1, 570 /* 571 * Soft bind the thread to the cluster; soft binding means the thread will be 572 * moved to an available cluster if the bound cluster is de-recommended/offline. 573 */ 574 THREAD_BIND_SOFT = 0x2, 575 /* 576 * Bind thread to the cluster only if it is eligible to run on that cluster. If 577 * the thread is not eligible to run on the cluster, thread_bind_cluster_id() 578 * returns KERN_INVALID_POLICY. 579 */ 580 THREAD_BIND_ELIGIBLE_ONLY = 0x4, 581 }); 582 extern kern_return_t thread_bind_cluster_id(thread_t thread, uint32_t cluster_id, thread_bind_option_t options); 583 584 extern int sched_get_rt_n_backup_processors(void); 585 extern void sched_set_rt_n_backup_processors(int n); 586 587 extern int sched_get_rt_deadline_epsilon(void); 588 extern void sched_set_rt_deadline_epsilon(int new_epsilon_us); 589 590 /* Toggles a global override to turn off CPU Throttling */ 591 extern void sys_override_cpu_throttle(boolean_t enable_override); 592 593 extern int sched_get_powered_cores(void); 594 extern void sched_set_powered_cores(int n); 595 596 uint64_t sched_sysctl_get_recommended_cores(void); 597 598 /* 599 ****************** Only exported until BSD stops using ******************** 600 */ 601 602 extern void thread_vm_bind_group_add(void); 603 604 /* Wake up thread directly, passing result */ 605 extern kern_return_t clear_wait( 606 thread_t thread, 607 wait_result_t result); 608 609 /* Start thread running */ 610 extern void thread_bootstrap_return(void) __attribute__((noreturn)); 611 612 /* Return from exception (BSD-visible interface) */ 613 extern void thread_exception_return(void) __dead2; 614 615 #define SCHED_STRING_MAX_LENGTH (48) 616 /* String declaring the name of the current scheduler */ 617 extern char sched_string[SCHED_STRING_MAX_LENGTH]; 618 619 __options_decl(thread_handoff_option_t, uint32_t, { 620 THREAD_HANDOFF_NONE = 0, 621 THREAD_HANDOFF_SETRUN_NEEDED = 0x1, 622 }); 623 624 /* Remove thread from its run queue */ 625 thread_t thread_prepare_for_handoff(thread_t thread, thread_handoff_option_t option); 626 627 /* Attempt to context switch to a specific runnable thread */ 628 extern wait_result_t thread_handoff_deallocate(thread_t thread, thread_handoff_option_t option); 629 630 __attribute__((nonnull(2))) 631 extern void thread_handoff_parameter(thread_t thread, 632 thread_continue_t continuation, void *parameter, thread_handoff_option_t) __dead2; 633 634 extern struct waitq *assert_wait_queue(event_t event); 635 636 extern kern_return_t thread_wakeup_one_with_pri(event_t event, int priority); 637 638 extern thread_t thread_wakeup_identify(event_t event, int priority); 639 640 /* 641 * sched_cond_t: 642 * 643 * A atomic condition variable used to synchronize wake/block operations on threads. 644 * Bits defined below are reserved for use by sched_prim. Remaining 645 * bits may be used by caller for additional synchronization semantics. 646 */ 647 __options_decl(sched_cond_t, uint32_t, { 648 SCHED_COND_INIT = 0x0000, /* initialize all bits to zero (inactive and not awoken) */ 649 SCHED_COND_ACTIVE = 0x0001, /* target thread is active */ 650 SCHED_COND_WAKEUP = 0x0002 /* wakeup has been issued for target thread */ 651 }); 652 typedef _Atomic sched_cond_t sched_cond_atomic_t; 653 654 /* 655 * sched_cond_init: 656 * 657 * Initialize an atomic condition variable. Note that this does not occur atomically and should be 658 * performed during thread initialization, before the condition is observable by other threads. 659 */ 660 extern void sched_cond_init( 661 sched_cond_atomic_t *cond); 662 663 /* 664 * sched_cond_signal: 665 * 666 * Wakeup the specified thread if it is waiting on this event and it has not already been issued a wakeup. 667 * 668 * parameters: 669 * thread thread to awaken 670 * cond atomic condition variable 671 */ 672 extern kern_return_t sched_cond_signal( 673 sched_cond_atomic_t *cond, 674 thread_t thread); 675 676 /* 677 * sched_cond_wait_parameter: 678 * 679 * Assert wait and block on cond if no wakeup has been issued. 680 * If a wakeup has been issued on cond since the last `sched_cond_ack`, clear_wait and 681 * return `THREAD_AWAKENED`. 682 * 683 * `sched_cond_wait_parameter` must be paired with `sched_cond_ack`. 684 * 685 * NOTE: `continuation` will only be jumped to if a wakeup has not been issued 686 * 687 * parameters: 688 * cond atomic condition variable to synchronize on 689 * interruptible interruptible value to pass to assert_wait 690 * continuation continuation if block succeeds 691 * parameter 692 */ 693 extern wait_result_t sched_cond_wait_parameter( 694 sched_cond_atomic_t *cond, 695 wait_interrupt_t interruptible, 696 thread_continue_t continuation, 697 void *parameter); 698 699 /* 700 * sched_cond_wait: 701 * 702 * Assert wait and block on cond if no wakeup has been issued. 703 * If a wakeup has been issued on cond since the last `sched_cond_ack`, clear_wait and 704 * return `THREAD_AWAKENED`. 705 * 706 * `sched_cond_wait` must be paired with `sched_cond_ack`. 707 * 708 * NOTE: `continuation` will only be jumped to if a wakeup has not been issued 709 * 710 * parameters: 711 * cond atomic condition variable to synchronize on 712 * interruptible interruptible value to pass to assert_wait 713 * continuation continuation if block succeeds 714 */ 715 extern wait_result_t sched_cond_wait( 716 sched_cond_atomic_t *cond, 717 wait_interrupt_t interruptible, 718 thread_continue_t continuation); 719 720 /* 721 * sched_cond_ack: 722 * 723 * Acknowledge an issued wakeup by clearing WAKEUP and setting ACTIVE (via XOR). 724 * It is the callers responsibility to ensure that the ACTIVE bit is always low prior to calling 725 * (i.e. by calling `sched_cond_wait` prior to any rerun or block). 726 * Synchronization schemes that allow for WAKEUP bit to be reset prior to wakeup 727 * (e.g. a cancellation mechanism) should check that WAKEUP was indeed cleared. 728 * 729 * e.g. 730 * ``` 731 * if (sched_cond_ack(&my_state) & SCHED_THREAD_WAKEUP) { 732 * // WAKEUP bit was no longer set by the time this thread woke up 733 * do_cancellation_policy(); 734 * } 735 * ``` 736 * 737 * parameters: 738 * cond: atomic condition variable 739 */ 740 extern sched_cond_t sched_cond_ack( 741 sched_cond_atomic_t *cond); 742 743 #endif /* XNU_KERNEL_PRIVATE */ 744 745 #ifdef KERNEL_PRIVATE 746 /* Set pending block hint for a particular object before we go into a wait state */ 747 extern void thread_set_pending_block_hint( 748 thread_t thread, 749 block_hint_t block_hint); 750 751 #define QOS_PARALLELISM_COUNT_LOGICAL 0x1 752 #define QOS_PARALLELISM_REALTIME 0x2 753 #define QOS_PARALLELISM_CLUSTER_SHARED_RESOURCE 0x4 754 755 extern uint32_t qos_max_parallelism(int qos, uint64_t options); 756 #endif /* KERNEL_PRIVATE */ 757 758 #if XNU_KERNEL_PRIVATE 759 extern void thread_yield_with_continuation( 760 thread_continue_t continuation, 761 void *parameter) __dead2; 762 #endif 763 764 /* Context switch */ 765 extern wait_result_t thread_block( 766 thread_continue_t continuation); 767 768 extern wait_result_t thread_block_parameter( 769 thread_continue_t continuation, 770 void *parameter); 771 772 /* Declare thread will wait on a particular event */ 773 extern wait_result_t assert_wait( 774 event_t event, 775 wait_interrupt_t interruptible); 776 777 /* Assert that the thread intends to wait with a timeout */ 778 extern wait_result_t assert_wait_timeout( 779 event_t event, 780 wait_interrupt_t interruptible, 781 uint32_t interval, 782 uint32_t scale_factor); 783 784 /* Assert that the thread intends to wait with an urgency, timeout and leeway */ 785 extern wait_result_t assert_wait_timeout_with_leeway( 786 event_t event, 787 wait_interrupt_t interruptible, 788 wait_timeout_urgency_t urgency, 789 uint32_t interval, 790 uint32_t leeway, 791 uint32_t scale_factor); 792 793 extern wait_result_t assert_wait_deadline( 794 event_t event, 795 wait_interrupt_t interruptible, 796 uint64_t deadline); 797 798 /* Assert that the thread intends to wait with an urgency, deadline, and leeway */ 799 extern wait_result_t assert_wait_deadline_with_leeway( 800 event_t event, 801 wait_interrupt_t interruptible, 802 wait_timeout_urgency_t urgency, 803 uint64_t deadline, 804 uint64_t leeway); 805 806 807 /* Wake up thread (or threads) waiting on a particular event */ 808 extern kern_return_t thread_wakeup_prim( 809 event_t event, 810 boolean_t one_thread, 811 wait_result_t result); 812 813 #define thread_wakeup(x) \ 814 thread_wakeup_prim((x), FALSE, THREAD_AWAKENED) 815 #define thread_wakeup_with_result(x, z) \ 816 thread_wakeup_prim((x), FALSE, (z)) 817 #define thread_wakeup_one(x) \ 818 thread_wakeup_prim((x), TRUE, THREAD_AWAKENED) 819 820 /* Wakeup the specified thread if it is waiting on this event */ 821 extern kern_return_t thread_wakeup_thread(event_t event, thread_t thread); 822 823 extern boolean_t preemption_enabled(void); 824 825 #ifdef MACH_KERNEL_PRIVATE 826 827 828 #if !CONFIG_SCHED_TIMESHARE_CORE && !CONFIG_SCHED_CLUTCH && !CONFIG_SCHED_EDGE 829 #error Enable at least one scheduler algorithm in osfmk/conf/MASTER.XXX 830 #endif 831 832 /* 833 * The scheduling policy is fixed at compile-time, in order to save the performance 834 * cost of function pointer indirection that we would otherwise pay each time when 835 * making a policy-specific callout. 836 */ 837 #if __AMP__ 838 839 #if CONFIG_SCHED_EDGE 840 extern const struct sched_dispatch_table sched_edge_dispatch; 841 #define SCHED(f) (sched_edge_dispatch.f) 842 #else /* CONFIG_SCHED_EDGE */ 843 extern const struct sched_dispatch_table sched_amp_dispatch; 844 #define SCHED(f) (sched_amp_dispatch.f) 845 #endif /* CONFIG_SCHED_EDGE */ 846 847 #else /* __AMP__ */ 848 849 #if CONFIG_SCHED_CLUTCH 850 extern const struct sched_dispatch_table sched_clutch_dispatch; 851 #define SCHED(f) (sched_clutch_dispatch.f) 852 #else /* CONFIG_SCHED_CLUTCH */ 853 extern const struct sched_dispatch_table sched_dualq_dispatch; 854 #define SCHED(f) (sched_dualq_dispatch.f) 855 #endif /* CONFIG_SCHED_CLUTCH */ 856 857 #endif /* __AMP__ */ 858 859 struct sched_dispatch_table { 860 const char *sched_name; 861 void (*init)(void); /* Init global state */ 862 void (*timebase_init)(void); /* Timebase-dependent initialization */ 863 void (*processor_init)(processor_t processor); /* Per-processor scheduler init */ 864 void (*pset_init)(processor_set_t pset); /* Per-processor set scheduler init */ 865 866 void (*maintenance_continuation)(void); /* Function called regularly */ 867 868 /* 869 * Choose a thread of greater or equal priority from the per-processor 870 * runqueue for timeshare/fixed threads 871 */ 872 thread_t (*choose_thread)( 873 processor_t processor, 874 int priority, 875 thread_t prev_thread, 876 ast_t reason); 877 878 /* True if scheduler supports stealing threads for this pset */ 879 bool (*steal_thread_enabled)(processor_set_t pset); 880 881 /* 882 * Steal a thread from another processor in the pset so that it can run 883 * immediately 884 */ 885 thread_t (*steal_thread)( 886 processor_set_t pset); 887 888 /* 889 * Compute priority for a timeshare thread based on base priority. 890 */ 891 int (*compute_timeshare_priority)(thread_t thread); 892 893 /* 894 * Pick the best node for a thread to run on. 895 */ 896 pset_node_t (*choose_node)( 897 thread_t thread); 898 899 /* 900 * Pick the best processor for a thread (any kind of thread) to run on. 901 */ 902 processor_t (*choose_processor)( 903 processor_set_t pset, 904 processor_t processor, 905 thread_t thread); 906 /* 907 * Enqueue a timeshare or fixed priority thread onto the per-processor 908 * runqueue 909 */ 910 boolean_t (*processor_enqueue)( 911 processor_t processor, 912 thread_t thread, 913 sched_options_t options); 914 915 /* Migrate threads away in preparation for processor shutdown */ 916 void (*processor_queue_shutdown)( 917 processor_t processor); 918 919 /* Remove the specific thread from the per-processor runqueue */ 920 boolean_t (*processor_queue_remove)( 921 processor_t processor, 922 thread_t thread); 923 924 /* 925 * Does the per-processor runqueue have any timeshare or fixed priority 926 * threads on it? Called without pset lock held, so should 927 * not assume immutability while executing. 928 */ 929 boolean_t (*processor_queue_empty)(processor_t processor); 930 931 /* 932 * Would this priority trigger an urgent preemption if it's sitting 933 * on the per-processor runqueue? 934 */ 935 boolean_t (*priority_is_urgent)(int priority); 936 937 /* 938 * Does the per-processor runqueue contain runnable threads that 939 * should cause the currently-running thread to be preempted? 940 */ 941 ast_t (*processor_csw_check)(processor_t processor); 942 943 /* 944 * Does the per-processor runqueue contain a runnable thread 945 * of > or >= priority, as a preflight for choose_thread() or other 946 * thread selection 947 */ 948 boolean_t (*processor_queue_has_priority)(processor_t processor, 949 int priority, 950 boolean_t gte); 951 952 /* Quantum size for the specified non-realtime thread. */ 953 uint32_t (*initial_quantum_size)(thread_t thread); 954 955 /* Scheduler mode for a new thread */ 956 sched_mode_t (*initial_thread_sched_mode)(task_t parent_task); 957 958 /* 959 * Is it safe to call update_priority, which may change a thread's 960 * runqueue or other state. This can be used to throttle changes 961 * to dynamic priority. 962 */ 963 boolean_t (*can_update_priority)(thread_t thread); 964 965 /* 966 * Update both scheduled priority and other persistent state. 967 * Side effects may including migration to another processor's runqueue. 968 */ 969 void (*update_priority)(thread_t thread); 970 971 /* Lower overhead update to scheduled priority and state. */ 972 void (*lightweight_update_priority)(thread_t thread); 973 974 /* Callback for non-realtime threads when the quantum timer fires */ 975 void (*quantum_expire)(thread_t thread); 976 977 /* 978 * Runnable threads on per-processor runqueue. Should only 979 * be used for relative comparisons of load between processors. 980 */ 981 int (*processor_runq_count)(processor_t processor); 982 983 /* Aggregate runcount statistics for per-processor runqueue */ 984 uint64_t (*processor_runq_stats_count_sum)(processor_t processor); 985 986 boolean_t (*processor_bound_count)(processor_t processor); 987 988 void (*thread_update_scan)(sched_update_scan_context_t scan_context); 989 990 /* Supports more than one pset */ 991 boolean_t multiple_psets_enabled; 992 993 /* Supports avoid-processor */ 994 boolean_t avoid_processor_enabled; 995 996 /* Returns true if this processor should avoid running this thread. */ 997 bool (*thread_avoid_processor)(processor_t processor, thread_t thread, ast_t reason); 998 999 /* 1000 * Invoked when a processor is about to choose the idle thread 1001 * Used to send IPIs to a processor which would be preferred to be idle instead. 1002 * Returns true if the current processor should anticipate a quick IPI reply back 1003 * from another core. 1004 * Called with pset lock held, returns with pset lock unlocked. 1005 */ 1006 bool (*processor_balance)(processor_t processor, processor_set_t pset); 1007 rt_queue_t (*rt_runq)(processor_set_t pset); 1008 void (*rt_init)(processor_set_t pset); 1009 void (*rt_queue_shutdown)(processor_t processor); 1010 void (*rt_runq_scan)(sched_update_scan_context_t scan_context); 1011 int64_t (*rt_runq_count_sum)(void); 1012 thread_t (*rt_steal_thread)(processor_set_t pset, uint64_t earliest_deadline); 1013 1014 uint32_t (*qos_max_parallelism)(int qos, uint64_t options); 1015 void (*check_spill)(processor_set_t pset, thread_t thread); 1016 sched_ipi_type_t (*ipi_policy)(processor_t dst, thread_t thread, boolean_t dst_idle, sched_ipi_event_t event); 1017 bool (*thread_should_yield)(processor_t processor, thread_t thread); 1018 1019 /* Routine to update run counts */ 1020 uint32_t (*run_count_incr)(thread_t thread); 1021 uint32_t (*run_count_decr)(thread_t thread); 1022 1023 /* Routine to update scheduling bucket for a thread */ 1024 void (*update_thread_bucket)(thread_t thread); 1025 1026 /* Routine to inform the scheduler when a new pset becomes schedulable */ 1027 void (*pset_made_schedulable)(processor_t processor, processor_set_t pset, boolean_t drop_lock); 1028 #if CONFIG_THREAD_GROUPS 1029 /* Routine to inform the scheduler when CLPC changes a thread group recommendation */ 1030 void (*thread_group_recommendation_change)(struct thread_group *tg, cluster_type_t new_recommendation); 1031 #endif 1032 /* Routine to inform the scheduler when all CPUs have finished initializing */ 1033 void (*cpu_init_completed)(void); 1034 /* Routine to check if a thread is eligible to execute on a specific pset */ 1035 bool (*thread_eligible_for_pset)(thread_t thread, processor_set_t pset); 1036 }; 1037 1038 extern const struct sched_dispatch_table sched_dualq_dispatch; 1039 #if __AMP__ 1040 extern const struct sched_dispatch_table sched_amp_dispatch; 1041 #endif 1042 1043 #if defined(CONFIG_SCHED_CLUTCH) 1044 extern const struct sched_dispatch_table sched_clutch_dispatch; 1045 #endif 1046 1047 #if defined(CONFIG_SCHED_EDGE) 1048 extern const struct sched_dispatch_table sched_edge_dispatch; 1049 #endif 1050 1051 extern void sched_set_max_unsafe_rt_quanta(int max); 1052 extern void sched_set_max_unsafe_fixed_quanta(int max); 1053 1054 #endif /* MACH_KERNEL_PRIVATE */ 1055 1056 __END_DECLS 1057 1058 #endif /* _KERN_SCHED_PRIM_H_ */ 1059