2 * Copyright (c) 2000-2010 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>
80 #ifdef CONFIG_MACH_APPROXIMATE_TIME
81 #include <machine/commpage.h> /* for commpage_update_mach_approximate_time */
85 * thread_quantum_expire:
87 * Recalculate the quantum and priority for a thread.
93 thread_quantum_expire(
94 timer_call_param_t p0
,
95 timer_call_param_t p1
)
97 processor_t processor
= p0
;
102 uint64_t ignore1
, ignore2
;
104 assert(processor
== current_processor());
105 assert(thread
== current_thread());
107 SCHED_STATS_QUANTUM_TIMER_EXPIRATION(processor
);
110 * We bill CPU time to both the individual thread and its task.
112 * Because this balance adjustment could potentially attempt to wake this very
113 * thread, we must credit the ledger before taking the thread lock. The ledger
114 * pointers are only manipulated by the thread itself at the ast boundary.
116 ledger_credit(thread
->t_ledger
, task_ledgers
.cpu_time
, thread
->quantum_remaining
);
117 ledger_credit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, thread
->quantum_remaining
);
119 if (thread
->t_bankledger
) {
120 ledger_credit(thread
->t_bankledger
, bank_ledgers
.cpu_time
,
121 (thread
->quantum_remaining
- thread
->t_deduct_bank_ledger_time
));
123 thread
->t_deduct_bank_ledger_time
= 0;
126 ctime
= mach_absolute_time();
128 #ifdef CONFIG_MACH_APPROXIMATE_TIME
129 commpage_update_mach_approximate_time(ctime
);
135 * We've run up until our quantum expiration, and will (potentially)
136 * continue without re-entering the scheduler, so update this now.
138 processor
->last_dispatch
= ctime
;
139 thread
->last_run_time
= ctime
;
142 * Check for fail-safe trip.
144 if ((thread
->sched_mode
== TH_MODE_REALTIME
|| thread
->sched_mode
== TH_MODE_FIXED
) &&
145 !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
146 !(thread
->options
& TH_OPT_SYSTEM_CRITICAL
)) {
147 uint64_t new_computation
;
149 new_computation
= ctime
- thread
->computation_epoch
;
150 new_computation
+= thread
->computation_metered
;
151 if (new_computation
> max_unsafe_computation
) {
152 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_FAILSAFE
)|DBG_FUNC_NONE
,
153 (uintptr_t)thread
->sched_pri
, (uintptr_t)thread
->sched_mode
, 0, 0, 0);
155 thread
->safe_release
= ctime
+ sched_safe_duration
;
157 sched_thread_mode_demote(thread
, TH_SFLAG_FAILSAFE
);
162 * Recompute scheduled priority if appropriate.
164 if (SCHED(can_update_priority
)(thread
))
165 SCHED(update_priority
)(thread
);
167 SCHED(lightweight_update_priority
)(thread
);
169 if (thread
->sched_mode
!= TH_MODE_REALTIME
)
170 SCHED(quantum_expire
)(thread
);
172 processor
->current_pri
= thread
->sched_pri
;
173 processor
->current_thmode
= thread
->sched_mode
;
175 /* Tell platform layer that we are still running this thread */
176 urgency
= thread_get_urgency(thread
, &ignore1
, &ignore2
);
177 machine_thread_going_on_core(thread
, urgency
, 0);
180 * This quantum is up, give this thread another.
182 processor
->first_timeslice
= FALSE
;
184 thread_quantum_init(thread
);
186 /* Reload precise timing global policy to thread-local policy */
187 thread
->precise_user_kernel_time
= use_precise_user_kernel_time(thread
);
190 * Since non-precise user/kernel time doesn't update the state/thread timer
191 * during privilege transitions, synthesize an event now.
193 if (!thread
->precise_user_kernel_time
) {
194 timer_switch(PROCESSOR_DATA(processor
, current_state
),
196 PROCESSOR_DATA(processor
, current_state
));
197 timer_switch(PROCESSOR_DATA(processor
, thread_timer
),
199 PROCESSOR_DATA(processor
, thread_timer
));
202 processor
->quantum_end
= ctime
+ thread
->quantum_remaining
;
205 * Context switch check.
207 if ((preempt
= csw_check(processor
, AST_QUANTUM
)) != AST_NONE
)
210 thread_unlock(thread
);
212 timer_call_enter1(&processor
->quantum_timer
, thread
,
213 processor
->quantum_end
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
215 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
216 sched_timeshare_consider_maintenance(ctime
);
217 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
222 * sched_set_thread_base_priority:
224 * Set the base priority of the thread
225 * and reset its scheduled priority.
227 * This is the only path to change base_pri.
229 * Called with the thread locked.
232 sched_set_thread_base_priority(thread_t thread
, int priority
)
234 thread
->base_pri
= priority
;
236 thread_recompute_sched_pri(thread
, FALSE
);
240 * thread_recompute_sched_pri:
242 * Reset the scheduled priority of the thread
243 * according to its base priority if the
244 * thread has not been promoted or depressed.
246 * This is the standard way to push base_pri changes into sched_pri,
247 * or to recalculate the appropriate sched_pri after clearing
248 * a promotion or depression.
250 * Called at splsched with the thread locked.
253 thread_recompute_sched_pri(
255 boolean_t override_depress
)
259 if (thread
->sched_mode
== TH_MODE_TIMESHARE
)
260 priority
= SCHED(compute_timeshare_priority
)(thread
);
262 priority
= thread
->base_pri
;
264 if ((!(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) || (priority
> thread
->sched_pri
)) &&
265 (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) || override_depress
)) {
266 set_sched_pri(thread
, priority
);
271 sched_default_quantum_expire(thread_t thread __unused
)
274 * No special behavior when a timeshare, fixed, or realtime thread
275 * uses up its entire quantum
279 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
282 * lightweight_update_priority:
284 * Update the scheduled priority for
285 * a timesharing thread.
287 * Only for use on the current thread.
289 * Called with the thread locked.
292 lightweight_update_priority(thread_t thread
)
294 assert(thread
->runq
== PROCESSOR_NULL
);
295 assert(thread
== current_thread());
297 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
301 thread_timer_delta(thread
, delta
);
304 * Accumulate timesharing usage only
305 * during contention for processor
308 if (thread
->pri_shift
< INT8_MAX
)
309 thread
->sched_usage
+= delta
;
311 thread
->cpu_delta
+= delta
;
313 priority
= sched_compute_timeshare_priority(thread
);
316 * Adjust the scheduled priority like thread_recompute_sched_pri,
317 * except with the benefit of knowing the thread is on this core.
319 if ((!(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) || (priority
> thread
->sched_pri
)) &&
320 (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
)) &&
321 priority
!= thread
->sched_pri
) {
323 thread
->sched_pri
= priority
;
325 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHANGE_PRIORITY
),
326 (uintptr_t)thread_tid(thread
),
329 0, /* eventually, 'reason' */
336 * Define shifts for simulating (5/8) ** n
338 * Shift structures for holding update shifts. Actual computation
339 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
340 * +/- is determined by the sign of shift 2.
347 #define SCHED_DECAY_TICKS 32
348 static struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
349 {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
350 {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
351 {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
352 {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
356 * sched_compute_timeshare_priority:
358 * Calculate the timesharing priority based upon usage and load.
360 extern int sched_pri_decay_band_limit
;
364 sched_compute_timeshare_priority(thread_t thread
)
366 /* start with base priority */
367 int priority
= thread
->base_pri
- (thread
->sched_usage
>> thread
->pri_shift
);
369 if (priority
< MINPRI_USER
)
370 priority
= MINPRI_USER
;
371 else if (priority
> MAXPRI_KERNEL
)
372 priority
= MAXPRI_KERNEL
;
379 * can_update_priority
381 * Make sure we don't do re-dispatches more frequently than a scheduler tick.
383 * Called with the thread locked.
389 if (sched_tick
== thread
->sched_stamp
)
398 * Perform housekeeping operations driven by scheduler tick.
400 * Called with the thread locked.
404 register thread_t thread
)
406 register unsigned ticks
;
407 register uint32_t delta
;
409 ticks
= sched_tick
- thread
->sched_stamp
;
411 thread
->sched_stamp
+= ticks
;
412 if (sched_use_combined_fgbg_decay
)
413 thread
->pri_shift
= sched_combined_fgbg_pri_shift
;
414 else if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
415 thread
->pri_shift
= sched_background_pri_shift
;
417 thread
->pri_shift
= sched_pri_shift
;
419 /* If requested, accelerate aging of sched_usage */
420 if (sched_decay_usage_age_factor
> 1)
421 ticks
*= sched_decay_usage_age_factor
;
424 * Gather cpu usage data.
426 thread_timer_delta(thread
, delta
);
427 if (ticks
< SCHED_DECAY_TICKS
) {
428 register struct shift_data
*shiftp
;
431 * Accumulate timesharing usage only
432 * during contention for processor
435 if (thread
->pri_shift
< INT8_MAX
)
436 thread
->sched_usage
+= delta
;
438 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
439 thread
->cpu_delta
= 0;
441 shiftp
= &sched_decay_shifts
[ticks
];
442 if (shiftp
->shift2
> 0) {
444 (thread
->cpu_usage
>> shiftp
->shift1
) +
445 (thread
->cpu_usage
>> shiftp
->shift2
);
446 thread
->sched_usage
=
447 (thread
->sched_usage
>> shiftp
->shift1
) +
448 (thread
->sched_usage
>> shiftp
->shift2
);
452 (thread
->cpu_usage
>> shiftp
->shift1
) -
453 (thread
->cpu_usage
>> -(shiftp
->shift2
));
454 thread
->sched_usage
=
455 (thread
->sched_usage
>> shiftp
->shift1
) -
456 (thread
->sched_usage
>> -(shiftp
->shift2
));
460 thread
->cpu_usage
= thread
->cpu_delta
= 0;
461 thread
->sched_usage
= 0;
465 * Check for fail-safe release.
467 if ((thread
->sched_flags
& TH_SFLAG_FAILSAFE
) &&
468 mach_absolute_time() >= thread
->safe_release
) {
469 sched_thread_mode_undemote(thread
, TH_SFLAG_FAILSAFE
);
473 * Recompute scheduled priority if appropriate.
475 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
476 int priority
= sched_compute_timeshare_priority(thread
);
479 * Adjust the scheduled priority like thread_recompute_sched_pri,
480 * except without setting an AST.
482 if ((!(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) || (priority
> thread
->sched_pri
)) &&
483 (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
)) &&
484 priority
!= thread
->sched_pri
) {
486 boolean_t removed
= thread_run_queue_remove(thread
);
488 thread
->sched_pri
= priority
;
490 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHANGE_PRIORITY
),
491 (uintptr_t)thread_tid(thread
),
494 0, /* eventually, 'reason' */
498 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
505 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
508 /* sched_mode == TH_MODE_TIMESHARE controls whether a thread has a timeshare count when it has a run count */
510 void sched_share_incr(thread_t thread
) {
511 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
);
512 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
513 assert(thread
->SHARE_COUNT
== 0);
514 thread
->SHARE_COUNT
++;
515 (void)hw_atomic_add(&sched_share_count
, 1);
518 void sched_share_decr(thread_t thread
) {
519 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
|| thread
->sched_mode
!= TH_MODE_TIMESHARE
);
520 assert(thread
->SHARE_COUNT
== 1);
521 (void)hw_atomic_sub(&sched_share_count
, 1);
522 thread
->SHARE_COUNT
--;
525 /* TH_SFLAG_THROTTLED controls whether a thread has a background count when it has a run count and a share count */
527 void sched_background_incr(thread_t thread
) {
528 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
);
529 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
530 assert((thread
->sched_flags
& TH_SFLAG_THROTTLED
) == TH_SFLAG_THROTTLED
);
532 assert(thread
->BG_COUNT
== 0);
534 int val
= hw_atomic_add(&sched_background_count
, 1);
537 /* Always do the background change while holding a share count */
538 assert(thread
->SHARE_COUNT
== 1);
541 void sched_background_decr(thread_t thread
) {
542 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
)
543 assert((thread
->sched_flags
& TH_SFLAG_THROTTLED
) != TH_SFLAG_THROTTLED
);
544 assert(thread
->BG_COUNT
== 1);
545 int val
= hw_atomic_sub(&sched_background_count
, 1);
548 assert(thread
->BG_COUNT
== 0);
550 /* Always do the background change while holding a share count */
551 assert(thread
->SHARE_COUNT
== 1);
556 assert_thread_sched_count(thread_t thread
) {
557 /* Only 0 or 1 are acceptable values */
558 assert(thread
->BG_COUNT
== 0 || thread
->BG_COUNT
== 1);
559 assert(thread
->SHARE_COUNT
== 0 || thread
->SHARE_COUNT
== 1);
561 /* BG is only allowed when you already have a share count */
562 if (thread
->BG_COUNT
== 1)
563 assert(thread
->SHARE_COUNT
== 1);
564 if (thread
->SHARE_COUNT
== 0)
565 assert(thread
->BG_COUNT
== 0);
567 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
||
568 (thread
->sched_mode
!= TH_MODE_TIMESHARE
))
569 assert(thread
->SHARE_COUNT
== 0);
571 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&&
572 (thread
->sched_mode
== TH_MODE_TIMESHARE
))
573 assert(thread
->SHARE_COUNT
== 1);
575 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
||
576 (thread
->sched_mode
!= TH_MODE_TIMESHARE
) ||
577 !(thread
->sched_flags
& TH_SFLAG_THROTTLED
))
578 assert(thread
->BG_COUNT
== 0);
580 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&&
581 (thread
->sched_mode
== TH_MODE_TIMESHARE
) &&
582 (thread
->sched_flags
& TH_SFLAG_THROTTLED
))
583 assert(thread
->BG_COUNT
== 1);
586 #endif /* MACH_ASSERT */
589 * Set the thread's true scheduling mode
590 * Called with thread mutex and thread locked
591 * The thread has already been removed from the runqueue.
593 * (saved_mode is handled before this point)
596 sched_set_thread_mode(thread_t thread
, sched_mode_t new_mode
)
598 assert_thread_sched_count(thread
);
599 assert(thread
->runq
== PROCESSOR_NULL
);
601 sched_mode_t old_mode
= thread
->sched_mode
;
603 thread
->sched_mode
= new_mode
;
607 case TH_MODE_REALTIME
:
608 if (old_mode
== TH_MODE_TIMESHARE
) {
609 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
) {
610 if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
611 sched_background_decr(thread
);
613 sched_share_decr(thread
);
618 case TH_MODE_TIMESHARE
:
619 if (old_mode
!= TH_MODE_TIMESHARE
) {
620 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
) {
621 sched_share_incr(thread
);
623 if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
624 sched_background_incr(thread
);
630 panic("unexpected mode: %d", new_mode
);
634 assert_thread_sched_count(thread
);
638 * Demote the true scheduler mode to timeshare (called with the thread locked)
641 sched_thread_mode_demote(thread_t thread
, uint32_t reason
)
643 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
644 assert((thread
->sched_flags
& reason
) != reason
);
645 assert_thread_sched_count(thread
);
647 if (thread
->policy_reset
)
650 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
651 /* Another demotion reason is already active */
652 thread
->sched_flags
|= reason
;
656 assert(thread
->saved_mode
== TH_MODE_NONE
);
658 boolean_t removed
= thread_run_queue_remove(thread
);
660 thread
->sched_flags
|= reason
;
662 thread
->saved_mode
= thread
->sched_mode
;
664 sched_set_thread_mode(thread
, TH_MODE_TIMESHARE
);
666 thread_recompute_priority(thread
);
669 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
671 assert_thread_sched_count(thread
);
675 * Un-demote the true scheduler mode back to the saved mode (called with the thread locked)
678 sched_thread_mode_undemote(thread_t thread
, uint32_t reason
)
680 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
681 assert((thread
->sched_flags
& reason
) == reason
);
682 assert(thread
->saved_mode
!= TH_MODE_NONE
);
683 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
684 assert(thread
->policy_reset
== 0);
686 assert_thread_sched_count(thread
);
688 thread
->sched_flags
&= ~reason
;
690 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
691 /* Another demotion reason is still active */
695 boolean_t removed
= thread_run_queue_remove(thread
);
697 sched_set_thread_mode(thread
, thread
->saved_mode
);
699 thread
->saved_mode
= TH_MODE_NONE
;
701 thread_recompute_priority(thread
);
704 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
708 * Set the thread to be categorized as 'background'
709 * Called with thread mutex and thread lock held
711 * TODO: Eventually, 'background' should be a true sched_mode.
714 sched_set_thread_throttled(thread_t thread
, boolean_t wants_throttle
)
716 if (thread
->policy_reset
)
719 assert(((thread
->sched_flags
& TH_SFLAG_THROTTLED
) ? TRUE
: FALSE
) != wants_throttle
);
721 assert_thread_sched_count(thread
);
724 * When backgrounding a thread, iOS has the semantic that
725 * realtime and fixed priority threads should be demoted
726 * to timeshare background threads.
728 * On OSX, realtime and fixed priority threads don't lose their mode.
731 if (wants_throttle
) {
732 thread
->sched_flags
|= TH_SFLAG_THROTTLED
;
733 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
) {
734 sched_background_incr(thread
);
737 assert_thread_sched_count(thread
);
740 thread
->sched_flags
&= ~TH_SFLAG_THROTTLED
;
741 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
) {
742 sched_background_decr(thread
);
745 assert_thread_sched_count(thread
);
749 assert_thread_sched_count(thread
);