]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/priority.c
d88ade25df761f5541508cde9bc8a669ec054396
[apple/xnu.git] / osfmk / kern / priority.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: clock_prim.c
54 * Author: Avadis Tevanian, Jr.
55 * Date: 1986
56 *
57 * Clock primitives.
58 */
59
60 #include <cpus.h>
61
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>
68 #include <kern/spl.h>
69 #include <kern/thread.h>
70 #include <kern/processor.h>
71 #include <machine/machparam.h>
72 #include <kern/sf.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? ***/
75
76
77
78 /*
79 * USAGE_THRESHOLD is the amount by which usage must change to
80 * cause a priority shift that moves a thread between run queues.
81 */
82
83 #ifdef PRI_SHIFT_2
84 #if PRI_SHIFT_2 > 0
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 */
92
93 /*
94 * thread_quantum_update:
95 *
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."
99 */
100
101 void
102 thread_quantum_update(
103 register int mycpu,
104 register thread_t thread,
105 int nticks,
106 int state)
107 {
108 register int quantum;
109 register processor_t myprocessor;
110 register processor_set_t pset;
111 spl_t s;
112
113 myprocessor = cpu_to_processor(mycpu);
114 pset = myprocessor->processor_set;
115
116 /*
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.
120 */
121
122 /*
123 * Update set_quantum and calculate the current quantum.
124 */
125 pset->set_quantum = pset->machine_quantum[
126 (pset->runq.count > pset->processor_count) ?
127 pset->processor_count : pset->runq.count];
128
129 if (myprocessor->runq.count != 0)
130 quantum = min_quantum;
131 else
132 quantum = pset->set_quantum;
133
134 /*
135 * Now recompute the priority of the thread if appropriate.
136 */
137
138 {
139 s = splsched();
140 thread_lock(thread);
141
142 if (!(thread->policy & (POLICY_TIMESHARE|POLICY_RR|POLICY_FIFO))) {
143 thread_unlock(thread);
144 splx(s);
145 return;
146 }
147
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);
154 splx(s);
155 ast_check();
156 return;
157 }
158
159 myprocessor->quantum -= nticks;
160 /*
161 * Runtime quantum adjustment. Use quantum_adj_index
162 * to avoid synchronizing quantum expirations.
163 */
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);
174 }
175 if (myprocessor->quantum <= 0) {
176 if (thread->sched_stamp != sched_tick)
177 update_priority(thread);
178 else
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);
185 }
186
187 /*
188 * This quantum is up, give this thread another.
189 */
190 myprocessor->first_quantum = FALSE;
191 if (thread->policy == POLICY_TIMESHARE)
192 myprocessor->quantum += quantum;
193 else
194 myprocessor->quantum += min_quantum;
195 }
196 /*
197 * Recompute priority if appropriate.
198 */
199 else {
200 if (thread->sched_stamp != sched_tick)
201 update_priority(thread);
202 else
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);
210 }
211 }
212 }
213
214 thread_unlock(thread);
215 splx(s);
216
217 /*
218 * Check for and schedule ast if needed.
219 */
220 ast_check();
221 }
222 }