2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr.
60 #include <mach/boolean.h>
61 #include <mach/kern_return.h>
62 #include <mach/machine.h>
63 #include <kern/host.h>
64 #include <kern/mach_param.h>
65 #include <kern/sched.h>
67 #include <kern/thread.h>
68 #include <kern/processor.h>
69 #include <machine/machparam.h>
72 * thread_quantum_expire:
74 * Recalculate the quantum and priority for a thread.
78 thread_quantum_expire(
79 timer_call_param_t p0
,
80 timer_call_param_t p1
)
82 register processor_t myprocessor
= p0
;
83 register thread_t thread
= p1
;
90 * Check for fail-safe trip.
92 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
93 uint64_t new_computation
;
95 new_computation
= myprocessor
->quantum_end
;
96 new_computation
-= thread
->computation_epoch
;
97 if (new_computation
+ thread
->computation_metered
>
98 max_unsafe_computation
) {
100 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
101 thread
->priority
= DEPRESSPRI
;
103 thread
->safe_mode
|= TH_MODE_REALTIME
;
104 thread
->sched_mode
&= ~TH_MODE_REALTIME
;
107 pset_share_incr(thread
->processor_set
);
109 thread
->safe_release
= sched_tick
+ sched_safe_duration
;
110 thread
->sched_mode
|= (TH_MODE_FAILSAFE
|TH_MODE_TIMESHARE
);
111 thread
->sched_mode
&= ~TH_MODE_PREEMPT
;
116 * Recompute scheduled priority if appropriate.
118 if (thread
->sched_stamp
!= sched_tick
)
119 update_priority(thread
);
121 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
122 register uint32_t delta
;
124 thread_timer_delta(thread
, delta
);
127 * Accumulate timesharing usage only
128 * during contention for processor
131 if (thread
->pri_shift
< INT8_MAX
)
132 thread
->sched_usage
+= delta
;
134 thread
->cpu_delta
+= delta
;
137 * Adjust the scheduled priority if
138 * the thread has not been promoted
139 * and is not depressed.
141 if ( !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
142 !(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) )
143 compute_my_priority(thread
);
147 * This quantum is up, give this thread another.
149 if (first_timeslice(myprocessor
))
150 myprocessor
->timeslice
--;
152 thread_quantum_init(thread
);
153 myprocessor
->quantum_end
+= thread
->current_quantum
;
154 timer_call_enter1(&myprocessor
->quantum_timer
,
155 thread
, myprocessor
->quantum_end
);
157 thread_unlock(thread
);
160 * Check for and schedule ast if needed.
162 ast_check(myprocessor
);
168 * Define shifts for simulating (5/8) ** n
170 * Shift structures for holding update shifts. Actual computation
171 * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
172 * +/- is determined by the sign of shift 2.
179 #define SCHED_DECAY_TICKS 32
180 static struct shift_data sched_decay_shifts
[SCHED_DECAY_TICKS
] = {
181 {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
182 {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
183 {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
184 {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
188 * do_priority_computation:
190 * Calculate the timesharing priority based upon usage and load.
192 #define do_priority_computation(thread, pri) \
194 (pri) = (thread)->priority /* start with base priority */ \
195 - ((thread)->sched_usage >> (thread)->pri_shift); \
196 if ((pri) < MINPRI_USER) \
197 (pri) = MINPRI_USER; \
199 if ((pri) > MAXPRI_KERNEL) \
200 (pri) = MAXPRI_KERNEL; \
206 * Set the base priority of the thread
207 * and reset its scheduled priority.
209 * Called with the thread locked.
213 register thread_t thread
,
214 register int priority
)
216 thread
->priority
= priority
;
217 compute_priority(thread
, FALSE
);
223 * Reset the scheduled priority of the thread
224 * according to its base priority if the
225 * thread has not been promoted or depressed.
227 * Called with the thread locked.
231 register thread_t thread
,
232 boolean_t override_depress
)
234 register int priority
;
236 if ( !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
237 (!(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) ||
238 override_depress
) ) {
239 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
240 do_priority_computation(thread
, priority
);
242 priority
= thread
->priority
;
244 set_sched_pri(thread
, priority
);
249 * compute_my_priority:
251 * Reset the scheduled priority for
252 * a timesharing thread.
254 * Only for use on the current thread
255 * if timesharing and not depressed.
257 * Called with the thread locked.
261 register thread_t thread
)
263 register int priority
;
265 do_priority_computation(thread
, priority
);
266 assert(thread
->runq
== RUN_QUEUE_NULL
);
267 thread
->sched_pri
= priority
;
273 * Perform housekeeping operations driven by scheduler tick.
275 * Called with the thread locked.
279 register thread_t thread
)
281 register unsigned ticks
;
282 register uint32_t delta
;
284 ticks
= sched_tick
- thread
->sched_stamp
;
286 thread
->sched_stamp
+= ticks
;
287 thread
->pri_shift
= thread
->processor_set
->pri_shift
;
290 * Gather cpu usage data.
292 thread_timer_delta(thread
, delta
);
293 if (ticks
< SCHED_DECAY_TICKS
) {
294 register struct shift_data
*shiftp
;
297 * Accumulate timesharing usage only
298 * during contention for processor
301 if (thread
->pri_shift
< INT8_MAX
)
302 thread
->sched_usage
+= delta
;
304 thread
->cpu_usage
+= delta
+ thread
->cpu_delta
;
305 thread
->cpu_delta
= 0;
307 shiftp
= &sched_decay_shifts
[ticks
];
308 if (shiftp
->shift2
> 0) {
310 (thread
->cpu_usage
>> shiftp
->shift1
) +
311 (thread
->cpu_usage
>> shiftp
->shift2
);
312 thread
->sched_usage
=
313 (thread
->sched_usage
>> shiftp
->shift1
) +
314 (thread
->sched_usage
>> shiftp
->shift2
);
318 (thread
->cpu_usage
>> shiftp
->shift1
) -
319 (thread
->cpu_usage
>> -(shiftp
->shift2
));
320 thread
->sched_usage
=
321 (thread
->sched_usage
>> shiftp
->shift1
) -
322 (thread
->sched_usage
>> -(shiftp
->shift2
));
326 thread
->cpu_usage
= thread
->cpu_delta
= 0;
327 thread
->sched_usage
= 0;
331 * Check for fail-safe release.
333 if ( (thread
->sched_mode
& TH_MODE_FAILSAFE
) &&
334 thread
->sched_stamp
>= thread
->safe_release
) {
335 if (!(thread
->safe_mode
& TH_MODE_TIMESHARE
)) {
336 if (thread
->safe_mode
& TH_MODE_REALTIME
) {
337 thread
->priority
= BASEPRI_RTQUEUES
;
339 thread
->sched_mode
|= TH_MODE_REALTIME
;
342 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
344 if (thread
->state
& TH_RUN
)
345 pset_share_decr(thread
->processor_set
);
347 if (!(thread
->sched_mode
& TH_MODE_ISDEPRESSED
))
348 set_sched_pri(thread
, thread
->priority
);
351 thread
->safe_mode
= 0;
352 thread
->sched_mode
&= ~TH_MODE_FAILSAFE
;
356 * Recompute scheduled priority if appropriate.
358 if ( (thread
->sched_mode
& TH_MODE_TIMESHARE
) &&
359 !(thread
->sched_mode
& TH_MODE_PROMOTED
) &&
360 !(thread
->sched_mode
& TH_MODE_ISDEPRESSED
) ) {
361 register int new_pri
;
363 do_priority_computation(thread
, new_pri
);
364 if (new_pri
!= thread
->sched_pri
) {
367 runq
= run_queue_remove(thread
);
368 thread
->sched_pri
= new_pri
;
369 if (runq
!= RUN_QUEUE_NULL
)
370 thread_setrun(thread
, SCHED_TAILQ
);