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