Rev 788 | Rev 823 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 788 | Rev 814 | ||
---|---|---|---|
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 <mm/heap.h> |
- | |
33 | #include <mm/frame.h> |
32 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
33 | #include <mm/page.h> |
35 | #include <mm/as.h> |
34 | #include <mm/as.h> |
36 | #include <arch/asm.h> |
35 | #include <arch/asm.h> |
37 | #include <arch/faddr.h> |
36 | #include <arch/faddr.h> |
38 | #include <arch/atomic.h> |
37 | #include <arch/atomic.h> |
39 | #include <synch/spinlock.h> |
38 | #include <synch/spinlock.h> |
40 | #include <config.h> |
39 | #include <config.h> |
41 | #include <context.h> |
40 | #include <context.h> |
42 | #include <func.h> |
41 | #include <func.h> |
43 | #include <arch.h> |
42 | #include <arch.h> |
44 | #include <adt/list.h> |
43 | #include <adt/list.h> |
45 | #include <panic.h> |
44 | #include <panic.h> |
46 | #include <typedefs.h> |
45 | #include <typedefs.h> |
47 | #include <cpu.h> |
46 | #include <cpu.h> |
48 | #include <print.h> |
47 | #include <print.h> |
49 | #include <debug.h> |
48 | #include <debug.h> |
50 | 49 | ||
51 | atomic_t nrdy; |
50 | atomic_t nrdy; |
52 | 51 | ||
53 | /** Take actions before new thread runs |
52 | /** Take actions before new thread runs |
54 | * |
53 | * |
55 | * Perform actions that need to be |
54 | * Perform actions that need to be |
56 | * taken before the newly selected |
55 | * taken before the newly selected |
57 | * tread is passed control. |
56 | * tread is passed control. |
58 | * |
57 | * |
59 | */ |
58 | */ |
60 | void before_thread_runs(void) |
59 | void before_thread_runs(void) |
61 | { |
60 | { |
62 | before_thread_runs_arch(); |
61 | before_thread_runs_arch(); |
63 | #ifdef CONFIG_FPU_LAZY |
62 | #ifdef CONFIG_FPU_LAZY |
64 | if(THREAD==CPU->fpu_owner) |
63 | if(THREAD==CPU->fpu_owner) |
65 | fpu_enable(); |
64 | fpu_enable(); |
66 | else |
65 | else |
67 | fpu_disable(); |
66 | fpu_disable(); |
68 | #else |
67 | #else |
69 | fpu_enable(); |
68 | fpu_enable(); |
70 | if (THREAD->fpu_context_exists) |
69 | if (THREAD->fpu_context_exists) |
71 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
70 | fpu_context_restore(&(THREAD->saved_fpu_context)); |
72 | else { |
71 | else { |
73 | fpu_init(); |
72 | fpu_init(); |
74 | THREAD->fpu_context_exists=1; |
73 | THREAD->fpu_context_exists=1; |
75 | } |
74 | } |
76 | #endif |
75 | #endif |
77 | } |
76 | } |
78 | 77 | ||
79 | #ifdef CONFIG_FPU_LAZY |
78 | #ifdef CONFIG_FPU_LAZY |
80 | void scheduler_fpu_lazy_request(void) |
79 | void scheduler_fpu_lazy_request(void) |
81 | { |
80 | { |
82 | fpu_enable(); |
81 | fpu_enable(); |
83 | if (CPU->fpu_owner != NULL) { |
82 | if (CPU->fpu_owner != NULL) { |
84 | fpu_context_save(&CPU->fpu_owner->saved_fpu_context); |
83 | fpu_context_save(&CPU->fpu_owner->saved_fpu_context); |
85 | /* don't prevent migration */ |
84 | /* don't prevent migration */ |
86 | CPU->fpu_owner->fpu_context_engaged=0; |
85 | CPU->fpu_owner->fpu_context_engaged=0; |
87 | } |
86 | } |
88 | if (THREAD->fpu_context_exists) |
87 | if (THREAD->fpu_context_exists) |
89 | fpu_context_restore(&THREAD->saved_fpu_context); |
88 | fpu_context_restore(&THREAD->saved_fpu_context); |
90 | else { |
89 | else { |
91 | fpu_init(); |
90 | fpu_init(); |
92 | THREAD->fpu_context_exists=1; |
91 | THREAD->fpu_context_exists=1; |
93 | } |
92 | } |
94 | CPU->fpu_owner=THREAD; |
93 | CPU->fpu_owner=THREAD; |
95 | THREAD->fpu_context_engaged = 1; |
94 | THREAD->fpu_context_engaged = 1; |
96 | } |
95 | } |
97 | #endif |
96 | #endif |
98 | 97 | ||
99 | /** Initialize scheduler |
98 | /** Initialize scheduler |
100 | * |
99 | * |
101 | * Initialize kernel scheduler. |
100 | * Initialize kernel scheduler. |
102 | * |
101 | * |
103 | */ |
102 | */ |
104 | void scheduler_init(void) |
103 | void scheduler_init(void) |
105 | { |
104 | { |
106 | } |
105 | } |
107 | 106 | ||
108 | 107 | ||
109 | /** Get thread to be scheduled |
108 | /** Get thread to be scheduled |
110 | * |
109 | * |
111 | * Get the optimal thread to be scheduled |
110 | * Get the optimal thread to be scheduled |
112 | * according to thread accounting and scheduler |
111 | * according to thread accounting and scheduler |
113 | * policy. |
112 | * policy. |
114 | * |
113 | * |
115 | * @return Thread to be scheduled. |
114 | * @return Thread to be scheduled. |
116 | * |
115 | * |
117 | */ |
116 | */ |
118 | static thread_t *find_best_thread(void) |
117 | static thread_t *find_best_thread(void) |
119 | { |
118 | { |
120 | thread_t *t; |
119 | thread_t *t; |
121 | runq_t *r; |
120 | runq_t *r; |
122 | int i; |
121 | int i; |
123 | 122 | ||
124 | ASSERT(CPU != NULL); |
123 | ASSERT(CPU != NULL); |
125 | 124 | ||
126 | loop: |
125 | loop: |
127 | interrupts_enable(); |
126 | interrupts_enable(); |
128 | 127 | ||
129 | if (atomic_get(&CPU->nrdy) == 0) { |
128 | if (atomic_get(&CPU->nrdy) == 0) { |
130 | /* |
129 | /* |
131 | * For there was nothing to run, the CPU goes to sleep |
130 | * For there was nothing to run, the CPU goes to sleep |
132 | * until a hardware interrupt or an IPI comes. |
131 | * until a hardware interrupt or an IPI comes. |
133 | * This improves energy saving and hyperthreading. |
132 | * This improves energy saving and hyperthreading. |
134 | */ |
133 | */ |
135 | 134 | ||
136 | /* |
135 | /* |
137 | * An interrupt might occur right now and wake up a thread. |
136 | * An interrupt might occur right now and wake up a thread. |
138 | * In such case, the CPU will continue to go to sleep |
137 | * In such case, the CPU will continue to go to sleep |
139 | * even though there is a runnable thread. |
138 | * even though there is a runnable thread. |
140 | */ |
139 | */ |
141 | 140 | ||
142 | cpu_sleep(); |
141 | cpu_sleep(); |
143 | goto loop; |
142 | goto loop; |
144 | } |
143 | } |
145 | 144 | ||
146 | interrupts_disable(); |
145 | interrupts_disable(); |
147 | 146 | ||
148 | i = 0; |
147 | i = 0; |
149 | for (; i<RQ_COUNT; i++) { |
148 | for (; i<RQ_COUNT; i++) { |
150 | r = &CPU->rq[i]; |
149 | r = &CPU->rq[i]; |
151 | spinlock_lock(&r->lock); |
150 | spinlock_lock(&r->lock); |
152 | if (r->n == 0) { |
151 | if (r->n == 0) { |
153 | /* |
152 | /* |
154 | * If this queue is empty, try a lower-priority queue. |
153 | * If this queue is empty, try a lower-priority queue. |
155 | */ |
154 | */ |
156 | spinlock_unlock(&r->lock); |
155 | spinlock_unlock(&r->lock); |
157 | continue; |
156 | continue; |
158 | } |
157 | } |
159 | 158 | ||
160 | atomic_dec(&CPU->nrdy); |
159 | atomic_dec(&CPU->nrdy); |
161 | atomic_dec(&nrdy); |
160 | atomic_dec(&nrdy); |
162 | r->n--; |
161 | r->n--; |
163 | 162 | ||
164 | /* |
163 | /* |
165 | * Take the first thread from the queue. |
164 | * Take the first thread from the queue. |
166 | */ |
165 | */ |
167 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
166 | t = list_get_instance(r->rq_head.next, thread_t, rq_link); |
168 | list_remove(&t->rq_link); |
167 | list_remove(&t->rq_link); |
169 | 168 | ||
170 | spinlock_unlock(&r->lock); |
169 | spinlock_unlock(&r->lock); |
171 | 170 | ||
172 | spinlock_lock(&t->lock); |
171 | spinlock_lock(&t->lock); |
173 | t->cpu = CPU; |
172 | t->cpu = CPU; |
174 | 173 | ||
175 | t->ticks = us2ticks((i+1)*10000); |
174 | t->ticks = us2ticks((i+1)*10000); |
176 | t->priority = i; /* eventually correct rq index */ |
175 | t->priority = i; /* eventually correct rq index */ |
177 | 176 | ||
178 | /* |
177 | /* |
179 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
178 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge. |
180 | */ |
179 | */ |
181 | t->flags &= ~X_STOLEN; |
180 | t->flags &= ~X_STOLEN; |
182 | spinlock_unlock(&t->lock); |
181 | spinlock_unlock(&t->lock); |
183 | 182 | ||
184 | return t; |
183 | return t; |
185 | } |
184 | } |
186 | goto loop; |
185 | goto loop; |
187 | 186 | ||
188 | } |
187 | } |
189 | 188 | ||
190 | 189 | ||
191 | /** Prevent rq starvation |
190 | /** Prevent rq starvation |
192 | * |
191 | * |
193 | * Prevent low priority threads from starving in rq's. |
192 | * Prevent low priority threads from starving in rq's. |
194 | * |
193 | * |
195 | * When the function decides to relink rq's, it reconnects |
194 | * When the function decides to relink rq's, it reconnects |
196 | * respective pointers so that in result threads with 'pri' |
195 | * respective pointers so that in result threads with 'pri' |
197 | * greater or equal 'start' are moved to a higher-priority queue. |
196 | * greater or equal 'start' are moved to a higher-priority queue. |
198 | * |
197 | * |
199 | * @param start Threshold priority. |
198 | * @param start Threshold priority. |
200 | * |
199 | * |
201 | */ |
200 | */ |
202 | static void relink_rq(int start) |
201 | static void relink_rq(int start) |
203 | { |
202 | { |
204 | link_t head; |
203 | link_t head; |
205 | runq_t *r; |
204 | runq_t *r; |
206 | int i, n; |
205 | int i, n; |
207 | 206 | ||
208 | list_initialize(&head); |
207 | list_initialize(&head); |
209 | spinlock_lock(&CPU->lock); |
208 | spinlock_lock(&CPU->lock); |
210 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
209 | if (CPU->needs_relink > NEEDS_RELINK_MAX) { |
211 | for (i = start; i<RQ_COUNT-1; i++) { |
210 | for (i = start; i<RQ_COUNT-1; i++) { |
212 | /* remember and empty rq[i + 1] */ |
211 | /* remember and empty rq[i + 1] */ |
213 | r = &CPU->rq[i + 1]; |
212 | r = &CPU->rq[i + 1]; |
214 | spinlock_lock(&r->lock); |
213 | spinlock_lock(&r->lock); |
215 | list_concat(&head, &r->rq_head); |
214 | list_concat(&head, &r->rq_head); |
216 | n = r->n; |
215 | n = r->n; |
217 | r->n = 0; |
216 | r->n = 0; |
218 | spinlock_unlock(&r->lock); |
217 | spinlock_unlock(&r->lock); |
219 | 218 | ||
220 | /* append rq[i + 1] to rq[i] */ |
219 | /* append rq[i + 1] to rq[i] */ |
221 | r = &CPU->rq[i]; |
220 | r = &CPU->rq[i]; |
222 | spinlock_lock(&r->lock); |
221 | spinlock_lock(&r->lock); |
223 | list_concat(&r->rq_head, &head); |
222 | list_concat(&r->rq_head, &head); |
224 | r->n += n; |
223 | r->n += n; |
225 | spinlock_unlock(&r->lock); |
224 | spinlock_unlock(&r->lock); |
226 | } |
225 | } |
227 | CPU->needs_relink = 0; |
226 | CPU->needs_relink = 0; |
228 | } |
227 | } |
229 | spinlock_unlock(&CPU->lock); |
228 | spinlock_unlock(&CPU->lock); |
230 | 229 | ||
231 | } |
230 | } |
232 | 231 | ||
233 | 232 | ||
234 | /** Scheduler stack switch wrapper |
233 | /** Scheduler stack switch wrapper |
235 | * |
234 | * |
236 | * Second part of the scheduler() function |
235 | * Second part of the scheduler() function |
237 | * using new stack. Handling the actual context |
236 | * using new stack. Handling the actual context |
238 | * switch to a new thread. |
237 | * switch to a new thread. |
239 | * |
238 | * |
240 | * Assume THREAD->lock is held. |
239 | * Assume THREAD->lock is held. |
241 | */ |
240 | */ |
242 | static void scheduler_separated_stack(void) |
241 | static void scheduler_separated_stack(void) |
243 | { |
242 | { |
244 | int priority; |
243 | int priority; |
245 | 244 | ||
246 | ASSERT(CPU != NULL); |
245 | ASSERT(CPU != NULL); |
247 | 246 | ||
248 | if (THREAD) { |
247 | if (THREAD) { |
249 | switch (THREAD->state) { |
248 | switch (THREAD->state) { |
250 | case Running: |
249 | case Running: |
251 | THREAD->state = Ready; |
250 | THREAD->state = Ready; |
252 | spinlock_unlock(&THREAD->lock); |
251 | spinlock_unlock(&THREAD->lock); |
253 | thread_ready(THREAD); |
252 | thread_ready(THREAD); |
254 | break; |
253 | break; |
255 | 254 | ||
256 | case Exiting: |
255 | case Exiting: |
257 | thread_destroy(THREAD); |
256 | thread_destroy(THREAD); |
258 | break; |
257 | break; |
259 | 258 | ||
260 | case Sleeping: |
259 | case Sleeping: |
261 | /* |
260 | /* |
262 | * Prefer the thread after it's woken up. |
261 | * Prefer the thread after it's woken up. |
263 | */ |
262 | */ |
264 | THREAD->priority = -1; |
263 | THREAD->priority = -1; |
265 | 264 | ||
266 | /* |
265 | /* |
267 | * We need to release wq->lock which we locked in waitq_sleep(). |
266 | * We need to release wq->lock which we locked in waitq_sleep(). |
268 | * Address of wq->lock is kept in THREAD->sleep_queue. |
267 | * Address of wq->lock is kept in THREAD->sleep_queue. |
269 | */ |
268 | */ |
270 | spinlock_unlock(&THREAD->sleep_queue->lock); |
269 | spinlock_unlock(&THREAD->sleep_queue->lock); |
271 | 270 | ||
272 | /* |
271 | /* |
273 | * Check for possible requests for out-of-context invocation. |
272 | * Check for possible requests for out-of-context invocation. |
274 | */ |
273 | */ |
275 | if (THREAD->call_me) { |
274 | if (THREAD->call_me) { |
276 | THREAD->call_me(THREAD->call_me_with); |
275 | THREAD->call_me(THREAD->call_me_with); |
277 | THREAD->call_me = NULL; |
276 | THREAD->call_me = NULL; |
278 | THREAD->call_me_with = NULL; |
277 | THREAD->call_me_with = NULL; |
279 | } |
278 | } |
280 | 279 | ||
281 | spinlock_unlock(&THREAD->lock); |
280 | spinlock_unlock(&THREAD->lock); |
282 | 281 | ||
283 | break; |
282 | break; |
284 | 283 | ||
285 | default: |
284 | default: |
286 | /* |
285 | /* |
287 | * Entering state is unexpected. |
286 | * Entering state is unexpected. |
288 | */ |
287 | */ |
289 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
288 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]); |
290 | break; |
289 | break; |
291 | } |
290 | } |
292 | THREAD = NULL; |
291 | THREAD = NULL; |
293 | } |
292 | } |
294 | 293 | ||
295 | 294 | ||
296 | THREAD = find_best_thread(); |
295 | THREAD = find_best_thread(); |
297 | 296 | ||
298 | spinlock_lock(&THREAD->lock); |
297 | spinlock_lock(&THREAD->lock); |
299 | priority = THREAD->priority; |
298 | priority = THREAD->priority; |
300 | spinlock_unlock(&THREAD->lock); |
299 | spinlock_unlock(&THREAD->lock); |
301 | 300 | ||
302 | relink_rq(priority); |
301 | relink_rq(priority); |
303 | 302 | ||
304 | spinlock_lock(&THREAD->lock); |
303 | spinlock_lock(&THREAD->lock); |
305 | 304 | ||
306 | /* |
305 | /* |
307 | * If both the old and the new task are the same, lots of work is avoided. |
306 | * If both the old and the new task are the same, lots of work is avoided. |
308 | */ |
307 | */ |
309 | if (TASK != THREAD->task) { |
308 | if (TASK != THREAD->task) { |
310 | as_t *as1 = NULL; |
309 | as_t *as1 = NULL; |
311 | as_t *as2; |
310 | as_t *as2; |
312 | 311 | ||
313 | if (TASK) { |
312 | if (TASK) { |
314 | spinlock_lock(&TASK->lock); |
313 | spinlock_lock(&TASK->lock); |
315 | as1 = TASK->as; |
314 | as1 = TASK->as; |
316 | spinlock_unlock(&TASK->lock); |
315 | spinlock_unlock(&TASK->lock); |
317 | } |
316 | } |
318 | 317 | ||
319 | spinlock_lock(&THREAD->task->lock); |
318 | spinlock_lock(&THREAD->task->lock); |
320 | as2 = THREAD->task->as; |
319 | as2 = THREAD->task->as; |
321 | spinlock_unlock(&THREAD->task->lock); |
320 | spinlock_unlock(&THREAD->task->lock); |
322 | 321 | ||
323 | /* |
322 | /* |
324 | * Note that it is possible for two tasks to share one address space. |
323 | * Note that it is possible for two tasks to share one address space. |
325 | */ |
324 | */ |
326 | if (as1 != as2) { |
325 | if (as1 != as2) { |
327 | /* |
326 | /* |
328 | * Both tasks and address spaces are different. |
327 | * Both tasks and address spaces are different. |
329 | * Replace the old one with the new one. |
328 | * Replace the old one with the new one. |
330 | */ |
329 | */ |
331 | as_install(as2); |
330 | as_install(as2); |
332 | } |
331 | } |
333 | TASK = THREAD->task; |
332 | TASK = THREAD->task; |
334 | } |
333 | } |
335 | 334 | ||
336 | THREAD->state = Running; |
335 | THREAD->state = Running; |
337 | 336 | ||
338 | #ifdef SCHEDULER_VERBOSE |
337 | #ifdef SCHEDULER_VERBOSE |
339 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, CPU->nrdy); |
338 | printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, CPU->nrdy); |
340 | #endif |
339 | #endif |
341 | 340 | ||
342 | /* |
341 | /* |
343 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
342 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack. |
344 | */ |
343 | */ |
345 | the_copy(THE, (the_t *) THREAD->kstack); |
344 | the_copy(THE, (the_t *) THREAD->kstack); |
346 | 345 | ||
347 | context_restore(&THREAD->saved_context); |
346 | context_restore(&THREAD->saved_context); |
348 | /* not reached */ |
347 | /* not reached */ |
349 | } |
348 | } |
350 | 349 | ||
351 | 350 | ||
352 | /** The scheduler |
351 | /** The scheduler |
353 | * |
352 | * |
354 | * The thread scheduling procedure. |
353 | * The thread scheduling procedure. |
355 | * Passes control directly to |
354 | * Passes control directly to |
356 | * scheduler_separated_stack(). |
355 | * scheduler_separated_stack(). |
357 | * |
356 | * |
358 | */ |
357 | */ |
359 | void scheduler(void) |
358 | void scheduler(void) |
360 | { |
359 | { |
361 | volatile ipl_t ipl; |
360 | volatile ipl_t ipl; |
362 | 361 | ||
363 | ASSERT(CPU != NULL); |
362 | ASSERT(CPU != NULL); |
364 | 363 | ||
365 | ipl = interrupts_disable(); |
364 | ipl = interrupts_disable(); |
366 | 365 | ||
367 | if (atomic_get(&haltstate)) |
366 | if (atomic_get(&haltstate)) |
368 | halt(); |
367 | halt(); |
369 | 368 | ||
370 | if (THREAD) { |
369 | if (THREAD) { |
371 | spinlock_lock(&THREAD->lock); |
370 | spinlock_lock(&THREAD->lock); |
372 | #ifndef CONFIG_FPU_LAZY |
371 | #ifndef CONFIG_FPU_LAZY |
373 | fpu_context_save(&(THREAD->saved_fpu_context)); |
372 | fpu_context_save(&(THREAD->saved_fpu_context)); |
374 | #endif |
373 | #endif |
375 | if (!context_save(&THREAD->saved_context)) { |
374 | if (!context_save(&THREAD->saved_context)) { |
376 | /* |
375 | /* |
377 | * This is the place where threads leave scheduler(); |
376 | * This is the place where threads leave scheduler(); |
378 | */ |
377 | */ |
379 | before_thread_runs(); |
378 | before_thread_runs(); |
380 | spinlock_unlock(&THREAD->lock); |
379 | spinlock_unlock(&THREAD->lock); |
381 | interrupts_restore(THREAD->saved_context.ipl); |
380 | interrupts_restore(THREAD->saved_context.ipl); |
382 | return; |
381 | return; |
383 | } |
382 | } |
384 | 383 | ||
385 | /* |
384 | /* |
386 | * Interrupt priority level of preempted thread is recorded here |
385 | * Interrupt priority level of preempted thread is recorded here |
387 | * to facilitate scheduler() invocations from interrupts_disable()'d |
386 | * to facilitate scheduler() invocations from interrupts_disable()'d |
388 | * code (e.g. waitq_sleep_timeout()). |
387 | * code (e.g. waitq_sleep_timeout()). |
389 | */ |
388 | */ |
390 | THREAD->saved_context.ipl = ipl; |
389 | THREAD->saved_context.ipl = ipl; |
391 | } |
390 | } |
392 | 391 | ||
393 | /* |
392 | /* |
394 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM |
393 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM |
395 | * and preemption counter. At this point THE could be coming either |
394 | * and preemption counter. At this point THE could be coming either |
396 | * from THREAD's or CPU's stack. |
395 | * from THREAD's or CPU's stack. |
397 | */ |
396 | */ |
398 | the_copy(THE, (the_t *) CPU->stack); |
397 | the_copy(THE, (the_t *) CPU->stack); |
399 | 398 | ||
400 | /* |
399 | /* |
401 | * We may not keep the old stack. |
400 | * We may not keep the old stack. |
402 | * Reason: If we kept the old stack and got blocked, for instance, in |
401 | * Reason: If we kept the old stack and got blocked, for instance, in |
403 | * find_best_thread(), the old thread could get rescheduled by another |
402 | * find_best_thread(), the old thread could get rescheduled by another |
404 | * CPU and overwrite the part of its own stack that was also used by |
403 | * CPU and overwrite the part of its own stack that was also used by |
405 | * the scheduler on this CPU. |
404 | * the scheduler on this CPU. |
406 | * |
405 | * |
407 | * Moreover, we have to bypass the compiler-generated POP sequence |
406 | * Moreover, we have to bypass the compiler-generated POP sequence |
408 | * which is fooled by SP being set to the very top of the stack. |
407 | * which is fooled by SP being set to the very top of the stack. |
409 | * Therefore the scheduler() function continues in |
408 | * Therefore the scheduler() function continues in |
410 | * scheduler_separated_stack(). |
409 | * scheduler_separated_stack(). |
411 | */ |
410 | */ |
412 | context_save(&CPU->saved_context); |
411 | context_save(&CPU->saved_context); |
413 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
412 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
414 | context_restore(&CPU->saved_context); |
413 | context_restore(&CPU->saved_context); |
415 | /* not reached */ |
414 | /* not reached */ |
416 | } |
415 | } |
417 | 416 | ||
418 | 417 | ||
419 | 418 | ||
420 | 419 | ||
421 | 420 | ||
422 | #ifdef CONFIG_SMP |
421 | #ifdef CONFIG_SMP |
423 | /** Load balancing thread |
422 | /** Load balancing thread |
424 | * |
423 | * |
425 | * SMP load balancing thread, supervising thread supplies |
424 | * SMP load balancing thread, supervising thread supplies |
426 | * for the CPU it's wired to. |
425 | * for the CPU it's wired to. |
427 | * |
426 | * |
428 | * @param arg Generic thread argument (unused). |
427 | * @param arg Generic thread argument (unused). |
429 | * |
428 | * |
430 | */ |
429 | */ |
431 | void kcpulb(void *arg) |
430 | void kcpulb(void *arg) |
432 | { |
431 | { |
433 | thread_t *t; |
432 | thread_t *t; |
434 | int count, average, i, j, k = 0; |
433 | int count, average, i, j, k = 0; |
435 | ipl_t ipl; |
434 | ipl_t ipl; |
436 | 435 | ||
437 | loop: |
436 | loop: |
438 | /* |
437 | /* |
439 | * Work in 1s intervals. |
438 | * Work in 1s intervals. |
440 | */ |
439 | */ |
441 | thread_sleep(1); |
440 | thread_sleep(1); |
442 | 441 | ||
443 | not_satisfied: |
442 | not_satisfied: |
444 | /* |
443 | /* |
445 | * Calculate the number of threads that will be migrated/stolen from |
444 | * Calculate the number of threads that will be migrated/stolen from |
446 | * other CPU's. Note that situation can have changed between two |
445 | * other CPU's. Note that situation can have changed between two |
447 | * passes. Each time get the most up to date counts. |
446 | * passes. Each time get the most up to date counts. |
448 | */ |
447 | */ |
449 | average = atomic_get(&nrdy) / config.cpu_active + 1; |
448 | average = atomic_get(&nrdy) / config.cpu_active + 1; |
450 | count = average - atomic_get(&CPU->nrdy); |
449 | count = average - atomic_get(&CPU->nrdy); |
451 | 450 | ||
452 | if (count <= 0) |
451 | if (count <= 0) |
453 | goto satisfied; |
452 | goto satisfied; |
454 | 453 | ||
455 | /* |
454 | /* |
456 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
455 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last. |
457 | */ |
456 | */ |
458 | for (j=RQ_COUNT-1; j >= 0; j--) { |
457 | for (j=RQ_COUNT-1; j >= 0; j--) { |
459 | for (i=0; i < config.cpu_active; i++) { |
458 | for (i=0; i < config.cpu_active; i++) { |
460 | link_t *l; |
459 | link_t *l; |
461 | runq_t *r; |
460 | runq_t *r; |
462 | cpu_t *cpu; |
461 | cpu_t *cpu; |
463 | 462 | ||
464 | cpu = &cpus[(i + k) % config.cpu_active]; |
463 | cpu = &cpus[(i + k) % config.cpu_active]; |
465 | 464 | ||
466 | /* |
465 | /* |
467 | * Not interested in ourselves. |
466 | * Not interested in ourselves. |
468 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
467 | * Doesn't require interrupt disabling for kcpulb is X_WIRED. |
469 | */ |
468 | */ |
470 | if (CPU == cpu) |
469 | if (CPU == cpu) |
471 | continue; |
470 | continue; |
472 | if (atomic_get(&cpu->nrdy) <= average) |
471 | if (atomic_get(&cpu->nrdy) <= average) |
473 | continue; |
472 | continue; |
474 | 473 | ||
475 | ipl = interrupts_disable(); |
474 | ipl = interrupts_disable(); |
476 | r = &cpu->rq[j]; |
475 | r = &cpu->rq[j]; |
477 | spinlock_lock(&r->lock); |
476 | spinlock_lock(&r->lock); |
478 | if (r->n == 0) { |
477 | if (r->n == 0) { |
479 | spinlock_unlock(&r->lock); |
478 | spinlock_unlock(&r->lock); |
480 | interrupts_restore(ipl); |
479 | interrupts_restore(ipl); |
481 | continue; |
480 | continue; |
482 | } |
481 | } |
483 | 482 | ||
484 | t = NULL; |
483 | t = NULL; |
485 | l = r->rq_head.prev; /* search rq from the back */ |
484 | l = r->rq_head.prev; /* search rq from the back */ |
486 | while (l != &r->rq_head) { |
485 | while (l != &r->rq_head) { |
487 | t = list_get_instance(l, thread_t, rq_link); |
486 | t = list_get_instance(l, thread_t, rq_link); |
488 | /* |
487 | /* |
489 | * We don't want to steal CPU-wired threads neither threads already stolen. |
488 | * We don't want to steal CPU-wired threads neither threads already stolen. |
490 | * The latter prevents threads from migrating between CPU's without ever being run. |
489 | * The latter prevents threads from migrating between CPU's without ever being run. |
491 | * We don't want to steal threads whose FPU context is still in CPU. |
490 | * We don't want to steal threads whose FPU context is still in CPU. |
492 | */ |
491 | */ |
493 | spinlock_lock(&t->lock); |
492 | spinlock_lock(&t->lock); |
494 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
493 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) { |
495 | /* |
494 | /* |
496 | * Remove t from r. |
495 | * Remove t from r. |
497 | */ |
496 | */ |
498 | spinlock_unlock(&t->lock); |
497 | spinlock_unlock(&t->lock); |
499 | 498 | ||
500 | atomic_dec(&cpu->nrdy); |
499 | atomic_dec(&cpu->nrdy); |
501 | atomic_dec(&nrdy); |
500 | atomic_dec(&nrdy); |
502 | 501 | ||
503 | r->n--; |
502 | r->n--; |
504 | list_remove(&t->rq_link); |
503 | list_remove(&t->rq_link); |
505 | 504 | ||
506 | break; |
505 | break; |
507 | } |
506 | } |
508 | spinlock_unlock(&t->lock); |
507 | spinlock_unlock(&t->lock); |
509 | l = l->prev; |
508 | l = l->prev; |
510 | t = NULL; |
509 | t = NULL; |
511 | } |
510 | } |
512 | spinlock_unlock(&r->lock); |
511 | spinlock_unlock(&r->lock); |
513 | 512 | ||
514 | if (t) { |
513 | if (t) { |
515 | /* |
514 | /* |
516 | * Ready t on local CPU |
515 | * Ready t on local CPU |
517 | */ |
516 | */ |
518 | spinlock_lock(&t->lock); |
517 | spinlock_lock(&t->lock); |
519 | #ifdef KCPULB_VERBOSE |
518 | #ifdef KCPULB_VERBOSE |
520 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active); |
519 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active); |
521 | #endif |
520 | #endif |
522 | t->flags |= X_STOLEN; |
521 | t->flags |= X_STOLEN; |
523 | spinlock_unlock(&t->lock); |
522 | spinlock_unlock(&t->lock); |
524 | 523 | ||
525 | thread_ready(t); |
524 | thread_ready(t); |
526 | 525 | ||
527 | interrupts_restore(ipl); |
526 | interrupts_restore(ipl); |
528 | 527 | ||
529 | if (--count == 0) |
528 | if (--count == 0) |
530 | goto satisfied; |
529 | goto satisfied; |
531 | 530 | ||
532 | /* |
531 | /* |
533 | * We are not satisfied yet, focus on another CPU next time. |
532 | * We are not satisfied yet, focus on another CPU next time. |
534 | */ |
533 | */ |
535 | k++; |
534 | k++; |
536 | 535 | ||
537 | continue; |
536 | continue; |
538 | } |
537 | } |
539 | interrupts_restore(ipl); |
538 | interrupts_restore(ipl); |
540 | } |
539 | } |
541 | } |
540 | } |
542 | 541 | ||
543 | if (atomic_get(&CPU->nrdy)) { |
542 | if (atomic_get(&CPU->nrdy)) { |
544 | /* |
543 | /* |
545 | * Be a little bit light-weight and let migrated threads run. |
544 | * Be a little bit light-weight and let migrated threads run. |
546 | */ |
545 | */ |
547 | scheduler(); |
546 | scheduler(); |
548 | } else { |
547 | } else { |
549 | /* |
548 | /* |
550 | * We failed to migrate a single thread. |
549 | * We failed to migrate a single thread. |
551 | * Give up this turn. |
550 | * Give up this turn. |
552 | */ |
551 | */ |
553 | goto loop; |
552 | goto loop; |
554 | } |
553 | } |
555 | 554 | ||
556 | goto not_satisfied; |
555 | goto not_satisfied; |
557 | 556 | ||
558 | satisfied: |
557 | satisfied: |
559 | goto loop; |
558 | goto loop; |
560 | } |
559 | } |
561 | 560 | ||
562 | #endif /* CONFIG_SMP */ |
561 | #endif /* CONFIG_SMP */ |
563 | 562 | ||
564 | 563 | ||
565 | /** Print information about threads & scheduler queues */ |
564 | /** Print information about threads & scheduler queues */ |
566 | void sched_print_list(void) |
565 | void sched_print_list(void) |
567 | { |
566 | { |
568 | ipl_t ipl; |
567 | ipl_t ipl; |
569 | int cpu,i; |
568 | int cpu,i; |
570 | runq_t *r; |
569 | runq_t *r; |
571 | thread_t *t; |
570 | thread_t *t; |
572 | link_t *cur; |
571 | link_t *cur; |
573 | 572 | ||
574 | /* We are going to mess with scheduler structures, |
573 | /* We are going to mess with scheduler structures, |
575 | * let's not be interrupted */ |
574 | * let's not be interrupted */ |
576 | ipl = interrupts_disable(); |
575 | ipl = interrupts_disable(); |
577 | printf("*********** Scheduler dump ***********\n"); |
576 | printf("*********** Scheduler dump ***********\n"); |
578 | for (cpu=0;cpu < config.cpu_count; cpu++) { |
577 | for (cpu=0;cpu < config.cpu_count; cpu++) { |
579 | if (!cpus[cpu].active) |
578 | if (!cpus[cpu].active) |
580 | continue; |
579 | continue; |
581 | spinlock_lock(&cpus[cpu].lock); |
580 | spinlock_lock(&cpus[cpu].lock); |
582 | printf("cpu%d: nrdy: %d needs_relink: %d\n", |
581 | printf("cpu%d: nrdy: %d needs_relink: %d\n", |
583 | cpus[cpu].id, atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink); |
582 | cpus[cpu].id, atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink); |
584 | 583 | ||
585 | for (i=0; i<RQ_COUNT; i++) { |
584 | for (i=0; i<RQ_COUNT; i++) { |
586 | r = &cpus[cpu].rq[i]; |
585 | r = &cpus[cpu].rq[i]; |
587 | spinlock_lock(&r->lock); |
586 | spinlock_lock(&r->lock); |
588 | if (!r->n) { |
587 | if (!r->n) { |
589 | spinlock_unlock(&r->lock); |
588 | spinlock_unlock(&r->lock); |
590 | continue; |
589 | continue; |
591 | } |
590 | } |
592 | printf("\tRq %d: ", i); |
591 | printf("\tRq %d: ", i); |
593 | for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) { |
592 | for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) { |
594 | t = list_get_instance(cur, thread_t, rq_link); |
593 | t = list_get_instance(cur, thread_t, rq_link); |
595 | printf("%d(%s) ", t->tid, |
594 | printf("%d(%s) ", t->tid, |
596 | thread_states[t->state]); |
595 | thread_states[t->state]); |
597 | } |
596 | } |
598 | printf("\n"); |
597 | printf("\n"); |
599 | spinlock_unlock(&r->lock); |
598 | spinlock_unlock(&r->lock); |
600 | } |
599 | } |
601 | spinlock_unlock(&cpus[cpu].lock); |
600 | spinlock_unlock(&cpus[cpu].lock); |
602 | } |
601 | } |
603 | 602 | ||
604 | interrupts_restore(ipl); |
603 | interrupts_restore(ipl); |
605 | } |
604 | } |
606 | 605 |