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.
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>
80 * thread_quantum_expire:
82 * Recalculate the quantum and priority for a thread.
88 thread_quantum_expire(
89 timer_call_param_t p0
,
90 timer_call_param_t p1
)
92 processor_t processor
= p0
;
97 SCHED_STATS_QUANTUM_TIMER_EXPIRATION(processor
);
100 * We bill CPU time to both the individual thread and its task.
102 * Because this balance adjustment could potentially attempt to wake this very
103 * thread, we must credit the ledger before taking the thread lock. The ledger
104 * pointers are only manipulated by the thread itself at the ast boundary.
106 ledger_credit(thread
->t_ledger
, task_ledgers
.cpu_time
, thread
->current_quantum
);
107 ledger_credit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, thread
->current_quantum
);
112 * We've run up until our quantum expiration, and will (potentially)
113 * continue without re-entering the scheduler, so update this now.
115 thread
->last_run_time
= processor
->quantum_end
;
118 * Check for fail-safe trip.
120 if ((thread
->sched_mode
== TH_MODE_REALTIME
|| thread
->sched_mode
== TH_MODE_FIXED
) &&
121 !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
122 !(thread
->options
& TH_OPT_SYSTEM_CRITICAL
)) {
123 uint64_t new_computation
;
125 new_computation
= processor
->quantum_end
- thread
->computation_epoch
;
126 new_computation
+= thread
->computation_metered
;
127 if (new_computation
> max_unsafe_computation
) {
128 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_FAILSAFE
)|DBG_FUNC_NONE
,
129 (uintptr_t)thread
->sched_pri
, (uintptr_t)thread
->sched_mode
, 0, 0, 0);
131 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
132 thread
->priority
= DEPRESSPRI
;
135 thread
->saved_mode
= thread
->sched_mode
;
137 if (SCHED(supports_timeshare_mode
)) {
139 thread
->sched_mode
= TH_MODE_TIMESHARE
;
141 /* XXX handle fixed->fixed case */
142 thread
->sched_mode
= TH_MODE_FIXED
;
145 thread
->safe_release
= processor
->quantum_end
+ sched_safe_duration
;
146 thread
->sched_flags
|= TH_SFLAG_FAILSAFE
;
151 * Recompute scheduled priority if appropriate.
153 if (SCHED(can_update_priority
)(thread
))
154 SCHED(update_priority
)(thread
);
156 SCHED(lightweight_update_priority
)(thread
);
158 SCHED(quantum_expire
)(thread
);
160 processor
->current_pri
= thread
->sched_pri
;
161 processor
->current_thmode
= thread
->sched_mode
;
164 * This quantum is up, give this thread another.
166 if (first_timeslice(processor
))
167 processor
->timeslice
--;
169 thread_quantum_init(thread
);
170 thread
->last_quantum_refill_time
= processor
->quantum_end
;
172 /* Reload precise timing global policy to thread-local policy */
173 thread
->precise_user_kernel_time
= use_precise_user_kernel_time(thread
);
176 * Since non-precise user/kernel time doesn't update the state/thread timer
177 * during privilege transitions, synthesize an event now.
179 if (!thread
->precise_user_kernel_time
) {
180 timer_switch(PROCESSOR_DATA(processor
, current_state
),
181 processor
->quantum_end
,
182 PROCESSOR_DATA(processor
, current_state
));
183 timer_switch(PROCESSOR_DATA(processor
, thread_timer
),
184 processor
->quantum_end
,
185 PROCESSOR_DATA(processor
, thread_timer
));
188 ctime
= mach_absolute_time();
189 processor
->quantum_end
= ctime
+ thread
->current_quantum
;
190 timer_call_enter1(&processor
->quantum_timer
, thread
,
191 processor
->quantum_end
, TIMER_CALL_SYS_CRITICAL
);
194 * Context switch check.
196 if ((preempt
= csw_check(processor
)) != AST_NONE
)
199 processor_set_t pset
= processor
->processor_set
;
203 pset_pri_hint(pset
, processor
, processor
->current_pri
);
204 pset_count_hint(pset
, processor
, SCHED(processor_runq_count
)(processor
));
209 thread_unlock(thread
);
211 #if defined(CONFIG_SCHED_TRADITIONAL)
212 sched_traditional_consider_maintenance(ctime
);
213 #endif /* CONFIG_SCHED_TRADITIONAL */
216 #if defined(CONFIG_SCHED_TRADITIONAL)
219 sched_traditional_quantum_expire(thread_t thread __unused
)
222 * No special behavior when a timeshare, fixed, or realtime thread
223 * uses up its entire quantum
228 lightweight_update_priority(thread_t thread
)
230 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
231 register uint32_t delta
;
233 thread_timer_delta(thread
, delta
);
236 * Accumulate timesharing usage only
237 * during contention for processor
240 if (thread
->pri_shift
< INT8_MAX
)
241 thread
->sched_usage
+= delta
;
243 thread
->cpu_delta
+= delta
;
246 * Adjust the scheduled priority if
247 * the thread has not been promoted
248 * and is not depressed.
250 if ( !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
251 !(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) )
252 compute_my_priority(thread
);
257 * Define shifts for simulating (5/8) ** n
259 * Shift structures for holding update shifts. Actual computation
260 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
261 * +/- is determined by the sign of shift 2.
268 #define SCHED_DECAY_TICKS 32
269 static struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
270 {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
271 {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
272 {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
273 {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
277 * do_priority_computation:
279 * Calculate the timesharing priority based upon usage and load.
282 #define do_priority_computation(thread, pri) \
284 (pri) = (thread)->priority /* start with base priority */ \
285 - ((thread)->sched_usage >> (thread)->pri_shift); \
286 if ((pri) < MINPRI_USER) \
287 (pri) = MINPRI_USER; \
289 if ((pri) > MAXPRI_KERNEL) \
290 (pri) = MAXPRI_KERNEL; \
299 * Set the base priority of the thread
300 * and reset its scheduled priority.
302 * Called with the thread locked.
306 register thread_t thread
,
307 register int priority
)
309 thread
->priority
= priority
;
310 SCHED(compute_priority
)(thread
, FALSE
);
313 #if defined(CONFIG_SCHED_TRADITIONAL)
318 * Reset the scheduled priority of the thread
319 * according to its base priority if the
320 * thread has not been promoted or depressed.
322 * Called with the thread locked.
326 register thread_t thread
,
327 boolean_t override_depress
)
329 register int priority
;
331 if ( !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
332 (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) ||
333 override_depress
) ) {
334 if (thread
->sched_mode
== TH_MODE_TIMESHARE
)
335 do_priority_computation(thread
, priority
);
337 priority
= thread
->priority
;
339 set_sched_pri(thread
, priority
);
344 * compute_my_priority:
346 * Reset the scheduled priority for
347 * a timesharing thread.
349 * Only for use on the current thread
350 * if timesharing and not depressed.
352 * Called with the thread locked.
356 register thread_t thread
)
358 register int priority
;
360 do_priority_computation(thread
, priority
);
361 assert(thread
->runq
== PROCESSOR_NULL
);
362 thread
->sched_pri
= priority
;
366 * can_update_priority
368 * Make sure we don't do re-dispatches more frequently than a scheduler tick.
370 * Called with the thread locked.
376 if (sched_tick
== thread
->sched_stamp
)
385 * Perform housekeeping operations driven by scheduler tick.
387 * Called with the thread locked.
391 register thread_t thread
)
393 register unsigned ticks
;
394 register uint32_t delta
;
396 ticks
= sched_tick
- thread
->sched_stamp
;
398 thread
->sched_stamp
+= ticks
;
399 if (sched_use_combined_fgbg_decay
)
400 thread
->pri_shift
= sched_combined_fgbg_pri_shift
;
401 else if (thread
->max_priority
<= MAXPRI_THROTTLE
)
402 thread
->pri_shift
= sched_background_pri_shift
;
404 thread
->pri_shift
= sched_pri_shift
;
406 /* If requested, accelerate aging of sched_usage */
407 if (sched_decay_usage_age_factor
> 1)
408 ticks
*= sched_decay_usage_age_factor
;
411 * Gather cpu usage data.
413 thread_timer_delta(thread
, delta
);
414 if (ticks
< SCHED_DECAY_TICKS
) {
415 register struct shift_data
*shiftp
;
418 * Accumulate timesharing usage only
419 * during contention for processor
422 if (thread
->pri_shift
< INT8_MAX
)
423 thread
->sched_usage
+= delta
;
425 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
426 thread
->cpu_delta
= 0;
428 shiftp
= &sched_decay_shifts
[ticks
];
429 if (shiftp
->shift2
> 0) {
431 (thread
->cpu_usage
>> shiftp
->shift1
) +
432 (thread
->cpu_usage
>> shiftp
->shift2
);
433 thread
->sched_usage
=
434 (thread
->sched_usage
>> shiftp
->shift1
) +
435 (thread
->sched_usage
>> shiftp
->shift2
);
439 (thread
->cpu_usage
>> shiftp
->shift1
) -
440 (thread
->cpu_usage
>> -(shiftp
->shift2
));
441 thread
->sched_usage
=
442 (thread
->sched_usage
>> shiftp
->shift1
) -
443 (thread
->sched_usage
>> -(shiftp
->shift2
));
447 thread
->cpu_usage
= thread
->cpu_delta
= 0;
448 thread
->sched_usage
= 0;
452 * Check for fail-safe release.
454 if ( (thread
->sched_flags
& TH_SFLAG_FAILSAFE
) &&
455 mach_absolute_time() >= thread
->safe_release
) {
456 if (thread
->saved_mode
!= TH_MODE_TIMESHARE
) {
457 if (thread
->saved_mode
== TH_MODE_REALTIME
) {
458 thread
->priority
= BASEPRI_RTQUEUES
;
461 thread
->sched_mode
= thread
->saved_mode
;
462 thread
->saved_mode
= TH_MODE_NONE
;
464 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
467 if (!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
))
468 set_sched_pri(thread
, thread
->priority
);
471 thread
->sched_flags
&= ~TH_SFLAG_FAILSAFE
;
476 * Recompute scheduled priority if appropriate.
478 if ( (thread
->sched_mode
== TH_MODE_TIMESHARE
) &&
479 !(thread
->sched_flags
& TH_SFLAG_PROMOTED_MASK
) &&
480 !(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) ) {
481 register int new_pri
;
483 do_priority_computation(thread
, new_pri
);
484 if (new_pri
!= thread
->sched_pri
) {
485 boolean_t removed
= thread_run_queue_remove(thread
);
488 if (sched_use_combined_fgbg_decay
&& ((thread
)->task
->max_priority
> MAXPRI_THROTTLE
) && (new_pri
== MAXPRI_THROTTLE
)) {
489 /* with the alternate (new) algorithm, would we have decayed this far? */
490 int alt_pri
= thread
->priority
- (thread
->sched_usage
>> sched_pri_shift
);
491 if ((alt_pri
> new_pri
) && (sched_background_count
> 0)) {
492 printf("thread %p would have decayed to only %d instead of %d\n", thread
, alt_pri
, new_pri
);
497 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_DECAY_PRIORITY
)|DBG_FUNC_NONE
,
498 (uintptr_t)thread_tid(thread
),
503 thread
->sched_pri
= new_pri
;
506 thread_setrun(thread
, SCHED_TAILQ
);
513 #endif /* CONFIG_SCHED_TRADITIONAL */