[f761f1eb] | 1 | /*
|
---|
[481d4751] | 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_proc
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[9179d0a] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief Scheduler and load balancing.
|
---|
[9179d0a] | 36 | *
|
---|
[cf26ba9] | 37 | * This file contains the scheduler and kcpulb kernel thread which
|
---|
[9179d0a] | 38 | * performs load-balancing of per-CPU run queues.
|
---|
| 39 | */
|
---|
| 40 |
|
---|
[63e27ef] | 41 | #include <assert.h>
|
---|
[4621d23] | 42 | #include <atomic.h>
|
---|
[f761f1eb] | 43 | #include <proc/scheduler.h>
|
---|
| 44 | #include <proc/thread.h>
|
---|
| 45 | #include <proc/task.h>
|
---|
[32ff43e6] | 46 | #include <mm/frame.h>
|
---|
| 47 | #include <mm/page.h>
|
---|
[20d50a1] | 48 | #include <mm/as.h>
|
---|
[b3f8fb7] | 49 | #include <time/timeout.h>
|
---|
[fe19611] | 50 | #include <time/delay.h>
|
---|
[32ff43e6] | 51 | #include <arch/asm.h>
|
---|
| 52 | #include <arch/faddr.h>
|
---|
[cce6acf] | 53 | #include <arch/cycle.h>
|
---|
[23684b7] | 54 | #include <atomic.h>
|
---|
[32ff43e6] | 55 | #include <synch/spinlock.h>
|
---|
[f761f1eb] | 56 | #include <config.h>
|
---|
| 57 | #include <context.h>
|
---|
[b3f8fb7] | 58 | #include <fpu_context.h>
|
---|
[b2e121a] | 59 | #include <halt.h>
|
---|
[f761f1eb] | 60 | #include <arch.h>
|
---|
[5c9a08b] | 61 | #include <adt/list.h>
|
---|
[02a99d2] | 62 | #include <panic.h>
|
---|
[32ff43e6] | 63 | #include <cpu.h>
|
---|
[bab75df6] | 64 | #include <stdio.h>
|
---|
[b2fa1204] | 65 | #include <log.h>
|
---|
[df58e44] | 66 | #include <stacktrace.h>
|
---|
[9c0a9b3] | 67 |
|
---|
[7d6ec87] | 68 | static void scheduler_separated_stack(void);
|
---|
| 69 |
|
---|
[31e15be] | 70 | atomic_size_t nrdy; /**< Number of ready threads in the system. */
|
---|
[f761f1eb] | 71 |
|
---|
[97f1691] | 72 | /** Take actions before new thread runs.
|
---|
[70527f1] | 73 | *
|
---|
[b60a22c] | 74 | * Perform actions that need to be
|
---|
| 75 | * taken before the newly selected
|
---|
[df58e44] | 76 | * thread is passed control.
|
---|
[70527f1] | 77 | *
|
---|
[a3eeceb6] | 78 | * THREAD->lock is locked on entry
|
---|
| 79 | *
|
---|
[70527f1] | 80 | */
|
---|
[4e7d3dd] | 81 | static void before_thread_runs(void)
|
---|
[0ca6faa] | 82 | {
|
---|
[b49f4ae] | 83 | before_thread_runs_arch();
|
---|
[a35b458] | 84 |
|
---|
[f76fed4] | 85 | #ifdef CONFIG_FPU_LAZY
|
---|
[f3dbe27] | 86 | /*
|
---|
| 87 | * The only concurrent modification possible for fpu_owner here is
|
---|
| 88 | * another thread changing it from itself to NULL in its destructor.
|
---|
| 89 | */
|
---|
| 90 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner,
|
---|
| 91 | memory_order_relaxed);
|
---|
[06f81c4] | 92 |
|
---|
[f3dbe27] | 93 | if (THREAD == owner)
|
---|
[b49f4ae] | 94 | fpu_enable();
|
---|
| 95 | else
|
---|
[da1bafb] | 96 | fpu_disable();
|
---|
[e1326cf] | 97 | #elif defined CONFIG_FPU
|
---|
[b49f4ae] | 98 | fpu_enable();
|
---|
| 99 | if (THREAD->fpu_context_exists)
|
---|
[0366d09d] | 100 | fpu_context_restore(&THREAD->fpu_context);
|
---|
[b49f4ae] | 101 | else {
|
---|
[f76fed4] | 102 | fpu_init();
|
---|
[6eef3c4] | 103 | THREAD->fpu_context_exists = true;
|
---|
[b49f4ae] | 104 | }
|
---|
[f76fed4] | 105 | #endif
|
---|
[a35b458] | 106 |
|
---|
[5b7a107] | 107 | #ifdef CONFIG_UDEBUG
|
---|
[df58e44] | 108 | if (THREAD->btrace) {
|
---|
| 109 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
| 110 | if (istate != NULL) {
|
---|
| 111 | printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
|
---|
| 112 | stack_trace_istate(istate);
|
---|
| 113 | }
|
---|
[a35b458] | 114 |
|
---|
[df58e44] | 115 | THREAD->btrace = false;
|
---|
| 116 | }
|
---|
[5b7a107] | 117 | #endif
|
---|
[0ca6faa] | 118 | }
|
---|
| 119 |
|
---|
[7d6ec87] | 120 | /** Take actions after THREAD had run.
|
---|
[97f1691] | 121 | *
|
---|
| 122 | * Perform actions that need to be
|
---|
| 123 | * taken after the running thread
|
---|
[7d6ec87] | 124 | * had been preempted by the scheduler.
|
---|
[97f1691] | 125 | *
|
---|
| 126 | * THREAD->lock is locked on entry
|
---|
| 127 | *
|
---|
| 128 | */
|
---|
[4e7d3dd] | 129 | static void after_thread_ran(void)
|
---|
[97f1691] | 130 | {
|
---|
| 131 | after_thread_ran_arch();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[5f85c91] | 134 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 135 | void scheduler_fpu_lazy_request(void)
|
---|
| 136 | {
|
---|
| 137 | fpu_enable();
|
---|
[f3dbe27] | 138 |
|
---|
| 139 | /* We need this lock to ensure synchronization with thread destructor. */
|
---|
[169815e] | 140 | irq_spinlock_lock(&CPU->fpu_lock, false);
|
---|
[a35b458] | 141 |
|
---|
[a3eeceb6] | 142 | /* Save old context */
|
---|
[f3dbe27] | 143 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner, memory_order_relaxed);
|
---|
| 144 | if (owner != NULL) {
|
---|
| 145 | fpu_context_save(&owner->fpu_context);
|
---|
| 146 | atomic_store_explicit(&CPU->fpu_owner, NULL, memory_order_relaxed);
|
---|
[b49f4ae] | 147 | }
|
---|
[a35b458] | 148 |
|
---|
[f3dbe27] | 149 | irq_spinlock_unlock(&CPU->fpu_lock, false);
|
---|
| 150 |
|
---|
[7d6ec87] | 151 | if (THREAD->fpu_context_exists) {
|
---|
[0366d09d] | 152 | fpu_context_restore(&THREAD->fpu_context);
|
---|
[7d6ec87] | 153 | } else {
|
---|
[f76fed4] | 154 | fpu_init();
|
---|
[6eef3c4] | 155 | THREAD->fpu_context_exists = true;
|
---|
[b49f4ae] | 156 | }
|
---|
[a35b458] | 157 |
|
---|
[f3dbe27] | 158 | atomic_store_explicit(&CPU->fpu_owner, THREAD, memory_order_relaxed);
|
---|
[b49f4ae] | 159 | }
|
---|
[da1bafb] | 160 | #endif /* CONFIG_FPU_LAZY */
|
---|
[0ca6faa] | 161 |
|
---|
[70527f1] | 162 | /** Initialize scheduler
|
---|
| 163 | *
|
---|
| 164 | * Initialize kernel scheduler.
|
---|
| 165 | *
|
---|
| 166 | */
|
---|
[f761f1eb] | 167 | void scheduler_init(void)
|
---|
| 168 | {
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[70527f1] | 171 | /** Get thread to be scheduled
|
---|
| 172 | *
|
---|
| 173 | * Get the optimal thread to be scheduled
|
---|
[d1a184f] | 174 | * according to thread accounting and scheduler
|
---|
[70527f1] | 175 | * policy.
|
---|
| 176 | *
|
---|
| 177 | * @return Thread to be scheduled.
|
---|
| 178 | *
|
---|
| 179 | */
|
---|
[ec8ef12] | 180 | static thread_t *try_find_thread(int *rq_index)
|
---|
[f761f1eb] | 181 | {
|
---|
[ec8ef12] | 182 | assert(interrupts_disabled());
|
---|
[63e27ef] | 183 | assert(CPU != NULL);
|
---|
[a35b458] | 184 |
|
---|
[ec8ef12] | 185 | if (atomic_load(&CPU->nrdy) == 0)
|
---|
| 186 | return NULL;
|
---|
[a35b458] | 187 |
|
---|
[ec8ef12] | 188 | for (int i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 189 | irq_spinlock_lock(&(CPU->rq[i].lock), false);
|
---|
| 190 | if (CPU->rq[i].n == 0) {
|
---|
[f761f1eb] | 191 | /*
|
---|
| 192 | * If this queue is empty, try a lower-priority queue.
|
---|
| 193 | */
|
---|
[da1bafb] | 194 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
[f761f1eb] | 195 | continue;
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[248fc1a] | 198 | atomic_dec(&CPU->nrdy);
|
---|
[59e07c91] | 199 | atomic_dec(&nrdy);
|
---|
[da1bafb] | 200 | CPU->rq[i].n--;
|
---|
[a35b458] | 201 |
|
---|
[f761f1eb] | 202 | /*
|
---|
| 203 | * Take the first thread from the queue.
|
---|
| 204 | */
|
---|
[55b77d9] | 205 | thread_t *thread = list_get_instance(
|
---|
| 206 | list_first(&CPU->rq[i].rq), thread_t, rq_link);
|
---|
[da1bafb] | 207 | list_remove(&thread->rq_link);
|
---|
[a35b458] | 208 |
|
---|
[da1bafb] | 209 | irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
|
---|
[a35b458] | 210 |
|
---|
[da1bafb] | 211 | thread->cpu = CPU;
|
---|
| 212 | thread->priority = i; /* Correct rq index */
|
---|
[a35b458] | 213 |
|
---|
[aae2869] | 214 | /* Time allocation in microseconds. */
|
---|
| 215 | uint64_t time_to_run = (i + 1) * 10000;
|
---|
| 216 |
|
---|
| 217 | /* This is safe because interrupts are disabled. */
|
---|
[4760793] | 218 | CPU_LOCAL->preempt_deadline =
|
---|
| 219 | CPU_LOCAL->current_clock_tick + us2ticks(time_to_run);
|
---|
[aae2869] | 220 |
|
---|
[f761f1eb] | 221 | /*
|
---|
[6eef3c4] | 222 | * Clear the stolen flag so that it can be migrated
|
---|
[32fffef0] | 223 | * when load balancing needs emerge.
|
---|
[f761f1eb] | 224 | */
|
---|
[6eef3c4] | 225 | thread->stolen = false;
|
---|
[da1bafb] | 226 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[a35b458] | 227 |
|
---|
[117ad5a2] | 228 | *rq_index = i;
|
---|
[da1bafb] | 229 | return thread;
|
---|
[f761f1eb] | 230 | }
|
---|
[a35b458] | 231 |
|
---|
[ec8ef12] | 232 | return NULL;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Get thread to be scheduled
|
---|
| 236 | *
|
---|
| 237 | * Get the optimal thread to be scheduled
|
---|
| 238 | * according to thread accounting and scheduler
|
---|
| 239 | * policy.
|
---|
| 240 | *
|
---|
| 241 | * @return Thread to be scheduled.
|
---|
| 242 | *
|
---|
| 243 | */
|
---|
| 244 | static thread_t *find_best_thread(int *rq_index)
|
---|
| 245 | {
|
---|
| 246 | assert(interrupts_disabled());
|
---|
| 247 | assert(CPU != NULL);
|
---|
| 248 |
|
---|
| 249 | while (true) {
|
---|
| 250 | thread_t *thread = try_find_thread(rq_index);
|
---|
| 251 |
|
---|
| 252 | if (thread != NULL)
|
---|
| 253 | return thread;
|
---|
| 254 |
|
---|
| 255 | /*
|
---|
| 256 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 257 | * until a hardware interrupt or an IPI comes.
|
---|
| 258 | * This improves energy saving and hyperthreading.
|
---|
| 259 | */
|
---|
[4760793] | 260 | CPU_LOCAL->idle = true;
|
---|
[ec8ef12] | 261 |
|
---|
| 262 | /*
|
---|
| 263 | * Go to sleep with interrupts enabled.
|
---|
| 264 | * Ideally, this should be atomic, but this is not guaranteed on
|
---|
| 265 | * all platforms yet, so it is possible we will go sleep when
|
---|
| 266 | * a thread has just become available.
|
---|
| 267 | */
|
---|
| 268 | cpu_interruptible_sleep();
|
---|
| 269 | }
|
---|
[f761f1eb] | 270 | }
|
---|
| 271 |
|
---|
[c680333] | 272 | static void switch_task(task_t *task)
|
---|
| 273 | {
|
---|
| 274 | /* If the task stays the same, a lot of work is avoided. */
|
---|
| 275 | if (TASK == task)
|
---|
| 276 | return;
|
---|
| 277 |
|
---|
| 278 | as_t *old_as = AS;
|
---|
| 279 | as_t *new_as = task->as;
|
---|
| 280 |
|
---|
| 281 | /* It is possible for two tasks to share one address space. */
|
---|
| 282 | if (old_as != new_as)
|
---|
| 283 | as_switch(old_as, new_as);
|
---|
| 284 |
|
---|
| 285 | if (TASK)
|
---|
| 286 | task_release(TASK);
|
---|
| 287 |
|
---|
| 288 | TASK = task;
|
---|
| 289 |
|
---|
| 290 | task_hold(TASK);
|
---|
| 291 |
|
---|
| 292 | before_task_runs_arch();
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[70527f1] | 295 | /** Prevent rq starvation
|
---|
| 296 | *
|
---|
| 297 | * Prevent low priority threads from starving in rq's.
|
---|
| 298 | *
|
---|
| 299 | * When the function decides to relink rq's, it reconnects
|
---|
| 300 | * respective pointers so that in result threads with 'pri'
|
---|
[abbc16e] | 301 | * greater or equal start are moved to a higher-priority queue.
|
---|
[70527f1] | 302 | *
|
---|
| 303 | * @param start Threshold priority.
|
---|
| 304 | *
|
---|
[f761f1eb] | 305 | */
|
---|
[e16e036a] | 306 | static void relink_rq(int start)
|
---|
[f761f1eb] | 307 | {
|
---|
[4760793] | 308 | if (CPU_LOCAL->current_clock_tick < CPU_LOCAL->relink_deadline)
|
---|
[011c79a] | 309 | return;
|
---|
| 310 |
|
---|
[4760793] | 311 | CPU_LOCAL->relink_deadline = CPU_LOCAL->current_clock_tick + NEEDS_RELINK_MAX;
|
---|
[a35b458] | 312 |
|
---|
[3118355] | 313 | /* Temporary cache for lists we are moving. */
|
---|
[011c79a] | 314 | list_t list;
|
---|
[55b77d9] | 315 | list_initialize(&list);
|
---|
[a35b458] | 316 |
|
---|
[3118355] | 317 | size_t n = 0;
|
---|
| 318 |
|
---|
| 319 | /* Move every list (except the one with highest priority) one level up. */
|
---|
| 320 | for (int i = RQ_COUNT - 1; i > start; i--) {
|
---|
| 321 | irq_spinlock_lock(&CPU->rq[i].lock, false);
|
---|
[a35b458] | 322 |
|
---|
[3118355] | 323 | /* Swap lists. */
|
---|
| 324 | list_swap(&CPU->rq[i].rq, &list);
|
---|
[a35b458] | 325 |
|
---|
[3118355] | 326 | /* Swap number of items. */
|
---|
| 327 | size_t tmpn = CPU->rq[i].n;
|
---|
| 328 | CPU->rq[i].n = n;
|
---|
| 329 | n = tmpn;
|
---|
[a35b458] | 330 |
|
---|
[011c79a] | 331 | irq_spinlock_unlock(&CPU->rq[i].lock, false);
|
---|
[f761f1eb] | 332 | }
|
---|
[a35b458] | 333 |
|
---|
[3118355] | 334 | /* Append the contents of rq[start + 1] to rq[start]. */
|
---|
| 335 | if (n != 0) {
|
---|
| 336 | irq_spinlock_lock(&CPU->rq[start].lock, false);
|
---|
| 337 | list_concat(&CPU->rq[start].rq, &list);
|
---|
| 338 | CPU->rq[start].n += n;
|
---|
| 339 | irq_spinlock_unlock(&CPU->rq[start].lock, false);
|
---|
| 340 | }
|
---|
[f761f1eb] | 341 | }
|
---|
| 342 |
|
---|
[111b9b9] | 343 | void scheduler(void)
|
---|
| 344 | {
|
---|
| 345 | ipl_t ipl = interrupts_disable();
|
---|
| 346 |
|
---|
| 347 | if (atomic_load(&haltstate))
|
---|
| 348 | halt();
|
---|
| 349 |
|
---|
| 350 | if (THREAD) {
|
---|
| 351 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | scheduler_locked(ipl);
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[7d6ec87] | 357 | /** The scheduler
|
---|
| 358 | *
|
---|
| 359 | * The thread scheduling procedure.
|
---|
| 360 | * Passes control directly to
|
---|
| 361 | * scheduler_separated_stack().
|
---|
| 362 | *
|
---|
| 363 | */
|
---|
[111b9b9] | 364 | void scheduler_locked(ipl_t ipl)
|
---|
[7d6ec87] | 365 | {
|
---|
[63e27ef] | 366 | assert(CPU != NULL);
|
---|
[a35b458] | 367 |
|
---|
[7d6ec87] | 368 | if (THREAD) {
|
---|
[1ba37fa] | 369 | /* Update thread kernel accounting */
|
---|
[a2a00e8] | 370 | THREAD->kcycles += get_cycle() - THREAD->last_cycle;
|
---|
[a35b458] | 371 |
|
---|
[e1326cf] | 372 | #if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
|
---|
[0366d09d] | 373 | fpu_context_save(&THREAD->fpu_context);
|
---|
[f76fed4] | 374 | #endif
|
---|
[7d6ec87] | 375 | if (!context_save(&THREAD->saved_context)) {
|
---|
| 376 | /*
|
---|
| 377 | * This is the place where threads leave scheduler();
|
---|
| 378 | */
|
---|
[a35b458] | 379 |
|
---|
[cce6acf] | 380 | /* Save current CPU cycle */
|
---|
| 381 | THREAD->last_cycle = get_cycle();
|
---|
[a35b458] | 382 |
|
---|
[da1bafb] | 383 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[c030818] | 384 | interrupts_restore(THREAD->saved_ipl);
|
---|
[a35b458] | 385 |
|
---|
[7d6ec87] | 386 | return;
|
---|
| 387 | }
|
---|
[a35b458] | 388 |
|
---|
[7d6ec87] | 389 | /*
|
---|
[4e33b6b] | 390 | * Interrupt priority level of preempted thread is recorded
|
---|
| 391 | * here to facilitate scheduler() invocations from
|
---|
[da1bafb] | 392 | * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
|
---|
| 393 | *
|
---|
[7d6ec87] | 394 | */
|
---|
[c030818] | 395 | THREAD->saved_ipl = ipl;
|
---|
[7d6ec87] | 396 | }
|
---|
[a35b458] | 397 |
|
---|
[7d6ec87] | 398 | /*
|
---|
[a6e55886] | 399 | * Through the 'CURRENT' structure, we keep track of THREAD, TASK, CPU, AS
|
---|
| 400 | * and preemption counter. At this point CURRENT could be coming either
|
---|
[7d6ec87] | 401 | * from THREAD's or CPU's stack.
|
---|
[da1bafb] | 402 | *
|
---|
[7d6ec87] | 403 | */
|
---|
[4760793] | 404 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
[a35b458] | 405 |
|
---|
[7d6ec87] | 406 | /*
|
---|
| 407 | * We may not keep the old stack.
|
---|
| 408 | * Reason: If we kept the old stack and got blocked, for instance, in
|
---|
| 409 | * find_best_thread(), the old thread could get rescheduled by another
|
---|
| 410 | * CPU and overwrite the part of its own stack that was also used by
|
---|
| 411 | * the scheduler on this CPU.
|
---|
| 412 | *
|
---|
| 413 | * Moreover, we have to bypass the compiler-generated POP sequence
|
---|
| 414 | * which is fooled by SP being set to the very top of the stack.
|
---|
| 415 | * Therefore the scheduler() function continues in
|
---|
| 416 | * scheduler_separated_stack().
|
---|
[da1bafb] | 417 | *
|
---|
[7d6ec87] | 418 | */
|
---|
[daadfa6] | 419 | context_t ctx;
|
---|
| 420 | context_save(&ctx);
|
---|
| 421 | context_set(&ctx, FADDR(scheduler_separated_stack),
|
---|
[4760793] | 422 | (uintptr_t) CPU_LOCAL->stack, STACK_SIZE);
|
---|
[daadfa6] | 423 | context_restore(&ctx);
|
---|
[a35b458] | 424 |
|
---|
[da1bafb] | 425 | /* Not reached */
|
---|
[7d6ec87] | 426 | }
|
---|
[70527f1] | 427 |
|
---|
| 428 | /** Scheduler stack switch wrapper
|
---|
| 429 | *
|
---|
| 430 | * Second part of the scheduler() function
|
---|
| 431 | * using new stack. Handling the actual context
|
---|
| 432 | * switch to a new thread.
|
---|
| 433 | *
|
---|
| 434 | */
|
---|
[7d6ec87] | 435 | void scheduler_separated_stack(void)
|
---|
[f761f1eb] | 436 | {
|
---|
[63e27ef] | 437 | assert((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
|
---|
| 438 | assert(CPU != NULL);
|
---|
| 439 | assert(interrupts_disabled());
|
---|
[a35b458] | 440 |
|
---|
[43114c5] | 441 | if (THREAD) {
|
---|
[da1bafb] | 442 | /* Must be run after the switch to scheduler stack */
|
---|
[97f1691] | 443 | after_thread_ran();
|
---|
[a35b458] | 444 |
|
---|
[5663872] | 445 | int expected;
|
---|
| 446 |
|
---|
[43114c5] | 447 | switch (THREAD->state) {
|
---|
[06e1e95] | 448 | case Running:
|
---|
[da1bafb] | 449 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[76cec1e] | 450 | thread_ready(THREAD);
|
---|
| 451 | break;
|
---|
[a35b458] | 452 |
|
---|
[06e1e95] | 453 | case Exiting:
|
---|
[1871118] | 454 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[111b9b9] | 455 | waitq_close(&THREAD->join_wq);
|
---|
[1871118] | 456 |
|
---|
| 457 | /*
|
---|
| 458 | * Release the reference CPU has for the thread.
|
---|
| 459 | * If there are no other references (e.g. threads calling join),
|
---|
| 460 | * the thread structure is deallocated.
|
---|
| 461 | */
|
---|
| 462 | thread_put(THREAD);
|
---|
[76cec1e] | 463 | break;
|
---|
[a35b458] | 464 |
|
---|
[06e1e95] | 465 | case Sleeping:
|
---|
[5663872] | 466 | expected = SLEEP_INITIAL;
|
---|
| 467 |
|
---|
| 468 | /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
|
---|
| 469 | if (atomic_compare_exchange_strong_explicit(&THREAD->sleep_state,
|
---|
| 470 | &expected, SLEEP_ASLEEP,
|
---|
| 471 | memory_order_acq_rel, memory_order_acquire)) {
|
---|
| 472 |
|
---|
| 473 | /* Prefer the thread after it's woken up. */
|
---|
| 474 | THREAD->priority = -1;
|
---|
| 475 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 476 | } else {
|
---|
| 477 | assert(expected == SLEEP_WOKE);
|
---|
| 478 | /* The thread has already been woken up, requeue immediately. */
|
---|
| 479 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 480 | thread_ready(THREAD);
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[76cec1e] | 483 | break;
|
---|
[a35b458] | 484 |
|
---|
[06e1e95] | 485 | default:
|
---|
[76cec1e] | 486 | /*
|
---|
| 487 | * Entering state is unexpected.
|
---|
| 488 | */
|
---|
[f651e80] | 489 | panic("tid%" PRIu64 ": unexpected state %s.",
|
---|
[1e9d0e3] | 490 | THREAD->tid, thread_states[THREAD->state]);
|
---|
[76cec1e] | 491 | break;
|
---|
[f761f1eb] | 492 | }
|
---|
[a35b458] | 493 |
|
---|
[43114c5] | 494 | THREAD = NULL;
|
---|
[f761f1eb] | 495 | }
|
---|
[a35b458] | 496 |
|
---|
[117ad5a2] | 497 | int rq_index;
|
---|
| 498 | THREAD = find_best_thread(&rq_index);
|
---|
[a35b458] | 499 |
|
---|
[117ad5a2] | 500 | relink_rq(rq_index);
|
---|
[a35b458] | 501 |
|
---|
[c680333] | 502 | switch_task(THREAD->task);
|
---|
[a35b458] | 503 |
|
---|
[da1bafb] | 504 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[43114c5] | 505 | THREAD->state = Running;
|
---|
[a35b458] | 506 |
|
---|
[f76fed4] | 507 | #ifdef SCHEDULER_VERBOSE
|
---|
[b2fa1204] | 508 | log(LF_OTHER, LVL_DEBUG,
|
---|
| 509 | "cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
|
---|
[077842c] | 510 | ", nrdy=%zu)", CPU->id, THREAD->tid, THREAD->priority,
|
---|
[036e97c] | 511 | THREAD->ticks, atomic_load(&CPU->nrdy));
|
---|
[da1bafb] | 512 | #endif
|
---|
[a35b458] | 513 |
|
---|
[97f1691] | 514 | /*
|
---|
| 515 | * Some architectures provide late kernel PA2KA(identity)
|
---|
| 516 | * mapping in a page fault handler. However, the page fault
|
---|
| 517 | * handler uses the kernel stack of the running thread and
|
---|
| 518 | * therefore cannot be used to map it. The kernel stack, if
|
---|
| 519 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
| 520 | * function must be executed before the switch to the new stack.
|
---|
| 521 | */
|
---|
| 522 | before_thread_runs();
|
---|
[a35b458] | 523 |
|
---|
[3e1607f] | 524 | /*
|
---|
[4e33b6b] | 525 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
|
---|
| 526 | * thread's stack.
|
---|
[3e1607f] | 527 | */
|
---|
[a6e55886] | 528 | current_copy(CURRENT, (current_t *) THREAD->kstack);
|
---|
[a35b458] | 529 |
|
---|
[43114c5] | 530 | context_restore(&THREAD->saved_context);
|
---|
[a35b458] | 531 |
|
---|
[da1bafb] | 532 | /* Not reached */
|
---|
[f761f1eb] | 533 | }
|
---|
| 534 |
|
---|
[5f85c91] | 535 | #ifdef CONFIG_SMP
|
---|
[fbaf6ac] | 536 |
|
---|
| 537 | static thread_t *steal_thread_from(cpu_t *old_cpu, int i)
|
---|
| 538 | {
|
---|
| 539 | runq_t *old_rq = &old_cpu->rq[i];
|
---|
| 540 | runq_t *new_rq = &CPU->rq[i];
|
---|
| 541 |
|
---|
[06f81c4] | 542 | ipl_t ipl = interrupts_disable();
|
---|
| 543 |
|
---|
| 544 | irq_spinlock_lock(&old_rq->lock, false);
|
---|
[fbaf6ac] | 545 |
|
---|
[f3dbe27] | 546 | /*
|
---|
| 547 | * If fpu_owner is any thread in the list, its store is seen here thanks to
|
---|
| 548 | * the runqueue lock.
|
---|
| 549 | */
|
---|
| 550 | thread_t *fpu_owner = atomic_load_explicit(&old_cpu->fpu_owner,
|
---|
| 551 | memory_order_relaxed);
|
---|
| 552 |
|
---|
[fbaf6ac] | 553 | /* Search rq from the back */
|
---|
| 554 | list_foreach_rev(old_rq->rq, rq_link, thread_t, thread) {
|
---|
| 555 |
|
---|
| 556 | irq_spinlock_lock(&thread->lock, false);
|
---|
| 557 |
|
---|
| 558 | /*
|
---|
| 559 | * Do not steal CPU-wired threads, threads
|
---|
| 560 | * already stolen, threads for which migration
|
---|
| 561 | * was temporarily disabled or threads whose
|
---|
| 562 | * FPU context is still in the CPU.
|
---|
| 563 | */
|
---|
[06f81c4] | 564 | if (thread->stolen || thread->nomigrate ||
|
---|
[f3dbe27] | 565 | thread == fpu_owner) {
|
---|
[fbaf6ac] | 566 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 567 | continue;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | thread->stolen = true;
|
---|
| 571 | thread->cpu = CPU;
|
---|
| 572 |
|
---|
| 573 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 574 |
|
---|
| 575 | /*
|
---|
| 576 | * Ready thread on local CPU
|
---|
| 577 | */
|
---|
| 578 |
|
---|
| 579 | #ifdef KCPULB_VERBOSE
|
---|
| 580 | log(LF_OTHER, LVL_DEBUG,
|
---|
| 581 | "kcpulb%u: TID %" PRIu64 " -> cpu%u, "
|
---|
| 582 | "nrdy=%ld, avg=%ld", CPU->id, thread->tid,
|
---|
| 583 | CPU->id, atomic_load(&CPU->nrdy),
|
---|
| 584 | atomic_load(&nrdy) / config.cpu_active);
|
---|
| 585 | #endif
|
---|
| 586 |
|
---|
| 587 | /* Remove thread from ready queue. */
|
---|
| 588 | old_rq->n--;
|
---|
| 589 | list_remove(&thread->rq_link);
|
---|
[06f81c4] | 590 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
[fbaf6ac] | 591 |
|
---|
| 592 | /* Append thread to local queue. */
|
---|
[06f81c4] | 593 | irq_spinlock_lock(&new_rq->lock, false);
|
---|
[fbaf6ac] | 594 | list_append(&thread->rq_link, &new_rq->rq);
|
---|
| 595 | new_rq->n++;
|
---|
[06f81c4] | 596 | irq_spinlock_unlock(&new_rq->lock, false);
|
---|
[fbaf6ac] | 597 |
|
---|
| 598 | atomic_dec(&old_cpu->nrdy);
|
---|
| 599 | atomic_inc(&CPU->nrdy);
|
---|
[06f81c4] | 600 | interrupts_restore(ipl);
|
---|
[fbaf6ac] | 601 | return thread;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
[06f81c4] | 604 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
| 605 | interrupts_restore(ipl);
|
---|
[fbaf6ac] | 606 | return NULL;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[70527f1] | 609 | /** Load balancing thread
|
---|
| 610 | *
|
---|
| 611 | * SMP load balancing thread, supervising thread supplies
|
---|
| 612 | * for the CPU it's wired to.
|
---|
| 613 | *
|
---|
| 614 | * @param arg Generic thread argument (unused).
|
---|
| 615 | *
|
---|
[f761f1eb] | 616 | */
|
---|
| 617 | void kcpulb(void *arg)
|
---|
| 618 | {
|
---|
[3cfe2b8] | 619 | size_t average;
|
---|
| 620 | size_t rdy;
|
---|
[a35b458] | 621 |
|
---|
[f761f1eb] | 622 | loop:
|
---|
| 623 | /*
|
---|
[3260ada] | 624 | * Work in 1s intervals.
|
---|
[f761f1eb] | 625 | */
|
---|
[3260ada] | 626 | thread_sleep(1);
|
---|
[a35b458] | 627 |
|
---|
[f761f1eb] | 628 | not_satisfied:
|
---|
| 629 | /*
|
---|
| 630 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 631 | * other CPU's. Note that situation can have changed between two
|
---|
| 632 | * passes. Each time get the most up to date counts.
|
---|
[da1bafb] | 633 | *
|
---|
[f761f1eb] | 634 | */
|
---|
[036e97c] | 635 | average = atomic_load(&nrdy) / config.cpu_active + 1;
|
---|
| 636 | rdy = atomic_load(&CPU->nrdy);
|
---|
[a35b458] | 637 |
|
---|
[da1bafb] | 638 | if (average <= rdy)
|
---|
[f761f1eb] | 639 | goto satisfied;
|
---|
[a35b458] | 640 |
|
---|
[3cfe2b8] | 641 | size_t count = average - rdy;
|
---|
[a35b458] | 642 |
|
---|
[f761f1eb] | 643 | /*
|
---|
[4e33b6b] | 644 | * Searching least priority queues on all CPU's first and most priority
|
---|
| 645 | * queues on all CPU's last.
|
---|
[f761f1eb] | 646 | */
|
---|
[da1bafb] | 647 | size_t acpu;
|
---|
| 648 | int rq;
|
---|
[a35b458] | 649 |
|
---|
[da1bafb] | 650 | for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
|
---|
| 651 | for (acpu = 0; acpu < config.cpu_active; acpu++) {
|
---|
[fbaf6ac] | 652 | cpu_t *cpu = &cpus[acpu];
|
---|
[a35b458] | 653 |
|
---|
[f761f1eb] | 654 | /*
|
---|
| 655 | * Not interested in ourselves.
|
---|
[4e33b6b] | 656 | * Doesn't require interrupt disabling for kcpulb has
|
---|
| 657 | * THREAD_FLAG_WIRED.
|
---|
[da1bafb] | 658 | *
|
---|
[f761f1eb] | 659 | */
|
---|
[43114c5] | 660 | if (CPU == cpu)
|
---|
[248fc1a] | 661 | continue;
|
---|
[a35b458] | 662 |
|
---|
[036e97c] | 663 | if (atomic_load(&cpu->nrdy) <= average)
|
---|
[248fc1a] | 664 | continue;
|
---|
[a35b458] | 665 |
|
---|
[fbaf6ac] | 666 | if (steal_thread_from(cpu, rq) && --count == 0)
|
---|
| 667 | goto satisfied;
|
---|
[f761f1eb] | 668 | }
|
---|
| 669 | }
|
---|
[a35b458] | 670 |
|
---|
[036e97c] | 671 | if (atomic_load(&CPU->nrdy)) {
|
---|
[f761f1eb] | 672 | /*
|
---|
| 673 | * Be a little bit light-weight and let migrated threads run.
|
---|
[da1bafb] | 674 | *
|
---|
[f761f1eb] | 675 | */
|
---|
| 676 | scheduler();
|
---|
[3260ada] | 677 | } else {
|
---|
[f761f1eb] | 678 | /*
|
---|
| 679 | * We failed to migrate a single thread.
|
---|
[3260ada] | 680 | * Give up this turn.
|
---|
[da1bafb] | 681 | *
|
---|
[f761f1eb] | 682 | */
|
---|
[3260ada] | 683 | goto loop;
|
---|
[f761f1eb] | 684 | }
|
---|
[a35b458] | 685 |
|
---|
[f761f1eb] | 686 | goto not_satisfied;
|
---|
[a35b458] | 687 |
|
---|
[f761f1eb] | 688 | satisfied:
|
---|
| 689 | goto loop;
|
---|
| 690 | }
|
---|
[5f85c91] | 691 | #endif /* CONFIG_SMP */
|
---|
[10e16a7] | 692 |
|
---|
[da1bafb] | 693 | /** Print information about threads & scheduler queues
|
---|
| 694 | *
|
---|
| 695 | */
|
---|
[10e16a7] | 696 | void sched_print_list(void)
|
---|
| 697 | {
|
---|
[da1bafb] | 698 | size_t cpu;
|
---|
[4184e76] | 699 | for (cpu = 0; cpu < config.cpu_count; cpu++) {
|
---|
[10e16a7] | 700 | if (!cpus[cpu].active)
|
---|
| 701 | continue;
|
---|
[a35b458] | 702 |
|
---|
[3b68542] | 703 | printf("cpu%u: address=%p, nrdy=%zu\n",
|
---|
| 704 | cpus[cpu].id, &cpus[cpu], atomic_load(&cpus[cpu].nrdy));
|
---|
[a35b458] | 705 |
|
---|
[da1bafb] | 706 | unsigned int i;
|
---|
[4e33b6b] | 707 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 708 | irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
|
---|
| 709 | if (cpus[cpu].rq[i].n == 0) {
|
---|
| 710 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 711 | continue;
|
---|
| 712 | }
|
---|
[a35b458] | 713 |
|
---|
[5b86d10] | 714 | printf("\trq[%u]: ", i);
|
---|
[feeac0d] | 715 | list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
|
---|
| 716 | thread) {
|
---|
[da1bafb] | 717 | printf("%" PRIu64 "(%s) ", thread->tid,
|
---|
| 718 | thread_states[thread->state]);
|
---|
[10e16a7] | 719 | }
|
---|
| 720 | printf("\n");
|
---|
[a35b458] | 721 |
|
---|
[da1bafb] | 722 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 723 | }
|
---|
| 724 | }
|
---|
| 725 | }
|
---|
[b45c443] | 726 |
|
---|
[cc73a8a1] | 727 | /** @}
|
---|
[b45c443] | 728 | */
|
---|