Rev 167 | Rev 184 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 167 | Rev 170 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <proc/scheduler.h> |
29 | #include <proc/scheduler.h> |
30 | #include <proc/thread.h> |
30 | #include <proc/thread.h> |
31 | #include <proc/task.h> |
31 | #include <proc/task.h> |
32 | #include <cpu.h> |
32 | #include <cpu.h> |
33 | #include <mm/vm.h> |
33 | #include <mm/vm.h> |
34 | #include <config.h> |
34 | #include <config.h> |
35 | #include <context.h> |
35 | #include <context.h> |
36 | #include <func.h> |
36 | #include <func.h> |
37 | #include <arch.h> |
37 | #include <arch.h> |
38 | #include <arch/asm.h> |
38 | #include <arch/asm.h> |
39 | #include <list.h> |
39 | #include <list.h> |
40 | #include <panic.h> |
40 | #include <panic.h> |
41 | #include <typedefs.h> |
41 | #include <typedefs.h> |
42 | #include <mm/page.h> |
42 | #include <mm/page.h> |
43 | #include <synch/spinlock.h> |
43 | #include <synch/spinlock.h> |
44 | #include <arch/faddr.h> |
44 | #include <arch/faddr.h> |
45 | #include <arch/atomic.h> |
45 | #include <arch/atomic.h> |
46 | 46 | ||
47 | volatile int nrdy; |
47 | volatile int nrdy; |
48 | 48 | ||
49 | 49 | ||
50 | /** Take actions before new thread runs |
50 | /** Take actions before new thread runs |
51 | * |
51 | * |
52 | * Perform actions that need to be |
52 | * Perform actions that need to be |
53 | * taken before the newly selected |
53 | * taken before the newly selected |
54 | * tread is passed control. |
54 | * tread is passed control. |
55 | * |
55 | * |
56 | */ |
56 | */ |
57 | void before_thread_runs(void) |
57 | void before_thread_runs(void) |
58 | { |
58 | { |
59 | before_thread_runs_arch(); |
59 | before_thread_runs_arch(); |
60 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
60 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
61 | } |
61 | } |
62 | 62 | ||
63 | 63 | ||
64 | /** Initialize scheduler |
64 | /** Initialize scheduler |
65 | * |
65 | * |
66 | * Initialize kernel scheduler. |
66 | * Initialize kernel scheduler. |
67 | * |
67 | * |
68 | */ |
68 | */ |
69 | void scheduler_init(void) |
69 | void scheduler_init(void) |
70 | { |
70 | { |
71 | } |
71 | } |
72 | 72 | ||
73 | 73 | ||
74 | /** Get thread to be scheduled |
74 | /** Get thread to be scheduled |
75 | * |
75 | * |
76 | * Get the optimal thread to be scheduled |
76 | * Get the optimal thread to be scheduled |
77 | * according to thread accounting and scheduler |
77 | * according to thread accounting and scheduler |
78 | * policy. |
78 | * policy. |
79 | * |
79 | * |
80 | * @return Thread to be scheduled. |
80 | * @return Thread to be scheduled. |
81 | * |
81 | * |
82 | */ |
82 | */ |
83 | struct thread *find_best_thread(void) |
83 | struct thread *find_best_thread(void) |
84 | { |
84 | { |
85 | thread_t *t; |
85 | thread_t *t; |
86 | runq_t *r; |
86 | runq_t *r; |
87 | int i, n; |
87 | int i, n; |
88 | 88 | ||
89 | loop: |
89 | loop: |
90 | cpu_priority_high(); |
90 | cpu_priority_high(); |
91 | 91 | ||
92 | spinlock_lock(&CPU->lock); |
92 | spinlock_lock(&CPU->lock); |
93 | n = CPU->nrdy; |
93 | n = CPU->nrdy; |
94 | spinlock_unlock(&CPU->lock); |
94 | spinlock_unlock(&CPU->lock); |
95 | 95 | ||
96 | cpu_priority_low(); |
96 | cpu_priority_low(); |
97 | 97 | ||
98 | if (n == 0) { |
98 | if (n == 0) { |
99 | #ifdef __SMP__ |
99 | #ifdef __SMP__ |
100 | /* |
100 | /* |
101 | * If the load balancing thread is not running, wake it up and |
101 | * If the load balancing thread is not running, wake it up and |
102 | * set CPU-private flag that the kcpulb has been started. |
102 | * set CPU-private flag that the kcpulb has been started. |
103 | */ |
103 | */ |
104 | if (test_and_set(&CPU->kcpulbstarted) == 0) { |
104 | if (test_and_set(&CPU->kcpulbstarted) == 0) { |
105 | waitq_wakeup(&CPU->kcpulb_wq, 0); |
105 | waitq_wakeup(&CPU->kcpulb_wq, 0); |
106 | goto loop; |
106 | goto loop; |
107 | } |
107 | } |
108 | #endif /* __SMP__ */ |
108 | #endif /* __SMP__ */ |
109 | 109 | ||
110 | /* |
110 | /* |
111 | * For there was nothing to run, the CPU goes to sleep |
111 | * For there was nothing to run, the CPU goes to sleep |
112 | * until a hardware interrupt or an IPI comes. |
112 | * until a hardware interrupt or an IPI comes. |
113 | * This improves energy saving and hyperthreading. |
113 | * This improves energy saving and hyperthreading. |
114 | * On the other hand, several hardware interrupts can be ignored. |
114 | * On the other hand, several hardware interrupts can be ignored. |
115 | */ |
115 | */ |
116 | cpu_sleep(); |
116 | cpu_sleep(); |
117 | goto loop; |
117 | goto loop; |
118 | } |
118 | } |
119 | 119 | ||
120 | cpu_priority_high(); |
120 | cpu_priority_high(); |
121 | 121 | ||
122 | i = 0; |
122 | i = 0; |
123 | retry: |
123 | retry: |
124 | for (; i<RQ_COUNT; i++) { |
124 | for (; i<RQ_COUNT; i++) { |
125 | r = &CPU->rq[i]; |
125 | r = &CPU->rq[i]; |
126 | spinlock_lock(&r->lock); |
126 | spinlock_lock(&r->lock); |
127 | if (r->n == 0) { |
127 | if (r->n == 0) { |
128 | /* |
128 | /* |
129 | * If this queue is empty, try a lower-priority queue. |
129 | * If this queue is empty, try a lower-priority queue. |
130 | */ |
130 | */ |
131 | spinlock_unlock(&r->lock); |
131 | spinlock_unlock(&r->lock); |
132 | continue; |
132 | continue; |
133 | } |
133 | } |
134 | 134 | ||
135 | /* avoid deadlock with relink_rq() */ |
135 | /* avoid deadlock with relink_rq() */ |
136 | if (!spinlock_trylock(&CPU->lock)) { |
136 | if (!spinlock_trylock(&CPU->lock)) { |
137 | /* |
137 | /* |
138 | * Unlock r and try again. |
138 | * Unlock r and try again. |
139 | */ |
139 | */ |
140 | spinlock_unlock(&r->lock); |
140 | spinlock_unlock(&r->lock); |
141 | goto retry; |
141 | goto retry; |
142 | } |
142 | } |
143 | CPU->nrdy--; |
143 | CPU->nrdy--; |
144 | spinlock_unlock(&CPU->lock); |
144 | spinlock_unlock(&CPU->lock); |
145 | 145 | ||
146 | atomic_dec(&nrdy); |
146 | atomic_dec(&nrdy); |
147 | r->n--; |
147 | r->n--; |
148 | 148 | ||
149 | /* |
149 | /* |
150 | * Take the first thread from the queue. |
150 | * Take the first thread from the queue. |
151 | */ |
151 | */ |
152 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
152 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
153 | list_remove(&t->rq_link); |
153 | list_remove(&t->rq_link); |
154 | 154 | ||
155 | spinlock_unlock(&r->lock); |
155 | spinlock_unlock(&r->lock); |
156 | 156 | ||
157 | spinlock_lock(&t->lock); |
157 | spinlock_lock(&t->lock); |
158 | t->cpu = CPU; |
158 | t->cpu = CPU; |
159 | 159 | ||
160 | t->ticks = us2ticks((i+1)*10000); |
160 | t->ticks = us2ticks((i+1)*10000); |
161 | t->pri = i; /* eventually correct rq index */ |
161 | t->pri = i; /* eventually correct rq index */ |
162 | 162 | ||
163 | /* |
163 | /* |
164 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
164 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
165 | */ |
165 | */ |
166 | t->flags &= ~X_STOLEN; |
166 | t->flags &= ~X_STOLEN; |
167 | spinlock_unlock(&t->lock); |
167 | spinlock_unlock(&t->lock); |
168 | 168 | ||
169 | return t; |
169 | return t; |
170 | } |
170 | } |
171 | goto loop; |
171 | goto loop; |
172 | 172 | ||
173 | } |
173 | } |
174 | 174 | ||
175 | 175 | ||
176 | /** Prevent rq starvation |
176 | /** Prevent rq starvation |
177 | * |
177 | * |
178 | * Prevent low priority threads from starving in rq's. |
178 | * Prevent low priority threads from starving in rq's. |
179 | * |
179 | * |
180 | * When the function decides to relink rq's, it reconnects |
180 | * When the function decides to relink rq's, it reconnects |
181 | * respective pointers so that in result threads with 'pri' |
181 | * respective pointers so that in result threads with 'pri' |
182 | * greater or equal 'start' are moved to a higher-priority queue. |
182 | * greater or equal 'start' are moved to a higher-priority queue. |
183 | * |
183 | * |
184 | * @param start Threshold priority. |
184 | * @param start Threshold priority. |
185 | * |
185 | * |
186 | */ |
186 | */ |
187 | void relink_rq(int start) |
187 | void relink_rq(int start) |
188 | { |
188 | { |
189 | link_t head; |
189 | link_t head; |
190 | runq_t *r; |
190 | runq_t *r; |
191 | int i, n; |
191 | int i, n; |
192 | 192 | ||
193 | list_initialize(&head); |
193 | list_initialize(&head); |
194 | spinlock_lock(&CPU->lock); |
194 | spinlock_lock(&CPU->lock); |
195 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
195 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
196 | for (i = start; i<RQ_COUNT-1; i++) { |
196 | for (i = start; i<RQ_COUNT-1; i++) { |
197 | /* remember and empty rq[i + 1] */ |
197 | /* remember and empty rq[i + 1] */ |
198 | r = &CPU->rq[i + 1]; |
198 | r = &CPU->rq[i + 1]; |
199 | spinlock_lock(&r->lock); |
199 | spinlock_lock(&r->lock); |
200 | list_concat(&head, &r->rq_head); |
200 | list_concat(&head, &r->rq_head); |
201 | n = r->n; |
201 | n = r->n; |
202 | r->n = 0; |
202 | r->n = 0; |
203 | spinlock_unlock(&r->lock); |
203 | spinlock_unlock(&r->lock); |
204 | 204 | ||
205 | /* append rq[i + 1] to rq[i] */ |
205 | /* append rq[i + 1] to rq[i] */ |
206 | r = &CPU->rq[i]; |
206 | r = &CPU->rq[i]; |
207 | spinlock_lock(&r->lock); |
207 | spinlock_lock(&r->lock); |
208 | list_concat(&r->rq_head, &head); |
208 | list_concat(&r->rq_head, &head); |
209 | r->n += n; |
209 | r->n += n; |
210 | spinlock_unlock(&r->lock); |
210 | spinlock_unlock(&r->lock); |
211 | } |
211 | } |
212 | CPU->needs_relink = 0; |
212 | CPU->needs_relink = 0; |
213 | } |
213 | } |
214 | spinlock_unlock(&CPU->lock); |
214 | spinlock_unlock(&CPU->lock); |
215 | 215 | ||
216 | } |
216 | } |
217 | 217 | ||
218 | 218 | ||
219 | /** The scheduler |
219 | /** The scheduler |
220 | * |
220 | * |
221 | * The thread scheduling procedure. |
221 | * The thread scheduling procedure. |
222 | * |
222 | * |
223 | */ |
223 | */ |
224 | void scheduler(void) |
224 | void scheduler(void) |
225 | { |
225 | { |
226 | volatile pri_t pri; |
226 | volatile pri_t pri; |
227 | 227 | ||
228 | pri = cpu_priority_high(); |
228 | pri = cpu_priority_high(); |
229 | 229 | ||
230 | if (haltstate) |
230 | if (haltstate) |
231 | halt(); |
231 | halt(); |
232 | 232 | ||
233 | if (THREAD) { |
233 | if (THREAD) { |
234 | spinlock_lock(&THREAD->lock); |
234 | spinlock_lock(&THREAD->lock); |
235 | fpu_context_save(&(THREAD->saved_fpu_context)); |
235 | fpu_context_save(&(THREAD->saved_fpu_context)); |
236 | if (!context_save(&THREAD->saved_context)) { |
236 | if (!context_save(&THREAD->saved_context)) { |
237 | /* |
237 | /* |
238 | * This is the place where threads leave scheduler(); |
238 | * This is the place where threads leave scheduler(); |
239 | */ |
239 | */ |
240 | before_thread_runs(); |
240 | before_thread_runs(); |
241 | spinlock_unlock(&THREAD->lock); |
241 | spinlock_unlock(&THREAD->lock); |
242 | cpu_priority_restore(THREAD->saved_context.pri); |
242 | cpu_priority_restore(THREAD->saved_context.pri); |
243 | return; |
243 | return; |
244 | } |
244 | } |
- | 245 | ||
- | 246 | /* |
|
- | 247 | * CPU priority of preempted thread is recorded here |
|
- | 248 | * to facilitate scheduler() invocations from |
|
- | 249 | * cpu_priority_high()'ed code (e.g. waitq_sleep_timeout()). |
|
- | 250 | */ |
|
245 | THREAD->saved_context.pri = pri; |
251 | THREAD->saved_context.pri = pri; |
246 | } |
252 | } |
247 | 253 | ||
248 | /* |
254 | /* |
249 | * We may not keep the old stack. |
255 | * We may not keep the old stack. |
250 | * Reason: If we kept the old stack and got blocked, for instance, in |
256 | * Reason: If we kept the old stack and got blocked, for instance, in |
251 | * find_best_thread(), the old thread could get rescheduled by another |
257 | * find_best_thread(), the old thread could get rescheduled by another |
252 | * CPU and overwrite the part of its own stack that was also used by |
258 | * CPU and overwrite the part of its own stack that was also used by |
253 | * the scheduler on this CPU. |
259 | * the scheduler on this CPU. |
254 | * |
260 | * |
255 | * Moreover, we have to bypass the compiler-generated POP sequence |
261 | * Moreover, we have to bypass the compiler-generated POP sequence |
256 | * which is fooled by SP being set to the very top of the stack. |
262 | * which is fooled by SP being set to the very top of the stack. |
257 | * Therefore the scheduler() function continues in |
263 | * Therefore the scheduler() function continues in |
258 | * scheduler_separated_stack(). |
264 | * scheduler_separated_stack(). |
259 | */ |
265 | */ |
260 | context_save(&CPU->saved_context); |
266 | context_save(&CPU->saved_context); |
261 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE); |
267 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE); |
262 | context_restore(&CPU->saved_context); |
268 | context_restore(&CPU->saved_context); |
263 | /* not reached */ |
269 | /* not reached */ |
264 | } |
270 | } |
265 | 271 | ||
266 | 272 | ||
267 | /** Scheduler stack switch wrapper |
273 | /** Scheduler stack switch wrapper |
268 | * |
274 | * |
269 | * Second part of the scheduler() function |
275 | * Second part of the scheduler() function |
270 | * using new stack. Handling the actual context |
276 | * using new stack. Handling the actual context |
271 | * switch to a new thread. |
277 | * switch to a new thread. |
272 | * |
278 | * |
273 | */ |
279 | */ |
274 | void scheduler_separated_stack(void) |
280 | void scheduler_separated_stack(void) |
275 | { |
281 | { |
276 | int priority; |
282 | int priority; |
277 | 283 | ||
278 | if (THREAD) { |
284 | if (THREAD) { |
279 | switch (THREAD->state) { |
285 | switch (THREAD->state) { |
280 | case Running: |
286 | case Running: |
281 | THREAD->state = Ready; |
287 | THREAD->state = Ready; |
282 | spinlock_unlock(&THREAD->lock); |
288 | spinlock_unlock(&THREAD->lock); |
283 | thread_ready(THREAD); |
289 | thread_ready(THREAD); |
284 | break; |
290 | break; |
285 | 291 | ||
286 | case Exiting: |
292 | case Exiting: |
287 | frame_free((__address) THREAD->kstack); |
293 | frame_free((__address) THREAD->kstack); |
288 | if (THREAD->ustack) { |
294 | if (THREAD->ustack) { |
289 | frame_free((__address) THREAD->ustack); |
295 | frame_free((__address) THREAD->ustack); |
290 | } |
296 | } |
291 | 297 | ||
292 | /* |
298 | /* |
293 | * Detach from the containing task. |
299 | * Detach from the containing task. |
294 | */ |
300 | */ |
295 | spinlock_lock(&TASK->lock); |
301 | spinlock_lock(&TASK->lock); |
296 | list_remove(&THREAD->th_link); |
302 | list_remove(&THREAD->th_link); |
297 | spinlock_unlock(&TASK->lock); |
303 | spinlock_unlock(&TASK->lock); |
298 | 304 | ||
299 | spinlock_unlock(&THREAD->lock); |
305 | spinlock_unlock(&THREAD->lock); |
300 | 306 | ||
301 | spinlock_lock(&threads_lock); |
307 | spinlock_lock(&threads_lock); |
302 | list_remove(&THREAD->threads_link); |
308 | list_remove(&THREAD->threads_link); |
303 | spinlock_unlock(&threads_lock); |
309 | spinlock_unlock(&threads_lock); |
304 | 310 | ||
305 | spinlock_lock(&CPU->lock); |
311 | spinlock_lock(&CPU->lock); |
306 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
312 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
307 | spinlock_unlock(&CPU->lock); |
313 | spinlock_unlock(&CPU->lock); |
308 | 314 | ||
309 | free(THREAD); |
315 | free(THREAD); |
310 | 316 | ||
311 | break; |
317 | break; |
312 | 318 | ||
313 | case Sleeping: |
319 | case Sleeping: |
314 | /* |
320 | /* |
315 | * Prefer the thread after it's woken up. |
321 | * Prefer the thread after it's woken up. |
316 | */ |
322 | */ |
317 | THREAD->pri = -1; |
323 | THREAD->pri = -1; |
318 | 324 | ||
319 | /* |
325 | /* |
320 | * We need to release wq->lock which we locked in waitq_sleep(). |
326 | * We need to release wq->lock which we locked in waitq_sleep(). |
321 | * Address of wq->lock is kept in THREAD->sleep_queue. |
327 | * Address of wq->lock is kept in THREAD->sleep_queue. |
322 | */ |
328 | */ |
323 | spinlock_unlock(&THREAD->sleep_queue->lock); |
329 | spinlock_unlock(&THREAD->sleep_queue->lock); |
324 | 330 | ||
325 | /* |
331 | /* |
326 | * Check for possible requests for out-of-context invocation. |
332 | * Check for possible requests for out-of-context invocation. |
327 | */ |
333 | */ |
328 | if (THREAD->call_me) { |
334 | if (THREAD->call_me) { |
329 | THREAD->call_me(THREAD->call_me_with); |
335 | THREAD->call_me(THREAD->call_me_with); |
330 | THREAD->call_me = NULL; |
336 | THREAD->call_me = NULL; |
331 | THREAD->call_me_with = NULL; |
337 | THREAD->call_me_with = NULL; |
332 | } |
338 | } |
333 | 339 | ||
334 | spinlock_unlock(&THREAD->lock); |
340 | spinlock_unlock(&THREAD->lock); |
335 | 341 | ||
336 | break; |
342 | break; |
337 | 343 | ||
338 | default: |
344 | default: |
339 | /* |
345 | /* |
340 | * Entering state is unexpected. |
346 | * Entering state is unexpected. |
341 | */ |
347 | */ |
342 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
348 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
343 | break; |
349 | break; |
344 | } |
350 | } |
345 | THREAD = NULL; |
351 | THREAD = NULL; |
346 | } |
352 | } |
347 | 353 | ||
348 | THREAD = find_best_thread(); |
354 | THREAD = find_best_thread(); |
349 | 355 | ||
350 | spinlock_lock(&THREAD->lock); |
356 | spinlock_lock(&THREAD->lock); |
351 | priority = THREAD->pri; |
357 | priority = THREAD->pri; |
352 | spinlock_unlock(&THREAD->lock); |
358 | spinlock_unlock(&THREAD->lock); |
353 | 359 | ||
354 | relink_rq(priority); |
360 | relink_rq(priority); |
355 | 361 | ||
356 | spinlock_lock(&THREAD->lock); |
362 | spinlock_lock(&THREAD->lock); |
357 | 363 | ||
358 | /* |
364 | /* |
359 | * If both the old and the new task are the same, lots of work is avoided. |
365 | * If both the old and the new task are the same, lots of work is avoided. |
360 | */ |
366 | */ |
361 | if (TASK != THREAD->task) { |
367 | if (TASK != THREAD->task) { |
362 | vm_t *m1 = NULL; |
368 | vm_t *m1 = NULL; |
363 | vm_t *m2; |
369 | vm_t *m2; |
364 | 370 | ||
365 | if (TASK) { |
371 | if (TASK) { |
366 | spinlock_lock(&TASK->lock); |
372 | spinlock_lock(&TASK->lock); |
367 | m1 = TASK->vm; |
373 | m1 = TASK->vm; |
368 | spinlock_unlock(&TASK->lock); |
374 | spinlock_unlock(&TASK->lock); |
369 | } |
375 | } |
370 | 376 | ||
371 | spinlock_lock(&THREAD->task->lock); |
377 | spinlock_lock(&THREAD->task->lock); |
372 | m2 = THREAD->task->vm; |
378 | m2 = THREAD->task->vm; |
373 | spinlock_unlock(&THREAD->task->lock); |
379 | spinlock_unlock(&THREAD->task->lock); |
374 | 380 | ||
375 | /* |
381 | /* |
376 | * Note that it is possible for two tasks to share one vm mapping. |
382 | * Note that it is possible for two tasks to share one vm mapping. |
377 | */ |
383 | */ |
378 | if (m1 != m2) { |
384 | if (m1 != m2) { |
379 | /* |
385 | /* |
380 | * Both tasks and vm mappings are different. |
386 | * Both tasks and vm mappings are different. |
381 | * Replace the old one with the new one. |
387 | * Replace the old one with the new one. |
382 | */ |
388 | */ |
383 | vm_install(m2); |
389 | vm_install(m2); |
384 | } |
390 | } |
385 | TASK = THREAD->task; |
391 | TASK = THREAD->task; |
386 | } |
392 | } |
387 | 393 | ||
388 | THREAD->state = Running; |
394 | THREAD->state = Running; |
389 | 395 | ||
390 | #ifdef SCHEDULER_VERBOSE |
396 | #ifdef SCHEDULER_VERBOSE |
391 | printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy); |
397 | printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy); |
392 | #endif |
398 | #endif |
393 | 399 | ||
394 | context_restore(&THREAD->saved_context); |
400 | context_restore(&THREAD->saved_context); |
395 | /* not reached */ |
401 | /* not reached */ |
396 | } |
402 | } |
397 | 403 | ||
398 | 404 | ||
399 | #ifdef __SMP__ |
405 | #ifdef __SMP__ |
400 | /** Load balancing thread |
406 | /** Load balancing thread |
401 | * |
407 | * |
402 | * SMP load balancing thread, supervising thread supplies |
408 | * SMP load balancing thread, supervising thread supplies |
403 | * for the CPU it's wired to. |
409 | * for the CPU it's wired to. |
404 | * |
410 | * |
405 | * @param arg Generic thread argument (unused). |
411 | * @param arg Generic thread argument (unused). |
406 | * |
412 | * |
407 | */ |
413 | */ |
408 | void kcpulb(void *arg) |
414 | void kcpulb(void *arg) |
409 | { |
415 | { |
410 | thread_t *t; |
416 | thread_t *t; |
411 | int count, i, j, k = 0; |
417 | int count, i, j, k = 0; |
412 | pri_t pri; |
418 | pri_t pri; |
413 | 419 | ||
414 | loop: |
420 | loop: |
415 | /* |
421 | /* |
416 | * Sleep until there's some work to do. |
422 | * Sleep until there's some work to do. |
417 | */ |
423 | */ |
418 | waitq_sleep(&CPU->kcpulb_wq); |
424 | waitq_sleep(&CPU->kcpulb_wq); |
419 | 425 | ||
420 | not_satisfied: |
426 | not_satisfied: |
421 | /* |
427 | /* |
422 | * Calculate the number of threads that will be migrated/stolen from |
428 | * Calculate the number of threads that will be migrated/stolen from |
423 | * other CPU's. Note that situation can have changed between two |
429 | * other CPU's. Note that situation can have changed between two |
424 | * passes. Each time get the most up to date counts. |
430 | * passes. Each time get the most up to date counts. |
425 | */ |
431 | */ |
426 | pri = cpu_priority_high(); |
432 | pri = cpu_priority_high(); |
427 | spinlock_lock(&CPU->lock); |
433 | spinlock_lock(&CPU->lock); |
428 | count = nrdy / config.cpu_active; |
434 | count = nrdy / config.cpu_active; |
429 | count -= CPU->nrdy; |
435 | count -= CPU->nrdy; |
430 | spinlock_unlock(&CPU->lock); |
436 | spinlock_unlock(&CPU->lock); |
431 | cpu_priority_restore(pri); |
437 | cpu_priority_restore(pri); |
432 | 438 | ||
433 | if (count <= 0) |
439 | if (count <= 0) |
434 | goto satisfied; |
440 | goto satisfied; |
435 | 441 | ||
436 | /* |
442 | /* |
437 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
443 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
438 | */ |
444 | */ |
439 | for (j=RQ_COUNT-1; j >= 0; j--) { |
445 | for (j=RQ_COUNT-1; j >= 0; j--) { |
440 | for (i=0; i < config.cpu_active; i++) { |
446 | for (i=0; i < config.cpu_active; i++) { |
441 | link_t *l; |
447 | link_t *l; |
442 | runq_t *r; |
448 | runq_t *r; |
443 | cpu_t *cpu; |
449 | cpu_t *cpu; |
444 | 450 | ||
445 | cpu = &cpus[(i + k) % config.cpu_active]; |
451 | cpu = &cpus[(i + k) % config.cpu_active]; |
446 | 452 | ||
447 | /* |
453 | /* |
448 | * Not interested in ourselves. |
454 | * Not interested in ourselves. |
449 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
455 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
450 | */ |
456 | */ |
451 | if (CPU == cpu) |
457 | if (CPU == cpu) |
452 | continue; |
458 | continue; |
453 | 459 | ||
454 | restart: pri = cpu_priority_high(); |
460 | restart: pri = cpu_priority_high(); |
455 | r = &cpu->rq[j]; |
461 | r = &cpu->rq[j]; |
456 | spinlock_lock(&r->lock); |
462 | spinlock_lock(&r->lock); |
457 | if (r->n == 0) { |
463 | if (r->n == 0) { |
458 | spinlock_unlock(&r->lock); |
464 | spinlock_unlock(&r->lock); |
459 | cpu_priority_restore(pri); |
465 | cpu_priority_restore(pri); |
460 | continue; |
466 | continue; |
461 | } |
467 | } |
462 | 468 | ||
463 | t = NULL; |
469 | t = NULL; |
464 | l = r->rq_head.prev; /* search rq from the back */ |
470 | l = r->rq_head.prev; /* search rq from the back */ |
465 | while (l != &r->rq_head) { |
471 | while (l != &r->rq_head) { |
466 | t = list_get_instance(l, thread_t, rq_link); |
472 | t = list_get_instance(l, thread_t, rq_link); |
467 | /* |
473 | /* |
468 | * We don't want to steal CPU-wired threads neither threads already stolen. |
474 | * We don't want to steal CPU-wired threads neither threads already stolen. |
469 | * The latter prevents threads from migrating between CPU's without ever being run. |
475 | * The latter prevents threads from migrating between CPU's without ever being run. |
470 | * We don't want to steal threads whose FPU context is still in CPU. |
476 | * We don't want to steal threads whose FPU context is still in CPU. |
471 | */ |
477 | */ |
472 | spinlock_lock(&t->lock); |
478 | spinlock_lock(&t->lock); |
473 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
479 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
474 | 480 | ||
475 | /* |
481 | /* |
476 | * Remove t from r. |
482 | * Remove t from r. |
477 | */ |
483 | */ |
478 | 484 | ||
479 | spinlock_unlock(&t->lock); |
485 | spinlock_unlock(&t->lock); |
480 | 486 | ||
481 | /* |
487 | /* |
482 | * Here we have to avoid deadlock with relink_rq(), |
488 | * Here we have to avoid deadlock with relink_rq(), |
483 | * because it locks cpu and r in a different order than we do. |
489 | * because it locks cpu and r in a different order than we do. |
484 | */ |
490 | */ |
485 | if (!spinlock_trylock(&cpu->lock)) { |
491 | if (!spinlock_trylock(&cpu->lock)) { |
486 | /* Release all locks and try again. */ |
492 | /* Release all locks and try again. */ |
487 | spinlock_unlock(&r->lock); |
493 | spinlock_unlock(&r->lock); |
488 | cpu_priority_restore(pri); |
494 | cpu_priority_restore(pri); |
489 | goto restart; |
495 | goto restart; |
490 | } |
496 | } |
491 | cpu->nrdy--; |
497 | cpu->nrdy--; |
492 | spinlock_unlock(&cpu->lock); |
498 | spinlock_unlock(&cpu->lock); |
493 | 499 | ||
494 | atomic_dec(&nrdy); |
500 | atomic_dec(&nrdy); |
495 | 501 | ||
496 | r->n--; |
502 | r->n--; |
497 | list_remove(&t->rq_link); |
503 | list_remove(&t->rq_link); |
498 | 504 | ||
499 | break; |
505 | break; |
500 | } |
506 | } |
501 | spinlock_unlock(&t->lock); |
507 | spinlock_unlock(&t->lock); |
502 | l = l->prev; |
508 | l = l->prev; |
503 | t = NULL; |
509 | t = NULL; |
504 | } |
510 | } |
505 | spinlock_unlock(&r->lock); |
511 | spinlock_unlock(&r->lock); |
506 | 512 | ||
507 | if (t) { |
513 | if (t) { |
508 | /* |
514 | /* |
509 | * Ready t on local CPU |
515 | * Ready t on local CPU |
510 | */ |
516 | */ |
511 | spinlock_lock(&t->lock); |
517 | spinlock_lock(&t->lock); |
512 | #ifdef KCPULB_VERBOSE |
518 | #ifdef KCPULB_VERBOSE |
513 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
519 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
514 | #endif |
520 | #endif |
515 | t->flags |= X_STOLEN; |
521 | t->flags |= X_STOLEN; |
516 | spinlock_unlock(&t->lock); |
522 | spinlock_unlock(&t->lock); |
517 | 523 | ||
518 | thread_ready(t); |
524 | thread_ready(t); |
519 | 525 | ||
520 | cpu_priority_restore(pri); |
526 | cpu_priority_restore(pri); |
521 | 527 | ||
522 | if (--count == 0) |
528 | if (--count == 0) |
523 | goto satisfied; |
529 | goto satisfied; |
524 | 530 | ||
525 | /* |
531 | /* |
526 | * We are not satisfied yet, focus on another CPU next time. |
532 | * We are not satisfied yet, focus on another CPU next time. |
527 | */ |
533 | */ |
528 | k++; |
534 | k++; |
529 | 535 | ||
530 | continue; |
536 | continue; |
531 | } |
537 | } |
532 | cpu_priority_restore(pri); |
538 | cpu_priority_restore(pri); |
533 | } |
539 | } |
534 | } |
540 | } |
535 | 541 | ||
536 | if (CPU->nrdy) { |
542 | if (CPU->nrdy) { |
537 | /* |
543 | /* |
538 | * Be a little bit light-weight and let migrated threads run. |
544 | * Be a little bit light-weight and let migrated threads run. |
539 | */ |
545 | */ |
540 | scheduler(); |
546 | scheduler(); |
541 | } |
547 | } |
542 | else { |
548 | else { |
543 | /* |
549 | /* |
544 | * We failed to migrate a single thread. |
550 | * We failed to migrate a single thread. |
545 | * Something more sophisticated should be done. |
551 | * Something more sophisticated should be done. |
546 | */ |
552 | */ |
547 | scheduler(); |
553 | scheduler(); |
548 | } |
554 | } |
549 | 555 | ||
550 | goto not_satisfied; |
556 | goto not_satisfied; |
551 | 557 | ||
552 | satisfied: |
558 | satisfied: |
553 | /* |
559 | /* |
554 | * Tell find_best_thread() to wake us up later again. |
560 | * Tell find_best_thread() to wake us up later again. |
555 | */ |
561 | */ |
556 | CPU->kcpulbstarted = 0; |
562 | CPU->kcpulbstarted = 0; |
557 | goto loop; |
563 | goto loop; |
558 | } |
564 | } |
559 | 565 | ||
560 | #endif /* __SMP__ */ |
566 | #endif /* __SMP__ */ |
561 | 567 |