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