- register struct proc *rp, *p, *inp;
- int outpri, inpri, rppri;
- int sleeper, desperate, deservin, needs, divisor;
- register struct bigp *bp, *nbp;
- int biggot, gives;
-
-loop:
- wantin = 0;
- deservin = 0;
- sleeper = 0;
- p = 0;
- /*
- * See if paging system is overloaded; if so swap someone out.
- * Conditions for hard outswap are:
- * if need kernel map (mix it up).
- * or
- * 1. if there are at least 2 runnable processes (on the average)
- * and 2. the paging rate is excessive or memory is now VERY low.
- * and 3. the short (5-second) and longer (30-second) average
- * memory is less than desirable.
- */
- if (kmapwnt ||
- (avenrun[0] >= 2 && imax(avefree, avefree30) < desfree &&
- (rate.v_pgin + rate.v_pgout > maxpgio || avefree < minfree))) {
- desperate = 1;
- goto hardswap;
- }
- desperate = 0;
- /*
- * Not desperate for core,
- * look for someone who deserves to be brought in.
- */
- outpri = -20000;
- for (rp = allproc; rp != NULL; rp = rp->p_nxt) switch(rp->p_stat) {
-
- case SRUN:
- if ((rp->p_flag&SLOAD) == 0) {
- rppri = rp->p_time -
- rp->p_swrss / nz((maxpgio/2) * (klin * CLSIZE)) +
- rp->p_slptime - (rp->p_nice-NZERO)*8;
- if (rppri > outpri) {
- if (rp->p_poip)
- continue;
- if (rp->p_textp && rp->p_textp->x_poip)
- continue;
- p = rp;
- outpri = rppri;
- }
- }
- continue;
-
- case SSLEEP:
- case SSTOP:
- if ((freemem < desfree || rp->p_rssize == 0) &&
- rp->p_slptime > maxslp &&
- (!rp->p_textp || (rp->p_textp->x_flag&XLOCK)==0) &&
- swappable(rp)) {
- /*
- * Kick out deadwood.
- */
- rp->p_flag &= ~SLOAD;
- (void) swapout(rp, rp->p_dsize, rp->p_ssize);
- goto loop;
- }
- continue;
- }
-
- /*
- * No one wants in, so nothing to do.
- */
- if (outpri == -20000) {
- (void) splhigh();
- if (wantin) {
- wantin = 0;
- sleep((caddr_t)&lbolt, PSWP);
- } else {
- runout++;
- sleep((caddr_t)&runout, PSWP);
- }
- (void) spl0();
- goto loop;
- }
- /*
- * Decide how deserving this guy is. If he is deserving
- * we will be willing to work harder to bring him in.
- * Needs is an estimate of how much core he will need.
- * If he has been out for a while, then we will
- * bring him in with 1/2 the core he will need, otherwise
- * we are conservative.
- */
- deservin = 0;
- divisor = 1;
- if (outpri > maxslp/2) {
- deservin = 1;
- divisor = 2;
- }
- needs = p->p_swrss;
- if (p->p_textp && p->p_textp->x_ccount == 0)
- needs += p->p_textp->x_swrss;
- needs = imin(needs, lotsfree);
- if (freemem - deficit > needs / divisor) {
- deficit += needs;
- if (swapin(p))
- goto loop;
- deficit -= imin(needs, deficit);
- }