calc_load() is called by timer interrupt to update avenrun[]. It currently calls nr_active() at each timer tick (HZ per second), while the update of avenrun[] is done only once every 5 seconds. (LOAD_FREQ=5 Hz) nr_active() is quite expensive on SMP machines, since it has to sum up nr_running and nr_uninterruptible of all online CPUS, bringing foreign dirty cache lines. This patch is an optimization of calc_load() so that nr_active() is called only if we need it. The use of unlikely() is welcome since the condition is true only once every 5*HZ time.
Unified · Split
| @@ -1146,11 +1146,15 @@ static inline void calc_load(unsigned long ticks) · 1145 unmodified lines | ||
| 1146 | 1146 | unsigned long active_tasks; /* fixed-point */ |
| 1147 | 1147 | static int count = LOAD_FREQ; |
| 1148 | 1148 | |
| 1149 | − active_tasks = count_active_tasks(); | |
| 1150 | − for (count -= ticks; count < 0; count += LOAD_FREQ) { | |
| 1151 | − CALC_LOAD(avenrun[0], EXP_1, active_tasks); | |
| 1152 | − CALC_LOAD(avenrun[1], EXP_5, active_tasks); | |
| 1153 | − CALC_LOAD(avenrun[2], EXP_15, active_tasks); | |
| 1149 | + count -= ticks; | |
| 1150 | + if (unlikely(count < 0)) { | |
| 1151 | + active_tasks = count_active_tasks(); | |
| 1152 | + do { | |
| 1153 | + CALC_LOAD(avenrun[0], EXP_1, active_tasks); | |
| 1154 | + CALC_LOAD(avenrun[1], EXP_5, active_tasks); | |
| 1155 | + CALC_LOAD(avenrun[2], EXP_15, active_tasks); | |
| 1156 | + count += LOAD_FREQ; | |
| 1157 | + } while (count < 0); | |
| 1154 | 1158 | } |
| 1155 | 1159 | } |
| 1156 | 1160 | |