source: mainline/generic/src/proc/scheduler.c@ ff14c520

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ff14c520 was ff14c520, checked in by Jakub Jermar <jakub@…>, 19 years ago

It is now possible to associate symbolic names with both threads and tasks.
More verbose kconsole threads, tasks and scheduler commands.

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