2 * Copyright (c) 2000-2007 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>
73 #include <kern/thread.h>
74 #include <kern/processor.h>
75 #include <machine/machparam.h>
78 * thread_quantum_expire:
80 * Recalculate the quantum and priority for a thread.
86 thread_quantum_expire(
87 timer_call_param_t p0
,
88 timer_call_param_t p1
)
90 processor_t processor
= p0
;
97 * Check for fail-safe trip.
99 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
100 uint64_t new_computation
;
102 new_computation
= processor
->quantum_end
;
103 new_computation
-= thread
->computation_epoch
;
104 if (new_computation
+ thread
->computation_metered
>
105 max_unsafe_computation
) {
107 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
108 thread
->priority
= DEPRESSPRI
;
110 thread
->safe_mode
|= TH_MODE_REALTIME
;
111 thread
->sched_mode
&= ~TH_MODE_REALTIME
;
116 thread
->safe_release
= sched_tick
+ sched_safe_duration
;
117 thread
->sched_mode
|= (TH_MODE_FAILSAFE
|TH_MODE_TIMESHARE
);
118 thread
->sched_mode
&= ~TH_MODE_PREEMPT
;
123 * Recompute scheduled priority if appropriate.
125 if (thread
->sched_stamp
!= sched_tick
)
126 update_priority(thread
);
128 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
129 register uint32_t delta
;
131 thread_timer_delta(thread
, delta
);
134 * Accumulate timesharing usage only
135 * during contention for processor
138 if (thread
->pri_shift
< INT8_MAX
)
139 thread
->sched_usage
+= delta
;
141 thread
->cpu_delta
+= delta
;
144 * Adjust the scheduled priority if
145 * the thread has not been promoted
146 * and is not depressed.
148 if ( !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
149 !(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) )
150 compute_my_priority(thread
);
153 processor
->current_pri
= thread
->sched_pri
;
156 * This quantum is up, give this thread another.
158 if (first_timeslice(processor
))
159 processor
->timeslice
--;
161 thread_quantum_init(thread
);
162 processor
->quantum_end
+= thread
->current_quantum
;
163 timer_call_enter1(&processor
->quantum_timer
,
164 thread
, processor
->quantum_end
);
167 * Context switch check.
169 if ((preempt
= csw_check(thread
, processor
)) != AST_NONE
)
172 processor_set_t pset
= processor
->processor_set
;
176 pset_hint_low(pset
, processor
);
177 pset_hint_high(pset
, processor
);
182 thread_unlock(thread
);
186 * Define shifts for simulating (5/8) ** n
188 * Shift structures for holding update shifts. Actual computation
189 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
190 * +/- is determined by the sign of shift 2.
197 #define SCHED_DECAY_TICKS 32
198 static struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
199 {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
200 {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
201 {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
202 {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
206 * do_priority_computation:
208 * Calculate the timesharing priority based upon usage and load.
210 #define do_priority_computation(thread, pri) \
212 (pri) = (thread)->priority /* start with base priority */ \
213 - ((thread)->sched_usage >> (thread)->pri_shift); \
214 if ((pri) < MINPRI_USER) \
215 (pri) = MINPRI_USER; \
217 if ((pri) > MAXPRI_KERNEL) \
218 (pri) = MAXPRI_KERNEL; \
224 * Set the base priority of the thread
225 * and reset its scheduled priority.
227 * Called with the thread locked.
231 register thread_t thread
,
232 register int priority
)
234 thread
->priority
= priority
;
235 compute_priority(thread
, FALSE
);
241 * Reset the scheduled priority of the thread
242 * according to its base priority if the
243 * thread has not been promoted or depressed.
245 * Called with the thread locked.
249 register thread_t thread
,
250 boolean_t override_depress
)
252 register int priority
;
254 if ( !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
255 (!(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) ||
256 override_depress
) ) {
257 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
258 do_priority_computation(thread
, priority
);
260 priority
= thread
->priority
;
262 set_sched_pri(thread
, priority
);
267 * compute_my_priority:
269 * Reset the scheduled priority for
270 * a timesharing thread.
272 * Only for use on the current thread
273 * if timesharing and not depressed.
275 * Called with the thread locked.
279 register thread_t thread
)
281 register int priority
;
283 do_priority_computation(thread
, priority
);
284 assert(thread
->runq
== PROCESSOR_NULL
);
285 thread
->sched_pri
= priority
;
291 * Perform housekeeping operations driven by scheduler tick.
293 * Called with the thread locked.
297 register thread_t thread
)
299 register unsigned ticks
;
300 register uint32_t delta
;
302 ticks
= sched_tick
- thread
->sched_stamp
;
304 thread
->sched_stamp
+= ticks
;
305 thread
->pri_shift
= sched_pri_shift
;
308 * Gather cpu usage data.
310 thread_timer_delta(thread
, delta
);
311 if (ticks
< SCHED_DECAY_TICKS
) {
312 register struct shift_data
*shiftp
;
315 * Accumulate timesharing usage only
316 * during contention for processor
319 if (thread
->pri_shift
< INT8_MAX
)
320 thread
->sched_usage
+= delta
;
322 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
323 thread
->cpu_delta
= 0;
325 shiftp
= &sched_decay_shifts
[ticks
];
326 if (shiftp
->shift2
> 0) {
328 (thread
->cpu_usage
>> shiftp
->shift1
) +
329 (thread
->cpu_usage
>> shiftp
->shift2
);
330 thread
->sched_usage
=
331 (thread
->sched_usage
>> shiftp
->shift1
) +
332 (thread
->sched_usage
>> shiftp
->shift2
);
336 (thread
->cpu_usage
>> shiftp
->shift1
) -
337 (thread
->cpu_usage
>> -(shiftp
->shift2
));
338 thread
->sched_usage
=
339 (thread
->sched_usage
>> shiftp
->shift1
) -
340 (thread
->sched_usage
>> -(shiftp
->shift2
));
344 thread
->cpu_usage
= thread
->cpu_delta
= 0;
345 thread
->sched_usage
= 0;
349 * Check for fail-safe release.
351 if ( (thread
->sched_mode
& TH_MODE_FAILSAFE
) &&
352 thread
->sched_stamp
>= thread
->safe_release
) {
353 if (!(thread
->safe_mode
& TH_MODE_TIMESHARE
)) {
354 if (thread
->safe_mode
& TH_MODE_REALTIME
) {
355 thread
->priority
= BASEPRI_RTQUEUES
;
357 thread
->sched_mode
|= TH_MODE_REALTIME
;
360 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
362 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
365 if (!(thread
->sched_mode
& TH_MODE_ISDEPRESSED
))
366 set_sched_pri(thread
, thread
->priority
);
369 thread
->safe_mode
= 0;
370 thread
->sched_mode
&= ~TH_MODE_FAILSAFE
;
374 * Recompute scheduled priority if appropriate.
376 if ( (thread
->sched_mode
& TH_MODE_TIMESHARE
) &&
377 !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
378 !(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) ) {
379 register int new_pri
;
381 do_priority_computation(thread
, new_pri
);
382 if (new_pri
!= thread
->sched_pri
) {
383 boolean_t removed
= run_queue_remove(thread
);
385 thread
->sched_pri
= new_pri
;
387 thread_setrun(thread
, SCHED_TAILQ
);