2 * Copyright (c) 2000-2019 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@
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 * Priority related scheduler bits.
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <mach/machine.h>
69 #include <kern/host.h>
70 #include <kern/mach_param.h>
71 #include <kern/sched.h>
72 #include <sys/kdebug.h>
74 #include <kern/thread.h>
75 #include <kern/processor.h>
76 #include <kern/ledger.h>
77 #include <machine/machparam.h>
78 #include <kern/machine.h>
79 #include <kern/policy_internal.h>
80 #include <kern/sched_clutch.h>
82 #ifdef CONFIG_MACH_APPROXIMATE_TIME
83 #include <machine/commpage.h> /* for commpage_update_mach_approximate_time */
87 #include <kern/monotonic.h>
88 #endif /* MONOTONIC */
91 * thread_quantum_expire:
93 * Recalculate the quantum and priority for a thread.
99 thread_quantum_expire(
100 timer_call_param_t p0
,
101 timer_call_param_t p1
)
103 processor_t processor
= p0
;
104 thread_t thread
= p1
;
108 assert(processor
== current_processor());
109 assert(thread
== current_thread());
111 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_QUANTUM_EXPIRED
) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
113 SCHED_STATS_QUANTUM_TIMER_EXPIRATION(processor
);
116 * We bill CPU time to both the individual thread and its task.
118 * Because this balance adjustment could potentially attempt to wake this
119 * very thread, we must credit the ledger before taking the thread lock.
120 * The ledger pointers are only manipulated by the thread itself at the ast
123 * TODO: This fails to account for the time between when the timer was
124 * armed and when it fired. It should be based on the system_timer and
125 * running a timer_update operation here.
127 ledger_credit(thread
->t_ledger
, task_ledgers
.cpu_time
, thread
->quantum_remaining
);
128 ledger_credit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, thread
->quantum_remaining
);
129 if (thread
->t_bankledger
) {
130 ledger_credit(thread
->t_bankledger
, bank_ledgers
.cpu_time
,
131 (thread
->quantum_remaining
- thread
->t_deduct_bank_ledger_time
));
133 thread
->t_deduct_bank_ledger_time
= 0;
135 ctime
= mach_absolute_time();
137 #ifdef CONFIG_MACH_APPROXIMATE_TIME
138 commpage_update_mach_approximate_time(ctime
);
142 mt_sched_update(thread
);
143 #endif /* MONOTONIC */
148 * We've run up until our quantum expiration, and will (potentially)
149 * continue without re-entering the scheduler, so update this now.
151 processor
->last_dispatch
= ctime
;
152 thread
->last_run_time
= ctime
;
155 * Check for fail-safe trip.
157 if ((thread
->sched_mode
== TH_MODE_REALTIME
|| thread
->sched_mode
== TH_MODE_FIXED
) &&
158 !(thread
->sched_flags
& TH_SFLAG_PROMOTED
) &&
159 !(thread
->kern_promotion_schedpri
!= 0) &&
160 !(thread
->sched_flags
& TH_SFLAG_PROMOTE_REASON_MASK
) &&
161 !(thread
->options
& TH_OPT_SYSTEM_CRITICAL
)) {
162 uint64_t new_computation
;
164 new_computation
= ctime
- thread
->computation_epoch
;
165 new_computation
+= thread
->computation_metered
;
166 if (new_computation
> max_unsafe_computation
) {
167 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_FAILSAFE
) | DBG_FUNC_NONE
,
168 (uintptr_t)thread
->sched_pri
, (uintptr_t)thread
->sched_mode
, 0, 0, 0);
170 thread
->safe_release
= ctime
+ sched_safe_duration
;
172 sched_thread_mode_demote(thread
, TH_SFLAG_FAILSAFE
);
177 * Recompute scheduled priority if appropriate.
179 if (SCHED(can_update_priority
)(thread
)) {
180 SCHED(update_priority
)(thread
);
182 SCHED(lightweight_update_priority
)(thread
);
185 if (thread
->sched_mode
!= TH_MODE_REALTIME
) {
186 SCHED(quantum_expire
)(thread
);
190 * This quantum is up, give this thread another.
192 processor
->first_timeslice
= FALSE
;
194 thread_quantum_init(thread
);
196 /* Reload precise timing global policy to thread-local policy */
197 thread
->precise_user_kernel_time
= use_precise_user_kernel_time(thread
);
200 * Since non-precise user/kernel time doesn't update the state/thread timer
201 * during privilege transitions, synthesize an event now.
203 if (!thread
->precise_user_kernel_time
) {
204 timer_update(PROCESSOR_DATA(processor
, current_state
), ctime
);
205 timer_update(PROCESSOR_DATA(processor
, thread_timer
), ctime
);
206 timer_update(&thread
->runnable_timer
, ctime
);
210 processor
->quantum_end
= ctime
+ thread
->quantum_remaining
;
213 * Context switch check
215 * non-urgent flags don't affect kernel threads, so upgrade to urgent
216 * to ensure that rebalancing and non-recommendation kick in quickly.
219 ast_t check_reason
= AST_QUANTUM
;
220 if (thread
->task
== kernel_task
) {
221 check_reason
|= AST_URGENT
;
224 if ((preempt
= csw_check(thread
, processor
, check_reason
)) != AST_NONE
) {
229 * AST_KEVENT does not send an IPI when setting the AST,
230 * to avoid waiting for the next context switch to propagate the AST,
231 * the AST is propagated here at quantum expiration.
233 ast_propagate(thread
);
235 thread_unlock(thread
);
237 timer_call_quantum_timer_enter(&processor
->quantum_timer
, thread
,
238 processor
->quantum_end
, ctime
);
240 /* Tell platform layer that we are still running this thread */
241 thread_urgency_t urgency
= thread_get_urgency(thread
, NULL
, NULL
);
242 machine_thread_going_on_core(thread
, urgency
, 0, 0, ctime
);
243 machine_switch_perfcontrol_state_update(QUANTUM_EXPIRY
, ctime
,
246 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
247 sched_timeshare_consider_maintenance(ctime
);
248 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
250 #if __arm__ || __arm64__
251 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
252 sched_consider_recommended_cores(ctime
, thread
);
254 #endif /* __arm__ || __arm64__ */
256 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_QUANTUM_EXPIRED
) | DBG_FUNC_END
, preempt
, 0, 0, 0, 0);
260 * sched_set_thread_base_priority:
262 * Set the base priority of the thread
263 * and reset its scheduled priority.
265 * This is the only path to change base_pri.
267 * Called with the thread locked.
270 sched_set_thread_base_priority(thread_t thread
, int priority
)
272 assert(priority
>= MINPRI
);
275 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
276 assert(priority
<= BASEPRI_RTQUEUES
);
278 assert(priority
< BASEPRI_RTQUEUES
);
281 int old_base_pri
= thread
->base_pri
;
282 thread
->req_base_pri
= priority
;
283 if (thread
->sched_flags
& TH_SFLAG_BASE_PRI_FROZEN
) {
284 priority
= MAX(priority
, old_base_pri
);
286 thread
->base_pri
= priority
;
288 if ((thread
->state
& TH_RUN
) == TH_RUN
) {
289 assert(thread
->last_made_runnable_time
!= THREAD_NOT_RUNNABLE
);
290 ctime
= mach_approximate_time();
291 thread
->last_basepri_change_time
= ctime
;
293 assert(thread
->last_basepri_change_time
== THREAD_NOT_RUNNABLE
);
294 assert(thread
->last_made_runnable_time
== THREAD_NOT_RUNNABLE
);
298 * Currently the perfcontrol_attr depends on the base pri of the
299 * thread. Therefore, we use this function as the hook for the
300 * perfcontrol callout.
302 if (thread
== current_thread() && old_base_pri
!= priority
) {
304 ctime
= mach_approximate_time();
306 machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE
,
307 ctime
, PERFCONTROL_CALLOUT_WAKE_UNSAFE
, thread
);
309 #if !CONFIG_SCHED_CLUTCH
310 /* For the clutch scheduler, this operation is done in set_sched_pri() */
311 SCHED(update_thread_bucket
)(thread
);
312 #endif /* !CONFIG_SCHED_CLUTCH */
314 thread_recompute_sched_pri(thread
, SETPRI_DEFAULT
);
318 * sched_set_kernel_thread_priority:
320 * Set the absolute base priority of the thread
321 * and reset its scheduled priority.
323 * Called with the thread unlocked.
326 sched_set_kernel_thread_priority(thread_t thread
, int new_priority
)
328 spl_t s
= splsched();
332 assert(thread
->sched_mode
!= TH_MODE_REALTIME
);
333 assert(thread
->effective_policy
.thep_qos
== THREAD_QOS_UNSPECIFIED
);
335 if (new_priority
> thread
->max_priority
) {
336 new_priority
= thread
->max_priority
;
339 if (new_priority
< MAXPRI_THROTTLE
) {
340 new_priority
= MAXPRI_THROTTLE
;
342 #endif /* CONFIG_EMBEDDED */
344 thread
->importance
= new_priority
- thread
->task_priority
;
346 sched_set_thread_base_priority(thread
, new_priority
);
348 thread_unlock(thread
);
353 * thread_recompute_sched_pri:
355 * Reset the scheduled priority of the thread
356 * according to its base priority if the
357 * thread has not been promoted or depressed.
359 * This is the only way to push base_pri changes into sched_pri,
360 * or to recalculate the appropriate sched_pri after changing
361 * a promotion or depression.
363 * Called at splsched with the thread locked.
365 * TODO: Add an 'update urgency' flag to avoid urgency callouts on every rwlock operation
368 thread_recompute_sched_pri(thread_t thread
, set_sched_pri_options_t options
)
370 uint32_t sched_flags
= thread
->sched_flags
;
371 sched_mode_t sched_mode
= thread
->sched_mode
;
373 int priority
= thread
->base_pri
;
375 if (sched_mode
== TH_MODE_TIMESHARE
) {
376 priority
= SCHED(compute_timeshare_priority
)(thread
);
379 if (sched_flags
& TH_SFLAG_DEPRESS
) {
380 /* thread_yield_internal overrides kernel mutex promotion */
381 priority
= DEPRESSPRI
;
383 /* poll-depress is overridden by mutex promotion and promote-reasons */
384 if ((sched_flags
& TH_SFLAG_POLLDEPRESS
)) {
385 priority
= DEPRESSPRI
;
388 if (thread
->kern_promotion_schedpri
> 0) {
389 priority
= MAX(priority
, thread
->kern_promotion_schedpri
);
391 if (sched_mode
!= TH_MODE_REALTIME
) {
392 priority
= MIN(priority
, MAXPRI_PROMOTE
);
396 if (sched_flags
& TH_SFLAG_PROMOTED
) {
397 priority
= MAX(priority
, thread
->promotion_priority
);
399 if (sched_mode
!= TH_MODE_REALTIME
) {
400 priority
= MIN(priority
, MAXPRI_PROMOTE
);
404 if (sched_flags
& TH_SFLAG_PROMOTE_REASON_MASK
) {
405 if (sched_flags
& TH_SFLAG_RW_PROMOTED
) {
406 priority
= MAX(priority
, MINPRI_RWLOCK
);
409 if (sched_flags
& TH_SFLAG_WAITQ_PROMOTED
) {
410 priority
= MAX(priority
, MINPRI_WAITQ
);
413 if (sched_flags
& TH_SFLAG_EXEC_PROMOTED
) {
414 priority
= MAX(priority
, MINPRI_EXEC
);
419 set_sched_pri(thread
, priority
, options
);
423 sched_default_quantum_expire(thread_t thread __unused
)
426 * No special behavior when a timeshare, fixed, or realtime thread
427 * uses up its entire quantum
431 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
434 * lightweight_update_priority:
436 * Update the scheduled priority for
437 * a timesharing thread.
439 * Only for use on the current thread.
441 * Called with the thread locked.
444 lightweight_update_priority(thread_t thread
)
446 assert(thread
->runq
== PROCESSOR_NULL
);
447 assert(thread
== current_thread());
449 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
453 thread_timer_delta(thread
, delta
);
456 * Accumulate timesharing usage only
457 * during contention for processor
460 if (thread
->pri_shift
< INT8_MAX
) {
461 thread
->sched_usage
+= delta
;
464 thread
->cpu_delta
+= delta
;
466 #if CONFIG_SCHED_CLUTCH
468 * Update the CPU usage for the thread group to which the thread belongs.
469 * The implementation assumes that the thread ran for the entire delta
470 * as part of the same thread group.
472 sched_clutch_cpu_usage_update(thread
, delta
);
473 #endif /* CONFIG_SCHED_CLUTCH */
475 priority
= sched_compute_timeshare_priority(thread
);
477 if (priority
!= thread
->sched_pri
) {
478 thread_recompute_sched_pri(thread
, SETPRI_LAZY
);
484 * Define shifts for simulating (5/8) ** n
486 * Shift structures for holding update shifts. Actual computation
487 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
488 * +/- is determined by the sign of shift 2.
491 const struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
492 { .shift1
= 1, .shift2
= 1 },
493 { .shift1
= 1, .shift2
= 3 },
494 { .shift1
= 1, .shift2
= -3 },
495 { .shift1
= 2, .shift2
= -7 },
496 { .shift1
= 3, .shift2
= 5 },
497 { .shift1
= 3, .shift2
= -5 },
498 { .shift1
= 4, .shift2
= -8 },
499 { .shift1
= 5, .shift2
= 7 },
500 { .shift1
= 5, .shift2
= -7 },
501 { .shift1
= 6, .shift2
= -10 },
502 { .shift1
= 7, .shift2
= 10 },
503 { .shift1
= 7, .shift2
= -9 },
504 { .shift1
= 8, .shift2
= -11 },
505 { .shift1
= 9, .shift2
= 12 },
506 { .shift1
= 9, .shift2
= -11 },
507 { .shift1
= 10, .shift2
= -13 },
508 { .shift1
= 11, .shift2
= 14 },
509 { .shift1
= 11, .shift2
= -13 },
510 { .shift1
= 12, .shift2
= -15 },
511 { .shift1
= 13, .shift2
= 17 },
512 { .shift1
= 13, .shift2
= -15 },
513 { .shift1
= 14, .shift2
= -17 },
514 { .shift1
= 15, .shift2
= 19 },
515 { .shift1
= 16, .shift2
= 18 },
516 { .shift1
= 16, .shift2
= -19 },
517 { .shift1
= 17, .shift2
= 22 },
518 { .shift1
= 18, .shift2
= 20 },
519 { .shift1
= 18, .shift2
= -20 },
520 { .shift1
= 19, .shift2
= 26 },
521 { .shift1
= 20, .shift2
= 22 },
522 { .shift1
= 20, .shift2
= -22 },
523 { .shift1
= 21, .shift2
= -27 }
527 * sched_compute_timeshare_priority:
529 * Calculate the timesharing priority based upon usage and load.
531 extern int sched_pri_decay_band_limit
;
534 /* Only use the decay floor logic on embedded non-clutch schedulers */
535 #if CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH
538 sched_compute_timeshare_priority(thread_t thread
)
540 int decay_amount
= (thread
->sched_usage
>> thread
->pri_shift
);
541 int decay_limit
= sched_pri_decay_band_limit
;
543 if (thread
->base_pri
> BASEPRI_FOREGROUND
) {
544 decay_limit
+= (thread
->base_pri
- BASEPRI_FOREGROUND
);
547 if (decay_amount
> decay_limit
) {
548 decay_amount
= decay_limit
;
551 /* start with base priority */
552 int priority
= thread
->base_pri
- decay_amount
;
554 if (priority
< MAXPRI_THROTTLE
) {
555 if (thread
->task
->max_priority
> MAXPRI_THROTTLE
) {
556 priority
= MAXPRI_THROTTLE
;
557 } else if (priority
< MINPRI_USER
) {
558 priority
= MINPRI_USER
;
560 } else if (priority
> MAXPRI_KERNEL
) {
561 priority
= MAXPRI_KERNEL
;
567 #else /* CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH */
570 sched_compute_timeshare_priority(thread_t thread
)
572 /* start with base priority */
573 int priority
= thread
->base_pri
- (thread
->sched_usage
>> thread
->pri_shift
);
575 if (priority
< MINPRI_USER
) {
576 priority
= MINPRI_USER
;
577 } else if (priority
> MAXPRI_KERNEL
) {
578 priority
= MAXPRI_KERNEL
;
584 #endif /* CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH */
587 * can_update_priority
589 * Make sure we don't do re-dispatches more frequently than a scheduler tick.
591 * Called with the thread locked.
597 if (sched_tick
== thread
->sched_stamp
) {
607 * Perform housekeeping operations driven by scheduler tick.
609 * Called with the thread locked.
615 uint32_t ticks
, delta
;
617 ticks
= sched_tick
- thread
->sched_stamp
;
620 thread
->sched_stamp
+= ticks
;
622 /* If requested, accelerate aging of sched_usage */
623 if (sched_decay_usage_age_factor
> 1) {
624 ticks
*= sched_decay_usage_age_factor
;
628 * Gather cpu usage data.
630 thread_timer_delta(thread
, delta
);
631 if (ticks
< SCHED_DECAY_TICKS
) {
633 * Accumulate timesharing usage only during contention for processor
634 * resources. Use the pri_shift from the previous tick window to
635 * determine if the system was in a contended state.
637 if (thread
->pri_shift
< INT8_MAX
) {
638 thread
->sched_usage
+= delta
;
641 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
642 thread
->cpu_delta
= 0;
644 #if CONFIG_SCHED_CLUTCH
646 * Update the CPU usage for the thread group to which the thread belongs.
647 * The implementation assumes that the thread ran for the entire delta
648 * as part of the same thread group.
650 sched_clutch_cpu_usage_update(thread
, delta
);
651 #endif /* CONFIG_SCHED_CLUTCH */
653 const struct shift_data
*shiftp
= &sched_decay_shifts
[ticks
];
655 if (shiftp
->shift2
> 0) {
656 thread
->cpu_usage
= (thread
->cpu_usage
>> shiftp
->shift1
) +
657 (thread
->cpu_usage
>> shiftp
->shift2
);
658 thread
->sched_usage
= (thread
->sched_usage
>> shiftp
->shift1
) +
659 (thread
->sched_usage
>> shiftp
->shift2
);
661 thread
->cpu_usage
= (thread
->cpu_usage
>> shiftp
->shift1
) -
662 (thread
->cpu_usage
>> -(shiftp
->shift2
));
663 thread
->sched_usage
= (thread
->sched_usage
>> shiftp
->shift1
) -
664 (thread
->sched_usage
>> -(shiftp
->shift2
));
667 thread
->cpu_usage
= thread
->cpu_delta
= 0;
668 thread
->sched_usage
= 0;
672 * Check for fail-safe release.
674 if ((thread
->sched_flags
& TH_SFLAG_FAILSAFE
) &&
675 mach_absolute_time() >= thread
->safe_release
) {
676 sched_thread_mode_undemote(thread
, TH_SFLAG_FAILSAFE
);
680 * Now that the thread's CPU usage has been accumulated and aged
681 * based on contention of the previous tick window, update the
682 * pri_shift of the thread to match the current global load/shift
683 * values. The updated pri_shift would be used to calculate the
684 * new priority of the thread.
686 #if CONFIG_SCHED_CLUTCH
687 thread
->pri_shift
= sched_clutch_thread_pri_shift(thread
, thread
->th_sched_bucket
);
688 #else /* CONFIG_SCHED_CLUTCH */
689 thread
->pri_shift
= sched_pri_shifts
[thread
->th_sched_bucket
];
690 #endif /* CONFIG_SCHED_CLUTCH */
692 /* Recompute scheduled priority if appropriate. */
693 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
694 thread_recompute_sched_pri(thread
, SETPRI_LAZY
);
698 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
702 * TH_BUCKET_RUN is a count of *all* runnable non-idle threads.
703 * Each other bucket is a count of the runnable non-idle threads
704 * with that property. All updates to these counts should be
705 * performed with os_atomic_* operations.
707 * For the clutch scheduler, this global bucket is used only for
708 * keeping the total global run count.
710 uint32_t sched_run_buckets
[TH_BUCKET_MAX
];
713 sched_incr_bucket(sched_bucket_t bucket
)
715 assert(bucket
>= TH_BUCKET_FIXPRI
&&
716 bucket
<= TH_BUCKET_SHARE_BG
);
718 os_atomic_inc(&sched_run_buckets
[bucket
], relaxed
);
722 sched_decr_bucket(sched_bucket_t bucket
)
724 assert(bucket
>= TH_BUCKET_FIXPRI
&&
725 bucket
<= TH_BUCKET_SHARE_BG
);
727 assert(os_atomic_load(&sched_run_buckets
[bucket
], relaxed
) > 0);
729 os_atomic_dec(&sched_run_buckets
[bucket
], relaxed
);
733 sched_run_incr(thread_t thread
)
735 assert((thread
->state
& (TH_RUN
| TH_IDLE
)) == TH_RUN
);
737 uint32_t new_count
= os_atomic_inc(&sched_run_buckets
[TH_BUCKET_RUN
], relaxed
);
739 sched_incr_bucket(thread
->th_sched_bucket
);
745 sched_run_decr(thread_t thread
)
747 assert((thread
->state
& (TH_RUN
| TH_IDLE
)) != TH_RUN
);
749 sched_decr_bucket(thread
->th_sched_bucket
);
751 uint32_t new_count
= os_atomic_dec(&sched_run_buckets
[TH_BUCKET_RUN
], relaxed
);
757 sched_update_thread_bucket(thread_t thread
)
759 sched_bucket_t old_bucket
= thread
->th_sched_bucket
;
760 sched_bucket_t new_bucket
= TH_BUCKET_RUN
;
762 switch (thread
->sched_mode
) {
764 case TH_MODE_REALTIME
:
765 new_bucket
= TH_BUCKET_FIXPRI
;
768 case TH_MODE_TIMESHARE
:
769 if (thread
->base_pri
> BASEPRI_DEFAULT
) {
770 new_bucket
= TH_BUCKET_SHARE_FG
;
771 } else if (thread
->base_pri
> BASEPRI_UTILITY
) {
772 new_bucket
= TH_BUCKET_SHARE_DF
;
773 } else if (thread
->base_pri
> MAXPRI_THROTTLE
) {
774 new_bucket
= TH_BUCKET_SHARE_UT
;
776 new_bucket
= TH_BUCKET_SHARE_BG
;
781 panic("unexpected mode: %d", thread
->sched_mode
);
785 if (old_bucket
!= new_bucket
) {
786 thread
->th_sched_bucket
= new_bucket
;
787 thread
->pri_shift
= sched_pri_shifts
[new_bucket
];
789 if ((thread
->state
& (TH_RUN
| TH_IDLE
)) == TH_RUN
) {
790 sched_decr_bucket(old_bucket
);
791 sched_incr_bucket(new_bucket
);
797 * Set the thread's true scheduling mode
798 * Called with thread mutex and thread locked
799 * The thread has already been removed from the runqueue.
801 * (saved_mode is handled before this point)
804 sched_set_thread_mode(thread_t thread
, sched_mode_t new_mode
)
806 assert(thread
->runq
== PROCESSOR_NULL
);
810 case TH_MODE_REALTIME
:
811 case TH_MODE_TIMESHARE
:
815 panic("unexpected mode: %d", new_mode
);
819 thread
->sched_mode
= new_mode
;
821 SCHED(update_thread_bucket
)(thread
);
825 * Demote the true scheduler mode to timeshare (called with the thread locked)
828 sched_thread_mode_demote(thread_t thread
, uint32_t reason
)
830 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
831 assert((thread
->sched_flags
& reason
) != reason
);
833 if (thread
->policy_reset
) {
837 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
838 /* Another demotion reason is already active */
839 thread
->sched_flags
|= reason
;
843 assert(thread
->saved_mode
== TH_MODE_NONE
);
845 boolean_t removed
= thread_run_queue_remove(thread
);
847 thread
->sched_flags
|= reason
;
849 thread
->saved_mode
= thread
->sched_mode
;
851 sched_set_thread_mode(thread
, TH_MODE_TIMESHARE
);
853 thread_recompute_priority(thread
);
856 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
861 * Un-demote the true scheduler mode back to the saved mode (called with the thread locked)
864 sched_thread_mode_undemote(thread_t thread
, uint32_t reason
)
866 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
867 assert((thread
->sched_flags
& reason
) == reason
);
868 assert(thread
->saved_mode
!= TH_MODE_NONE
);
869 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
870 assert(thread
->policy_reset
== 0);
872 thread
->sched_flags
&= ~reason
;
874 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
875 /* Another demotion reason is still active */
879 boolean_t removed
= thread_run_queue_remove(thread
);
881 sched_set_thread_mode(thread
, thread
->saved_mode
);
883 thread
->saved_mode
= TH_MODE_NONE
;
885 thread_recompute_priority(thread
);
888 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
893 * Promote thread to have a sched pri floor for a specific reason
895 * Promotion must not last past syscall boundary
896 * Clients must always pair promote and demote 1:1,
897 * Handling nesting of the same promote reason is the client's responsibility
899 * Called at splsched with thread locked
902 sched_thread_promote_reason(thread_t thread
,
904 __kdebug_only
uintptr_t trace_obj
/* already unslid */)
906 assert(reason
& TH_SFLAG_PROMOTE_REASON_MASK
);
907 assert((thread
->sched_flags
& reason
) != reason
);
910 case TH_SFLAG_RW_PROMOTED
:
911 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_RW_PROMOTE
),
912 thread_tid(thread
), thread
->sched_pri
,
913 thread
->base_pri
, trace_obj
);
915 case TH_SFLAG_WAITQ_PROMOTED
:
916 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAITQ_PROMOTE
),
917 thread_tid(thread
), thread
->sched_pri
,
918 thread
->base_pri
, trace_obj
);
920 case TH_SFLAG_EXEC_PROMOTED
:
921 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_EXEC_PROMOTE
),
922 thread_tid(thread
), thread
->sched_pri
,
923 thread
->base_pri
, trace_obj
);
927 thread
->sched_flags
|= reason
;
929 thread_recompute_sched_pri(thread
, SETPRI_DEFAULT
);
933 * End a specific promotion reason
934 * Demotes a thread back to its expected priority without the promotion in place
936 * Called at splsched with thread locked
939 sched_thread_unpromote_reason(thread_t thread
,
941 __kdebug_only
uintptr_t trace_obj
/* already unslid */)
943 assert(reason
& TH_SFLAG_PROMOTE_REASON_MASK
);
944 assert((thread
->sched_flags
& reason
) == reason
);
947 case TH_SFLAG_RW_PROMOTED
:
948 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_RW_DEMOTE
),
949 thread_tid(thread
), thread
->sched_pri
,
950 thread
->base_pri
, trace_obj
);
952 case TH_SFLAG_WAITQ_PROMOTED
:
953 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_WAITQ_DEMOTE
),
954 thread_tid(thread
), thread
->sched_pri
,
955 thread
->base_pri
, trace_obj
);
957 case TH_SFLAG_EXEC_PROMOTED
:
958 KDBG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_EXEC_DEMOTE
),
959 thread_tid(thread
), thread
->sched_pri
,
960 thread
->base_pri
, trace_obj
);
964 thread
->sched_flags
&= ~reason
;
966 thread_recompute_sched_pri(thread
, SETPRI_DEFAULT
);