| 164 | } |
| 165 | |
| 166 | static int |
| 167 | proc_compare(struct proc *p1, struct proc *p2) |
| 168 | { |
| 169 | |
| 170 | int runa, runb; |
| 171 | fixpt_t esta, estb; |
| 172 | |
| 173 | if (p1 == NULL) |
| 174 | return (1); |
| 175 | |
| 176 | /* |
| 177 | * Fetch various stats about these processes. After we drop the |
| 178 | * lock the information could be stale but the race is unimportant. |
| 179 | */ |
| 180 | PROC_LOCK(p1); |
| 181 | runa = proc_sum(p1, &esta); |
| 182 | PROC_UNLOCK(p1); |
| 183 | PROC_LOCK(p2); |
| 184 | runb = proc_sum(p2, &estb); |
| 185 | PROC_UNLOCK(p2); |
| 186 | |
| 187 | /* |
| 188 | * see if at least one of them is runnable |
| 189 | */ |
| 190 | switch (TESTAB(runa, runb)) { |
| 191 | case ONLYA: |
| 192 | return (0); |
| 193 | case ONLYB: |
| 194 | return (1); |
| 195 | case BOTH: |
| 196 | break; |
| 197 | } |
| 198 | /* |
| 199 | * favor one with highest recent cpu utilization |
| 200 | */ |
| 201 | if (estb > esta) |
| 202 | return (1); |
| 203 | if (esta > estb) |
| 204 | return (0); |
| 205 | /* |
| 206 | * weed out zombies |
| 207 | */ |
| 208 | switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) { |
| 209 | case ONLYA: |
| 210 | return (1); |
| 211 | case ONLYB: |
| 212 | return (0); |
| 213 | case BOTH: |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | sbuf_tty_drain(void *a, const char *d, int len) |