123376Smckusick /* 229096Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323376Smckusick * All rights reserved. The Berkeley software License Agreement 423376Smckusick * specifies the terms and conditions for redistribution. 523376Smckusick * 6*30532Skarels * @(#)kern_synch.c 7.4 (Berkeley) 02/19/87 723376Smckusick */ 833Sbill 99756Ssam #include "../machine/pte.h" 1029946Skarels #include "../machine/psl.h" 1129946Skarels #include "../machine/mtpr.h" 129756Ssam 1317093Sbloom #include "param.h" 1417093Sbloom #include "systm.h" 1517093Sbloom #include "dir.h" 1617093Sbloom #include "user.h" 1717093Sbloom #include "proc.h" 1817093Sbloom #include "file.h" 1917093Sbloom #include "inode.h" 2017093Sbloom #include "vm.h" 2117093Sbloom #include "kernel.h" 2217093Sbloom #include "buf.h" 239756Ssam 248102Sroot /* 258102Sroot * Force switch among equal priority processes every 100ms. 268102Sroot */ 278102Sroot roundrobin() 288102Sroot { 298102Sroot 308102Sroot runrun++; 318102Sroot aston(); 328624Sroot timeout(roundrobin, (caddr_t)0, hz / 10); 338102Sroot } 348102Sroot 3517541Skarels /* fraction for digital decay to forget 90% of usage in 5*loadav sec */ 3617541Skarels #define filter(loadav) ((2 * (loadav)) / (2 * (loadav) + 1)) 3717541Skarels 388102Sroot double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 398102Sroot 408102Sroot /* 418102Sroot * Recompute process priorities, once a second 428102Sroot */ 438102Sroot schedcpu() 448102Sroot { 4516795Skarels register double ccpu1 = (1.0 - ccpu) / (double)hz; 468102Sroot register struct proc *p; 478102Sroot register int s, a; 4817541Skarels float scale = filter(avenrun[0]); 498102Sroot 508102Sroot wakeup((caddr_t)&lbolt); 5116532Skarels for (p = allproc; p != NULL; p = p->p_nxt) { 528102Sroot if (p->p_time != 127) 538102Sroot p->p_time++; 548102Sroot if (p->p_stat==SSLEEP || p->p_stat==SSTOP) 558102Sroot if (p->p_slptime != 127) 568102Sroot p->p_slptime++; 5717541Skarels /* 5817541Skarels * If the process has slept the entire second, 5917541Skarels * stop recalculating its priority until it wakes up. 6017541Skarels */ 6117541Skarels if (p->p_slptime > 1) { 6217541Skarels p->p_pctcpu *= ccpu; 6317541Skarels continue; 6417541Skarels } 6517541Skarels /* 6617541Skarels * p_pctcpu is only for ps. 6717541Skarels */ 6817541Skarels p->p_pctcpu = ccpu * p->p_pctcpu + ccpu1 * p->p_cpticks; 698102Sroot p->p_cpticks = 0; 7017541Skarels a = (int) (scale * (p->p_cpu & 0377)) + p->p_nice; 718102Sroot if (a < 0) 728102Sroot a = 0; 738102Sroot if (a > 255) 748102Sroot a = 255; 758102Sroot p->p_cpu = a; 768102Sroot (void) setpri(p); 7717541Skarels s = splhigh(); /* prevent state changes */ 788102Sroot if (p->p_pri >= PUSER) { 7916795Skarels #define PPQ (128 / NQS) 808102Sroot if ((p != u.u_procp || noproc) && 818102Sroot p->p_stat == SRUN && 828102Sroot (p->p_flag & SLOAD) && 8316795Skarels (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) { 848102Sroot remrq(p); 858102Sroot p->p_pri = p->p_usrpri; 868102Sroot setrq(p); 878102Sroot } else 888102Sroot p->p_pri = p->p_usrpri; 898102Sroot } 908102Sroot splx(s); 918102Sroot } 928102Sroot vmmeter(); 938102Sroot if (runin!=0) { 948102Sroot runin = 0; 958102Sroot wakeup((caddr_t)&runin); 968102Sroot } 978102Sroot if (bclnlist != NULL) 988102Sroot wakeup((caddr_t)&proc[2]); 998624Sroot timeout(schedcpu, (caddr_t)0, hz); 1008102Sroot } 1018102Sroot 10217541Skarels /* 10317541Skarels * Recalculate the priority of a process after it has slept for a while. 10417541Skarels */ 10517541Skarels updatepri(p) 10617541Skarels register struct proc *p; 10717541Skarels { 10817541Skarels register int a = p->p_cpu & 0377; 10917541Skarels float scale = filter(avenrun[0]); 11017541Skarels 11117541Skarels p->p_slptime--; /* the first time was done in schedcpu */ 11217541Skarels while (a && --p->p_slptime) 11317541Skarels a = (int) (scale * a) /* + p->p_nice */; 11430232Skarels p->p_slptime = 0; 11517541Skarels if (a < 0) 11617541Skarels a = 0; 11717541Skarels if (a > 255) 11817541Skarels a = 255; 11917541Skarels p->p_cpu = a; 12017541Skarels (void) setpri(p); 12117541Skarels } 12217541Skarels 12333Sbill #define SQSIZE 0100 /* Must be power of 2 */ 12433Sbill #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 12521099Smckusick struct slpque { 12621099Smckusick struct proc *sq_head; 12721099Smckusick struct proc **sq_tailp; 12821099Smckusick } slpque[SQSIZE]; 12933Sbill 13033Sbill /* 13133Sbill * Give up the processor till a wakeup occurs 13233Sbill * on chan, at which time the process 13333Sbill * enters the scheduling queue at priority pri. 13433Sbill * The most important effect of pri is that when 13533Sbill * pri<=PZERO a signal cannot disturb the sleep; 13633Sbill * if pri>PZERO signals will be processed. 13733Sbill * Callers of this routine must be prepared for 13833Sbill * premature return, and check that the reason for 13933Sbill * sleeping has gone away. 14033Sbill */ 14133Sbill sleep(chan, pri) 1428033Sroot caddr_t chan; 1438033Sroot int pri; 14433Sbill { 14521099Smckusick register struct proc *rp; 14621099Smckusick register struct slpque *qp; 147207Sbill register s; 148*30532Skarels extern int cold; 14933Sbill 15033Sbill rp = u.u_procp; 15117541Skarels s = splhigh(); 152*30532Skarels if (cold || panicstr) { 15318363Skarels /* 154*30532Skarels * After a panic, or during autoconfiguration, 155*30532Skarels * just give interrupts a chance, then just return; 156*30532Skarels * don't run any other procs or panic below, 157*30532Skarels * in case this is the idle process and already asleep. 15818363Skarels * The splnet should be spl0 if the network was being used 15918363Skarels * by the filesystem, but for now avoid network interrupts 16018363Skarels * that might cause another panic. 16118363Skarels */ 16218363Skarels (void) splnet(); 16318363Skarels splx(s); 16418363Skarels return; 16518363Skarels } 16618363Skarels if (chan==0 || rp->p_stat != SRUN || rp->p_rlink) 16733Sbill panic("sleep"); 16833Sbill rp->p_wchan = chan; 16933Sbill rp->p_slptime = 0; 17033Sbill rp->p_pri = pri; 17121099Smckusick qp = &slpque[HASH(chan)]; 17221099Smckusick if (qp->sq_head == 0) 17321099Smckusick qp->sq_head = rp; 17421099Smckusick else 17521099Smckusick *qp->sq_tailp = rp; 17621099Smckusick *(qp->sq_tailp = &rp->p_link) = 0; 1774826Swnj if (pri > PZERO) { 17821763Skarels /* 17921763Skarels * If we stop in issig(), wakeup may already have happened 18021763Skarels * when we return (rp->p_wchan will then be 0). 18121763Skarels */ 1824826Swnj if (ISSIG(rp)) { 183187Sbill if (rp->p_wchan) 184187Sbill unsleep(rp); 18533Sbill rp->p_stat = SRUN; 186131Sbill (void) spl0(); 18733Sbill goto psig; 18833Sbill } 189187Sbill if (rp->p_wchan == 0) 190187Sbill goto out; 191187Sbill rp->p_stat = SSLEEP; 192131Sbill (void) spl0(); 1938033Sroot u.u_ru.ru_nvcsw++; 19433Sbill swtch(); 1954826Swnj if (ISSIG(rp)) 19633Sbill goto psig; 19733Sbill } else { 198207Sbill rp->p_stat = SSLEEP; 199131Sbill (void) spl0(); 2008033Sroot u.u_ru.ru_nvcsw++; 20133Sbill swtch(); 20233Sbill } 20316795Skarels curpri = rp->p_usrpri; 204187Sbill out: 20533Sbill splx(s); 20633Sbill return; 20733Sbill 20833Sbill /* 20933Sbill * If priority was low (>PZERO) and 2104826Swnj * there has been a signal, execute non-local goto through 2118113Sroot * u.u_qsave, aborting the system call in progress (see trap.c) 21233Sbill */ 21333Sbill psig: 2148113Sroot longjmp(&u.u_qsave); 21533Sbill /*NOTREACHED*/ 21633Sbill } 21733Sbill 21833Sbill /* 219181Sbill * Remove a process from its wait queue 220181Sbill */ 221181Sbill unsleep(p) 2224826Swnj register struct proc *p; 223181Sbill { 22421099Smckusick register struct slpque *qp; 225181Sbill register struct proc **hp; 22621099Smckusick int s; 227181Sbill 22817541Skarels s = splhigh(); 229181Sbill if (p->p_wchan) { 23021099Smckusick hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head; 231181Sbill while (*hp != p) 232181Sbill hp = &(*hp)->p_link; 233181Sbill *hp = p->p_link; 23421099Smckusick if (qp->sq_tailp == &p->p_link) 23521099Smckusick qp->sq_tailp = hp; 236181Sbill p->p_wchan = 0; 237181Sbill } 238181Sbill splx(s); 239181Sbill } 240181Sbill 241181Sbill /* 24233Sbill * Wake up all processes sleeping on chan. 24333Sbill */ 24433Sbill wakeup(chan) 2454826Swnj register caddr_t chan; 24633Sbill { 24721099Smckusick register struct slpque *qp; 24821099Smckusick register struct proc *p, **q; 24933Sbill int s; 25033Sbill 25117541Skarels s = splhigh(); 25221099Smckusick qp = &slpque[HASH(chan)]; 25333Sbill restart: 25421099Smckusick for (q = &qp->sq_head; p = *q; ) { 255181Sbill if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 25633Sbill panic("wakeup"); 257207Sbill if (p->p_wchan==chan) { 25833Sbill p->p_wchan = 0; 259187Sbill *q = p->p_link; 26021099Smckusick if (qp->sq_tailp == &p->p_link) 26121099Smckusick qp->sq_tailp = q; 262181Sbill if (p->p_stat == SSLEEP) { 263181Sbill /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 26421763Skarels if (p->p_slptime > 1) 26521763Skarels updatepri(p); 266181Sbill p->p_stat = SRUN; 2672702Swnj if (p->p_flag & SLOAD) 268181Sbill setrq(p); 26916795Skarels /* 27016795Skarels * Since curpri is a usrpri, 27116795Skarels * p->p_pri is always better than curpri. 27216795Skarels */ 27316795Skarels runrun++; 27416795Skarels aston(); 2753545Swnj if ((p->p_flag&SLOAD) == 0) { 2763545Swnj if (runout != 0) { 2773545Swnj runout = 0; 2783545Swnj wakeup((caddr_t)&runout); 2793545Swnj } 2803545Swnj wantin++; 281181Sbill } 282181Sbill /* END INLINE EXPANSION */ 283187Sbill goto restart; 28433Sbill } 285187Sbill } else 286187Sbill q = &p->p_link; 28733Sbill } 28833Sbill splx(s); 28933Sbill } 29033Sbill 29133Sbill /* 29233Sbill * Initialize the (doubly-linked) run queues 29333Sbill * to be empty. 29433Sbill */ 29533Sbill rqinit() 29633Sbill { 29733Sbill register int i; 29833Sbill 29933Sbill for (i = 0; i < NQS; i++) 30033Sbill qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 30133Sbill } 30233Sbill 30333Sbill /* 30433Sbill * Set the process running; 30533Sbill * arrange for it to be swapped in if necessary. 30633Sbill */ 30733Sbill setrun(p) 3084826Swnj register struct proc *p; 30933Sbill { 3104826Swnj register int s; 31133Sbill 31217541Skarels s = splhigh(); 31333Sbill switch (p->p_stat) { 31433Sbill 31533Sbill case 0: 31633Sbill case SWAIT: 31733Sbill case SRUN: 31833Sbill case SZOMB: 31933Sbill default: 32033Sbill panic("setrun"); 32133Sbill 322207Sbill case SSTOP: 32333Sbill case SSLEEP: 324181Sbill unsleep(p); /* e.g. when sending signals */ 32533Sbill break; 32633Sbill 32733Sbill case SIDL: 32833Sbill break; 32933Sbill } 33033Sbill p->p_stat = SRUN; 33133Sbill if (p->p_flag & SLOAD) 33233Sbill setrq(p); 33333Sbill splx(s); 33430232Skarels if (p->p_slptime > 1) 33530232Skarels updatepri(p); 3364826Swnj if (p->p_pri < curpri) { 33733Sbill runrun++; 3382443Swnj aston(); 3392443Swnj } 3403545Swnj if ((p->p_flag&SLOAD) == 0) { 3414826Swnj if (runout != 0) { 3423545Swnj runout = 0; 3433545Swnj wakeup((caddr_t)&runout); 3443545Swnj } 3453545Swnj wantin++; 34633Sbill } 34733Sbill } 34833Sbill 34933Sbill /* 35033Sbill * Set user priority. 35133Sbill * The rescheduling flag (runrun) 35233Sbill * is set if the priority is better 35333Sbill * than the currently running process. 35433Sbill */ 35533Sbill setpri(pp) 3564826Swnj register struct proc *pp; 35733Sbill { 3584826Swnj register int p; 35933Sbill 3603875Swnj p = (pp->p_cpu & 0377)/4; 36117541Skarels p += PUSER + 2 * pp->p_nice; 3623530Swnj if (pp->p_rssize > pp->p_maxrss && freemem < desfree) 3633530Swnj p += 2*4; /* effectively, nice(4) */ 3644826Swnj if (p > 127) 36533Sbill p = 127; 3664826Swnj if (p < curpri) { 36733Sbill runrun++; 3682453Swnj aston(); 3692453Swnj } 37033Sbill pp->p_usrpri = p; 3714826Swnj return (p); 37233Sbill } 373