loadavg.c 11 KB

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