Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
45ceebf7 | 2 | /* |
3289bdb4 | 3 | * kernel/sched/loadavg.c |
45ceebf7 | 4 | * |
3289bdb4 PZ |
5 | * This file contains the magic bits required to compute the global loadavg |
6 | * figure. Its a silly number but people think its important. We go through | |
7 | * great pains to make it work on big machines and tickless kernels. | |
45ceebf7 | 8 | */ |
45ceebf7 PG |
9 | #include "sched.h" |
10 | ||
45ceebf7 PG |
11 | /* |
12 | * Global load-average calculations | |
13 | * | |
14 | * We take a distributed and async approach to calculating the global load-avg | |
15 | * in order to minimize overhead. | |
16 | * | |
17 | * The global load average is an exponentially decaying average of nr_running + | |
18 | * nr_uninterruptible. | |
19 | * | |
20 | * Once every LOAD_FREQ: | |
21 | * | |
22 | * nr_active = 0; | |
23 | * for_each_possible_cpu(cpu) | |
24 | * nr_active += cpu_of(cpu)->nr_running + cpu_of(cpu)->nr_uninterruptible; | |
25 | * | |
26 | * avenrun[n] = avenrun[0] * exp_n + nr_active * (1 - exp_n) | |
27 | * | |
28 | * Due to a number of reasons the above turns in the mess below: | |
29 | * | |
30 | * - for_each_possible_cpu() is prohibitively expensive on machines with | |
97fb7a0a | 31 | * serious number of CPUs, therefore we need to take a distributed approach |
45ceebf7 PG |
32 | * to calculating nr_active. |
33 | * | |
34 | * \Sum_i x_i(t) = \Sum_i x_i(t) - x_i(t_0) | x_i(t_0) := 0 | |
35 | * = \Sum_i { \Sum_j=1 x_i(t_j) - x_i(t_j-1) } | |
36 | * | |
37 | * So assuming nr_active := 0 when we start out -- true per definition, we | |
97fb7a0a | 38 | * can simply take per-CPU deltas and fold those into a global accumulate |
45ceebf7 PG |
39 | * to obtain the same result. See calc_load_fold_active(). |
40 | * | |
97fb7a0a | 41 | * Furthermore, in order to avoid synchronizing all per-CPU delta folding |
45ceebf7 | 42 | * across the machine, we assume 10 ticks is sufficient time for every |
97fb7a0a | 43 | * CPU to have completed this task. |
45ceebf7 PG |
44 | * |
45 | * This places an upper-bound on the IRQ-off latency of the machine. Then | |
46 | * again, being late doesn't loose the delta, just wrecks the sample. | |
47 | * | |
97fb7a0a IM |
48 | * - cpu_rq()->nr_uninterruptible isn't accurately tracked per-CPU because |
49 | * this would add another cross-CPU cacheline miss and atomic operation | |
50 | * to the wakeup path. Instead we increment on whatever CPU the task ran | |
51 | * when it went into uninterruptible state and decrement on whatever CPU | |
45ceebf7 | 52 | * did the wakeup. This means that only the sum of nr_uninterruptible over |
97fb7a0a | 53 | * all CPUs yields the correct result. |
45ceebf7 PG |
54 | * |
55 | * This covers the NO_HZ=n code, for extra head-aches, see the comment below. | |
56 | */ | |
57 | ||
58 | /* Variables and functions for calc_load */ | |
59 | atomic_long_t calc_load_tasks; | |
60 | unsigned long calc_load_update; | |
61 | unsigned long avenrun[3]; | |
62 | EXPORT_SYMBOL(avenrun); /* should be removed */ | |
63 | ||
64 | /** | |
65 | * get_avenrun - get the load average array | |
66 | * @loads: pointer to dest load array | |
67 | * @offset: offset to add | |
68 | * @shift: shift count to shift the result left | |
69 | * | |
70 | * These values are estimates at best, so no need for locking. | |
71 | */ | |
72 | void get_avenrun(unsigned long *loads, unsigned long offset, int shift) | |
73 | { | |
74 | loads[0] = (avenrun[0] + offset) << shift; | |
75 | loads[1] = (avenrun[1] + offset) << shift; | |
76 | loads[2] = (avenrun[2] + offset) << shift; | |
77 | } | |
78 | ||
d60585c5 | 79 | long calc_load_fold_active(struct rq *this_rq, long adjust) |
45ceebf7 PG |
80 | { |
81 | long nr_active, delta = 0; | |
82 | ||
d60585c5 | 83 | nr_active = this_rq->nr_running - adjust; |
3289bdb4 | 84 | nr_active += (long)this_rq->nr_uninterruptible; |
45ceebf7 PG |
85 | |
86 | if (nr_active != this_rq->calc_load_active) { | |
87 | delta = nr_active - this_rq->calc_load_active; | |
88 | this_rq->calc_load_active = nr_active; | |
89 | } | |
90 | ||
91 | return delta; | |
92 | } | |
93 | ||
45ceebf7 PG |
94 | #ifdef CONFIG_NO_HZ_COMMON |
95 | /* | |
96 | * Handle NO_HZ for the global load-average. | |
97 | * | |
98 | * Since the above described distributed algorithm to compute the global | |
97fb7a0a | 99 | * load-average relies on per-CPU sampling from the tick, it is affected by |
45ceebf7 PG |
100 | * NO_HZ. |
101 | * | |
3c85d6db | 102 | * The basic idea is to fold the nr_active delta into a global NO_HZ-delta upon |
97fb7a0a | 103 | * entering NO_HZ state such that we can include this as an 'extra' CPU delta |
45ceebf7 PG |
104 | * when we read the global state. |
105 | * | |
106 | * Obviously reality has to ruin such a delightfully simple scheme: | |
107 | * | |
108 | * - When we go NO_HZ idle during the window, we can negate our sample | |
109 | * contribution, causing under-accounting. | |
110 | * | |
3c85d6db | 111 | * We avoid this by keeping two NO_HZ-delta counters and flipping them |
45ceebf7 PG |
112 | * when the window starts, thus separating old and new NO_HZ load. |
113 | * | |
114 | * The only trick is the slight shift in index flip for read vs write. | |
115 | * | |
116 | * 0s 5s 10s 15s | |
117 | * +10 +10 +10 +10 | |
118 | * |-|-----------|-|-----------|-|-----------|-| | |
119 | * r:0 0 1 1 0 0 1 1 0 | |
120 | * w:0 1 1 0 0 1 1 0 0 | |
121 | * | |
3c85d6db | 122 | * This ensures we'll fold the old NO_HZ contribution in this window while |
45ceebf7 PG |
123 | * accumlating the new one. |
124 | * | |
3c85d6db | 125 | * - When we wake up from NO_HZ during the window, we push up our |
45ceebf7 PG |
126 | * contribution, since we effectively move our sample point to a known |
127 | * busy state. | |
128 | * | |
129 | * This is solved by pushing the window forward, and thus skipping the | |
97fb7a0a | 130 | * sample, for this CPU (effectively using the NO_HZ-delta for this CPU which |
45ceebf7 | 131 | * was in effect at the time the window opened). This also solves the issue |
97fb7a0a | 132 | * of having to deal with a CPU having been in NO_HZ for multiple LOAD_FREQ |
3c85d6db | 133 | * intervals. |
45ceebf7 PG |
134 | * |
135 | * When making the ILB scale, we should try to pull this in as well. | |
136 | */ | |
3c85d6db | 137 | static atomic_long_t calc_load_nohz[2]; |
45ceebf7 PG |
138 | static int calc_load_idx; |
139 | ||
140 | static inline int calc_load_write_idx(void) | |
141 | { | |
142 | int idx = calc_load_idx; | |
143 | ||
144 | /* | |
145 | * See calc_global_nohz(), if we observe the new index, we also | |
146 | * need to observe the new update time. | |
147 | */ | |
148 | smp_rmb(); | |
149 | ||
150 | /* | |
151 | * If the folding window started, make sure we start writing in the | |
3c85d6db | 152 | * next NO_HZ-delta. |
45ceebf7 | 153 | */ |
caeb5882 | 154 | if (!time_before(jiffies, READ_ONCE(calc_load_update))) |
45ceebf7 PG |
155 | idx++; |
156 | ||
157 | return idx & 1; | |
158 | } | |
159 | ||
160 | static inline int calc_load_read_idx(void) | |
161 | { | |
162 | return calc_load_idx & 1; | |
163 | } | |
164 | ||
3c85d6db | 165 | void calc_load_nohz_start(void) |
45ceebf7 PG |
166 | { |
167 | struct rq *this_rq = this_rq(); | |
168 | long delta; | |
169 | ||
170 | /* | |
3c85d6db FW |
171 | * We're going into NO_HZ mode, if there's any pending delta, fold it |
172 | * into the pending NO_HZ delta. | |
45ceebf7 | 173 | */ |
d60585c5 | 174 | delta = calc_load_fold_active(this_rq, 0); |
45ceebf7 PG |
175 | if (delta) { |
176 | int idx = calc_load_write_idx(); | |
3289bdb4 | 177 | |
3c85d6db | 178 | atomic_long_add(delta, &calc_load_nohz[idx]); |
45ceebf7 PG |
179 | } |
180 | } | |
181 | ||
3c85d6db | 182 | void calc_load_nohz_stop(void) |
45ceebf7 PG |
183 | { |
184 | struct rq *this_rq = this_rq(); | |
185 | ||
186 | /* | |
6e5f32f7 | 187 | * If we're still before the pending sample window, we're done. |
45ceebf7 | 188 | */ |
caeb5882 | 189 | this_rq->calc_load_update = READ_ONCE(calc_load_update); |
45ceebf7 PG |
190 | if (time_before(jiffies, this_rq->calc_load_update)) |
191 | return; | |
192 | ||
193 | /* | |
194 | * We woke inside or after the sample window, this means we're already | |
195 | * accounted through the nohz accounting, so skip the entire deal and | |
196 | * sync up for the next window. | |
197 | */ | |
45ceebf7 PG |
198 | if (time_before(jiffies, this_rq->calc_load_update + 10)) |
199 | this_rq->calc_load_update += LOAD_FREQ; | |
200 | } | |
201 | ||
3c85d6db | 202 | static long calc_load_nohz_fold(void) |
45ceebf7 PG |
203 | { |
204 | int idx = calc_load_read_idx(); | |
205 | long delta = 0; | |
206 | ||
3c85d6db FW |
207 | if (atomic_long_read(&calc_load_nohz[idx])) |
208 | delta = atomic_long_xchg(&calc_load_nohz[idx], 0); | |
45ceebf7 PG |
209 | |
210 | return delta; | |
211 | } | |
212 | ||
213 | /** | |
214 | * fixed_power_int - compute: x^n, in O(log n) time | |
215 | * | |
216 | * @x: base of the power | |
217 | * @frac_bits: fractional bits of @x | |
218 | * @n: power to raise @x to. | |
219 | * | |
220 | * By exploiting the relation between the definition of the natural power | |
221 | * function: x^n := x*x*...*x (x multiplied by itself for n times), and | |
222 | * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i, | |
223 | * (where: n_i \elem {0, 1}, the binary vector representing n), | |
224 | * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is | |
225 | * of course trivially computable in O(log_2 n), the length of our binary | |
226 | * vector. | |
227 | */ | |
228 | static unsigned long | |
229 | fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n) | |
230 | { | |
231 | unsigned long result = 1UL << frac_bits; | |
232 | ||
3289bdb4 PZ |
233 | if (n) { |
234 | for (;;) { | |
235 | if (n & 1) { | |
236 | result *= x; | |
237 | result += 1UL << (frac_bits - 1); | |
238 | result >>= frac_bits; | |
239 | } | |
240 | n >>= 1; | |
241 | if (!n) | |
242 | break; | |
243 | x *= x; | |
244 | x += 1UL << (frac_bits - 1); | |
245 | x >>= frac_bits; | |
45ceebf7 | 246 | } |
45ceebf7 PG |
247 | } |
248 | ||
249 | return result; | |
250 | } | |
251 | ||
252 | /* | |
253 | * a1 = a0 * e + a * (1 - e) | |
254 | * | |
255 | * a2 = a1 * e + a * (1 - e) | |
256 | * = (a0 * e + a * (1 - e)) * e + a * (1 - e) | |
257 | * = a0 * e^2 + a * (1 - e) * (1 + e) | |
258 | * | |
259 | * a3 = a2 * e + a * (1 - e) | |
260 | * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e) | |
261 | * = a0 * e^3 + a * (1 - e) * (1 + e + e^2) | |
262 | * | |
263 | * ... | |
264 | * | |
265 | * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1] | |
266 | * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e) | |
267 | * = a0 * e^n + a * (1 - e^n) | |
268 | * | |
269 | * [1] application of the geometric series: | |
270 | * | |
271 | * n 1 - x^(n+1) | |
272 | * S_n := \Sum x^i = ------------- | |
273 | * i=0 1 - x | |
274 | */ | |
275 | static unsigned long | |
276 | calc_load_n(unsigned long load, unsigned long exp, | |
277 | unsigned long active, unsigned int n) | |
278 | { | |
45ceebf7 PG |
279 | return calc_load(load, fixed_power_int(exp, FSHIFT, n), active); |
280 | } | |
281 | ||
282 | /* | |
97fb7a0a | 283 | * NO_HZ can leave us missing all per-CPU ticks calling |
3c85d6db FW |
284 | * calc_load_fold_active(), but since a NO_HZ CPU folds its delta into |
285 | * calc_load_nohz per calc_load_nohz_start(), all we need to do is fold | |
286 | * in the pending NO_HZ delta if our NO_HZ period crossed a load cycle boundary. | |
45ceebf7 PG |
287 | * |
288 | * Once we've updated the global active value, we need to apply the exponential | |
289 | * weights adjusted to the number of cycles missed. | |
290 | */ | |
291 | static void calc_global_nohz(void) | |
292 | { | |
caeb5882 | 293 | unsigned long sample_window; |
45ceebf7 PG |
294 | long delta, active, n; |
295 | ||
caeb5882 MF |
296 | sample_window = READ_ONCE(calc_load_update); |
297 | if (!time_before(jiffies, sample_window + 10)) { | |
45ceebf7 PG |
298 | /* |
299 | * Catch-up, fold however many we are behind still | |
300 | */ | |
caeb5882 | 301 | delta = jiffies - sample_window - 10; |
45ceebf7 PG |
302 | n = 1 + (delta / LOAD_FREQ); |
303 | ||
304 | active = atomic_long_read(&calc_load_tasks); | |
305 | active = active > 0 ? active * FIXED_1 : 0; | |
306 | ||
307 | avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n); | |
308 | avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n); | |
309 | avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n); | |
310 | ||
caeb5882 | 311 | WRITE_ONCE(calc_load_update, sample_window + n * LOAD_FREQ); |
45ceebf7 PG |
312 | } |
313 | ||
314 | /* | |
3c85d6db | 315 | * Flip the NO_HZ index... |
45ceebf7 PG |
316 | * |
317 | * Make sure we first write the new time then flip the index, so that | |
318 | * calc_load_write_idx() will see the new time when it reads the new | |
319 | * index, this avoids a double flip messing things up. | |
320 | */ | |
321 | smp_wmb(); | |
322 | calc_load_idx++; | |
323 | } | |
324 | #else /* !CONFIG_NO_HZ_COMMON */ | |
325 | ||
3c85d6db | 326 | static inline long calc_load_nohz_fold(void) { return 0; } |
45ceebf7 PG |
327 | static inline void calc_global_nohz(void) { } |
328 | ||
329 | #endif /* CONFIG_NO_HZ_COMMON */ | |
330 | ||
331 | /* | |
332 | * calc_load - update the avenrun load estimates 10 ticks after the | |
333 | * CPUs have updated calc_load_tasks. | |
3289bdb4 PZ |
334 | * |
335 | * Called from the global timer code. | |
45ceebf7 PG |
336 | */ |
337 | void calc_global_load(unsigned long ticks) | |
338 | { | |
caeb5882 | 339 | unsigned long sample_window; |
45ceebf7 PG |
340 | long active, delta; |
341 | ||
caeb5882 MF |
342 | sample_window = READ_ONCE(calc_load_update); |
343 | if (time_before(jiffies, sample_window + 10)) | |
45ceebf7 PG |
344 | return; |
345 | ||
346 | /* | |
97fb7a0a | 347 | * Fold the 'old' NO_HZ-delta to include all NO_HZ CPUs. |
45ceebf7 | 348 | */ |
3c85d6db | 349 | delta = calc_load_nohz_fold(); |
45ceebf7 PG |
350 | if (delta) |
351 | atomic_long_add(delta, &calc_load_tasks); | |
352 | ||
353 | active = atomic_long_read(&calc_load_tasks); | |
354 | active = active > 0 ? active * FIXED_1 : 0; | |
355 | ||
356 | avenrun[0] = calc_load(avenrun[0], EXP_1, active); | |
357 | avenrun[1] = calc_load(avenrun[1], EXP_5, active); | |
358 | avenrun[2] = calc_load(avenrun[2], EXP_15, active); | |
359 | ||
caeb5882 | 360 | WRITE_ONCE(calc_load_update, sample_window + LOAD_FREQ); |
45ceebf7 PG |
361 | |
362 | /* | |
3c85d6db FW |
363 | * In case we went to NO_HZ for multiple LOAD_FREQ intervals |
364 | * catch up in bulk. | |
45ceebf7 PG |
365 | */ |
366 | calc_global_nohz(); | |
367 | } | |
368 | ||
369 | /* | |
3289bdb4 | 370 | * Called from scheduler_tick() to periodically update this CPU's |
45ceebf7 PG |
371 | * active count. |
372 | */ | |
3289bdb4 | 373 | void calc_global_load_tick(struct rq *this_rq) |
45ceebf7 PG |
374 | { |
375 | long delta; | |
376 | ||
377 | if (time_before(jiffies, this_rq->calc_load_update)) | |
378 | return; | |
379 | ||
d60585c5 | 380 | delta = calc_load_fold_active(this_rq, 0); |
45ceebf7 PG |
381 | if (delta) |
382 | atomic_long_add(delta, &calc_load_tasks); | |
383 | ||
384 | this_rq->calc_load_update += LOAD_FREQ; | |
385 | } |