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