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