2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * Scheduling primitives
69 #include <mach/mach_types.h>
70 #include <mach/machine.h>
71 #include <mach/policy.h>
72 #include <mach/sync_policy.h>
73 #include <mach/thread_act.h>
75 #include <machine/machine_routines.h>
76 #include <machine/sched_param.h>
77 #include <machine/machine_cpu.h>
78 #include <machine/limits.h>
79 #include <machine/atomic.h>
81 #include <machine/commpage.h>
83 #include <kern/kern_types.h>
84 #include <kern/backtrace.h>
85 #include <kern/clock.h>
86 #include <kern/counters.h>
87 #include <kern/cpu_number.h>
88 #include <kern/cpu_data.h>
90 #include <kern/debug.h>
91 #include <kern/macro_help.h>
92 #include <kern/machine.h>
93 #include <kern/misc_protos.h>
95 #include <kern/monotonic.h>
96 #endif /* MONOTONIC */
97 #include <kern/processor.h>
98 #include <kern/queue.h>
99 #include <kern/sched.h>
100 #include <kern/sched_prim.h>
101 #include <kern/sfi.h>
102 #include <kern/syscall_subr.h>
103 #include <kern/task.h>
104 #include <kern/thread.h>
105 #include <kern/ledger.h>
106 #include <kern/timer_queue.h>
107 #include <kern/waitq.h>
108 #include <kern/policy_internal.h>
109 #include <kern/cpu_quiesce.h>
112 #include <vm/vm_kern.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_pageout.h>
116 #include <mach/sdt.h>
117 #include <mach/mach_host.h>
118 #include <mach/host_info.h>
120 #include <sys/kdebug.h>
121 #include <kperf/kperf.h>
122 #include <kern/kpc.h>
123 #include <san/kasan.h>
124 #include <kern/pms.h>
125 #include <kern/host.h>
126 #include <stdatomic.h>
128 struct sched_statistics
PERCPU_DATA(sched_stats
);
129 bool sched_stats_active
;
132 rt_runq_count(processor_set_t pset
)
134 return atomic_load_explicit(&SCHED(rt_runq
)(pset
)->count
, memory_order_relaxed
);
138 rt_runq_count_incr(processor_set_t pset
)
140 atomic_fetch_add_explicit(&SCHED(rt_runq
)(pset
)->count
, 1, memory_order_relaxed
);
144 rt_runq_count_decr(processor_set_t pset
)
146 atomic_fetch_sub_explicit(&SCHED(rt_runq
)(pset
)->count
, 1, memory_order_relaxed
);
149 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
150 TUNABLE(int, default_preemption_rate
, "preempt", DEFAULT_PREEMPTION_RATE
);
152 #define DEFAULT_BG_PREEMPTION_RATE 400 /* (1/s) */
153 TUNABLE(int, default_bg_preemption_rate
, "bg_preempt", DEFAULT_BG_PREEMPTION_RATE
);
155 #define MAX_UNSAFE_QUANTA 800
156 TUNABLE(int, max_unsafe_quanta
, "unsafe", MAX_UNSAFE_QUANTA
);
158 #define MAX_POLL_QUANTA 2
159 TUNABLE(int, max_poll_quanta
, "poll", MAX_POLL_QUANTA
);
161 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
162 int sched_poll_yield_shift
= SCHED_POLL_YIELD_SHIFT
;
164 uint64_t max_poll_computation
;
166 uint64_t max_unsafe_computation
;
167 uint64_t sched_safe_duration
;
169 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
171 uint32_t std_quantum
;
172 uint32_t min_std_quantum
;
175 uint32_t std_quantum_us
;
176 uint32_t bg_quantum_us
;
178 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
180 uint32_t thread_depress_time
;
181 uint32_t default_timeshare_computation
;
182 uint32_t default_timeshare_constraint
;
184 uint32_t max_rt_quantum
;
185 uint32_t min_rt_quantum
;
187 uint32_t rt_constraint_threshold
;
189 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
192 uint32_t sched_tick_interval
;
194 /* Timeshare load calculation interval (15ms) */
195 uint32_t sched_load_compute_interval_us
= 15000;
196 uint64_t sched_load_compute_interval_abs
;
197 static _Atomic
uint64_t sched_load_compute_deadline
;
199 uint32_t sched_pri_shifts
[TH_BUCKET_MAX
];
200 uint32_t sched_fixed_shift
;
202 uint32_t sched_decay_usage_age_factor
= 1; /* accelerate 5/8^n usage aging */
204 /* Allow foreground to decay past default to resolve inversions */
205 #define DEFAULT_DECAY_BAND_LIMIT ((BASEPRI_FOREGROUND - BASEPRI_DEFAULT) + 2)
206 int sched_pri_decay_band_limit
= DEFAULT_DECAY_BAND_LIMIT
;
208 /* Defaults for timer deadline profiling */
209 #define TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT 2000000 /* Timers with deadlines <=
211 #define TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT 5000000 /* Timers with deadlines
214 uint64_t timer_deadline_tracking_bin_1
;
215 uint64_t timer_deadline_tracking_bin_2
;
217 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
219 thread_t sched_maintenance_thread
;
221 /* interrupts disabled lock to guard recommended cores state */
222 decl_simple_lock_data(static, sched_recommended_cores_lock
);
223 static uint64_t usercontrol_requested_recommended_cores
= ALL_CORES_RECOMMENDED
;
224 static void sched_update_recommended_cores(uint64_t recommended_cores
);
226 #if __arm__ || __arm64__
227 static void sched_recommended_cores_maintenance(void);
228 uint64_t perfcontrol_failsafe_starvation_threshold
;
229 extern char *proc_name_address(struct proc
*p
);
230 #endif /* __arm__ || __arm64__ */
232 uint64_t sched_one_second_interval
;
233 boolean_t allow_direct_handoff
= TRUE
;
237 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
239 static void load_shift_init(void);
240 static void preempt_pri_init(void);
242 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
244 thread_t
processor_idle(
246 processor_t processor
);
251 processor_t processor
,
252 processor_set_t pset
,
255 static void processor_setrun(
256 processor_t processor
,
261 sched_realtime_timebase_init(void);
264 sched_timer_deadline_tracking_init(void);
267 extern int debug_task
;
268 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
270 #define TLOG(a, fmt, args...) do {} while (0)
274 thread_bind_internal(
276 processor_t processor
);
279 sched_vm_group_maintenance(void);
281 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
282 int8_t sched_load_shifts
[NRQS
];
283 bitmap_t sched_preempt_pri
[BITMAP_LEN(NRQS_MAX
)];
284 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
287 * Statically allocate a buffer to hold the longest possible
288 * scheduler description string, as currently implemented.
289 * bsd/kern/kern_sysctl.c has a corresponding definition in bsd/
290 * to export to userspace via sysctl(3). If either version
291 * changes, update the other.
293 * Note that in addition to being an upper bound on the strings
294 * in the kernel, it's also an exact parameter to PE_get_default(),
295 * which interrogates the device tree on some platforms. That
296 * API requires the caller know the exact size of the device tree
297 * property, so we need both a legacy size (32) and the current size
298 * (48) to deal with old and new device trees. The device tree property
299 * is similarly padded to a fixed size so that the same kernel image
300 * can run on multiple devices with different schedulers configured
301 * in the device tree.
303 char sched_string
[SCHED_STRING_MAX_LENGTH
];
305 uint32_t sched_debug_flags
= SCHED_DEBUG_FLAG_CHOOSE_PROCESSOR_TRACEPOINTS
;
307 /* Global flag which indicates whether Background Stepper Context is enabled */
308 static int cpu_throttle_enabled
= 1;
313 boolean_t direct_handoff
= FALSE
;
314 kprintf("Scheduler: Default of %s\n", SCHED(sched_name
));
316 if (!PE_parse_boot_argn("sched_pri_decay_limit", &sched_pri_decay_band_limit
, sizeof(sched_pri_decay_band_limit
))) {
317 /* No boot-args, check in device tree */
318 if (!PE_get_default("kern.sched_pri_decay_limit",
319 &sched_pri_decay_band_limit
,
320 sizeof(sched_pri_decay_band_limit
))) {
321 /* Allow decay all the way to normal limits */
322 sched_pri_decay_band_limit
= DEFAULT_DECAY_BAND_LIMIT
;
326 kprintf("Setting scheduler priority decay band limit %d\n", sched_pri_decay_band_limit
);
328 if (PE_parse_boot_argn("sched_debug", &sched_debug_flags
, sizeof(sched_debug_flags
))) {
329 kprintf("Scheduler: Debug flags 0x%08x\n", sched_debug_flags
);
331 strlcpy(sched_string
, SCHED(sched_name
), sizeof(sched_string
));
333 cpu_quiescent_counter_init();
336 SCHED(rt_init
)(&pset0
);
337 sched_timer_deadline_tracking_init();
339 SCHED(pset_init
)(&pset0
);
340 SCHED(processor_init
)(master_processor
);
342 if (PE_parse_boot_argn("direct_handoff", &direct_handoff
, sizeof(direct_handoff
))) {
343 allow_direct_handoff
= direct_handoff
;
348 sched_timebase_init(void)
352 clock_interval_to_absolutetime_interval(1, NSEC_PER_SEC
, &abstime
);
353 sched_one_second_interval
= abstime
;
355 SCHED(timebase_init
)();
356 sched_realtime_timebase_init();
359 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
362 sched_timeshare_init(void)
365 * Calculate the timeslicing quantum
368 if (default_preemption_rate
< 1) {
369 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
371 std_quantum_us
= (1000 * 1000) / default_preemption_rate
;
373 printf("standard timeslicing quantum is %d us\n", std_quantum_us
);
375 if (default_bg_preemption_rate
< 1) {
376 default_bg_preemption_rate
= DEFAULT_BG_PREEMPTION_RATE
;
378 bg_quantum_us
= (1000 * 1000) / default_bg_preemption_rate
;
380 printf("standard background quantum is %d us\n", bg_quantum_us
);
388 sched_timeshare_timebase_init(void)
393 /* standard timeslicing quantum */
394 clock_interval_to_absolutetime_interval(
395 std_quantum_us
, NSEC_PER_USEC
, &abstime
);
396 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
397 std_quantum
= (uint32_t)abstime
;
399 /* smallest remaining quantum (250 us) */
400 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC
, &abstime
);
401 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
402 min_std_quantum
= (uint32_t)abstime
;
404 /* quantum for background tasks */
405 clock_interval_to_absolutetime_interval(
406 bg_quantum_us
, NSEC_PER_USEC
, &abstime
);
407 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
408 bg_quantum
= (uint32_t)abstime
;
410 /* scheduler tick interval */
411 clock_interval_to_absolutetime_interval(USEC_PER_SEC
>> SCHED_TICK_SHIFT
,
412 NSEC_PER_USEC
, &abstime
);
413 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
414 sched_tick_interval
= (uint32_t)abstime
;
416 /* timeshare load calculation interval & deadline initialization */
417 clock_interval_to_absolutetime_interval(sched_load_compute_interval_us
, NSEC_PER_USEC
, &sched_load_compute_interval_abs
);
418 os_atomic_init(&sched_load_compute_deadline
, sched_load_compute_interval_abs
);
421 * Compute conversion factor from usage to
422 * timesharing priorities with 5/8 ** n aging.
424 abstime
= (abstime
* 5) / 3;
425 for (shift
= 0; abstime
> BASEPRI_DEFAULT
; ++shift
) {
428 sched_fixed_shift
= shift
;
430 for (uint32_t i
= 0; i
< TH_BUCKET_MAX
; i
++) {
431 sched_pri_shifts
[i
] = INT8_MAX
;
434 max_unsafe_computation
= ((uint64_t)max_unsafe_quanta
) * std_quantum
;
435 sched_safe_duration
= 2 * ((uint64_t)max_unsafe_quanta
) * std_quantum
;
437 max_poll_computation
= ((uint64_t)max_poll_quanta
) * std_quantum
;
438 thread_depress_time
= 1 * std_quantum
;
439 default_timeshare_computation
= std_quantum
/ 2;
440 default_timeshare_constraint
= std_quantum
;
442 #if __arm__ || __arm64__
443 perfcontrol_failsafe_starvation_threshold
= (2 * sched_tick_interval
);
444 #endif /* __arm__ || __arm64__ */
447 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
450 pset_rt_init(processor_set_t pset
)
452 os_atomic_init(&pset
->rt_runq
.count
, 0);
453 queue_init(&pset
->rt_runq
.queue
);
454 memset(&pset
->rt_runq
.runq_stats
, 0, sizeof pset
->rt_runq
.runq_stats
);
458 sched_realtime_timebase_init(void)
462 /* smallest rt computaton (50 us) */
463 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC
, &abstime
);
464 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
465 min_rt_quantum
= (uint32_t)abstime
;
467 /* maximum rt computation (50 ms) */
468 clock_interval_to_absolutetime_interval(
469 50, 1000 * NSEC_PER_USEC
, &abstime
);
470 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
471 max_rt_quantum
= (uint32_t)abstime
;
473 /* constraint threshold for sending backup IPIs (4 ms) */
474 clock_interval_to_absolutetime_interval(4, NSEC_PER_MSEC
, &abstime
);
475 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
476 rt_constraint_threshold
= (uint32_t)abstime
;
480 sched_check_spill(processor_set_t pset
, thread_t thread
)
489 sched_thread_should_yield(processor_t processor
, thread_t thread
)
493 return !SCHED(processor_queue_empty
)(processor
) || rt_runq_count(processor
->processor_set
) > 0;
496 /* Default implementations of .steal_thread_enabled */
498 sched_steal_thread_DISABLED(processor_set_t pset
)
505 sched_steal_thread_enabled(processor_set_t pset
)
507 return bit_count(pset
->node
->pset_map
) > 1;
510 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
513 * Set up values for timeshare
517 load_shift_init(void)
519 int8_t k
, *p
= sched_load_shifts
;
522 uint32_t sched_decay_penalty
= 1;
524 if (PE_parse_boot_argn("sched_decay_penalty", &sched_decay_penalty
, sizeof(sched_decay_penalty
))) {
525 kprintf("Overriding scheduler decay penalty %u\n", sched_decay_penalty
);
528 if (PE_parse_boot_argn("sched_decay_usage_age_factor", &sched_decay_usage_age_factor
, sizeof(sched_decay_usage_age_factor
))) {
529 kprintf("Overriding scheduler decay usage age factor %u\n", sched_decay_usage_age_factor
);
532 if (sched_decay_penalty
== 0) {
534 * There is no penalty for timeshare threads for using too much
535 * CPU, so set all load shifts to INT8_MIN. Even under high load,
536 * sched_pri_shift will be >INT8_MAX, and there will be no
537 * penalty applied to threads (nor will sched_usage be updated per
540 for (i
= 0; i
< NRQS
; i
++) {
541 sched_load_shifts
[i
] = INT8_MIN
;
547 *p
++ = INT8_MIN
; *p
++ = 0;
550 * For a given system load "i", the per-thread priority
551 * penalty per quantum of CPU usage is ~2^k priority
552 * levels. "sched_decay_penalty" can cause more
553 * array entries to be filled with smaller "k" values
555 for (i
= 2, j
= 1 << sched_decay_penalty
, k
= 1; i
< NRQS
; ++k
) {
556 for (j
<<= 1; (i
< j
) && (i
< NRQS
); ++i
) {
563 preempt_pri_init(void)
565 bitmap_t
*p
= sched_preempt_pri
;
567 for (int i
= BASEPRI_FOREGROUND
; i
< MINPRI_KERNEL
; ++i
) {
571 for (int i
= BASEPRI_PREEMPT
; i
<= MAXPRI
; ++i
) {
576 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
579 * Thread wait timer expiration.
586 thread_t thread
= p0
;
589 assert_thread_magic(thread
);
593 if (--thread
->wait_timer_active
== 0) {
594 if (thread
->wait_timer_is_set
) {
595 thread
->wait_timer_is_set
= FALSE
;
596 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
599 thread_unlock(thread
);
606 * Unblock thread on wake up.
608 * Returns TRUE if the thread should now be placed on the runqueue.
610 * Thread must be locked.
612 * Called at splsched().
617 wait_result_t wresult
)
619 boolean_t ready_for_runq
= FALSE
;
620 thread_t cthread
= current_thread();
621 uint32_t new_run_count
;
622 int old_thread_state
;
627 thread
->wait_result
= wresult
;
630 * Cancel pending wait timer.
632 if (thread
->wait_timer_is_set
) {
633 if (timer_call_cancel(&thread
->wait_timer
)) {
634 thread
->wait_timer_active
--;
636 thread
->wait_timer_is_set
= FALSE
;
639 boolean_t aticontext
, pidle
;
640 ml_get_power_state(&aticontext
, &pidle
);
643 * Update scheduling state: not waiting,
646 old_thread_state
= thread
->state
;
647 thread
->state
= (old_thread_state
| TH_RUN
) &
648 ~(TH_WAIT
| TH_UNINT
| TH_WAIT_REPORT
);
650 if ((old_thread_state
& TH_RUN
) == 0) {
651 uint64_t ctime
= mach_approximate_time();
652 thread
->last_made_runnable_time
= thread
->last_basepri_change_time
= ctime
;
653 timer_start(&thread
->runnable_timer
, ctime
);
655 ready_for_runq
= TRUE
;
657 if (old_thread_state
& TH_WAIT_REPORT
) {
658 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
661 /* Update the runnable thread count */
662 new_run_count
= SCHED(run_count_incr
)(thread
);
664 #if CONFIG_SCHED_AUTO_JOIN
665 if (aticontext
== FALSE
&& work_interval_should_propagate(cthread
, thread
)) {
666 work_interval_auto_join_propagate(cthread
, thread
);
668 #endif /*CONFIG_SCHED_AUTO_JOIN */
671 * Either the thread is idling in place on another processor,
672 * or it hasn't finished context switching yet.
674 assert((thread
->state
& TH_IDLE
) == 0);
676 * The run count is only dropped after the context switch completes
677 * and the thread is still waiting, so we should not run_incr here
679 new_run_count
= os_atomic_load(&sched_run_buckets
[TH_BUCKET_RUN
], relaxed
);
683 * Calculate deadline for real-time threads.
685 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
688 ctime
= mach_absolute_time();
689 thread
->realtime
.deadline
= thread
->realtime
.constraint
+ ctime
;
693 * Clear old quantum, fail-safe computation, etc.
695 thread
->quantum_remaining
= 0;
696 thread
->computation_metered
= 0;
697 thread
->reason
= AST_NONE
;
698 thread
->block_hint
= kThreadWaitNone
;
700 /* Obtain power-relevant interrupt and "platform-idle exit" statistics.
701 * We also account for "double hop" thread signaling via
702 * the thread callout infrastructure.
703 * DRK: consider removing the callout wakeup counters in the future
704 * they're present for verification at the moment.
707 if (__improbable(aticontext
&& !(thread_get_tag_internal(thread
) & THREAD_TAG_CALLOUT
))) {
708 DTRACE_SCHED2(iwakeup
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
710 uint64_t ttd
= current_processor()->timer_call_ttd
;
713 if (ttd
<= timer_deadline_tracking_bin_1
) {
714 thread
->thread_timer_wakeups_bin_1
++;
715 } else if (ttd
<= timer_deadline_tracking_bin_2
) {
716 thread
->thread_timer_wakeups_bin_2
++;
720 ledger_credit_thread(thread
, thread
->t_ledger
,
721 task_ledgers
.interrupt_wakeups
, 1);
723 ledger_credit_thread(thread
, thread
->t_ledger
,
724 task_ledgers
.platform_idle_wakeups
, 1);
726 } else if (thread_get_tag_internal(cthread
) & THREAD_TAG_CALLOUT
) {
727 /* TODO: what about an interrupt that does a wake taken on a callout thread? */
728 if (cthread
->callout_woken_from_icontext
) {
729 ledger_credit_thread(thread
, thread
->t_ledger
,
730 task_ledgers
.interrupt_wakeups
, 1);
731 thread
->thread_callout_interrupt_wakeups
++;
733 if (cthread
->callout_woken_from_platform_idle
) {
734 ledger_credit_thread(thread
, thread
->t_ledger
,
735 task_ledgers
.platform_idle_wakeups
, 1);
736 thread
->thread_callout_platform_idle_wakeups
++;
739 cthread
->callout_woke_thread
= TRUE
;
743 if (thread_get_tag_internal(thread
) & THREAD_TAG_CALLOUT
) {
744 thread
->callout_woken_from_icontext
= !!aticontext
;
745 thread
->callout_woken_from_platform_idle
= !!pidle
;
746 thread
->callout_woke_thread
= FALSE
;
750 if (ready_for_runq
) {
751 kperf_make_runnable(thread
, aticontext
);
755 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
756 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_MAKE_RUNNABLE
) | DBG_FUNC_NONE
,
757 (uintptr_t)thread_tid(thread
), thread
->sched_pri
, thread
->wait_result
,
758 sched_run_buckets
[TH_BUCKET_RUN
], 0);
760 DTRACE_SCHED2(wakeup
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
762 return ready_for_runq
;
766 * Routine: thread_allowed_for_handoff
768 * Check if the thread is allowed for handoff operation
770 * thread lock held, IPC locks may be held.
771 * TODO: In future, do not allow handoff if threads have different cluster
775 thread_allowed_for_handoff(
778 thread_t self
= current_thread();
780 if (allow_direct_handoff
&&
781 thread
->sched_mode
== TH_MODE_REALTIME
&&
782 self
->sched_mode
== TH_MODE_REALTIME
) {
792 * Unblock and dispatch thread.
794 * thread lock held, IPC locks may be held.
795 * thread must have been pulled from wait queue under same lock hold.
796 * thread must have been waiting
798 * KERN_SUCCESS - Thread was set running
800 * TODO: This should return void
805 wait_result_t wresult
,
806 waitq_options_t option
)
808 thread_t self
= current_thread();
810 assert_thread_magic(thread
);
812 assert(thread
->at_safe_point
== FALSE
);
813 assert(thread
->wait_event
== NO_EVENT64
);
814 assert(thread
->waitq
== NULL
);
816 assert(!(thread
->state
& (TH_TERMINATE
| TH_TERMINATE2
)));
817 assert(thread
->state
& TH_WAIT
);
820 if (thread_unblock(thread
, wresult
)) {
821 #if SCHED_TRACE_THREAD_WAKEUPS
822 backtrace(&thread
->thread_wakeup_bt
[0],
823 (sizeof(thread
->thread_wakeup_bt
) / sizeof(uintptr_t)), NULL
);
825 if ((option
& WQ_OPTION_HANDOFF
) &&
826 thread_allowed_for_handoff(thread
)) {
827 thread_reference(thread
);
828 assert(self
->handoff_thread
== NULL
);
829 self
->handoff_thread
= thread
;
831 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
839 * Routine: thread_mark_wait_locked
841 * Mark a thread as waiting. If, given the circumstances,
842 * it doesn't want to wait (i.e. already aborted), then
843 * indicate that in the return value.
845 * at splsched() and thread is locked.
849 thread_mark_wait_locked(
851 wait_interrupt_t interruptible_orig
)
853 boolean_t at_safe_point
;
854 wait_interrupt_t interruptible
= interruptible_orig
;
856 if (thread
->state
& TH_IDLE
) {
857 panic("Invalid attempt to wait while running the idle thread");
860 assert(!(thread
->state
& (TH_WAIT
| TH_IDLE
| TH_UNINT
| TH_TERMINATE2
| TH_WAIT_REPORT
)));
863 * The thread may have certain types of interrupts/aborts masked
864 * off. Even if the wait location says these types of interrupts
865 * are OK, we have to honor mask settings (outer-scoped code may
866 * not be able to handle aborts at the moment).
868 interruptible
&= TH_OPT_INTMASK
;
869 if (interruptible
> (thread
->options
& TH_OPT_INTMASK
)) {
870 interruptible
= thread
->options
& TH_OPT_INTMASK
;
873 at_safe_point
= (interruptible
== THREAD_ABORTSAFE
);
875 if (interruptible
== THREAD_UNINT
||
876 !(thread
->sched_flags
& TH_SFLAG_ABORT
) ||
878 (thread
->sched_flags
& TH_SFLAG_ABORTSAFELY
))) {
879 if (!(thread
->state
& TH_TERMINATE
)) {
883 int state_bits
= TH_WAIT
;
884 if (!interruptible
) {
885 state_bits
|= TH_UNINT
;
887 if (thread
->sched_call
) {
888 wait_interrupt_t mask
= THREAD_WAIT_NOREPORT_USER
;
889 if (is_kerneltask(thread
->task
)) {
890 mask
= THREAD_WAIT_NOREPORT_KERNEL
;
892 if ((interruptible_orig
& mask
) == 0) {
893 state_bits
|= TH_WAIT_REPORT
;
896 thread
->state
|= state_bits
;
897 thread
->at_safe_point
= at_safe_point
;
899 /* TODO: pass this through assert_wait instead, have
900 * assert_wait just take a struct as an argument */
901 assert(!thread
->block_hint
);
902 thread
->block_hint
= thread
->pending_block_hint
;
903 thread
->pending_block_hint
= kThreadWaitNone
;
905 return thread
->wait_result
= THREAD_WAITING
;
907 if (thread
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
908 thread
->sched_flags
&= ~TH_SFLAG_ABORTED_MASK
;
911 thread
->pending_block_hint
= kThreadWaitNone
;
913 return thread
->wait_result
= THREAD_INTERRUPTED
;
917 * Routine: thread_interrupt_level
919 * Set the maximum interruptible state for the
920 * current thread. The effective value of any
921 * interruptible flag passed into assert_wait
922 * will never exceed this.
924 * Useful for code that must not be interrupted,
925 * but which calls code that doesn't know that.
927 * The old interrupt level for the thread.
931 thread_interrupt_level(
932 wait_interrupt_t new_level
)
934 thread_t thread
= current_thread();
935 wait_interrupt_t result
= thread
->options
& TH_OPT_INTMASK
;
937 thread
->options
= (thread
->options
& ~TH_OPT_INTMASK
) | (new_level
& TH_OPT_INTMASK
);
945 * Assert that the current thread is about to go to
946 * sleep until the specified event occurs.
951 wait_interrupt_t interruptible
)
953 if (__improbable(event
== NO_EVENT
)) {
954 panic("%s() called with NO_EVENT", __func__
);
957 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
958 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAIT
) | DBG_FUNC_NONE
,
959 VM_KERNEL_UNSLIDE_OR_PERM(event
), 0, 0, 0, 0);
962 waitq
= global_eventq(event
);
963 return waitq_assert_wait64(waitq
, CAST_EVENT64_T(event
), interruptible
, TIMEOUT_WAIT_FOREVER
);
969 * Return the global waitq for the specified event
975 return global_eventq(event
);
981 wait_interrupt_t interruptible
,
983 uint32_t scale_factor
)
985 thread_t thread
= current_thread();
986 wait_result_t wresult
;
990 if (__improbable(event
== NO_EVENT
)) {
991 panic("%s() called with NO_EVENT", __func__
);
995 waitq
= global_eventq(event
);
1000 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
1002 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1003 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAIT
) | DBG_FUNC_NONE
,
1004 VM_KERNEL_UNSLIDE_OR_PERM(event
), interruptible
, deadline
, 0, 0);
1006 wresult
= waitq_assert_wait64_locked(waitq
, CAST_EVENT64_T(event
),
1008 TIMEOUT_URGENCY_SYS_NORMAL
,
1009 deadline
, TIMEOUT_NO_LEEWAY
,
1012 waitq_unlock(waitq
);
1018 assert_wait_timeout_with_leeway(
1020 wait_interrupt_t interruptible
,
1021 wait_timeout_urgency_t urgency
,
1024 uint32_t scale_factor
)
1026 thread_t thread
= current_thread();
1027 wait_result_t wresult
;
1034 if (__improbable(event
== NO_EVENT
)) {
1035 panic("%s() called with NO_EVENT", __func__
);
1038 now
= mach_absolute_time();
1039 clock_interval_to_absolutetime_interval(interval
, scale_factor
, &abstime
);
1040 deadline
= now
+ abstime
;
1042 clock_interval_to_absolutetime_interval(leeway
, scale_factor
, &slop
);
1044 struct waitq
*waitq
;
1045 waitq
= global_eventq(event
);
1050 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1051 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAIT
) | DBG_FUNC_NONE
,
1052 VM_KERNEL_UNSLIDE_OR_PERM(event
), interruptible
, deadline
, 0, 0);
1054 wresult
= waitq_assert_wait64_locked(waitq
, CAST_EVENT64_T(event
),
1056 urgency
, deadline
, slop
,
1059 waitq_unlock(waitq
);
1065 assert_wait_deadline(
1067 wait_interrupt_t interruptible
,
1070 thread_t thread
= current_thread();
1071 wait_result_t wresult
;
1074 if (__improbable(event
== NO_EVENT
)) {
1075 panic("%s() called with NO_EVENT", __func__
);
1078 struct waitq
*waitq
;
1079 waitq
= global_eventq(event
);
1084 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1085 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAIT
) | DBG_FUNC_NONE
,
1086 VM_KERNEL_UNSLIDE_OR_PERM(event
), interruptible
, deadline
, 0, 0);
1088 wresult
= waitq_assert_wait64_locked(waitq
, CAST_EVENT64_T(event
),
1090 TIMEOUT_URGENCY_SYS_NORMAL
, deadline
,
1091 TIMEOUT_NO_LEEWAY
, thread
);
1092 waitq_unlock(waitq
);
1098 assert_wait_deadline_with_leeway(
1100 wait_interrupt_t interruptible
,
1101 wait_timeout_urgency_t urgency
,
1105 thread_t thread
= current_thread();
1106 wait_result_t wresult
;
1109 if (__improbable(event
== NO_EVENT
)) {
1110 panic("%s() called with NO_EVENT", __func__
);
1113 struct waitq
*waitq
;
1114 waitq
= global_eventq(event
);
1119 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1120 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAIT
) | DBG_FUNC_NONE
,
1121 VM_KERNEL_UNSLIDE_OR_PERM(event
), interruptible
, deadline
, 0, 0);
1123 wresult
= waitq_assert_wait64_locked(waitq
, CAST_EVENT64_T(event
),
1125 urgency
, deadline
, leeway
,
1127 waitq_unlock(waitq
);
1135 * Return TRUE if a thread is running on a processor such that an AST
1136 * is needed to pull it out of userspace execution, or if executing in
1137 * the kernel, bring to a context switch boundary that would cause
1138 * thread state to be serialized in the thread PCB.
1140 * Thread locked, returns the same way. While locked, fields
1141 * like "state" cannot change. "runq" can change only from set to unset.
1143 static inline boolean_t
1144 thread_isoncpu(thread_t thread
)
1146 /* Not running or runnable */
1147 if (!(thread
->state
& TH_RUN
)) {
1151 /* Waiting on a runqueue, not currently running */
1152 /* TODO: This is invalid - it can get dequeued without thread lock, but not context switched. */
1153 if (thread
->runq
!= PROCESSOR_NULL
) {
1158 * Thread does not have a stack yet
1159 * It could be on the stack alloc queue or preparing to be invoked
1161 if (!thread
->kernel_stack
) {
1166 * Thread must be running on a processor, or
1167 * about to run, or just did run. In all these
1168 * cases, an AST to the processor is needed
1169 * to guarantee that the thread is kicked out
1170 * of userspace and the processor has
1171 * context switched (and saved register state).
1179 * Force a preemption point for a thread and wait
1180 * for it to stop running on a CPU. If a stronger
1181 * guarantee is requested, wait until no longer
1182 * runnable. Arbitrates access among
1183 * multiple stop requests. (released by unstop)
1185 * The thread must enter a wait state and stop via a
1188 * Returns FALSE if interrupted.
1193 boolean_t until_not_runnable
)
1195 wait_result_t wresult
;
1196 spl_t s
= splsched();
1200 thread_lock(thread
);
1202 while (thread
->state
& TH_SUSP
) {
1203 thread
->wake_active
= TRUE
;
1204 thread_unlock(thread
);
1206 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
1207 wake_unlock(thread
);
1210 if (wresult
== THREAD_WAITING
) {
1211 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1214 if (wresult
!= THREAD_AWAKENED
) {
1220 thread_lock(thread
);
1223 thread
->state
|= TH_SUSP
;
1225 while ((oncpu
= thread_isoncpu(thread
)) ||
1226 (until_not_runnable
&& (thread
->state
& TH_RUN
))) {
1227 processor_t processor
;
1230 assert(thread
->state
& TH_RUN
);
1231 processor
= thread
->chosen_processor
;
1232 cause_ast_check(processor
);
1235 thread
->wake_active
= TRUE
;
1236 thread_unlock(thread
);
1238 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
1239 wake_unlock(thread
);
1242 if (wresult
== THREAD_WAITING
) {
1243 wresult
= thread_block(THREAD_CONTINUE_NULL
);
1246 if (wresult
!= THREAD_AWAKENED
) {
1247 thread_unstop(thread
);
1253 thread_lock(thread
);
1256 thread_unlock(thread
);
1257 wake_unlock(thread
);
1261 * We return with the thread unlocked. To prevent it from
1262 * transitioning to a runnable state (or from TH_RUN to
1263 * being on the CPU), the caller must ensure the thread
1264 * is stopped via an external means (such as an AST)
1273 * Release a previous stop request and set
1274 * the thread running if appropriate.
1276 * Use only after a successful stop operation.
1282 spl_t s
= splsched();
1285 thread_lock(thread
);
1287 assert((thread
->state
& (TH_RUN
| TH_WAIT
| TH_SUSP
)) != TH_SUSP
);
1289 if (thread
->state
& TH_SUSP
) {
1290 thread
->state
&= ~TH_SUSP
;
1292 if (thread
->wake_active
) {
1293 thread
->wake_active
= FALSE
;
1294 thread_unlock(thread
);
1296 thread_wakeup(&thread
->wake_active
);
1297 wake_unlock(thread
);
1304 thread_unlock(thread
);
1305 wake_unlock(thread
);
1312 * Wait for a thread to stop running. (non-interruptible)
1318 boolean_t until_not_runnable
)
1320 wait_result_t wresult
;
1322 processor_t processor
;
1323 spl_t s
= splsched();
1326 thread_lock(thread
);
1329 * Wait until not running on a CPU. If stronger requirement
1330 * desired, wait until not runnable. Assumption: if thread is
1331 * on CPU, then TH_RUN is set, so we're not waiting in any case
1332 * where the original, pure "TH_RUN" check would have let us
1335 while ((oncpu
= thread_isoncpu(thread
)) ||
1336 (until_not_runnable
&& (thread
->state
& TH_RUN
))) {
1338 assert(thread
->state
& TH_RUN
);
1339 processor
= thread
->chosen_processor
;
1340 cause_ast_check(processor
);
1343 thread
->wake_active
= TRUE
;
1344 thread_unlock(thread
);
1346 wresult
= assert_wait(&thread
->wake_active
, THREAD_UNINT
);
1347 wake_unlock(thread
);
1350 if (wresult
== THREAD_WAITING
) {
1351 thread_block(THREAD_CONTINUE_NULL
);
1356 thread_lock(thread
);
1359 thread_unlock(thread
);
1360 wake_unlock(thread
);
1365 * Routine: clear_wait_internal
1367 * Clear the wait condition for the specified thread.
1368 * Start the thread executing if that is appropriate.
1370 * thread thread to awaken
1371 * result Wakeup result the thread should see
1374 * the thread is locked.
1376 * KERN_SUCCESS thread was rousted out a wait
1377 * KERN_FAILURE thread was waiting but could not be rousted
1378 * KERN_NOT_WAITING thread was not waiting
1380 __private_extern__ kern_return_t
1381 clear_wait_internal(
1383 wait_result_t wresult
)
1385 uint32_t i
= LockTimeOutUsec
;
1386 struct waitq
*waitq
= thread
->waitq
;
1389 if (wresult
== THREAD_INTERRUPTED
&& (thread
->state
& TH_UNINT
)) {
1390 return KERN_FAILURE
;
1393 if (waitq
!= NULL
) {
1394 if (!waitq_pull_thread_locked(waitq
, thread
)) {
1395 thread_unlock(thread
);
1397 if (i
> 0 && !machine_timeout_suspended()) {
1400 thread_lock(thread
);
1401 if (waitq
!= thread
->waitq
) {
1402 return KERN_NOT_WAITING
;
1408 /* TODO: Can we instead assert TH_TERMINATE is not set? */
1409 if ((thread
->state
& (TH_WAIT
| TH_TERMINATE
)) == TH_WAIT
) {
1410 return thread_go(thread
, wresult
, WQ_OPTION_NONE
);
1412 return KERN_NOT_WAITING
;
1416 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
1417 thread
, waitq
, cpu_number());
1419 return KERN_FAILURE
;
1426 * Clear the wait condition for the specified thread. Start the thread
1427 * executing if that is appropriate.
1430 * thread thread to awaken
1431 * result Wakeup result the thread should see
1436 wait_result_t result
)
1442 thread_lock(thread
);
1443 ret
= clear_wait_internal(thread
, result
);
1444 thread_unlock(thread
);
1451 * thread_wakeup_prim:
1453 * Common routine for thread_wakeup, thread_wakeup_with_result,
1454 * and thread_wakeup_one.
1460 boolean_t one_thread
,
1461 wait_result_t result
)
1463 if (__improbable(event
== NO_EVENT
)) {
1464 panic("%s() called with NO_EVENT", __func__
);
1467 struct waitq
*wq
= global_eventq(event
);
1470 return waitq_wakeup64_one(wq
, CAST_EVENT64_T(event
), result
, WAITQ_ALL_PRIORITIES
);
1472 return waitq_wakeup64_all(wq
, CAST_EVENT64_T(event
), result
, WAITQ_ALL_PRIORITIES
);
1477 * Wakeup a specified thread if and only if it's waiting for this event
1480 thread_wakeup_thread(
1484 if (__improbable(event
== NO_EVENT
)) {
1485 panic("%s() called with NO_EVENT", __func__
);
1488 if (__improbable(thread
== THREAD_NULL
)) {
1489 panic("%s() called with THREAD_NULL", __func__
);
1492 struct waitq
*wq
= global_eventq(event
);
1494 return waitq_wakeup64_thread(wq
, CAST_EVENT64_T(event
), thread
, THREAD_AWAKENED
);
1498 * Wakeup a thread waiting on an event and promote it to a priority.
1500 * Requires woken thread to un-promote itself when done.
1503 thread_wakeup_one_with_pri(
1507 if (__improbable(event
== NO_EVENT
)) {
1508 panic("%s() called with NO_EVENT", __func__
);
1511 struct waitq
*wq
= global_eventq(event
);
1513 return waitq_wakeup64_one(wq
, CAST_EVENT64_T(event
), THREAD_AWAKENED
, priority
);
1517 * Wakeup a thread waiting on an event,
1518 * promote it to a priority,
1519 * and return a reference to the woken thread.
1521 * Requires woken thread to un-promote itself when done.
1524 thread_wakeup_identify(event_t event
,
1527 if (__improbable(event
== NO_EVENT
)) {
1528 panic("%s() called with NO_EVENT", __func__
);
1531 struct waitq
*wq
= global_eventq(event
);
1533 return waitq_wakeup64_identify(wq
, CAST_EVENT64_T(event
), THREAD_AWAKENED
, priority
);
1539 * Force the current thread to execute on the specified processor.
1540 * Takes effect after the next thread_block().
1542 * Returns the previous binding. PROCESSOR_NULL means
1545 * XXX - DO NOT export this to users - XXX
1549 processor_t processor
)
1551 thread_t self
= current_thread();
1558 prev
= thread_bind_internal(self
, processor
);
1560 thread_unlock(self
);
1567 * thread_bind_internal:
1569 * If the specified thread is not the current thread, and it is currently
1570 * running on another CPU, a remote AST must be sent to that CPU to cause
1571 * the thread to migrate to its bound processor. Otherwise, the migration
1572 * will occur at the next quantum expiration or blocking point.
1574 * When the thread is the current thread, and explicit thread_block() should
1575 * be used to force the current processor to context switch away and
1576 * let the thread migrate to the bound processor.
1578 * Thread must be locked, and at splsched.
1582 thread_bind_internal(
1584 processor_t processor
)
1588 /* <rdar://problem/15102234> */
1589 assert(thread
->sched_pri
< BASEPRI_RTQUEUES
);
1590 /* A thread can't be bound if it's sitting on a (potentially incorrect) runqueue */
1591 assert(thread
->runq
== PROCESSOR_NULL
);
1593 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_THREAD_BIND
), thread_tid(thread
), processor
? (uintptr_t)processor
->cpu_id
: (uintptr_t)-1, 0, 0, 0);
1595 prev
= thread
->bound_processor
;
1596 thread
->bound_processor
= processor
;
1602 * thread_vm_bind_group_add:
1604 * The "VM bind group" is a special mechanism to mark a collection
1605 * of threads from the VM subsystem that, in general, should be scheduled
1606 * with only one CPU of parallelism. To accomplish this, we initially
1607 * bind all the threads to the master processor, which has the effect
1608 * that only one of the threads in the group can execute at once, including
1609 * preempting threads in the group that are a lower priority. Future
1610 * mechanisms may use more dynamic mechanisms to prevent the collection
1611 * of VM threads from using more CPU time than desired.
1613 * The current implementation can result in priority inversions where
1614 * compute-bound priority 95 or realtime threads that happen to have
1615 * landed on the master processor prevent the VM threads from running.
1616 * When this situation is detected, we unbind the threads for one
1617 * scheduler tick to allow the scheduler to run the threads an
1618 * additional CPUs, before restoring the binding (assuming high latency
1619 * is no longer a problem).
1623 * The current max is provisioned for:
1624 * vm_compressor_swap_trigger_thread (92)
1625 * 2 x vm_pageout_iothread_internal (92) when vm_restricted_to_single_processor==TRUE
1626 * vm_pageout_continue (92)
1627 * memorystatus_thread (95)
1629 #define MAX_VM_BIND_GROUP_COUNT (5)
1630 decl_simple_lock_data(static, sched_vm_group_list_lock
);
1631 static thread_t sched_vm_group_thread_list
[MAX_VM_BIND_GROUP_COUNT
];
1632 static int sched_vm_group_thread_count
;
1633 static boolean_t sched_vm_group_temporarily_unbound
= FALSE
;
1636 thread_vm_bind_group_add(void)
1638 thread_t self
= current_thread();
1640 thread_reference_internal(self
);
1641 self
->options
|= TH_OPT_SCHED_VM_GROUP
;
1643 simple_lock(&sched_vm_group_list_lock
, LCK_GRP_NULL
);
1644 assert(sched_vm_group_thread_count
< MAX_VM_BIND_GROUP_COUNT
);
1645 sched_vm_group_thread_list
[sched_vm_group_thread_count
++] = self
;
1646 simple_unlock(&sched_vm_group_list_lock
);
1648 thread_bind(master_processor
);
1650 /* Switch to bound processor if not already there */
1651 thread_block(THREAD_CONTINUE_NULL
);
1655 sched_vm_group_maintenance(void)
1657 uint64_t ctime
= mach_absolute_time();
1658 uint64_t longtime
= ctime
- sched_tick_interval
;
1661 boolean_t high_latency_observed
= FALSE
;
1662 boolean_t runnable_and_not_on_runq_observed
= FALSE
;
1663 boolean_t bind_target_changed
= FALSE
;
1664 processor_t bind_target
= PROCESSOR_NULL
;
1666 /* Make sure nobody attempts to add new threads while we are enumerating them */
1667 simple_lock(&sched_vm_group_list_lock
, LCK_GRP_NULL
);
1671 for (i
= 0; i
< sched_vm_group_thread_count
; i
++) {
1672 thread_t thread
= sched_vm_group_thread_list
[i
];
1673 assert(thread
!= THREAD_NULL
);
1674 thread_lock(thread
);
1675 if ((thread
->state
& (TH_RUN
| TH_WAIT
)) == TH_RUN
) {
1676 if (thread
->runq
!= PROCESSOR_NULL
&& thread
->last_made_runnable_time
< longtime
) {
1677 high_latency_observed
= TRUE
;
1678 } else if (thread
->runq
== PROCESSOR_NULL
) {
1679 /* There are some cases where a thread be transitiong that also fall into this case */
1680 runnable_and_not_on_runq_observed
= TRUE
;
1683 thread_unlock(thread
);
1685 if (high_latency_observed
&& runnable_and_not_on_runq_observed
) {
1686 /* All the things we are looking for are true, stop looking */
1693 if (sched_vm_group_temporarily_unbound
) {
1694 /* If we turned off binding, make sure everything is OK before rebinding */
1695 if (!high_latency_observed
) {
1697 bind_target_changed
= TRUE
;
1698 bind_target
= master_processor
;
1699 sched_vm_group_temporarily_unbound
= FALSE
; /* might be reset to TRUE if change cannot be completed */
1703 * Check if we're in a bad state, which is defined by high
1704 * latency with no core currently executing a thread. If a
1705 * single thread is making progress on a CPU, that means the
1706 * binding concept to reduce parallelism is working as
1709 if (high_latency_observed
&& !runnable_and_not_on_runq_observed
) {
1711 bind_target_changed
= TRUE
;
1712 bind_target
= PROCESSOR_NULL
;
1713 sched_vm_group_temporarily_unbound
= TRUE
;
1717 if (bind_target_changed
) {
1719 for (i
= 0; i
< sched_vm_group_thread_count
; i
++) {
1720 thread_t thread
= sched_vm_group_thread_list
[i
];
1722 assert(thread
!= THREAD_NULL
);
1724 thread_lock(thread
);
1725 removed
= thread_run_queue_remove(thread
);
1726 if (removed
|| ((thread
->state
& (TH_RUN
| TH_WAIT
)) == TH_WAIT
)) {
1727 thread_bind_internal(thread
, bind_target
);
1730 * Thread was in the middle of being context-switched-to,
1731 * or was in the process of blocking. To avoid switching the bind
1732 * state out mid-flight, defer the change if possible.
1734 if (bind_target
== PROCESSOR_NULL
) {
1735 thread_bind_internal(thread
, bind_target
);
1737 sched_vm_group_temporarily_unbound
= TRUE
; /* next pass will try again */
1742 thread_run_queue_reinsert(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
1744 thread_unlock(thread
);
1749 simple_unlock(&sched_vm_group_list_lock
);
1752 /* Invoked prior to idle entry to determine if, on SMT capable processors, an SMT
1753 * rebalancing opportunity exists when a core is (instantaneously) idle, but
1754 * other SMT-capable cores may be over-committed. TODO: some possible negatives:
1755 * IPI thrash if this core does not remain idle following the load balancing ASTs
1756 * Idle "thrash", when IPI issue is followed by idle entry/core power down
1757 * followed by a wakeup shortly thereafter.
1760 #if (DEVELOPMENT || DEBUG)
1761 int sched_smt_balance
= 1;
1764 /* Invoked with pset locked, returns with pset unlocked */
1766 sched_SMT_balance(processor_t cprocessor
, processor_set_t cpset
)
1768 processor_t ast_processor
= NULL
;
1770 #if (DEVELOPMENT || DEBUG)
1771 if (__improbable(sched_smt_balance
== 0)) {
1772 goto smt_balance_exit
;
1776 assert(cprocessor
== current_processor());
1777 if (cprocessor
->is_SMT
== FALSE
) {
1778 goto smt_balance_exit
;
1781 processor_t sib_processor
= cprocessor
->processor_secondary
? cprocessor
->processor_secondary
: cprocessor
->processor_primary
;
1783 /* Determine if both this processor and its sibling are idle,
1784 * indicating an SMT rebalancing opportunity.
1786 if (sib_processor
->state
!= PROCESSOR_IDLE
) {
1787 goto smt_balance_exit
;
1790 processor_t sprocessor
;
1792 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
1793 uint64_t running_secondary_map
= (cpset
->cpu_state_map
[PROCESSOR_RUNNING
] &
1794 ~cpset
->primary_map
);
1795 for (int cpuid
= lsb_first(running_secondary_map
); cpuid
>= 0; cpuid
= lsb_next(running_secondary_map
, cpuid
)) {
1796 sprocessor
= processor_array
[cpuid
];
1797 if ((sprocessor
->processor_primary
->state
== PROCESSOR_RUNNING
) &&
1798 (sprocessor
->current_pri
< BASEPRI_RTQUEUES
)) {
1799 ipi_type
= sched_ipi_action(sprocessor
, NULL
, false, SCHED_IPI_EVENT_SMT_REBAL
);
1800 if (ipi_type
!= SCHED_IPI_NONE
) {
1801 assert(sprocessor
!= cprocessor
);
1802 ast_processor
= sprocessor
;
1811 if (ast_processor
) {
1812 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_SMT_BALANCE
), ast_processor
->cpu_id
, ast_processor
->state
, ast_processor
->processor_primary
->state
, 0, 0);
1813 sched_ipi_perform(ast_processor
, ipi_type
);
1818 pset_available_cpumap(processor_set_t pset
)
1820 return (pset
->cpu_state_map
[PROCESSOR_IDLE
] | pset
->cpu_state_map
[PROCESSOR_DISPATCHING
] | pset
->cpu_state_map
[PROCESSOR_RUNNING
]) &
1821 pset
->recommended_bitmask
;
1825 pset_available_but_not_running_cpumap(processor_set_t pset
)
1827 return (pset
->cpu_state_map
[PROCESSOR_IDLE
] | pset
->cpu_state_map
[PROCESSOR_DISPATCHING
]) &
1828 pset
->recommended_bitmask
;
1832 pset_has_stealable_threads(processor_set_t pset
)
1834 pset_assert_locked(pset
);
1836 cpumap_t avail_map
= pset_available_but_not_running_cpumap(pset
);
1838 * Secondary CPUs never steal, so allow stealing of threads if there are more threads than
1839 * available primary CPUs
1841 avail_map
&= pset
->primary_map
;
1843 return (pset
->pset_runq
.count
> 0) && ((pset
->pset_runq
.count
+ rt_runq_count(pset
)) > bit_count(avail_map
));
1847 * Called with pset locked, on a processor that is committing to run a new thread
1848 * Will transition an idle or dispatching processor to running as it picks up
1849 * the first new thread from the idle thread.
1852 pset_commit_processor_to_new_thread(processor_set_t pset
, processor_t processor
, thread_t new_thread
)
1854 pset_assert_locked(pset
);
1856 if (processor
->state
== PROCESSOR_DISPATCHING
|| processor
->state
== PROCESSOR_IDLE
) {
1857 assert(current_thread() == processor
->idle_thread
);
1860 * Dispatching processor is now committed to running new_thread,
1861 * so change its state to PROCESSOR_RUNNING.
1863 pset_update_processor_state(pset
, processor
, PROCESSOR_RUNNING
);
1865 assert((processor
->state
== PROCESSOR_RUNNING
) || (processor
->state
== PROCESSOR_SHUTDOWN
));
1868 processor_state_update_from_thread(processor
, new_thread
);
1870 if (new_thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
1871 bit_set(pset
->realtime_map
, processor
->cpu_id
);
1873 bit_clear(pset
->realtime_map
, processor
->cpu_id
);
1876 pset_node_t node
= pset
->node
;
1878 if (bit_count(node
->pset_map
) == 1) {
1879 /* Node has only a single pset, so skip node pset map updates */
1883 cpumap_t avail_map
= pset_available_cpumap(pset
);
1885 if (new_thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
1886 if ((avail_map
& pset
->realtime_map
) == avail_map
) {
1887 /* No more non-RT CPUs in this pset */
1888 atomic_bit_clear(&node
->pset_non_rt_map
, pset
->pset_id
, memory_order_relaxed
);
1890 avail_map
&= pset
->primary_map
;
1891 if ((avail_map
& pset
->realtime_map
) == avail_map
) {
1892 /* No more non-RT primary CPUs in this pset */
1893 atomic_bit_clear(&node
->pset_non_rt_primary_map
, pset
->pset_id
, memory_order_relaxed
);
1896 if ((avail_map
& pset
->realtime_map
) != avail_map
) {
1897 if (!bit_test(atomic_load(&node
->pset_non_rt_map
), pset
->pset_id
)) {
1898 atomic_bit_set(&node
->pset_non_rt_map
, pset
->pset_id
, memory_order_relaxed
);
1901 avail_map
&= pset
->primary_map
;
1902 if ((avail_map
& pset
->realtime_map
) != avail_map
) {
1903 if (!bit_test(atomic_load(&node
->pset_non_rt_primary_map
), pset
->pset_id
)) {
1904 atomic_bit_set(&node
->pset_non_rt_primary_map
, pset
->pset_id
, memory_order_relaxed
);
1910 static processor_t
choose_processor_for_realtime_thread(processor_set_t pset
, processor_t skip_processor
, bool consider_secondaries
);
1911 static bool all_available_primaries_are_running_realtime_threads(processor_set_t pset
);
1912 #if defined(__x86_64__)
1913 static bool these_processors_are_running_realtime_threads(processor_set_t pset
, uint64_t these_map
);
1915 static bool sched_ok_to_run_realtime_thread(processor_set_t pset
, processor_t processor
);
1916 static bool processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset
, processor_t processor
);
1917 int sched_allow_rt_smt
= 1;
1918 int sched_avoid_cpu0
= 1;
1923 * Select a new thread for the current processor to execute.
1925 * May select the current thread, which must be locked.
1928 thread_select(thread_t thread
,
1929 processor_t processor
,
1932 processor_set_t pset
= processor
->processor_set
;
1933 thread_t new_thread
= THREAD_NULL
;
1935 assert(processor
== current_processor());
1936 assert((thread
->state
& (TH_RUN
| TH_TERMINATE2
)) == TH_RUN
);
1940 * Update the priority.
1942 if (SCHED(can_update_priority
)(thread
)) {
1943 SCHED(update_priority
)(thread
);
1948 processor_state_update_from_thread(processor
, thread
);
1951 /* Acknowledge any pending IPIs here with pset lock held */
1952 bit_clear(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
1953 bit_clear(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
1955 #if defined(CONFIG_SCHED_DEFERRED_AST)
1956 bit_clear(pset
->pending_deferred_AST_cpu_mask
, processor
->cpu_id
);
1959 bool secondary_can_only_run_realtime_thread
= false;
1961 assert(processor
->state
!= PROCESSOR_OFF_LINE
);
1963 if (!processor
->is_recommended
) {
1965 * The performance controller has provided a hint to not dispatch more threads,
1966 * unless they are bound to us (and thus we are the only option
1968 if (!SCHED(processor_bound_count
)(processor
)) {
1971 } else if (processor
->processor_primary
!= processor
) {
1973 * Should this secondary SMT processor attempt to find work? For pset runqueue systems,
1974 * we should look for work only under the same conditions that choose_processor()
1975 * would have assigned work, which is when all primary processors have been assigned work.
1977 * An exception is that bound threads are dispatched to a processor without going through
1978 * choose_processor(), so in those cases we should continue trying to dequeue work.
1980 if (!SCHED(processor_bound_count
)(processor
)) {
1981 if ((pset
->recommended_bitmask
& pset
->primary_map
& pset
->cpu_state_map
[PROCESSOR_IDLE
]) != 0) {
1986 * TODO: What if a secondary core beat an idle primary to waking up from an IPI?
1987 * Should it dequeue immediately, or spin waiting for the primary to wake up?
1990 /* There are no idle primaries */
1992 if (processor
->processor_primary
->current_pri
>= BASEPRI_RTQUEUES
) {
1993 bool secondary_can_run_realtime_thread
= sched_allow_rt_smt
&& rt_runq_count(pset
) && all_available_primaries_are_running_realtime_threads(pset
);
1994 if (!secondary_can_run_realtime_thread
) {
1997 secondary_can_only_run_realtime_thread
= true;
2003 * Test to see if the current thread should continue
2004 * to run on this processor. Must not be attempting to wait, and not
2005 * bound to a different processor, nor be in the wrong
2006 * processor set, nor be forced to context switch by TH_SUSP.
2008 * Note that there are never any RT threads in the regular runqueue.
2010 * This code is very insanely tricky.
2013 /* i.e. not waiting, not TH_SUSP'ed */
2014 bool still_running
= ((thread
->state
& (TH_TERMINATE
| TH_IDLE
| TH_WAIT
| TH_RUN
| TH_SUSP
)) == TH_RUN
);
2017 * Threads running on SMT processors are forced to context switch. Don't rebalance realtime threads.
2018 * TODO: This should check if it's worth it to rebalance, i.e. 'are there any idle primary processors'
2019 * <rdar://problem/47907700>
2021 * A yielding thread shouldn't be forced to context switch.
2024 bool is_yielding
= (*reason
& AST_YIELD
) == AST_YIELD
;
2026 bool needs_smt_rebalance
= !is_yielding
&& thread
->sched_pri
< BASEPRI_RTQUEUES
&& processor
->processor_primary
!= processor
;
2028 bool affinity_mismatch
= thread
->affinity_set
!= AFFINITY_SET_NULL
&& thread
->affinity_set
->aset_pset
!= pset
;
2030 bool bound_elsewhere
= thread
->bound_processor
!= PROCESSOR_NULL
&& thread
->bound_processor
!= processor
;
2032 bool avoid_processor
= !is_yielding
&& SCHED(avoid_processor_enabled
) && SCHED(thread_avoid_processor
)(processor
, thread
);
2034 if (still_running
&& !needs_smt_rebalance
&& !affinity_mismatch
&& !bound_elsewhere
&& !avoid_processor
) {
2036 * This thread is eligible to keep running on this processor.
2038 * RT threads with un-expired quantum stay on processor,
2039 * unless there's a valid RT thread with an earlier deadline.
2041 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
&& processor
->first_timeslice
) {
2042 if (rt_runq_count(pset
) > 0) {
2043 thread_t next_rt
= qe_queue_first(&SCHED(rt_runq
)(pset
)->queue
, struct thread
, runq_links
);
2045 if (next_rt
->realtime
.deadline
< processor
->deadline
&&
2046 (next_rt
->bound_processor
== PROCESSOR_NULL
||
2047 next_rt
->bound_processor
== processor
)) {
2048 /* The next RT thread is better, so pick it off the runqueue. */
2049 goto pick_new_rt_thread
;
2053 /* This is still the best RT thread to run. */
2054 processor
->deadline
= thread
->realtime
.deadline
;
2056 sched_update_pset_load_average(pset
, 0);
2058 processor_t next_rt_processor
= PROCESSOR_NULL
;
2059 sched_ipi_type_t next_rt_ipi_type
= SCHED_IPI_NONE
;
2061 if (rt_runq_count(pset
) - bit_count(pset
->pending_AST_URGENT_cpu_mask
) > 0) {
2062 next_rt_processor
= choose_processor_for_realtime_thread(pset
, processor
, true);
2063 if (next_rt_processor
) {
2064 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHOOSE_PROCESSOR
) | DBG_FUNC_NONE
,
2065 (uintptr_t)0, (uintptr_t)-4, next_rt_processor
->cpu_id
, next_rt_processor
->state
, 0);
2066 if (next_rt_processor
->state
== PROCESSOR_IDLE
) {
2067 pset_update_processor_state(pset
, next_rt_processor
, PROCESSOR_DISPATCHING
);
2069 next_rt_ipi_type
= sched_ipi_action(next_rt_processor
, NULL
, false, SCHED_IPI_EVENT_PREEMPT
);
2074 if (next_rt_processor
) {
2075 sched_ipi_perform(next_rt_processor
, next_rt_ipi_type
);
2081 if ((rt_runq_count(pset
) == 0) &&
2082 SCHED(processor_queue_has_priority
)(processor
, thread
->sched_pri
, TRUE
) == FALSE
) {
2083 /* This thread is still the highest priority runnable (non-idle) thread */
2084 processor
->deadline
= UINT64_MAX
;
2086 sched_update_pset_load_average(pset
, 0);
2093 * This processor must context switch.
2094 * If it's due to a rebalance, we should aggressively find this thread a new home.
2096 if (needs_smt_rebalance
|| affinity_mismatch
|| bound_elsewhere
|| avoid_processor
) {
2097 *reason
|= AST_REBALANCE
;
2101 /* OK, so we're not going to run the current thread. Look at the RT queue. */
2102 bool ok_to_run_realtime_thread
= sched_ok_to_run_realtime_thread(pset
, processor
);
2103 if ((rt_runq_count(pset
) > 0) && ok_to_run_realtime_thread
) {
2104 thread_t next_rt
= qe_queue_first(&SCHED(rt_runq
)(pset
)->queue
, struct thread
, runq_links
);
2106 if (__probable((next_rt
->bound_processor
== PROCESSOR_NULL
||
2107 (next_rt
->bound_processor
== processor
)))) {
2109 new_thread
= qe_dequeue_head(&SCHED(rt_runq
)(pset
)->queue
, struct thread
, runq_links
);
2111 new_thread
->runq
= PROCESSOR_NULL
;
2112 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq
)(pset
)->runq_stats
, rt_runq_count(pset
));
2113 rt_runq_count_decr(pset
);
2115 processor
->deadline
= new_thread
->realtime
.deadline
;
2117 pset_commit_processor_to_new_thread(pset
, processor
, new_thread
);
2119 sched_update_pset_load_average(pset
, 0);
2121 processor_t ast_processor
= PROCESSOR_NULL
;
2122 processor_t next_rt_processor
= PROCESSOR_NULL
;
2123 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
2124 sched_ipi_type_t next_rt_ipi_type
= SCHED_IPI_NONE
;
2126 if (processor
->processor_secondary
!= NULL
) {
2127 processor_t sprocessor
= processor
->processor_secondary
;
2128 if ((sprocessor
->state
== PROCESSOR_RUNNING
) || (sprocessor
->state
== PROCESSOR_DISPATCHING
)) {
2129 ipi_type
= sched_ipi_action(sprocessor
, NULL
, false, SCHED_IPI_EVENT_SMT_REBAL
);
2130 ast_processor
= sprocessor
;
2133 if (rt_runq_count(pset
) - bit_count(pset
->pending_AST_URGENT_cpu_mask
) > 0) {
2134 next_rt_processor
= choose_processor_for_realtime_thread(pset
, processor
, true);
2135 if (next_rt_processor
) {
2136 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHOOSE_PROCESSOR
) | DBG_FUNC_NONE
,
2137 (uintptr_t)0, (uintptr_t)-5, next_rt_processor
->cpu_id
, next_rt_processor
->state
, 0);
2138 if (next_rt_processor
->state
== PROCESSOR_IDLE
) {
2139 pset_update_processor_state(pset
, next_rt_processor
, PROCESSOR_DISPATCHING
);
2141 next_rt_ipi_type
= sched_ipi_action(next_rt_processor
, NULL
, false, SCHED_IPI_EVENT_PREEMPT
);
2146 if (ast_processor
) {
2147 sched_ipi_perform(ast_processor
, ipi_type
);
2150 if (next_rt_processor
) {
2151 sched_ipi_perform(next_rt_processor
, next_rt_ipi_type
);
2157 if (secondary_can_only_run_realtime_thread
) {
2161 processor
->deadline
= UINT64_MAX
;
2163 /* No RT threads, so let's look at the regular threads. */
2164 if ((new_thread
= SCHED(choose_thread
)(processor
, MINPRI
, *reason
)) != THREAD_NULL
) {
2165 pset_commit_processor_to_new_thread(pset
, processor
, new_thread
);
2166 sched_update_pset_load_average(pset
, 0);
2168 processor_t ast_processor
= PROCESSOR_NULL
;
2169 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
2171 processor_t sprocessor
= processor
->processor_secondary
;
2172 if ((sprocessor
!= NULL
) && (sprocessor
->state
== PROCESSOR_RUNNING
)) {
2173 if (thread_no_smt(new_thread
)) {
2174 ipi_type
= sched_ipi_action(sprocessor
, NULL
, false, SCHED_IPI_EVENT_SMT_REBAL
);
2175 ast_processor
= sprocessor
;
2180 if (ast_processor
) {
2181 sched_ipi_perform(ast_processor
, ipi_type
);
2186 if (processor
->must_idle
) {
2187 processor
->must_idle
= false;
2191 if (SCHED(steal_thread_enabled
)(pset
) && (processor
->processor_primary
== processor
)) {
2193 * No runnable threads, attempt to steal
2194 * from other processors. Returns with pset lock dropped.
2197 if ((new_thread
= SCHED(steal_thread
)(pset
)) != THREAD_NULL
) {
2199 * Avoid taking the pset_lock unless it is necessary to change state.
2200 * It's safe to read processor->state here, as only the current processor can change state
2201 * from this point (interrupts are disabled and this processor is committed to run new_thread).
2203 if (processor
->state
== PROCESSOR_DISPATCHING
|| processor
->state
== PROCESSOR_IDLE
) {
2205 pset_commit_processor_to_new_thread(pset
, processor
, new_thread
);
2208 assert((processor
->state
== PROCESSOR_RUNNING
) || (processor
->state
== PROCESSOR_SHUTDOWN
));
2209 processor_state_update_from_thread(processor
, new_thread
);
2216 * If other threads have appeared, shortcut
2219 if (!SCHED(processor_queue_empty
)(processor
) || (ok_to_run_realtime_thread
&& (rt_runq_count(pset
) > 0))) {
2225 /* Someone selected this processor while we had dropped the lock */
2226 if (bit_test(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
)) {
2233 * Nothing is runnable, so set this processor idle if it
2236 if ((processor
->state
== PROCESSOR_RUNNING
) || (processor
->state
== PROCESSOR_DISPATCHING
)) {
2237 pset_update_processor_state(pset
, processor
, PROCESSOR_IDLE
);
2238 processor_state_update_idle(processor
);
2241 /* Invoked with pset locked, returns with pset unlocked */
2242 SCHED(processor_balance
)(processor
, pset
);
2244 new_thread
= processor
->idle_thread
;
2245 } while (new_thread
== THREAD_NULL
);
2253 * Called at splsched with neither thread locked.
2255 * Perform a context switch and start executing the new thread.
2257 * Returns FALSE when the context switch didn't happen.
2258 * The reference to the new thread is still consumed.
2260 * "self" is what is currently running on the processor,
2261 * "thread" is the new thread to context switch to
2262 * (which may be the same thread in some cases)
2270 if (__improbable(get_preemption_level() != 0)) {
2271 int pl
= get_preemption_level();
2272 panic("thread_invoke: preemption_level %d, possible cause: %s",
2273 pl
, (pl
< 0 ? "unlocking an unlocked mutex or spinlock" :
2274 "blocking while holding a spinlock, or within interrupt context"));
2277 thread_continue_t continuation
= self
->continuation
;
2278 void *parameter
= self
->parameter
;
2279 processor_t processor
;
2281 uint64_t ctime
= mach_absolute_time();
2283 #ifdef CONFIG_MACH_APPROXIMATE_TIME
2284 commpage_update_mach_approximate_time(ctime
);
2287 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
2288 if (!((thread
->state
& TH_IDLE
) != 0 ||
2289 ((reason
& AST_HANDOFF
) && self
->sched_mode
== TH_MODE_REALTIME
))) {
2290 sched_timeshare_consider_maintenance(ctime
);
2295 mt_sched_update(self
);
2296 #endif /* MONOTONIC */
2298 assert_thread_magic(self
);
2299 assert(self
== current_thread());
2300 assert(self
->runq
== PROCESSOR_NULL
);
2301 assert((self
->state
& (TH_RUN
| TH_TERMINATE2
)) == TH_RUN
);
2303 thread_lock(thread
);
2305 assert_thread_magic(thread
);
2306 assert((thread
->state
& (TH_RUN
| TH_WAIT
| TH_UNINT
| TH_TERMINATE
| TH_TERMINATE2
)) == TH_RUN
);
2307 assert(thread
->bound_processor
== PROCESSOR_NULL
|| thread
->bound_processor
== current_processor());
2308 assert(thread
->runq
== PROCESSOR_NULL
);
2310 /* Reload precise timing global policy to thread-local policy */
2311 thread
->precise_user_kernel_time
= use_precise_user_kernel_time(thread
);
2313 /* Update SFI class based on other factors */
2314 thread
->sfi_class
= sfi_thread_classify(thread
);
2316 /* Update the same_pri_latency for the thread (used by perfcontrol callouts) */
2317 thread
->same_pri_latency
= ctime
- thread
->last_basepri_change_time
;
2319 * In case a base_pri update happened between the timestamp and
2320 * taking the thread lock
2322 if (ctime
<= thread
->last_basepri_change_time
) {
2323 thread
->same_pri_latency
= ctime
- thread
->last_made_runnable_time
;
2326 /* Allow realtime threads to hang onto a stack. */
2327 if ((self
->sched_mode
== TH_MODE_REALTIME
) && !self
->reserved_stack
) {
2328 self
->reserved_stack
= self
->kernel_stack
;
2331 /* Prepare for spin debugging */
2332 #if INTERRUPT_MASKED_DEBUG
2333 ml_spin_debug_clear(thread
);
2336 if (continuation
!= NULL
) {
2337 if (!thread
->kernel_stack
) {
2339 * If we are using a privileged stack,
2340 * check to see whether we can exchange it with
2341 * that of the other thread.
2343 if (self
->kernel_stack
== self
->reserved_stack
&& !thread
->reserved_stack
) {
2348 * Context switch by performing a stack handoff.
2349 * Requires both threads to be parked in a continuation.
2351 continuation
= thread
->continuation
;
2352 parameter
= thread
->parameter
;
2354 processor
= current_processor();
2355 processor
->active_thread
= thread
;
2356 processor_state_update_from_thread(processor
, thread
);
2358 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
2359 if (thread
->last_processor
->processor_set
!= processor
->processor_set
) {
2360 thread
->ps_switch
++;
2364 thread
->last_processor
= processor
;
2366 ast_context(thread
);
2368 thread_unlock(thread
);
2370 self
->reason
= reason
;
2372 processor
->last_dispatch
= ctime
;
2373 self
->last_run_time
= ctime
;
2374 processor_timer_switch_thread(ctime
, &thread
->system_timer
);
2375 timer_update(&thread
->runnable_timer
, ctime
);
2376 processor
->kernel_timer
= &thread
->system_timer
;
2379 * Since non-precise user/kernel time doesn't update the state timer
2380 * during privilege transitions, synthesize an event now.
2382 if (!thread
->precise_user_kernel_time
) {
2383 timer_update(processor
->current_state
, ctime
);
2386 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2387 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_HANDOFF
) | DBG_FUNC_NONE
,
2388 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
2390 if ((thread
->chosen_processor
!= processor
) && (thread
->chosen_processor
!= PROCESSOR_NULL
)) {
2391 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_MOVED
) | DBG_FUNC_NONE
,
2392 (uintptr_t)thread_tid(thread
), (uintptr_t)thread
->chosen_processor
->cpu_id
, 0, 0, 0);
2395 DTRACE_SCHED2(off__cpu
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
2397 SCHED_STATS_CSW(processor
, self
->reason
, self
->sched_pri
, thread
->sched_pri
);
2400 kperf_off_cpu(self
);
2404 * This is where we actually switch thread identity,
2405 * and address space if required. However, register
2406 * state is not switched - this routine leaves the
2407 * stack and register state active on the current CPU.
2409 TLOG(1, "thread_invoke: calling stack_handoff\n");
2410 stack_handoff(self
, thread
);
2412 /* 'self' is now off core */
2413 assert(thread
== current_thread_volatile());
2415 DTRACE_SCHED(on__cpu
);
2418 kperf_on_cpu(thread
, continuation
, NULL
);
2421 thread_dispatch(self
, thread
);
2424 /* Old thread's stack has been moved to the new thread, so explicitly
2426 kasan_unpoison_stack(thread
->kernel_stack
, kernel_stack_size
);
2429 thread
->continuation
= thread
->parameter
= NULL
;
2431 counter(c_thread_invoke_hits
++);
2433 boolean_t enable_interrupts
= TRUE
;
2435 /* idle thread needs to stay interrupts-disabled */
2436 if ((thread
->state
& TH_IDLE
)) {
2437 enable_interrupts
= FALSE
;
2440 assert(continuation
);
2441 call_continuation(continuation
, parameter
,
2442 thread
->wait_result
, enable_interrupts
);
2444 } else if (thread
== self
) {
2445 /* same thread but with continuation */
2447 counter(++c_thread_invoke_same
);
2449 thread_unlock(self
);
2452 kperf_on_cpu(thread
, continuation
, NULL
);
2455 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2456 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED
) | DBG_FUNC_NONE
,
2457 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
2460 /* stack handoff to self - no thread_dispatch(), so clear the stack
2461 * and free the fakestack directly */
2462 kasan_fakestack_drop(self
);
2463 kasan_fakestack_gc(self
);
2464 kasan_unpoison_stack(self
->kernel_stack
, kernel_stack_size
);
2467 self
->continuation
= self
->parameter
= NULL
;
2469 boolean_t enable_interrupts
= TRUE
;
2471 /* idle thread needs to stay interrupts-disabled */
2472 if ((self
->state
& TH_IDLE
)) {
2473 enable_interrupts
= FALSE
;
2476 call_continuation(continuation
, parameter
,
2477 self
->wait_result
, enable_interrupts
);
2482 * Check that the other thread has a stack
2484 if (!thread
->kernel_stack
) {
2486 if (!stack_alloc_try(thread
)) {
2487 counter(c_thread_invoke_misses
++);
2488 thread_unlock(thread
);
2489 thread_stack_enqueue(thread
);
2492 } else if (thread
== self
) {
2494 counter(++c_thread_invoke_same
);
2495 thread_unlock(self
);
2497 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2498 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED
) | DBG_FUNC_NONE
,
2499 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
2506 * Context switch by full context save.
2508 processor
= current_processor();
2509 processor
->active_thread
= thread
;
2510 processor_state_update_from_thread(processor
, thread
);
2512 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
2513 if (thread
->last_processor
->processor_set
!= processor
->processor_set
) {
2514 thread
->ps_switch
++;
2518 thread
->last_processor
= processor
;
2520 ast_context(thread
);
2522 thread_unlock(thread
);
2524 counter(c_thread_invoke_csw
++);
2526 self
->reason
= reason
;
2528 processor
->last_dispatch
= ctime
;
2529 self
->last_run_time
= ctime
;
2530 processor_timer_switch_thread(ctime
, &thread
->system_timer
);
2531 timer_update(&thread
->runnable_timer
, ctime
);
2532 processor
->kernel_timer
= &thread
->system_timer
;
2535 * Since non-precise user/kernel time doesn't update the state timer
2536 * during privilege transitions, synthesize an event now.
2538 if (!thread
->precise_user_kernel_time
) {
2539 timer_update(processor
->current_state
, ctime
);
2542 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2543 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED
) | DBG_FUNC_NONE
,
2544 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
2546 if ((thread
->chosen_processor
!= processor
) && (thread
->chosen_processor
!= NULL
)) {
2547 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_MOVED
) | DBG_FUNC_NONE
,
2548 (uintptr_t)thread_tid(thread
), (uintptr_t)thread
->chosen_processor
->cpu_id
, 0, 0, 0);
2551 DTRACE_SCHED2(off__cpu
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
2553 SCHED_STATS_CSW(processor
, self
->reason
, self
->sched_pri
, thread
->sched_pri
);
2556 kperf_off_cpu(self
);
2560 * This is where we actually switch register context,
2561 * and address space if required. We will next run
2562 * as a result of a subsequent context switch.
2564 * Once registers are switched and the processor is running "thread",
2565 * the stack variables and non-volatile registers will contain whatever
2566 * was there the last time that thread blocked. No local variables should
2567 * be used after this point, except for the special case of "thread", which
2568 * the platform layer returns as the previous thread running on the processor
2569 * via the function call ABI as a return register, and "self", which may have
2570 * been stored on the stack or a non-volatile register, but a stale idea of
2571 * what was on the CPU is newly-accurate because that thread is again
2572 * running on the CPU.
2574 * If one of the threads is using a continuation, thread_continue
2575 * is used to stitch up its context.
2577 * If we are invoking a thread which is resuming from a continuation,
2578 * the CPU will invoke thread_continue next.
2580 * If the current thread is parking in a continuation, then its state
2581 * won't be saved and the stack will be discarded. When the stack is
2582 * re-allocated, it will be configured to resume from thread_continue.
2584 assert(continuation
== self
->continuation
);
2585 thread
= machine_switch_context(self
, continuation
, thread
);
2586 assert(self
== current_thread_volatile());
2587 TLOG(1, "thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self
, continuation
, thread
);
2589 assert(continuation
== NULL
&& self
->continuation
== NULL
);
2591 DTRACE_SCHED(on__cpu
);
2594 kperf_on_cpu(self
, NULL
, __builtin_frame_address(0));
2597 /* We have been resumed and are set to run. */
2598 thread_dispatch(thread
, self
);
2603 #if defined(CONFIG_SCHED_DEFERRED_AST)
2605 * pset_cancel_deferred_dispatch:
2607 * Cancels all ASTs that we can cancel for the given processor set
2608 * if the current processor is running the last runnable thread in the
2611 * This function assumes the current thread is runnable. This must
2612 * be called with the pset unlocked.
2615 pset_cancel_deferred_dispatch(
2616 processor_set_t pset
,
2617 processor_t processor
)
2619 processor_t active_processor
= NULL
;
2620 uint32_t sampled_sched_run_count
;
2623 sampled_sched_run_count
= os_atomic_load(&sched_run_buckets
[TH_BUCKET_RUN
], relaxed
);
2626 * If we have emptied the run queue, and our current thread is runnable, we
2627 * should tell any processors that are still DISPATCHING that they will
2628 * probably not have any work to do. In the event that there are no
2629 * pending signals that we can cancel, this is also uninteresting.
2631 * In the unlikely event that another thread becomes runnable while we are
2632 * doing this (sched_run_count is atomically updated, not guarded), the
2633 * codepath making it runnable SHOULD (a dangerous word) need the pset lock
2634 * in order to dispatch it to a processor in our pset. So, the other
2635 * codepath will wait while we squash all cancelable ASTs, get the pset
2636 * lock, and then dispatch the freshly runnable thread. So this should be
2637 * correct (we won't accidentally have a runnable thread that hasn't been
2638 * dispatched to an idle processor), if not ideal (we may be restarting the
2639 * dispatch process, which could have some overhead).
2642 if ((sampled_sched_run_count
== 1) && (pset
->pending_deferred_AST_cpu_mask
)) {
2643 uint64_t dispatching_map
= (pset
->cpu_state_map
[PROCESSOR_DISPATCHING
] &
2644 pset
->pending_deferred_AST_cpu_mask
&
2645 ~pset
->pending_AST_URGENT_cpu_mask
);
2646 for (int cpuid
= lsb_first(dispatching_map
); cpuid
>= 0; cpuid
= lsb_next(dispatching_map
, cpuid
)) {
2647 active_processor
= processor_array
[cpuid
];
2649 * If a processor is DISPATCHING, it could be because of
2650 * a cancelable signal.
2652 * IF the processor is not our
2653 * current processor (the current processor should not
2654 * be DISPATCHING, so this is a bit paranoid), AND there
2655 * is a cancelable signal pending on the processor, AND
2656 * there is no non-cancelable signal pending (as there is
2657 * no point trying to backtrack on bringing the processor
2658 * up if a signal we cannot cancel is outstanding), THEN
2659 * it should make sense to roll back the processor state
2660 * to the IDLE state.
2662 * If the racey nature of this approach (as the signal
2663 * will be arbitrated by hardware, and can fire as we
2664 * roll back state) results in the core responding
2665 * despite being pushed back to the IDLE state, it
2666 * should be no different than if the core took some
2667 * interrupt while IDLE.
2669 if (active_processor
!= processor
) {
2671 * Squash all of the processor state back to some
2672 * reasonable facsimile of PROCESSOR_IDLE.
2675 processor_state_update_idle(active_processor
);
2676 active_processor
->deadline
= UINT64_MAX
;
2677 pset_update_processor_state(pset
, active_processor
, PROCESSOR_IDLE
);
2678 bit_clear(pset
->pending_deferred_AST_cpu_mask
, active_processor
->cpu_id
);
2679 machine_signal_idle_cancel(active_processor
);
2687 /* We don't support deferred ASTs; everything is candycanes and sunshine. */
2696 perfcontrol_event event
= (new->state
& TH_IDLE
) ? IDLE
: CONTEXT_SWITCH
;
2697 uint64_t same_pri_latency
= (new->state
& TH_IDLE
) ? 0 : new->same_pri_latency
;
2698 machine_switch_perfcontrol_context(event
, timestamp
, 0,
2699 same_pri_latency
, old
, new);
2706 * Handle threads at context switch. Re-dispatch other thread
2707 * if still running, otherwise update run state and perform
2708 * special actions. Update quantum for other thread and begin
2709 * the quantum for ourselves.
2711 * "thread" is the old thread that we have switched away from.
2712 * "self" is the new current thread that we have context switched to
2714 * Called at splsched.
2722 processor_t processor
= self
->last_processor
;
2723 bool was_idle
= false;
2725 assert(processor
== current_processor());
2726 assert(self
== current_thread_volatile());
2727 assert(thread
!= self
);
2729 if (thread
!= THREAD_NULL
) {
2731 * Do the perfcontrol callout for context switch.
2732 * The reason we do this here is:
2733 * - thread_dispatch() is called from various places that are not
2734 * the direct context switch path for eg. processor shutdown etc.
2735 * So adding the callout here covers all those cases.
2736 * - We want this callout as early as possible to be close
2737 * to the timestamp taken in thread_invoke()
2738 * - We want to avoid holding the thread lock while doing the
2740 * - We do not want to callout if "thread" is NULL.
2742 thread_csw_callout(thread
, self
, processor
->last_dispatch
);
2745 if (thread
->continuation
!= NULL
) {
2747 * Thread has a continuation and the normal stack is going away.
2748 * Unpoison the stack and mark all fakestack objects as unused.
2750 kasan_fakestack_drop(thread
);
2751 if (thread
->kernel_stack
) {
2752 kasan_unpoison_stack(thread
->kernel_stack
, kernel_stack_size
);
2757 * Free all unused fakestack objects.
2759 kasan_fakestack_gc(thread
);
2763 * If blocked at a continuation, discard
2766 if (thread
->continuation
!= NULL
&& thread
->kernel_stack
!= 0) {
2770 if (thread
->state
& TH_IDLE
) {
2772 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2773 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DISPATCH
) | DBG_FUNC_NONE
,
2774 (uintptr_t)thread_tid(thread
), 0, thread
->state
,
2775 sched_run_buckets
[TH_BUCKET_RUN
], 0);
2778 int64_t remainder
= 0;
2780 if (processor
->quantum_end
> processor
->last_dispatch
) {
2781 remainder
= processor
->quantum_end
-
2782 processor
->last_dispatch
;
2785 consumed
= thread
->quantum_remaining
- remainder
;
2787 if ((thread
->reason
& AST_LEDGER
) == 0) {
2789 * Bill CPU time to both the task and
2790 * the individual thread.
2792 ledger_credit_thread(thread
, thread
->t_ledger
,
2793 task_ledgers
.cpu_time
, consumed
);
2794 ledger_credit_thread(thread
, thread
->t_threadledger
,
2795 thread_ledgers
.cpu_time
, consumed
);
2796 if (thread
->t_bankledger
) {
2797 ledger_credit_thread(thread
, thread
->t_bankledger
,
2798 bank_ledgers
.cpu_time
,
2799 (consumed
- thread
->t_deduct_bank_ledger_time
));
2801 thread
->t_deduct_bank_ledger_time
= 0;
2804 * This should never be negative, but in traces we are seeing some instances
2805 * of consumed being negative.
2806 * <rdar://problem/57782596> thread_dispatch() thread CPU consumed calculation sometimes results in negative value
2808 sched_update_pset_avg_execution_time(current_processor()->processor_set
, consumed
, processor
->last_dispatch
, thread
->th_sched_bucket
);
2813 thread_lock(thread
);
2816 * Apply a priority floor if the thread holds a kernel resource
2817 * Do this before checking starting_pri to avoid overpenalizing
2818 * repeated rwlock blockers.
2820 if (__improbable(thread
->rwlock_count
!= 0)) {
2821 lck_rw_set_promotion_locked(thread
);
2824 boolean_t keep_quantum
= processor
->first_timeslice
;
2827 * Treat a thread which has dropped priority since it got on core
2828 * as having expired its quantum.
2830 if (processor
->starting_pri
> thread
->sched_pri
) {
2831 keep_quantum
= FALSE
;
2834 /* Compute remainder of current quantum. */
2836 processor
->quantum_end
> processor
->last_dispatch
) {
2837 thread
->quantum_remaining
= (uint32_t)remainder
;
2839 thread
->quantum_remaining
= 0;
2842 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
2844 * Cancel the deadline if the thread has
2845 * consumed the entire quantum.
2847 if (thread
->quantum_remaining
== 0) {
2848 thread
->realtime
.deadline
= UINT64_MAX
;
2851 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
2853 * For non-realtime threads treat a tiny
2854 * remaining quantum as an expired quantum
2855 * but include what's left next time.
2857 if (thread
->quantum_remaining
< min_std_quantum
) {
2858 thread
->reason
|= AST_QUANTUM
;
2859 thread
->quantum_remaining
+= SCHED(initial_quantum_size
)(thread
);
2861 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
2865 * If we are doing a direct handoff then
2866 * take the remainder of the quantum.
2868 if ((thread
->reason
& (AST_HANDOFF
| AST_QUANTUM
)) == AST_HANDOFF
) {
2869 self
->quantum_remaining
= thread
->quantum_remaining
;
2870 thread
->reason
|= AST_QUANTUM
;
2871 thread
->quantum_remaining
= 0;
2873 #if defined(CONFIG_SCHED_MULTIQ)
2874 if (SCHED(sched_groups_enabled
) &&
2875 thread
->sched_group
== self
->sched_group
) {
2876 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2877 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_QUANTUM_HANDOFF
),
2878 self
->reason
, (uintptr_t)thread_tid(thread
),
2879 self
->quantum_remaining
, thread
->quantum_remaining
, 0);
2881 self
->quantum_remaining
= thread
->quantum_remaining
;
2882 thread
->quantum_remaining
= 0;
2883 /* Don't set AST_QUANTUM here - old thread might still want to preempt someone else */
2885 #endif /* defined(CONFIG_SCHED_MULTIQ) */
2888 thread
->computation_metered
+= (processor
->last_dispatch
- thread
->computation_epoch
);
2890 if (!(thread
->state
& TH_WAIT
)) {
2894 thread
->last_made_runnable_time
= thread
->last_basepri_change_time
= processor
->last_dispatch
;
2896 machine_thread_going_off_core(thread
, FALSE
, processor
->last_dispatch
, TRUE
);
2898 ast_t reason
= thread
->reason
;
2899 sched_options_t options
= SCHED_NONE
;
2901 if (reason
& AST_REBALANCE
) {
2902 options
|= SCHED_REBALANCE
;
2903 if (reason
& AST_QUANTUM
) {
2905 * Having gone to the trouble of forcing this thread off a less preferred core,
2906 * we should force the preferable core to reschedule immediately to give this
2907 * thread a chance to run instead of just sitting on the run queue where
2908 * it may just be stolen back by the idle core we just forced it off.
2909 * But only do this at the end of a quantum to prevent cascading effects.
2911 options
|= SCHED_PREEMPT
;
2915 if (reason
& AST_QUANTUM
) {
2916 options
|= SCHED_TAILQ
;
2917 } else if (reason
& AST_PREEMPT
) {
2918 options
|= SCHED_HEADQ
;
2920 options
|= (SCHED_PREEMPT
| SCHED_TAILQ
);
2923 thread_setrun(thread
, options
);
2925 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2926 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DISPATCH
) | DBG_FUNC_NONE
,
2927 (uintptr_t)thread_tid(thread
), thread
->reason
, thread
->state
,
2928 sched_run_buckets
[TH_BUCKET_RUN
], 0);
2930 if (thread
->wake_active
) {
2931 thread
->wake_active
= FALSE
;
2932 thread_unlock(thread
);
2934 thread_wakeup(&thread
->wake_active
);
2936 thread_unlock(thread
);
2939 wake_unlock(thread
);
2944 boolean_t should_terminate
= FALSE
;
2945 uint32_t new_run_count
;
2946 int thread_state
= thread
->state
;
2948 /* Only the first call to thread_dispatch
2949 * after explicit termination should add
2950 * the thread to the termination queue
2952 if ((thread_state
& (TH_TERMINATE
| TH_TERMINATE2
)) == TH_TERMINATE
) {
2953 should_terminate
= TRUE
;
2954 thread_state
|= TH_TERMINATE2
;
2957 timer_stop(&thread
->runnable_timer
, processor
->last_dispatch
);
2959 thread_state
&= ~TH_RUN
;
2960 thread
->state
= thread_state
;
2962 thread
->last_made_runnable_time
= thread
->last_basepri_change_time
= THREAD_NOT_RUNNABLE
;
2963 thread
->chosen_processor
= PROCESSOR_NULL
;
2965 new_run_count
= SCHED(run_count_decr
)(thread
);
2967 #if CONFIG_SCHED_AUTO_JOIN
2968 if ((thread
->sched_flags
& TH_SFLAG_THREAD_GROUP_AUTO_JOIN
) != 0) {
2969 work_interval_auto_join_unwind(thread
);
2971 #endif /* CONFIG_SCHED_AUTO_JOIN */
2973 #if CONFIG_SCHED_SFI
2974 if (thread
->reason
& AST_SFI
) {
2975 thread
->wait_sfi_begin_time
= processor
->last_dispatch
;
2978 machine_thread_going_off_core(thread
, should_terminate
, processor
->last_dispatch
, FALSE
);
2980 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2981 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DISPATCH
) | DBG_FUNC_NONE
,
2982 (uintptr_t)thread_tid(thread
), thread
->reason
, thread_state
,
2985 if (thread_state
& TH_WAIT_REPORT
) {
2986 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
2989 if (thread
->wake_active
) {
2990 thread
->wake_active
= FALSE
;
2991 thread_unlock(thread
);
2993 thread_wakeup(&thread
->wake_active
);
2995 thread_unlock(thread
);
2998 wake_unlock(thread
);
3000 if (should_terminate
) {
3001 thread_terminate_enqueue(thread
);
3006 * The thread could have been added to the termination queue, so it's
3007 * unsafe to use after this point.
3009 thread
= THREAD_NULL
;
3012 int urgency
= THREAD_URGENCY_NONE
;
3013 uint64_t latency
= 0;
3015 /* Update (new) current thread and reprogram running timers */
3018 if (!(self
->state
& TH_IDLE
)) {
3019 uint64_t arg1
, arg2
;
3021 #if CONFIG_SCHED_SFI
3024 new_ast
= sfi_thread_needs_ast(self
, NULL
);
3026 if (new_ast
!= AST_NONE
) {
3031 assertf(processor
->last_dispatch
>= self
->last_made_runnable_time
,
3032 "Non-monotonic time? dispatch at 0x%llx, runnable at 0x%llx",
3033 processor
->last_dispatch
, self
->last_made_runnable_time
);
3035 assert(self
->last_made_runnable_time
<= self
->last_basepri_change_time
);
3037 latency
= processor
->last_dispatch
- self
->last_made_runnable_time
;
3038 assert(latency
>= self
->same_pri_latency
);
3040 urgency
= thread_get_urgency(self
, &arg1
, &arg2
);
3042 thread_tell_urgency(urgency
, arg1
, arg2
, latency
, self
);
3045 * Get a new quantum if none remaining.
3047 if (self
->quantum_remaining
== 0) {
3048 thread_quantum_init(self
);
3052 * Set up quantum timer and timeslice.
3054 processor
->quantum_end
= processor
->last_dispatch
+
3055 self
->quantum_remaining
;
3057 running_timer_setup(processor
, RUNNING_TIMER_QUANTUM
, self
,
3058 processor
->quantum_end
, processor
->last_dispatch
);
3061 * kperf's running timer is active whenever the idle thread for a
3062 * CPU is not running.
3064 kperf_running_setup(processor
, processor
->last_dispatch
);
3066 running_timers_activate(processor
);
3067 processor
->first_timeslice
= TRUE
;
3069 running_timers_deactivate(processor
);
3070 processor
->first_timeslice
= FALSE
;
3071 thread_tell_urgency(THREAD_URGENCY_NONE
, 0, 0, 0, self
);
3074 assert(self
->block_hint
== kThreadWaitNone
);
3075 self
->computation_epoch
= processor
->last_dispatch
;
3076 self
->reason
= AST_NONE
;
3077 processor
->starting_pri
= self
->sched_pri
;
3079 thread_unlock(self
);
3081 machine_thread_going_on_core(self
, urgency
, latency
, self
->same_pri_latency
,
3082 processor
->last_dispatch
);
3084 #if defined(CONFIG_SCHED_DEFERRED_AST)
3086 * TODO: Can we state that redispatching our old thread is also
3089 if ((os_atomic_load(&sched_run_buckets
[TH_BUCKET_RUN
], relaxed
) == 1) && !(self
->state
& TH_IDLE
)) {
3090 pset_cancel_deferred_dispatch(processor
->processor_set
, processor
);
3096 * thread_block_reason:
3098 * Forces a reschedule, blocking the caller if a wait
3099 * has been asserted.
3101 * If a continuation is specified, then thread_invoke will
3102 * attempt to discard the thread's kernel stack. When the
3103 * thread resumes, it will execute the continuation function
3104 * on a new kernel stack.
3106 counter(mach_counter_t c_thread_block_calls
= 0; )
3109 thread_block_reason(
3110 thread_continue_t continuation
,
3114 thread_t self
= current_thread();
3115 processor_t processor
;
3116 thread_t new_thread
;
3119 counter(++c_thread_block_calls
);
3123 processor
= current_processor();
3125 /* If we're explicitly yielding, force a subsequent quantum */
3126 if (reason
& AST_YIELD
) {
3127 processor
->first_timeslice
= FALSE
;
3130 /* We're handling all scheduling AST's */
3131 ast_off(AST_SCHEDULING
);
3134 if ((continuation
!= NULL
) && (self
->task
!= kernel_task
)) {
3135 if (uthread_get_proc_refcount(self
->uthread
) != 0) {
3136 panic("thread_block_reason with continuation uthread %p with uu_proc_refcount != 0", self
->uthread
);
3141 self
->continuation
= continuation
;
3142 self
->parameter
= parameter
;
3144 if (self
->state
& ~(TH_RUN
| TH_IDLE
)) {
3145 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3146 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_BLOCK
),
3147 reason
, VM_KERNEL_UNSLIDE(continuation
), 0, 0, 0);
3152 new_thread
= thread_select(self
, processor
, &reason
);
3153 thread_unlock(self
);
3154 } while (!thread_invoke(self
, new_thread
, reason
));
3158 return self
->wait_result
;
3164 * Block the current thread if a wait has been asserted.
3168 thread_continue_t continuation
)
3170 return thread_block_reason(continuation
, NULL
, AST_NONE
);
3174 thread_block_parameter(
3175 thread_continue_t continuation
,
3178 return thread_block_reason(continuation
, parameter
, AST_NONE
);
3184 * Switch directly from the current thread to the
3185 * new thread, handing off our quantum if appropriate.
3187 * New thread must be runnable, and not on a run queue.
3189 * Called at splsched.
3194 thread_continue_t continuation
,
3196 thread_t new_thread
)
3198 ast_t reason
= AST_NONE
;
3200 if ((self
->state
& TH_IDLE
) == 0) {
3201 reason
= AST_HANDOFF
;
3205 * If this thread hadn't been setrun'ed, it
3206 * might not have a chosen processor, so give it one
3208 if (new_thread
->chosen_processor
== NULL
) {
3209 new_thread
->chosen_processor
= current_processor();
3212 self
->continuation
= continuation
;
3213 self
->parameter
= parameter
;
3215 while (!thread_invoke(self
, new_thread
, reason
)) {
3216 /* the handoff failed, so we have to fall back to the normal block path */
3217 processor_t processor
= current_processor();
3222 new_thread
= thread_select(self
, processor
, &reason
);
3223 thread_unlock(self
);
3226 return self
->wait_result
;
3232 * Called at splsched when a thread first receives
3233 * a new stack after a continuation.
3235 * Called with THREAD_NULL as the old thread when
3236 * invoked by machine_load_context.
3242 thread_t self
= current_thread();
3243 thread_continue_t continuation
;
3246 DTRACE_SCHED(on__cpu
);
3248 continuation
= self
->continuation
;
3249 parameter
= self
->parameter
;
3251 assert(continuation
!= NULL
);
3254 kperf_on_cpu(self
, continuation
, NULL
);
3257 thread_dispatch(thread
, self
);
3259 self
->continuation
= self
->parameter
= NULL
;
3261 #if INTERRUPT_MASKED_DEBUG
3262 /* Reset interrupt-masked spin debugging timeout */
3263 ml_spin_debug_clear(self
);
3266 TLOG(1, "thread_continue: calling call_continuation\n");
3268 boolean_t enable_interrupts
= TRUE
;
3270 /* bootstrap thread, idle thread need to stay interrupts-disabled */
3271 if (thread
== THREAD_NULL
|| (self
->state
& TH_IDLE
)) {
3272 enable_interrupts
= FALSE
;
3275 call_continuation(continuation
, parameter
, self
->wait_result
, enable_interrupts
);
3280 thread_quantum_init(thread_t thread
)
3282 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
3283 thread
->quantum_remaining
= thread
->realtime
.computation
;
3285 thread
->quantum_remaining
= SCHED(initial_quantum_size
)(thread
);
3290 sched_timeshare_initial_quantum_size(thread_t thread
)
3292 if ((thread
!= THREAD_NULL
) && thread
->th_sched_bucket
== TH_BUCKET_SHARE_BG
) {
3302 * Initialize a run queue before first use.
3309 for (u_int i
= 0; i
< BITMAP_LEN(NRQS
); i
++) {
3312 rq
->urgency
= rq
->count
= 0;
3313 for (int i
= 0; i
< NRQS
; i
++) {
3314 circle_queue_init(&rq
->queues
[i
]);
3319 * run_queue_dequeue:
3321 * Perform a dequeue operation on a run queue,
3322 * and return the resulting thread.
3324 * The run queue must be locked (see thread_run_queue_remove()
3325 * for more info), and not empty.
3330 sched_options_t options
)
3333 circle_queue_t queue
= &rq
->queues
[rq
->highq
];
3335 if (options
& SCHED_HEADQ
) {
3336 thread
= cqe_dequeue_head(queue
, struct thread
, runq_links
);
3338 thread
= cqe_dequeue_tail(queue
, struct thread
, runq_links
);
3341 assert(thread
!= THREAD_NULL
);
3342 assert_thread_magic(thread
);
3344 thread
->runq
= PROCESSOR_NULL
;
3345 SCHED_STATS_RUNQ_CHANGE(&rq
->runq_stats
, rq
->count
);
3347 if (SCHED(priority_is_urgent
)(rq
->highq
)) {
3348 rq
->urgency
--; assert(rq
->urgency
>= 0);
3350 if (circle_queue_empty(queue
)) {
3351 bitmap_clear(rq
->bitmap
, rq
->highq
);
3352 rq
->highq
= bitmap_first(rq
->bitmap
, NRQS
);
3359 * run_queue_enqueue:
3361 * Perform a enqueue operation on a run queue.
3363 * The run queue must be locked (see thread_run_queue_remove()
3370 sched_options_t options
)
3372 circle_queue_t queue
= &rq
->queues
[thread
->sched_pri
];
3373 boolean_t result
= FALSE
;
3375 assert_thread_magic(thread
);
3377 if (circle_queue_empty(queue
)) {
3378 circle_enqueue_tail(queue
, &thread
->runq_links
);
3380 rq_bitmap_set(rq
->bitmap
, thread
->sched_pri
);
3381 if (thread
->sched_pri
> rq
->highq
) {
3382 rq
->highq
= thread
->sched_pri
;
3386 if (options
& SCHED_TAILQ
) {
3387 circle_enqueue_tail(queue
, &thread
->runq_links
);
3389 circle_enqueue_head(queue
, &thread
->runq_links
);
3392 if (SCHED(priority_is_urgent
)(thread
->sched_pri
)) {
3395 SCHED_STATS_RUNQ_CHANGE(&rq
->runq_stats
, rq
->count
);
3404 * Remove a specific thread from a runqueue.
3406 * The run queue must be locked.
3413 circle_queue_t queue
= &rq
->queues
[thread
->sched_pri
];
3415 assert(thread
->runq
!= PROCESSOR_NULL
);
3416 assert_thread_magic(thread
);
3418 circle_dequeue(queue
, &thread
->runq_links
);
3419 SCHED_STATS_RUNQ_CHANGE(&rq
->runq_stats
, rq
->count
);
3421 if (SCHED(priority_is_urgent
)(thread
->sched_pri
)) {
3422 rq
->urgency
--; assert(rq
->urgency
>= 0);
3425 if (circle_queue_empty(queue
)) {
3426 /* update run queue status */
3427 bitmap_clear(rq
->bitmap
, thread
->sched_pri
);
3428 rq
->highq
= bitmap_first(rq
->bitmap
, NRQS
);
3431 thread
->runq
= PROCESSOR_NULL
;
3437 * Peek at the runq and return the highest
3438 * priority thread from the runq.
3440 * The run queue must be locked.
3446 if (rq
->count
> 0) {
3447 circle_queue_t queue
= &rq
->queues
[rq
->highq
];
3448 thread_t thread
= cqe_queue_first(queue
, struct thread
, runq_links
);
3449 assert_thread_magic(thread
);
3457 sched_rtlocal_runq(processor_set_t pset
)
3459 return &pset
->rt_runq
;
3463 sched_rtlocal_init(processor_set_t pset
)
3469 sched_rtlocal_queue_shutdown(processor_t processor
)
3471 processor_set_t pset
= processor
->processor_set
;
3473 queue_head_t tqueue
;
3477 /* We only need to migrate threads if this is the last active or last recommended processor in the pset */
3478 if ((pset
->online_processor_count
> 0) && pset_is_recommended(pset
)) {
3483 queue_init(&tqueue
);
3485 while (rt_runq_count(pset
) > 0) {
3486 thread
= qe_dequeue_head(&pset
->rt_runq
.queue
, struct thread
, runq_links
);
3487 thread
->runq
= PROCESSOR_NULL
;
3488 SCHED_STATS_RUNQ_CHANGE(&pset
->rt_runq
.runq_stats
, rt_runq_count(pset
));
3489 rt_runq_count_decr(pset
);
3490 enqueue_tail(&tqueue
, &thread
->runq_links
);
3492 sched_update_pset_load_average(pset
, 0);
3495 qe_foreach_element_safe(thread
, &tqueue
, runq_links
) {
3496 remqueue(&thread
->runq_links
);
3498 thread_lock(thread
);
3500 thread_setrun(thread
, SCHED_TAILQ
);
3502 thread_unlock(thread
);
3506 /* Assumes RT lock is not held, and acquires splsched/rt_lock itself */
3508 sched_rtlocal_runq_scan(sched_update_scan_context_t scan_context
)
3512 pset_node_t node
= &pset_node0
;
3513 processor_set_t pset
= node
->psets
;
3515 spl_t s
= splsched();
3517 while (pset
!= NULL
) {
3520 qe_foreach_element_safe(thread
, &pset
->rt_runq
.queue
, runq_links
) {
3521 if (thread
->last_made_runnable_time
< scan_context
->earliest_rt_make_runnable_time
) {
3522 scan_context
->earliest_rt_make_runnable_time
= thread
->last_made_runnable_time
;
3528 pset
= pset
->pset_list
;
3530 } while (((node
= node
->node_list
) != NULL
) && ((pset
= node
->psets
) != NULL
));
3535 sched_rtlocal_runq_count_sum(void)
3537 pset_node_t node
= &pset_node0
;
3538 processor_set_t pset
= node
->psets
;
3542 while (pset
!= NULL
) {
3543 count
+= pset
->rt_runq
.runq_stats
.count_sum
;
3545 pset
= pset
->pset_list
;
3547 } while (((node
= node
->node_list
) != NULL
) && ((pset
= node
->psets
) != NULL
));
3553 * realtime_queue_insert:
3555 * Enqueue a thread for realtime execution.
3558 realtime_queue_insert(processor_t processor
, processor_set_t pset
, thread_t thread
)
3560 queue_t queue
= &SCHED(rt_runq
)(pset
)->queue
;
3561 uint64_t deadline
= thread
->realtime
.deadline
;
3562 boolean_t preempt
= FALSE
;
3564 pset_assert_locked(pset
);
3566 if (queue_empty(queue
)) {
3567 enqueue_tail(queue
, &thread
->runq_links
);
3570 /* Insert into rt_runq in thread deadline order */
3572 qe_foreach(iter
, queue
) {
3573 thread_t iter_thread
= qe_element(iter
, struct thread
, runq_links
);
3574 assert_thread_magic(iter_thread
);
3576 if (deadline
< iter_thread
->realtime
.deadline
) {
3577 if (iter
== queue_first(queue
)) {
3580 insque(&thread
->runq_links
, queue_prev(iter
));
3582 } else if (iter
== queue_last(queue
)) {
3583 enqueue_tail(queue
, &thread
->runq_links
);
3589 thread
->runq
= processor
;
3590 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq
)(pset
)->runq_stats
, rt_runq_count(pset
));
3591 rt_runq_count_incr(pset
);
3596 #define MAX_BACKUP_PROCESSORS 7
3597 #if defined(__x86_64__)
3598 #define DEFAULT_BACKUP_PROCESSORS 1
3600 #define DEFAULT_BACKUP_PROCESSORS 0
3603 int sched_rt_n_backup_processors
= DEFAULT_BACKUP_PROCESSORS
;
3606 sched_get_rt_n_backup_processors(void)
3608 return sched_rt_n_backup_processors
;
3612 sched_set_rt_n_backup_processors(int n
)
3616 } else if (n
> MAX_BACKUP_PROCESSORS
) {
3617 n
= MAX_BACKUP_PROCESSORS
;
3620 sched_rt_n_backup_processors
= n
;
3626 * Dispatch a thread for realtime execution.
3628 * Thread must be locked. Associated pset must
3629 * be locked, and is returned unlocked.
3633 processor_t chosen_processor
,
3636 processor_set_t pset
= chosen_processor
->processor_set
;
3637 pset_assert_locked(pset
);
3642 if (thread
->realtime
.constraint
<= rt_constraint_threshold
) {
3643 n_backup
= sched_rt_n_backup_processors
;
3645 assert((n_backup
>= 0) && (n_backup
<= MAX_BACKUP_PROCESSORS
));
3647 sched_ipi_type_t ipi_type
[MAX_BACKUP_PROCESSORS
+ 1] = {};
3648 processor_t ipi_processor
[MAX_BACKUP_PROCESSORS
+ 1] = {};
3650 thread
->chosen_processor
= chosen_processor
;
3652 /* <rdar://problem/15102234> */
3653 assert(thread
->bound_processor
== PROCESSOR_NULL
);
3655 realtime_queue_insert(chosen_processor
, pset
, thread
);
3657 processor_t processor
= chosen_processor
;
3658 bool chosen_process_is_secondary
= chosen_processor
->processor_primary
!= chosen_processor
;
3661 for (int i
= 0; i
<= n_backup
; i
++) {
3663 processor
= choose_processor_for_realtime_thread(pset
, chosen_processor
, chosen_process_is_secondary
);
3664 if ((processor
== PROCESSOR_NULL
) || (sched_avoid_cpu0
&& (processor
->cpu_id
== 0))) {
3667 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHOOSE_PROCESSOR
) | DBG_FUNC_NONE
,
3668 (uintptr_t)thread_tid(thread
), (uintptr_t)-3, processor
->cpu_id
, processor
->state
, 0);
3670 ipi_type
[i
] = SCHED_IPI_NONE
;
3671 ipi_processor
[i
] = processor
;
3674 if (processor
->current_pri
< BASEPRI_RTQUEUES
) {
3675 preempt
= (AST_PREEMPT
| AST_URGENT
);
3676 } else if (thread
->realtime
.deadline
< processor
->deadline
) {
3677 preempt
= (AST_PREEMPT
| AST_URGENT
);
3682 if (preempt
!= AST_NONE
) {
3683 if (processor
->state
== PROCESSOR_IDLE
) {
3684 processor_state_update_from_thread(processor
, thread
);
3685 processor
->deadline
= thread
->realtime
.deadline
;
3686 pset_update_processor_state(pset
, processor
, PROCESSOR_DISPATCHING
);
3687 if (processor
== current_processor()) {
3690 if ((preempt
& AST_URGENT
) == AST_URGENT
) {
3691 bit_set(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
3694 if ((preempt
& AST_PREEMPT
) == AST_PREEMPT
) {
3695 bit_set(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
3698 ipi_type
[i
] = sched_ipi_action(processor
, thread
, true, SCHED_IPI_EVENT_PREEMPT
);
3700 } else if (processor
->state
== PROCESSOR_DISPATCHING
) {
3701 if ((processor
->current_pri
< thread
->sched_pri
) || (processor
->deadline
> thread
->realtime
.deadline
)) {
3702 processor_state_update_from_thread(processor
, thread
);
3703 processor
->deadline
= thread
->realtime
.deadline
;
3706 if (processor
== current_processor()) {
3709 if ((preempt
& AST_URGENT
) == AST_URGENT
) {
3710 bit_set(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
3713 if ((preempt
& AST_PREEMPT
) == AST_PREEMPT
) {
3714 bit_set(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
3717 ipi_type
[i
] = sched_ipi_action(processor
, thread
, false, SCHED_IPI_EVENT_PREEMPT
);
3721 /* Selected processor was too busy, just keep thread enqueued and let other processors drain it naturally. */
3727 assert((count
> 0) && (count
<= (n_backup
+ 1)));
3728 for (int i
= 0; i
< count
; i
++) {
3729 assert(ipi_processor
[i
] != PROCESSOR_NULL
);
3730 sched_ipi_perform(ipi_processor
[i
], ipi_type
[i
]);
3736 sched_ipi_deferred_policy(processor_set_t pset
, processor_t dst
,
3737 __unused sched_ipi_event_t event
)
3739 #if defined(CONFIG_SCHED_DEFERRED_AST)
3740 if (!bit_test(pset
->pending_deferred_AST_cpu_mask
, dst
->cpu_id
)) {
3741 return SCHED_IPI_DEFERRED
;
3743 #else /* CONFIG_SCHED_DEFERRED_AST */
3744 panic("Request for deferred IPI on an unsupported platform; pset: %p CPU: %d", pset
, dst
->cpu_id
);
3745 #endif /* CONFIG_SCHED_DEFERRED_AST */
3746 return SCHED_IPI_NONE
;
3750 sched_ipi_action(processor_t dst
, thread_t thread
, boolean_t dst_idle
, sched_ipi_event_t event
)
3752 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
3753 assert(dst
!= NULL
);
3755 processor_set_t pset
= dst
->processor_set
;
3756 if (current_processor() == dst
) {
3757 return SCHED_IPI_NONE
;
3760 if (bit_test(pset
->pending_AST_URGENT_cpu_mask
, dst
->cpu_id
)) {
3761 return SCHED_IPI_NONE
;
3764 ipi_type
= SCHED(ipi_policy
)(dst
, thread
, dst_idle
, event
);
3766 case SCHED_IPI_NONE
:
3767 return SCHED_IPI_NONE
;
3768 #if defined(CONFIG_SCHED_DEFERRED_AST)
3769 case SCHED_IPI_DEFERRED
:
3770 bit_set(pset
->pending_deferred_AST_cpu_mask
, dst
->cpu_id
);
3772 #endif /* CONFIG_SCHED_DEFERRED_AST */
3774 bit_set(pset
->pending_AST_URGENT_cpu_mask
, dst
->cpu_id
);
3775 bit_set(pset
->pending_AST_PREEMPT_cpu_mask
, dst
->cpu_id
);
3782 sched_ipi_policy(processor_t dst
, thread_t thread
, boolean_t dst_idle
, sched_ipi_event_t event
)
3784 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
3785 boolean_t deferred_ipi_supported
= false;
3786 processor_set_t pset
= dst
->processor_set
;
3788 #if defined(CONFIG_SCHED_DEFERRED_AST)
3789 deferred_ipi_supported
= true;
3790 #endif /* CONFIG_SCHED_DEFERRED_AST */
3793 case SCHED_IPI_EVENT_SPILL
:
3794 case SCHED_IPI_EVENT_SMT_REBAL
:
3795 case SCHED_IPI_EVENT_REBALANCE
:
3796 case SCHED_IPI_EVENT_BOUND_THR
:
3798 * The spill, SMT rebalance, rebalance and the bound thread
3799 * scenarios use immediate IPIs always.
3801 ipi_type
= dst_idle
? SCHED_IPI_IDLE
: SCHED_IPI_IMMEDIATE
;
3803 case SCHED_IPI_EVENT_PREEMPT
:
3804 /* In the preemption case, use immediate IPIs for RT threads */
3805 if (thread
&& (thread
->sched_pri
>= BASEPRI_RTQUEUES
)) {
3806 ipi_type
= dst_idle
? SCHED_IPI_IDLE
: SCHED_IPI_IMMEDIATE
;
3811 * For Non-RT threads preemption,
3812 * If the core is active, use immediate IPIs.
3813 * If the core is idle, use deferred IPIs if supported; otherwise immediate IPI.
3815 if (deferred_ipi_supported
&& dst_idle
) {
3816 return sched_ipi_deferred_policy(pset
, dst
, event
);
3818 ipi_type
= dst_idle
? SCHED_IPI_IDLE
: SCHED_IPI_IMMEDIATE
;
3821 panic("Unrecognized scheduler IPI event type %d", event
);
3823 assert(ipi_type
!= SCHED_IPI_NONE
);
3828 sched_ipi_perform(processor_t dst
, sched_ipi_type_t ipi
)
3831 case SCHED_IPI_NONE
:
3833 case SCHED_IPI_IDLE
:
3834 machine_signal_idle(dst
);
3836 case SCHED_IPI_IMMEDIATE
:
3837 cause_ast_check(dst
);
3839 case SCHED_IPI_DEFERRED
:
3840 machine_signal_idle_deferred(dst
);
3843 panic("Unrecognized scheduler IPI type: %d", ipi
);
3847 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
3850 priority_is_urgent(int priority
)
3852 return bitmap_test(sched_preempt_pri
, priority
) ? TRUE
: FALSE
;
3855 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
3860 * Dispatch a thread for execution on a
3863 * Thread must be locked. Associated pset must
3864 * be locked, and is returned unlocked.
3868 processor_t processor
,
3872 processor_set_t pset
= processor
->processor_set
;
3873 pset_assert_locked(pset
);
3875 enum { eExitIdle
, eInterruptRunning
, eDoNothing
} ipi_action
= eDoNothing
;
3877 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
3879 thread
->chosen_processor
= processor
;
3882 * Set preemption mode.
3884 #if defined(CONFIG_SCHED_DEFERRED_AST)
3885 /* TODO: Do we need to care about urgency (see rdar://problem/20136239)? */
3887 if (SCHED(priority_is_urgent
)(thread
->sched_pri
) && thread
->sched_pri
> processor
->current_pri
) {
3888 preempt
= (AST_PREEMPT
| AST_URGENT
);
3889 } else if (processor
->active_thread
&& thread_eager_preemption(processor
->active_thread
)) {
3890 preempt
= (AST_PREEMPT
| AST_URGENT
);
3891 } else if ((thread
->sched_mode
== TH_MODE_TIMESHARE
) && (thread
->sched_pri
< thread
->base_pri
)) {
3892 if (SCHED(priority_is_urgent
)(thread
->base_pri
) && thread
->sched_pri
> processor
->current_pri
) {
3893 preempt
= (options
& SCHED_PREEMPT
)? AST_PREEMPT
: AST_NONE
;
3898 preempt
= (options
& SCHED_PREEMPT
)? AST_PREEMPT
: AST_NONE
;
3901 if ((options
& (SCHED_PREEMPT
| SCHED_REBALANCE
)) == (SCHED_PREEMPT
| SCHED_REBALANCE
)) {
3903 * Having gone to the trouble of forcing this thread off a less preferred core,
3904 * we should force the preferable core to reschedule immediately to give this
3905 * thread a chance to run instead of just sitting on the run queue where
3906 * it may just be stolen back by the idle core we just forced it off.
3908 preempt
|= AST_PREEMPT
;
3911 SCHED(processor_enqueue
)(processor
, thread
, options
);
3912 sched_update_pset_load_average(pset
, 0);
3914 if (preempt
!= AST_NONE
) {
3915 if (processor
->state
== PROCESSOR_IDLE
) {
3916 processor_state_update_from_thread(processor
, thread
);
3917 processor
->deadline
= UINT64_MAX
;
3918 pset_update_processor_state(pset
, processor
, PROCESSOR_DISPATCHING
);
3919 ipi_action
= eExitIdle
;
3920 } else if (processor
->state
== PROCESSOR_DISPATCHING
) {
3921 if (processor
->current_pri
< thread
->sched_pri
) {
3922 processor_state_update_from_thread(processor
, thread
);
3923 processor
->deadline
= UINT64_MAX
;
3925 } else if ((processor
->state
== PROCESSOR_RUNNING
||
3926 processor
->state
== PROCESSOR_SHUTDOWN
) &&
3927 (thread
->sched_pri
>= processor
->current_pri
)) {
3928 ipi_action
= eInterruptRunning
;
3932 * New thread is not important enough to preempt what is running, but
3933 * special processor states may need special handling
3935 if (processor
->state
== PROCESSOR_SHUTDOWN
&&
3936 thread
->sched_pri
>= processor
->current_pri
) {
3937 ipi_action
= eInterruptRunning
;
3938 } else if (processor
->state
== PROCESSOR_IDLE
) {
3939 processor_state_update_from_thread(processor
, thread
);
3940 processor
->deadline
= UINT64_MAX
;
3941 pset_update_processor_state(pset
, processor
, PROCESSOR_DISPATCHING
);
3943 ipi_action
= eExitIdle
;
3947 if (ipi_action
!= eDoNothing
) {
3948 if (processor
== current_processor()) {
3949 if ((preempt
= csw_check_locked(processor
->active_thread
, processor
, pset
, AST_NONE
)) != AST_NONE
) {
3953 if ((preempt
& AST_URGENT
) == AST_URGENT
) {
3954 bit_set(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
3956 bit_clear(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
3959 if ((preempt
& AST_PREEMPT
) == AST_PREEMPT
) {
3960 bit_set(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
3962 bit_clear(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
3965 sched_ipi_event_t event
= (options
& SCHED_REBALANCE
) ? SCHED_IPI_EVENT_REBALANCE
: SCHED_IPI_EVENT_PREEMPT
;
3966 ipi_type
= sched_ipi_action(processor
, thread
, (ipi_action
== eExitIdle
), event
);
3970 sched_ipi_perform(processor
, ipi_type
);
3976 * Return the next sibling pset containing
3977 * available processors.
3979 * Returns the original pset if none other is
3982 static processor_set_t
3984 processor_set_t pset
)
3986 processor_set_t nset
= pset
;
3989 nset
= next_pset(nset
);
3990 } while (nset
->online_processor_count
< 1 && nset
!= pset
);
3995 inline static processor_set_t
3996 change_locked_pset(processor_set_t current_pset
, processor_set_t new_pset
)
3998 if (current_pset
!= new_pset
) {
3999 pset_unlock(current_pset
);
4000 pset_lock(new_pset
);
4009 * Choose a processor for the thread, beginning at
4010 * the pset. Accepts an optional processor hint in
4013 * Returns a processor, possibly from a different pset.
4015 * The thread must be locked. The pset must be locked,
4016 * and the resulting pset is locked on return.
4020 processor_set_t starting_pset
,
4021 processor_t processor
,
4024 processor_set_t pset
= starting_pset
;
4025 processor_set_t nset
;
4027 assert(thread
->sched_pri
<= BASEPRI_RTQUEUES
);
4030 * Prefer the hinted processor, when appropriate.
4033 /* Fold last processor hint from secondary processor to its primary */
4034 if (processor
!= PROCESSOR_NULL
) {
4035 processor
= processor
->processor_primary
;
4039 * Only consult platform layer if pset is active, which
4040 * it may not be in some cases when a multi-set system
4041 * is going to sleep.
4043 if (pset
->online_processor_count
) {
4044 if ((processor
== PROCESSOR_NULL
) || (processor
->processor_set
== pset
&& processor
->state
== PROCESSOR_IDLE
)) {
4045 processor_t mc_processor
= machine_choose_processor(pset
, processor
);
4046 if (mc_processor
!= PROCESSOR_NULL
) {
4047 processor
= mc_processor
->processor_primary
;
4053 * At this point, we may have a processor hint, and we may have
4054 * an initial starting pset. If the hint is not in the pset, or
4055 * if the hint is for a processor in an invalid state, discard
4058 if (processor
!= PROCESSOR_NULL
) {
4059 if (processor
->processor_set
!= pset
) {
4060 processor
= PROCESSOR_NULL
;
4061 } else if (!processor
->is_recommended
) {
4062 processor
= PROCESSOR_NULL
;
4064 switch (processor
->state
) {
4065 case PROCESSOR_START
:
4066 case PROCESSOR_SHUTDOWN
:
4067 case PROCESSOR_OFF_LINE
:
4069 * Hint is for a processor that cannot support running new threads.
4071 processor
= PROCESSOR_NULL
;
4073 case PROCESSOR_IDLE
:
4075 * Hint is for an idle processor. Assume it is no worse than any other
4076 * idle processor. The platform layer had an opportunity to provide
4077 * the "least cost idle" processor above.
4079 if ((thread
->sched_pri
< BASEPRI_RTQUEUES
) || processor_is_fast_track_candidate_for_realtime_thread(pset
, processor
)) {
4082 processor
= PROCESSOR_NULL
;
4084 case PROCESSOR_RUNNING
:
4085 case PROCESSOR_DISPATCHING
:
4087 * Hint is for an active CPU. This fast-path allows
4088 * realtime threads to preempt non-realtime threads
4089 * to regain their previous executing processor.
4091 if ((thread
->sched_pri
>= BASEPRI_RTQUEUES
) &&
4092 processor_is_fast_track_candidate_for_realtime_thread(pset
, processor
)) {
4096 /* Otherwise, use hint as part of search below */
4099 processor
= PROCESSOR_NULL
;
4106 * Iterate through the processor sets to locate
4107 * an appropriate processor. Seed results with
4108 * a last-processor hint, if available, so that
4109 * a search must find something strictly better
4112 * A primary/secondary pair of SMT processors are
4113 * "unpaired" if the primary is busy but its
4114 * corresponding secondary is idle (so the physical
4115 * core has full use of its resources).
4118 integer_t lowest_priority
= MAXPRI
+ 1;
4119 integer_t lowest_secondary_priority
= MAXPRI
+ 1;
4120 integer_t lowest_unpaired_primary_priority
= MAXPRI
+ 1;
4121 integer_t lowest_idle_secondary_priority
= MAXPRI
+ 1;
4122 integer_t lowest_count
= INT_MAX
;
4123 uint64_t furthest_deadline
= 1;
4124 processor_t lp_processor
= PROCESSOR_NULL
;
4125 processor_t lp_unpaired_primary_processor
= PROCESSOR_NULL
;
4126 processor_t lp_idle_secondary_processor
= PROCESSOR_NULL
;
4127 processor_t lp_paired_secondary_processor
= PROCESSOR_NULL
;
4128 processor_t lc_processor
= PROCESSOR_NULL
;
4129 processor_t fd_processor
= PROCESSOR_NULL
;
4131 if (processor
!= PROCESSOR_NULL
) {
4132 /* All other states should be enumerated above. */
4133 assert(processor
->state
== PROCESSOR_RUNNING
|| processor
->state
== PROCESSOR_DISPATCHING
);
4135 lowest_priority
= processor
->current_pri
;
4136 lp_processor
= processor
;
4138 if (processor
->current_pri
>= BASEPRI_RTQUEUES
) {
4139 furthest_deadline
= processor
->deadline
;
4140 fd_processor
= processor
;
4143 lowest_count
= SCHED(processor_runq_count
)(processor
);
4144 lc_processor
= processor
;
4147 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
4148 pset_node_t node
= pset
->node
;
4149 int consider_secondaries
= (!pset
->is_SMT
) || (bit_count(node
->pset_map
) == 1) || (node
->pset_non_rt_primary_map
== 0);
4150 for (; consider_secondaries
< 2; consider_secondaries
++) {
4151 pset
= change_locked_pset(pset
, starting_pset
);
4153 processor
= choose_processor_for_realtime_thread(pset
, PROCESSOR_NULL
, consider_secondaries
);
4158 /* NRG Collect processor stats for furthest deadline etc. here */
4160 nset
= next_pset(pset
);
4162 if (nset
!= starting_pset
) {
4163 pset
= change_locked_pset(pset
, nset
);
4165 } while (nset
!= starting_pset
);
4167 /* Or we could just let it change to starting_pset in the loop above */
4168 pset
= change_locked_pset(pset
, starting_pset
);
4173 * Choose an idle processor, in pset traversal order
4176 uint64_t idle_primary_map
= (pset
->cpu_state_map
[PROCESSOR_IDLE
] &
4178 pset
->recommended_bitmask
);
4180 /* there shouldn't be a pending AST if the processor is idle */
4181 assert((idle_primary_map
& pset
->pending_AST_URGENT_cpu_mask
) == 0);
4183 int cpuid
= lsb_first(idle_primary_map
);
4185 processor
= processor_array
[cpuid
];
4190 * Otherwise, enumerate active and idle processors to find primary candidates
4191 * with lower priority/etc.
4194 uint64_t active_map
= ((pset
->cpu_state_map
[PROCESSOR_RUNNING
] | pset
->cpu_state_map
[PROCESSOR_DISPATCHING
]) &
4195 pset
->recommended_bitmask
&
4196 ~pset
->pending_AST_URGENT_cpu_mask
);
4198 if (SCHED(priority_is_urgent
)(thread
->sched_pri
) == FALSE
) {
4199 active_map
&= ~pset
->pending_AST_PREEMPT_cpu_mask
;
4202 active_map
= bit_ror64(active_map
, (pset
->last_chosen
+ 1));
4203 for (int rotid
= lsb_first(active_map
); rotid
>= 0; rotid
= lsb_next(active_map
, rotid
)) {
4204 cpuid
= ((rotid
+ pset
->last_chosen
+ 1) & 63);
4205 processor
= processor_array
[cpuid
];
4207 integer_t cpri
= processor
->current_pri
;
4208 processor_t primary
= processor
->processor_primary
;
4209 if (primary
!= processor
) {
4210 /* If primary is running a NO_SMT thread, don't choose its secondary */
4211 if (!((primary
->state
== PROCESSOR_RUNNING
) && processor_active_thread_no_smt(primary
))) {
4212 if (cpri
< lowest_secondary_priority
) {
4213 lowest_secondary_priority
= cpri
;
4214 lp_paired_secondary_processor
= processor
;
4218 if (cpri
< lowest_priority
) {
4219 lowest_priority
= cpri
;
4220 lp_processor
= processor
;
4224 if ((cpri
>= BASEPRI_RTQUEUES
) && (processor
->deadline
> furthest_deadline
)) {
4225 furthest_deadline
= processor
->deadline
;
4226 fd_processor
= processor
;
4229 integer_t ccount
= SCHED(processor_runq_count
)(processor
);
4230 if (ccount
< lowest_count
) {
4231 lowest_count
= ccount
;
4232 lc_processor
= processor
;
4237 * For SMT configs, these idle secondary processors must have active primary. Otherwise
4238 * the idle primary would have short-circuited the loop above
4240 uint64_t idle_secondary_map
= (pset
->cpu_state_map
[PROCESSOR_IDLE
] &
4241 ~pset
->primary_map
&
4242 pset
->recommended_bitmask
);
4244 /* there shouldn't be a pending AST if the processor is idle */
4245 assert((idle_secondary_map
& pset
->pending_AST_URGENT_cpu_mask
) == 0);
4246 assert((idle_secondary_map
& pset
->pending_AST_PREEMPT_cpu_mask
) == 0);
4248 for (cpuid
= lsb_first(idle_secondary_map
); cpuid
>= 0; cpuid
= lsb_next(idle_secondary_map
, cpuid
)) {
4249 processor
= processor_array
[cpuid
];
4251 processor_t cprimary
= processor
->processor_primary
;
4253 integer_t primary_pri
= cprimary
->current_pri
;
4256 * TODO: This should also make the same decisions
4257 * as secondary_can_run_realtime_thread
4259 * TODO: Keep track of the pending preemption priority
4260 * of the primary to make this more accurate.
4263 /* If the primary is running a no-smt thread, then don't choose its secondary */
4264 if (cprimary
->state
== PROCESSOR_RUNNING
&&
4265 processor_active_thread_no_smt(cprimary
)) {
4270 * Find the idle secondary processor with the lowest priority primary
4272 * We will choose this processor as a fallback if we find no better
4273 * primary to preempt.
4275 if (primary_pri
< lowest_idle_secondary_priority
) {
4276 lp_idle_secondary_processor
= processor
;
4277 lowest_idle_secondary_priority
= primary_pri
;
4280 /* Find the the lowest priority active primary with idle secondary */
4281 if (primary_pri
< lowest_unpaired_primary_priority
) {
4282 /* If the primary processor is offline or starting up, it's not a candidate for this path */
4283 if (cprimary
->state
!= PROCESSOR_RUNNING
&&
4284 cprimary
->state
!= PROCESSOR_DISPATCHING
) {
4288 if (!cprimary
->is_recommended
) {
4292 /* if the primary is pending preemption, don't try to re-preempt it */
4293 if (bit_test(pset
->pending_AST_URGENT_cpu_mask
, cprimary
->cpu_id
)) {
4297 if (SCHED(priority_is_urgent
)(thread
->sched_pri
) == FALSE
&&
4298 bit_test(pset
->pending_AST_PREEMPT_cpu_mask
, cprimary
->cpu_id
)) {
4302 lowest_unpaired_primary_priority
= primary_pri
;
4303 lp_unpaired_primary_processor
= cprimary
;
4308 * We prefer preempting a primary processor over waking up its secondary.
4309 * The secondary will then be woken up by the preempted thread.
4311 if (thread
->sched_pri
> lowest_unpaired_primary_priority
) {
4312 pset
->last_chosen
= lp_unpaired_primary_processor
->cpu_id
;
4313 return lp_unpaired_primary_processor
;
4317 * We prefer preempting a lower priority active processor over directly
4318 * waking up an idle secondary.
4319 * The preempted thread will then find the idle secondary.
4321 if (thread
->sched_pri
> lowest_priority
) {
4322 pset
->last_chosen
= lp_processor
->cpu_id
;
4323 return lp_processor
;
4326 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
4328 * For realtime threads, the most important aspect is
4329 * scheduling latency, so we will pick an active
4330 * secondary processor in this pset, or preempt
4331 * another RT thread with a further deadline before
4332 * going to the next pset.
4335 if (sched_allow_rt_smt
&& (thread
->sched_pri
> lowest_secondary_priority
)) {
4336 pset
->last_chosen
= lp_paired_secondary_processor
->cpu_id
;
4337 return lp_paired_secondary_processor
;
4340 if (thread
->realtime
.deadline
< furthest_deadline
) {
4341 return fd_processor
;
4346 * lc_processor is used to indicate the best processor set run queue
4347 * on which to enqueue a thread when all available CPUs are busy with
4348 * higher priority threads, so try to make sure it is initialized.
4350 if (lc_processor
== PROCESSOR_NULL
) {
4351 cpumap_t available_map
= ((pset
->cpu_state_map
[PROCESSOR_IDLE
] |
4352 pset
->cpu_state_map
[PROCESSOR_RUNNING
] |
4353 pset
->cpu_state_map
[PROCESSOR_DISPATCHING
]) &
4354 pset
->recommended_bitmask
);
4355 cpuid
= lsb_first(available_map
);
4357 lc_processor
= processor_array
[cpuid
];
4358 lowest_count
= SCHED(processor_runq_count
)(lc_processor
);
4363 * Move onto the next processor set.
4365 * If all primary processors in this pset are running a higher
4366 * priority thread, move on to next pset. Only when we have
4367 * exhausted the search for primary processors do we
4368 * fall back to secondaries.
4370 nset
= next_pset(pset
);
4372 if (nset
!= starting_pset
) {
4373 pset
= change_locked_pset(pset
, nset
);
4375 } while (nset
!= starting_pset
);
4378 * Make sure that we pick a running processor,
4379 * and that the correct processor set is locked.
4380 * Since we may have unlocked the candidate processor's
4381 * pset, it may have changed state.
4383 * All primary processors are running a higher priority
4384 * thread, so the only options left are enqueuing on
4385 * the secondary processor that would perturb the least priority
4386 * primary, or the least busy primary.
4388 boolean_t fallback_processor
= false;
4390 /* lowest_priority is evaluated in the main loops above */
4391 if (lp_idle_secondary_processor
!= PROCESSOR_NULL
) {
4392 processor
= lp_idle_secondary_processor
;
4393 lp_idle_secondary_processor
= PROCESSOR_NULL
;
4394 } else if (lp_paired_secondary_processor
!= PROCESSOR_NULL
) {
4395 processor
= lp_paired_secondary_processor
;
4396 lp_paired_secondary_processor
= PROCESSOR_NULL
;
4397 } else if (lc_processor
!= PROCESSOR_NULL
) {
4398 processor
= lc_processor
;
4399 lc_processor
= PROCESSOR_NULL
;
4402 * All processors are executing higher priority threads, and
4403 * the lowest_count candidate was not usable.
4405 * For AMP platforms running the clutch scheduler always
4406 * return a processor from the requested pset to allow the
4407 * thread to be enqueued in the correct runq. For non-AMP
4408 * platforms, simply return the master_processor.
4410 fallback_processor
= true;
4411 #if CONFIG_SCHED_EDGE
4412 processor
= processor_array
[lsb_first(starting_pset
->primary_map
)];
4413 #else /* CONFIG_SCHED_EDGE */
4414 processor
= master_processor
;
4415 #endif /* CONFIG_SCHED_EDGE */
4419 * Check that the correct processor set is
4422 pset
= change_locked_pset(pset
, processor
->processor_set
);
4425 * We must verify that the chosen processor is still available.
4426 * The cases where we pick the master_processor or the fallback
4427 * processor are execptions, since we may need enqueue a thread
4428 * on its runqueue if this is the last remaining processor
4429 * during pset shutdown.
4431 * <rdar://problem/47559304> would really help here since it
4432 * gets rid of the weird last processor SHUTDOWN case where
4433 * the pset is still schedulable.
4435 if (processor
!= master_processor
&& (fallback_processor
== false) && (processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)) {
4436 processor
= PROCESSOR_NULL
;
4438 } while (processor
== PROCESSOR_NULL
);
4440 pset
->last_chosen
= processor
->cpu_id
;
4445 * Default implementation of SCHED(choose_node)()
4446 * for single node systems
4449 sched_choose_node(__unused thread_t thread
)
4455 * choose_starting_pset:
4457 * Choose a starting processor set for the thread.
4458 * May return a processor hint within the pset.
4460 * Returns a starting processor set, to be used by
4463 * The thread must be locked. The resulting pset is unlocked on return,
4464 * and is chosen without taking any pset locks.
4467 choose_starting_pset(pset_node_t node
, thread_t thread
, processor_t
*processor_hint
)
4469 processor_set_t pset
;
4470 processor_t processor
= PROCESSOR_NULL
;
4472 if (thread
->affinity_set
!= AFFINITY_SET_NULL
) {
4474 * Use affinity set policy hint.
4476 pset
= thread
->affinity_set
->aset_pset
;
4477 } else if (thread
->last_processor
!= PROCESSOR_NULL
) {
4479 * Simple (last processor) affinity case.
4481 processor
= thread
->last_processor
;
4482 pset
= processor
->processor_set
;
4487 * Utilitize a per task hint to spread threads
4488 * among the available processor sets.
4489 * NRG this seems like the wrong thing to do.
4490 * See also task->pset_hint = pset in thread_setrun()
4492 task_t task
= thread
->task
;
4494 pset
= task
->pset_hint
;
4495 if (pset
== PROCESSOR_SET_NULL
) {
4496 pset
= current_processor()->processor_set
;
4499 pset
= choose_next_pset(pset
);
4502 if (!bit_test(node
->pset_map
, pset
->pset_id
)) {
4503 /* pset is not from this node so choose one that is */
4504 int id
= lsb_first(node
->pset_map
);
4506 pset
= pset_array
[id
];
4509 if (bit_count(node
->pset_map
) == 1) {
4510 /* Only a single pset in this node */
4514 bool avoid_cpu0
= false;
4516 #if defined(__x86_64__)
4517 if ((thread
->sched_pri
>= BASEPRI_RTQUEUES
) && sched_avoid_cpu0
) {
4518 /* Avoid the pset containing cpu0 */
4520 /* Assert that cpu0 is in pset0. I expect this to be true on __x86_64__ */
4521 assert(bit_test(pset_array
[0]->cpu_bitmask
, 0));
4525 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
4526 pset_map_t rt_target_map
= atomic_load(&node
->pset_non_rt_primary_map
);
4527 if ((avoid_cpu0
&& pset
->pset_id
== 0) || !bit_test(rt_target_map
, pset
->pset_id
)) {
4529 rt_target_map
= bit_ror64(rt_target_map
, 1);
4531 int rotid
= lsb_first(rt_target_map
);
4533 int id
= avoid_cpu0
? ((rotid
+ 1) & 63) : rotid
;
4534 pset
= pset_array
[id
];
4538 if (!pset
->is_SMT
|| !sched_allow_rt_smt
) {
4539 /* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
4542 rt_target_map
= atomic_load(&node
->pset_non_rt_map
);
4543 if ((avoid_cpu0
&& pset
->pset_id
== 0) || !bit_test(rt_target_map
, pset
->pset_id
)) {
4545 rt_target_map
= bit_ror64(rt_target_map
, 1);
4547 int rotid
= lsb_first(rt_target_map
);
4549 int id
= avoid_cpu0
? ((rotid
+ 1) & 63) : rotid
;
4550 pset
= pset_array
[id
];
4554 /* All psets are full of RT threads - fall back to choose processor to find the furthest deadline RT thread */
4556 pset_map_t idle_map
= atomic_load(&node
->pset_idle_map
);
4557 if (!bit_test(idle_map
, pset
->pset_id
)) {
4558 int next_idle_pset_id
= lsb_first(idle_map
);
4559 if (next_idle_pset_id
>= 0) {
4560 pset
= pset_array
[next_idle_pset_id
];
4566 if ((processor
!= PROCESSOR_NULL
) && (processor
->processor_set
!= pset
)) {
4567 processor
= PROCESSOR_NULL
;
4569 if (processor
!= PROCESSOR_NULL
) {
4570 *processor_hint
= processor
;
4579 * Dispatch thread for execution, onto an idle
4580 * processor or run queue, and signal a preemption
4583 * Thread must be locked.
4588 sched_options_t options
)
4590 processor_t processor
;
4591 processor_set_t pset
;
4593 assert((thread
->state
& (TH_RUN
| TH_WAIT
| TH_UNINT
| TH_TERMINATE
| TH_TERMINATE2
)) == TH_RUN
);
4594 assert(thread
->runq
== PROCESSOR_NULL
);
4597 * Update priority if needed.
4599 if (SCHED(can_update_priority
)(thread
)) {
4600 SCHED(update_priority
)(thread
);
4603 thread
->sfi_class
= sfi_thread_classify(thread
);
4605 assert(thread
->runq
== PROCESSOR_NULL
);
4607 if (thread
->bound_processor
== PROCESSOR_NULL
) {
4611 processor_t processor_hint
= PROCESSOR_NULL
;
4612 pset_node_t node
= SCHED(choose_node
)(thread
);
4613 processor_set_t starting_pset
= choose_starting_pset(node
, thread
, &processor_hint
);
4615 pset_lock(starting_pset
);
4617 processor
= SCHED(choose_processor
)(starting_pset
, processor_hint
, thread
);
4618 pset
= processor
->processor_set
;
4619 task_t task
= thread
->task
;
4620 task
->pset_hint
= pset
; /* NRG this is done without holding the task lock */
4622 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHOOSE_PROCESSOR
) | DBG_FUNC_NONE
,
4623 (uintptr_t)thread_tid(thread
), (uintptr_t)-1, processor
->cpu_id
, processor
->state
, 0);
4628 * Unconditionally dispatch on the processor.
4630 processor
= thread
->bound_processor
;
4631 pset
= processor
->processor_set
;
4634 SCHED_DEBUG_CHOOSE_PROCESSOR_KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHOOSE_PROCESSOR
) | DBG_FUNC_NONE
,
4635 (uintptr_t)thread_tid(thread
), (uintptr_t)-2, processor
->cpu_id
, processor
->state
, 0);
4639 * Dispatch the thread on the chosen processor.
4640 * TODO: This should be based on sched_mode, not sched_pri
4642 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
4643 realtime_setrun(processor
, thread
);
4645 processor_setrun(processor
, thread
, options
);
4647 /* pset is now unlocked */
4648 if (thread
->bound_processor
== PROCESSOR_NULL
) {
4649 SCHED(check_spill
)(pset
, thread
);
4657 processor_set_t pset
= task
->pset_hint
;
4659 if (pset
!= PROCESSOR_SET_NULL
) {
4660 pset
= choose_next_pset(pset
);
4667 * Check for a preemption point in
4668 * the current context.
4670 * Called at splsched with thread locked.
4675 processor_t processor
,
4678 processor_set_t pset
= processor
->processor_set
;
4680 assert(thread
== processor
->active_thread
);
4684 processor_state_update_from_thread(processor
, thread
);
4686 ast_t preempt
= csw_check_locked(thread
, processor
, pset
, check_reason
);
4688 /* Acknowledge the IPI if we decided not to preempt */
4690 if ((preempt
& AST_URGENT
) == 0) {
4691 bit_clear(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
);
4694 if ((preempt
& AST_PREEMPT
) == 0) {
4695 bit_clear(pset
->pending_AST_PREEMPT_cpu_mask
, processor
->cpu_id
);
4704 * Check for preemption at splsched with
4705 * pset and thread locked
4710 processor_t processor
,
4711 processor_set_t pset
,
4716 if (processor
->first_timeslice
) {
4717 if (rt_runq_count(pset
) > 0) {
4718 return check_reason
| AST_PREEMPT
| AST_URGENT
;
4721 if (rt_runq_count(pset
) > 0) {
4722 if (BASEPRI_RTQUEUES
> processor
->current_pri
) {
4723 return check_reason
| AST_PREEMPT
| AST_URGENT
;
4725 return check_reason
| AST_PREEMPT
;
4731 * If the current thread is running on a processor that is no longer recommended,
4732 * urgently preempt it, at which point thread_select() should
4733 * try to idle the processor and re-dispatch the thread to a recommended processor.
4735 if (!processor
->is_recommended
) {
4736 return check_reason
| AST_PREEMPT
| AST_URGENT
;
4739 result
= SCHED(processor_csw_check
)(processor
);
4740 if (result
!= AST_NONE
) {
4741 return check_reason
| result
| (thread_eager_preemption(thread
) ? AST_URGENT
: AST_NONE
);
4745 * Same for avoid-processor
4747 * TODO: Should these set AST_REBALANCE?
4749 if (SCHED(avoid_processor_enabled
) && SCHED(thread_avoid_processor
)(processor
, thread
)) {
4750 return check_reason
| AST_PREEMPT
;
4754 * Even though we could continue executing on this processor, a
4755 * secondary SMT core should try to shed load to another primary core.
4757 * TODO: Should this do the same check that thread_select does? i.e.
4758 * if no bound threads target this processor, and idle primaries exist, preempt
4759 * The case of RT threads existing is already taken care of above
4762 if (processor
->current_pri
< BASEPRI_RTQUEUES
&&
4763 processor
->processor_primary
!= processor
) {
4764 return check_reason
| AST_PREEMPT
;
4767 if (thread
->state
& TH_SUSP
) {
4768 return check_reason
| AST_PREEMPT
;
4771 #if CONFIG_SCHED_SFI
4773 * Current thread may not need to be preempted, but maybe needs
4776 result
= sfi_thread_needs_ast(thread
, NULL
);
4777 if (result
!= AST_NONE
) {
4778 return check_reason
| result
;
4786 * Handle preemption IPI or IPI in response to setting an AST flag
4787 * Triggered by cause_ast_check
4788 * Called at splsched
4791 ast_check(processor_t processor
)
4793 if (processor
->state
!= PROCESSOR_RUNNING
&&
4794 processor
->state
!= PROCESSOR_SHUTDOWN
) {
4798 thread_t thread
= processor
->active_thread
;
4800 assert(thread
== current_thread());
4802 thread_lock(thread
);
4805 * Propagate thread ast to processor.
4806 * (handles IPI in response to setting AST flag)
4808 ast_propagate(thread
);
4811 * Stash the old urgency and perfctl values to find out if
4812 * csw_check updates them.
4814 thread_urgency_t old_urgency
= processor
->current_urgency
;
4815 perfcontrol_class_t old_perfctl_class
= processor
->current_perfctl_class
;
4819 if ((preempt
= csw_check(thread
, processor
, AST_NONE
)) != AST_NONE
) {
4823 if (old_urgency
!= processor
->current_urgency
) {
4825 * Urgency updates happen with the thread lock held (ugh).
4826 * TODO: This doesn't notice QoS changes...
4828 uint64_t urgency_param1
, urgency_param2
;
4830 thread_urgency_t urgency
= thread_get_urgency(thread
, &urgency_param1
, &urgency_param2
);
4831 thread_tell_urgency(urgency
, urgency_param1
, urgency_param2
, 0, thread
);
4834 thread_unlock(thread
);
4836 if (old_perfctl_class
!= processor
->current_perfctl_class
) {
4838 * We updated the perfctl class of this thread from another core.
4839 * Let CLPC know that the currently running thread has a new
4843 machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE
,
4844 mach_approximate_time(), 0, thread
);
4852 * Set the scheduled priority of the specified thread.
4854 * This may cause the thread to change queues.
4856 * Thread must be locked.
4861 int16_t new_priority
,
4862 set_sched_pri_options_t options
)
4864 bool is_current_thread
= (thread
== current_thread());
4865 bool removed_from_runq
= false;
4866 bool lazy_update
= ((options
& SETPRI_LAZY
) == SETPRI_LAZY
);
4868 int16_t old_priority
= thread
->sched_pri
;
4870 /* If we're already at this priority, no need to mess with the runqueue */
4871 if (new_priority
== old_priority
) {
4872 #if CONFIG_SCHED_CLUTCH
4873 /* For the first thread in the system, the priority is correct but
4874 * th_sched_bucket is still TH_BUCKET_RUN. Since the clutch
4875 * scheduler relies on the bucket being set for all threads, update
4878 if (thread
->th_sched_bucket
== TH_BUCKET_RUN
) {
4879 assert(is_current_thread
);
4880 SCHED(update_thread_bucket
)(thread
);
4882 #endif /* CONFIG_SCHED_CLUTCH */
4887 if (is_current_thread
) {
4888 assert(thread
->state
& TH_RUN
);
4889 assert(thread
->runq
== PROCESSOR_NULL
);
4891 removed_from_runq
= thread_run_queue_remove(thread
);
4894 thread
->sched_pri
= new_priority
;
4896 #if CONFIG_SCHED_CLUTCH
4898 * Since for the clutch scheduler, the thread's bucket determines its runq
4899 * in the hierarchy it is important to update the bucket when the thread
4900 * lock is held and the thread has been removed from the runq hierarchy.
4902 SCHED(update_thread_bucket
)(thread
);
4904 #endif /* CONFIG_SCHED_CLUTCH */
4906 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHANGE_PRIORITY
),
4907 (uintptr_t)thread_tid(thread
),
4910 thread
->sched_usage
,
4913 if (removed_from_runq
) {
4914 thread_run_queue_reinsert(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
4915 } else if (is_current_thread
) {
4916 processor_t processor
= thread
->last_processor
;
4917 assert(processor
== current_processor());
4919 thread_urgency_t old_urgency
= processor
->current_urgency
;
4922 * When dropping in priority, check if the thread no longer belongs on core.
4923 * If a thread raises its own priority, don't aggressively rebalance it.
4924 * <rdar://problem/31699165>
4926 * csw_check does a processor_state_update_from_thread, but
4927 * we should do our own if we're being lazy.
4929 if (!lazy_update
&& new_priority
< old_priority
) {
4932 if ((preempt
= csw_check(thread
, processor
, AST_NONE
)) != AST_NONE
) {
4936 processor_state_update_from_thread(processor
, thread
);
4940 * set_sched_pri doesn't alter RT params. We expect direct base priority/QoS
4941 * class alterations from user space to occur relatively infrequently, hence
4942 * those are lazily handled. QoS classes have distinct priority bands, and QoS
4943 * inheritance is expected to involve priority changes.
4945 if (processor
->current_urgency
!= old_urgency
) {
4946 uint64_t urgency_param1
, urgency_param2
;
4948 thread_urgency_t new_urgency
= thread_get_urgency(thread
,
4949 &urgency_param1
, &urgency_param2
);
4951 thread_tell_urgency(new_urgency
, urgency_param1
,
4952 urgency_param2
, 0, thread
);
4955 /* TODO: only call this if current_perfctl_class changed */
4956 uint64_t ctime
= mach_approximate_time();
4957 machine_thread_going_on_core(thread
, processor
->current_urgency
, 0, 0, ctime
);
4958 } else if (thread
->state
& TH_RUN
) {
4959 processor_t processor
= thread
->last_processor
;
4962 processor
!= PROCESSOR_NULL
&&
4963 processor
!= current_processor() &&
4964 processor
->active_thread
== thread
) {
4965 cause_ast_check(processor
);
4971 * thread_run_queue_remove_for_handoff
4973 * Pull a thread or its (recursive) push target out of the runqueue
4974 * so that it is ready for thread_run()
4976 * Called at splsched
4978 * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
4979 * This may be different than the thread that was passed in.
4982 thread_run_queue_remove_for_handoff(thread_t thread
)
4984 thread_t pulled_thread
= THREAD_NULL
;
4986 thread_lock(thread
);
4989 * Check that the thread is not bound to a different processor,
4990 * NO_SMT flag is not set on the thread, cluster type of
4991 * processor matches with thread if the thread is pinned to a
4992 * particular cluster and that realtime is not involved.
4994 * Next, pull it off its run queue. If it doesn't come, it's not eligible.
4996 processor_t processor
= current_processor();
4997 if ((thread
->bound_processor
== PROCESSOR_NULL
|| thread
->bound_processor
== processor
)
4998 && (!thread_no_smt(thread
))
4999 && (processor
->current_pri
< BASEPRI_RTQUEUES
)
5000 && (thread
->sched_pri
< BASEPRI_RTQUEUES
)
5002 && ((!(thread
->sched_flags
& TH_SFLAG_PCORE_ONLY
)) ||
5003 processor
->processor_set
->pset_cluster_type
== PSET_AMP_P
)
5004 && ((!(thread
->sched_flags
& TH_SFLAG_ECORE_ONLY
)) ||
5005 processor
->processor_set
->pset_cluster_type
== PSET_AMP_E
)
5006 #endif /* __AMP__ */
5008 if (thread_run_queue_remove(thread
)) {
5009 pulled_thread
= thread
;
5013 thread_unlock(thread
);
5015 return pulled_thread
;
5019 * thread_prepare_for_handoff
5021 * Make the thread ready for handoff.
5022 * If the thread was runnable then pull it off the runq, if the thread could
5023 * not be pulled, return NULL.
5025 * If the thread was woken up from wait for handoff, make sure it is not bound to
5026 * different processor.
5028 * Called at splsched
5030 * Returns the thread that was pulled or THREAD_NULL if no thread could be pulled.
5031 * This may be different than the thread that was passed in.
5034 thread_prepare_for_handoff(thread_t thread
, thread_handoff_option_t option
)
5036 thread_t pulled_thread
= THREAD_NULL
;
5038 if (option
& THREAD_HANDOFF_SETRUN_NEEDED
) {
5039 processor_t processor
= current_processor();
5040 thread_lock(thread
);
5043 * Check that the thread is not bound to a different processor,
5044 * NO_SMT flag is not set on the thread and cluster type of
5045 * processor matches with thread if the thread is pinned to a
5046 * particular cluster. Call setrun instead if above conditions
5047 * are not satisfied.
5049 if ((thread
->bound_processor
== PROCESSOR_NULL
|| thread
->bound_processor
== processor
)
5050 && (!thread_no_smt(thread
))
5052 && ((!(thread
->sched_flags
& TH_SFLAG_PCORE_ONLY
)) ||
5053 processor
->processor_set
->pset_cluster_type
== PSET_AMP_P
)
5054 && ((!(thread
->sched_flags
& TH_SFLAG_ECORE_ONLY
)) ||
5055 processor
->processor_set
->pset_cluster_type
== PSET_AMP_E
)
5056 #endif /* __AMP__ */
5058 pulled_thread
= thread
;
5060 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
5062 thread_unlock(thread
);
5064 pulled_thread
= thread_run_queue_remove_for_handoff(thread
);
5067 return pulled_thread
;
5071 * thread_run_queue_remove:
5073 * Remove a thread from its current run queue and
5074 * return TRUE if successful.
5076 * Thread must be locked.
5078 * If thread->runq is PROCESSOR_NULL, the thread will not re-enter the
5079 * run queues because the caller locked the thread. Otherwise
5080 * the thread is on a run queue, but could be chosen for dispatch
5081 * and removed by another processor under a different lock, which
5082 * will set thread->runq to PROCESSOR_NULL.
5084 * Hence the thread select path must not rely on anything that could
5085 * be changed under the thread lock after calling this function,
5086 * most importantly thread->sched_pri.
5089 thread_run_queue_remove(
5092 boolean_t removed
= FALSE
;
5093 processor_t processor
= thread
->runq
;
5095 if ((thread
->state
& (TH_RUN
| TH_WAIT
)) == TH_WAIT
) {
5096 /* Thread isn't runnable */
5097 assert(thread
->runq
== PROCESSOR_NULL
);
5101 if (processor
== PROCESSOR_NULL
) {
5103 * The thread is either not on the runq,
5104 * or is in the midst of being removed from the runq.
5106 * runq is set to NULL under the pset lock, not the thread
5107 * lock, so the thread may still be in the process of being dequeued
5108 * from the runq. It will wait in invoke for the thread lock to be
5115 if (thread
->sched_pri
< BASEPRI_RTQUEUES
) {
5116 return SCHED(processor_queue_remove
)(processor
, thread
);
5119 processor_set_t pset
= processor
->processor_set
;
5123 if (thread
->runq
!= PROCESSOR_NULL
) {
5125 * Thread is on the RT run queue and we have a lock on
5129 remqueue(&thread
->runq_links
);
5130 SCHED_STATS_RUNQ_CHANGE(&SCHED(rt_runq
)(pset
)->runq_stats
, rt_runq_count(pset
));
5131 rt_runq_count_decr(pset
);
5133 thread
->runq
= PROCESSOR_NULL
;
5144 * Put the thread back where it goes after a thread_run_queue_remove
5146 * Thread must have been removed under the same thread lock hold
5148 * thread locked, at splsched
5151 thread_run_queue_reinsert(thread_t thread
, sched_options_t options
)
5153 assert(thread
->runq
== PROCESSOR_NULL
);
5154 assert(thread
->state
& (TH_RUN
));
5156 thread_setrun(thread
, options
);
5160 sys_override_cpu_throttle(boolean_t enable_override
)
5162 if (enable_override
) {
5163 cpu_throttle_enabled
= 0;
5165 cpu_throttle_enabled
= 1;
5170 thread_get_urgency(thread_t thread
, uint64_t *arg1
, uint64_t *arg2
)
5172 uint64_t urgency_param1
= 0, urgency_param2
= 0;
5174 thread_urgency_t urgency
;
5176 if (thread
== NULL
|| (thread
->state
& TH_IDLE
)) {
5180 urgency
= THREAD_URGENCY_NONE
;
5181 } else if (thread
->sched_mode
== TH_MODE_REALTIME
) {
5182 urgency_param1
= thread
->realtime
.period
;
5183 urgency_param2
= thread
->realtime
.deadline
;
5185 urgency
= THREAD_URGENCY_REAL_TIME
;
5186 } else if (cpu_throttle_enabled
&&
5187 (thread
->sched_pri
<= MAXPRI_THROTTLE
) &&
5188 (thread
->base_pri
<= MAXPRI_THROTTLE
)) {
5190 * Threads that are running at low priority but are not
5191 * tagged with a specific QoS are separated out from
5192 * the "background" urgency. Performance management
5193 * subsystem can decide to either treat these threads
5194 * as normal threads or look at other signals like thermal
5195 * levels for optimal power/perf tradeoffs for a platform.
5197 boolean_t thread_lacks_qos
= (proc_get_effective_thread_policy(thread
, TASK_POLICY_QOS
) == THREAD_QOS_UNSPECIFIED
); //thread_has_qos_policy(thread);
5198 boolean_t task_is_suppressed
= (proc_get_effective_task_policy(thread
->task
, TASK_POLICY_SUP_ACTIVE
) == 0x1);
5201 * Background urgency applied when thread priority is
5202 * MAXPRI_THROTTLE or lower and thread is not promoted
5203 * and thread has a QoS specified
5205 urgency_param1
= thread
->sched_pri
;
5206 urgency_param2
= thread
->base_pri
;
5208 if (thread_lacks_qos
&& !task_is_suppressed
) {
5209 urgency
= THREAD_URGENCY_LOWPRI
;
5211 urgency
= THREAD_URGENCY_BACKGROUND
;
5214 /* For otherwise unclassified threads, report throughput QoS parameters */
5215 urgency_param1
= proc_get_effective_thread_policy(thread
, TASK_POLICY_THROUGH_QOS
);
5216 urgency_param2
= proc_get_effective_task_policy(thread
->task
, TASK_POLICY_THROUGH_QOS
);
5217 urgency
= THREAD_URGENCY_NORMAL
;
5221 *arg1
= urgency_param1
;
5224 *arg2
= urgency_param2
;
5231 thread_get_perfcontrol_class(thread_t thread
)
5233 /* Special case handling */
5234 if (thread
->state
& TH_IDLE
) {
5235 return PERFCONTROL_CLASS_IDLE
;
5237 if (thread
->task
== kernel_task
) {
5238 return PERFCONTROL_CLASS_KERNEL
;
5240 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
5241 return PERFCONTROL_CLASS_REALTIME
;
5244 /* perfcontrol_class based on base_pri */
5245 if (thread
->base_pri
<= MAXPRI_THROTTLE
) {
5246 return PERFCONTROL_CLASS_BACKGROUND
;
5247 } else if (thread
->base_pri
<= BASEPRI_UTILITY
) {
5248 return PERFCONTROL_CLASS_UTILITY
;
5249 } else if (thread
->base_pri
<= BASEPRI_DEFAULT
) {
5250 return PERFCONTROL_CLASS_NONUI
;
5251 } else if (thread
->base_pri
<= BASEPRI_FOREGROUND
) {
5252 return PERFCONTROL_CLASS_UI
;
5254 return PERFCONTROL_CLASS_ABOVEUI
;
5259 * This is the processor idle loop, which just looks for other threads
5260 * to execute. Processor idle threads invoke this without supplying a
5261 * current thread to idle without an asserted wait state.
5263 * Returns a the next thread to execute if dispatched directly.
5267 #define IDLE_KERNEL_DEBUG_CONSTANT(...) KERNEL_DEBUG_CONSTANT(__VA_ARGS__)
5269 #define IDLE_KERNEL_DEBUG_CONSTANT(...) do { } while(0)
5275 processor_t processor
)
5277 processor_set_t pset
= processor
->processor_set
;
5281 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5282 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_IDLE
) | DBG_FUNC_START
,
5283 (uintptr_t)thread_tid(thread
), 0, 0, 0, 0);
5285 SCHED_STATS_INC(idle_transitions
);
5286 assert(processor
->running_timers_active
== false);
5288 uint64_t ctime
= mach_absolute_time();
5290 timer_switch(&processor
->system_state
, ctime
, &processor
->idle_state
);
5291 processor
->current_state
= &processor
->idle_state
;
5293 cpu_quiescent_counter_leave(ctime
);
5297 * Ensure that updates to my processor and pset state,
5298 * made by the IPI source processor before sending the IPI,
5299 * are visible on this processor now (even though we don't
5300 * take the pset lock yet).
5302 atomic_thread_fence(memory_order_acquire
);
5304 if (processor
->state
!= PROCESSOR_IDLE
) {
5307 if (bit_test(pset
->pending_AST_URGENT_cpu_mask
, processor
->cpu_id
)) {
5310 #if defined(CONFIG_SCHED_DEFERRED_AST)
5311 if (bit_test(pset
->pending_deferred_AST_cpu_mask
, processor
->cpu_id
)) {
5315 if (processor
->is_recommended
&& (processor
->processor_primary
== processor
)) {
5316 if (rt_runq_count(pset
)) {
5320 if (SCHED(processor_bound_count
)(processor
)) {
5325 IDLE_KERNEL_DEBUG_CONSTANT(
5326 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_IDLE
) | DBG_FUNC_NONE
, (uintptr_t)thread_tid(thread
), rt_runq_count(pset
), SCHED(processor_runq_count
)(processor
), -1, 0);
5328 machine_track_platform_idle(TRUE
);
5331 /* returns with interrupts enabled */
5333 machine_track_platform_idle(FALSE
);
5338 * Check if we should call sched_timeshare_consider_maintenance() here.
5339 * The CPU was woken out of idle due to an interrupt and we should do the
5340 * call only if the processor is still idle. If the processor is non-idle,
5341 * the threads running on the processor would do the call as part of
5344 if (processor
->state
== PROCESSOR_IDLE
) {
5345 sched_timeshare_consider_maintenance(mach_absolute_time());
5348 IDLE_KERNEL_DEBUG_CONSTANT(
5349 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_IDLE
) | DBG_FUNC_NONE
, (uintptr_t)thread_tid(thread
), rt_runq_count(pset
), SCHED(processor_runq_count
)(processor
), -2, 0);
5351 if (!SCHED(processor_queue_empty
)(processor
)) {
5352 /* Secondary SMT processors respond to directed wakeups
5353 * exclusively. Some platforms induce 'spurious' SMT wakeups.
5355 if (processor
->processor_primary
== processor
) {
5361 ctime
= mach_absolute_time();
5363 timer_switch(&processor
->idle_state
, ctime
, &processor
->system_state
);
5364 processor
->current_state
= &processor
->system_state
;
5366 cpu_quiescent_counter_join(ctime
);
5368 ast_t reason
= AST_NONE
;
5370 /* We're handling all scheduling AST's */
5371 ast_off(AST_SCHEDULING
);
5374 * thread_select will move the processor from dispatching to running,
5375 * or put it in idle if there's nothing to do.
5377 thread_t current_thread
= current_thread();
5379 thread_lock(current_thread
);
5380 thread_t new_thread
= thread_select(current_thread
, processor
, &reason
);
5381 thread_unlock(current_thread
);
5383 assert(processor
->running_timers_active
== false);
5385 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
5386 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_IDLE
) | DBG_FUNC_END
,
5387 (uintptr_t)thread_tid(thread
), processor
->state
, (uintptr_t)thread_tid(new_thread
), reason
, 0);
5393 * Each processor has a dedicated thread which
5394 * executes the idle loop when there is no suitable
5397 * This continuation is entered with interrupts disabled.
5400 idle_thread(__assert_only
void* parameter
,
5401 __unused wait_result_t result
)
5403 assert(ml_get_interrupts_enabled() == FALSE
);
5404 assert(parameter
== NULL
);
5406 processor_t processor
= current_processor();
5409 * Ensure that anything running in idle context triggers
5410 * preemption-disabled checks.
5412 disable_preemption();
5415 * Enable interrupts temporarily to handle any pending interrupts
5416 * or IPIs before deciding to sleep
5420 thread_t new_thread
= processor_idle(THREAD_NULL
, processor
);
5421 /* returns with interrupts disabled */
5423 enable_preemption();
5425 if (new_thread
!= THREAD_NULL
) {
5426 thread_run(processor
->idle_thread
,
5427 idle_thread
, NULL
, new_thread
);
5431 thread_block(idle_thread
);
5437 processor_t processor
)
5439 kern_return_t result
;
5442 char name
[MAXTHREADNAMESIZE
];
5444 result
= kernel_thread_create(idle_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
5445 if (result
!= KERN_SUCCESS
) {
5449 snprintf(name
, sizeof(name
), "idle #%d", processor
->cpu_id
);
5450 thread_set_thread_name(thread
, name
);
5453 thread_lock(thread
);
5454 thread
->bound_processor
= processor
;
5455 processor
->idle_thread
= thread
;
5456 thread
->sched_pri
= thread
->base_pri
= IDLEPRI
;
5457 thread
->state
= (TH_RUN
| TH_IDLE
);
5458 thread
->options
|= TH_OPT_IDLE_THREAD
;
5459 thread_unlock(thread
);
5462 thread_deallocate(thread
);
5464 return KERN_SUCCESS
;
5470 * Kicks off scheduler services.
5472 * Called at splsched.
5477 kern_return_t result
;
5480 simple_lock_init(&sched_vm_group_list_lock
, 0);
5482 #if __arm__ || __arm64__
5483 simple_lock_init(&sched_recommended_cores_lock
, 0);
5484 #endif /* __arm__ || __arm64__ */
5486 result
= kernel_thread_start_priority((thread_continue_t
)sched_init_thread
,
5487 NULL
, MAXPRI_KERNEL
, &thread
);
5488 if (result
!= KERN_SUCCESS
) {
5489 panic("sched_startup");
5492 thread_deallocate(thread
);
5494 assert_thread_magic(thread
);
5497 * Yield to the sched_init_thread once, to
5498 * initialize our own thread after being switched
5501 * The current thread is the only other thread
5502 * active at this point.
5504 thread_block(THREAD_CONTINUE_NULL
);
5508 static _Atomic
uint64_t sched_perfcontrol_callback_deadline
;
5509 #endif /* __arm64__ */
5512 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
5514 static volatile uint64_t sched_maintenance_deadline
;
5515 static uint64_t sched_tick_last_abstime
;
5516 static uint64_t sched_tick_delta
;
5517 uint64_t sched_tick_max_delta
;
5521 * sched_init_thread:
5523 * Perform periodic bookkeeping functions about ten
5527 sched_timeshare_maintenance_continue(void)
5529 uint64_t sched_tick_ctime
, late_time
;
5531 struct sched_update_scan_context scan_context
= {
5532 .earliest_bg_make_runnable_time
= UINT64_MAX
,
5533 .earliest_normal_make_runnable_time
= UINT64_MAX
,
5534 .earliest_rt_make_runnable_time
= UINT64_MAX
5537 sched_tick_ctime
= mach_absolute_time();
5539 if (__improbable(sched_tick_last_abstime
== 0)) {
5540 sched_tick_last_abstime
= sched_tick_ctime
;
5542 sched_tick_delta
= 1;
5544 late_time
= sched_tick_ctime
- sched_tick_last_abstime
;
5545 sched_tick_delta
= late_time
/ sched_tick_interval
;
5546 /* Ensure a delta of 1, since the interval could be slightly
5547 * smaller than the sched_tick_interval due to dispatch
5550 sched_tick_delta
= MAX(sched_tick_delta
, 1);
5552 /* In the event interrupt latencies or platform
5553 * idle events that advanced the timebase resulted
5554 * in periods where no threads were dispatched,
5555 * cap the maximum "tick delta" at SCHED_TICK_MAX_DELTA
5558 sched_tick_delta
= MIN(sched_tick_delta
, SCHED_TICK_MAX_DELTA
);
5560 sched_tick_last_abstime
= sched_tick_ctime
;
5561 sched_tick_max_delta
= MAX(sched_tick_delta
, sched_tick_max_delta
);
5564 scan_context
.sched_tick_last_abstime
= sched_tick_last_abstime
;
5565 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_MAINTENANCE
) | DBG_FUNC_START
,
5566 sched_tick_delta
, late_time
, 0, 0, 0);
5568 /* Add a number of pseudo-ticks corresponding to the elapsed interval
5569 * This could be greater than 1 if substantial intervals where
5570 * all processors are idle occur, which rarely occurs in practice.
5573 sched_tick
+= sched_tick_delta
;
5578 * Compute various averages.
5580 compute_averages(sched_tick_delta
);
5583 * Scan the run queues for threads which
5584 * may need to be updated, and find the earliest runnable thread on the runqueue
5585 * to report its latency.
5587 SCHED(thread_update_scan
)(&scan_context
);
5589 SCHED(rt_runq_scan
)(&scan_context
);
5591 uint64_t ctime
= mach_absolute_time();
5593 uint64_t bg_max_latency
= (ctime
> scan_context
.earliest_bg_make_runnable_time
) ?
5594 ctime
- scan_context
.earliest_bg_make_runnable_time
: 0;
5596 uint64_t default_max_latency
= (ctime
> scan_context
.earliest_normal_make_runnable_time
) ?
5597 ctime
- scan_context
.earliest_normal_make_runnable_time
: 0;
5599 uint64_t realtime_max_latency
= (ctime
> scan_context
.earliest_rt_make_runnable_time
) ?
5600 ctime
- scan_context
.earliest_rt_make_runnable_time
: 0;
5602 machine_max_runnable_latency(bg_max_latency
, default_max_latency
, realtime_max_latency
);
5605 * Check to see if the special sched VM group needs attention.
5607 sched_vm_group_maintenance();
5609 #if __arm__ || __arm64__
5610 /* Check to see if the recommended cores failsafe is active */
5611 sched_recommended_cores_maintenance();
5612 #endif /* __arm__ || __arm64__ */
5615 #if DEBUG || DEVELOPMENT
5617 #include <i386/misc_protos.h>
5618 /* Check for long-duration interrupts */
5619 mp_interrupt_watchdog();
5620 #endif /* __x86_64__ */
5621 #endif /* DEBUG || DEVELOPMENT */
5623 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_MAINTENANCE
) | DBG_FUNC_END
,
5624 sched_pri_shifts
[TH_BUCKET_SHARE_FG
], sched_pri_shifts
[TH_BUCKET_SHARE_BG
],
5625 sched_pri_shifts
[TH_BUCKET_SHARE_UT
], sched_pri_shifts
[TH_BUCKET_SHARE_DF
], 0);
5627 assert_wait((event_t
)sched_timeshare_maintenance_continue
, THREAD_UNINT
);
5628 thread_block((thread_continue_t
)sched_timeshare_maintenance_continue
);
5632 static uint64_t sched_maintenance_wakeups
;
5635 * Determine if the set of routines formerly driven by a maintenance timer
5636 * must be invoked, based on a deadline comparison. Signals the scheduler
5637 * maintenance thread on deadline expiration. Must be invoked at an interval
5638 * lower than the "sched_tick_interval", currently accomplished by
5639 * invocation via the quantum expiration timer and at context switch time.
5640 * Performance matters: this routine reuses a timestamp approximating the
5641 * current absolute time received from the caller, and should perform
5642 * no more than a comparison against the deadline in the common case.
5645 sched_timeshare_consider_maintenance(uint64_t ctime
)
5647 cpu_quiescent_counter_checkin(ctime
);
5649 uint64_t deadline
= sched_maintenance_deadline
;
5651 if (__improbable(ctime
>= deadline
)) {
5652 if (__improbable(current_thread() == sched_maintenance_thread
)) {
5657 uint64_t ndeadline
= ctime
+ sched_tick_interval
;
5659 if (__probable(os_atomic_cmpxchg(&sched_maintenance_deadline
, deadline
, ndeadline
, seq_cst
))) {
5660 thread_wakeup((event_t
)sched_timeshare_maintenance_continue
);
5661 sched_maintenance_wakeups
++;
5665 #if !CONFIG_SCHED_CLUTCH
5667 * Only non-clutch schedulers use the global load calculation EWMA algorithm. For clutch
5668 * scheduler, the load is maintained at the thread group and bucket level.
5670 uint64_t load_compute_deadline
= os_atomic_load_wide(&sched_load_compute_deadline
, relaxed
);
5672 if (__improbable(load_compute_deadline
&& ctime
>= load_compute_deadline
)) {
5673 uint64_t new_deadline
= 0;
5674 if (os_atomic_cmpxchg(&sched_load_compute_deadline
, load_compute_deadline
, new_deadline
, relaxed
)) {
5675 compute_sched_load();
5676 new_deadline
= ctime
+ sched_load_compute_interval_abs
;
5677 os_atomic_store_wide(&sched_load_compute_deadline
, new_deadline
, relaxed
);
5680 #endif /* CONFIG_SCHED_CLUTCH */
5683 uint64_t perf_deadline
= os_atomic_load(&sched_perfcontrol_callback_deadline
, relaxed
);
5685 if (__improbable(perf_deadline
&& ctime
>= perf_deadline
)) {
5686 /* CAS in 0, if success, make callback. Otherwise let the next context switch check again. */
5687 if (os_atomic_cmpxchg(&sched_perfcontrol_callback_deadline
, perf_deadline
, 0, relaxed
)) {
5688 machine_perfcontrol_deadline_passed(perf_deadline
);
5691 #endif /* __arm64__ */
5694 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
5697 sched_init_thread(void)
5699 thread_block(THREAD_CONTINUE_NULL
);
5701 thread_t thread
= current_thread();
5703 thread_set_thread_name(thread
, "sched_maintenance_thread");
5705 sched_maintenance_thread
= thread
;
5707 SCHED(maintenance_continuation
)();
5712 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
5715 * thread_update_scan / runq_scan:
5717 * Scan the run queues to account for timesharing threads
5718 * which need to be updated.
5720 * Scanner runs in two passes. Pass one squirrels likely
5721 * threads away in an array, pass two does the update.
5723 * This is necessary because the run queue is locked for
5724 * the candidate scan, but the thread is locked for the update.
5726 * Array should be sized to make forward progress, without
5727 * disabling preemption for long periods.
5730 #define THREAD_UPDATE_SIZE 128
5732 static thread_t thread_update_array
[THREAD_UPDATE_SIZE
];
5733 static uint32_t thread_update_count
= 0;
5735 /* Returns TRUE if thread was added, FALSE if thread_update_array is full */
5737 thread_update_add_thread(thread_t thread
)
5739 if (thread_update_count
== THREAD_UPDATE_SIZE
) {
5743 thread_update_array
[thread_update_count
++] = thread
;
5744 thread_reference_internal(thread
);
5749 thread_update_process_threads(void)
5751 assert(thread_update_count
<= THREAD_UPDATE_SIZE
);
5753 for (uint32_t i
= 0; i
< thread_update_count
; i
++) {
5754 thread_t thread
= thread_update_array
[i
];
5755 assert_thread_magic(thread
);
5756 thread_update_array
[i
] = THREAD_NULL
;
5758 spl_t s
= splsched();
5759 thread_lock(thread
);
5760 if (!(thread
->state
& (TH_WAIT
)) && thread
->sched_stamp
!= sched_tick
) {
5761 SCHED(update_priority
)(thread
);
5763 thread_unlock(thread
);
5766 thread_deallocate(thread
);
5769 thread_update_count
= 0;
5775 sched_update_scan_context_t scan_context
)
5777 assert_thread_magic(thread
);
5779 if (thread
->sched_stamp
!= sched_tick
&&
5780 thread
->sched_mode
== TH_MODE_TIMESHARE
) {
5781 if (thread_update_add_thread(thread
) == FALSE
) {
5786 if (cpu_throttle_enabled
&& ((thread
->sched_pri
<= MAXPRI_THROTTLE
) && (thread
->base_pri
<= MAXPRI_THROTTLE
))) {
5787 if (thread
->last_made_runnable_time
< scan_context
->earliest_bg_make_runnable_time
) {
5788 scan_context
->earliest_bg_make_runnable_time
= thread
->last_made_runnable_time
;
5791 if (thread
->last_made_runnable_time
< scan_context
->earliest_normal_make_runnable_time
) {
5792 scan_context
->earliest_normal_make_runnable_time
= thread
->last_made_runnable_time
;
5800 * Scan a runq for candidate threads.
5802 * Returns TRUE if retry is needed.
5807 sched_update_scan_context_t scan_context
)
5809 int count
= runq
->count
;
5818 for (queue_index
= bitmap_first(runq
->bitmap
, NRQS
);
5820 queue_index
= bitmap_next(runq
->bitmap
, queue_index
)) {
5822 circle_queue_t queue
= &runq
->queues
[queue_index
];
5824 cqe_foreach_element(thread
, queue
, runq_links
) {
5826 if (runq_scan_thread(thread
, scan_context
) == TRUE
) {
5836 #if CONFIG_SCHED_CLUTCH
5839 sched_clutch_timeshare_scan(
5840 queue_t thread_queue
,
5841 uint16_t thread_count
,
5842 sched_update_scan_context_t scan_context
)
5844 if (thread_count
== 0) {
5849 qe_foreach_element_safe(thread
, thread_queue
, th_clutch_timeshare_link
) {
5850 if (runq_scan_thread(thread
, scan_context
) == TRUE
) {
5856 assert(thread_count
== 0);
5861 #endif /* CONFIG_SCHED_CLUTCH */
5863 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
5866 thread_eager_preemption(thread_t thread
)
5868 return (thread
->sched_flags
& TH_SFLAG_EAGERPREEMPT
) != 0;
5872 thread_set_eager_preempt(thread_t thread
)
5876 ast_t ast
= AST_NONE
;
5879 p
= current_processor();
5881 thread_lock(thread
);
5882 thread
->sched_flags
|= TH_SFLAG_EAGERPREEMPT
;
5884 if (thread
== current_thread()) {
5885 ast
= csw_check(thread
, p
, AST_NONE
);
5886 thread_unlock(thread
);
5887 if (ast
!= AST_NONE
) {
5888 (void) thread_block_reason(THREAD_CONTINUE_NULL
, NULL
, ast
);
5891 p
= thread
->last_processor
;
5893 if (p
!= PROCESSOR_NULL
&& p
->state
== PROCESSOR_RUNNING
&&
5894 p
->active_thread
== thread
) {
5898 thread_unlock(thread
);
5905 thread_clear_eager_preempt(thread_t thread
)
5910 thread_lock(thread
);
5912 thread
->sched_flags
&= ~TH_SFLAG_EAGERPREEMPT
;
5914 thread_unlock(thread
);
5919 * Scheduling statistics
5922 sched_stats_handle_csw(processor_t processor
, int reasons
, int selfpri
, int otherpri
)
5924 struct sched_statistics
*stats
;
5925 boolean_t to_realtime
= FALSE
;
5927 stats
= PERCPU_GET_RELATIVE(sched_stats
, processor
, processor
);
5930 if (otherpri
>= BASEPRI_REALTIME
) {
5931 stats
->rt_sched_count
++;
5935 if ((reasons
& AST_PREEMPT
) != 0) {
5936 stats
->preempt_count
++;
5938 if (selfpri
>= BASEPRI_REALTIME
) {
5939 stats
->preempted_rt_count
++;
5943 stats
->preempted_by_rt_count
++;
5949 sched_stats_handle_runq_change(struct runq_stats
*stats
, int old_count
)
5951 uint64_t timestamp
= mach_absolute_time();
5953 stats
->count_sum
+= (timestamp
- stats
->last_change_timestamp
) * old_count
;
5954 stats
->last_change_timestamp
= timestamp
;
5958 * For calls from assembly code
5960 #undef thread_wakeup
5969 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
5973 preemption_enabled(void)
5975 return get_preemption_level() == 0 && ml_get_interrupts_enabled();
5979 sched_timer_deadline_tracking_init(void)
5981 nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_1_DEFAULT
, &timer_deadline_tracking_bin_1
);
5982 nanoseconds_to_absolutetime(TIMER_DEADLINE_TRACKING_BIN_2_DEFAULT
, &timer_deadline_tracking_bin_2
);
5985 #if __arm__ || __arm64__
5987 uint32_t perfcontrol_requested_recommended_cores
= ALL_CORES_RECOMMENDED
;
5988 uint32_t perfcontrol_requested_recommended_core_count
= MAX_CPUS
;
5989 bool perfcontrol_failsafe_active
= false;
5990 bool perfcontrol_sleep_override
= false;
5992 uint64_t perfcontrol_failsafe_maintenance_runnable_time
;
5993 uint64_t perfcontrol_failsafe_activation_time
;
5994 uint64_t perfcontrol_failsafe_deactivation_time
;
5996 /* data covering who likely caused it and how long they ran */
5997 #define FAILSAFE_NAME_LEN 33 /* (2*MAXCOMLEN)+1 from size of p_name */
5998 char perfcontrol_failsafe_name
[FAILSAFE_NAME_LEN
];
5999 int perfcontrol_failsafe_pid
;
6000 uint64_t perfcontrol_failsafe_tid
;
6001 uint64_t perfcontrol_failsafe_thread_timer_at_start
;
6002 uint64_t perfcontrol_failsafe_thread_timer_last_seen
;
6003 uint32_t perfcontrol_failsafe_recommended_at_trigger
;
6006 * Perf controller calls here to update the recommended core bitmask.
6007 * If the failsafe is active, we don't immediately apply the new value.
6008 * Instead, we store the new request and use it after the failsafe deactivates.
6010 * If the failsafe is not active, immediately apply the update.
6012 * No scheduler locks are held, no other locks are held that scheduler might depend on,
6013 * interrupts are enabled
6015 * currently prototype is in osfmk/arm/machine_routines.h
6018 sched_perfcontrol_update_recommended_cores(uint32_t recommended_cores
)
6020 assert(preemption_enabled());
6022 spl_t s
= splsched();
6023 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6025 perfcontrol_requested_recommended_cores
= recommended_cores
;
6026 perfcontrol_requested_recommended_core_count
= __builtin_popcountll(recommended_cores
);
6028 if ((perfcontrol_failsafe_active
== false) && (perfcontrol_sleep_override
== false)) {
6029 sched_update_recommended_cores(perfcontrol_requested_recommended_cores
& usercontrol_requested_recommended_cores
);
6031 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6032 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REC_CORES_FAILSAFE
) | DBG_FUNC_NONE
,
6033 perfcontrol_requested_recommended_cores
,
6034 sched_maintenance_thread
->last_made_runnable_time
, 0, 0, 0);
6037 simple_unlock(&sched_recommended_cores_lock
);
6042 sched_override_recommended_cores_for_sleep(void)
6044 spl_t s
= splsched();
6045 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6047 if (perfcontrol_sleep_override
== false) {
6048 perfcontrol_sleep_override
= true;
6049 sched_update_recommended_cores(ALL_CORES_RECOMMENDED
);
6052 simple_unlock(&sched_recommended_cores_lock
);
6057 sched_restore_recommended_cores_after_sleep(void)
6059 spl_t s
= splsched();
6060 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6062 if (perfcontrol_sleep_override
== true) {
6063 perfcontrol_sleep_override
= false;
6064 sched_update_recommended_cores(perfcontrol_requested_recommended_cores
& usercontrol_requested_recommended_cores
);
6067 simple_unlock(&sched_recommended_cores_lock
);
6072 * Consider whether we need to activate the recommended cores failsafe
6074 * Called from quantum timer interrupt context of a realtime thread
6075 * No scheduler locks are held, interrupts are disabled
6078 sched_consider_recommended_cores(uint64_t ctime
, thread_t cur_thread
)
6081 * Check if a realtime thread is starving the system
6082 * and bringing up non-recommended cores would help
6084 * TODO: Is this the correct check for recommended == possible cores?
6085 * TODO: Validate the checks without the relevant lock are OK.
6088 if (__improbable(perfcontrol_failsafe_active
== TRUE
)) {
6089 /* keep track of how long the responsible thread runs */
6091 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6093 if (perfcontrol_failsafe_active
== TRUE
&&
6094 cur_thread
->thread_id
== perfcontrol_failsafe_tid
) {
6095 perfcontrol_failsafe_thread_timer_last_seen
= timer_grab(&cur_thread
->user_timer
) +
6096 timer_grab(&cur_thread
->system_timer
);
6099 simple_unlock(&sched_recommended_cores_lock
);
6101 /* we're already trying to solve the problem, so bail */
6105 /* The failsafe won't help if there are no more processors to enable */
6106 if (__probable(perfcontrol_requested_recommended_core_count
>= processor_count
)) {
6110 uint64_t too_long_ago
= ctime
- perfcontrol_failsafe_starvation_threshold
;
6112 /* Use the maintenance thread as our canary in the coal mine */
6113 thread_t m_thread
= sched_maintenance_thread
;
6115 /* If it doesn't look bad, nothing to see here */
6116 if (__probable(m_thread
->last_made_runnable_time
>= too_long_ago
)) {
6120 /* It looks bad, take the lock to be sure */
6121 thread_lock(m_thread
);
6123 if (m_thread
->runq
== PROCESSOR_NULL
||
6124 (m_thread
->state
& (TH_RUN
| TH_WAIT
)) != TH_RUN
||
6125 m_thread
->last_made_runnable_time
>= too_long_ago
) {
6127 * Maintenance thread is either on cpu or blocked, and
6128 * therefore wouldn't benefit from more cores
6130 thread_unlock(m_thread
);
6134 uint64_t maintenance_runnable_time
= m_thread
->last_made_runnable_time
;
6136 thread_unlock(m_thread
);
6139 * There are cores disabled at perfcontrol's recommendation, but the
6140 * system is so overloaded that the maintenance thread can't run.
6141 * That likely means that perfcontrol can't run either, so it can't fix
6142 * the recommendation. We have to kick in a failsafe to keep from starving.
6144 * When the maintenance thread has been starved for too long,
6145 * ignore the recommendation from perfcontrol and light up all the cores.
6147 * TODO: Consider weird states like boot, sleep, or debugger
6150 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6152 if (perfcontrol_failsafe_active
== TRUE
) {
6153 simple_unlock(&sched_recommended_cores_lock
);
6157 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6158 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REC_CORES_FAILSAFE
) | DBG_FUNC_START
,
6159 perfcontrol_requested_recommended_cores
, maintenance_runnable_time
, 0, 0, 0);
6161 perfcontrol_failsafe_active
= TRUE
;
6162 perfcontrol_failsafe_activation_time
= mach_absolute_time();
6163 perfcontrol_failsafe_maintenance_runnable_time
= maintenance_runnable_time
;
6164 perfcontrol_failsafe_recommended_at_trigger
= perfcontrol_requested_recommended_cores
;
6166 /* Capture some data about who screwed up (assuming that the thread on core is at fault) */
6167 task_t task
= cur_thread
->task
;
6168 perfcontrol_failsafe_pid
= task_pid(task
);
6169 strlcpy(perfcontrol_failsafe_name
, proc_name_address(task
->bsd_info
), sizeof(perfcontrol_failsafe_name
));
6171 perfcontrol_failsafe_tid
= cur_thread
->thread_id
;
6173 /* Blame the thread for time it has run recently */
6174 uint64_t recent_computation
= (ctime
- cur_thread
->computation_epoch
) + cur_thread
->computation_metered
;
6176 uint64_t last_seen
= timer_grab(&cur_thread
->user_timer
) + timer_grab(&cur_thread
->system_timer
);
6178 /* Compute the start time of the bad behavior in terms of the thread's on core time */
6179 perfcontrol_failsafe_thread_timer_at_start
= last_seen
- recent_computation
;
6180 perfcontrol_failsafe_thread_timer_last_seen
= last_seen
;
6182 /* Ignore the previously recommended core configuration */
6183 sched_update_recommended_cores(ALL_CORES_RECOMMENDED
);
6185 simple_unlock(&sched_recommended_cores_lock
);
6189 * Now that our bacon has been saved by the failsafe, consider whether to turn it off
6191 * Runs in the context of the maintenance thread, no locks held
6194 sched_recommended_cores_maintenance(void)
6196 /* Common case - no failsafe, nothing to be done here */
6197 if (__probable(perfcontrol_failsafe_active
== FALSE
)) {
6201 uint64_t ctime
= mach_absolute_time();
6203 boolean_t print_diagnostic
= FALSE
;
6204 char p_name
[FAILSAFE_NAME_LEN
] = "";
6206 spl_t s
= splsched();
6207 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6209 /* Check again, under the lock, to avoid races */
6210 if (perfcontrol_failsafe_active
== FALSE
) {
6215 * Ensure that the other cores get another few ticks to run some threads
6216 * If we don't have this hysteresis, the maintenance thread is the first
6217 * to run, and then it immediately kills the other cores
6219 if ((ctime
- perfcontrol_failsafe_activation_time
) < perfcontrol_failsafe_starvation_threshold
) {
6223 /* Capture some diagnostic state under the lock so we can print it out later */
6225 int pid
= perfcontrol_failsafe_pid
;
6226 uint64_t tid
= perfcontrol_failsafe_tid
;
6228 uint64_t thread_usage
= perfcontrol_failsafe_thread_timer_last_seen
-
6229 perfcontrol_failsafe_thread_timer_at_start
;
6230 uint32_t rec_cores_before
= perfcontrol_failsafe_recommended_at_trigger
;
6231 uint32_t rec_cores_after
= perfcontrol_requested_recommended_cores
;
6232 uint64_t failsafe_duration
= ctime
- perfcontrol_failsafe_activation_time
;
6233 strlcpy(p_name
, perfcontrol_failsafe_name
, sizeof(p_name
));
6235 print_diagnostic
= TRUE
;
6237 /* Deactivate the failsafe and reinstate the requested recommendation settings */
6239 perfcontrol_failsafe_deactivation_time
= ctime
;
6240 perfcontrol_failsafe_active
= FALSE
;
6242 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6243 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REC_CORES_FAILSAFE
) | DBG_FUNC_END
,
6244 perfcontrol_requested_recommended_cores
, failsafe_duration
, 0, 0, 0);
6246 sched_update_recommended_cores(perfcontrol_requested_recommended_cores
& usercontrol_requested_recommended_cores
);
6249 simple_unlock(&sched_recommended_cores_lock
);
6252 if (print_diagnostic
) {
6253 uint64_t failsafe_duration_ms
= 0, thread_usage_ms
= 0;
6255 absolutetime_to_nanoseconds(failsafe_duration
, &failsafe_duration_ms
);
6256 failsafe_duration_ms
= failsafe_duration_ms
/ NSEC_PER_MSEC
;
6258 absolutetime_to_nanoseconds(thread_usage
, &thread_usage_ms
);
6259 thread_usage_ms
= thread_usage_ms
/ NSEC_PER_MSEC
;
6261 printf("recommended core failsafe kicked in for %lld ms "
6262 "likely due to %s[%d] thread 0x%llx spending "
6263 "%lld ms on cpu at realtime priority - "
6264 "new recommendation: 0x%x -> 0x%x\n",
6265 failsafe_duration_ms
, p_name
, pid
, tid
, thread_usage_ms
,
6266 rec_cores_before
, rec_cores_after
);
6270 #endif /* __arm__ || __arm64__ */
6273 sched_processor_enable(processor_t processor
, boolean_t enable
)
6275 assert(preemption_enabled());
6277 spl_t s
= splsched();
6278 simple_lock(&sched_recommended_cores_lock
, LCK_GRP_NULL
);
6281 bit_set(usercontrol_requested_recommended_cores
, processor
->cpu_id
);
6283 bit_clear(usercontrol_requested_recommended_cores
, processor
->cpu_id
);
6286 #if __arm__ || __arm64__
6287 if ((perfcontrol_failsafe_active
== false) && (perfcontrol_sleep_override
== false)) {
6288 sched_update_recommended_cores(perfcontrol_requested_recommended_cores
& usercontrol_requested_recommended_cores
);
6290 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
6291 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_REC_CORES_FAILSAFE
) | DBG_FUNC_NONE
,
6292 perfcontrol_requested_recommended_cores
,
6293 sched_maintenance_thread
->last_made_runnable_time
, 0, 0, 0);
6295 #else /* __arm__ || __arm64__ */
6296 sched_update_recommended_cores(usercontrol_requested_recommended_cores
);
6297 #endif /* !__arm__ || __arm64__ */
6299 simple_unlock(&sched_recommended_cores_lock
);
6302 return KERN_SUCCESS
;
6307 * Apply a new recommended cores mask to the processors it affects
6308 * Runs after considering failsafes and such
6310 * Iterate over processors and update their ->is_recommended field.
6311 * If a processor is running, we let it drain out at its next
6312 * quantum expiration or blocking point. If a processor is idle, there
6313 * may be more work for it to do, so IPI it.
6315 * interrupts disabled, sched_recommended_cores_lock is held
6318 sched_update_recommended_cores(uint64_t recommended_cores
)
6320 processor_set_t pset
, nset
;
6321 processor_t processor
;
6322 uint64_t needs_exit_idle_mask
= 0x0;
6323 uint32_t avail_count
;
6325 processor
= processor_list
;
6326 pset
= processor
->processor_set
;
6328 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_UPDATE_REC_CORES
) | DBG_FUNC_START
,
6330 #if __arm__ || __arm64__
6331 perfcontrol_failsafe_active
, 0, 0);
6332 #else /* __arm__ || __arm64__ */
6334 #endif /* ! __arm__ || __arm64__ */
6336 if (__builtin_popcountll(recommended_cores
) == 0) {
6337 bit_set(recommended_cores
, master_processor
->cpu_id
); /* add boot processor or we hang */
6340 /* First set recommended cores */
6344 nset
= processor
->processor_set
;
6351 if (bit_test(recommended_cores
, processor
->cpu_id
)) {
6352 processor
->is_recommended
= TRUE
;
6353 bit_set(pset
->recommended_bitmask
, processor
->cpu_id
);
6355 if (processor
->state
== PROCESSOR_IDLE
) {
6356 if (processor
!= current_processor()) {
6357 bit_set(needs_exit_idle_mask
, processor
->cpu_id
);
6360 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
6362 SCHED(pset_made_schedulable
)(processor
, pset
, false);
6365 } while ((processor
= processor
->processor_list
) != NULL
);
6368 /* Now shutdown not recommended cores */
6369 processor
= processor_list
;
6370 pset
= processor
->processor_set
;
6374 nset
= processor
->processor_set
;
6381 if (!bit_test(recommended_cores
, processor
->cpu_id
)) {
6382 sched_ipi_type_t ipi_type
= SCHED_IPI_NONE
;
6384 processor
->is_recommended
= FALSE
;
6385 bit_clear(pset
->recommended_bitmask
, processor
->cpu_id
);
6387 if ((processor
->state
== PROCESSOR_RUNNING
) || (processor
->state
== PROCESSOR_DISPATCHING
)) {
6388 ipi_type
= SCHED_IPI_IMMEDIATE
;
6390 SCHED(processor_queue_shutdown
)(processor
);
6393 SCHED(rt_queue_shutdown
)(processor
);
6395 if (ipi_type
!= SCHED_IPI_NONE
) {
6396 if (processor
== current_processor()) {
6397 ast_on(AST_PREEMPT
);
6399 sched_ipi_perform(processor
, ipi_type
);
6405 } while ((processor
= processor
->processor_list
) != NULL
);
6407 processor_avail_count_user
= avail_count
;
6408 #if defined(__x86_64__)
6409 commpage_update_active_cpus();
6414 /* Issue all pending IPIs now that the pset lock has been dropped */
6415 for (int cpuid
= lsb_first(needs_exit_idle_mask
); cpuid
>= 0; cpuid
= lsb_next(needs_exit_idle_mask
, cpuid
)) {
6416 processor
= processor_array
[cpuid
];
6417 machine_signal_idle(processor
);
6420 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_UPDATE_REC_CORES
) | DBG_FUNC_END
,
6421 needs_exit_idle_mask
, 0, 0, 0);
6425 thread_set_options(uint32_t thopt
)
6428 thread_t t
= current_thread();
6433 t
->options
|= thopt
;
6440 thread_set_pending_block_hint(thread_t thread
, block_hint_t block_hint
)
6442 thread
->pending_block_hint
= block_hint
;
6446 qos_max_parallelism(int qos
, uint64_t options
)
6448 return SCHED(qos_max_parallelism
)(qos
, options
);
6452 sched_qos_max_parallelism(__unused
int qos
, uint64_t options
)
6454 host_basic_info_data_t hinfo
;
6455 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
6456 /* Query the machine layer for core information */
6457 __assert_only kern_return_t kret
= host_info(host_self(), HOST_BASIC_INFO
,
6458 (host_info_t
)&hinfo
, &count
);
6459 assert(kret
== KERN_SUCCESS
);
6461 if (options
& QOS_PARALLELISM_COUNT_LOGICAL
) {
6462 return hinfo
.logical_cpu
;
6464 return hinfo
.physical_cpu
;
6468 int sched_allow_NO_SMT_threads
= 1;
6470 thread_no_smt(thread_t thread
)
6472 return sched_allow_NO_SMT_threads
&& (thread
->bound_processor
== PROCESSOR_NULL
) && ((thread
->sched_flags
& TH_SFLAG_NO_SMT
) || (thread
->task
->t_flags
& TF_NO_SMT
));
6476 processor_active_thread_no_smt(processor_t processor
)
6478 return sched_allow_NO_SMT_threads
&& !processor
->current_is_bound
&& processor
->current_is_NO_SMT
;
6484 * Set up or replace old timer with new timer
6486 * Returns true if canceled old timer, false if it did not
6489 sched_perfcontrol_update_callback_deadline(uint64_t new_deadline
)
6492 * Exchange deadline for new deadline, if old deadline was nonzero,
6493 * then I cancelled the callback, otherwise I didn't
6496 return os_atomic_xchg(&sched_perfcontrol_callback_deadline
, new_deadline
,
6500 #endif /* __arm64__ */
6502 #if CONFIG_SCHED_EDGE
6504 #define SCHED_PSET_LOAD_EWMA_TC_NSECS 10000000u
6507 * sched_edge_pset_running_higher_bucket()
6509 * Routine to calculate cumulative running counts for each scheduling
6510 * bucket. This effectively lets the load calculation calculate if a
6511 * cluster is running any threads at a QoS lower than the thread being
6516 sched_edge_pset_running_higher_bucket(processor_set_t pset
, uint32_t *running_higher
)
6518 bitmap_t
*active_map
= &pset
->cpu_state_map
[PROCESSOR_RUNNING
];
6520 /* Edge Scheduler Optimization */
6521 for (int cpu
= bitmap_first(active_map
, MAX_CPUS
); cpu
>= 0; cpu
= bitmap_next(active_map
, cpu
)) {
6522 sched_bucket_t cpu_bucket
= os_atomic_load(&pset
->cpu_running_buckets
[cpu
], relaxed
);
6523 for (sched_bucket_t bucket
= cpu_bucket
; bucket
< TH_BUCKET_SCHED_MAX
; bucket
++) {
6524 running_higher
[bucket
]++;
6530 * sched_update_pset_load_average()
6532 * Updates the load average for each sched bucket for a cluster.
6533 * This routine must be called with the pset lock held.
6536 sched_update_pset_load_average(processor_set_t pset
, uint64_t curtime
)
6538 if (pset
->online_processor_count
== 0) {
6539 /* Looks like the pset is not runnable any more; nothing to do here */
6544 * Edge Scheduler Optimization
6546 * See if more callers of this routine can pass in timestamps to avoid the
6547 * mach_absolute_time() call here.
6551 curtime
= mach_absolute_time();
6553 uint64_t last_update
= os_atomic_load(&pset
->pset_load_last_update
, relaxed
);
6554 int64_t delta_ticks
= curtime
- last_update
;
6555 if (delta_ticks
< 0) {
6559 uint64_t delta_nsecs
= 0;
6560 absolutetime_to_nanoseconds(delta_ticks
, &delta_nsecs
);
6562 if (__improbable(delta_nsecs
> UINT32_MAX
)) {
6563 delta_nsecs
= UINT32_MAX
;
6566 uint32_t running_higher
[TH_BUCKET_SCHED_MAX
] = {0};
6567 sched_edge_pset_running_higher_bucket(pset
, running_higher
);
6569 for (sched_bucket_t sched_bucket
= TH_BUCKET_FIXPRI
; sched_bucket
< TH_BUCKET_SCHED_MAX
; sched_bucket
++) {
6570 uint64_t old_load_average
= os_atomic_load(&pset
->pset_load_average
[sched_bucket
], relaxed
);
6571 uint64_t old_load_average_factor
= old_load_average
* SCHED_PSET_LOAD_EWMA_TC_NSECS
;
6572 uint32_t current_runq_depth
= (sched_edge_cluster_cumulative_count(&pset
->pset_clutch_root
, sched_bucket
) + rt_runq_count(pset
) + running_higher
[sched_bucket
]) / pset
->online_processor_count
;
6575 * For the new load average multiply current_runq_depth by delta_nsecs (which resuts in a 32.0 value).
6576 * Since we want to maintain the load average as a 24.8 fixed arithmetic value for precision, the
6577 * new load averga needs to be shifted before it can be added to the old load average.
6579 uint64_t new_load_average_factor
= (current_runq_depth
* delta_nsecs
) << SCHED_PSET_LOAD_EWMA_FRACTION_BITS
;
6582 * For extremely parallel workloads, it is important that the load average on a cluster moves zero to non-zero
6583 * instantly to allow threads to be migrated to other (potentially idle) clusters quickly. Hence use the EWMA
6584 * when the system is already loaded; otherwise for an idle system use the latest load average immediately.
6586 int old_load_shifted
= (int)((old_load_average
+ SCHED_PSET_LOAD_EWMA_ROUND_BIT
) >> SCHED_PSET_LOAD_EWMA_FRACTION_BITS
);
6587 boolean_t load_uptick
= (old_load_shifted
== 0) && (current_runq_depth
!= 0);
6588 boolean_t load_downtick
= (old_load_shifted
!= 0) && (current_runq_depth
== 0);
6589 uint64_t load_average
;
6590 if (load_uptick
|| load_downtick
) {
6591 load_average
= (current_runq_depth
<< SCHED_PSET_LOAD_EWMA_FRACTION_BITS
);
6593 /* Indicates a loaded system; use EWMA for load average calculation */
6594 load_average
= (old_load_average_factor
+ new_load_average_factor
) / (delta_nsecs
+ SCHED_PSET_LOAD_EWMA_TC_NSECS
);
6596 os_atomic_store(&pset
->pset_load_average
[sched_bucket
], load_average
, relaxed
);
6597 KDBG(MACHDBG_CODE(DBG_MACH_SCHED_CLUTCH
, MACH_SCHED_EDGE_LOAD_AVG
) | DBG_FUNC_NONE
, pset
->pset_cluster_id
, (load_average
>> SCHED_PSET_LOAD_EWMA_FRACTION_BITS
), load_average
& SCHED_PSET_LOAD_EWMA_FRACTION_MASK
, sched_bucket
);
6599 os_atomic_store(&pset
->pset_load_last_update
, curtime
, relaxed
);
6603 sched_update_pset_avg_execution_time(processor_set_t pset
, uint64_t execution_time
, uint64_t curtime
, sched_bucket_t sched_bucket
)
6605 pset_execution_time_t old_execution_time_packed
, new_execution_time_packed
;
6606 uint64_t avg_thread_execution_time
= 0;
6608 os_atomic_rmw_loop(&pset
->pset_execution_time
[sched_bucket
].pset_execution_time_packed
,
6609 old_execution_time_packed
.pset_execution_time_packed
,
6610 new_execution_time_packed
.pset_execution_time_packed
, relaxed
, {
6611 uint64_t last_update
= old_execution_time_packed
.pset_execution_time_last_update
;
6612 int64_t delta_ticks
= curtime
- last_update
;
6613 if (delta_ticks
< 0) {
6615 * Its possible that another CPU came in and updated the pset_execution_time
6616 * before this CPU could do it. Since the average execution time is meant to
6617 * be an approximate measure per cluster, ignore the older update.
6619 os_atomic_rmw_loop_give_up(return );
6621 uint64_t delta_nsecs
= 0;
6622 absolutetime_to_nanoseconds(delta_ticks
, &delta_nsecs
);
6624 uint64_t nanotime
= 0;
6625 absolutetime_to_nanoseconds(execution_time
, &nanotime
);
6626 uint64_t execution_time_us
= nanotime
/ NSEC_PER_USEC
;
6628 uint64_t old_execution_time
= (old_execution_time_packed
.pset_avg_thread_execution_time
* SCHED_PSET_LOAD_EWMA_TC_NSECS
);
6629 uint64_t new_execution_time
= (execution_time_us
* delta_nsecs
);
6631 avg_thread_execution_time
= (old_execution_time
+ new_execution_time
) / (delta_nsecs
+ SCHED_PSET_LOAD_EWMA_TC_NSECS
);
6632 new_execution_time_packed
.pset_avg_thread_execution_time
= avg_thread_execution_time
;
6633 new_execution_time_packed
.pset_execution_time_last_update
= curtime
;
6635 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_PSET_AVG_EXEC_TIME
) | DBG_FUNC_NONE
, pset
->pset_cluster_id
, avg_thread_execution_time
, sched_bucket
);
6638 #else /* CONFIG_SCHED_EDGE */
6641 sched_update_pset_load_average(processor_set_t pset
, __unused
uint64_t curtime
)
6643 int non_rt_load
= pset
->pset_runq
.count
;
6644 int load
= ((bit_count(pset
->cpu_state_map
[PROCESSOR_RUNNING
]) + non_rt_load
+ rt_runq_count(pset
)) << PSET_LOAD_NUMERATOR_SHIFT
);
6645 int new_load_average
= ((int)pset
->load_average
+ load
) >> 1;
6647 pset
->load_average
= new_load_average
;
6648 #if (DEVELOPMENT || DEBUG)
6650 if (pset
->pset_cluster_type
== PSET_AMP_P
) {
6651 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_PSET_LOAD_AVERAGE
) | DBG_FUNC_NONE
, sched_get_pset_load_average(pset
, 0), (bit_count(pset
->cpu_state_map
[PROCESSOR_RUNNING
]) + pset
->pset_runq
.count
+ rt_runq_count(pset
)));
6658 sched_update_pset_avg_execution_time(__unused processor_set_t pset
, __unused
uint64_t execution_time
, __unused
uint64_t curtime
, __unused sched_bucket_t sched_bucket
)
6661 #endif /* CONFIG_SCHED_EDGE */
6663 /* pset is locked */
6665 processor_is_fast_track_candidate_for_realtime_thread(processor_set_t pset
, processor_t processor
)
6667 int cpuid
= processor
->cpu_id
;
6668 #if defined(__x86_64__)
6669 if (sched_avoid_cpu0
&& (cpuid
== 0)) {
6674 cpumap_t fasttrack_map
= pset_available_cpumap(pset
) & ~pset
->pending_AST_URGENT_cpu_mask
& ~pset
->realtime_map
;
6676 return bit_test(fasttrack_map
, cpuid
);
6679 /* pset is locked */
6681 choose_processor_for_realtime_thread(processor_set_t pset
, processor_t skip_processor
, bool consider_secondaries
)
6683 #if defined(__x86_64__)
6684 bool avoid_cpu0
= sched_avoid_cpu0
&& bit_test(pset
->cpu_bitmask
, 0);
6686 const bool avoid_cpu0
= false;
6689 cpumap_t cpu_map
= pset_available_cpumap(pset
) & ~pset
->pending_AST_URGENT_cpu_mask
& ~pset
->realtime_map
;
6690 if (skip_processor
) {
6691 bit_clear(cpu_map
, skip_processor
->cpu_id
);
6694 cpumap_t primary_map
= cpu_map
& pset
->primary_map
;
6696 primary_map
= bit_ror64(primary_map
, 1);
6699 int rotid
= lsb_first(primary_map
);
6701 int cpuid
= avoid_cpu0
? ((rotid
+ 1) & 63) : rotid
;
6703 processor_t processor
= processor_array
[cpuid
];
6708 if (!pset
->is_SMT
|| !sched_allow_rt_smt
|| !consider_secondaries
) {
6712 /* Consider secondary processors */
6713 cpumap_t secondary_map
= cpu_map
& ~pset
->primary_map
;
6715 /* Also avoid cpu1 */
6716 secondary_map
= bit_ror64(secondary_map
, 2);
6718 rotid
= lsb_first(secondary_map
);
6720 int cpuid
= avoid_cpu0
? ((rotid
+ 2) & 63) : rotid
;
6722 processor_t processor
= processor_array
[cpuid
];
6728 if (skip_processor
) {
6729 return PROCESSOR_NULL
;
6733 * If we didn't find an obvious processor to choose, but there are still more CPUs
6734 * not already running realtime threads than realtime threads in the realtime run queue,
6735 * this thread belongs in this pset, so choose some other processor in this pset
6736 * to ensure the thread is enqueued here.
6738 cpumap_t non_realtime_map
= pset_available_cpumap(pset
) & pset
->primary_map
& ~pset
->realtime_map
;
6739 if (bit_count(non_realtime_map
) > rt_runq_count(pset
)) {
6740 cpu_map
= non_realtime_map
;
6741 assert(cpu_map
!= 0);
6742 int cpuid
= bit_first(cpu_map
);
6744 return processor_array
[cpuid
];
6747 if (!pset
->is_SMT
|| !sched_allow_rt_smt
|| !consider_secondaries
) {
6748 goto skip_secondaries
;
6751 non_realtime_map
= pset_available_cpumap(pset
) & ~pset
->realtime_map
;
6752 if (bit_count(non_realtime_map
) > rt_runq_count(pset
)) {
6753 cpu_map
= non_realtime_map
;
6754 assert(cpu_map
!= 0);
6755 int cpuid
= bit_first(cpu_map
);
6757 return processor_array
[cpuid
];
6761 return PROCESSOR_NULL
;
6764 /* pset is locked */
6766 all_available_primaries_are_running_realtime_threads(processor_set_t pset
)
6768 cpumap_t cpu_map
= pset_available_cpumap(pset
) & pset
->primary_map
& ~pset
->realtime_map
;
6769 return rt_runq_count(pset
) > bit_count(cpu_map
);
6772 #if defined(__x86_64__)
6773 /* pset is locked */
6775 these_processors_are_running_realtime_threads(processor_set_t pset
, uint64_t these_map
)
6777 cpumap_t cpu_map
= pset_available_cpumap(pset
) & these_map
& ~pset
->realtime_map
;
6778 return rt_runq_count(pset
) > bit_count(cpu_map
);
6783 sched_ok_to_run_realtime_thread(processor_set_t pset
, processor_t processor
)
6785 bool ok_to_run_realtime_thread
= true;
6786 #if defined(__x86_64__)
6787 if (sched_avoid_cpu0
&& processor
->cpu_id
== 0) {
6788 ok_to_run_realtime_thread
= these_processors_are_running_realtime_threads(pset
, pset
->primary_map
& ~0x1);
6789 } else if (sched_avoid_cpu0
&& (processor
->cpu_id
== 1) && processor
->is_SMT
) {
6790 ok_to_run_realtime_thread
= sched_allow_rt_smt
&& these_processors_are_running_realtime_threads(pset
, ~0x2);
6791 } else if (processor
->processor_primary
!= processor
) {
6792 ok_to_run_realtime_thread
= (sched_allow_rt_smt
&& all_available_primaries_are_running_realtime_threads(pset
));
6798 return ok_to_run_realtime_thread
;
6802 sched_pset_made_schedulable(__unused processor_t processor
, processor_set_t pset
, boolean_t drop_lock
)
6810 thread_set_no_smt(bool set
)
6812 if (!system_is_SMT
) {
6813 /* Not a machine that supports SMT */
6817 thread_t thread
= current_thread();
6819 spl_t s
= splsched();
6820 thread_lock(thread
);
6822 thread
->sched_flags
|= TH_SFLAG_NO_SMT
;
6824 thread_unlock(thread
);
6829 thread_get_no_smt(void)
6831 return current_thread()->sched_flags
& TH_SFLAG_NO_SMT
;
6834 extern void task_set_no_smt(task_t
);
6836 task_set_no_smt(task_t task
)
6838 if (!system_is_SMT
) {
6839 /* Not a machine that supports SMT */
6843 if (task
== TASK_NULL
) {
6844 task
= current_task();
6848 task
->t_flags
|= TF_NO_SMT
;
6852 #if DEBUG || DEVELOPMENT
6853 extern void sysctl_task_set_no_smt(char no_smt
);
6855 sysctl_task_set_no_smt(char no_smt
)
6857 if (!system_is_SMT
) {
6858 /* Not a machine that supports SMT */
6862 task_t task
= current_task();
6865 if (no_smt
== '1') {
6866 task
->t_flags
|= TF_NO_SMT
;
6871 extern char sysctl_task_get_no_smt(void);
6873 sysctl_task_get_no_smt(void)
6875 task_t task
= current_task();
6877 if (task
->t_flags
& TF_NO_SMT
) {
6882 #endif /* DEVELOPMENT || DEBUG */
6885 __private_extern__
void
6886 thread_bind_cluster_type(thread_t thread
, char cluster_type
, bool soft_bound
)
6889 spl_t s
= splsched();
6890 thread_lock(thread
);
6891 thread
->sched_flags
&= ~(TH_SFLAG_ECORE_ONLY
| TH_SFLAG_PCORE_ONLY
| TH_SFLAG_BOUND_SOFT
);
6893 thread
->sched_flags
|= TH_SFLAG_BOUND_SOFT
;
6895 switch (cluster_type
) {
6898 thread
->sched_flags
|= TH_SFLAG_ECORE_ONLY
;
6902 thread
->sched_flags
|= TH_SFLAG_PCORE_ONLY
;
6907 thread_unlock(thread
);
6910 if (thread
== current_thread()) {
6911 thread_block(THREAD_CONTINUE_NULL
);
6917 #endif /* __AMP__ */