| 108 | } |
| 109 | |
| 110 | static int |
| 111 | thread_compare(struct thread *td, struct thread *td2) |
| 112 | { |
| 113 | int runa, runb; |
| 114 | int slpa, slpb; |
| 115 | fixpt_t esta, estb; |
| 116 | |
| 117 | if (td == NULL) |
| 118 | return (1); |
| 119 | |
| 120 | /* |
| 121 | * Fetch running stats, pctcpu usage, and interruptable flag. |
| 122 | */ |
| 123 | thread_lock(td); |
| 124 | runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td); |
| 125 | slpa = td->td_flags & TDF_SINTR; |
| 126 | esta = sched_pctcpu(td); |
| 127 | thread_unlock(td); |
| 128 | thread_lock(td2); |
| 129 | runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2); |
| 130 | estb = sched_pctcpu(td2); |
| 131 | slpb = td2->td_flags & TDF_SINTR; |
| 132 | thread_unlock(td2); |
| 133 | /* |
| 134 | * see if at least one of them is runnable |
| 135 | */ |
| 136 | switch (TESTAB(runa, runb)) { |
| 137 | case ONLYA: |
| 138 | return (0); |
| 139 | case ONLYB: |
| 140 | return (1); |
| 141 | case BOTH: |
| 142 | break; |
| 143 | } |
| 144 | /* |
| 145 | * favor one with highest recent cpu utilization |
| 146 | */ |
| 147 | if (estb > esta) |
| 148 | return (1); |
| 149 | if (esta > estb) |
| 150 | return (0); |
| 151 | /* |
| 152 | * favor one sleeping in a non-interruptible sleep |
| 153 | */ |
| 154 | switch (TESTAB(slpa, slpb)) { |
| 155 | case ONLYA: |
| 156 | return (0); |
| 157 | case ONLYB: |
| 158 | return (1); |
| 159 | case BOTH: |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return (td < td2); |
| 164 | } |
| 165 | |
| 166 | static int |
| 167 | proc_compare(struct proc *p1, struct proc *p2) |
no test coverage detected