kern_synch.c revision 116182
1198090Srdivacky/*- 2198090Srdivacky * Copyright (c) 1982, 1986, 1990, 1991, 1993 3353358Sdim * The Regents of the University of California. All rights reserved. 4353358Sdim * (c) UNIX System Laboratories, Inc. 5353358Sdim * All or some portions of this file are derived from material licensed 6198090Srdivacky * to the University of California by American Telephone and Telegraph 7198090Srdivacky * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8198090Srdivacky * the permission of UNIX System Laboratories, Inc. 9198090Srdivacky * 10198090Srdivacky * Redistribution and use in source and binary forms, with or without 11198090Srdivacky * modification, are permitted provided that the following conditions 12204642Srdivacky * are met: 13204642Srdivacky * 1. Redistributions of source code must retain the above copyright 14210299Sed * notice, this list of conditions and the following disclaimer. 15204642Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 16198090Srdivacky * notice, this list of conditions and the following disclaimer in the 17198090Srdivacky * documentation and/or other materials provided with the distribution. 18198090Srdivacky * 3. All advertising materials mentioning features or use of this software 19198090Srdivacky * must display the following acknowledgement: 20198090Srdivacky * This product includes software developed by the University of 21296417Sdim * California, Berkeley and its contributors. 22360784Sdim * 4. Neither the name of the University nor the names of its contributors 23198090Srdivacky * may be used to endorse or promote products derived from this software 24198090Srdivacky * without specific prior written permission. 25296417Sdim * 26353358Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27210299Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28210299Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29210299Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30344779Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31210299Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32210299Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33296417Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34296417Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35296417Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36198090Srdivacky * SUCH DAMAGE. 37198090Srdivacky * 38296417Sdim * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95 39296417Sdim */ 40198090Srdivacky 41198090Srdivacky#include <sys/cdefs.h> 42198090Srdivacky__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 116182 2003-06-11 00:56:59Z obrien $"); 43296417Sdim 44296417Sdim#include "opt_ddb.h" 45296417Sdim#include "opt_ktrace.h" 46344779Sdim#ifdef __i386__ 47344779Sdim#include "opt_swtch.h" 48344779Sdim#endif 49344779Sdim 50344779Sdim#include <sys/param.h> 51344779Sdim#include <sys/systm.h> 52210299Sed#include <sys/condvar.h> 53210299Sed#include <sys/kernel.h> 54296417Sdim#include <sys/ktr.h> 55210299Sed#include <sys/lock.h> 56210299Sed#include <sys/mutex.h> 57210299Sed#include <sys/proc.h> 58210299Sed#include <sys/resourcevar.h> 59296417Sdim#include <sys/sched.h> 60296417Sdim#include <sys/signalvar.h> 61210299Sed#include <sys/smp.h> 62210299Sed#include <sys/sx.h> 63210299Sed#include <sys/sysctl.h> 64210299Sed#include <sys/sysproto.h> 65210299Sed#include <sys/vmmeter.h> 66210299Sed#ifdef DDB 67210299Sed#include <ddb/ddb.h> 68296417Sdim#endif 69210299Sed#ifdef KTRACE 70210299Sed#include <sys/uio.h> 71210299Sed#include <sys/ktrace.h> 72210299Sed#endif 73296417Sdim 74296417Sdim#include <machine/cpu.h> 75210299Sed#ifdef SWTCH_OPTIM_STATS 76198090Srdivacky#include <machine/md_var.h> 77198090Srdivacky#endif 78198090Srdivacky 79198090Srdivackystatic void sched_setup(void *dummy); 80198090SrdivackySYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL) 81198892Srdivacky 82198892Srdivackyint hogticks; 83218893Sdimint lbolt; 84288943Sdim 85344779Sdimstatic struct callout loadav_callout; 86288943Sdimstatic struct callout lbolt_callout; 87288943Sdim 88344779Sdimstruct loadavg averunnable = 89353358Sdim { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ 90353358Sdim/* 91198090Srdivacky * Constants for averages over 1, 5, and 15 minutes 92198090Srdivacky * when sampling at 5 second intervals. 93198090Srdivacky */ 94353358Sdimstatic fixpt_t cexp[3] = { 95198090Srdivacky 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 96296417Sdim 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 97296417Sdim 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 98296417Sdim}; 99296417Sdim 100296417Sdim/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */ 101296417Sdimstatic int fscale __unused = FSCALE; 102296417SdimSYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, ""); 103296417Sdim 104296417Sdimstatic void endtsleep(void *); 105296417Sdimstatic void loadav(void *arg); 106296417Sdimstatic void lboltcb(void *arg); 107296417Sdim 108296417Sdim/* 109296417Sdim * We're only looking at 7 bits of the address; everything is 110296417Sdim * aligned to 4, lots of things are aligned to greater powers 111296417Sdim * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 112296417Sdim */ 113296417Sdim#define TABLESIZE 128 114296417Sdimstatic TAILQ_HEAD(slpquehead, thread) slpque[TABLESIZE]; 115296417Sdim#define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1)) 116296417Sdim 117296417Sdimvoid 118314564Sdimsleepinit(void) 119309124Sdim{ 120314564Sdim int i; 121309124Sdim 122296417Sdim hogticks = (hz / 10) * 2; /* Default only. */ 123296417Sdim for (i = 0; i < TABLESIZE; i++) 124296417Sdim TAILQ_INIT(&slpque[i]); 125296417Sdim} 126296417Sdim 127296417Sdim/* 128296417Sdim * General sleep call. Suspends the current process until a wakeup is 129296417Sdim * performed on the specified identifier. The process will then be made 130296417Sdim * runnable with the specified priority. Sleeps at most timo/hz seconds 131296417Sdim * (0 means no timeout). If pri includes PCATCH flag, signals are checked 132296417Sdim * before and after sleeping, else signals are not checked. Returns 0 if 133296417Sdim * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 134296417Sdim * signal needs to be delivered, ERESTART is returned if the current system 135296417Sdim * call should be restarted if possible, and EINTR is returned if the system 136296417Sdim * call should be interrupted by the signal (return EINTR). 137296417Sdim * 138296417Sdim * The mutex argument is exited before the caller is suspended, and 139296417Sdim * entered before msleep returns. If priority includes the PDROP 140296417Sdim * flag the mutex is not entered before returning. 141309124Sdim */ 142296417Sdim 143296417Sdimint 144296417Sdimmsleep(ident, mtx, priority, wmesg, timo) 145296417Sdim void *ident; 146296417Sdim struct mtx *mtx; 147296417Sdim int priority, timo; 148296417Sdim const char *wmesg; 149{ 150 struct thread *td = curthread; 151 struct proc *p = td->td_proc; 152 int sig, catch = priority & PCATCH; 153 int rval = 0; 154 WITNESS_SAVE_DECL(mtx); 155 156#ifdef KTRACE 157 if (KTRPOINT(td, KTR_CSW)) 158 ktrcsw(1, 0); 159#endif 160 /* XXX: mtx == NULL ?? */ 161 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mtx->mtx_object, 162 "Sleeping on \"%s\"", wmesg); 163 KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL, 164 ("sleeping without a mutex")); 165 /* 166 * If we are capable of async syscalls and there isn't already 167 * another one ready to return, start a new thread 168 * and queue it as ready to run. Note that there is danger here 169 * because we need to make sure that we don't sleep allocating 170 * the thread (recursion here might be bad). 171 */ 172 mtx_lock_spin(&sched_lock); 173 if (p->p_flag & P_THREADED || p->p_numthreads > 1) { 174 /* 175 * Just don't bother if we are exiting 176 * and not the exiting thread or thread was marked as 177 * interrupted. 178 */ 179 if (catch && 180 (((p->p_flag & P_WEXIT) && (p->p_singlethread != td)) || 181 (td->td_flags & TDF_INTERRUPT))) { 182 td->td_flags &= ~TDF_INTERRUPT; 183 mtx_unlock_spin(&sched_lock); 184 return (EINTR); 185 } 186 } 187 if (cold ) { 188 /* 189 * During autoconfiguration, just give interrupts 190 * a chance, then just return. 191 * Don't run any other procs or panic below, 192 * in case this is the idle process and already asleep. 193 */ 194 if (mtx != NULL && priority & PDROP) 195 mtx_unlock(mtx); 196 mtx_unlock_spin(&sched_lock); 197 return (0); 198 } 199 200 DROP_GIANT(); 201 202 if (mtx != NULL) { 203 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); 204 WITNESS_SAVE(&mtx->mtx_object, mtx); 205 mtx_unlock(mtx); 206 if (priority & PDROP) 207 mtx = NULL; 208 } 209 210 KASSERT(p != NULL, ("msleep1")); 211 KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep")); 212 213 CTR5(KTR_PROC, "msleep: thread %p (pid %d, %s) on %s (%p)", 214 td, p->p_pid, p->p_comm, wmesg, ident); 215 216 td->td_wchan = ident; 217 td->td_wmesg = wmesg; 218 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], td, td_slpq); 219 TD_SET_ON_SLEEPQ(td); 220 if (timo) 221 callout_reset(&td->td_slpcallout, timo, endtsleep, td); 222 /* 223 * We put ourselves on the sleep queue and start our timeout 224 * before calling thread_suspend_check, as we could stop there, and 225 * a wakeup or a SIGCONT (or both) could occur while we were stopped. 226 * without resuming us, thus we must be ready for sleep 227 * when cursig is called. If the wakeup happens while we're 228 * stopped, td->td_wchan will be 0 upon return from cursig. 229 */ 230 if (catch) { 231 CTR3(KTR_PROC, "msleep caught: thread %p (pid %d, %s)", td, 232 p->p_pid, p->p_comm); 233 td->td_flags |= TDF_SINTR; 234 mtx_unlock_spin(&sched_lock); 235 PROC_LOCK(p); 236 mtx_lock(&p->p_sigacts->ps_mtx); 237 sig = cursig(td); 238 mtx_unlock(&p->p_sigacts->ps_mtx); 239 if (sig == 0 && thread_suspend_check(1)) 240 sig = SIGSTOP; 241 mtx_lock_spin(&sched_lock); 242 PROC_UNLOCK(p); 243 if (sig != 0) { 244 if (TD_ON_SLEEPQ(td)) 245 unsleep(td); 246 } else if (!TD_ON_SLEEPQ(td)) 247 catch = 0; 248 } else 249 sig = 0; 250 251 /* 252 * Let the scheduler know we're about to voluntarily go to sleep. 253 */ 254 sched_sleep(td, priority & PRIMASK); 255 256 if (TD_ON_SLEEPQ(td)) { 257 p->p_stats->p_ru.ru_nvcsw++; 258 TD_SET_SLEEPING(td); 259 mi_switch(); 260 } 261 /* 262 * We're awake from voluntary sleep. 263 */ 264 CTR3(KTR_PROC, "msleep resume: thread %p (pid %d, %s)", td, p->p_pid, 265 p->p_comm); 266 KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING")); 267 td->td_flags &= ~TDF_SINTR; 268 if (td->td_flags & TDF_TIMEOUT) { 269 td->td_flags &= ~TDF_TIMEOUT; 270 if (sig == 0) 271 rval = EWOULDBLOCK; 272 } else if (td->td_flags & TDF_TIMOFAIL) { 273 td->td_flags &= ~TDF_TIMOFAIL; 274 } else if (timo && callout_stop(&td->td_slpcallout) == 0) { 275 /* 276 * This isn't supposed to be pretty. If we are here, then 277 * the endtsleep() callout is currently executing on another 278 * CPU and is either spinning on the sched_lock or will be 279 * soon. If we don't synchronize here, there is a chance 280 * that this process may msleep() again before the callout 281 * has a chance to run and the callout may end up waking up 282 * the wrong msleep(). Yuck. 283 */ 284 TD_SET_SLEEPING(td); 285 p->p_stats->p_ru.ru_nivcsw++; 286 mi_switch(); 287 td->td_flags &= ~TDF_TIMOFAIL; 288 } 289 if ((td->td_flags & TDF_INTERRUPT) && (priority & PCATCH) && 290 (rval == 0)) { 291 td->td_flags &= ~TDF_INTERRUPT; 292 rval = EINTR; 293 } 294 mtx_unlock_spin(&sched_lock); 295 296 if (rval == 0 && catch) { 297 PROC_LOCK(p); 298 /* XXX: shouldn't we always be calling cursig() */ 299 mtx_lock(&p->p_sigacts->ps_mtx); 300 if (sig != 0 || (sig = cursig(td))) { 301 if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) 302 rval = EINTR; 303 else 304 rval = ERESTART; 305 } 306 mtx_unlock(&p->p_sigacts->ps_mtx); 307 PROC_UNLOCK(p); 308 } 309#ifdef KTRACE 310 if (KTRPOINT(td, KTR_CSW)) 311 ktrcsw(0, 0); 312#endif 313 PICKUP_GIANT(); 314 if (mtx != NULL) { 315 mtx_lock(mtx); 316 WITNESS_RESTORE(&mtx->mtx_object, mtx); 317 } 318 return (rval); 319} 320 321/* 322 * Implement timeout for msleep() 323 * 324 * If process hasn't been awakened (wchan non-zero), 325 * set timeout flag and undo the sleep. If proc 326 * is stopped, just unsleep so it will remain stopped. 327 * MP-safe, called without the Giant mutex. 328 */ 329static void 330endtsleep(arg) 331 void *arg; 332{ 333 register struct thread *td = arg; 334 335 CTR3(KTR_PROC, "endtsleep: thread %p (pid %d, %s)", 336 td, td->td_proc->p_pid, td->td_proc->p_comm); 337 mtx_lock_spin(&sched_lock); 338 /* 339 * This is the other half of the synchronization with msleep() 340 * described above. If the TDS_TIMEOUT flag is set, we lost the 341 * race and just need to put the process back on the runqueue. 342 */ 343 if (TD_ON_SLEEPQ(td)) { 344 TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq); 345 TD_CLR_ON_SLEEPQ(td); 346 td->td_flags |= TDF_TIMEOUT; 347 td->td_wmesg = NULL; 348 } else { 349 td->td_flags |= TDF_TIMOFAIL; 350 } 351 TD_CLR_SLEEPING(td); 352 setrunnable(td); 353 mtx_unlock_spin(&sched_lock); 354} 355 356/* 357 * Abort a thread, as if an interrupt had occured. Only abort 358 * interruptable waits (unfortunatly it isn't only safe to abort others). 359 * This is about identical to cv_abort(). 360 * Think about merging them? 361 * Also, whatever the signal code does... 362 */ 363void 364abortsleep(struct thread *td) 365{ 366 367 mtx_assert(&sched_lock, MA_OWNED); 368 /* 369 * If the TDF_TIMEOUT flag is set, just leave. A 370 * timeout is scheduled anyhow. 371 */ 372 if ((td->td_flags & (TDF_TIMEOUT | TDF_SINTR)) == TDF_SINTR) { 373 if (TD_ON_SLEEPQ(td)) { 374 unsleep(td); 375 TD_CLR_SLEEPING(td); 376 setrunnable(td); 377 } 378 } 379} 380 381/* 382 * Remove a process from its wait queue 383 */ 384void 385unsleep(struct thread *td) 386{ 387 388 mtx_lock_spin(&sched_lock); 389 if (TD_ON_SLEEPQ(td)) { 390 TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq); 391 TD_CLR_ON_SLEEPQ(td); 392 td->td_wmesg = NULL; 393 } 394 mtx_unlock_spin(&sched_lock); 395} 396 397/* 398 * Make all processes sleeping on the specified identifier runnable. 399 */ 400void 401wakeup(ident) 402 register void *ident; 403{ 404 register struct slpquehead *qp; 405 register struct thread *td; 406 struct thread *ntd; 407 struct proc *p; 408 409 mtx_lock_spin(&sched_lock); 410 qp = &slpque[LOOKUP(ident)]; 411restart: 412 for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) { 413 ntd = TAILQ_NEXT(td, td_slpq); 414 if (td->td_wchan == ident) { 415 unsleep(td); 416 TD_CLR_SLEEPING(td); 417 setrunnable(td); 418 p = td->td_proc; 419 CTR3(KTR_PROC,"wakeup: thread %p (pid %d, %s)", 420 td, p->p_pid, p->p_comm); 421 goto restart; 422 } 423 } 424 mtx_unlock_spin(&sched_lock); 425} 426 427/* 428 * Make a process sleeping on the specified identifier runnable. 429 * May wake more than one process if a target process is currently 430 * swapped out. 431 */ 432void 433wakeup_one(ident) 434 register void *ident; 435{ 436 register struct slpquehead *qp; 437 register struct thread *td; 438 register struct proc *p; 439 struct thread *ntd; 440 441 mtx_lock_spin(&sched_lock); 442 qp = &slpque[LOOKUP(ident)]; 443 for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) { 444 ntd = TAILQ_NEXT(td, td_slpq); 445 if (td->td_wchan == ident) { 446 unsleep(td); 447 TD_CLR_SLEEPING(td); 448 setrunnable(td); 449 p = td->td_proc; 450 CTR3(KTR_PROC,"wakeup1: thread %p (pid %d, %s)", 451 td, p->p_pid, p->p_comm); 452 break; 453 } 454 } 455 mtx_unlock_spin(&sched_lock); 456} 457 458/* 459 * The machine independent parts of mi_switch(). 460 */ 461void 462mi_switch(void) 463{ 464 struct bintime new_switchtime; 465 struct thread *td; 466#if !defined(__alpha__) && !defined(__powerpc__) 467 struct thread *newtd; 468#endif 469 struct proc *p; 470 u_int sched_nest; 471 472 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); 473 td = curthread; /* XXX */ 474 p = td->td_proc; /* XXX */ 475 KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code")); 476#ifdef INVARIANTS 477 if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td)) 478 mtx_assert(&Giant, MA_NOTOWNED); 479#endif 480 KASSERT(td->td_critnest == 1, 481 ("mi_switch: switch in a critical section")); 482 483 /* 484 * Compute the amount of time during which the current 485 * process was running, and add that to its total so far. 486 */ 487 binuptime(&new_switchtime); 488 bintime_add(&p->p_runtime, &new_switchtime); 489 bintime_sub(&p->p_runtime, PCPU_PTR(switchtime)); 490 491#ifdef DDB 492 /* 493 * Don't perform context switches from the debugger. 494 */ 495 if (db_active) { 496 mtx_unlock_spin(&sched_lock); 497 db_print_backtrace(); 498 db_error("Context switches not allowed in the debugger."); 499 } 500#endif 501 502 /* 503 * Check if the process exceeds its cpu resource allocation. If 504 * over max, arrange to kill the process in ast(). 505 */ 506 if (p->p_cpulimit != RLIM_INFINITY && 507 p->p_runtime.sec > p->p_cpulimit) { 508 p->p_sflag |= PS_XCPU; 509 td->td_flags |= TDF_ASTPENDING; 510 } 511 512 /* 513 * Finish up stats for outgoing thread. 514 */ 515 cnt.v_swtch++; 516 PCPU_SET(switchtime, new_switchtime); 517 CTR3(KTR_PROC, "mi_switch: old thread %p (pid %d, %s)", td, p->p_pid, 518 p->p_comm); 519 sched_nest = sched_lock.mtx_recurse; 520 if (td->td_proc->p_flag & P_THREADED) 521 thread_switchout(td); 522 sched_switchout(td); 523 524#if !defined(__alpha__) && !defined(__powerpc__) 525 newtd = choosethread(); 526 if (td != newtd) 527 cpu_switch(td, newtd); /* SHAZAM!! */ 528#ifdef SWTCH_OPTIM_STATS 529 else 530 stupid_switch++; 531#endif 532#else 533 cpu_switch(); /* SHAZAM!!*/ 534#endif 535 536 sched_lock.mtx_recurse = sched_nest; 537 sched_lock.mtx_lock = (uintptr_t)td; 538 sched_switchin(td); 539 540 /* 541 * Start setting up stats etc. for the incoming thread. 542 * Similar code in fork_exit() is returned to by cpu_switch() 543 * in the case of a new thread/process. 544 */ 545 CTR3(KTR_PROC, "mi_switch: new thread %p (pid %d, %s)", td, p->p_pid, 546 p->p_comm); 547 if (PCPU_GET(switchtime.sec) == 0) 548 binuptime(PCPU_PTR(switchtime)); 549 PCPU_SET(switchticks, ticks); 550 551 /* 552 * Call the switchin function while still holding the scheduler lock 553 * (used by the idlezero code and the general page-zeroing code) 554 */ 555 if (td->td_switchin) 556 td->td_switchin(); 557 558 /* 559 * If the last thread was exiting, finish cleaning it up. 560 */ 561 if ((td = PCPU_GET(deadthread))) { 562 PCPU_SET(deadthread, NULL); 563 thread_stash(td); 564 } 565} 566 567/* 568 * Change process state to be runnable, 569 * placing it on the run queue if it is in memory, 570 * and awakening the swapper if it isn't in memory. 571 */ 572void 573setrunnable(struct thread *td) 574{ 575 struct proc *p = td->td_proc; 576 577 mtx_assert(&sched_lock, MA_OWNED); 578 switch (p->p_state) { 579 case PRS_ZOMBIE: 580 panic("setrunnable(1)"); 581 default: 582 break; 583 } 584 switch (td->td_state) { 585 case TDS_RUNNING: 586 case TDS_RUNQ: 587 return; 588 case TDS_INHIBITED: 589 /* 590 * If we are only inhibited because we are swapped out 591 * then arange to swap in this process. Otherwise just return. 592 */ 593 if (td->td_inhibitors != TDI_SWAPPED) 594 return; 595 /* XXX: intentional fall-through ? */ 596 case TDS_CAN_RUN: 597 break; 598 default: 599 printf("state is 0x%x", td->td_state); 600 panic("setrunnable(2)"); 601 } 602 if ((p->p_sflag & PS_INMEM) == 0) { 603 if ((p->p_sflag & PS_SWAPPINGIN) == 0) { 604 p->p_sflag |= PS_SWAPINREQ; 605 wakeup(&proc0); 606 } 607 } else 608 sched_wakeup(td); 609} 610 611/* 612 * Compute a tenex style load average of a quantity on 613 * 1, 5 and 15 minute intervals. 614 * XXXKSE Needs complete rewrite when correct info is available. 615 * Completely Bogus.. only works with 1:1 (but compiles ok now :-) 616 */ 617static void 618loadav(void *arg) 619{ 620 int i, nrun; 621 struct loadavg *avg; 622 struct proc *p; 623 struct thread *td; 624 625 avg = &averunnable; 626 sx_slock(&allproc_lock); 627 nrun = 0; 628 FOREACH_PROC_IN_SYSTEM(p) { 629 FOREACH_THREAD_IN_PROC(p, td) { 630 switch (td->td_state) { 631 case TDS_RUNQ: 632 case TDS_RUNNING: 633 if ((p->p_flag & P_NOLOAD) != 0) 634 goto nextproc; 635 nrun++; /* XXXKSE */ 636 default: 637 break; 638 } 639nextproc: 640 continue; 641 } 642 } 643 sx_sunlock(&allproc_lock); 644 for (i = 0; i < 3; i++) 645 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + 646 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 647 648 /* 649 * Schedule the next update to occur after 5 seconds, but add a 650 * random variation to avoid synchronisation with processes that 651 * run at regular intervals. 652 */ 653 callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)), 654 loadav, NULL); 655} 656 657static void 658lboltcb(void *arg) 659{ 660 wakeup(&lbolt); 661 callout_reset(&lbolt_callout, hz, lboltcb, NULL); 662} 663 664/* ARGSUSED */ 665static void 666sched_setup(dummy) 667 void *dummy; 668{ 669 callout_init(&loadav_callout, 0); 670 callout_init(&lbolt_callout, 1); 671 672 /* Kick off timeout driven events by calling first time. */ 673 loadav(NULL); 674 lboltcb(NULL); 675} 676 677/* 678 * General purpose yield system call 679 */ 680int 681yield(struct thread *td, struct yield_args *uap) 682{ 683 struct ksegrp *kg = td->td_ksegrp; 684 685 mtx_assert(&Giant, MA_NOTOWNED); 686 mtx_lock_spin(&sched_lock); 687 kg->kg_proc->p_stats->p_ru.ru_nvcsw++; 688 sched_prio(td, PRI_MAX_TIMESHARE); 689 mi_switch(); 690 mtx_unlock_spin(&sched_lock); 691 td->td_retval[0] = 0; 692 693 return (0); 694} 695 696