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