1 /* $OpenBSD: kern_sched.c,v 1.27 2012/07/10 18:20:37 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2007, 2008 Artur Grabowski <art@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 20 #include <sys/sched.h> 21 #include <sys/proc.h> 22 #include <sys/kthread.h> 23 #include <sys/systm.h> 24 #include <sys/resourcevar.h> 25 #include <sys/signalvar.h> 26 #include <sys/mutex.h> 27 28 #include <uvm/uvm_extern.h> 29 30 #include <sys/malloc.h> 31 32 33 void sched_kthreads_create(void *); 34 35 int sched_proc_to_cpu_cost(struct cpu_info *ci, struct proc *p); 36 struct proc *sched_steal_proc(struct cpu_info *); 37 38 /* 39 * To help choosing which cpu should run which process we keep track 40 * of cpus which are currently idle and which cpus have processes 41 * queued. 42 */ 43 struct cpuset sched_idle_cpus; 44 struct cpuset sched_queued_cpus; 45 struct cpuset sched_all_cpus; 46 47 /* 48 * Some general scheduler counters. 49 */ 50 uint64_t sched_nmigrations; /* Cpu migration counter */ 51 uint64_t sched_nomigrations; /* Cpu no migration counter */ 52 uint64_t sched_noidle; /* Times we didn't pick the idle task */ 53 uint64_t sched_stolen; /* Times we stole proc from other cpus */ 54 uint64_t sched_choose; /* Times we chose a cpu */ 55 uint64_t sched_wasidle; /* Times we came out of idle */ 56 57 /* 58 * A few notes about cpu_switchto that is implemented in MD code. 59 * 60 * cpu_switchto takes two arguments, the old proc and the proc 61 * it should switch to. The new proc will never be NULL, so we always have 62 * a saved state that we need to switch to. The old proc however can 63 * be NULL if the process is exiting. NULL for the old proc simply 64 * means "don't bother saving old state". 65 * 66 * cpu_switchto is supposed to atomically load the new state of the process 67 * including the pcb, pmap and setting curproc, the p_cpu pointer in the 68 * proc and p_stat to SONPROC. Atomically with respect to interrupts, other 69 * cpus in the system must not depend on this state being consistent. 70 * Therefore no locking is necessary in cpu_switchto other than blocking 71 * interrupts during the context switch. 72 */ 73 74 /* 75 * sched_init_cpu is called from main() for the boot cpu, then it's the 76 * responsibility of the MD code to call it for all other cpus. 77 */ 78 void 79 sched_init_cpu(struct cpu_info *ci) 80 { 81 struct schedstate_percpu *spc = &ci->ci_schedstate; 82 int i; 83 84 for (i = 0; i < SCHED_NQS; i++) 85 TAILQ_INIT(&spc->spc_qs[i]); 86 87 spc->spc_idleproc = NULL; 88 89 kthread_create_deferred(sched_kthreads_create, ci); 90 91 LIST_INIT(&spc->spc_deadproc); 92 93 /* 94 * Slight hack here until the cpuset code handles cpu_info 95 * structures. 96 */ 97 cpuset_init_cpu(ci); 98 cpuset_add(&sched_all_cpus, ci); 99 } 100 101 void 102 sched_kthreads_create(void *v) 103 { 104 struct cpu_info *ci = v; 105 struct schedstate_percpu *spc = &ci->ci_schedstate; 106 static int num; 107 108 if (kthread_create(sched_idle, ci, &spc->spc_idleproc, "idle%d", num)) 109 panic("fork idle"); 110 111 num++; 112 } 113 114 void 115 sched_idle(void *v) 116 { 117 struct schedstate_percpu *spc; 118 struct proc *p = curproc; 119 struct cpu_info *ci = v; 120 int s; 121 122 KERNEL_UNLOCK(); 123 124 spc = &ci->ci_schedstate; 125 126 /* 127 * First time we enter here, we're not supposed to idle, 128 * just go away for a while. 129 */ 130 SCHED_LOCK(s); 131 cpuset_add(&sched_idle_cpus, ci); 132 p->p_stat = SSLEEP; 133 p->p_cpu = ci; 134 atomic_setbits_int(&p->p_flag, P_CPUPEG); 135 mi_switch(); 136 cpuset_del(&sched_idle_cpus, ci); 137 SCHED_UNLOCK(s); 138 139 KASSERT(ci == curcpu()); 140 KASSERT(curproc == spc->spc_idleproc); 141 142 while (1) { 143 while (!curcpu_is_idle()) { 144 struct proc *dead; 145 146 SCHED_LOCK(s); 147 p->p_stat = SSLEEP; 148 mi_switch(); 149 SCHED_UNLOCK(s); 150 151 while ((dead = LIST_FIRST(&spc->spc_deadproc))) { 152 LIST_REMOVE(dead, p_hash); 153 exit2(dead); 154 } 155 } 156 157 splassert(IPL_NONE); 158 159 cpuset_add(&sched_idle_cpus, ci); 160 cpu_idle_enter(); 161 while (spc->spc_whichqs == 0) { 162 if (spc->spc_schedflags & SPCF_SHOULDHALT && 163 (spc->spc_schedflags & SPCF_HALTED) == 0) { 164 cpuset_del(&sched_idle_cpus, ci); 165 SCHED_LOCK(s); 166 atomic_setbits_int(&spc->spc_schedflags, 167 spc->spc_whichqs ? 0 : SPCF_HALTED); 168 SCHED_UNLOCK(s); 169 wakeup(spc); 170 } 171 cpu_idle_cycle(); 172 } 173 cpu_idle_leave(); 174 cpuset_del(&sched_idle_cpus, ci); 175 } 176 } 177 178 /* 179 * To free our address space we have to jump through a few hoops. 180 * The freeing is done by the reaper, but until we have one reaper 181 * per cpu, we have no way of putting this proc on the deadproc list 182 * and waking up the reaper without risking having our address space and 183 * stack torn from under us before we manage to switch to another proc. 184 * Therefore we have a per-cpu list of dead processes where we put this 185 * proc and have idle clean up that list and move it to the reaper list. 186 * All this will be unnecessary once we can bind the reaper this cpu 187 * and not risk having it switch to another in case it sleeps. 188 */ 189 void 190 sched_exit(struct proc *p) 191 { 192 struct schedstate_percpu *spc = &curcpu()->ci_schedstate; 193 struct timeval tv; 194 struct proc *idle; 195 int s; 196 197 microuptime(&tv); 198 timersub(&tv, &spc->spc_runtime, &tv); 199 timeradd(&p->p_rtime, &tv, &p->p_rtime); 200 201 LIST_INSERT_HEAD(&spc->spc_deadproc, p, p_hash); 202 203 /* This process no longer needs to hold the kernel lock. */ 204 KERNEL_UNLOCK(); 205 206 SCHED_LOCK(s); 207 idle = spc->spc_idleproc; 208 idle->p_stat = SRUN; 209 cpu_switchto(NULL, idle); 210 panic("cpu_switchto returned"); 211 } 212 213 /* 214 * Run queue management. 215 */ 216 void 217 sched_init_runqueues(void) 218 { 219 } 220 221 void 222 setrunqueue(struct proc *p) 223 { 224 struct schedstate_percpu *spc; 225 int queue = p->p_priority >> 2; 226 227 SCHED_ASSERT_LOCKED(); 228 spc = &p->p_cpu->ci_schedstate; 229 spc->spc_nrun++; 230 231 TAILQ_INSERT_TAIL(&spc->spc_qs[queue], p, p_runq); 232 spc->spc_whichqs |= (1 << queue); 233 cpuset_add(&sched_queued_cpus, p->p_cpu); 234 235 if (cpuset_isset(&sched_idle_cpus, p->p_cpu)) 236 cpu_unidle(p->p_cpu); 237 } 238 239 void 240 remrunqueue(struct proc *p) 241 { 242 struct schedstate_percpu *spc; 243 int queue = p->p_priority >> 2; 244 245 SCHED_ASSERT_LOCKED(); 246 spc = &p->p_cpu->ci_schedstate; 247 spc->spc_nrun--; 248 249 TAILQ_REMOVE(&spc->spc_qs[queue], p, p_runq); 250 if (TAILQ_EMPTY(&spc->spc_qs[queue])) { 251 spc->spc_whichqs &= ~(1 << queue); 252 if (spc->spc_whichqs == 0) 253 cpuset_del(&sched_queued_cpus, p->p_cpu); 254 } 255 } 256 257 struct proc * 258 sched_chooseproc(void) 259 { 260 struct schedstate_percpu *spc = &curcpu()->ci_schedstate; 261 struct proc *p; 262 int queue; 263 264 SCHED_ASSERT_LOCKED(); 265 266 if (spc->spc_schedflags & SPCF_SHOULDHALT) { 267 if (spc->spc_whichqs) { 268 for (queue = 0; queue < SCHED_NQS; queue++) { 269 TAILQ_FOREACH(p, &spc->spc_qs[queue], p_runq) { 270 remrunqueue(p); 271 p->p_cpu = sched_choosecpu(p); 272 setrunqueue(p); 273 } 274 } 275 } 276 p = spc->spc_idleproc; 277 KASSERT(p); 278 KASSERT(p->p_wchan == NULL); 279 p->p_stat = SRUN; 280 return (p); 281 } 282 283 again: 284 if (spc->spc_whichqs) { 285 queue = ffs(spc->spc_whichqs) - 1; 286 p = TAILQ_FIRST(&spc->spc_qs[queue]); 287 remrunqueue(p); 288 sched_noidle++; 289 KASSERT(p->p_stat == SRUN); 290 } else if ((p = sched_steal_proc(curcpu())) == NULL) { 291 p = spc->spc_idleproc; 292 if (p == NULL) { 293 int s; 294 /* 295 * We get here if someone decides to switch during 296 * boot before forking kthreads, bleh. 297 * This is kind of like a stupid idle loop. 298 */ 299 #ifdef MULTIPROCESSOR 300 __mp_unlock(&sched_lock); 301 #endif 302 spl0(); 303 delay(10); 304 SCHED_LOCK(s); 305 goto again; 306 } 307 KASSERT(p); 308 p->p_stat = SRUN; 309 } 310 311 KASSERT(p->p_wchan == NULL); 312 return (p); 313 } 314 315 struct cpu_info * 316 sched_choosecpu_fork(struct proc *parent, int flags) 317 { 318 struct cpu_info *choice = NULL; 319 fixpt_t load, best_load = ~0; 320 int run, best_run = INT_MAX; 321 struct cpu_info *ci; 322 struct cpuset set; 323 324 #if 0 325 /* 326 * XXX 327 * Don't do this until we have a painless way to move the cpu in exec. 328 * Preferably when nuking the old pmap and getting a new one on a 329 * new cpu. 330 */ 331 /* 332 * PPWAIT forks are simple. We know that the parent will not 333 * run until we exec and choose another cpu, so we just steal its 334 * cpu. 335 */ 336 if (flags & FORK_PPWAIT) 337 return (parent->p_cpu); 338 #endif 339 340 /* 341 * Look at all cpus that are currently idle and have nothing queued. 342 * If there are none, pick the one with least queued procs first, 343 * then the one with lowest load average. 344 */ 345 cpuset_complement(&set, &sched_queued_cpus, &sched_idle_cpus); 346 cpuset_intersection(&set, &set, &sched_all_cpus); 347 if (cpuset_first(&set) == NULL) 348 cpuset_copy(&set, &sched_all_cpus); 349 350 while ((ci = cpuset_first(&set)) != NULL) { 351 cpuset_del(&set, ci); 352 353 load = ci->ci_schedstate.spc_ldavg; 354 run = ci->ci_schedstate.spc_nrun; 355 356 if (choice == NULL || run < best_run || 357 (run == best_run &&load < best_load)) { 358 choice = ci; 359 best_load = load; 360 best_run = run; 361 } 362 } 363 364 return (choice); 365 } 366 367 struct cpu_info * 368 sched_choosecpu(struct proc *p) 369 { 370 struct cpu_info *choice = NULL; 371 int last_cost = INT_MAX; 372 struct cpu_info *ci; 373 struct cpuset set; 374 375 /* 376 * If pegged to a cpu, don't allow it to move. 377 */ 378 if (p->p_flag & P_CPUPEG) 379 return (p->p_cpu); 380 381 sched_choose++; 382 383 /* 384 * Look at all cpus that are currently idle and have nothing queued. 385 * If there are none, pick the cheapest of those. 386 * (idle + queued could mean that the cpu is handling an interrupt 387 * at this moment and haven't had time to leave idle yet). 388 */ 389 cpuset_complement(&set, &sched_queued_cpus, &sched_idle_cpus); 390 cpuset_intersection(&set, &set, &sched_all_cpus); 391 392 /* 393 * First, just check if our current cpu is in that set, if it is, 394 * this is simple. 395 * Also, our cpu might not be idle, but if it's the current cpu 396 * and it has nothing else queued and we're curproc, take it. 397 */ 398 if (cpuset_isset(&set, p->p_cpu) || 399 (p->p_cpu == curcpu() && p->p_cpu->ci_schedstate.spc_nrun == 0 && 400 curproc == p)) { 401 sched_wasidle++; 402 return (p->p_cpu); 403 } 404 405 if (cpuset_first(&set) == NULL) 406 cpuset_copy(&set, &sched_all_cpus); 407 408 while ((ci = cpuset_first(&set)) != NULL) { 409 int cost = sched_proc_to_cpu_cost(ci, p); 410 411 if (choice == NULL || cost < last_cost) { 412 choice = ci; 413 last_cost = cost; 414 } 415 cpuset_del(&set, ci); 416 } 417 418 if (p->p_cpu != choice) 419 sched_nmigrations++; 420 else 421 sched_nomigrations++; 422 423 return (choice); 424 } 425 426 /* 427 * Attempt to steal a proc from some cpu. 428 */ 429 struct proc * 430 sched_steal_proc(struct cpu_info *self) 431 { 432 struct schedstate_percpu *spc; 433 struct proc *best = NULL; 434 int bestcost = INT_MAX; 435 struct cpu_info *ci; 436 struct cpuset set; 437 438 KASSERT((self->ci_schedstate.spc_schedflags & SPCF_SHOULDHALT) == 0); 439 440 cpuset_copy(&set, &sched_queued_cpus); 441 442 while ((ci = cpuset_first(&set)) != NULL) { 443 struct proc *p; 444 int queue; 445 int cost; 446 447 cpuset_del(&set, ci); 448 449 spc = &ci->ci_schedstate; 450 451 queue = ffs(spc->spc_whichqs) - 1; 452 TAILQ_FOREACH(p, &spc->spc_qs[queue], p_runq) { 453 if (p->p_flag & P_CPUPEG) 454 continue; 455 456 cost = sched_proc_to_cpu_cost(self, p); 457 458 if (best == NULL || cost < bestcost) { 459 best = p; 460 bestcost = cost; 461 } 462 } 463 } 464 if (best == NULL) 465 return (NULL); 466 467 spc = &best->p_cpu->ci_schedstate; 468 remrunqueue(best); 469 best->p_cpu = self; 470 471 sched_stolen++; 472 473 return (best); 474 } 475 476 /* 477 * Base 2 logarithm of an int. returns 0 for 0 (yeye, I know). 478 */ 479 static int 480 log2(unsigned int i) 481 { 482 int ret = 0; 483 484 while (i >>= 1) 485 ret++; 486 487 return (ret); 488 } 489 490 /* 491 * Calculate the cost of moving the proc to this cpu. 492 * 493 * What we want is some guesstimate of how much "performance" it will 494 * cost us to move the proc here. Not just for caches and TLBs and NUMA 495 * memory, but also for the proc itself. A highly loaded cpu might not 496 * be the best candidate for this proc since it won't get run. 497 * 498 * Just total guesstimates for now. 499 */ 500 501 int sched_cost_load = 1; 502 int sched_cost_priority = 1; 503 int sched_cost_runnable = 3; 504 int sched_cost_resident = 1; 505 506 int 507 sched_proc_to_cpu_cost(struct cpu_info *ci, struct proc *p) 508 { 509 struct schedstate_percpu *spc; 510 int l2resident = 0; 511 int cost; 512 513 spc = &ci->ci_schedstate; 514 515 cost = 0; 516 517 /* 518 * First, account for the priority of the proc we want to move. 519 * More willing to move, the lower the priority of the destination 520 * and the higher the priority of the proc. 521 */ 522 if (!cpuset_isset(&sched_idle_cpus, ci)) { 523 cost += (p->p_priority - spc->spc_curpriority) * 524 sched_cost_priority; 525 cost += sched_cost_runnable; 526 } 527 if (cpuset_isset(&sched_queued_cpus, ci)) 528 cost += spc->spc_nrun * sched_cost_runnable; 529 530 /* 531 * Higher load on the destination means we don't want to go there. 532 */ 533 cost += ((sched_cost_load * spc->spc_ldavg) >> FSHIFT); 534 535 /* 536 * If the proc is on this cpu already, lower the cost by how much 537 * it has been running and an estimate of its footprint. 538 */ 539 if (p->p_cpu == ci && p->p_slptime == 0) { 540 l2resident = 541 log2(pmap_resident_count(p->p_vmspace->vm_map.pmap)); 542 cost -= l2resident * sched_cost_resident; 543 } 544 545 return (cost); 546 } 547 548 /* 549 * Peg a proc to a cpu. 550 */ 551 void 552 sched_peg_curproc(struct cpu_info *ci) 553 { 554 struct proc *p = curproc; 555 int s; 556 557 SCHED_LOCK(s); 558 p->p_priority = p->p_usrpri; 559 p->p_stat = SRUN; 560 p->p_cpu = ci; 561 atomic_setbits_int(&p->p_flag, P_CPUPEG); 562 setrunqueue(p); 563 p->p_ru.ru_nvcsw++; 564 mi_switch(); 565 SCHED_UNLOCK(s); 566 } 567 568 #ifdef MULTIPROCESSOR 569 570 void 571 sched_start_secondary_cpus(void) 572 { 573 CPU_INFO_ITERATOR cii; 574 struct cpu_info *ci; 575 576 CPU_INFO_FOREACH(cii, ci) { 577 struct schedstate_percpu *spc = &ci->ci_schedstate; 578 579 if (CPU_IS_PRIMARY(ci)) 580 continue; 581 cpuset_add(&sched_all_cpus, ci); 582 atomic_clearbits_int(&spc->spc_schedflags, 583 SPCF_SHOULDHALT | SPCF_HALTED); 584 } 585 } 586 587 void 588 sched_stop_secondary_cpus(void) 589 { 590 CPU_INFO_ITERATOR cii; 591 struct cpu_info *ci; 592 593 /* 594 * Make sure we stop the secondary CPUs. 595 */ 596 CPU_INFO_FOREACH(cii, ci) { 597 struct schedstate_percpu *spc = &ci->ci_schedstate; 598 599 if (CPU_IS_PRIMARY(ci)) 600 continue; 601 cpuset_del(&sched_all_cpus, ci); 602 atomic_setbits_int(&spc->spc_schedflags, SPCF_SHOULDHALT); 603 } 604 CPU_INFO_FOREACH(cii, ci) { 605 struct schedstate_percpu *spc = &ci->ci_schedstate; 606 struct sleep_state sls; 607 608 if (CPU_IS_PRIMARY(ci)) 609 continue; 610 while ((spc->spc_schedflags & SPCF_HALTED) == 0) { 611 sleep_setup(&sls, spc, PZERO, "schedstate"); 612 sleep_finish(&sls, 613 (spc->spc_schedflags & SPCF_HALTED) == 0); 614 } 615 } 616 } 617 618 #endif 619 620 /* 621 * Functions to manipulate cpu sets. 622 */ 623 struct cpu_info *cpuset_infos[MAXCPUS]; 624 static struct cpuset cpuset_all; 625 626 void 627 cpuset_init_cpu(struct cpu_info *ci) 628 { 629 cpuset_add(&cpuset_all, ci); 630 cpuset_infos[CPU_INFO_UNIT(ci)] = ci; 631 } 632 633 void 634 cpuset_clear(struct cpuset *cs) 635 { 636 memset(cs, 0, sizeof(*cs)); 637 } 638 639 void 640 cpuset_add(struct cpuset *cs, struct cpu_info *ci) 641 { 642 unsigned int num = CPU_INFO_UNIT(ci); 643 atomic_setbits_int(&cs->cs_set[num/32], (1 << (num % 32))); 644 } 645 646 void 647 cpuset_del(struct cpuset *cs, struct cpu_info *ci) 648 { 649 unsigned int num = CPU_INFO_UNIT(ci); 650 atomic_clearbits_int(&cs->cs_set[num/32], (1 << (num % 32))); 651 } 652 653 int 654 cpuset_isset(struct cpuset *cs, struct cpu_info *ci) 655 { 656 unsigned int num = CPU_INFO_UNIT(ci); 657 return (cs->cs_set[num/32] & (1 << (num % 32))); 658 } 659 660 void 661 cpuset_add_all(struct cpuset *cs) 662 { 663 cpuset_copy(cs, &cpuset_all); 664 } 665 666 void 667 cpuset_copy(struct cpuset *to, struct cpuset *from) 668 { 669 memcpy(to, from, sizeof(*to)); 670 } 671 672 struct cpu_info * 673 cpuset_first(struct cpuset *cs) 674 { 675 int i; 676 677 for (i = 0; i < CPUSET_ASIZE(ncpus); i++) 678 if (cs->cs_set[i]) 679 return (cpuset_infos[i * 32 + ffs(cs->cs_set[i]) - 1]); 680 681 return (NULL); 682 } 683 684 void 685 cpuset_union(struct cpuset *to, struct cpuset *a, struct cpuset *b) 686 { 687 int i; 688 689 for (i = 0; i < CPUSET_ASIZE(ncpus); i++) 690 to->cs_set[i] = a->cs_set[i] | b->cs_set[i]; 691 } 692 693 void 694 cpuset_intersection(struct cpuset *to, struct cpuset *a, struct cpuset *b) 695 { 696 int i; 697 698 for (i = 0; i < CPUSET_ASIZE(ncpus); i++) 699 to->cs_set[i] = a->cs_set[i] & b->cs_set[i]; 700 } 701 702 void 703 cpuset_complement(struct cpuset *to, struct cpuset *a, struct cpuset *b) 704 { 705 int i; 706 707 for (i = 0; i < CPUSET_ASIZE(ncpus); i++) 708 to->cs_set[i] = b->cs_set[i] & ~a->cs_set[i]; 709 } 710