Rev 184 | Rev 195 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 184 | Rev 192 | ||
---|---|---|---|
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 | 245 | ||
246 | /* |
246 | /* |
247 | * CPU priority of preempted thread is recorded here |
247 | * CPU priority of preempted thread is recorded here |
248 | * to facilitate scheduler() invocations from |
248 | * to facilitate scheduler() invocations from |
249 | * cpu_priority_high()'ed code (e.g. waitq_sleep_timeout()). |
249 | * cpu_priority_high()'ed code (e.g. waitq_sleep_timeout()). |
250 | */ |
250 | */ |
251 | THREAD->saved_context.pri = pri; |
251 | THREAD->saved_context.pri = pri; |
252 | } |
252 | } |
253 | 253 | ||
254 | /* |
254 | /* |
255 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU |
255 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU |
256 | * and preemption counter. At this point THE could be coming either |
256 | * and preemption counter. At this point THE could be coming either |
257 | * from THREAD's or CPU's stack. |
257 | * from THREAD's or CPU's stack. |
258 | */ |
258 | */ |
259 | the_copy(THE, (the_t *) CPU->stack); |
259 | the_copy(THE, (the_t *) CPU->stack); |
260 | 260 | ||
261 | /* |
261 | /* |
262 | * We may not keep the old stack. |
262 | * We may not keep the old stack. |
263 | * Reason: If we kept the old stack and got blocked, for instance, in |
263 | * Reason: If we kept the old stack and got blocked, for instance, in |
264 | * find_best_thread(), the old thread could get rescheduled by another |
264 | * find_best_thread(), the old thread could get rescheduled by another |
265 | * CPU and overwrite the part of its own stack that was also used by |
265 | * CPU and overwrite the part of its own stack that was also used by |
266 | * the scheduler on this CPU. |
266 | * the scheduler on this CPU. |
267 | * |
267 | * |
268 | * Moreover, we have to bypass the compiler-generated POP sequence |
268 | * Moreover, we have to bypass the compiler-generated POP sequence |
269 | * which is fooled by SP being set to the very top of the stack. |
269 | * which is fooled by SP being set to the very top of the stack. |
270 | * Therefore the scheduler() function continues in |
270 | * Therefore the scheduler() function continues in |
271 | * scheduler_separated_stack(). |
271 | * scheduler_separated_stack(). |
272 | */ |
272 | */ |
273 | context_save(&CPU->saved_context); |
273 | context_save(&CPU->saved_context); |
274 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE); |
274 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE); |
275 | context_restore(&CPU->saved_context); |
275 | context_restore(&CPU->saved_context); |
276 | /* not reached */ |
276 | /* not reached */ |
277 | } |
277 | } |
278 | 278 | ||
279 | 279 | ||
280 | /** Scheduler stack switch wrapper |
280 | /** Scheduler stack switch wrapper |
281 | * |
281 | * |
282 | * Second part of the scheduler() function |
282 | * Second part of the scheduler() function |
283 | * using new stack. Handling the actual context |
283 | * using new stack. Handling the actual context |
284 | * switch to a new thread. |
284 | * switch to a new thread. |
285 | * |
285 | * |
286 | */ |
286 | */ |
287 | void scheduler_separated_stack(void) |
287 | void scheduler_separated_stack(void) |
288 | { |
288 | { |
289 | int priority; |
289 | int priority; |
290 | 290 | ||
291 | if (THREAD) { |
291 | if (THREAD) { |
292 | switch (THREAD->state) { |
292 | switch (THREAD->state) { |
293 | case Running: |
293 | case Running: |
294 | THREAD->state = Ready; |
294 | THREAD->state = Ready; |
295 | spinlock_unlock(&THREAD->lock); |
295 | spinlock_unlock(&THREAD->lock); |
296 | thread_ready(THREAD); |
296 | thread_ready(THREAD); |
297 | break; |
297 | break; |
298 | 298 | ||
299 | case Exiting: |
299 | case Exiting: |
300 | frame_free((__address) THREAD->kstack); |
300 | frame_free((__address) THREAD->kstack); |
301 | if (THREAD->ustack) { |
301 | if (THREAD->ustack) { |
302 | frame_free((__address) THREAD->ustack); |
302 | frame_free((__address) THREAD->ustack); |
303 | } |
303 | } |
304 | 304 | ||
305 | /* |
305 | /* |
306 | * Detach from the containing task. |
306 | * Detach from the containing task. |
307 | */ |
307 | */ |
308 | spinlock_lock(&TASK->lock); |
308 | spinlock_lock(&TASK->lock); |
309 | list_remove(&THREAD->th_link); |
309 | list_remove(&THREAD->th_link); |
310 | spinlock_unlock(&TASK->lock); |
310 | spinlock_unlock(&TASK->lock); |
311 | 311 | ||
312 | spinlock_unlock(&THREAD->lock); |
312 | spinlock_unlock(&THREAD->lock); |
313 | 313 | ||
314 | spinlock_lock(&threads_lock); |
314 | spinlock_lock(&threads_lock); |
315 | list_remove(&THREAD->threads_link); |
315 | list_remove(&THREAD->threads_link); |
316 | spinlock_unlock(&threads_lock); |
316 | spinlock_unlock(&threads_lock); |
317 | 317 | ||
318 | spinlock_lock(&CPU->lock); |
318 | spinlock_lock(&CPU->lock); |
319 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
319 | if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL; |
320 | spinlock_unlock(&CPU->lock); |
320 | spinlock_unlock(&CPU->lock); |
321 | 321 | ||
322 | free(THREAD); |
322 | free(THREAD); |
323 | 323 | ||
324 | break; |
324 | break; |
325 | 325 | ||
326 | case Sleeping: |
326 | case Sleeping: |
327 | /* |
327 | /* |
328 | * Prefer the thread after it's woken up. |
328 | * Prefer the thread after it's woken up. |
329 | */ |
329 | */ |
330 | THREAD->pri = -1; |
330 | THREAD->pri = -1; |
331 | 331 | ||
332 | /* |
332 | /* |
333 | * We need to release wq->lock which we locked in waitq_sleep(). |
333 | * We need to release wq->lock which we locked in waitq_sleep(). |
334 | * Address of wq->lock is kept in THREAD->sleep_queue. |
334 | * Address of wq->lock is kept in THREAD->sleep_queue. |
335 | */ |
335 | */ |
336 | spinlock_unlock(&THREAD->sleep_queue->lock); |
336 | spinlock_unlock(&THREAD->sleep_queue->lock); |
337 | 337 | ||
338 | /* |
338 | /* |
339 | * Check for possible requests for out-of-context invocation. |
339 | * Check for possible requests for out-of-context invocation. |
340 | */ |
340 | */ |
341 | if (THREAD->call_me) { |
341 | if (THREAD->call_me) { |
342 | THREAD->call_me(THREAD->call_me_with); |
342 | THREAD->call_me(THREAD->call_me_with); |
343 | THREAD->call_me = NULL; |
343 | THREAD->call_me = NULL; |
344 | THREAD->call_me_with = NULL; |
344 | THREAD->call_me_with = NULL; |
345 | } |
345 | } |
346 | 346 | ||
347 | spinlock_unlock(&THREAD->lock); |
347 | spinlock_unlock(&THREAD->lock); |
348 | 348 | ||
349 | break; |
349 | break; |
350 | 350 | ||
351 | default: |
351 | default: |
352 | /* |
352 | /* |
353 | * Entering state is unexpected. |
353 | * Entering state is unexpected. |
354 | */ |
354 | */ |
355 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
355 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
356 | break; |
356 | break; |
357 | } |
357 | } |
358 | THREAD = NULL; |
358 | THREAD = NULL; |
359 | } |
359 | } |
360 | 360 | ||
361 | THREAD = find_best_thread(); |
361 | THREAD = find_best_thread(); |
362 | 362 | ||
363 | spinlock_lock(&THREAD->lock); |
363 | spinlock_lock(&THREAD->lock); |
364 | priority = THREAD->pri; |
364 | priority = THREAD->pri; |
365 | spinlock_unlock(&THREAD->lock); |
365 | spinlock_unlock(&THREAD->lock); |
366 | 366 | ||
367 | relink_rq(priority); |
367 | relink_rq(priority); |
368 | 368 | ||
369 | spinlock_lock(&THREAD->lock); |
369 | spinlock_lock(&THREAD->lock); |
370 | 370 | ||
371 | /* |
371 | /* |
372 | * If both the old and the new task are the same, lots of work is avoided. |
372 | * If both the old and the new task are the same, lots of work is avoided. |
373 | */ |
373 | */ |
374 | if (TASK != THREAD->task) { |
374 | if (TASK != THREAD->task) { |
375 | vm_t *m1 = NULL; |
375 | vm_t *m1 = NULL; |
376 | vm_t *m2; |
376 | vm_t *m2; |
377 | 377 | ||
378 | if (TASK) { |
378 | if (TASK) { |
379 | spinlock_lock(&TASK->lock); |
379 | spinlock_lock(&TASK->lock); |
380 | m1 = TASK->vm; |
380 | m1 = TASK->vm; |
381 | spinlock_unlock(&TASK->lock); |
381 | spinlock_unlock(&TASK->lock); |
382 | } |
382 | } |
383 | 383 | ||
384 | spinlock_lock(&THREAD->task->lock); |
384 | spinlock_lock(&THREAD->task->lock); |
385 | m2 = THREAD->task->vm; |
385 | m2 = THREAD->task->vm; |
386 | spinlock_unlock(&THREAD->task->lock); |
386 | spinlock_unlock(&THREAD->task->lock); |
387 | 387 | ||
388 | /* |
388 | /* |
389 | * Note that it is possible for two tasks to share one vm mapping. |
389 | * Note that it is possible for two tasks to share one vm mapping. |
390 | */ |
390 | */ |
391 | if (m1 != m2) { |
391 | if (m1 != m2) { |
392 | /* |
392 | /* |
393 | * Both tasks and vm mappings are different. |
393 | * Both tasks and vm mappings are different. |
394 | * Replace the old one with the new one. |
394 | * Replace the old one with the new one. |
395 | */ |
395 | */ |
396 | vm_install(m2); |
396 | vm_install(m2); |
397 | } |
397 | } |
398 | TASK = THREAD->task; |
398 | TASK = THREAD->task; |
399 | } |
399 | } |
400 | 400 | ||
401 | THREAD->state = Running; |
401 | THREAD->state = Running; |
402 | 402 | ||
403 | #ifdef SCHEDULER_VERBOSE |
403 | #ifdef SCHEDULER_VERBOSE |
404 | printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy); |
404 | printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy); |
405 | #endif |
405 | #endif |
406 | 406 | ||
407 | the_copy(THE, (the_t *) THREAD->kstack); |
407 | the_copy(THE, (the_t *) THREAD->kstack); |
408 | 408 | ||
409 | context_restore(&THREAD->saved_context); |
409 | context_restore(&THREAD->saved_context); |
410 | /* not reached */ |
410 | /* not reached */ |
411 | } |
411 | } |
412 | 412 | ||
413 | 413 | ||
414 | #ifdef __SMP__ |
414 | #ifdef __SMP__ |
415 | /** Load balancing thread |
415 | /** Load balancing thread |
416 | * |
416 | * |
417 | * SMP load balancing thread, supervising thread supplies |
417 | * SMP load balancing thread, supervising thread supplies |
418 | * for the CPU it's wired to. |
418 | * for the CPU it's wired to. |
419 | * |
419 | * |
420 | * @param arg Generic thread argument (unused). |
420 | * @param arg Generic thread argument (unused). |
421 | * |
421 | * |
422 | */ |
422 | */ |
423 | void kcpulb(void *arg) |
423 | void kcpulb(void *arg) |
424 | { |
424 | { |
425 | thread_t *t; |
425 | thread_t *t; |
426 | int count, i, j, k = 0; |
426 | int count, i, j, k = 0; |
427 | pri_t pri; |
427 | pri_t pri; |
428 | 428 | ||
429 | loop: |
429 | loop: |
430 | /* |
430 | /* |
431 | * Sleep until there's some work to do. |
431 | * Sleep until there's some work to do. |
432 | */ |
432 | */ |
433 | waitq_sleep(&CPU->kcpulb_wq); |
433 | waitq_sleep(&CPU->kcpulb_wq); |
434 | 434 | ||
435 | not_satisfied: |
435 | not_satisfied: |
436 | /* |
436 | /* |
437 | * Calculate the number of threads that will be migrated/stolen from |
437 | * Calculate the number of threads that will be migrated/stolen from |
438 | * other CPU's. Note that situation can have changed between two |
438 | * other CPU's. Note that situation can have changed between two |
439 | * passes. Each time get the most up to date counts. |
439 | * passes. Each time get the most up to date counts. |
440 | */ |
440 | */ |
441 | pri = cpu_priority_high(); |
441 | pri = cpu_priority_high(); |
442 | spinlock_lock(&CPU->lock); |
442 | spinlock_lock(&CPU->lock); |
443 | count = nrdy / config.cpu_active; |
443 | count = nrdy / config.cpu_active; |
444 | count -= CPU->nrdy; |
444 | count -= CPU->nrdy; |
445 | spinlock_unlock(&CPU->lock); |
445 | spinlock_unlock(&CPU->lock); |
446 | cpu_priority_restore(pri); |
446 | cpu_priority_restore(pri); |
447 | 447 | ||
448 | if (count <= 0) |
448 | if (count <= 0) |
449 | goto satisfied; |
449 | goto satisfied; |
450 | 450 | ||
451 | /* |
451 | /* |
452 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
452 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
453 | */ |
453 | */ |
454 | for (j=RQ_COUNT-1; j >= 0; j--) { |
454 | for (j=RQ_COUNT-1; j >= 0; j--) { |
455 | for (i=0; i < config.cpu_active; i++) { |
455 | for (i=0; i < config.cpu_active; i++) { |
456 | link_t *l; |
456 | link_t *l; |
457 | runq_t *r; |
457 | runq_t *r; |
458 | cpu_t *cpu; |
458 | cpu_t *cpu; |
459 | 459 | ||
460 | cpu = &cpus[(i + k) % config.cpu_active]; |
460 | cpu = &cpus[(i + k) % config.cpu_active]; |
461 | 461 | ||
462 | /* |
462 | /* |
463 | * Not interested in ourselves. |
463 | * Not interested in ourselves. |
464 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
464 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
465 | */ |
465 | */ |
466 | if (CPU == cpu) |
466 | if (CPU == cpu) |
467 | continue; |
467 | continue; |
468 | 468 | ||
469 | restart: pri = cpu_priority_high(); |
469 | restart: pri = cpu_priority_high(); |
470 | r = &cpu->rq[j]; |
470 | r = &cpu->rq[j]; |
471 | spinlock_lock(&r->lock); |
471 | spinlock_lock(&r->lock); |
472 | if (r->n == 0) { |
472 | if (r->n == 0) { |
473 | spinlock_unlock(&r->lock); |
473 | spinlock_unlock(&r->lock); |
474 | cpu_priority_restore(pri); |
474 | cpu_priority_restore(pri); |
475 | continue; |
475 | continue; |
476 | } |
476 | } |
477 | 477 | ||
478 | t = NULL; |
478 | t = NULL; |
479 | l = r->rq_head.prev; /* search rq from the back */ |
479 | l = r->rq_head.prev; /* search rq from the back */ |
480 | while (l != &r->rq_head) { |
480 | while (l != &r->rq_head) { |
481 | t = list_get_instance(l, thread_t, rq_link); |
481 | t = list_get_instance(l, thread_t, rq_link); |
482 | /* |
482 | /* |
483 | * We don't want to steal CPU-wired threads neither threads already stolen. |
483 | * We don't want to steal CPU-wired threads neither threads already stolen. |
484 | * The latter prevents threads from migrating between CPU's without ever being run. |
484 | * The latter prevents threads from migrating between CPU's without ever being run. |
485 | * We don't want to steal threads whose FPU context is still in CPU. |
485 | * We don't want to steal threads whose FPU context is still in CPU. |
486 | */ |
486 | */ |
487 | spinlock_lock(&t->lock); |
487 | spinlock_lock(&t->lock); |
488 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
488 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
489 | 489 | ||
490 | /* |
490 | /* |
491 | * Remove t from r. |
491 | * Remove t from r. |
492 | */ |
492 | */ |
493 | 493 | ||
494 | spinlock_unlock(&t->lock); |
494 | spinlock_unlock(&t->lock); |
495 | 495 | ||
496 | /* |
496 | /* |
497 | * Here we have to avoid deadlock with relink_rq(), |
497 | * Here we have to avoid deadlock with relink_rq(), |
498 | * because it locks cpu and r in a different order than we do. |
498 | * because it locks cpu and r in a different order than we do. |
499 | */ |
499 | */ |
500 | if (!spinlock_trylock(&cpu->lock)) { |
500 | if (!spinlock_trylock(&cpu->lock)) { |
501 | /* Release all locks and try again. */ |
501 | /* Release all locks and try again. */ |
502 | spinlock_unlock(&r->lock); |
502 | spinlock_unlock(&r->lock); |
503 | cpu_priority_restore(pri); |
503 | cpu_priority_restore(pri); |
504 | goto restart; |
504 | goto restart; |
505 | } |
505 | } |
506 | cpu->nrdy--; |
506 | cpu->nrdy--; |
507 | spinlock_unlock(&cpu->lock); |
507 | spinlock_unlock(&cpu->lock); |
508 | 508 | ||
509 | atomic_dec(&nrdy); |
509 | atomic_dec(&nrdy); |
510 | 510 | ||
511 | r->n--; |
511 | r->n--; |
512 | list_remove(&t->rq_link); |
512 | list_remove(&t->rq_link); |
513 | 513 | ||
514 | break; |
514 | break; |
515 | } |
515 | } |
516 | spinlock_unlock(&t->lock); |
516 | spinlock_unlock(&t->lock); |
517 | l = l->prev; |
517 | l = l->prev; |
518 | t = NULL; |
518 | t = NULL; |
519 | } |
519 | } |
520 | spinlock_unlock(&r->lock); |
520 | spinlock_unlock(&r->lock); |
521 | 521 | ||
522 | if (t) { |
522 | if (t) { |
523 | /* |
523 | /* |
524 | * Ready t on local CPU |
524 | * Ready t on local CPU |
525 | */ |
525 | */ |
526 | spinlock_lock(&t->lock); |
526 | spinlock_lock(&t->lock); |
527 | #ifdef KCPULB_VERBOSE |
527 | #ifdef KCPULB_VERBOSE |
528 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
528 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active); |
529 | #endif |
529 | #endif |
530 | t->flags |= X_STOLEN; |
530 | t->flags |= X_STOLEN; |
531 | spinlock_unlock(&t->lock); |
531 | spinlock_unlock(&t->lock); |
532 | 532 | ||
533 | thread_ready(t); |
533 | thread_ready(t); |
534 | 534 | ||
535 | cpu_priority_restore(pri); |
535 | cpu_priority_restore(pri); |
536 | 536 | ||
537 | if (--count == 0) |
537 | if (--count == 0) |
538 | goto satisfied; |
538 | goto satisfied; |
539 | 539 | ||
540 | /* |
540 | /* |
541 | * We are not satisfied yet, focus on another CPU next time. |
541 | * We are not satisfied yet, focus on another CPU next time. |
542 | */ |
542 | */ |
543 | k++; |
543 | k++; |
544 | 544 | ||
545 | continue; |
545 | continue; |
546 | } |
546 | } |
547 | cpu_priority_restore(pri); |
547 | cpu_priority_restore(pri); |
548 | } |
548 | } |
549 | } |
549 | } |
550 | 550 | ||
551 | if (CPU->nrdy) { |
551 | if (CPU->nrdy) { |
552 | /* |
552 | /* |
553 | * Be a little bit light-weight and let migrated threads run. |
553 | * Be a little bit light-weight and let migrated threads run. |
554 | */ |
554 | */ |
555 | scheduler(); |
555 | scheduler(); |
556 | } |
556 | } |
557 | else { |
557 | else { |
558 | /* |
558 | /* |
559 | * We failed to migrate a single thread. |
559 | * We failed to migrate a single thread. |
560 | * Something more sophisticated should be done. |
560 | * Something more sophisticated should be done. |
561 | */ |
561 | */ |
562 | scheduler(); |
562 | scheduler(); |
563 | } |
563 | } |
564 | 564 | ||
565 | goto not_satisfied; |
565 | goto not_satisfied; |
566 | 566 | ||
567 | satisfied: |
567 | satisfied: |
568 | /* |
568 | /* |
569 | * Tell find_best_thread() to wake us up later again. |
569 | * Tell find_best_thread() to wake us up later again. |
570 | */ |
570 | */ |
571 | CPU->kcpulbstarted = 0; |
571 | CPU->kcpulbstarted = 0; |
572 | goto loop; |
572 | goto loop; |
573 | } |
573 | } |
574 | 574 | ||
575 | #endif /* __SMP__ */ |
575 | #endif /* __SMP__ */ |
576 | 576 |