blob: f14716a3522f796d98d79dc2c623a675725c08d6 [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 Molnar4f177222017-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
97/*
98 * a1 = a0 * e + a * (1 - e)
99 */
100static unsigned long
101calc_load(unsigned long load, unsigned long exp, unsigned long active)
102{
Vik Heyndrickx20878232016-04-28 20:46:28 +0200103 unsigned long newload;
104
105 newload = load * exp + active * (FIXED_1 - exp);
106 if (active >= load)
107 newload += FIXED_1-1;
108
109 return newload / FIXED_1;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400110}
111
112#ifdef CONFIG_NO_HZ_COMMON
113/*
114 * Handle NO_HZ for the global load-average.
115 *
116 * Since the above described distributed algorithm to compute the global
117 * load-average relies on per-cpu sampling from the tick, it is affected by
118 * NO_HZ.
119 *
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200120 * The basic idea is to fold the nr_active delta into a global NO_HZ-delta upon
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400121 * entering NO_HZ state such that we can include this as an 'extra' cpu delta
122 * when we read the global state.
123 *
124 * Obviously reality has to ruin such a delightfully simple scheme:
125 *
126 * - When we go NO_HZ idle during the window, we can negate our sample
127 * contribution, causing under-accounting.
128 *
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200129 * We avoid this by keeping two NO_HZ-delta counters and flipping them
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400130 * when the window starts, thus separating old and new NO_HZ load.
131 *
132 * The only trick is the slight shift in index flip for read vs write.
133 *
134 * 0s 5s 10s 15s
135 * +10 +10 +10 +10
136 * |-|-----------|-|-----------|-|-----------|-|
137 * r:0 0 1 1 0 0 1 1 0
138 * w:0 1 1 0 0 1 1 0 0
139 *
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200140 * This ensures we'll fold the old NO_HZ contribution in this window while
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400141 * accumlating the new one.
142 *
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200143 * - When we wake up from NO_HZ during the window, we push up our
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400144 * contribution, since we effectively move our sample point to a known
145 * busy state.
146 *
147 * This is solved by pushing the window forward, and thus skipping the
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200148 * sample, for this cpu (effectively using the NO_HZ-delta for this cpu which
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400149 * was in effect at the time the window opened). This also solves the issue
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200150 * of having to deal with a cpu having been in NO_HZ for multiple LOAD_FREQ
151 * intervals.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400152 *
153 * When making the ILB scale, we should try to pull this in as well.
154 */
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200155static atomic_long_t calc_load_nohz[2];
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400156static int calc_load_idx;
157
158static inline int calc_load_write_idx(void)
159{
160 int idx = calc_load_idx;
161
162 /*
163 * See calc_global_nohz(), if we observe the new index, we also
164 * need to observe the new update time.
165 */
166 smp_rmb();
167
168 /*
169 * If the folding window started, make sure we start writing in the
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200170 * next NO_HZ-delta.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400171 */
Matt Flemingcaeb5882017-02-17 12:07:31 +0000172 if (!time_before(jiffies, READ_ONCE(calc_load_update)))
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400173 idx++;
174
175 return idx & 1;
176}
177
178static inline int calc_load_read_idx(void)
179{
180 return calc_load_idx & 1;
181}
182
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200183void calc_load_nohz_start(void)
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400184{
185 struct rq *this_rq = this_rq();
186 long delta;
187
188 /*
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200189 * We're going into NO_HZ mode, if there's any pending delta, fold it
190 * into the pending NO_HZ delta.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400191 */
Thomas Gleixnerd60585c2016-07-12 18:33:56 +0200192 delta = calc_load_fold_active(this_rq, 0);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400193 if (delta) {
194 int idx = calc_load_write_idx();
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200195
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200196 atomic_long_add(delta, &calc_load_nohz[idx]);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400197 }
198}
199
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200200void calc_load_nohz_stop(void)
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400201{
202 struct rq *this_rq = this_rq();
203
204 /*
Matt Fleming6e5f32f2017-02-17 12:07:30 +0000205 * If we're still before the pending sample window, we're done.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400206 */
Matt Flemingcaeb5882017-02-17 12:07:31 +0000207 this_rq->calc_load_update = READ_ONCE(calc_load_update);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400208 if (time_before(jiffies, this_rq->calc_load_update))
209 return;
210
211 /*
212 * We woke inside or after the sample window, this means we're already
213 * accounted through the nohz accounting, so skip the entire deal and
214 * sync up for the next window.
215 */
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400216 if (time_before(jiffies, this_rq->calc_load_update + 10))
217 this_rq->calc_load_update += LOAD_FREQ;
218}
219
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200220static long calc_load_nohz_fold(void)
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400221{
222 int idx = calc_load_read_idx();
223 long delta = 0;
224
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200225 if (atomic_long_read(&calc_load_nohz[idx]))
226 delta = atomic_long_xchg(&calc_load_nohz[idx], 0);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400227
228 return delta;
229}
230
231/**
232 * fixed_power_int - compute: x^n, in O(log n) time
233 *
234 * @x: base of the power
235 * @frac_bits: fractional bits of @x
236 * @n: power to raise @x to.
237 *
238 * By exploiting the relation between the definition of the natural power
239 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
240 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
241 * (where: n_i \elem {0, 1}, the binary vector representing n),
242 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
243 * of course trivially computable in O(log_2 n), the length of our binary
244 * vector.
245 */
246static unsigned long
247fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
248{
249 unsigned long result = 1UL << frac_bits;
250
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200251 if (n) {
252 for (;;) {
253 if (n & 1) {
254 result *= x;
255 result += 1UL << (frac_bits - 1);
256 result >>= frac_bits;
257 }
258 n >>= 1;
259 if (!n)
260 break;
261 x *= x;
262 x += 1UL << (frac_bits - 1);
263 x >>= frac_bits;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400264 }
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400265 }
266
267 return result;
268}
269
270/*
271 * a1 = a0 * e + a * (1 - e)
272 *
273 * a2 = a1 * e + a * (1 - e)
274 * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
275 * = a0 * e^2 + a * (1 - e) * (1 + e)
276 *
277 * a3 = a2 * e + a * (1 - e)
278 * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
279 * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
280 *
281 * ...
282 *
283 * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
284 * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
285 * = a0 * e^n + a * (1 - e^n)
286 *
287 * [1] application of the geometric series:
288 *
289 * n 1 - x^(n+1)
290 * S_n := \Sum x^i = -------------
291 * i=0 1 - x
292 */
293static unsigned long
294calc_load_n(unsigned long load, unsigned long exp,
295 unsigned long active, unsigned int n)
296{
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400297 return calc_load(load, fixed_power_int(exp, FSHIFT, n), active);
298}
299
300/*
301 * NO_HZ can leave us missing all per-cpu ticks calling
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200302 * calc_load_fold_active(), but since a NO_HZ CPU folds its delta into
303 * calc_load_nohz per calc_load_nohz_start(), all we need to do is fold
304 * in the pending NO_HZ delta if our NO_HZ period crossed a load cycle boundary.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400305 *
306 * Once we've updated the global active value, we need to apply the exponential
307 * weights adjusted to the number of cycles missed.
308 */
309static void calc_global_nohz(void)
310{
Matt Flemingcaeb5882017-02-17 12:07:31 +0000311 unsigned long sample_window;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400312 long delta, active, n;
313
Matt Flemingcaeb5882017-02-17 12:07:31 +0000314 sample_window = READ_ONCE(calc_load_update);
315 if (!time_before(jiffies, sample_window + 10)) {
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400316 /*
317 * Catch-up, fold however many we are behind still
318 */
Matt Flemingcaeb5882017-02-17 12:07:31 +0000319 delta = jiffies - sample_window - 10;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400320 n = 1 + (delta / LOAD_FREQ);
321
322 active = atomic_long_read(&calc_load_tasks);
323 active = active > 0 ? active * FIXED_1 : 0;
324
325 avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n);
326 avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n);
327 avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n);
328
Matt Flemingcaeb5882017-02-17 12:07:31 +0000329 WRITE_ONCE(calc_load_update, sample_window + n * LOAD_FREQ);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400330 }
331
332 /*
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200333 * Flip the NO_HZ index...
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400334 *
335 * Make sure we first write the new time then flip the index, so that
336 * calc_load_write_idx() will see the new time when it reads the new
337 * index, this avoids a double flip messing things up.
338 */
339 smp_wmb();
340 calc_load_idx++;
341}
342#else /* !CONFIG_NO_HZ_COMMON */
343
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200344static inline long calc_load_nohz_fold(void) { return 0; }
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400345static inline void calc_global_nohz(void) { }
346
347#endif /* CONFIG_NO_HZ_COMMON */
348
349/*
350 * calc_load - update the avenrun load estimates 10 ticks after the
351 * CPUs have updated calc_load_tasks.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200352 *
353 * Called from the global timer code.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400354 */
355void calc_global_load(unsigned long ticks)
356{
Matt Flemingcaeb5882017-02-17 12:07:31 +0000357 unsigned long sample_window;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400358 long active, delta;
359
Matt Flemingcaeb5882017-02-17 12:07:31 +0000360 sample_window = READ_ONCE(calc_load_update);
361 if (time_before(jiffies, sample_window + 10))
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400362 return;
363
364 /*
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200365 * Fold the 'old' NO_HZ-delta to include all NO_HZ cpus.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400366 */
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200367 delta = calc_load_nohz_fold();
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400368 if (delta)
369 atomic_long_add(delta, &calc_load_tasks);
370
371 active = atomic_long_read(&calc_load_tasks);
372 active = active > 0 ? active * FIXED_1 : 0;
373
374 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
375 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
376 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
377
Matt Flemingcaeb5882017-02-17 12:07:31 +0000378 WRITE_ONCE(calc_load_update, sample_window + LOAD_FREQ);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400379
380 /*
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200381 * In case we went to NO_HZ for multiple LOAD_FREQ intervals
382 * catch up in bulk.
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400383 */
384 calc_global_nohz();
385}
386
387/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200388 * Called from scheduler_tick() to periodically update this CPU's
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400389 * active count.
390 */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200391void calc_global_load_tick(struct rq *this_rq)
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400392{
393 long delta;
394
395 if (time_before(jiffies, this_rq->calc_load_update))
396 return;
397
Thomas Gleixnerd60585c2016-07-12 18:33:56 +0200398 delta = calc_load_fold_active(this_rq, 0);
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400399 if (delta)
400 atomic_long_add(delta, &calc_load_tasks);
401
402 this_rq->calc_load_update += LOAD_FREQ;
403}