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