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>
79 #ifdef CONFIG_MACH_APPROXIMATE_TIME
80 #include <machine/commpage.h> /* for commpage_update_mach_approximate_time */
84 * thread_quantum_expire:
86 * Recalculate the quantum and priority for a thread.
92 thread_quantum_expire(
93 timer_call_param_t p0
,
94 timer_call_param_t p1
)
96 processor_t processor
= p0
;
101 assert(processor
== current_processor());
103 SCHED_STATS_QUANTUM_TIMER_EXPIRATION(processor
);
106 * We bill CPU time to both the individual thread and its task.
108 * Because this balance adjustment could potentially attempt to wake this very
109 * thread, we must credit the ledger before taking the thread lock. The ledger
110 * pointers are only manipulated by the thread itself at the ast boundary.
112 ledger_credit(thread
->t_ledger
, task_ledgers
.cpu_time
, thread
->quantum_remaining
);
113 ledger_credit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, thread
->quantum_remaining
);
115 if (thread
->t_bankledger
) {
116 ledger_credit(thread
->t_bankledger
, bank_ledgers
.cpu_time
,
117 (thread
->quantum_remaining
- thread
->t_deduct_bank_ledger_time
));
119 thread
->t_deduct_bank_ledger_time
= 0;
122 ctime
= mach_absolute_time();
127 * We've run up until our quantum expiration, and will (potentially)
128 * continue without re-entering the scheduler, so update this now.
130 thread
->last_run_time
= ctime
;
132 #ifdef CONFIG_MACH_APPROXIMATE_TIME
133 commpage_update_mach_approximate_time(ctime
);
136 * Check for fail-safe trip.
138 if ((thread
->sched_mode
== TH_MODE_REALTIME
|| thread
->sched_mode
== TH_MODE_FIXED
) &&
139 !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
140 !(thread
->options
& TH_OPT_SYSTEM_CRITICAL
)) {
141 uint64_t new_computation
;
143 new_computation
= ctime
- thread
->computation_epoch
;
144 new_computation
+= thread
->computation_metered
;
145 if (new_computation
> max_unsafe_computation
) {
146 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_FAILSAFE
)|DBG_FUNC_NONE
,
147 (uintptr_t)thread
->sched_pri
, (uintptr_t)thread
->sched_mode
, 0, 0, 0);
149 thread
->safe_release
= ctime
+ sched_safe_duration
;
151 sched_thread_mode_demote(thread
, TH_SFLAG_FAILSAFE
);
156 * Recompute scheduled priority if appropriate.
158 if (SCHED(can_update_priority
)(thread
))
159 SCHED(update_priority
)(thread
);
161 SCHED(lightweight_update_priority
)(thread
);
163 SCHED(quantum_expire
)(thread
);
165 processor
->current_pri
= thread
->sched_pri
;
166 processor
->current_thmode
= thread
->sched_mode
;
169 * This quantum is up, give this thread another.
171 if (first_timeslice(processor
))
172 processor
->timeslice
--;
174 thread_quantum_init(thread
);
176 /* Reload precise timing global policy to thread-local policy */
177 thread
->precise_user_kernel_time
= use_precise_user_kernel_time(thread
);
180 * Since non-precise user/kernel time doesn't update the state/thread timer
181 * during privilege transitions, synthesize an event now.
183 if (!thread
->precise_user_kernel_time
) {
184 timer_switch(PROCESSOR_DATA(processor
, current_state
),
186 PROCESSOR_DATA(processor
, current_state
));
187 timer_switch(PROCESSOR_DATA(processor
, thread_timer
),
189 PROCESSOR_DATA(processor
, thread_timer
));
192 processor
->quantum_end
= ctime
+ thread
->quantum_remaining
;
193 timer_call_enter1(&processor
->quantum_timer
, thread
,
194 processor
->quantum_end
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
197 * Context switch check.
199 if ((preempt
= csw_check(processor
, AST_QUANTUM
)) != AST_NONE
)
202 thread_unlock(thread
);
204 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
205 sched_traditional_consider_maintenance(ctime
);
206 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
210 * sched_set_thread_base_priority:
212 * Set the base priority of the thread
213 * and reset its scheduled priority.
215 * Called with the thread locked.
218 sched_set_thread_base_priority(thread_t thread
, int priority
)
220 thread
->priority
= priority
;
221 SCHED(compute_priority
)(thread
, FALSE
);
225 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
228 sched_traditional_quantum_expire(thread_t thread __unused
)
231 * No special behavior when a timeshare, fixed, or realtime thread
232 * uses up its entire quantum
237 lightweight_update_priority(thread_t thread
)
239 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
240 register uint32_t delta
;
242 thread_timer_delta(thread
, delta
);
245 * Accumulate timesharing usage only
246 * during contention for processor
249 if (thread
->pri_shift
< INT8_MAX
)
250 thread
->sched_usage
+= delta
;
252 thread
->cpu_delta
+= delta
;
255 * Adjust the scheduled priority if
256 * the thread has not been promoted
257 * and is not depressed.
259 if ( !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
260 !(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) )
261 compute_my_priority(thread
);
266 * Define shifts for simulating (5/8) ** n
268 * Shift structures for holding update shifts. Actual computation
269 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
270 * +/- is determined by the sign of shift 2.
277 #define SCHED_DECAY_TICKS 32
278 static struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
279 {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
280 {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
281 {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
282 {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
286 * do_priority_computation:
288 * Calculate the timesharing priority based upon usage and load.
290 extern int sched_pri_decay_band_limit
;
293 static int do_priority_computation(thread_t th
) {
294 register int priority
= th
->priority
/* start with base priority */
295 - (th
->sched_usage
>> th
->pri_shift
);
296 if (priority
< MINPRI_USER
)
297 priority
= MINPRI_USER
;
299 if (priority
> MAXPRI_KERNEL
)
300 priority
= MAXPRI_KERNEL
;
309 * Reset the scheduled priority of the thread
310 * according to its base priority if the
311 * thread has not been promoted or depressed.
313 * Called with the thread locked.
317 register thread_t thread
,
318 boolean_t override_depress
)
320 register int priority
;
322 if (thread
->sched_mode
== TH_MODE_TIMESHARE
)
323 priority
= do_priority_computation(thread
);
325 priority
= thread
->priority
;
327 if ((!(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) || (priority
> thread
->sched_pri
)) &&
328 (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) || override_depress
)) {
329 set_sched_pri(thread
, priority
);
334 * compute_my_priority:
336 * Reset the scheduled priority for
337 * a timesharing thread.
339 * Only for use on the current thread
340 * if timesharing and not depressed.
342 * Called with the thread locked.
346 register thread_t thread
)
348 register int priority
;
350 priority
= do_priority_computation(thread
);
351 assert(thread
->runq
== PROCESSOR_NULL
);
353 if (priority
!= thread
->sched_pri
) {
354 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_DECAY_PRIORITY
)|DBG_FUNC_NONE
,
355 (uintptr_t)thread_tid(thread
),
361 thread
->sched_pri
= priority
;
365 * can_update_priority
367 * Make sure we don't do re-dispatches more frequently than a scheduler tick.
369 * Called with the thread locked.
375 if (sched_tick
== thread
->sched_stamp
)
384 * Perform housekeeping operations driven by scheduler tick.
386 * Called with the thread locked.
390 register thread_t thread
)
392 register unsigned ticks
;
393 register uint32_t delta
;
395 ticks
= sched_tick
- thread
->sched_stamp
;
397 thread
->sched_stamp
+= ticks
;
398 if (sched_use_combined_fgbg_decay
)
399 thread
->pri_shift
= sched_combined_fgbg_pri_shift
;
400 else if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
401 thread
->pri_shift
= sched_background_pri_shift
;
403 thread
->pri_shift
= sched_pri_shift
;
405 /* If requested, accelerate aging of sched_usage */
406 if (sched_decay_usage_age_factor
> 1)
407 ticks
*= sched_decay_usage_age_factor
;
410 * Gather cpu usage data.
412 thread_timer_delta(thread
, delta
);
413 if (ticks
< SCHED_DECAY_TICKS
) {
414 register struct shift_data
*shiftp
;
417 * Accumulate timesharing usage only
418 * during contention for processor
421 if (thread
->pri_shift
< INT8_MAX
)
422 thread
->sched_usage
+= delta
;
424 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
425 thread
->cpu_delta
= 0;
427 shiftp
= &sched_decay_shifts
[ticks
];
428 if (shiftp
->shift2
> 0) {
430 (thread
->cpu_usage
>> shiftp
->shift1
) +
431 (thread
->cpu_usage
>> shiftp
->shift2
);
432 thread
->sched_usage
=
433 (thread
->sched_usage
>> shiftp
->shift1
) +
434 (thread
->sched_usage
>> shiftp
->shift2
);
438 (thread
->cpu_usage
>> shiftp
->shift1
) -
439 (thread
->cpu_usage
>> -(shiftp
->shift2
));
440 thread
->sched_usage
=
441 (thread
->sched_usage
>> shiftp
->shift1
) -
442 (thread
->sched_usage
>> -(shiftp
->shift2
));
446 thread
->cpu_usage
= thread
->cpu_delta
= 0;
447 thread
->sched_usage
= 0;
451 * Check for fail-safe release.
453 if ((thread
->sched_flags
& TH_SFLAG_FAILSAFE
) &&
454 mach_absolute_time() >= thread
->safe_release
) {
455 sched_thread_mode_undemote(thread
, TH_SFLAG_FAILSAFE
);
460 * Recompute scheduled priority if appropriate.
462 if ( (thread
->sched_mode
== TH_MODE_TIMESHARE
) &&
463 !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
464 !(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) ) {
465 register int new_pri
;
467 new_pri
= do_priority_computation(thread
);
468 if (new_pri
!= thread
->sched_pri
) {
469 boolean_t removed
= thread_run_queue_remove(thread
);
472 if (sched_use_combined_fgbg_decay
&& ((thread
)->task
->max_priority
> MAXPRI_THROTTLE
) && (new_pri
== MAXPRI_THROTTLE
)) {
473 /* with the alternate (new) algorithm, would we have decayed this far? */
474 int alt_pri
= thread
->priority
- (thread
->sched_usage
>> sched_pri_shift
);
475 if ((alt_pri
> new_pri
) && (sched_background_count
> 0)) {
476 printf("thread %p would have decayed to only %d instead of %d\n", thread
, alt_pri
, new_pri
);
481 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_DECAY_PRIORITY
)|DBG_FUNC_NONE
,
482 (uintptr_t)thread_tid(thread
),
487 thread
->sched_pri
= new_pri
;
490 thread_setrun(thread
, SCHED_TAILQ
);
497 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
500 /* sched_mode == TH_MODE_TIMESHARE controls whether a thread has a timeshare count when it has a run count */
502 void sched_share_incr(thread_t thread
) {
503 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
);
504 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
505 assert(thread
->SHARE_COUNT
== 0);
506 thread
->SHARE_COUNT
++;
507 (void)hw_atomic_add(&sched_share_count
, 1);
510 void sched_share_decr(thread_t thread
) {
511 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
|| thread
->sched_mode
!= TH_MODE_TIMESHARE
);
512 assert(thread
->SHARE_COUNT
== 1);
513 (void)hw_atomic_sub(&sched_share_count
, 1);
514 thread
->SHARE_COUNT
--;
517 /* TH_SFLAG_THROTTLED controls whether a thread has a background count when it has a run count and a share count */
519 void sched_background_incr(thread_t thread
) {
520 assert((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
);
521 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
522 assert((thread
->sched_flags
& TH_SFLAG_THROTTLED
) == TH_SFLAG_THROTTLED
);
524 assert(thread
->BG_COUNT
== 0);
526 int val
= hw_atomic_add(&sched_background_count
, 1);
529 /* Always do the background change while holding a share count */
530 assert(thread
->SHARE_COUNT
== 1);
533 void sched_background_decr(thread_t thread
) {
534 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
)
535 assert((thread
->sched_flags
& TH_SFLAG_THROTTLED
) != TH_SFLAG_THROTTLED
);
536 assert(thread
->BG_COUNT
== 1);
537 int val
= hw_atomic_sub(&sched_background_count
, 1);
540 assert(thread
->BG_COUNT
== 0);
542 /* Always do the background change while holding a share count */
543 assert(thread
->SHARE_COUNT
== 1);
548 assert_thread_sched_count(thread_t thread
) {
549 /* Only 0 or 1 are acceptable values */
550 assert(thread
->BG_COUNT
== 0 || thread
->BG_COUNT
== 1);
551 assert(thread
->SHARE_COUNT
== 0 || thread
->SHARE_COUNT
== 1);
553 /* BG is only allowed when you already have a share count */
554 if (thread
->BG_COUNT
== 1)
555 assert(thread
->SHARE_COUNT
== 1);
556 if (thread
->SHARE_COUNT
== 0)
557 assert(thread
->BG_COUNT
== 0);
559 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
||
560 (thread
->sched_mode
!= TH_MODE_TIMESHARE
))
561 assert(thread
->SHARE_COUNT
== 0);
563 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&&
564 (thread
->sched_mode
== TH_MODE_TIMESHARE
))
565 assert(thread
->SHARE_COUNT
== 1);
567 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) != TH_RUN
||
568 (thread
->sched_mode
!= TH_MODE_TIMESHARE
) ||
569 !(thread
->sched_flags
& TH_SFLAG_THROTTLED
))
570 assert(thread
->BG_COUNT
== 0);
572 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&&
573 (thread
->sched_mode
== TH_MODE_TIMESHARE
) &&
574 (thread
->sched_flags
& TH_SFLAG_THROTTLED
))
575 assert(thread
->BG_COUNT
== 1);
578 #endif /* MACH_ASSERT */
581 * Set the thread's true scheduling mode
582 * Called with thread mutex and thread locked
583 * The thread has already been removed from the runqueue.
585 * (saved_mode is handled before this point)
588 sched_set_thread_mode(thread_t thread
, sched_mode_t new_mode
)
590 assert_thread_sched_count(thread
);
592 sched_mode_t old_mode
= thread
->sched_mode
;
594 thread
->sched_mode
= new_mode
;
598 case TH_MODE_REALTIME
:
599 if (old_mode
== TH_MODE_TIMESHARE
) {
600 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
) {
601 if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
602 sched_background_decr(thread
);
604 sched_share_decr(thread
);
609 case TH_MODE_TIMESHARE
:
610 if (old_mode
!= TH_MODE_TIMESHARE
) {
611 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
) {
612 sched_share_incr(thread
);
614 if (thread
->sched_flags
& TH_SFLAG_THROTTLED
)
615 sched_background_incr(thread
);
621 panic("unexpected mode: %d", new_mode
);
625 assert_thread_sched_count(thread
);
629 * Demote the true scheduler mode to timeshare (called with the thread locked)
632 sched_thread_mode_demote(thread_t thread
, uint32_t reason
)
634 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
635 assert((thread
->sched_flags
& reason
) != reason
);
636 assert_thread_sched_count(thread
);
638 if (thread
->policy_reset
)
641 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
642 /* Another demotion reason is already active */
643 thread
->sched_flags
|= reason
;
647 assert(thread
->saved_mode
== TH_MODE_NONE
);
649 boolean_t removed
= thread_run_queue_remove(thread
);
651 if (thread
->sched_mode
== TH_MODE_REALTIME
)
652 thread
->priority
= DEPRESSPRI
;
654 thread
->sched_flags
|= reason
;
656 thread
->saved_mode
= thread
->sched_mode
;
658 sched_set_thread_mode(thread
, TH_MODE_TIMESHARE
);
661 thread_setrun(thread
, SCHED_TAILQ
);
663 assert_thread_sched_count(thread
);
667 * Un-demote the true scheduler mode back to the saved mode (called with the thread locked)
670 sched_thread_mode_undemote(thread_t thread
, uint32_t reason
)
672 assert(reason
& TH_SFLAG_DEMOTED_MASK
);
673 assert((thread
->sched_flags
& reason
) == reason
);
674 assert(thread
->saved_mode
!= TH_MODE_NONE
);
675 assert(thread
->sched_mode
== TH_MODE_TIMESHARE
);
676 assert(thread
->policy_reset
== 0);
678 assert_thread_sched_count(thread
);
680 thread
->sched_flags
&= ~reason
;
682 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
683 /* Another demotion reason is still active */
687 boolean_t removed
= thread_run_queue_remove(thread
);
689 sched_set_thread_mode(thread
, thread
->saved_mode
);
691 thread
->saved_mode
= TH_MODE_NONE
;
693 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
694 thread
->priority
= BASEPRI_RTQUEUES
;
697 SCHED(compute_priority
)(thread
, FALSE
);
700 thread_setrun(thread
, SCHED_TAILQ
);
704 * Set the thread to be categorized as 'background'
705 * Called with thread mutex and thread lock held
707 * TODO: Eventually, 'background' should be a true sched_mode.
710 sched_set_thread_throttled(thread_t thread
, boolean_t wants_throttle
)
712 if (thread
->policy_reset
)
715 assert(((thread
->sched_flags
& TH_SFLAG_THROTTLED
) ? TRUE
: FALSE
) != wants_throttle
);
717 assert_thread_sched_count(thread
);
720 * When backgrounding a thread, iOS has the semantic that
721 * realtime and fixed priority threads should be demoted
722 * to timeshare background threads.
724 * On OSX, realtime and fixed priority threads don't lose their mode.
727 if (wants_throttle
) {
728 thread
->sched_flags
|= TH_SFLAG_THROTTLED
;
729 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
) {
730 sched_background_incr(thread
);
733 assert_thread_sched_count(thread
);
736 thread
->sched_flags
&= ~TH_SFLAG_THROTTLED
;
737 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
&& thread
->sched_mode
== TH_MODE_TIMESHARE
) {
738 sched_background_decr(thread
);
741 assert_thread_sched_count(thread
);
745 assert_thread_sched_count(thread
);