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