loadavg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. unsigned long newload;
  93. newload = load * exp + active * (FIXED_1 - exp);
  94. if (active >= load)
  95. newload += FIXED_1-1;
  96. return newload / FIXED_1;
  97. }
  98. #ifdef CONFIG_NO_HZ_COMMON
  99. /*
  100. * Handle NO_HZ for the global load-average.
  101. *
  102. * Since the above described distributed algorithm to compute the global
  103. * load-average relies on per-cpu sampling from the tick, it is affected by
  104. * NO_HZ.
  105. *
  106. * The basic idea is to fold the nr_active delta into a global idle-delta upon
  107. * entering NO_HZ state such that we can include this as an 'extra' cpu delta
  108. * when we read the global state.
  109. *
  110. * Obviously reality has to ruin such a delightfully simple scheme:
  111. *
  112. * - When we go NO_HZ idle during the window, we can negate our sample
  113. * contribution, causing under-accounting.
  114. *
  115. * We avoid this by keeping two idle-delta counters and flipping them
  116. * when the window starts, thus separating old and new NO_HZ load.
  117. *
  118. * The only trick is the slight shift in index flip for read vs write.
  119. *
  120. * 0s 5s 10s 15s
  121. * +10 +10 +10 +10
  122. * |-|-----------|-|-----------|-|-----------|-|
  123. * r:0 0 1 1 0 0 1 1 0
  124. * w:0 1 1 0 0 1 1 0 0
  125. *
  126. * This ensures we'll fold the old idle contribution in this window while
  127. * accumlating the new one.
  128. *
  129. * - When we wake up from NO_HZ idle during the window, we push up our
  130. * contribution, since we effectively move our sample point to a known
  131. * busy state.
  132. *
  133. * This is solved by pushing the window forward, and thus skipping the
  134. * sample, for this cpu (effectively using the idle-delta for this cpu which
  135. * was in effect at the time the window opened). This also solves the issue
  136. * of having to deal with a cpu having been in NOHZ idle for multiple
  137. * LOAD_FREQ intervals.
  138. *
  139. * When making the ILB scale, we should try to pull this in as well.
  140. */
  141. static atomic_long_t calc_load_idle[2];
  142. static int calc_load_idx;
  143. static inline int calc_load_write_idx(void)
  144. {
  145. int idx = calc_load_idx;
  146. /*
  147. * See calc_global_nohz(), if we observe the new index, we also
  148. * need to observe the new update time.
  149. */
  150. smp_rmb();
  151. /*
  152. * If the folding window started, make sure we start writing in the
  153. * next idle-delta.
  154. */
  155. if (!time_before(jiffies, calc_load_update))
  156. idx++;
  157. return idx & 1;
  158. }
  159. static inline int calc_load_read_idx(void)
  160. {
  161. return calc_load_idx & 1;
  162. }
  163. void calc_load_enter_idle(void)
  164. {
  165. struct rq *this_rq = this_rq();
  166. long delta;
  167. /*
  168. * We're going into NOHZ mode, if there's any pending delta, fold it
  169. * into the pending idle delta.
  170. */
  171. delta = calc_load_fold_active(this_rq);
  172. if (delta) {
  173. int idx = calc_load_write_idx();
  174. atomic_long_add(delta, &calc_load_idle[idx]);
  175. }
  176. }
  177. void calc_load_exit_idle(void)
  178. {
  179. struct rq *this_rq = this_rq();
  180. /*
  181. * If we're still before the pending sample window, we're done.
  182. */
  183. this_rq->calc_load_update = calc_load_update;
  184. if (time_before(jiffies, this_rq->calc_load_update))
  185. return;
  186. /*
  187. * We woke inside or after the sample window, this means we're already
  188. * accounted through the nohz accounting, so skip the entire deal and
  189. * sync up for the next window.
  190. */
  191. if (time_before(jiffies, this_rq->calc_load_update + 10))
  192. this_rq->calc_load_update += LOAD_FREQ;
  193. }
  194. static long calc_load_fold_idle(void)
  195. {
  196. int idx = calc_load_read_idx();
  197. long delta = 0;
  198. if (atomic_long_read(&calc_load_idle[idx]))
  199. delta = atomic_long_xchg(&calc_load_idle[idx], 0);
  200. return delta;
  201. }
  202. /**
  203. * fixed_power_int - compute: x^n, in O(log n) time
  204. *
  205. * @x: base of the power
  206. * @frac_bits: fractional bits of @x
  207. * @n: power to raise @x to.
  208. *
  209. * By exploiting the relation between the definition of the natural power
  210. * function: x^n := x*x*...*x (x multiplied by itself for n times), and
  211. * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
  212. * (where: n_i \elem {0, 1}, the binary vector representing n),
  213. * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
  214. * of course trivially computable in O(log_2 n), the length of our binary
  215. * vector.
  216. */
  217. static unsigned long
  218. fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
  219. {
  220. unsigned long result = 1UL << frac_bits;
  221. if (n) {
  222. for (;;) {
  223. if (n & 1) {
  224. result *= x;
  225. result += 1UL << (frac_bits - 1);
  226. result >>= frac_bits;
  227. }
  228. n >>= 1;
  229. if (!n)
  230. break;
  231. x *= x;
  232. x += 1UL << (frac_bits - 1);
  233. x >>= frac_bits;
  234. }
  235. }
  236. return result;
  237. }
  238. /*
  239. * a1 = a0 * e + a * (1 - e)
  240. *
  241. * a2 = a1 * e + a * (1 - e)
  242. * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
  243. * = a0 * e^2 + a * (1 - e) * (1 + e)
  244. *
  245. * a3 = a2 * e + a * (1 - e)
  246. * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
  247. * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
  248. *
  249. * ...
  250. *
  251. * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
  252. * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
  253. * = a0 * e^n + a * (1 - e^n)
  254. *
  255. * [1] application of the geometric series:
  256. *
  257. * n 1 - x^(n+1)
  258. * S_n := \Sum x^i = -------------
  259. * i=0 1 - x
  260. */
  261. static unsigned long
  262. calc_load_n(unsigned long load, unsigned long exp,
  263. unsigned long active, unsigned int n)
  264. {
  265. return calc_load(load, fixed_power_int(exp, FSHIFT, n), active);
  266. }
  267. /*
  268. * NO_HZ can leave us missing all per-cpu ticks calling
  269. * calc_load_account_active(), but since an idle CPU folds its delta into
  270. * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold
  271. * in the pending idle delta if our idle period crossed a load cycle boundary.
  272. *
  273. * Once we've updated the global active value, we need to apply the exponential
  274. * weights adjusted to the number of cycles missed.
  275. */
  276. static void calc_global_nohz(void)
  277. {
  278. long delta, active, n;
  279. if (!time_before(jiffies, calc_load_update + 10)) {
  280. /*
  281. * Catch-up, fold however many we are behind still
  282. */
  283. delta = jiffies - calc_load_update - 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. calc_load_update += n * LOAD_FREQ;
  291. }
  292. /*
  293. * Flip the idle 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_fold_idle(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(unsigned long ticks)
  313. {
  314. long active, delta;
  315. if (time_before(jiffies, calc_load_update + 10))
  316. return;
  317. /*
  318. * Fold the 'old' idle-delta to include all NO_HZ cpus.
  319. */
  320. delta = calc_load_fold_idle();
  321. if (delta)
  322. atomic_long_add(delta, &calc_load_tasks);
  323. active = atomic_long_read(&calc_load_tasks);
  324. active = active > 0 ? active * FIXED_1 : 0;
  325. avenrun[0] = calc_load(avenrun[0], EXP_1, active);
  326. avenrun[1] = calc_load(avenrun[1], EXP_5, active);
  327. avenrun[2] = calc_load(avenrun[2], EXP_15, active);
  328. calc_load_update += LOAD_FREQ;
  329. /*
  330. * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.
  331. */
  332. calc_global_nohz();
  333. }
  334. /*
  335. * Called from scheduler_tick() to periodically update this CPU's
  336. * active count.
  337. */
  338. void calc_global_load_tick(struct rq *this_rq)
  339. {
  340. long delta;
  341. if (time_before(jiffies, this_rq->calc_load_update))
  342. return;
  343. delta = calc_load_fold_active(this_rq);
  344. if (delta)
  345. atomic_long_add(delta, &calc_load_tasks);
  346. this_rq->calc_load_update += LOAD_FREQ;
  347. }