1 /* kern_synch.c 6.3 84/07/31 */ 2 3 #include "../machine/pte.h" 4 5 #include "../h/param.h" 6 #include "../h/systm.h" 7 #include "../h/dir.h" 8 #include "../h/user.h" 9 #include "../h/proc.h" 10 #include "../h/file.h" 11 #include "../h/inode.h" 12 #include "../h/vm.h" 13 #include "../h/kernel.h" 14 #include "../h/buf.h" 15 16 #ifdef vax 17 #include "../vax/mtpr.h" /* XXX */ 18 #endif 19 /* 20 * Force switch among equal priority processes every 100ms. 21 */ 22 roundrobin() 23 { 24 25 runrun++; 26 aston(); 27 timeout(roundrobin, (caddr_t)0, hz / 10); 28 } 29 30 /* constants to digital decay and forget 90% of usage in 5*loadav time */ 31 #undef ave 32 #define ave(a,b) ((int)(((int)(a*b))/(b+1))) 33 int nrscale = 2; 34 double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 35 36 /* 37 * Recompute process priorities, once a second 38 */ 39 schedcpu() 40 { 41 register double ccpu1 = (1.0 - ccpu) / (double)hz; 42 register struct proc *p; 43 register int s, a; 44 45 wakeup((caddr_t)&lbolt); 46 for (p = allproc; p != NULL; p = p->p_nxt) { 47 if (p->p_time != 127) 48 p->p_time++; 49 if (p->p_stat==SSLEEP || p->p_stat==SSTOP) 50 if (p->p_slptime != 127) 51 p->p_slptime++; 52 if (p->p_flag&SLOAD) 53 p->p_pctcpu = ccpu * p->p_pctcpu + ccpu1 * p->p_cpticks; 54 p->p_cpticks = 0; 55 a = ave((p->p_cpu & 0377), avenrun[0]*nrscale) + 56 p->p_nice - NZERO; 57 if (a < 0) 58 a = 0; 59 if (a > 255) 60 a = 255; 61 p->p_cpu = a; 62 (void) setpri(p); 63 s = spl6(); /* prevent state changes */ 64 if (p->p_pri >= PUSER) { 65 #define PPQ (128 / NQS) 66 if ((p != u.u_procp || noproc) && 67 p->p_stat == SRUN && 68 (p->p_flag & SLOAD) && 69 (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) { 70 remrq(p); 71 p->p_pri = p->p_usrpri; 72 setrq(p); 73 } else 74 p->p_pri = p->p_usrpri; 75 } 76 splx(s); 77 } 78 vmmeter(); 79 if (runin!=0) { 80 runin = 0; 81 wakeup((caddr_t)&runin); 82 } 83 if (bclnlist != NULL) 84 wakeup((caddr_t)&proc[2]); 85 timeout(schedcpu, (caddr_t)0, hz); 86 } 87 88 #define SQSIZE 0100 /* Must be power of 2 */ 89 #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 90 struct proc *slpque[SQSIZE]; 91 92 /* 93 * Give up the processor till a wakeup occurs 94 * on chan, at which time the process 95 * enters the scheduling queue at priority pri. 96 * The most important effect of pri is that when 97 * pri<=PZERO a signal cannot disturb the sleep; 98 * if pri>PZERO signals will be processed. 99 * Callers of this routine must be prepared for 100 * premature return, and check that the reason for 101 * sleeping has gone away. 102 */ 103 sleep(chan, pri) 104 caddr_t chan; 105 int pri; 106 { 107 register struct proc *rp, **hp; 108 register s; 109 110 rp = u.u_procp; 111 s = spl6(); 112 if (chan==0 || rp->p_stat != SRUN || rp->p_rlink) 113 panic("sleep"); 114 rp->p_wchan = chan; 115 rp->p_slptime = 0; 116 rp->p_pri = pri; 117 hp = &slpque[HASH(chan)]; 118 rp->p_link = *hp; 119 *hp = rp; 120 if (pri > PZERO) { 121 if (ISSIG(rp)) { 122 if (rp->p_wchan) 123 unsleep(rp); 124 rp->p_stat = SRUN; 125 (void) spl0(); 126 goto psig; 127 } 128 if (rp->p_wchan == 0) 129 goto out; 130 rp->p_stat = SSLEEP; 131 (void) spl0(); 132 u.u_ru.ru_nvcsw++; 133 swtch(); 134 if (ISSIG(rp)) 135 goto psig; 136 } else { 137 rp->p_stat = SSLEEP; 138 (void) spl0(); 139 u.u_ru.ru_nvcsw++; 140 swtch(); 141 } 142 curpri = rp->p_usrpri; 143 out: 144 splx(s); 145 return; 146 147 /* 148 * If priority was low (>PZERO) and 149 * there has been a signal, execute non-local goto through 150 * u.u_qsave, aborting the system call in progress (see trap.c) 151 * (or finishing a tsleep, see below) 152 */ 153 psig: 154 longjmp(&u.u_qsave); 155 /*NOTREACHED*/ 156 } 157 158 /* 159 * Remove a process from its wait queue 160 */ 161 unsleep(p) 162 register struct proc *p; 163 { 164 register struct proc **hp; 165 register s; 166 167 s = spl6(); 168 if (p->p_wchan) { 169 hp = &slpque[HASH(p->p_wchan)]; 170 while (*hp != p) 171 hp = &(*hp)->p_link; 172 *hp = p->p_link; 173 p->p_wchan = 0; 174 } 175 splx(s); 176 } 177 178 /* 179 * Wake up all processes sleeping on chan. 180 */ 181 wakeup(chan) 182 register caddr_t chan; 183 { 184 register struct proc *p, **q, **h; 185 int s; 186 187 s = spl6(); 188 h = &slpque[HASH(chan)]; 189 restart: 190 for (q = h; p = *q; ) { 191 if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 192 panic("wakeup"); 193 if (p->p_wchan==chan) { 194 p->p_wchan = 0; 195 *q = p->p_link; 196 p->p_slptime = 0; 197 if (p->p_stat == SSLEEP) { 198 /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 199 p->p_stat = SRUN; 200 if (p->p_flag & SLOAD) 201 setrq(p); 202 /* 203 * Since curpri is a usrpri, 204 * p->p_pri is always better than curpri. 205 */ 206 runrun++; 207 aston(); 208 if ((p->p_flag&SLOAD) == 0) { 209 if (runout != 0) { 210 runout = 0; 211 wakeup((caddr_t)&runout); 212 } 213 wantin++; 214 } 215 /* END INLINE EXPANSION */ 216 goto restart; 217 } 218 } else 219 q = &p->p_link; 220 } 221 splx(s); 222 } 223 224 /* 225 * Initialize the (doubly-linked) run queues 226 * to be empty. 227 */ 228 rqinit() 229 { 230 register int i; 231 232 for (i = 0; i < NQS; i++) 233 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 234 } 235 236 /* 237 * Set the process running; 238 * arrange for it to be swapped in if necessary. 239 */ 240 setrun(p) 241 register struct proc *p; 242 { 243 register int s; 244 245 s = spl6(); 246 switch (p->p_stat) { 247 248 case 0: 249 case SWAIT: 250 case SRUN: 251 case SZOMB: 252 default: 253 panic("setrun"); 254 255 case SSTOP: 256 case SSLEEP: 257 unsleep(p); /* e.g. when sending signals */ 258 break; 259 260 case SIDL: 261 break; 262 } 263 p->p_stat = SRUN; 264 if (p->p_flag & SLOAD) 265 setrq(p); 266 splx(s); 267 if (p->p_pri < curpri) { 268 runrun++; 269 aston(); 270 } 271 if ((p->p_flag&SLOAD) == 0) { 272 if (runout != 0) { 273 runout = 0; 274 wakeup((caddr_t)&runout); 275 } 276 wantin++; 277 } 278 } 279 280 /* 281 * Set user priority. 282 * The rescheduling flag (runrun) 283 * is set if the priority is better 284 * than the currently running process. 285 */ 286 setpri(pp) 287 register struct proc *pp; 288 { 289 register int p; 290 291 p = (pp->p_cpu & 0377)/4; 292 p += PUSER + 2*(pp->p_nice - NZERO); 293 if (pp->p_rssize > pp->p_maxrss && freemem < desfree) 294 p += 2*4; /* effectively, nice(4) */ 295 if (p > 127) 296 p = 127; 297 if (p < curpri) { 298 runrun++; 299 aston(); 300 } 301 pp->p_usrpri = p; 302 return (p); 303 } 304