]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/priority.c
d88ade25df761f5541508cde9bc8a669ec054396
2 * Copyright (c) 2000 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.
62 #include <mach/boolean.h>
63 #include <mach/kern_return.h>
64 #include <mach/machine.h>
65 #include <kern/host.h>
66 #include <kern/mach_param.h>
67 #include <kern/sched.h>
69 #include <kern/thread.h>
70 #include <kern/processor.h>
71 #include <machine/machparam.h>
73 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
74 /*** ??? Should this file be MK SP-specific? Or is it more general purpose? ***/
79 * USAGE_THRESHOLD is the amount by which usage must change to
80 * cause a priority shift that moves a thread between run queues.
85 #define USAGE_THRESHOLD (((1 << PRI_SHIFT) + (1 << PRI_SHIFT_2)) << (2 + SCHED_SHIFT))
86 #else /* PRI_SHIFT_2 > 0 */
87 #define USAGE_THRESHOLD (((1 << PRI_SHIFT) - (1 << -(PRI_SHIFT_2))) << (2 + SCHED_SHIFT))
88 #endif /* PRI_SHIFT_2 > 0 */
89 #else /* PRI_SHIFT_2 */
90 #define USAGE_THRESHOLD (1 << (PRI_SHIFT + 2 + SCHED_SHIFT))
91 #endif /* PRI_SHIFT_2 */
94 * thread_quantum_update:
96 * Recalculate the quantum and priority for a thread.
97 * The number of ticks that has elapsed since we were last called
98 * is passed as "nticks."
102 thread_quantum_update(
104 register thread_t thread
,
108 register int quantum
;
109 register processor_t myprocessor
;
110 register processor_set_t pset
;
113 myprocessor
= cpu_to_processor(mycpu
);
114 pset
= myprocessor
->processor_set
;
117 * Account for thread's utilization of these ticks.
118 * This assumes that there is *always* a current thread.
119 * When the processor is idle, it should be the idle thread.
123 * Update set_quantum and calculate the current quantum.
125 pset
->set_quantum
= pset
->machine_quantum
[
126 (pset
->runq
.count
> pset
->processor_count
) ?
127 pset
->processor_count
: pset
->runq
.count
];
129 if (myprocessor
->runq
.count
!= 0)
130 quantum
= min_quantum
;
132 quantum
= pset
->set_quantum
;
135 * Now recompute the priority of the thread if appropriate.
142 if (!(thread
->policy
& (POLICY_TIMESHARE
|POLICY_RR
|POLICY_FIFO
))) {
143 thread_unlock(thread
);
148 if (thread
->state
&TH_IDLE
) {
149 /* Don't try to time-slice idle threads */
150 myprocessor
->first_quantum
= TRUE
;
151 if (thread
->sched_stamp
!= sched_tick
)
152 update_priority(thread
);
153 thread_unlock(thread
);
159 myprocessor
->quantum
-= nticks
;
161 * Runtime quantum adjustment. Use quantum_adj_index
162 * to avoid synchronizing quantum expirations.
164 if ( quantum
!= myprocessor
->last_quantum
&&
165 pset
->processor_count
> 1 ) {
166 myprocessor
->last_quantum
= quantum
;
167 simple_lock(&pset
->quantum_adj_lock
);
168 quantum
= min_quantum
+ (pset
->quantum_adj_index
*
169 (quantum
- min_quantum
)) /
170 (pset
->processor_count
- 1);
171 if (++(pset
->quantum_adj_index
) >= pset
->processor_count
)
172 pset
->quantum_adj_index
= 0;
173 simple_unlock(&pset
->quantum_adj_lock
);
175 if (myprocessor
->quantum
<= 0) {
176 if (thread
->sched_stamp
!= sched_tick
)
177 update_priority(thread
);
179 if ( thread
->policy
== POLICY_TIMESHARE
&&
180 thread
->depress_priority
< 0 ) {
181 thread_timer_delta(thread
);
182 thread
->sched_usage
+= thread
->sched_delta
;
183 thread
->sched_delta
= 0;
184 compute_my_priority(thread
);
188 * This quantum is up, give this thread another.
190 myprocessor
->first_quantum
= FALSE
;
191 if (thread
->policy
== POLICY_TIMESHARE
)
192 myprocessor
->quantum
+= quantum
;
194 myprocessor
->quantum
+= min_quantum
;
197 * Recompute priority if appropriate.
200 if (thread
->sched_stamp
!= sched_tick
)
201 update_priority(thread
);
203 if ( thread
->policy
== POLICY_TIMESHARE
&&
204 thread
->depress_priority
< 0 ) {
205 thread_timer_delta(thread
);
206 if (thread
->sched_delta
>= USAGE_THRESHOLD
) {
207 thread
->sched_usage
+= thread
->sched_delta
;
208 thread
->sched_delta
= 0;
209 compute_my_priority(thread
);
214 thread_unlock(thread
);
218 * Check for and schedule ast if needed.