1 /* kern_synch.c 6.8 85/06/02 */ 2 3 #include "../machine/pte.h" 4 5 #include "param.h" 6 #include "systm.h" 7 #include "dir.h" 8 #include "user.h" 9 #include "proc.h" 10 #include "file.h" 11 #include "inode.h" 12 #include "vm.h" 13 #include "kernel.h" 14 #include "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 /* fraction for digital decay to forget 90% of usage in 5*loadav sec */ 31 #define filter(loadav) ((2 * (loadav)) / (2 * (loadav) + 1)) 32 33 double ccpu = 0.95122942450071400909; /* exp(-1/20) */ 34 35 /* 36 * Recompute process priorities, once a second 37 */ 38 schedcpu() 39 { 40 register double ccpu1 = (1.0 - ccpu) / (double)hz; 41 register struct proc *p; 42 register int s, a; 43 float scale = filter(avenrun[0]); 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 /* 53 * If the process has slept the entire second, 54 * stop recalculating its priority until it wakes up. 55 */ 56 if (p->p_slptime > 1) { 57 p->p_pctcpu *= ccpu; 58 continue; 59 } 60 /* 61 * p_pctcpu is only for ps. 62 */ 63 p->p_pctcpu = ccpu * p->p_pctcpu + ccpu1 * p->p_cpticks; 64 p->p_cpticks = 0; 65 a = (int) (scale * (p->p_cpu & 0377)) + p->p_nice; 66 if (a < 0) 67 a = 0; 68 if (a > 255) 69 a = 255; 70 p->p_cpu = a; 71 (void) setpri(p); 72 s = splhigh(); /* prevent state changes */ 73 if (p->p_pri >= PUSER) { 74 #define PPQ (128 / NQS) 75 if ((p != u.u_procp || noproc) && 76 p->p_stat == SRUN && 77 (p->p_flag & SLOAD) && 78 (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) { 79 remrq(p); 80 p->p_pri = p->p_usrpri; 81 setrq(p); 82 } else 83 p->p_pri = p->p_usrpri; 84 } 85 splx(s); 86 } 87 vmmeter(); 88 if (runin!=0) { 89 runin = 0; 90 wakeup((caddr_t)&runin); 91 } 92 if (bclnlist != NULL) 93 wakeup((caddr_t)&proc[2]); 94 timeout(schedcpu, (caddr_t)0, hz); 95 } 96 97 /* 98 * Recalculate the priority of a process after it has slept for a while. 99 */ 100 updatepri(p) 101 register struct proc *p; 102 { 103 register int a = p->p_cpu & 0377; 104 float scale = filter(avenrun[0]); 105 106 p->p_slptime--; /* the first time was done in schedcpu */ 107 while (a && --p->p_slptime) 108 a = (int) (scale * a) /* + p->p_nice */; 109 if (a < 0) 110 a = 0; 111 if (a > 255) 112 a = 255; 113 p->p_cpu = a; 114 (void) setpri(p); 115 } 116 117 #define SQSIZE 0100 /* Must be power of 2 */ 118 #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 119 struct slpque { 120 struct proc *sq_head; 121 struct proc **sq_tailp; 122 } slpque[SQSIZE]; 123 124 /* 125 * Give up the processor till a wakeup occurs 126 * on chan, at which time the process 127 * enters the scheduling queue at priority pri. 128 * The most important effect of pri is that when 129 * pri<=PZERO a signal cannot disturb the sleep; 130 * if pri>PZERO signals will be processed. 131 * Callers of this routine must be prepared for 132 * premature return, and check that the reason for 133 * sleeping has gone away. 134 */ 135 sleep(chan, pri) 136 caddr_t chan; 137 int pri; 138 { 139 register struct proc *rp; 140 register struct slpque *qp; 141 register s; 142 143 rp = u.u_procp; 144 s = splhigh(); 145 if (panicstr) { 146 /* 147 * After a panic, just give interrupts a chance, 148 * then just return; don't run any other procs 149 * or panic below, in case this is the idle process 150 * and already asleep. 151 * The splnet should be spl0 if the network was being used 152 * by the filesystem, but for now avoid network interrupts 153 * that might cause another panic. 154 */ 155 (void) splnet(); 156 splx(s); 157 return; 158 } 159 if (chan==0 || rp->p_stat != SRUN || rp->p_rlink) 160 panic("sleep"); 161 rp->p_wchan = chan; 162 rp->p_slptime = 0; 163 rp->p_pri = pri; 164 qp = &slpque[HASH(chan)]; 165 if (qp->sq_head == 0) 166 qp->sq_head = rp; 167 else 168 *qp->sq_tailp = rp; 169 *(qp->sq_tailp = &rp->p_link) = 0; 170 if (pri > PZERO) { 171 /* 172 * If we stop in issig(), wakeup may already have happened 173 * when we return (rp->p_wchan will then be 0). 174 */ 175 if (ISSIG(rp)) { 176 if (rp->p_wchan) 177 unsleep(rp); 178 rp->p_stat = SRUN; 179 (void) spl0(); 180 goto psig; 181 } 182 if (rp->p_wchan == 0) 183 goto out; 184 rp->p_stat = SSLEEP; 185 (void) spl0(); 186 u.u_ru.ru_nvcsw++; 187 swtch(); 188 if (ISSIG(rp)) 189 goto psig; 190 } else { 191 rp->p_stat = SSLEEP; 192 (void) spl0(); 193 u.u_ru.ru_nvcsw++; 194 swtch(); 195 } 196 curpri = rp->p_usrpri; 197 out: 198 splx(s); 199 return; 200 201 /* 202 * If priority was low (>PZERO) and 203 * there has been a signal, execute non-local goto through 204 * u.u_qsave, aborting the system call in progress (see trap.c) 205 */ 206 psig: 207 longjmp(&u.u_qsave); 208 /*NOTREACHED*/ 209 } 210 211 /* 212 * Remove a process from its wait queue 213 */ 214 unsleep(p) 215 register struct proc *p; 216 { 217 register struct slpque *qp; 218 register struct proc **hp; 219 int s; 220 221 s = splhigh(); 222 if (p->p_wchan) { 223 hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head; 224 while (*hp != p) 225 hp = &(*hp)->p_link; 226 *hp = p->p_link; 227 if (qp->sq_tailp == &p->p_link) 228 qp->sq_tailp = hp; 229 p->p_wchan = 0; 230 } 231 splx(s); 232 } 233 234 /* 235 * Wake up all processes sleeping on chan. 236 */ 237 wakeup(chan) 238 register caddr_t chan; 239 { 240 register struct slpque *qp; 241 register struct proc *p, **q; 242 int s; 243 244 s = splhigh(); 245 qp = &slpque[HASH(chan)]; 246 restart: 247 for (q = &qp->sq_head; p = *q; ) { 248 if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 249 panic("wakeup"); 250 if (p->p_wchan==chan) { 251 p->p_wchan = 0; 252 *q = p->p_link; 253 if (qp->sq_tailp == &p->p_link) 254 qp->sq_tailp = q; 255 p->p_slptime = 0; 256 if (p->p_stat == SSLEEP) { 257 /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 258 if (p->p_slptime > 1) 259 updatepri(p); 260 p->p_stat = SRUN; 261 if (p->p_flag & SLOAD) 262 setrq(p); 263 /* 264 * Since curpri is a usrpri, 265 * p->p_pri is always better than curpri. 266 */ 267 runrun++; 268 aston(); 269 if ((p->p_flag&SLOAD) == 0) { 270 if (runout != 0) { 271 runout = 0; 272 wakeup((caddr_t)&runout); 273 } 274 wantin++; 275 } 276 /* END INLINE EXPANSION */ 277 goto restart; 278 } 279 } else 280 q = &p->p_link; 281 } 282 splx(s); 283 } 284 285 /* 286 * Initialize the (doubly-linked) run queues 287 * to be empty. 288 */ 289 rqinit() 290 { 291 register int i; 292 293 for (i = 0; i < NQS; i++) 294 qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 295 } 296 297 /* 298 * Set the process running; 299 * arrange for it to be swapped in if necessary. 300 */ 301 setrun(p) 302 register struct proc *p; 303 { 304 register int s; 305 306 s = splhigh(); 307 switch (p->p_stat) { 308 309 case 0: 310 case SWAIT: 311 case SRUN: 312 case SZOMB: 313 default: 314 panic("setrun"); 315 316 case SSTOP: 317 case SSLEEP: 318 unsleep(p); /* e.g. when sending signals */ 319 break; 320 321 case SIDL: 322 break; 323 } 324 if (p->p_slptime > 1) 325 updatepri(p); 326 p->p_stat = SRUN; 327 if (p->p_flag & SLOAD) 328 setrq(p); 329 splx(s); 330 if (p->p_pri < curpri) { 331 runrun++; 332 aston(); 333 } 334 if ((p->p_flag&SLOAD) == 0) { 335 if (runout != 0) { 336 runout = 0; 337 wakeup((caddr_t)&runout); 338 } 339 wantin++; 340 } 341 } 342 343 /* 344 * Set user priority. 345 * The rescheduling flag (runrun) 346 * is set if the priority is better 347 * than the currently running process. 348 */ 349 setpri(pp) 350 register struct proc *pp; 351 { 352 register int p; 353 354 p = (pp->p_cpu & 0377)/4; 355 p += PUSER + 2 * pp->p_nice; 356 if (pp->p_rssize > pp->p_maxrss && freemem < desfree) 357 p += 2*4; /* effectively, nice(4) */ 358 if (p > 127) 359 p = 127; 360 if (p < curpri) { 361 runrun++; 362 aston(); 363 } 364 pp->p_usrpri = p; 365 return (p); 366 } 367