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