1 /* $OpenBSD: kern_synch.c,v 1.5 1996/11/23 23:19:51 kstailey Exp $ */ 2 /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1990, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_synch.c 8.6 (Berkeley) 1/21/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/kernel.h> 48 #include <sys/buf.h> 49 #include <sys/signalvar.h> 50 #include <sys/resourcevar.h> 51 #include <vm/vm.h> 52 #ifdef KTRACE 53 #include <sys/ktrace.h> 54 #endif 55 56 #include <machine/cpu.h> 57 58 u_char curpriority; /* usrpri of curproc */ 59 int lbolt; /* once a second sleep address */ 60 61 void roundrobin __P((void *)); 62 void schedcpu __P((void *)); 63 void updatepri __P((struct proc *)); 64 void endtsleep __P((void *)); 65 66 /* 67 * Force switch among equal priority processes every 100ms. 68 */ 69 /* ARGSUSED */ 70 void 71 roundrobin(arg) 72 void *arg; 73 { 74 75 need_resched(); 76 timeout(roundrobin, NULL, hz / 10); 77 } 78 79 /* 80 * Constants for digital decay and forget: 81 * 90% of (p_estcpu) usage in 5 * loadav time 82 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive) 83 * Note that, as ps(1) mentions, this can let percentages 84 * total over 100% (I've seen 137.9% for 3 processes). 85 * 86 * Note that hardclock updates p_estcpu and p_cpticks independently. 87 * 88 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds. 89 * That is, the system wants to compute a value of decay such 90 * that the following for loop: 91 * for (i = 0; i < (5 * loadavg); i++) 92 * p_estcpu *= decay; 93 * will compute 94 * p_estcpu *= 0.1; 95 * for all values of loadavg: 96 * 97 * Mathematically this loop can be expressed by saying: 98 * decay ** (5 * loadavg) ~= .1 99 * 100 * The system computes decay as: 101 * decay = (2 * loadavg) / (2 * loadavg + 1) 102 * 103 * We wish to prove that the system's computation of decay 104 * will always fulfill the equation: 105 * decay ** (5 * loadavg) ~= .1 106 * 107 * If we compute b as: 108 * b = 2 * loadavg 109 * then 110 * decay = b / (b + 1) 111 * 112 * We now need to prove two things: 113 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1) 114 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg) 115 * 116 * Facts: 117 * For x close to zero, exp(x) =~ 1 + x, since 118 * exp(x) = 0! + x**1/1! + x**2/2! + ... . 119 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b. 120 * For x close to zero, ln(1+x) =~ x, since 121 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1 122 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1). 123 * ln(.1) =~ -2.30 124 * 125 * Proof of (1): 126 * Solve (factor)**(power) =~ .1 given power (5*loadav): 127 * solving for factor, 128 * ln(factor) =~ (-2.30/5*loadav), or 129 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) = 130 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED 131 * 132 * Proof of (2): 133 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)): 134 * solving for power, 135 * power*ln(b/(b+1)) =~ -2.30, or 136 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED 137 * 138 * Actual power values for the implemented algorithm are as follows: 139 * loadav: 1 2 3 4 140 * power: 5.68 10.32 14.94 19.55 141 */ 142 143 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */ 144 #define loadfactor(loadav) (2 * (loadav)) 145 #define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE)) 146 147 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */ 148 fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 149 150 /* 151 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the 152 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below 153 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT). 154 * 155 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used: 156 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits). 157 * 158 * If you dont want to bother with the faster/more-accurate formula, you 159 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate 160 * (more general) method of calculating the %age of CPU used by a process. 161 */ 162 #define CCPU_SHIFT 11 163 164 /* 165 * Recompute process priorities, every hz ticks. 166 */ 167 /* ARGSUSED */ 168 void 169 schedcpu(arg) 170 void *arg; 171 { 172 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 173 register struct proc *p; 174 register int s; 175 register unsigned int newcpu; 176 177 wakeup((caddr_t)&lbolt); 178 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 179 /* 180 * Increment time in/out of memory and sleep time 181 * (if sleeping). We ignore overflow; with 16-bit int's 182 * (remember them?) overflow takes 45 days. 183 */ 184 p->p_swtime++; 185 if (p->p_stat == SSLEEP || p->p_stat == SSTOP) 186 p->p_slptime++; 187 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT; 188 /* 189 * If the process has slept the entire second, 190 * stop recalculating its priority until it wakes up. 191 */ 192 if (p->p_slptime > 1) 193 continue; 194 s = splstatclock(); /* prevent state changes */ 195 /* 196 * p_pctcpu is only for ps. 197 */ 198 #if (FSHIFT >= CCPU_SHIFT) 199 p->p_pctcpu += (hz == 100)? 200 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT): 201 100 * (((fixpt_t) p->p_cpticks) 202 << (FSHIFT - CCPU_SHIFT)) / hz; 203 #else 204 p->p_pctcpu += ((FSCALE - ccpu) * 205 (p->p_cpticks * FSCALE / hz)) >> FSHIFT; 206 #endif 207 p->p_cpticks = 0; 208 newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice; 209 p->p_estcpu = min(newcpu, UCHAR_MAX); 210 resetpriority(p); 211 if (p->p_priority >= PUSER) { 212 #define PPQ (128 / NQS) /* priorities per queue */ 213 if ((p != curproc) && 214 p->p_stat == SRUN && 215 (p->p_flag & P_INMEM) && 216 (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) { 217 remrunqueue(p); 218 p->p_priority = p->p_usrpri; 219 setrunqueue(p); 220 } else 221 p->p_priority = p->p_usrpri; 222 } 223 splx(s); 224 } 225 vmmeter(); 226 if (bclnlist != NULL) 227 wakeup((caddr_t)pageproc); 228 timeout(schedcpu, (void *)0, hz); 229 } 230 231 /* 232 * Recalculate the priority of a process after it has slept for a while. 233 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at 234 * least six times the loadfactor will decay p_estcpu to zero. 235 */ 236 void 237 updatepri(p) 238 register struct proc *p; 239 { 240 register unsigned int newcpu = p->p_estcpu; 241 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); 242 243 if (p->p_slptime > 5 * loadfac) 244 p->p_estcpu = 0; 245 else { 246 p->p_slptime--; /* the first time was done in schedcpu */ 247 while (newcpu && --p->p_slptime) 248 newcpu = (int) decay_cpu(loadfac, newcpu); 249 p->p_estcpu = min(newcpu, UCHAR_MAX); 250 } 251 resetpriority(p); 252 } 253 254 /* 255 * We're only looking at 7 bits of the address; everything is 256 * aligned to 4, lots of things are aligned to greater powers 257 * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 258 */ 259 #define TABLESIZE 128 260 #define LOOKUP(x) (((long)(x) >> 8) & (TABLESIZE - 1)) 261 struct slpque { 262 struct proc *sq_head; 263 struct proc **sq_tailp; 264 } slpque[TABLESIZE]; 265 266 /* 267 * During autoconfiguration or after a panic, a sleep will simply 268 * lower the priority briefly to allow interrupts, then return. 269 * The priority to be used (safepri) is machine-dependent, thus this 270 * value is initialized and maintained in the machine-dependent layers. 271 * This priority will typically be 0, or the lowest priority 272 * that is safe for use on the interrupt stack; it can be made 273 * higher to block network software interrupts after panics. 274 */ 275 int safepri; 276 277 /* 278 * General sleep call. Suspends the current process until a wakeup is 279 * performed on the specified identifier. The process will then be made 280 * runnable with the specified priority. Sleeps at most timo/hz seconds 281 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 282 * before and after sleeping, else signals are not checked. Returns 0 if 283 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 284 * signal needs to be delivered, ERESTART is returned if the current system 285 * call should be restarted if possible, and EINTR is returned if the system 286 * call should be interrupted by the signal (return EINTR). 287 */ 288 int 289 tsleep(ident, priority, wmesg, timo) 290 void *ident; 291 int priority, timo; 292 char *wmesg; 293 { 294 register struct proc *p = curproc; 295 register struct slpque *qp; 296 register s; 297 int sig, catch = priority & PCATCH; 298 extern int cold; 299 void endtsleep __P((void *)); 300 301 #ifdef KTRACE 302 if (KTRPOINT(p, KTR_CSW)) 303 ktrcsw(p->p_tracep, 1, 0); 304 #endif 305 s = splhigh(); 306 if (cold || panicstr) { 307 /* 308 * After a panic, or during autoconfiguration, 309 * just give interrupts a chance, then just return; 310 * don't run any other procs or panic below, 311 * in case this is the idle process and already asleep. 312 */ 313 splx(safepri); 314 splx(s); 315 return (0); 316 } 317 #ifdef DIAGNOSTIC 318 if (ident == NULL || p->p_stat != SRUN || p->p_back) 319 panic("tsleep"); 320 #endif 321 p->p_wchan = ident; 322 p->p_wmesg = wmesg; 323 p->p_slptime = 0; 324 p->p_priority = priority & PRIMASK; 325 qp = &slpque[LOOKUP(ident)]; 326 if (qp->sq_head == 0) 327 qp->sq_head = p; 328 else 329 *qp->sq_tailp = p; 330 *(qp->sq_tailp = &p->p_forw) = 0; 331 if (timo) 332 timeout(endtsleep, (void *)p, timo); 333 /* 334 * We put ourselves on the sleep queue and start our timeout 335 * before calling CURSIG, as we could stop there, and a wakeup 336 * or a SIGCONT (or both) could occur while we were stopped. 337 * A SIGCONT would cause us to be marked as SSLEEP 338 * without resuming us, thus we must be ready for sleep 339 * when CURSIG is called. If the wakeup happens while we're 340 * stopped, p->p_wchan will be 0 upon return from CURSIG. 341 */ 342 if (catch) { 343 p->p_flag |= P_SINTR; 344 if ((sig = CURSIG(p)) != 0) { 345 if (p->p_wchan) 346 unsleep(p); 347 p->p_stat = SRUN; 348 goto resume; 349 } 350 if (p->p_wchan == 0) { 351 catch = 0; 352 goto resume; 353 } 354 } else 355 sig = 0; 356 p->p_stat = SSLEEP; 357 p->p_stats->p_ru.ru_nvcsw++; 358 mi_switch(); 359 #ifdef DDB 360 /* handy breakpoint location after process "wakes" */ 361 asm(".globl bpendtsleep ; bpendtsleep:"); 362 #endif 363 resume: 364 curpriority = p->p_usrpri; 365 splx(s); 366 p->p_flag &= ~P_SINTR; 367 if (p->p_flag & P_TIMEOUT) { 368 p->p_flag &= ~P_TIMEOUT; 369 if (sig == 0) { 370 #ifdef KTRACE 371 if (KTRPOINT(p, KTR_CSW)) 372 ktrcsw(p->p_tracep, 0, 0); 373 #endif 374 return (EWOULDBLOCK); 375 } 376 } else if (timo) 377 untimeout(endtsleep, (void *)p); 378 if (catch && (sig != 0 || (sig = CURSIG(p)) != 0)) { 379 #ifdef KTRACE 380 if (KTRPOINT(p, KTR_CSW)) 381 ktrcsw(p->p_tracep, 0, 0); 382 #endif 383 if (p->p_sigacts->ps_sigintr & sigmask(sig)) 384 return (EINTR); 385 return (ERESTART); 386 } 387 #ifdef KTRACE 388 if (KTRPOINT(p, KTR_CSW)) 389 ktrcsw(p->p_tracep, 0, 0); 390 #endif 391 return (0); 392 } 393 394 /* 395 * Implement timeout for tsleep. 396 * If process hasn't been awakened (wchan non-zero), 397 * set timeout flag and undo the sleep. If proc 398 * is stopped, just unsleep so it will remain stopped. 399 */ 400 void 401 endtsleep(arg) 402 void *arg; 403 { 404 register struct proc *p; 405 int s; 406 407 p = (struct proc *)arg; 408 s = splhigh(); 409 if (p->p_wchan) { 410 if (p->p_stat == SSLEEP) 411 setrunnable(p); 412 else 413 unsleep(p); 414 p->p_flag |= P_TIMEOUT; 415 } 416 splx(s); 417 } 418 419 /* 420 * Short-term, non-interruptable sleep. 421 */ 422 void 423 sleep(ident, priority) 424 void *ident; 425 int priority; 426 { 427 register struct proc *p = curproc; 428 register struct slpque *qp; 429 register s; 430 extern int cold; 431 432 #ifdef DIAGNOSTIC 433 if (priority > PZERO) { 434 printf("sleep called with priority %d > PZERO, wchan: %p\n", 435 priority, ident); 436 panic("old sleep"); 437 } 438 #endif 439 s = splhigh(); 440 if (cold || panicstr) { 441 /* 442 * After a panic, or during autoconfiguration, 443 * just give interrupts a chance, then just return; 444 * don't run any other procs or panic below, 445 * in case this is the idle process and already asleep. 446 */ 447 splx(safepri); 448 splx(s); 449 return; 450 } 451 #ifdef DIAGNOSTIC 452 if (ident == NULL || p->p_stat != SRUN || p->p_back) 453 panic("sleep"); 454 #endif 455 p->p_wchan = ident; 456 p->p_wmesg = NULL; 457 p->p_slptime = 0; 458 p->p_priority = priority; 459 qp = &slpque[LOOKUP(ident)]; 460 if (qp->sq_head == 0) 461 qp->sq_head = p; 462 else 463 *qp->sq_tailp = p; 464 *(qp->sq_tailp = &p->p_forw) = 0; 465 p->p_stat = SSLEEP; 466 p->p_stats->p_ru.ru_nvcsw++; 467 #ifdef KTRACE 468 if (KTRPOINT(p, KTR_CSW)) 469 ktrcsw(p->p_tracep, 1, 0); 470 #endif 471 mi_switch(); 472 #ifdef DDB 473 /* handy breakpoint location after process "wakes" */ 474 asm(".globl bpendsleep ; bpendsleep:"); 475 #endif 476 #ifdef KTRACE 477 if (KTRPOINT(p, KTR_CSW)) 478 ktrcsw(p->p_tracep, 0, 0); 479 #endif 480 curpriority = p->p_usrpri; 481 splx(s); 482 } 483 484 /* 485 * Remove a process from its wait queue 486 */ 487 void 488 unsleep(p) 489 register struct proc *p; 490 { 491 register struct slpque *qp; 492 register struct proc **hp; 493 int s; 494 495 s = splhigh(); 496 if (p->p_wchan) { 497 hp = &(qp = &slpque[LOOKUP(p->p_wchan)])->sq_head; 498 while (*hp != p) 499 hp = &(*hp)->p_forw; 500 *hp = p->p_forw; 501 if (qp->sq_tailp == &p->p_forw) 502 qp->sq_tailp = hp; 503 p->p_wchan = 0; 504 } 505 splx(s); 506 } 507 508 /* 509 * Make all processes sleeping on the specified identifier runnable. 510 */ 511 void 512 wakeup(ident) 513 register void *ident; 514 { 515 register struct slpque *qp; 516 register struct proc *p, **q; 517 int s; 518 519 s = splhigh(); 520 qp = &slpque[LOOKUP(ident)]; 521 restart: 522 for (q = &qp->sq_head; (p = *q) != NULL; ) { 523 #ifdef DIAGNOSTIC 524 if (p->p_back || (p->p_stat != SSLEEP && p->p_stat != SSTOP)) 525 panic("wakeup"); 526 #endif 527 if (p->p_wchan == ident) { 528 p->p_wchan = 0; 529 *q = p->p_forw; 530 if (qp->sq_tailp == &p->p_forw) 531 qp->sq_tailp = q; 532 if (p->p_stat == SSLEEP) { 533 /* OPTIMIZED EXPANSION OF setrunnable(p); */ 534 if (p->p_slptime > 1) 535 updatepri(p); 536 p->p_slptime = 0; 537 p->p_stat = SRUN; 538 if (p->p_flag & P_INMEM) 539 setrunqueue(p); 540 /* 541 * Since curpriority is a user priority, 542 * p->p_priority is always better than 543 * curpriority. 544 */ 545 if ((p->p_flag & P_INMEM) == 0) 546 wakeup((caddr_t)&proc0); 547 else 548 need_resched(); 549 /* END INLINE EXPANSION */ 550 goto restart; 551 } 552 } else 553 q = &p->p_forw; 554 } 555 splx(s); 556 } 557 558 /* 559 * The machine independent parts of mi_switch(). 560 * Must be called at splstatclock() or higher. 561 */ 562 void 563 mi_switch() 564 { 565 register struct proc *p = curproc; /* XXX */ 566 register struct rlimit *rlim; 567 register long s, u; 568 struct timeval tv; 569 570 /* 571 * Compute the amount of time during which the current 572 * process was running, and add that to its total so far. 573 */ 574 microtime(&tv); 575 u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec); 576 s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec); 577 if (u < 0) { 578 u += 1000000; 579 s--; 580 } else if (u >= 1000000) { 581 u -= 1000000; 582 s++; 583 } 584 p->p_rtime.tv_usec = u; 585 p->p_rtime.tv_sec = s; 586 587 /* 588 * Check if the process exceeds its cpu resource allocation. 589 * If over max, kill it. In any case, if it has run for more 590 * than 10 minutes, reduce priority to give others a chance. 591 */ 592 rlim = &p->p_rlimit[RLIMIT_CPU]; 593 if (s >= rlim->rlim_cur) { 594 if (s >= rlim->rlim_max) 595 psignal(p, SIGKILL); 596 else { 597 psignal(p, SIGXCPU); 598 if (rlim->rlim_cur < rlim->rlim_max) 599 rlim->rlim_cur += 5; 600 } 601 } 602 if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) { 603 p->p_nice = NZERO + 4; 604 resetpriority(p); 605 } 606 607 /* 608 * Pick a new current process and record its start time. 609 */ 610 cnt.v_swtch++; 611 cpu_switch(p); 612 microtime(&runtime); 613 } 614 615 /* 616 * Initialize the (doubly-linked) run queues 617 * to be empty. 618 */ 619 void 620 rqinit() 621 { 622 register int i; 623 624 for (i = 0; i < NQS; i++) 625 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 626 } 627 628 /* 629 * Change process state to be runnable, 630 * placing it on the run queue if it is in memory, 631 * and awakening the swapper if it isn't in memory. 632 */ 633 void 634 setrunnable(p) 635 register struct proc *p; 636 { 637 register int s; 638 639 s = splhigh(); 640 switch (p->p_stat) { 641 case 0: 642 case SRUN: 643 case SZOMB: 644 default: 645 panic("setrunnable"); 646 case SSTOP: 647 /* 648 * If we're being traced (possibly because someone attached us 649 * while we were stopped), check for a signal from the debugger. 650 */ 651 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) 652 p->p_siglist |= sigmask(p->p_xstat); 653 case SSLEEP: 654 unsleep(p); /* e.g. when sending signals */ 655 break; 656 657 case SIDL: 658 break; 659 } 660 p->p_stat = SRUN; 661 if (p->p_flag & P_INMEM) 662 setrunqueue(p); 663 splx(s); 664 if (p->p_slptime > 1) 665 updatepri(p); 666 p->p_slptime = 0; 667 if ((p->p_flag & P_INMEM) == 0) 668 wakeup((caddr_t)&proc0); 669 else if (p->p_priority < curpriority) 670 need_resched(); 671 } 672 673 /* 674 * Compute the priority of a process when running in user mode. 675 * Arrange to reschedule if the resulting priority is better 676 * than that of the current process. 677 */ 678 void 679 resetpriority(p) 680 register struct proc *p; 681 { 682 register unsigned int newpriority; 683 684 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice; 685 newpriority = min(newpriority, MAXPRI); 686 p->p_usrpri = newpriority; 687 if (newpriority < curpriority) 688 need_resched(); 689 } 690 691 #ifdef DDB 692 #include <machine/db_machdep.h> 693 694 #include <ddb/db_interface.h> 695 #include <ddb/db_output.h> 696 697 void 698 db_show_all_procs(addr, haddr, count, modif) 699 db_expr_t addr; 700 int haddr; 701 db_expr_t count; 702 char *modif; 703 { 704 int map = modif[0] == 'm'; 705 int doingzomb = 0; 706 struct proc *p, *pp; 707 708 p = allproc.lh_first; 709 db_printf(" pid proc addr %s comm wchan\n", 710 map ? "map " : "uid ppid pgrp flag stat em "); 711 while (p != 0) { 712 pp = p->p_pptr; 713 if (p->p_stat) { 714 db_printf("%5d %p %p ", 715 p->p_pid, p, p->p_addr); 716 if (map) 717 db_printf("%p %s ", 718 p->p_vmspace, p->p_comm); 719 else 720 db_printf("%3d %5d %5d %06x %d %s %s ", 721 p->p_cred->p_ruid, pp ? pp->p_pid : -1, 722 p->p_pgrp->pg_id, p->p_flag, p->p_stat, 723 p->p_emul->e_name, p->p_comm); 724 if (p->p_wchan) { 725 if (p->p_wmesg) 726 db_printf("%s ", p->p_wmesg); 727 db_printf("%p", p->p_wchan); 728 } 729 db_printf("\n"); 730 } 731 p = p->p_list.le_next; 732 if (p == 0 && doingzomb == 0) { 733 doingzomb = 1; 734 p = zombproc.lh_first; 735 } 736 } 737 } 738 #endif 739