loadavg.c 11 KB

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