Subversion Repositories HelenOS-historic

Rev

Rev 1576 | Rev 1705 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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