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