1 | /*
|
---|
2 | * Copyright (c) 2010 Jakub Jermar
|
---|
3 | * Copyright (c) 2023 Jiří Zárevúcky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup kernel_generic_proc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * @brief Scheduler and load balancing.
|
---|
37 | *
|
---|
38 | * This file contains the scheduler and kcpulb kernel thread which
|
---|
39 | * performs load-balancing of per-CPU run queues.
|
---|
40 | */
|
---|
41 |
|
---|
42 | #include <assert.h>
|
---|
43 | #include <atomic.h>
|
---|
44 | #include <proc/scheduler.h>
|
---|
45 | #include <proc/thread.h>
|
---|
46 | #include <proc/task.h>
|
---|
47 | #include <mm/frame.h>
|
---|
48 | #include <mm/page.h>
|
---|
49 | #include <mm/as.h>
|
---|
50 | #include <time/timeout.h>
|
---|
51 | #include <time/delay.h>
|
---|
52 | #include <arch/asm.h>
|
---|
53 | #include <arch/cycle.h>
|
---|
54 | #include <atomic.h>
|
---|
55 | #include <synch/spinlock.h>
|
---|
56 | #include <config.h>
|
---|
57 | #include <context.h>
|
---|
58 | #include <fpu_context.h>
|
---|
59 | #include <halt.h>
|
---|
60 | #include <arch.h>
|
---|
61 | #include <adt/list.h>
|
---|
62 | #include <panic.h>
|
---|
63 | #include <cpu.h>
|
---|
64 | #include <stdio.h>
|
---|
65 | #include <log.h>
|
---|
66 | #include <stacktrace.h>
|
---|
67 |
|
---|
68 | atomic_size_t nrdy; /**< Number of ready threads in the system. */
|
---|
69 |
|
---|
70 | #ifdef CONFIG_FPU_LAZY
|
---|
71 | void scheduler_fpu_lazy_request(void)
|
---|
72 | {
|
---|
73 | fpu_enable();
|
---|
74 |
|
---|
75 | /* We need this lock to ensure synchronization with thread destructor. */
|
---|
76 | irq_spinlock_lock(&CPU->fpu_lock, false);
|
---|
77 |
|
---|
78 | /* Save old context */
|
---|
79 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner, memory_order_relaxed);
|
---|
80 | if (owner != NULL) {
|
---|
81 | fpu_context_save(&owner->fpu_context);
|
---|
82 | atomic_store_explicit(&CPU->fpu_owner, NULL, memory_order_relaxed);
|
---|
83 | }
|
---|
84 |
|
---|
85 | irq_spinlock_unlock(&CPU->fpu_lock, false);
|
---|
86 |
|
---|
87 | if (THREAD->fpu_context_exists) {
|
---|
88 | fpu_context_restore(&THREAD->fpu_context);
|
---|
89 | } else {
|
---|
90 | fpu_init();
|
---|
91 | THREAD->fpu_context_exists = true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | atomic_store_explicit(&CPU->fpu_owner, THREAD, memory_order_relaxed);
|
---|
95 | }
|
---|
96 | #endif /* CONFIG_FPU_LAZY */
|
---|
97 |
|
---|
98 | /** Initialize scheduler
|
---|
99 | *
|
---|
100 | * Initialize kernel scheduler.
|
---|
101 | *
|
---|
102 | */
|
---|
103 | void scheduler_init(void)
|
---|
104 | {
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Get thread to be scheduled
|
---|
108 | *
|
---|
109 | * Get the optimal thread to be scheduled
|
---|
110 | * according to thread accounting and scheduler
|
---|
111 | * policy.
|
---|
112 | *
|
---|
113 | * @return Thread to be scheduled.
|
---|
114 | *
|
---|
115 | */
|
---|
116 | static thread_t *try_find_thread(int *rq_index)
|
---|
117 | {
|
---|
118 | assert(interrupts_disabled());
|
---|
119 | assert(CPU != NULL);
|
---|
120 |
|
---|
121 | if (atomic_load(&CPU->nrdy) == 0)
|
---|
122 | return NULL;
|
---|
123 |
|
---|
124 | for (int i = 0; i < RQ_COUNT; i++) {
|
---|
125 | irq_spinlock_lock(&(CPU->rq[i].lock), false);
|
---|
126 | if (CPU->rq[i].n == 0) {
|
---|
127 | /*
|
---|
128 | * If this queue is empty, try a lower-priority queue.
|
---|
129 | */
|
---|
130 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
131 | continue;
|
---|
132 | }
|
---|
133 |
|
---|
134 | atomic_dec(&CPU->nrdy);
|
---|
135 | atomic_dec(&nrdy);
|
---|
136 | CPU->rq[i].n--;
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Take the first thread from the queue.
|
---|
140 | */
|
---|
141 | thread_t *thread = list_get_instance(
|
---|
142 | list_first(&CPU->rq[i].rq), thread_t, rq_link);
|
---|
143 | list_remove(&thread->rq_link);
|
---|
144 |
|
---|
145 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
146 |
|
---|
147 | *rq_index = i;
|
---|
148 | return thread;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Get thread to be scheduled
|
---|
155 | *
|
---|
156 | * Get the optimal thread to be scheduled
|
---|
157 | * according to thread accounting and scheduler
|
---|
158 | * policy.
|
---|
159 | *
|
---|
160 | * @return Thread to be scheduled.
|
---|
161 | *
|
---|
162 | */
|
---|
163 | static thread_t *find_best_thread(int *rq_index)
|
---|
164 | {
|
---|
165 | assert(interrupts_disabled());
|
---|
166 | assert(CPU != NULL);
|
---|
167 |
|
---|
168 | while (true) {
|
---|
169 | thread_t *thread = try_find_thread(rq_index);
|
---|
170 |
|
---|
171 | if (thread != NULL)
|
---|
172 | return thread;
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * For there was nothing to run, the CPU goes to sleep
|
---|
176 | * until a hardware interrupt or an IPI comes.
|
---|
177 | * This improves energy saving and hyperthreading.
|
---|
178 | */
|
---|
179 | CPU_LOCAL->idle = true;
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Go to sleep with interrupts enabled.
|
---|
183 | * Ideally, this should be atomic, but this is not guaranteed on
|
---|
184 | * all platforms yet, so it is possible we will go sleep when
|
---|
185 | * a thread has just become available.
|
---|
186 | */
|
---|
187 | cpu_interruptible_sleep();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | static void switch_task(task_t *task)
|
---|
192 | {
|
---|
193 | /* If the task stays the same, a lot of work is avoided. */
|
---|
194 | if (TASK == task)
|
---|
195 | return;
|
---|
196 |
|
---|
197 | as_t *old_as = AS;
|
---|
198 | as_t *new_as = task->as;
|
---|
199 |
|
---|
200 | /* It is possible for two tasks to share one address space. */
|
---|
201 | if (old_as != new_as)
|
---|
202 | as_switch(old_as, new_as);
|
---|
203 |
|
---|
204 | if (TASK)
|
---|
205 | task_release(TASK);
|
---|
206 |
|
---|
207 | TASK = task;
|
---|
208 |
|
---|
209 | task_hold(TASK);
|
---|
210 |
|
---|
211 | before_task_runs_arch();
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Prevent rq starvation
|
---|
215 | *
|
---|
216 | * Prevent low priority threads from starving in rq's.
|
---|
217 | *
|
---|
218 | * When the function decides to relink rq's, it reconnects
|
---|
219 | * respective pointers so that in result threads with 'pri'
|
---|
220 | * greater or equal start are moved to a higher-priority queue.
|
---|
221 | *
|
---|
222 | * @param start Threshold priority.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | static void relink_rq(int start)
|
---|
226 | {
|
---|
227 | assert(interrupts_disabled());
|
---|
228 |
|
---|
229 | if (CPU_LOCAL->current_clock_tick < CPU_LOCAL->relink_deadline)
|
---|
230 | return;
|
---|
231 |
|
---|
232 | CPU_LOCAL->relink_deadline = CPU_LOCAL->current_clock_tick + NEEDS_RELINK_MAX;
|
---|
233 |
|
---|
234 | /* Temporary cache for lists we are moving. */
|
---|
235 | list_t list;
|
---|
236 | list_initialize(&list);
|
---|
237 |
|
---|
238 | size_t n = 0;
|
---|
239 |
|
---|
240 | /* Move every list (except the one with highest priority) one level up. */
|
---|
241 | for (int i = RQ_COUNT - 1; i > start; i--) {
|
---|
242 | irq_spinlock_lock(&CPU->rq[i].lock, false);
|
---|
243 |
|
---|
244 | /* Swap lists. */
|
---|
245 | list_swap(&CPU->rq[i].rq, &list);
|
---|
246 |
|
---|
247 | /* Swap number of items. */
|
---|
248 | size_t tmpn = CPU->rq[i].n;
|
---|
249 | CPU->rq[i].n = n;
|
---|
250 | n = tmpn;
|
---|
251 |
|
---|
252 | irq_spinlock_unlock(&CPU->rq[i].lock, false);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /* Append the contents of rq[start + 1] to rq[start]. */
|
---|
256 | if (n != 0) {
|
---|
257 | irq_spinlock_lock(&CPU->rq[start].lock, false);
|
---|
258 | list_concat(&CPU->rq[start].rq, &list);
|
---|
259 | CPU->rq[start].n += n;
|
---|
260 | irq_spinlock_unlock(&CPU->rq[start].lock, false);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Do whatever needs to be done with current FPU state before we switch to
|
---|
266 | * another thread.
|
---|
267 | */
|
---|
268 | static void fpu_cleanup(void)
|
---|
269 | {
|
---|
270 | #if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
|
---|
271 | fpu_context_save(&THREAD->fpu_context);
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Set correct FPU state for this thread after switch from another thread.
|
---|
277 | */
|
---|
278 | static void fpu_restore(void)
|
---|
279 | {
|
---|
280 | #ifdef CONFIG_FPU_LAZY
|
---|
281 | /*
|
---|
282 | * The only concurrent modification possible for fpu_owner here is
|
---|
283 | * another thread changing it from itself to NULL in its destructor.
|
---|
284 | */
|
---|
285 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner,
|
---|
286 | memory_order_relaxed);
|
---|
287 |
|
---|
288 | if (THREAD == owner)
|
---|
289 | fpu_enable();
|
---|
290 | else
|
---|
291 | fpu_disable();
|
---|
292 |
|
---|
293 | #elif defined CONFIG_FPU
|
---|
294 | fpu_enable();
|
---|
295 | if (THREAD->fpu_context_exists)
|
---|
296 | fpu_context_restore(&THREAD->fpu_context);
|
---|
297 | else {
|
---|
298 | fpu_init();
|
---|
299 | THREAD->fpu_context_exists = true;
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Things to do before we switch to THREAD context.
|
---|
305 | */
|
---|
306 | static void prepare_to_run_thread(int rq_index)
|
---|
307 | {
|
---|
308 | relink_rq(rq_index);
|
---|
309 |
|
---|
310 | switch_task(THREAD->task);
|
---|
311 |
|
---|
312 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
313 | assert(atomic_get_unordered(&THREAD->cpu) == CPU);
|
---|
314 |
|
---|
315 | atomic_set_unordered(&THREAD->state, Running);
|
---|
316 | atomic_set_unordered(&THREAD->priority, rq_index); /* Correct rq index */
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Clear the stolen flag so that it can be migrated
|
---|
320 | * when load balancing needs emerge.
|
---|
321 | */
|
---|
322 | THREAD->stolen = false;
|
---|
323 |
|
---|
324 | #ifdef SCHEDULER_VERBOSE
|
---|
325 | log(LF_OTHER, LVL_DEBUG,
|
---|
326 | "cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
|
---|
327 | ", nrdy=%zu)", CPU->id, THREAD->tid, rq_index,
|
---|
328 | THREAD->ticks, atomic_load(&CPU->nrdy));
|
---|
329 | #endif
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * Some architectures provide late kernel PA2KA(identity)
|
---|
333 | * mapping in a page fault handler. However, the page fault
|
---|
334 | * handler uses the kernel stack of the running thread and
|
---|
335 | * therefore cannot be used to map it. The kernel stack, if
|
---|
336 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
337 | * function must be executed before the switch to the new stack.
|
---|
338 | */
|
---|
339 | before_thread_runs_arch();
|
---|
340 |
|
---|
341 | #ifdef CONFIG_UDEBUG
|
---|
342 | if (atomic_get_unordered(&THREAD->btrace)) {
|
---|
343 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
344 | if (istate != NULL) {
|
---|
345 | printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
|
---|
346 | stack_trace_istate(istate);
|
---|
347 | } else {
|
---|
348 | printf("Thread %" PRIu64 " interrupt state not available\n", THREAD->tid);
|
---|
349 | }
|
---|
350 |
|
---|
351 | atomic_set_unordered(&THREAD->btrace, false);
|
---|
352 | }
|
---|
353 | #endif
|
---|
354 |
|
---|
355 | fpu_restore();
|
---|
356 |
|
---|
357 | /* Time allocation in microseconds. */
|
---|
358 | uint64_t time_to_run = (rq_index + 1) * 10000;
|
---|
359 |
|
---|
360 | /* Set the time of next preemption. */
|
---|
361 | CPU_LOCAL->preempt_deadline =
|
---|
362 | CPU_LOCAL->current_clock_tick + us2ticks(time_to_run);
|
---|
363 |
|
---|
364 | /* Save current CPU cycle */
|
---|
365 | THREAD->last_cycle = get_cycle();
|
---|
366 |
|
---|
367 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
368 | }
|
---|
369 |
|
---|
370 | static void add_to_rq(thread_t *thread, cpu_t *cpu, int i)
|
---|
371 | {
|
---|
372 | /* Add to the appropriate runqueue. */
|
---|
373 | runq_t *rq = &cpu->rq[i];
|
---|
374 |
|
---|
375 | irq_spinlock_lock(&rq->lock, false);
|
---|
376 | list_append(&thread->rq_link, &rq->rq);
|
---|
377 | rq->n++;
|
---|
378 | irq_spinlock_unlock(&rq->lock, false);
|
---|
379 |
|
---|
380 | atomic_inc(&nrdy);
|
---|
381 | atomic_inc(&cpu->nrdy);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /** Requeue a thread that was just preempted on this CPU.
|
---|
385 | */
|
---|
386 | static void thread_requeue_preempted(thread_t *thread)
|
---|
387 | {
|
---|
388 | irq_spinlock_lock(&thread->lock, false);
|
---|
389 |
|
---|
390 | assert(atomic_get_unordered(&thread->state) == Running);
|
---|
391 | assert(atomic_get_unordered(&thread->cpu) == CPU);
|
---|
392 |
|
---|
393 | int prio = atomic_get_unordered(&thread->priority);
|
---|
394 |
|
---|
395 | if (prio < RQ_COUNT - 1) {
|
---|
396 | prio++;
|
---|
397 | atomic_set_unordered(&thread->priority, prio);
|
---|
398 | }
|
---|
399 |
|
---|
400 | atomic_set_unordered(&thread->state, Ready);
|
---|
401 |
|
---|
402 | irq_spinlock_unlock(&thread->lock, false);
|
---|
403 |
|
---|
404 | add_to_rq(thread, CPU, prio);
|
---|
405 | }
|
---|
406 |
|
---|
407 | void thread_requeue_sleeping(thread_t *thread)
|
---|
408 | {
|
---|
409 | ipl_t ipl = interrupts_disable();
|
---|
410 |
|
---|
411 | irq_spinlock_lock(&thread->lock, false);
|
---|
412 |
|
---|
413 | assert(atomic_get_unordered(&thread->state) == Sleeping || atomic_get_unordered(&thread->state) == Entering);
|
---|
414 |
|
---|
415 | atomic_set_unordered(&thread->priority, 0);
|
---|
416 | atomic_set_unordered(&thread->state, Ready);
|
---|
417 |
|
---|
418 | /* Prefer the CPU on which the thread ran last */
|
---|
419 | cpu_t *cpu = atomic_get_unordered(&thread->cpu);
|
---|
420 |
|
---|
421 | if (!cpu) {
|
---|
422 | cpu = CPU;
|
---|
423 | atomic_set_unordered(&thread->cpu, CPU);
|
---|
424 | }
|
---|
425 |
|
---|
426 | irq_spinlock_unlock(&thread->lock, false);
|
---|
427 |
|
---|
428 | add_to_rq(thread, cpu, 0);
|
---|
429 |
|
---|
430 | interrupts_restore(ipl);
|
---|
431 | }
|
---|
432 |
|
---|
433 | static void cleanup_after_thread(thread_t *thread, state_t out_state)
|
---|
434 | {
|
---|
435 | assert(CURRENT->mutex_locks == 0);
|
---|
436 | assert(interrupts_disabled());
|
---|
437 |
|
---|
438 | int expected;
|
---|
439 |
|
---|
440 | switch (out_state) {
|
---|
441 | case Running:
|
---|
442 | thread_requeue_preempted(thread);
|
---|
443 | break;
|
---|
444 |
|
---|
445 | case Exiting:
|
---|
446 | waitq_close(&thread->join_wq);
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * Release the reference CPU has for the thread.
|
---|
450 | * If there are no other references (e.g. threads calling join),
|
---|
451 | * the thread structure is deallocated.
|
---|
452 | */
|
---|
453 | thread_put(thread);
|
---|
454 | break;
|
---|
455 |
|
---|
456 | case Sleeping:
|
---|
457 | expected = SLEEP_INITIAL;
|
---|
458 |
|
---|
459 | /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
|
---|
460 | if (!atomic_compare_exchange_strong_explicit(&thread->sleep_state,
|
---|
461 | &expected, SLEEP_ASLEEP,
|
---|
462 | memory_order_acq_rel, memory_order_acquire)) {
|
---|
463 |
|
---|
464 | assert(expected == SLEEP_WOKE);
|
---|
465 | /* The thread has already been woken up, requeue immediately. */
|
---|
466 | thread_requeue_sleeping(thread);
|
---|
467 | }
|
---|
468 | break;
|
---|
469 |
|
---|
470 | default:
|
---|
471 | /*
|
---|
472 | * Entering state is unexpected.
|
---|
473 | */
|
---|
474 | panic("tid%" PRIu64 ": unexpected state %s.",
|
---|
475 | thread->tid, thread_states[out_state]);
|
---|
476 | break;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | /** Switch to scheduler context to let other threads run. */
|
---|
481 | void scheduler_enter(state_t new_state)
|
---|
482 | {
|
---|
483 | ipl_t ipl = interrupts_disable();
|
---|
484 |
|
---|
485 | assert(CPU != NULL);
|
---|
486 | assert(THREAD != NULL);
|
---|
487 |
|
---|
488 | if (atomic_load(&haltstate))
|
---|
489 | halt();
|
---|
490 |
|
---|
491 | /* Check if we have a thread to switch to. */
|
---|
492 |
|
---|
493 | int rq_index;
|
---|
494 | thread_t *new_thread = try_find_thread(&rq_index);
|
---|
495 |
|
---|
496 | if (new_thread == NULL && new_state == Running) {
|
---|
497 | /* No other thread to run, but we still have work to do here. */
|
---|
498 | interrupts_restore(ipl);
|
---|
499 | return;
|
---|
500 | }
|
---|
501 |
|
---|
502 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
503 |
|
---|
504 | atomic_set_unordered(&THREAD->state, new_state);
|
---|
505 |
|
---|
506 | /* Update thread kernel accounting */
|
---|
507 | atomic_time_increment(&THREAD->kcycles, get_cycle() - THREAD->last_cycle);
|
---|
508 |
|
---|
509 | fpu_cleanup();
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * On Sparc, this saves some extra userspace state that's not
|
---|
513 | * covered by context_save()/context_restore().
|
---|
514 | */
|
---|
515 | after_thread_ran_arch();
|
---|
516 |
|
---|
517 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
518 |
|
---|
519 | CPU_LOCAL->exiting_state = new_state;
|
---|
520 |
|
---|
521 | if (new_thread) {
|
---|
522 | thread_t *old_thread = THREAD;
|
---|
523 | CPU_LOCAL->prev_thread = old_thread;
|
---|
524 | THREAD = new_thread;
|
---|
525 | /* No waiting necessary, we can switch to the new thread directly. */
|
---|
526 | prepare_to_run_thread(rq_index);
|
---|
527 |
|
---|
528 | current_copy(CURRENT, (current_t *) new_thread->kstack);
|
---|
529 | context_swap(&old_thread->saved_context, &new_thread->saved_context);
|
---|
530 | } else {
|
---|
531 | /*
|
---|
532 | * A new thread isn't immediately available, switch to a separate
|
---|
533 | * stack to sleep or do other idle stuff.
|
---|
534 | */
|
---|
535 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
536 | context_swap(&THREAD->saved_context, &CPU_LOCAL->scheduler_context);
|
---|
537 | }
|
---|
538 |
|
---|
539 | assert(CURRENT->mutex_locks == 0);
|
---|
540 | assert(interrupts_disabled());
|
---|
541 |
|
---|
542 | /* Check if we need to clean up after another thread. */
|
---|
543 | if (CPU_LOCAL->prev_thread) {
|
---|
544 | cleanup_after_thread(CPU_LOCAL->prev_thread, CPU_LOCAL->exiting_state);
|
---|
545 | CPU_LOCAL->prev_thread = NULL;
|
---|
546 | }
|
---|
547 |
|
---|
548 | interrupts_restore(ipl);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /** Enter main scheduler loop. Never returns.
|
---|
552 | *
|
---|
553 | * This function switches to a runnable thread as soon as one is available,
|
---|
554 | * after which it is only switched back to if a thread is stopping and there is
|
---|
555 | * no other thread to run in its place. We need a separate context for that
|
---|
556 | * because we're going to block the CPU, which means we need another context
|
---|
557 | * to clean up after the previous thread.
|
---|
558 | */
|
---|
559 | void scheduler_run(void)
|
---|
560 | {
|
---|
561 | assert(interrupts_disabled());
|
---|
562 |
|
---|
563 | assert(CPU != NULL);
|
---|
564 | assert(TASK == NULL);
|
---|
565 | assert(THREAD == NULL);
|
---|
566 | assert(interrupts_disabled());
|
---|
567 |
|
---|
568 | while (!atomic_load(&haltstate)) {
|
---|
569 | assert(CURRENT->mutex_locks == 0);
|
---|
570 |
|
---|
571 | int rq_index;
|
---|
572 | THREAD = find_best_thread(&rq_index);
|
---|
573 | prepare_to_run_thread(rq_index);
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
|
---|
577 | * thread's stack.
|
---|
578 | */
|
---|
579 | current_copy(CURRENT, (current_t *) THREAD->kstack);
|
---|
580 |
|
---|
581 | /* Switch to thread context. */
|
---|
582 | context_swap(&CPU_LOCAL->scheduler_context, &THREAD->saved_context);
|
---|
583 |
|
---|
584 | /* Back from another thread. */
|
---|
585 | assert(CPU != NULL);
|
---|
586 | assert(THREAD != NULL);
|
---|
587 | assert(CURRENT->mutex_locks == 0);
|
---|
588 | assert(interrupts_disabled());
|
---|
589 |
|
---|
590 | cleanup_after_thread(THREAD, CPU_LOCAL->exiting_state);
|
---|
591 |
|
---|
592 | /*
|
---|
593 | * Necessary because we're allowing interrupts in find_best_thread(),
|
---|
594 | * so we need to avoid other code referencing the thread we left.
|
---|
595 | */
|
---|
596 | THREAD = NULL;
|
---|
597 | }
|
---|
598 |
|
---|
599 | halt();
|
---|
600 | }
|
---|
601 |
|
---|
602 | /** Thread wrapper.
|
---|
603 | *
|
---|
604 | * This wrapper is provided to ensure that a starting thread properly handles
|
---|
605 | * everything it needs to do when first scheduled, and when it exits.
|
---|
606 | */
|
---|
607 | void thread_main_func(void)
|
---|
608 | {
|
---|
609 | assert(interrupts_disabled());
|
---|
610 |
|
---|
611 | void (*f)(void *) = THREAD->thread_code;
|
---|
612 | void *arg = THREAD->thread_arg;
|
---|
613 |
|
---|
614 | /* This is where each thread wakes up after its creation */
|
---|
615 |
|
---|
616 | /* Check if we need to clean up after another thread. */
|
---|
617 | if (CPU_LOCAL->prev_thread) {
|
---|
618 | cleanup_after_thread(CPU_LOCAL->prev_thread, CPU_LOCAL->exiting_state);
|
---|
619 | CPU_LOCAL->prev_thread = NULL;
|
---|
620 | }
|
---|
621 |
|
---|
622 | interrupts_enable();
|
---|
623 |
|
---|
624 | f(arg);
|
---|
625 |
|
---|
626 | thread_exit();
|
---|
627 |
|
---|
628 | /* Not reached */
|
---|
629 | }
|
---|
630 |
|
---|
631 | #ifdef CONFIG_SMP
|
---|
632 |
|
---|
633 | static thread_t *steal_thread_from(cpu_t *old_cpu, int i)
|
---|
634 | {
|
---|
635 | runq_t *old_rq = &old_cpu->rq[i];
|
---|
636 | runq_t *new_rq = &CPU->rq[i];
|
---|
637 |
|
---|
638 | ipl_t ipl = interrupts_disable();
|
---|
639 |
|
---|
640 | irq_spinlock_lock(&old_rq->lock, false);
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * If fpu_owner is any thread in the list, its store is seen here thanks to
|
---|
644 | * the runqueue lock.
|
---|
645 | */
|
---|
646 | thread_t *fpu_owner = atomic_load_explicit(&old_cpu->fpu_owner,
|
---|
647 | memory_order_relaxed);
|
---|
648 |
|
---|
649 | /* Search rq from the back */
|
---|
650 | list_foreach_rev(old_rq->rq, rq_link, thread_t, thread) {
|
---|
651 |
|
---|
652 | irq_spinlock_lock(&thread->lock, false);
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * Do not steal CPU-wired threads, threads
|
---|
656 | * already stolen, threads for which migration
|
---|
657 | * was temporarily disabled or threads whose
|
---|
658 | * FPU context is still in the CPU.
|
---|
659 | */
|
---|
660 | if (thread->stolen || thread->nomigrate ||
|
---|
661 | thread == fpu_owner) {
|
---|
662 | irq_spinlock_unlock(&thread->lock, false);
|
---|
663 | continue;
|
---|
664 | }
|
---|
665 |
|
---|
666 | thread->stolen = true;
|
---|
667 | atomic_set_unordered(&thread->cpu, CPU);
|
---|
668 |
|
---|
669 | irq_spinlock_unlock(&thread->lock, false);
|
---|
670 |
|
---|
671 | /*
|
---|
672 | * Ready thread on local CPU
|
---|
673 | */
|
---|
674 |
|
---|
675 | #ifdef KCPULB_VERBOSE
|
---|
676 | log(LF_OTHER, LVL_DEBUG,
|
---|
677 | "kcpulb%u: TID %" PRIu64 " -> cpu%u, "
|
---|
678 | "nrdy=%ld, avg=%ld", CPU->id, thread->tid,
|
---|
679 | CPU->id, atomic_load(&CPU->nrdy),
|
---|
680 | atomic_load(&nrdy) / config.cpu_active);
|
---|
681 | #endif
|
---|
682 |
|
---|
683 | /* Remove thread from ready queue. */
|
---|
684 | old_rq->n--;
|
---|
685 | list_remove(&thread->rq_link);
|
---|
686 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
687 |
|
---|
688 | /* Append thread to local queue. */
|
---|
689 | irq_spinlock_lock(&new_rq->lock, false);
|
---|
690 | list_append(&thread->rq_link, &new_rq->rq);
|
---|
691 | new_rq->n++;
|
---|
692 | irq_spinlock_unlock(&new_rq->lock, false);
|
---|
693 |
|
---|
694 | atomic_dec(&old_cpu->nrdy);
|
---|
695 | atomic_inc(&CPU->nrdy);
|
---|
696 | interrupts_restore(ipl);
|
---|
697 | return thread;
|
---|
698 | }
|
---|
699 |
|
---|
700 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
701 | interrupts_restore(ipl);
|
---|
702 | return NULL;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /** Load balancing thread
|
---|
706 | *
|
---|
707 | * SMP load balancing thread, supervising thread supplies
|
---|
708 | * for the CPU it's wired to.
|
---|
709 | *
|
---|
710 | * @param arg Generic thread argument (unused).
|
---|
711 | *
|
---|
712 | */
|
---|
713 | void kcpulb(void *arg)
|
---|
714 | {
|
---|
715 | size_t average;
|
---|
716 | size_t rdy;
|
---|
717 |
|
---|
718 | loop:
|
---|
719 | /*
|
---|
720 | * Work in 1s intervals.
|
---|
721 | */
|
---|
722 | thread_sleep(1);
|
---|
723 |
|
---|
724 | not_satisfied:
|
---|
725 | /*
|
---|
726 | * Calculate the number of threads that will be migrated/stolen from
|
---|
727 | * other CPU's. Note that situation can have changed between two
|
---|
728 | * passes. Each time get the most up to date counts.
|
---|
729 | *
|
---|
730 | */
|
---|
731 | average = atomic_load(&nrdy) / config.cpu_active + 1;
|
---|
732 | rdy = atomic_load(&CPU->nrdy);
|
---|
733 |
|
---|
734 | if (average <= rdy)
|
---|
735 | goto satisfied;
|
---|
736 |
|
---|
737 | size_t count = average - rdy;
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * Searching least priority queues on all CPU's first and most priority
|
---|
741 | * queues on all CPU's last.
|
---|
742 | */
|
---|
743 | size_t acpu;
|
---|
744 | int rq;
|
---|
745 |
|
---|
746 | for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
|
---|
747 | for (acpu = 0; acpu < config.cpu_active; acpu++) {
|
---|
748 | cpu_t *cpu = &cpus[acpu];
|
---|
749 |
|
---|
750 | /*
|
---|
751 | * Not interested in ourselves.
|
---|
752 | * Doesn't require interrupt disabling for kcpulb has
|
---|
753 | * THREAD_FLAG_WIRED.
|
---|
754 | *
|
---|
755 | */
|
---|
756 | if (CPU == cpu)
|
---|
757 | continue;
|
---|
758 |
|
---|
759 | if (atomic_load(&cpu->nrdy) <= average)
|
---|
760 | continue;
|
---|
761 |
|
---|
762 | if (steal_thread_from(cpu, rq) && --count == 0)
|
---|
763 | goto satisfied;
|
---|
764 | }
|
---|
765 | }
|
---|
766 |
|
---|
767 | if (atomic_load(&CPU->nrdy)) {
|
---|
768 | /*
|
---|
769 | * Be a little bit light-weight and let migrated threads run.
|
---|
770 | *
|
---|
771 | */
|
---|
772 | thread_yield();
|
---|
773 | } else {
|
---|
774 | /*
|
---|
775 | * We failed to migrate a single thread.
|
---|
776 | * Give up this turn.
|
---|
777 | *
|
---|
778 | */
|
---|
779 | goto loop;
|
---|
780 | }
|
---|
781 |
|
---|
782 | goto not_satisfied;
|
---|
783 |
|
---|
784 | satisfied:
|
---|
785 | goto loop;
|
---|
786 | }
|
---|
787 | #endif /* CONFIG_SMP */
|
---|
788 |
|
---|
789 | /** Print information about threads & scheduler queues
|
---|
790 | *
|
---|
791 | */
|
---|
792 | void sched_print_list(void)
|
---|
793 | {
|
---|
794 | size_t cpu;
|
---|
795 | for (cpu = 0; cpu < config.cpu_count; cpu++) {
|
---|
796 | if (!cpus[cpu].active)
|
---|
797 | continue;
|
---|
798 |
|
---|
799 | printf("cpu%u: address=%p, nrdy=%zu\n",
|
---|
800 | cpus[cpu].id, &cpus[cpu], atomic_load(&cpus[cpu].nrdy));
|
---|
801 |
|
---|
802 | unsigned int i;
|
---|
803 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
804 | irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
|
---|
805 | if (cpus[cpu].rq[i].n == 0) {
|
---|
806 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
807 | continue;
|
---|
808 | }
|
---|
809 |
|
---|
810 | printf("\trq[%u]: ", i);
|
---|
811 | list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
|
---|
812 | thread) {
|
---|
813 | printf("%" PRIu64 "(%s) ", thread->tid,
|
---|
814 | thread_states[atomic_get_unordered(&thread->state)]);
|
---|
815 | }
|
---|
816 | printf("\n");
|
---|
817 |
|
---|
818 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
819 | }
|
---|
820 | }
|
---|
821 | }
|
---|
822 |
|
---|
823 | /** @}
|
---|
824 | */
|
---|