loadavg.c 11 KB

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