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