1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 30 #pragma ident "%Z%%M% %I% %E% SMI" /* from SVr4.0 1.30 */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/sysmacros.h> 35 #include <sys/signal.h> 36 #include <sys/user.h> 37 #include <sys/systm.h> 38 #include <sys/sysinfo.h> 39 #include <sys/var.h> 40 #include <sys/errno.h> 41 #include <sys/cmn_err.h> 42 #include <sys/debug.h> 43 #include <sys/inline.h> 44 #include <sys/disp.h> 45 #include <sys/class.h> 46 #include <sys/bitmap.h> 47 #include <sys/kmem.h> 48 #include <sys/cpuvar.h> 49 #include <sys/vtrace.h> 50 #include <sys/tnf.h> 51 #include <sys/cpupart.h> 52 #include <sys/lgrp.h> 53 #include <sys/pg.h> 54 #include <sys/cmt.h> 55 #include <sys/bitset.h> 56 #include <sys/schedctl.h> 57 #include <sys/atomic.h> 58 #include <sys/dtrace.h> 59 #include <sys/sdt.h> 60 61 #include <vm/as.h> 62 63 #define BOUND_CPU 0x1 64 #define BOUND_PARTITION 0x2 65 #define BOUND_INTR 0x4 66 67 /* Dispatch queue allocation structure and functions */ 68 struct disp_queue_info { 69 disp_t *dp; 70 dispq_t *olddispq; 71 dispq_t *newdispq; 72 ulong_t *olddqactmap; 73 ulong_t *newdqactmap; 74 int oldnglobpris; 75 }; 76 static void disp_dq_alloc(struct disp_queue_info *dptr, int numpris, 77 disp_t *dp); 78 static void disp_dq_assign(struct disp_queue_info *dptr, int numpris); 79 static void disp_dq_free(struct disp_queue_info *dptr); 80 81 /* platform-specific routine to call when processor is idle */ 82 static void generic_idle_cpu(); 83 void (*idle_cpu)() = generic_idle_cpu; 84 85 /* routines invoked when a CPU enters/exits the idle loop */ 86 static void idle_enter(); 87 static void idle_exit(); 88 89 /* platform-specific routine to call when thread is enqueued */ 90 static void generic_enq_thread(cpu_t *, int); 91 void (*disp_enq_thread)(cpu_t *, int) = generic_enq_thread; 92 93 pri_t kpreemptpri; /* priority where kernel preemption applies */ 94 pri_t upreemptpri = 0; /* priority where normal preemption applies */ 95 pri_t intr_pri; /* interrupt thread priority base level */ 96 97 #define KPQPRI -1 /* pri where cpu affinity is dropped for kpq */ 98 pri_t kpqpri = KPQPRI; /* can be set in /etc/system */ 99 disp_t cpu0_disp; /* boot CPU's dispatch queue */ 100 disp_lock_t swapped_lock; /* lock swapped threads and swap queue */ 101 int nswapped; /* total number of swapped threads */ 102 void disp_swapped_enq(kthread_t *tp); 103 static void disp_swapped_setrun(kthread_t *tp); 104 static void cpu_resched(cpu_t *cp, pri_t tpri); 105 106 /* 107 * If this is set, only interrupt threads will cause kernel preemptions. 108 * This is done by changing the value of kpreemptpri. kpreemptpri 109 * will either be the max sysclass pri + 1 or the min interrupt pri. 110 */ 111 int only_intr_kpreempt; 112 113 extern void set_idle_cpu(int cpun); 114 extern void unset_idle_cpu(int cpun); 115 static void setkpdq(kthread_t *tp, int borf); 116 #define SETKP_BACK 0 117 #define SETKP_FRONT 1 118 /* 119 * Parameter that determines how recently a thread must have run 120 * on the CPU to be considered loosely-bound to that CPU to reduce 121 * cold cache effects. The interval is in hertz. 122 */ 123 #define RECHOOSE_INTERVAL 3 124 int rechoose_interval = RECHOOSE_INTERVAL; 125 static cpu_t *cpu_choose(kthread_t *, pri_t); 126 127 /* 128 * Parameter that determines how long (in nanoseconds) a thread must 129 * be sitting on a run queue before it can be stolen by another CPU 130 * to reduce migrations. The interval is in nanoseconds. 131 * 132 * The nosteal_nsec should be set by a platform code to an appropriate value. 133 * Setting it to 0 effectively disables the nosteal 'protection' 134 */ 135 hrtime_t nosteal_nsec = -1; 136 137 id_t defaultcid; /* system "default" class; see dispadmin(1M) */ 138 139 disp_lock_t transition_lock; /* lock on transitioning threads */ 140 disp_lock_t stop_lock; /* lock on stopped threads */ 141 142 static void cpu_dispqalloc(int numpris); 143 144 /* 145 * This gets returned by disp_getwork/disp_getbest if we couldn't steal 146 * a thread because it was sitting on its run queue for a very short 147 * period of time. 148 */ 149 #define T_DONTSTEAL (kthread_t *)(-1) /* returned by disp_getwork/getbest */ 150 151 static kthread_t *disp_getwork(cpu_t *to); 152 static kthread_t *disp_getbest(disp_t *from); 153 static kthread_t *disp_ratify(kthread_t *tp, disp_t *kpq); 154 155 void swtch_to(kthread_t *); 156 157 /* 158 * dispatcher and scheduler initialization 159 */ 160 161 /* 162 * disp_setup - Common code to calculate and allocate dispatcher 163 * variables and structures based on the maximum priority. 164 */ 165 static void 166 disp_setup(pri_t maxglobpri, pri_t oldnglobpris) 167 { 168 pri_t newnglobpris; 169 170 ASSERT(MUTEX_HELD(&cpu_lock)); 171 172 newnglobpris = maxglobpri + 1 + LOCK_LEVEL; 173 174 if (newnglobpris > oldnglobpris) { 175 /* 176 * Allocate new kp queues for each CPU partition. 177 */ 178 cpupart_kpqalloc(newnglobpris); 179 180 /* 181 * Allocate new dispatch queues for each CPU. 182 */ 183 cpu_dispqalloc(newnglobpris); 184 185 /* 186 * compute new interrupt thread base priority 187 */ 188 intr_pri = maxglobpri; 189 if (only_intr_kpreempt) { 190 kpreemptpri = intr_pri + 1; 191 if (kpqpri == KPQPRI) 192 kpqpri = kpreemptpri; 193 } 194 v.v_nglobpris = newnglobpris; 195 } 196 } 197 198 /* 199 * dispinit - Called to initialize all loaded classes and the 200 * dispatcher framework. 201 */ 202 void 203 dispinit(void) 204 { 205 id_t cid; 206 pri_t maxglobpri; 207 pri_t cl_maxglobpri; 208 209 maxglobpri = -1; 210 211 /* 212 * Initialize transition lock, which will always be set. 213 */ 214 DISP_LOCK_INIT(&transition_lock); 215 disp_lock_enter_high(&transition_lock); 216 DISP_LOCK_INIT(&stop_lock); 217 218 mutex_enter(&cpu_lock); 219 CPU->cpu_disp->disp_maxrunpri = -1; 220 CPU->cpu_disp->disp_max_unbound_pri = -1; 221 222 /* 223 * Initialize the default CPU partition. 224 */ 225 cpupart_initialize_default(); 226 /* 227 * Call the class specific initialization functions for 228 * all pre-installed schedulers. 229 * 230 * We pass the size of a class specific parameter 231 * buffer to each of the initialization functions 232 * to try to catch problems with backward compatibility 233 * of class modules. 234 * 235 * For example a new class module running on an old system 236 * which didn't provide sufficiently large parameter buffers 237 * would be bad news. Class initialization modules can check for 238 * this and take action if they detect a problem. 239 */ 240 241 for (cid = 0; cid < nclass; cid++) { 242 sclass_t *sc; 243 244 sc = &sclass[cid]; 245 if (SCHED_INSTALLED(sc)) { 246 cl_maxglobpri = sc->cl_init(cid, PC_CLPARMSZ, 247 &sc->cl_funcs); 248 if (cl_maxglobpri > maxglobpri) 249 maxglobpri = cl_maxglobpri; 250 } 251 } 252 kpreemptpri = (pri_t)v.v_maxsyspri + 1; 253 if (kpqpri == KPQPRI) 254 kpqpri = kpreemptpri; 255 256 ASSERT(maxglobpri >= 0); 257 disp_setup(maxglobpri, 0); 258 259 mutex_exit(&cpu_lock); 260 261 /* 262 * Get the default class ID; this may be later modified via 263 * dispadmin(1M). This will load the class (normally TS) and that will 264 * call disp_add(), which is why we had to drop cpu_lock first. 265 */ 266 if (getcid(defaultclass, &defaultcid) != 0) { 267 cmn_err(CE_PANIC, "Couldn't load default scheduling class '%s'", 268 defaultclass); 269 } 270 } 271 272 /* 273 * disp_add - Called with class pointer to initialize the dispatcher 274 * for a newly loaded class. 275 */ 276 void 277 disp_add(sclass_t *clp) 278 { 279 pri_t maxglobpri; 280 pri_t cl_maxglobpri; 281 282 mutex_enter(&cpu_lock); 283 /* 284 * Initialize the scheduler class. 285 */ 286 maxglobpri = (pri_t)(v.v_nglobpris - LOCK_LEVEL - 1); 287 cl_maxglobpri = clp->cl_init(clp - sclass, PC_CLPARMSZ, &clp->cl_funcs); 288 if (cl_maxglobpri > maxglobpri) 289 maxglobpri = cl_maxglobpri; 290 291 /* 292 * Save old queue information. Since we're initializing a 293 * new scheduling class which has just been loaded, then 294 * the size of the dispq may have changed. We need to handle 295 * that here. 296 */ 297 disp_setup(maxglobpri, v.v_nglobpris); 298 299 mutex_exit(&cpu_lock); 300 } 301 302 303 /* 304 * For each CPU, allocate new dispatch queues 305 * with the stated number of priorities. 306 */ 307 static void 308 cpu_dispqalloc(int numpris) 309 { 310 cpu_t *cpup; 311 struct disp_queue_info *disp_mem; 312 int i, num; 313 314 ASSERT(MUTEX_HELD(&cpu_lock)); 315 316 disp_mem = kmem_zalloc(NCPU * 317 sizeof (struct disp_queue_info), KM_SLEEP); 318 319 /* 320 * This routine must allocate all of the memory before stopping 321 * the cpus because it must not sleep in kmem_alloc while the 322 * CPUs are stopped. Locks they hold will not be freed until they 323 * are restarted. 324 */ 325 i = 0; 326 cpup = cpu_list; 327 do { 328 disp_dq_alloc(&disp_mem[i], numpris, cpup->cpu_disp); 329 i++; 330 cpup = cpup->cpu_next; 331 } while (cpup != cpu_list); 332 num = i; 333 334 pause_cpus(NULL); 335 for (i = 0; i < num; i++) 336 disp_dq_assign(&disp_mem[i], numpris); 337 start_cpus(); 338 339 /* 340 * I must free all of the memory after starting the cpus because 341 * I can not risk sleeping in kmem_free while the cpus are stopped. 342 */ 343 for (i = 0; i < num; i++) 344 disp_dq_free(&disp_mem[i]); 345 346 kmem_free(disp_mem, NCPU * sizeof (struct disp_queue_info)); 347 } 348 349 static void 350 disp_dq_alloc(struct disp_queue_info *dptr, int numpris, disp_t *dp) 351 { 352 dptr->newdispq = kmem_zalloc(numpris * sizeof (dispq_t), KM_SLEEP); 353 dptr->newdqactmap = kmem_zalloc(((numpris / BT_NBIPUL) + 1) * 354 sizeof (long), KM_SLEEP); 355 dptr->dp = dp; 356 } 357 358 static void 359 disp_dq_assign(struct disp_queue_info *dptr, int numpris) 360 { 361 disp_t *dp; 362 363 dp = dptr->dp; 364 dptr->olddispq = dp->disp_q; 365 dptr->olddqactmap = dp->disp_qactmap; 366 dptr->oldnglobpris = dp->disp_npri; 367 368 ASSERT(dptr->oldnglobpris < numpris); 369 370 if (dptr->olddispq != NULL) { 371 /* 372 * Use kcopy because bcopy is platform-specific 373 * and could block while we might have paused the cpus. 374 */ 375 (void) kcopy(dptr->olddispq, dptr->newdispq, 376 dptr->oldnglobpris * sizeof (dispq_t)); 377 (void) kcopy(dptr->olddqactmap, dptr->newdqactmap, 378 ((dptr->oldnglobpris / BT_NBIPUL) + 1) * 379 sizeof (long)); 380 } 381 dp->disp_q = dptr->newdispq; 382 dp->disp_qactmap = dptr->newdqactmap; 383 dp->disp_q_limit = &dptr->newdispq[numpris]; 384 dp->disp_npri = numpris; 385 } 386 387 static void 388 disp_dq_free(struct disp_queue_info *dptr) 389 { 390 if (dptr->olddispq != NULL) 391 kmem_free(dptr->olddispq, 392 dptr->oldnglobpris * sizeof (dispq_t)); 393 if (dptr->olddqactmap != NULL) 394 kmem_free(dptr->olddqactmap, 395 ((dptr->oldnglobpris / BT_NBIPUL) + 1) * sizeof (long)); 396 } 397 398 /* 399 * For a newly created CPU, initialize the dispatch queue. 400 * This is called before the CPU is known through cpu[] or on any lists. 401 */ 402 void 403 disp_cpu_init(cpu_t *cp) 404 { 405 disp_t *dp; 406 dispq_t *newdispq; 407 ulong_t *newdqactmap; 408 409 ASSERT(MUTEX_HELD(&cpu_lock)); /* protect dispatcher queue sizes */ 410 411 if (cp == cpu0_disp.disp_cpu) 412 dp = &cpu0_disp; 413 else 414 dp = kmem_alloc(sizeof (disp_t), KM_SLEEP); 415 bzero(dp, sizeof (disp_t)); 416 cp->cpu_disp = dp; 417 dp->disp_cpu = cp; 418 dp->disp_maxrunpri = -1; 419 dp->disp_max_unbound_pri = -1; 420 DISP_LOCK_INIT(&cp->cpu_thread_lock); 421 /* 422 * Allocate memory for the dispatcher queue headers 423 * and the active queue bitmap. 424 */ 425 newdispq = kmem_zalloc(v.v_nglobpris * sizeof (dispq_t), KM_SLEEP); 426 newdqactmap = kmem_zalloc(((v.v_nglobpris / BT_NBIPUL) + 1) * 427 sizeof (long), KM_SLEEP); 428 dp->disp_q = newdispq; 429 dp->disp_qactmap = newdqactmap; 430 dp->disp_q_limit = &newdispq[v.v_nglobpris]; 431 dp->disp_npri = v.v_nglobpris; 432 } 433 434 void 435 disp_cpu_fini(cpu_t *cp) 436 { 437 ASSERT(MUTEX_HELD(&cpu_lock)); 438 439 disp_kp_free(cp->cpu_disp); 440 if (cp->cpu_disp != &cpu0_disp) 441 kmem_free(cp->cpu_disp, sizeof (disp_t)); 442 } 443 444 /* 445 * Allocate new, larger kpreempt dispatch queue to replace the old one. 446 */ 447 void 448 disp_kp_alloc(disp_t *dq, pri_t npri) 449 { 450 struct disp_queue_info mem_info; 451 452 if (npri > dq->disp_npri) { 453 /* 454 * Allocate memory for the new array. 455 */ 456 disp_dq_alloc(&mem_info, npri, dq); 457 458 /* 459 * We need to copy the old structures to the new 460 * and free the old. 461 */ 462 disp_dq_assign(&mem_info, npri); 463 disp_dq_free(&mem_info); 464 } 465 } 466 467 /* 468 * Free dispatch queue. 469 * Used for the kpreempt queues for a removed CPU partition and 470 * for the per-CPU queues of deleted CPUs. 471 */ 472 void 473 disp_kp_free(disp_t *dq) 474 { 475 struct disp_queue_info mem_info; 476 477 mem_info.olddispq = dq->disp_q; 478 mem_info.olddqactmap = dq->disp_qactmap; 479 mem_info.oldnglobpris = dq->disp_npri; 480 disp_dq_free(&mem_info); 481 } 482 483 /* 484 * End dispatcher and scheduler initialization. 485 */ 486 487 /* 488 * See if there's anything to do other than remain idle. 489 * Return non-zero if there is. 490 * 491 * This function must be called with high spl, or with 492 * kernel preemption disabled to prevent the partition's 493 * active cpu list from changing while being traversed. 494 * 495 */ 496 int 497 disp_anywork(void) 498 { 499 cpu_t *cp = CPU; 500 cpu_t *ocp; 501 502 if (cp->cpu_disp->disp_nrunnable != 0) 503 return (1); 504 505 if (!(cp->cpu_flags & CPU_OFFLINE)) { 506 if (CP_MAXRUNPRI(cp->cpu_part) >= 0) 507 return (1); 508 509 /* 510 * Work can be taken from another CPU if: 511 * - There is unbound work on the run queue 512 * - That work isn't a thread undergoing a 513 * - context switch on an otherwise empty queue. 514 * - The CPU isn't running the idle loop. 515 */ 516 for (ocp = cp->cpu_next_part; ocp != cp; 517 ocp = ocp->cpu_next_part) { 518 ASSERT(CPU_ACTIVE(ocp)); 519 520 if (ocp->cpu_disp->disp_max_unbound_pri != -1 && 521 !((ocp->cpu_disp_flags & CPU_DISP_DONTSTEAL) && 522 ocp->cpu_disp->disp_nrunnable == 1) && 523 ocp->cpu_dispatch_pri != -1) 524 return (1); 525 } 526 } 527 return (0); 528 } 529 530 /* 531 * Called when CPU enters the idle loop 532 */ 533 static void 534 idle_enter() 535 { 536 cpu_t *cp = CPU; 537 538 new_cpu_mstate(CMS_IDLE, gethrtime_unscaled()); 539 CPU_STATS_ADDQ(cp, sys, idlethread, 1); 540 set_idle_cpu(cp->cpu_id); /* arch-dependent hook */ 541 } 542 543 /* 544 * Called when CPU exits the idle loop 545 */ 546 static void 547 idle_exit() 548 { 549 cpu_t *cp = CPU; 550 551 new_cpu_mstate(CMS_SYSTEM, gethrtime_unscaled()); 552 unset_idle_cpu(cp->cpu_id); /* arch-dependent hook */ 553 } 554 555 /* 556 * Idle loop. 557 */ 558 void 559 idle() 560 { 561 struct cpu *cp = CPU; /* pointer to this CPU */ 562 kthread_t *t; /* taken thread */ 563 564 idle_enter(); 565 566 /* 567 * Uniprocessor version of idle loop. 568 * Do this until notified that we're on an actual multiprocessor. 569 */ 570 while (ncpus == 1) { 571 if (cp->cpu_disp->disp_nrunnable == 0) { 572 (*idle_cpu)(); 573 continue; 574 } 575 idle_exit(); 576 swtch(); 577 578 idle_enter(); /* returned from swtch */ 579 } 580 581 /* 582 * Multiprocessor idle loop. 583 */ 584 for (;;) { 585 /* 586 * If CPU is completely quiesced by p_online(2), just wait 587 * here with minimal bus traffic until put online. 588 */ 589 while (cp->cpu_flags & CPU_QUIESCED) 590 (*idle_cpu)(); 591 592 if (cp->cpu_disp->disp_nrunnable != 0) { 593 idle_exit(); 594 swtch(); 595 } else { 596 if (cp->cpu_flags & CPU_OFFLINE) 597 continue; 598 if ((t = disp_getwork(cp)) == NULL) { 599 if (cp->cpu_chosen_level != -1) { 600 disp_t *dp = cp->cpu_disp; 601 disp_t *kpq; 602 603 disp_lock_enter(&dp->disp_lock); 604 /* 605 * Set kpq under lock to prevent 606 * migration between partitions. 607 */ 608 kpq = &cp->cpu_part->cp_kp_queue; 609 if (kpq->disp_maxrunpri == -1) 610 cp->cpu_chosen_level = -1; 611 disp_lock_exit(&dp->disp_lock); 612 } 613 (*idle_cpu)(); 614 continue; 615 } 616 /* 617 * If there was a thread but we couldn't steal 618 * it, then keep trying. 619 */ 620 if (t == T_DONTSTEAL) 621 continue; 622 idle_exit(); 623 swtch_to(t); 624 } 625 idle_enter(); /* returned from swtch/swtch_to */ 626 } 627 } 628 629 630 /* 631 * Preempt the currently running thread in favor of the highest 632 * priority thread. The class of the current thread controls 633 * where it goes on the dispatcher queues. If panicking, turn 634 * preemption off. 635 */ 636 void 637 preempt() 638 { 639 kthread_t *t = curthread; 640 klwp_t *lwp = ttolwp(curthread); 641 642 if (panicstr) 643 return; 644 645 TRACE_0(TR_FAC_DISP, TR_PREEMPT_START, "preempt_start"); 646 647 thread_lock(t); 648 649 if (t->t_state != TS_ONPROC || t->t_disp_queue != CPU->cpu_disp) { 650 /* 651 * this thread has already been chosen to be run on 652 * another CPU. Clear kprunrun on this CPU since we're 653 * already headed for swtch(). 654 */ 655 CPU->cpu_kprunrun = 0; 656 thread_unlock_nopreempt(t); 657 TRACE_0(TR_FAC_DISP, TR_PREEMPT_END, "preempt_end"); 658 } else { 659 if (lwp != NULL) 660 lwp->lwp_ru.nivcsw++; 661 CPU_STATS_ADDQ(CPU, sys, inv_swtch, 1); 662 THREAD_TRANSITION(t); 663 CL_PREEMPT(t); 664 DTRACE_SCHED(preempt); 665 thread_unlock_nopreempt(t); 666 667 TRACE_0(TR_FAC_DISP, TR_PREEMPT_END, "preempt_end"); 668 669 swtch(); /* clears CPU->cpu_runrun via disp() */ 670 } 671 } 672 673 extern kthread_t *thread_unpin(); 674 675 /* 676 * disp() - find the highest priority thread for this processor to run, and 677 * set it in TS_ONPROC state so that resume() can be called to run it. 678 */ 679 static kthread_t * 680 disp() 681 { 682 cpu_t *cpup; 683 disp_t *dp; 684 kthread_t *tp; 685 dispq_t *dq; 686 int maxrunword; 687 pri_t pri; 688 disp_t *kpq; 689 690 TRACE_0(TR_FAC_DISP, TR_DISP_START, "disp_start"); 691 692 cpup = CPU; 693 /* 694 * Find the highest priority loaded, runnable thread. 695 */ 696 dp = cpup->cpu_disp; 697 698 reschedule: 699 /* 700 * If there is more important work on the global queue with a better 701 * priority than the maximum on this CPU, take it now. 702 */ 703 kpq = &cpup->cpu_part->cp_kp_queue; 704 while ((pri = kpq->disp_maxrunpri) >= 0 && 705 pri >= dp->disp_maxrunpri && 706 (cpup->cpu_flags & CPU_OFFLINE) == 0 && 707 (tp = disp_getbest(kpq)) != NULL) { 708 if (disp_ratify(tp, kpq) != NULL) { 709 TRACE_1(TR_FAC_DISP, TR_DISP_END, 710 "disp_end:tid %p", tp); 711 return (tp); 712 } 713 } 714 715 disp_lock_enter(&dp->disp_lock); 716 pri = dp->disp_maxrunpri; 717 718 /* 719 * If there is nothing to run, look at what's runnable on other queues. 720 * Choose the idle thread if the CPU is quiesced. 721 * Note that CPUs that have the CPU_OFFLINE flag set can still run 722 * interrupt threads, which will be the only threads on the CPU's own 723 * queue, but cannot run threads from other queues. 724 */ 725 if (pri == -1) { 726 if (!(cpup->cpu_flags & CPU_OFFLINE)) { 727 disp_lock_exit(&dp->disp_lock); 728 if ((tp = disp_getwork(cpup)) == NULL || 729 tp == T_DONTSTEAL) { 730 tp = cpup->cpu_idle_thread; 731 (void) splhigh(); 732 THREAD_ONPROC(tp, cpup); 733 cpup->cpu_dispthread = tp; 734 cpup->cpu_dispatch_pri = -1; 735 cpup->cpu_runrun = cpup->cpu_kprunrun = 0; 736 cpup->cpu_chosen_level = -1; 737 } 738 } else { 739 disp_lock_exit_high(&dp->disp_lock); 740 tp = cpup->cpu_idle_thread; 741 THREAD_ONPROC(tp, cpup); 742 cpup->cpu_dispthread = tp; 743 cpup->cpu_dispatch_pri = -1; 744 cpup->cpu_runrun = cpup->cpu_kprunrun = 0; 745 cpup->cpu_chosen_level = -1; 746 } 747 TRACE_1(TR_FAC_DISP, TR_DISP_END, 748 "disp_end:tid %p", tp); 749 return (tp); 750 } 751 752 dq = &dp->disp_q[pri]; 753 tp = dq->dq_first; 754 755 ASSERT(tp != NULL); 756 ASSERT(tp->t_schedflag & TS_LOAD); /* thread must be swapped in */ 757 758 DTRACE_SCHED2(dequeue, kthread_t *, tp, disp_t *, dp); 759 760 /* 761 * Found it so remove it from queue. 762 */ 763 dp->disp_nrunnable--; 764 dq->dq_sruncnt--; 765 if ((dq->dq_first = tp->t_link) == NULL) { 766 ulong_t *dqactmap = dp->disp_qactmap; 767 768 ASSERT(dq->dq_sruncnt == 0); 769 dq->dq_last = NULL; 770 771 /* 772 * The queue is empty, so the corresponding bit needs to be 773 * turned off in dqactmap. If nrunnable != 0 just took the 774 * last runnable thread off the 775 * highest queue, so recompute disp_maxrunpri. 776 */ 777 maxrunword = pri >> BT_ULSHIFT; 778 dqactmap[maxrunword] &= ~BT_BIW(pri); 779 780 if (dp->disp_nrunnable == 0) { 781 dp->disp_max_unbound_pri = -1; 782 dp->disp_maxrunpri = -1; 783 } else { 784 int ipri; 785 786 ipri = bt_gethighbit(dqactmap, maxrunword); 787 dp->disp_maxrunpri = ipri; 788 if (ipri < dp->disp_max_unbound_pri) 789 dp->disp_max_unbound_pri = ipri; 790 } 791 } else { 792 tp->t_link = NULL; 793 } 794 795 /* 796 * Set TS_DONT_SWAP flag to prevent another processor from swapping 797 * out this thread before we have a chance to run it. 798 * While running, it is protected against swapping by t_lock. 799 */ 800 tp->t_schedflag |= TS_DONT_SWAP; 801 cpup->cpu_dispthread = tp; /* protected by spl only */ 802 cpup->cpu_dispatch_pri = pri; 803 ASSERT(pri == DISP_PRIO(tp)); 804 thread_onproc(tp, cpup); /* set t_state to TS_ONPROC */ 805 disp_lock_exit_high(&dp->disp_lock); /* drop run queue lock */ 806 807 ASSERT(tp != NULL); 808 TRACE_1(TR_FAC_DISP, TR_DISP_END, 809 "disp_end:tid %p", tp); 810 811 if (disp_ratify(tp, kpq) == NULL) 812 goto reschedule; 813 814 return (tp); 815 } 816 817 /* 818 * swtch() 819 * Find best runnable thread and run it. 820 * Called with the current thread already switched to a new state, 821 * on a sleep queue, run queue, stopped, and not zombied. 822 * May be called at any spl level less than or equal to LOCK_LEVEL. 823 * Always drops spl to the base level (spl0()). 824 */ 825 void 826 swtch() 827 { 828 kthread_t *t = curthread; 829 kthread_t *next; 830 cpu_t *cp; 831 832 TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start"); 833 834 if (t->t_flag & T_INTR_THREAD) 835 cpu_intr_swtch_enter(t); 836 837 if (t->t_intr != NULL) { 838 /* 839 * We are an interrupt thread. Setup and return 840 * the interrupted thread to be resumed. 841 */ 842 (void) splhigh(); /* block other scheduler action */ 843 cp = CPU; /* now protected against migration */ 844 ASSERT(CPU_ON_INTR(cp) == 0); /* not called with PIL > 10 */ 845 CPU_STATS_ADDQ(cp, sys, pswitch, 1); 846 CPU_STATS_ADDQ(cp, sys, intrblk, 1); 847 next = thread_unpin(); 848 TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start"); 849 resume_from_intr(next); 850 } else { 851 #ifdef DEBUG 852 if (t->t_state == TS_ONPROC && 853 t->t_disp_queue->disp_cpu == CPU && 854 t->t_preempt == 0) { 855 thread_lock(t); 856 ASSERT(t->t_state != TS_ONPROC || 857 t->t_disp_queue->disp_cpu != CPU || 858 t->t_preempt != 0); /* cannot migrate */ 859 thread_unlock_nopreempt(t); 860 } 861 #endif /* DEBUG */ 862 cp = CPU; 863 next = disp(); /* returns with spl high */ 864 ASSERT(CPU_ON_INTR(cp) == 0); /* not called with PIL > 10 */ 865 866 /* OK to steal anything left on run queue */ 867 cp->cpu_disp_flags &= ~CPU_DISP_DONTSTEAL; 868 869 if (next != t) { 870 if (t == cp->cpu_idle_thread) { 871 PG_NRUN_UPDATE(cp, 1); 872 } else if (next == cp->cpu_idle_thread) { 873 PG_NRUN_UPDATE(cp, -1); 874 } 875 876 /* 877 * If t was previously in the TS_ONPROC state, 878 * setfrontdq and setbackdq won't have set its t_waitrq. 879 * Since we now finally know that we're switching away 880 * from this thread, set its t_waitrq if it is on a run 881 * queue. 882 */ 883 if ((t->t_state == TS_RUN) && (t->t_waitrq == 0)) { 884 t->t_waitrq = gethrtime_unscaled(); 885 } 886 887 /* 888 * restore mstate of thread that we are switching to 889 */ 890 restore_mstate(next); 891 892 CPU_STATS_ADDQ(cp, sys, pswitch, 1); 893 cp->cpu_last_swtch = t->t_disp_time = lbolt; 894 TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start"); 895 896 if (dtrace_vtime_active) 897 dtrace_vtime_switch(next); 898 899 resume(next); 900 /* 901 * The TR_RESUME_END and TR_SWTCH_END trace points 902 * appear at the end of resume(), because we may not 903 * return here 904 */ 905 } else { 906 if (t->t_flag & T_INTR_THREAD) 907 cpu_intr_swtch_exit(t); 908 909 DTRACE_SCHED(remain__cpu); 910 TRACE_0(TR_FAC_DISP, TR_SWTCH_END, "swtch_end"); 911 (void) spl0(); 912 } 913 } 914 } 915 916 /* 917 * swtch_from_zombie() 918 * Special case of swtch(), which allows checks for TS_ZOMB to be 919 * eliminated from normal resume. 920 * Find best runnable thread and run it. 921 * Called with the current thread zombied. 922 * Zombies cannot migrate, so CPU references are safe. 923 */ 924 void 925 swtch_from_zombie() 926 { 927 kthread_t *next; 928 cpu_t *cpu = CPU; 929 930 TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start"); 931 932 ASSERT(curthread->t_state == TS_ZOMB); 933 934 next = disp(); /* returns with spl high */ 935 ASSERT(CPU_ON_INTR(CPU) == 0); /* not called with PIL > 10 */ 936 CPU_STATS_ADDQ(CPU, sys, pswitch, 1); 937 ASSERT(next != curthread); 938 TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start"); 939 940 if (next == cpu->cpu_idle_thread) 941 PG_NRUN_UPDATE(cpu, -1); 942 943 restore_mstate(next); 944 945 if (dtrace_vtime_active) 946 dtrace_vtime_switch(next); 947 948 resume_from_zombie(next); 949 /* 950 * The TR_RESUME_END and TR_SWTCH_END trace points 951 * appear at the end of resume(), because we certainly will not 952 * return here 953 */ 954 } 955 956 #if defined(DEBUG) && (defined(DISP_DEBUG) || defined(lint)) 957 static int 958 thread_on_queue(kthread_t *tp) 959 { 960 cpu_t *cp; 961 cpu_t *self; 962 disp_t *dp; 963 964 self = CPU; 965 cp = self->cpu_next_onln; 966 dp = cp->cpu_disp; 967 for (;;) { 968 dispq_t *dq; 969 dispq_t *eq; 970 971 disp_lock_enter_high(&dp->disp_lock); 972 for (dq = dp->disp_q, eq = dp->disp_q_limit; dq < eq; ++dq) { 973 kthread_t *rp; 974 975 ASSERT(dq->dq_last == NULL || 976 dq->dq_last->t_link == NULL); 977 for (rp = dq->dq_first; rp; rp = rp->t_link) 978 if (tp == rp) { 979 disp_lock_exit_high(&dp->disp_lock); 980 return (1); 981 } 982 } 983 disp_lock_exit_high(&dp->disp_lock); 984 if (cp == NULL) 985 break; 986 if (cp == self) { 987 cp = NULL; 988 dp = &cp->cpu_part->cp_kp_queue; 989 } else { 990 cp = cp->cpu_next_onln; 991 dp = cp->cpu_disp; 992 } 993 } 994 return (0); 995 } /* end of thread_on_queue */ 996 #else 997 998 #define thread_on_queue(tp) 0 /* ASSERT must be !thread_on_queue */ 999 1000 #endif /* DEBUG */ 1001 1002 /* 1003 * like swtch(), but switch to a specified thread taken from another CPU. 1004 * called with spl high.. 1005 */ 1006 void 1007 swtch_to(kthread_t *next) 1008 { 1009 cpu_t *cp = CPU; 1010 1011 TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start"); 1012 1013 /* 1014 * Update context switch statistics. 1015 */ 1016 CPU_STATS_ADDQ(cp, sys, pswitch, 1); 1017 1018 TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start"); 1019 1020 if (curthread == cp->cpu_idle_thread) 1021 PG_NRUN_UPDATE(cp, 1); 1022 1023 /* OK to steal anything left on run queue */ 1024 cp->cpu_disp_flags &= ~CPU_DISP_DONTSTEAL; 1025 1026 /* record last execution time */ 1027 cp->cpu_last_swtch = curthread->t_disp_time = lbolt; 1028 1029 /* 1030 * If t was previously in the TS_ONPROC state, setfrontdq and setbackdq 1031 * won't have set its t_waitrq. Since we now finally know that we're 1032 * switching away from this thread, set its t_waitrq if it is on a run 1033 * queue. 1034 */ 1035 if ((curthread->t_state == TS_RUN) && (curthread->t_waitrq == 0)) { 1036 curthread->t_waitrq = gethrtime_unscaled(); 1037 } 1038 1039 /* restore next thread to previously running microstate */ 1040 restore_mstate(next); 1041 1042 if (dtrace_vtime_active) 1043 dtrace_vtime_switch(next); 1044 1045 resume(next); 1046 /* 1047 * The TR_RESUME_END and TR_SWTCH_END trace points 1048 * appear at the end of resume(), because we may not 1049 * return here 1050 */ 1051 } 1052 1053 1054 1055 #define CPU_IDLING(pri) ((pri) == -1) 1056 1057 static void 1058 cpu_resched(cpu_t *cp, pri_t tpri) 1059 { 1060 int call_poke_cpu = 0; 1061 pri_t cpupri = cp->cpu_dispatch_pri; 1062 1063 if (!CPU_IDLING(cpupri) && (cpupri < tpri)) { 1064 TRACE_2(TR_FAC_DISP, TR_CPU_RESCHED, 1065 "CPU_RESCHED:Tpri %d Cpupri %d", tpri, cpupri); 1066 if (tpri >= upreemptpri && cp->cpu_runrun == 0) { 1067 cp->cpu_runrun = 1; 1068 aston(cp->cpu_dispthread); 1069 if (tpri < kpreemptpri && cp != CPU) 1070 call_poke_cpu = 1; 1071 } 1072 if (tpri >= kpreemptpri && cp->cpu_kprunrun == 0) { 1073 cp->cpu_kprunrun = 1; 1074 if (cp != CPU) 1075 call_poke_cpu = 1; 1076 } 1077 } 1078 1079 /* 1080 * Propagate cpu_runrun, and cpu_kprunrun to global visibility. 1081 */ 1082 membar_enter(); 1083 1084 if (call_poke_cpu) 1085 poke_cpu(cp->cpu_id); 1086 } 1087 1088 /* 1089 * Perform multi-level CMT load balancing of running threads. 1090 * tp is the thread being enqueued 1091 * cp is the hint CPU (chosen by cpu_choose()). 1092 */ 1093 static cpu_t * 1094 cmt_balance(kthread_t *tp, cpu_t *cp) 1095 { 1096 int hint, i, cpu, nsiblings; 1097 int self = 0; 1098 group_t *cmt_pgs, *siblings; 1099 pg_cmt_t *pg, *pg_tmp, *tpg = NULL; 1100 int pg_nrun, tpg_nrun; 1101 int level = 0; 1102 cpu_t *newcp; 1103 1104 ASSERT(THREAD_LOCK_HELD(tp)); 1105 1106 cmt_pgs = &cp->cpu_pg->cmt_pgs; 1107 1108 if (GROUP_SIZE(cmt_pgs) == 0) 1109 return (cp); /* nothing to do */ 1110 1111 if (tp == curthread) 1112 self = 1; 1113 1114 /* 1115 * Balance across siblings in the CPUs CMT lineage 1116 */ 1117 do { 1118 pg = GROUP_ACCESS(cmt_pgs, level); 1119 1120 siblings = pg->cmt_siblings; 1121 nsiblings = GROUP_SIZE(siblings); /* self inclusive */ 1122 if (nsiblings == 1) 1123 continue; /* nobody to balance against */ 1124 1125 pg_nrun = pg->cmt_nrunning; 1126 if (self && 1127 bitset_in_set(&pg->cmt_cpus_actv_set, CPU->cpu_seqid)) 1128 pg_nrun--; /* Ignore curthread's effect */ 1129 1130 hint = pg->cmt_hint; 1131 /* 1132 * Check for validity of the hint 1133 * It should reference a valid sibling 1134 */ 1135 if (hint >= nsiblings) 1136 hint = pg->cmt_hint = 0; 1137 else 1138 pg->cmt_hint++; 1139 1140 /* 1141 * Find a balancing candidate from among our siblings 1142 * "hint" is a hint for where to start looking 1143 */ 1144 i = hint; 1145 do { 1146 ASSERT(i < nsiblings); 1147 pg_tmp = GROUP_ACCESS(siblings, i); 1148 1149 /* 1150 * The candidate must not be us, and must 1151 * have some CPU resources in the thread's 1152 * partition 1153 */ 1154 if (pg_tmp != pg && 1155 bitset_in_set(&tp->t_cpupart->cp_cmt_pgs, 1156 ((pg_t *)pg_tmp)->pg_id)) { 1157 tpg = pg_tmp; 1158 break; 1159 } 1160 1161 if (++i >= nsiblings) 1162 i = 0; 1163 } while (i != hint); 1164 1165 if (!tpg) 1166 continue; /* no candidates at this level */ 1167 1168 /* 1169 * Check if the balancing target is underloaded 1170 * Decide to balance if the target is running fewer 1171 * threads, or if it's running the same number of threads 1172 * with more online CPUs 1173 */ 1174 tpg_nrun = tpg->cmt_nrunning; 1175 if (pg_nrun > tpg_nrun || 1176 (pg_nrun == tpg_nrun && 1177 (GROUP_SIZE(&tpg->cmt_cpus_actv) > 1178 GROUP_SIZE(&pg->cmt_cpus_actv)))) { 1179 break; 1180 } 1181 tpg = NULL; 1182 } while (++level < GROUP_SIZE(cmt_pgs)); 1183 1184 1185 if (tpg) { 1186 /* 1187 * Select an idle CPU from the target PG 1188 */ 1189 for (cpu = 0; cpu < GROUP_SIZE(&tpg->cmt_cpus_actv); cpu++) { 1190 newcp = GROUP_ACCESS(&tpg->cmt_cpus_actv, cpu); 1191 if (newcp->cpu_part == tp->t_cpupart && 1192 newcp->cpu_dispatch_pri == -1) { 1193 cp = newcp; 1194 break; 1195 } 1196 } 1197 } 1198 1199 return (cp); 1200 } 1201 1202 /* 1203 * setbackdq() keeps runqs balanced such that the difference in length 1204 * between the chosen runq and the next one is no more than RUNQ_MAX_DIFF. 1205 * For threads with priorities below RUNQ_MATCH_PRI levels, the runq's lengths 1206 * must match. When per-thread TS_RUNQMATCH flag is set, setbackdq() will 1207 * try to keep runqs perfectly balanced regardless of the thread priority. 1208 */ 1209 #define RUNQ_MATCH_PRI 16 /* pri below which queue lengths must match */ 1210 #define RUNQ_MAX_DIFF 2 /* maximum runq length difference */ 1211 #define RUNQ_LEN(cp, pri) ((cp)->cpu_disp->disp_q[pri].dq_sruncnt) 1212 1213 /* 1214 * Put the specified thread on the back of the dispatcher 1215 * queue corresponding to its current priority. 1216 * 1217 * Called with the thread in transition, onproc or stopped state 1218 * and locked (transition implies locked) and at high spl. 1219 * Returns with the thread in TS_RUN state and still locked. 1220 */ 1221 void 1222 setbackdq(kthread_t *tp) 1223 { 1224 dispq_t *dq; 1225 disp_t *dp; 1226 cpu_t *cp; 1227 pri_t tpri; 1228 int bound; 1229 1230 ASSERT(THREAD_LOCK_HELD(tp)); 1231 ASSERT((tp->t_schedflag & TS_ALLSTART) == 0); 1232 ASSERT(!thread_on_queue(tp)); /* make sure tp isn't on a runq */ 1233 1234 /* 1235 * If thread is "swapped" or on the swap queue don't 1236 * queue it, but wake sched. 1237 */ 1238 if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD) { 1239 disp_swapped_setrun(tp); 1240 return; 1241 } 1242 1243 tpri = DISP_PRIO(tp); 1244 if (ncpus == 1) 1245 cp = tp->t_cpu; 1246 else if (!tp->t_bound_cpu && !tp->t_weakbound_cpu) { 1247 if (tpri >= kpqpri) { 1248 setkpdq(tp, SETKP_BACK); 1249 return; 1250 } 1251 /* 1252 * Let cpu_choose suggest a CPU. 1253 */ 1254 cp = cpu_choose(tp, tpri); 1255 1256 if (tp->t_cpupart == cp->cpu_part) { 1257 int qlen; 1258 1259 /* 1260 * Perform any CMT load balancing 1261 */ 1262 cp = cmt_balance(tp, cp); 1263 1264 /* 1265 * Balance across the run queues 1266 */ 1267 qlen = RUNQ_LEN(cp, tpri); 1268 if (tpri >= RUNQ_MATCH_PRI && 1269 !(tp->t_schedflag & TS_RUNQMATCH)) 1270 qlen -= RUNQ_MAX_DIFF; 1271 if (qlen > 0) { 1272 cpu_t *newcp; 1273 1274 if (tp->t_lpl->lpl_lgrpid == LGRP_ROOTID) { 1275 newcp = cp->cpu_next_part; 1276 } else if ((newcp = cp->cpu_next_lpl) == cp) { 1277 newcp = cp->cpu_next_part; 1278 } 1279 1280 if (RUNQ_LEN(newcp, tpri) < qlen) { 1281 DTRACE_PROBE3(runq__balance, 1282 kthread_t *, tp, 1283 cpu_t *, cp, cpu_t *, newcp); 1284 cp = newcp; 1285 } 1286 } 1287 } else { 1288 /* 1289 * Migrate to a cpu in the new partition. 1290 */ 1291 cp = disp_lowpri_cpu(tp->t_cpupart->cp_cpulist, 1292 tp->t_lpl, tp->t_pri, NULL); 1293 } 1294 bound = 0; 1295 ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0); 1296 } else { 1297 /* 1298 * It is possible that t_weakbound_cpu != t_bound_cpu (for 1299 * a short time until weak binding that existed when the 1300 * strong binding was established has dropped) so we must 1301 * favour weak binding over strong. 1302 */ 1303 cp = tp->t_weakbound_cpu ? 1304 tp->t_weakbound_cpu : tp->t_bound_cpu; 1305 bound = 1; 1306 } 1307 /* 1308 * A thread that is ONPROC may be temporarily placed on the run queue 1309 * but then chosen to run again by disp. If the thread we're placing on 1310 * the queue is in TS_ONPROC state, don't set its t_waitrq until a 1311 * replacement process is actually scheduled in swtch(). In this 1312 * situation, curthread is the only thread that could be in the ONPROC 1313 * state. 1314 */ 1315 if ((tp != curthread) && (tp->t_waitrq == 0)) { 1316 hrtime_t curtime; 1317 1318 curtime = gethrtime_unscaled(); 1319 (void) cpu_update_pct(tp, curtime); 1320 tp->t_waitrq = curtime; 1321 } else { 1322 (void) cpu_update_pct(tp, gethrtime_unscaled()); 1323 } 1324 1325 dp = cp->cpu_disp; 1326 disp_lock_enter_high(&dp->disp_lock); 1327 1328 DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, 0); 1329 TRACE_3(TR_FAC_DISP, TR_BACKQ, "setbackdq:pri %d cpu %p tid %p", 1330 tpri, cp, tp); 1331 1332 #ifndef NPROBE 1333 /* Kernel probe */ 1334 if (tnf_tracing_active) 1335 tnf_thread_queue(tp, cp, tpri); 1336 #endif /* NPROBE */ 1337 1338 ASSERT(tpri >= 0 && tpri < dp->disp_npri); 1339 1340 THREAD_RUN(tp, &dp->disp_lock); /* set t_state to TS_RUN */ 1341 tp->t_disp_queue = dp; 1342 tp->t_link = NULL; 1343 1344 dq = &dp->disp_q[tpri]; 1345 dp->disp_nrunnable++; 1346 if (!bound) 1347 dp->disp_steal = 0; 1348 membar_enter(); 1349 1350 if (dq->dq_sruncnt++ != 0) { 1351 ASSERT(dq->dq_first != NULL); 1352 dq->dq_last->t_link = tp; 1353 dq->dq_last = tp; 1354 } else { 1355 ASSERT(dq->dq_first == NULL); 1356 ASSERT(dq->dq_last == NULL); 1357 dq->dq_first = dq->dq_last = tp; 1358 BT_SET(dp->disp_qactmap, tpri); 1359 if (tpri > dp->disp_maxrunpri) { 1360 dp->disp_maxrunpri = tpri; 1361 membar_enter(); 1362 cpu_resched(cp, tpri); 1363 } 1364 } 1365 1366 if (!bound && tpri > dp->disp_max_unbound_pri) { 1367 if (tp == curthread && dp->disp_max_unbound_pri == -1 && 1368 cp == CPU) { 1369 /* 1370 * If there are no other unbound threads on the 1371 * run queue, don't allow other CPUs to steal 1372 * this thread while we are in the middle of a 1373 * context switch. We may just switch to it 1374 * again right away. CPU_DISP_DONTSTEAL is cleared 1375 * in swtch and swtch_to. 1376 */ 1377 cp->cpu_disp_flags |= CPU_DISP_DONTSTEAL; 1378 } 1379 dp->disp_max_unbound_pri = tpri; 1380 } 1381 (*disp_enq_thread)(cp, bound); 1382 } 1383 1384 /* 1385 * Put the specified thread on the front of the dispatcher 1386 * queue corresponding to its current priority. 1387 * 1388 * Called with the thread in transition, onproc or stopped state 1389 * and locked (transition implies locked) and at high spl. 1390 * Returns with the thread in TS_RUN state and still locked. 1391 */ 1392 void 1393 setfrontdq(kthread_t *tp) 1394 { 1395 disp_t *dp; 1396 dispq_t *dq; 1397 cpu_t *cp; 1398 pri_t tpri; 1399 int bound; 1400 1401 ASSERT(THREAD_LOCK_HELD(tp)); 1402 ASSERT((tp->t_schedflag & TS_ALLSTART) == 0); 1403 ASSERT(!thread_on_queue(tp)); /* make sure tp isn't on a runq */ 1404 1405 /* 1406 * If thread is "swapped" or on the swap queue don't 1407 * queue it, but wake sched. 1408 */ 1409 if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD) { 1410 disp_swapped_setrun(tp); 1411 return; 1412 } 1413 1414 tpri = DISP_PRIO(tp); 1415 if (ncpus == 1) 1416 cp = tp->t_cpu; 1417 else if (!tp->t_bound_cpu && !tp->t_weakbound_cpu) { 1418 if (tpri >= kpqpri) { 1419 setkpdq(tp, SETKP_FRONT); 1420 return; 1421 } 1422 cp = tp->t_cpu; 1423 if (tp->t_cpupart == cp->cpu_part) { 1424 /* 1425 * If we are of higher or equal priority than 1426 * the highest priority runnable thread of 1427 * the current CPU, just pick this CPU. Otherwise 1428 * Let cpu_choose() select the CPU. If this cpu 1429 * is the target of an offline request then do not 1430 * pick it - a thread_nomigrate() on the in motion 1431 * cpu relies on this when it forces a preempt. 1432 */ 1433 if (tpri < cp->cpu_disp->disp_maxrunpri || 1434 cp == cpu_inmotion) 1435 cp = cpu_choose(tp, tpri); 1436 } else { 1437 /* 1438 * Migrate to a cpu in the new partition. 1439 */ 1440 cp = disp_lowpri_cpu(tp->t_cpupart->cp_cpulist, 1441 tp->t_lpl, tp->t_pri, NULL); 1442 } 1443 bound = 0; 1444 ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0); 1445 } else { 1446 /* 1447 * It is possible that t_weakbound_cpu != t_bound_cpu (for 1448 * a short time until weak binding that existed when the 1449 * strong binding was established has dropped) so we must 1450 * favour weak binding over strong. 1451 */ 1452 cp = tp->t_weakbound_cpu ? 1453 tp->t_weakbound_cpu : tp->t_bound_cpu; 1454 bound = 1; 1455 } 1456 1457 /* 1458 * A thread that is ONPROC may be temporarily placed on the run queue 1459 * but then chosen to run again by disp. If the thread we're placing on 1460 * the queue is in TS_ONPROC state, don't set its t_waitrq until a 1461 * replacement process is actually scheduled in swtch(). In this 1462 * situation, curthread is the only thread that could be in the ONPROC 1463 * state. 1464 */ 1465 if ((tp != curthread) && (tp->t_waitrq == 0)) { 1466 hrtime_t curtime; 1467 1468 curtime = gethrtime_unscaled(); 1469 (void) cpu_update_pct(tp, curtime); 1470 tp->t_waitrq = curtime; 1471 } else { 1472 (void) cpu_update_pct(tp, gethrtime_unscaled()); 1473 } 1474 1475 dp = cp->cpu_disp; 1476 disp_lock_enter_high(&dp->disp_lock); 1477 1478 TRACE_2(TR_FAC_DISP, TR_FRONTQ, "frontq:pri %d tid %p", tpri, tp); 1479 DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, 1); 1480 1481 #ifndef NPROBE 1482 /* Kernel probe */ 1483 if (tnf_tracing_active) 1484 tnf_thread_queue(tp, cp, tpri); 1485 #endif /* NPROBE */ 1486 1487 ASSERT(tpri >= 0 && tpri < dp->disp_npri); 1488 1489 THREAD_RUN(tp, &dp->disp_lock); /* set TS_RUN state and lock */ 1490 tp->t_disp_queue = dp; 1491 1492 dq = &dp->disp_q[tpri]; 1493 dp->disp_nrunnable++; 1494 if (!bound) 1495 dp->disp_steal = 0; 1496 membar_enter(); 1497 1498 if (dq->dq_sruncnt++ != 0) { 1499 ASSERT(dq->dq_last != NULL); 1500 tp->t_link = dq->dq_first; 1501 dq->dq_first = tp; 1502 } else { 1503 ASSERT(dq->dq_last == NULL); 1504 ASSERT(dq->dq_first == NULL); 1505 tp->t_link = NULL; 1506 dq->dq_first = dq->dq_last = tp; 1507 BT_SET(dp->disp_qactmap, tpri); 1508 if (tpri > dp->disp_maxrunpri) { 1509 dp->disp_maxrunpri = tpri; 1510 membar_enter(); 1511 cpu_resched(cp, tpri); 1512 } 1513 } 1514 1515 if (!bound && tpri > dp->disp_max_unbound_pri) { 1516 if (tp == curthread && dp->disp_max_unbound_pri == -1 && 1517 cp == CPU) { 1518 /* 1519 * If there are no other unbound threads on the 1520 * run queue, don't allow other CPUs to steal 1521 * this thread while we are in the middle of a 1522 * context switch. We may just switch to it 1523 * again right away. CPU_DISP_DONTSTEAL is cleared 1524 * in swtch and swtch_to. 1525 */ 1526 cp->cpu_disp_flags |= CPU_DISP_DONTSTEAL; 1527 } 1528 dp->disp_max_unbound_pri = tpri; 1529 } 1530 (*disp_enq_thread)(cp, bound); 1531 } 1532 1533 /* 1534 * Put a high-priority unbound thread on the kp queue 1535 */ 1536 static void 1537 setkpdq(kthread_t *tp, int borf) 1538 { 1539 dispq_t *dq; 1540 disp_t *dp; 1541 cpu_t *cp; 1542 pri_t tpri; 1543 1544 tpri = DISP_PRIO(tp); 1545 1546 dp = &tp->t_cpupart->cp_kp_queue; 1547 disp_lock_enter_high(&dp->disp_lock); 1548 1549 TRACE_2(TR_FAC_DISP, TR_FRONTQ, "frontq:pri %d tid %p", tpri, tp); 1550 1551 ASSERT(tpri >= 0 && tpri < dp->disp_npri); 1552 DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, borf); 1553 THREAD_RUN(tp, &dp->disp_lock); /* set t_state to TS_RUN */ 1554 tp->t_disp_queue = dp; 1555 dp->disp_nrunnable++; 1556 dq = &dp->disp_q[tpri]; 1557 1558 if (dq->dq_sruncnt++ != 0) { 1559 if (borf == SETKP_BACK) { 1560 ASSERT(dq->dq_first != NULL); 1561 tp->t_link = NULL; 1562 dq->dq_last->t_link = tp; 1563 dq->dq_last = tp; 1564 } else { 1565 ASSERT(dq->dq_last != NULL); 1566 tp->t_link = dq->dq_first; 1567 dq->dq_first = tp; 1568 } 1569 } else { 1570 if (borf == SETKP_BACK) { 1571 ASSERT(dq->dq_first == NULL); 1572 ASSERT(dq->dq_last == NULL); 1573 dq->dq_first = dq->dq_last = tp; 1574 } else { 1575 ASSERT(dq->dq_last == NULL); 1576 ASSERT(dq->dq_first == NULL); 1577 tp->t_link = NULL; 1578 dq->dq_first = dq->dq_last = tp; 1579 } 1580 BT_SET(dp->disp_qactmap, tpri); 1581 if (tpri > dp->disp_max_unbound_pri) 1582 dp->disp_max_unbound_pri = tpri; 1583 if (tpri > dp->disp_maxrunpri) { 1584 dp->disp_maxrunpri = tpri; 1585 membar_enter(); 1586 } 1587 } 1588 1589 cp = tp->t_cpu; 1590 if (tp->t_cpupart != cp->cpu_part) { 1591 /* migrate to a cpu in the new partition */ 1592 cp = tp->t_cpupart->cp_cpulist; 1593 } 1594 cp = disp_lowpri_cpu(cp, tp->t_lpl, tp->t_pri, NULL); 1595 disp_lock_enter_high(&cp->cpu_disp->disp_lock); 1596 ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0); 1597 1598 #ifndef NPROBE 1599 /* Kernel probe */ 1600 if (tnf_tracing_active) 1601 tnf_thread_queue(tp, cp, tpri); 1602 #endif /* NPROBE */ 1603 1604 if (cp->cpu_chosen_level < tpri) 1605 cp->cpu_chosen_level = tpri; 1606 cpu_resched(cp, tpri); 1607 disp_lock_exit_high(&cp->cpu_disp->disp_lock); 1608 (*disp_enq_thread)(cp, 0); 1609 } 1610 1611 /* 1612 * Remove a thread from the dispatcher queue if it is on it. 1613 * It is not an error if it is not found but we return whether 1614 * or not it was found in case the caller wants to check. 1615 */ 1616 int 1617 dispdeq(kthread_t *tp) 1618 { 1619 disp_t *dp; 1620 dispq_t *dq; 1621 kthread_t *rp; 1622 kthread_t *trp; 1623 kthread_t **ptp; 1624 int tpri; 1625 1626 ASSERT(THREAD_LOCK_HELD(tp)); 1627 1628 if (tp->t_state != TS_RUN) 1629 return (0); 1630 1631 /* 1632 * The thread is "swapped" or is on the swap queue and 1633 * hence no longer on the run queue, so return true. 1634 */ 1635 if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD) 1636 return (1); 1637 1638 tpri = DISP_PRIO(tp); 1639 dp = tp->t_disp_queue; 1640 ASSERT(tpri < dp->disp_npri); 1641 dq = &dp->disp_q[tpri]; 1642 ptp = &dq->dq_first; 1643 rp = *ptp; 1644 trp = NULL; 1645 1646 ASSERT(dq->dq_last == NULL || dq->dq_last->t_link == NULL); 1647 1648 /* 1649 * Search for thread in queue. 1650 * Double links would simplify this at the expense of disp/setrun. 1651 */ 1652 while (rp != tp && rp != NULL) { 1653 trp = rp; 1654 ptp = &trp->t_link; 1655 rp = trp->t_link; 1656 } 1657 1658 if (rp == NULL) { 1659 panic("dispdeq: thread not on queue"); 1660 } 1661 1662 DTRACE_SCHED2(dequeue, kthread_t *, tp, disp_t *, dp); 1663 1664 /* 1665 * Found it so remove it from queue. 1666 */ 1667 if ((*ptp = rp->t_link) == NULL) 1668 dq->dq_last = trp; 1669 1670 dp->disp_nrunnable--; 1671 if (--dq->dq_sruncnt == 0) { 1672 dp->disp_qactmap[tpri >> BT_ULSHIFT] &= ~BT_BIW(tpri); 1673 if (dp->disp_nrunnable == 0) { 1674 dp->disp_max_unbound_pri = -1; 1675 dp->disp_maxrunpri = -1; 1676 } else if (tpri == dp->disp_maxrunpri) { 1677 int ipri; 1678 1679 ipri = bt_gethighbit(dp->disp_qactmap, 1680 dp->disp_maxrunpri >> BT_ULSHIFT); 1681 if (ipri < dp->disp_max_unbound_pri) 1682 dp->disp_max_unbound_pri = ipri; 1683 dp->disp_maxrunpri = ipri; 1684 } 1685 } 1686 tp->t_link = NULL; 1687 THREAD_TRANSITION(tp); /* put in intermediate state */ 1688 return (1); 1689 } 1690 1691 1692 /* 1693 * dq_sruninc and dq_srundec are public functions for 1694 * incrementing/decrementing the sruncnts when a thread on 1695 * a dispatcher queue is made schedulable/unschedulable by 1696 * resetting the TS_LOAD flag. 1697 * 1698 * The caller MUST have the thread lock and therefore the dispatcher 1699 * queue lock so that the operation which changes 1700 * the flag, the operation that checks the status of the thread to 1701 * determine if it's on a disp queue AND the call to this function 1702 * are one atomic operation with respect to interrupts. 1703 */ 1704 1705 /* 1706 * Called by sched AFTER TS_LOAD flag is set on a swapped, runnable thread. 1707 */ 1708 void 1709 dq_sruninc(kthread_t *t) 1710 { 1711 ASSERT(t->t_state == TS_RUN); 1712 ASSERT(t->t_schedflag & TS_LOAD); 1713 1714 THREAD_TRANSITION(t); 1715 setfrontdq(t); 1716 } 1717 1718 /* 1719 * See comment on calling conventions above. 1720 * Called by sched BEFORE TS_LOAD flag is cleared on a runnable thread. 1721 */ 1722 void 1723 dq_srundec(kthread_t *t) 1724 { 1725 ASSERT(t->t_schedflag & TS_LOAD); 1726 1727 (void) dispdeq(t); 1728 disp_swapped_enq(t); 1729 } 1730 1731 /* 1732 * Change the dispatcher lock of thread to the "swapped_lock" 1733 * and return with thread lock still held. 1734 * 1735 * Called with thread_lock held, in transition state, and at high spl. 1736 */ 1737 void 1738 disp_swapped_enq(kthread_t *tp) 1739 { 1740 ASSERT(THREAD_LOCK_HELD(tp)); 1741 ASSERT(tp->t_schedflag & TS_LOAD); 1742 1743 switch (tp->t_state) { 1744 case TS_RUN: 1745 disp_lock_enter_high(&swapped_lock); 1746 THREAD_SWAP(tp, &swapped_lock); /* set TS_RUN state and lock */ 1747 break; 1748 case TS_ONPROC: 1749 disp_lock_enter_high(&swapped_lock); 1750 THREAD_TRANSITION(tp); 1751 wake_sched_sec = 1; /* tell clock to wake sched */ 1752 THREAD_SWAP(tp, &swapped_lock); /* set TS_RUN state and lock */ 1753 break; 1754 default: 1755 panic("disp_swapped: tp: %p bad t_state", (void *)tp); 1756 } 1757 } 1758 1759 /* 1760 * This routine is called by setbackdq/setfrontdq if the thread is 1761 * not loaded or loaded and on the swap queue. 1762 * 1763 * Thread state TS_SLEEP implies that a swapped thread 1764 * has been woken up and needs to be swapped in by the swapper. 1765 * 1766 * Thread state TS_RUN, it implies that the priority of a swapped 1767 * thread is being increased by scheduling class (e.g. ts_update). 1768 */ 1769 static void 1770 disp_swapped_setrun(kthread_t *tp) 1771 { 1772 ASSERT(THREAD_LOCK_HELD(tp)); 1773 ASSERT((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD); 1774 1775 switch (tp->t_state) { 1776 case TS_SLEEP: 1777 disp_lock_enter_high(&swapped_lock); 1778 /* 1779 * Wakeup sched immediately (i.e., next tick) if the 1780 * thread priority is above maxclsyspri. 1781 */ 1782 if (DISP_PRIO(tp) > maxclsyspri) 1783 wake_sched = 1; 1784 else 1785 wake_sched_sec = 1; 1786 THREAD_RUN(tp, &swapped_lock); /* set TS_RUN state and lock */ 1787 break; 1788 case TS_RUN: /* called from ts_update */ 1789 break; 1790 default: 1791 panic("disp_swapped_setrun: tp: %p bad t_state", tp); 1792 } 1793 } 1794 1795 1796 /* 1797 * Make a thread give up its processor. Find the processor on 1798 * which this thread is executing, and have that processor 1799 * preempt. 1800 */ 1801 void 1802 cpu_surrender(kthread_t *tp) 1803 { 1804 cpu_t *cpup; 1805 int max_pri; 1806 int max_run_pri; 1807 klwp_t *lwp; 1808 1809 ASSERT(THREAD_LOCK_HELD(tp)); 1810 1811 if (tp->t_state != TS_ONPROC) 1812 return; 1813 cpup = tp->t_disp_queue->disp_cpu; /* CPU thread dispatched to */ 1814 max_pri = cpup->cpu_disp->disp_maxrunpri; /* best pri of that CPU */ 1815 max_run_pri = CP_MAXRUNPRI(cpup->cpu_part); 1816 if (max_pri < max_run_pri) 1817 max_pri = max_run_pri; 1818 1819 cpup->cpu_runrun = 1; 1820 if (max_pri >= kpreemptpri && cpup->cpu_kprunrun == 0) { 1821 cpup->cpu_kprunrun = 1; 1822 } 1823 1824 /* 1825 * Propagate cpu_runrun, and cpu_kprunrun to global visibility. 1826 */ 1827 membar_enter(); 1828 1829 DTRACE_SCHED1(surrender, kthread_t *, tp); 1830 1831 /* 1832 * Make the target thread take an excursion through trap() 1833 * to do preempt() (unless we're already in trap or post_syscall, 1834 * calling cpu_surrender via CL_TRAPRET). 1835 */ 1836 if (tp != curthread || (lwp = tp->t_lwp) == NULL || 1837 lwp->lwp_state != LWP_USER) { 1838 aston(tp); 1839 if (cpup != CPU) 1840 poke_cpu(cpup->cpu_id); 1841 } 1842 TRACE_2(TR_FAC_DISP, TR_CPU_SURRENDER, 1843 "cpu_surrender:tid %p cpu %p", tp, cpup); 1844 } 1845 1846 1847 /* 1848 * Commit to and ratify a scheduling decision 1849 */ 1850 /*ARGSUSED*/ 1851 static kthread_t * 1852 disp_ratify(kthread_t *tp, disp_t *kpq) 1853 { 1854 pri_t tpri, maxpri; 1855 pri_t maxkpri; 1856 cpu_t *cpup; 1857 1858 ASSERT(tp != NULL); 1859 /* 1860 * Commit to, then ratify scheduling decision 1861 */ 1862 cpup = CPU; 1863 if (cpup->cpu_runrun != 0) 1864 cpup->cpu_runrun = 0; 1865 if (cpup->cpu_kprunrun != 0) 1866 cpup->cpu_kprunrun = 0; 1867 if (cpup->cpu_chosen_level != -1) 1868 cpup->cpu_chosen_level = -1; 1869 membar_enter(); 1870 tpri = DISP_PRIO(tp); 1871 maxpri = cpup->cpu_disp->disp_maxrunpri; 1872 maxkpri = kpq->disp_maxrunpri; 1873 if (maxpri < maxkpri) 1874 maxpri = maxkpri; 1875 if (tpri < maxpri) { 1876 /* 1877 * should have done better 1878 * put this one back and indicate to try again 1879 */ 1880 cpup->cpu_dispthread = curthread; /* fixup dispthread */ 1881 cpup->cpu_dispatch_pri = DISP_PRIO(curthread); 1882 thread_lock_high(tp); 1883 THREAD_TRANSITION(tp); 1884 setfrontdq(tp); 1885 thread_unlock_nopreempt(tp); 1886 1887 tp = NULL; 1888 } 1889 return (tp); 1890 } 1891 1892 /* 1893 * See if there is any work on the dispatcher queue for other CPUs. 1894 * If there is, dequeue the best thread and return. 1895 */ 1896 static kthread_t * 1897 disp_getwork(cpu_t *cp) 1898 { 1899 cpu_t *ocp; /* other CPU */ 1900 cpu_t *ocp_start; 1901 cpu_t *tcp; /* target local CPU */ 1902 kthread_t *tp; 1903 kthread_t *retval = NULL; 1904 pri_t maxpri; 1905 disp_t *kpq; /* kp queue for this partition */ 1906 lpl_t *lpl, *lpl_leaf; 1907 int hint, leafidx; 1908 hrtime_t stealtime; 1909 1910 maxpri = -1; 1911 tcp = NULL; 1912 1913 kpq = &cp->cpu_part->cp_kp_queue; 1914 while (kpq->disp_maxrunpri >= 0) { 1915 /* 1916 * Try to take a thread from the kp_queue. 1917 */ 1918 tp = (disp_getbest(kpq)); 1919 if (tp) 1920 return (disp_ratify(tp, kpq)); 1921 } 1922 1923 kpreempt_disable(); /* protect the cpu_active list */ 1924 1925 /* 1926 * Try to find something to do on another CPU's run queue. 1927 * Loop through all other CPUs looking for the one with the highest 1928 * priority unbound thread. 1929 * 1930 * On NUMA machines, the partition's CPUs are consulted in order of 1931 * distance from the current CPU. This way, the first available 1932 * work found is also the closest, and will suffer the least 1933 * from being migrated. 1934 */ 1935 lpl = lpl_leaf = cp->cpu_lpl; 1936 hint = leafidx = 0; 1937 1938 /* 1939 * This loop traverses the lpl hierarchy. Higher level lpls represent 1940 * broader levels of locality 1941 */ 1942 do { 1943 /* This loop iterates over the lpl's leaves */ 1944 do { 1945 if (lpl_leaf != cp->cpu_lpl) 1946 ocp = lpl_leaf->lpl_cpus; 1947 else 1948 ocp = cp->cpu_next_lpl; 1949 1950 /* This loop iterates over the CPUs in the leaf */ 1951 ocp_start = ocp; 1952 do { 1953 pri_t pri; 1954 1955 ASSERT(CPU_ACTIVE(ocp)); 1956 1957 /* 1958 * End our stroll around this lpl if: 1959 * 1960 * - Something became runnable on the local 1961 * queue...which also ends our stroll around 1962 * the partition. 1963 * 1964 * - We happen across another idle CPU. 1965 * Since it is patrolling the next portion 1966 * of the lpl's list (assuming it's not 1967 * halted), move to the next higher level 1968 * of locality. 1969 */ 1970 if (cp->cpu_disp->disp_nrunnable != 0) { 1971 kpreempt_enable(); 1972 return (NULL); 1973 } 1974 if (ocp->cpu_dispatch_pri == -1) { 1975 if (ocp->cpu_disp_flags & 1976 CPU_DISP_HALTED) 1977 continue; 1978 else 1979 break; 1980 } 1981 1982 /* 1983 * If there's only one thread and the CPU 1984 * is in the middle of a context switch, 1985 * or it's currently running the idle thread, 1986 * don't steal it. 1987 */ 1988 if ((ocp->cpu_disp_flags & 1989 CPU_DISP_DONTSTEAL) && 1990 ocp->cpu_disp->disp_nrunnable == 1) 1991 continue; 1992 1993 pri = ocp->cpu_disp->disp_max_unbound_pri; 1994 if (pri > maxpri) { 1995 /* 1996 * Don't steal threads that we attempted 1997 * to steal recently until they're ready 1998 * to be stolen again. 1999 */ 2000 stealtime = ocp->cpu_disp->disp_steal; 2001 if (stealtime == 0 || 2002 stealtime - gethrtime() <= 0) { 2003 maxpri = pri; 2004 tcp = ocp; 2005 } else { 2006 /* 2007 * Don't update tcp, just set 2008 * the retval to T_DONTSTEAL, so 2009 * that if no acceptable CPUs 2010 * are found the return value 2011 * will be T_DONTSTEAL rather 2012 * then NULL. 2013 */ 2014 retval = T_DONTSTEAL; 2015 } 2016 } 2017 } while ((ocp = ocp->cpu_next_lpl) != ocp_start); 2018 2019 if ((lpl_leaf = lpl->lpl_rset[++leafidx]) == NULL) { 2020 leafidx = 0; 2021 lpl_leaf = lpl->lpl_rset[leafidx]; 2022 } 2023 } while (leafidx != hint); 2024 2025 hint = leafidx = lpl->lpl_hint; 2026 if ((lpl = lpl->lpl_parent) != NULL) 2027 lpl_leaf = lpl->lpl_rset[hint]; 2028 } while (!tcp && lpl); 2029 2030 kpreempt_enable(); 2031 2032 /* 2033 * If another queue looks good, and there is still nothing on 2034 * the local queue, try to transfer one or more threads 2035 * from it to our queue. 2036 */ 2037 if (tcp && cp->cpu_disp->disp_nrunnable == 0) { 2038 tp = disp_getbest(tcp->cpu_disp); 2039 if (tp == NULL || tp == T_DONTSTEAL) 2040 return (tp); 2041 return (disp_ratify(tp, kpq)); 2042 } 2043 return (retval); 2044 } 2045 2046 2047 /* 2048 * disp_fix_unbound_pri() 2049 * Determines the maximum priority of unbound threads on the queue. 2050 * The priority is kept for the queue, but is only increased, never 2051 * reduced unless some CPU is looking for something on that queue. 2052 * 2053 * The priority argument is the known upper limit. 2054 * 2055 * Perhaps this should be kept accurately, but that probably means 2056 * separate bitmaps for bound and unbound threads. Since only idled 2057 * CPUs will have to do this recalculation, it seems better this way. 2058 */ 2059 static void 2060 disp_fix_unbound_pri(disp_t *dp, pri_t pri) 2061 { 2062 kthread_t *tp; 2063 dispq_t *dq; 2064 ulong_t *dqactmap = dp->disp_qactmap; 2065 ulong_t mapword; 2066 int wx; 2067 2068 ASSERT(DISP_LOCK_HELD(&dp->disp_lock)); 2069 2070 ASSERT(pri >= 0); /* checked by caller */ 2071 2072 /* 2073 * Start the search at the next lowest priority below the supplied 2074 * priority. This depends on the bitmap implementation. 2075 */ 2076 do { 2077 wx = pri >> BT_ULSHIFT; /* index of word in map */ 2078 2079 /* 2080 * Form mask for all lower priorities in the word. 2081 */ 2082 mapword = dqactmap[wx] & (BT_BIW(pri) - 1); 2083 2084 /* 2085 * Get next lower active priority. 2086 */ 2087 if (mapword != 0) { 2088 pri = (wx << BT_ULSHIFT) + highbit(mapword) - 1; 2089 } else if (wx > 0) { 2090 pri = bt_gethighbit(dqactmap, wx - 1); /* sign extend */ 2091 if (pri < 0) 2092 break; 2093 } else { 2094 pri = -1; 2095 break; 2096 } 2097 2098 /* 2099 * Search the queue for unbound, runnable threads. 2100 */ 2101 dq = &dp->disp_q[pri]; 2102 tp = dq->dq_first; 2103 2104 while (tp && (tp->t_bound_cpu || tp->t_weakbound_cpu)) { 2105 tp = tp->t_link; 2106 } 2107 2108 /* 2109 * If a thread was found, set the priority and return. 2110 */ 2111 } while (tp == NULL); 2112 2113 /* 2114 * pri holds the maximum unbound thread priority or -1. 2115 */ 2116 if (dp->disp_max_unbound_pri != pri) 2117 dp->disp_max_unbound_pri = pri; 2118 } 2119 2120 /* 2121 * disp_adjust_unbound_pri() - thread is becoming unbound, so we should 2122 * check if the CPU to which is was previously bound should have 2123 * its disp_max_unbound_pri increased. 2124 */ 2125 void 2126 disp_adjust_unbound_pri(kthread_t *tp) 2127 { 2128 disp_t *dp; 2129 pri_t tpri; 2130 2131 ASSERT(THREAD_LOCK_HELD(tp)); 2132 2133 /* 2134 * Don't do anything if the thread is not bound, or 2135 * currently not runnable or swapped out. 2136 */ 2137 if (tp->t_bound_cpu == NULL || 2138 tp->t_state != TS_RUN || 2139 tp->t_schedflag & TS_ON_SWAPQ) 2140 return; 2141 2142 tpri = DISP_PRIO(tp); 2143 dp = tp->t_bound_cpu->cpu_disp; 2144 ASSERT(tpri >= 0 && tpri < dp->disp_npri); 2145 if (tpri > dp->disp_max_unbound_pri) 2146 dp->disp_max_unbound_pri = tpri; 2147 } 2148 2149 /* 2150 * disp_getbest() 2151 * De-queue the highest priority unbound runnable thread. 2152 * Returns with the thread unlocked and onproc but at splhigh (like disp()). 2153 * Returns NULL if nothing found. 2154 * Returns T_DONTSTEAL if the thread was not stealable. 2155 * so that the caller will try again later. 2156 * 2157 * Passed a pointer to a dispatch queue not associated with this CPU, and 2158 * its type. 2159 */ 2160 static kthread_t * 2161 disp_getbest(disp_t *dp) 2162 { 2163 kthread_t *tp; 2164 dispq_t *dq; 2165 pri_t pri; 2166 cpu_t *cp, *tcp; 2167 boolean_t allbound; 2168 2169 disp_lock_enter(&dp->disp_lock); 2170 2171 /* 2172 * If there is nothing to run, or the CPU is in the middle of a 2173 * context switch of the only thread, return NULL. 2174 */ 2175 tcp = dp->disp_cpu; 2176 cp = CPU; 2177 pri = dp->disp_max_unbound_pri; 2178 if (pri == -1 || 2179 (tcp != NULL && (tcp->cpu_disp_flags & CPU_DISP_DONTSTEAL) && 2180 tcp->cpu_disp->disp_nrunnable == 1)) { 2181 disp_lock_exit_nopreempt(&dp->disp_lock); 2182 return (NULL); 2183 } 2184 2185 dq = &dp->disp_q[pri]; 2186 2187 2188 /* 2189 * Assume that all threads are bound on this queue, and change it 2190 * later when we find out that it is not the case. 2191 */ 2192 allbound = B_TRUE; 2193 for (tp = dq->dq_first; tp != NULL; tp = tp->t_link) { 2194 hrtime_t now, nosteal, rqtime; 2195 2196 /* 2197 * Skip over bound threads which could be here even 2198 * though disp_max_unbound_pri indicated this level. 2199 */ 2200 if (tp->t_bound_cpu || tp->t_weakbound_cpu) 2201 continue; 2202 2203 /* 2204 * We've got some unbound threads on this queue, so turn 2205 * the allbound flag off now. 2206 */ 2207 allbound = B_FALSE; 2208 2209 /* 2210 * The thread is a candidate for stealing from its run queue. We 2211 * don't want to steal threads that became runnable just a 2212 * moment ago. This improves CPU affinity for threads that get 2213 * preempted for short periods of time and go back on the run 2214 * queue. 2215 * 2216 * We want to let it stay on its run queue if it was only placed 2217 * there recently and it was running on the same CPU before that 2218 * to preserve its cache investment. For the thread to remain on 2219 * its run queue, ALL of the following conditions must be 2220 * satisfied: 2221 * 2222 * - the disp queue should not be the kernel preemption queue 2223 * - delayed idle stealing should not be disabled 2224 * - nosteal_nsec should be non-zero 2225 * - it should run with user priority 2226 * - it should be on the run queue of the CPU where it was 2227 * running before being placed on the run queue 2228 * - it should be the only thread on the run queue (to prevent 2229 * extra scheduling latency for other threads) 2230 * - it should sit on the run queue for less than per-chip 2231 * nosteal interval or global nosteal interval 2232 * - in case of CPUs with shared cache it should sit in a run 2233 * queue of a CPU from a different chip 2234 * 2235 * The checks are arranged so that the ones that are faster are 2236 * placed earlier. 2237 */ 2238 if (tcp == NULL || 2239 pri >= minclsyspri || 2240 tp->t_cpu != tcp) 2241 break; 2242 2243 /* 2244 * Steal immediately if, due to CMT processor architecture 2245 * migraiton between cp and tcp would incur no performance 2246 * penalty. 2247 */ 2248 if (pg_cmt_can_migrate(cp, tcp)) 2249 break; 2250 2251 nosteal = nosteal_nsec; 2252 if (nosteal == 0) 2253 break; 2254 2255 /* 2256 * Calculate time spent sitting on run queue 2257 */ 2258 now = gethrtime_unscaled(); 2259 rqtime = now - tp->t_waitrq; 2260 scalehrtime(&rqtime); 2261 2262 /* 2263 * Steal immediately if the time spent on this run queue is more 2264 * than allowed nosteal delay. 2265 * 2266 * Negative rqtime check is needed here to avoid infinite 2267 * stealing delays caused by unlikely but not impossible 2268 * drifts between CPU times on different CPUs. 2269 */ 2270 if (rqtime > nosteal || rqtime < 0) 2271 break; 2272 2273 DTRACE_PROBE4(nosteal, kthread_t *, tp, 2274 cpu_t *, tcp, cpu_t *, cp, hrtime_t, rqtime); 2275 scalehrtime(&now); 2276 /* 2277 * Calculate when this thread becomes stealable 2278 */ 2279 now += (nosteal - rqtime); 2280 2281 /* 2282 * Calculate time when some thread becomes stealable 2283 */ 2284 if (now < dp->disp_steal) 2285 dp->disp_steal = now; 2286 } 2287 2288 /* 2289 * If there were no unbound threads on this queue, find the queue 2290 * where they are and then return later. The value of 2291 * disp_max_unbound_pri is not always accurate because it isn't 2292 * reduced until another idle CPU looks for work. 2293 */ 2294 if (allbound) 2295 disp_fix_unbound_pri(dp, pri); 2296 2297 /* 2298 * If we reached the end of the queue and found no unbound threads 2299 * then return NULL so that other CPUs will be considered. If there 2300 * are unbound threads but they cannot yet be stolen, then 2301 * return T_DONTSTEAL and try again later. 2302 */ 2303 if (tp == NULL) { 2304 disp_lock_exit_nopreempt(&dp->disp_lock); 2305 return (allbound ? NULL : T_DONTSTEAL); 2306 } 2307 2308 /* 2309 * Found a runnable, unbound thread, so remove it from queue. 2310 * dispdeq() requires that we have the thread locked, and we do, 2311 * by virtue of holding the dispatch queue lock. dispdeq() will 2312 * put the thread in transition state, thereby dropping the dispq 2313 * lock. 2314 */ 2315 2316 #ifdef DEBUG 2317 { 2318 int thread_was_on_queue; 2319 2320 thread_was_on_queue = dispdeq(tp); /* drops disp_lock */ 2321 ASSERT(thread_was_on_queue); 2322 } 2323 2324 #else /* DEBUG */ 2325 (void) dispdeq(tp); /* drops disp_lock */ 2326 #endif /* DEBUG */ 2327 2328 /* 2329 * Reset the disp_queue steal time - we do not know what is the smallest 2330 * value across the queue is. 2331 */ 2332 dp->disp_steal = 0; 2333 2334 tp->t_schedflag |= TS_DONT_SWAP; 2335 2336 /* 2337 * Setup thread to run on the current CPU. 2338 */ 2339 tp->t_disp_queue = cp->cpu_disp; 2340 2341 cp->cpu_dispthread = tp; /* protected by spl only */ 2342 cp->cpu_dispatch_pri = pri; 2343 ASSERT(pri == DISP_PRIO(tp)); 2344 2345 DTRACE_PROBE3(steal, kthread_t *, tp, cpu_t *, tcp, cpu_t *, cp); 2346 2347 thread_onproc(tp, cp); /* set t_state to TS_ONPROC */ 2348 2349 /* 2350 * Return with spl high so that swtch() won't need to raise it. 2351 * The disp_lock was dropped by dispdeq(). 2352 */ 2353 2354 return (tp); 2355 } 2356 2357 /* 2358 * disp_bound_common() - common routine for higher level functions 2359 * that check for bound threads under certain conditions. 2360 * If 'threadlistsafe' is set then there is no need to acquire 2361 * pidlock to stop the thread list from changing (eg, if 2362 * disp_bound_* is called with cpus paused). 2363 */ 2364 static int 2365 disp_bound_common(cpu_t *cp, int threadlistsafe, int flag) 2366 { 2367 int found = 0; 2368 kthread_t *tp; 2369 2370 ASSERT(flag); 2371 2372 if (!threadlistsafe) 2373 mutex_enter(&pidlock); 2374 tp = curthread; /* faster than allthreads */ 2375 do { 2376 if (tp->t_state != TS_FREE) { 2377 /* 2378 * If an interrupt thread is busy, but the 2379 * caller doesn't care (i.e. BOUND_INTR is off), 2380 * then just ignore it and continue through. 2381 */ 2382 if ((tp->t_flag & T_INTR_THREAD) && 2383 !(flag & BOUND_INTR)) 2384 continue; 2385 2386 /* 2387 * Skip the idle thread for the CPU 2388 * we're about to set offline. 2389 */ 2390 if (tp == cp->cpu_idle_thread) 2391 continue; 2392 2393 /* 2394 * Skip the pause thread for the CPU 2395 * we're about to set offline. 2396 */ 2397 if (tp == cp->cpu_pause_thread) 2398 continue; 2399 2400 if ((flag & BOUND_CPU) && 2401 (tp->t_bound_cpu == cp || 2402 tp->t_bind_cpu == cp->cpu_id || 2403 tp->t_weakbound_cpu == cp)) { 2404 found = 1; 2405 break; 2406 } 2407 2408 if ((flag & BOUND_PARTITION) && 2409 (tp->t_cpupart == cp->cpu_part)) { 2410 found = 1; 2411 break; 2412 } 2413 } 2414 } while ((tp = tp->t_next) != curthread && found == 0); 2415 if (!threadlistsafe) 2416 mutex_exit(&pidlock); 2417 return (found); 2418 } 2419 2420 /* 2421 * disp_bound_threads - return nonzero if threads are bound to the processor. 2422 * Called infrequently. Keep this simple. 2423 * Includes threads that are asleep or stopped but not onproc. 2424 */ 2425 int 2426 disp_bound_threads(cpu_t *cp, int threadlistsafe) 2427 { 2428 return (disp_bound_common(cp, threadlistsafe, BOUND_CPU)); 2429 } 2430 2431 /* 2432 * disp_bound_anythreads - return nonzero if _any_ threads are bound 2433 * to the given processor, including interrupt threads. 2434 */ 2435 int 2436 disp_bound_anythreads(cpu_t *cp, int threadlistsafe) 2437 { 2438 return (disp_bound_common(cp, threadlistsafe, BOUND_CPU | BOUND_INTR)); 2439 } 2440 2441 /* 2442 * disp_bound_partition - return nonzero if threads are bound to the same 2443 * partition as the processor. 2444 * Called infrequently. Keep this simple. 2445 * Includes threads that are asleep or stopped but not onproc. 2446 */ 2447 int 2448 disp_bound_partition(cpu_t *cp, int threadlistsafe) 2449 { 2450 return (disp_bound_common(cp, threadlistsafe, BOUND_PARTITION)); 2451 } 2452 2453 /* 2454 * disp_cpu_inactive - make a CPU inactive by moving all of its unbound 2455 * threads to other CPUs. 2456 */ 2457 void 2458 disp_cpu_inactive(cpu_t *cp) 2459 { 2460 kthread_t *tp; 2461 disp_t *dp = cp->cpu_disp; 2462 dispq_t *dq; 2463 pri_t pri; 2464 int wasonq; 2465 2466 disp_lock_enter(&dp->disp_lock); 2467 while ((pri = dp->disp_max_unbound_pri) != -1) { 2468 dq = &dp->disp_q[pri]; 2469 tp = dq->dq_first; 2470 2471 /* 2472 * Skip over bound threads. 2473 */ 2474 while (tp != NULL && tp->t_bound_cpu != NULL) { 2475 tp = tp->t_link; 2476 } 2477 2478 if (tp == NULL) { 2479 /* disp_max_unbound_pri must be inaccurate, so fix it */ 2480 disp_fix_unbound_pri(dp, pri); 2481 continue; 2482 } 2483 2484 wasonq = dispdeq(tp); /* drops disp_lock */ 2485 ASSERT(wasonq); 2486 ASSERT(tp->t_weakbound_cpu == NULL); 2487 2488 setbackdq(tp); 2489 /* 2490 * Called from cpu_offline: 2491 * 2492 * cp has already been removed from the list of active cpus 2493 * and tp->t_cpu has been changed so there is no risk of 2494 * tp ending up back on cp. 2495 * 2496 * Called from cpupart_move_cpu: 2497 * 2498 * The cpu has moved to a new cpupart. Any threads that 2499 * were on it's dispatch queues before the move remain 2500 * in the old partition and can't run in the new partition. 2501 */ 2502 ASSERT(tp->t_cpu != cp); 2503 thread_unlock(tp); 2504 2505 disp_lock_enter(&dp->disp_lock); 2506 } 2507 disp_lock_exit(&dp->disp_lock); 2508 } 2509 2510 /* 2511 * disp_lowpri_cpu - find CPU running the lowest priority thread. 2512 * The hint passed in is used as a starting point so we don't favor 2513 * CPU 0 or any other CPU. The caller should pass in the most recently 2514 * used CPU for the thread. 2515 * 2516 * The lgroup and priority are used to determine the best CPU to run on 2517 * in a NUMA machine. The lgroup specifies which CPUs are closest while 2518 * the thread priority will indicate whether the thread will actually run 2519 * there. To pick the best CPU, the CPUs inside and outside of the given 2520 * lgroup which are running the lowest priority threads are found. The 2521 * remote CPU is chosen only if the thread will not run locally on a CPU 2522 * within the lgroup, but will run on the remote CPU. If the thread 2523 * cannot immediately run on any CPU, the best local CPU will be chosen. 2524 * 2525 * The lpl specified also identifies the cpu partition from which 2526 * disp_lowpri_cpu should select a CPU. 2527 * 2528 * curcpu is used to indicate that disp_lowpri_cpu is being called on 2529 * behalf of the current thread. (curthread is looking for a new cpu) 2530 * In this case, cpu_dispatch_pri for this thread's cpu should be 2531 * ignored. 2532 * 2533 * If a cpu is the target of an offline request then try to avoid it. 2534 * 2535 * This function must be called at either high SPL, or with preemption 2536 * disabled, so that the "hint" CPU cannot be removed from the online 2537 * CPU list while we are traversing it. 2538 */ 2539 cpu_t * 2540 disp_lowpri_cpu(cpu_t *hint, lpl_t *lpl, pri_t tpri, cpu_t *curcpu) 2541 { 2542 cpu_t *bestcpu; 2543 cpu_t *besthomecpu; 2544 cpu_t *cp, *cpstart; 2545 2546 pri_t bestpri; 2547 pri_t cpupri; 2548 2549 klgrpset_t done; 2550 klgrpset_t cur_set; 2551 2552 lpl_t *lpl_iter, *lpl_leaf; 2553 int i; 2554 2555 /* 2556 * Scan for a CPU currently running the lowest priority thread. 2557 * Cannot get cpu_lock here because it is adaptive. 2558 * We do not require lock on CPU list. 2559 */ 2560 ASSERT(hint != NULL); 2561 ASSERT(lpl != NULL); 2562 ASSERT(lpl->lpl_ncpu > 0); 2563 2564 /* 2565 * First examine local CPUs. Note that it's possible the hint CPU 2566 * passed in in remote to the specified home lgroup. If our priority 2567 * isn't sufficient enough such that we can run immediately at home, 2568 * then examine CPUs remote to our home lgroup. 2569 * We would like to give preference to CPUs closest to "home". 2570 * If we can't find a CPU where we'll run at a given level 2571 * of locality, we expand our search to include the next level. 2572 */ 2573 bestcpu = besthomecpu = NULL; 2574 klgrpset_clear(done); 2575 /* start with lpl we were passed */ 2576 2577 lpl_iter = lpl; 2578 2579 do { 2580 2581 bestpri = SHRT_MAX; 2582 klgrpset_clear(cur_set); 2583 2584 for (i = 0; i < lpl_iter->lpl_nrset; i++) { 2585 lpl_leaf = lpl_iter->lpl_rset[i]; 2586 if (klgrpset_ismember(done, lpl_leaf->lpl_lgrpid)) 2587 continue; 2588 2589 klgrpset_add(cur_set, lpl_leaf->lpl_lgrpid); 2590 2591 if (hint->cpu_lpl == lpl_leaf) 2592 cp = cpstart = hint; 2593 else 2594 cp = cpstart = lpl_leaf->lpl_cpus; 2595 2596 do { 2597 if (cp == curcpu) 2598 cpupri = -1; 2599 else if (cp == cpu_inmotion) 2600 cpupri = SHRT_MAX; 2601 else 2602 cpupri = cp->cpu_dispatch_pri; 2603 if (cp->cpu_disp->disp_maxrunpri > cpupri) 2604 cpupri = cp->cpu_disp->disp_maxrunpri; 2605 if (cp->cpu_chosen_level > cpupri) 2606 cpupri = cp->cpu_chosen_level; 2607 if (cpupri < bestpri) { 2608 if (CPU_IDLING(cpupri)) { 2609 ASSERT((cp->cpu_flags & 2610 CPU_QUIESCED) == 0); 2611 return (cp); 2612 } 2613 bestcpu = cp; 2614 bestpri = cpupri; 2615 } 2616 } while ((cp = cp->cpu_next_lpl) != cpstart); 2617 } 2618 2619 if (bestcpu && (tpri > bestpri)) { 2620 ASSERT((bestcpu->cpu_flags & CPU_QUIESCED) == 0); 2621 return (bestcpu); 2622 } 2623 if (besthomecpu == NULL) 2624 besthomecpu = bestcpu; 2625 /* 2626 * Add the lgrps we just considered to the "done" set 2627 */ 2628 klgrpset_or(done, cur_set); 2629 2630 } while ((lpl_iter = lpl_iter->lpl_parent) != NULL); 2631 2632 /* 2633 * The specified priority isn't high enough to run immediately 2634 * anywhere, so just return the best CPU from the home lgroup. 2635 */ 2636 ASSERT((besthomecpu->cpu_flags & CPU_QUIESCED) == 0); 2637 return (besthomecpu); 2638 } 2639 2640 /* 2641 * This routine provides the generic idle cpu function for all processors. 2642 * If a processor has some specific code to execute when idle (say, to stop 2643 * the pipeline and save power) then that routine should be defined in the 2644 * processors specific code (module_xx.c) and the global variable idle_cpu 2645 * set to that function. 2646 */ 2647 static void 2648 generic_idle_cpu(void) 2649 { 2650 } 2651 2652 /*ARGSUSED*/ 2653 static void 2654 generic_enq_thread(cpu_t *cpu, int bound) 2655 { 2656 } 2657 2658 /* 2659 * Select a CPU for this thread to run on. Choose t->t_cpu unless: 2660 * - t->t_cpu is not in this thread's assigned lgrp 2661 * - the time since the thread last came off t->t_cpu exceeds the 2662 * rechoose time for this cpu (ignore this if t is curthread in 2663 * which case it's on CPU and t->t_disp_time is inaccurate) 2664 * - t->t_cpu is presently the target of an offline or partition move 2665 * request 2666 */ 2667 static cpu_t * 2668 cpu_choose(kthread_t *t, pri_t tpri) 2669 { 2670 ASSERT(tpri < kpqpri); 2671 2672 if ((((lbolt - t->t_disp_time) > rechoose_interval) && 2673 t != curthread) || t->t_cpu == cpu_inmotion) { 2674 return (disp_lowpri_cpu(t->t_cpu, t->t_lpl, tpri, NULL)); 2675 } 2676 2677 /* 2678 * Take a trip through disp_lowpri_cpu() if the thread was 2679 * running outside it's home lgroup 2680 */ 2681 if (!klgrpset_ismember(t->t_lpl->lpl_lgrp->lgrp_set[LGRP_RSRC_CPU], 2682 t->t_cpu->cpu_lpl->lpl_lgrpid)) { 2683 return (disp_lowpri_cpu(t->t_cpu, t->t_lpl, tpri, 2684 (t == curthread) ? t->t_cpu : NULL)); 2685 } 2686 return (t->t_cpu); 2687 } 2688