pelt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Per Entity Load Tracking (PELT)
  4. *
  5. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Interactivity improvements by Mike Galbraith
  8. * (C) 2007 Mike Galbraith <efault@gmx.de>
  9. *
  10. * Various enhancements by Dmitry Adamushko.
  11. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  12. *
  13. * Group scheduling enhancements by Srivatsa Vaddagiri
  14. * Copyright IBM Corporation, 2007
  15. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  16. *
  17. * Scaled math optimizations by Thomas Gleixner
  18. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  19. *
  20. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  21. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
  22. *
  23. * Move PELT related code from fair.c into this pelt.c file
  24. * Author: Vincent Guittot <vincent.guittot@linaro.org>
  25. */
  26. /*
  27. * Approximate:
  28. * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
  29. */
  30. static u64 decay_load(u64 val, u64 n)
  31. {
  32. unsigned int local_n;
  33. if (unlikely(n > LOAD_AVG_PERIOD * 63))
  34. return 0;
  35. /* after bounds checking we can collapse to 32-bit */
  36. local_n = n;
  37. /*
  38. * As y^PERIOD = 1/2, we can combine
  39. * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
  40. * With a look-up table which covers y^n (n<PERIOD)
  41. *
  42. * To achieve constant time decay_load.
  43. */
  44. if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
  45. val >>= local_n / LOAD_AVG_PERIOD;
  46. local_n %= LOAD_AVG_PERIOD;
  47. }
  48. val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
  49. return val;
  50. }
  51. static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3)
  52. {
  53. u32 c1, c2, c3 = d3; /* y^0 == 1 */
  54. /*
  55. * c1 = d1 y^p
  56. */
  57. c1 = decay_load((u64)d1, periods);
  58. /*
  59. * p-1
  60. * c2 = 1024 \Sum y^n
  61. * n=1
  62. *
  63. * inf inf
  64. * = 1024 ( \Sum y^n - \Sum y^n - y^0 )
  65. * n=0 n=p
  66. */
  67. c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 1024;
  68. return c1 + c2 + c3;
  69. }
  70. /*
  71. * Accumulate the three separate parts of the sum; d1 the remainder
  72. * of the last (incomplete) period, d2 the span of full periods and d3
  73. * the remainder of the (incomplete) current period.
  74. *
  75. * d1 d2 d3
  76. * ^ ^ ^
  77. * | | |
  78. * |<->|<----------------->|<--->|
  79. * ... |---x---|------| ... |------|-----x (now)
  80. *
  81. * p-1
  82. * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0
  83. * n=1
  84. *
  85. * = u y^p + (Step 1)
  86. *
  87. * p-1
  88. * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2)
  89. * n=1
  90. */
  91. static __always_inline u32
  92. accumulate_sum(u64 delta, struct sched_avg *sa,
  93. unsigned long load, unsigned long runnable, int running)
  94. {
  95. u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */
  96. u64 periods;
  97. delta += sa->period_contrib;
  98. periods = delta / 1024; /* A period is 1024us (~1ms) */
  99. /*
  100. * Step 1: decay old *_sum if we crossed period boundaries.
  101. */
  102. if (periods) {
  103. sa->load_sum = decay_load(sa->load_sum, periods);
  104. sa->runnable_sum =
  105. decay_load(sa->runnable_sum, periods);
  106. sa->util_sum = decay_load((u64)(sa->util_sum), periods);
  107. /*
  108. * Step 2
  109. */
  110. delta %= 1024;
  111. if (load) {
  112. /*
  113. * This relies on the:
  114. *
  115. * if (!load)
  116. * runnable = running = 0;
  117. *
  118. * clause from ___update_load_sum(); this results in
  119. * the below usage of @contrib to disappear entirely,
  120. * so no point in calculating it.
  121. */
  122. contrib = __accumulate_pelt_segments(periods,
  123. 1024 - sa->period_contrib, delta);
  124. }
  125. }
  126. sa->period_contrib = delta;
  127. if (load)
  128. sa->load_sum += load * contrib;
  129. if (runnable)
  130. sa->runnable_sum += runnable * contrib << SCHED_CAPACITY_SHIFT;
  131. if (running)
  132. sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
  133. return periods;
  134. }
  135. /*
  136. * We can represent the historical contribution to runnable average as the
  137. * coefficients of a geometric series. To do this we sub-divide our runnable
  138. * history into segments of approximately 1ms (1024us); label the segment that
  139. * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
  140. *
  141. * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
  142. * p0 p1 p2
  143. * (now) (~1ms ago) (~2ms ago)
  144. *
  145. * Let u_i denote the fraction of p_i that the entity was runnable.
  146. *
  147. * We then designate the fractions u_i as our co-efficients, yielding the
  148. * following representation of historical load:
  149. * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
  150. *
  151. * We choose y based on the with of a reasonably scheduling period, fixing:
  152. * y^32 = 0.5
  153. *
  154. * This means that the contribution to load ~32ms ago (u_32) will be weighted
  155. * approximately half as much as the contribution to load within the last ms
  156. * (u_0).
  157. *
  158. * When a period "rolls over" and we have new u_0`, multiplying the previous
  159. * sum again by y is sufficient to update:
  160. * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
  161. * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
  162. */
  163. static __always_inline int
  164. ___update_load_sum(u64 now, struct sched_avg *sa,
  165. unsigned long load, unsigned long runnable, int running)
  166. {
  167. u64 delta;
  168. delta = now - sa->last_update_time;
  169. /*
  170. * This should only happen when time goes backwards, which it
  171. * unfortunately does during sched clock init when we swap over to TSC.
  172. */
  173. if ((s64)delta < 0) {
  174. sa->last_update_time = now;
  175. return 0;
  176. }
  177. /*
  178. * Use 1024ns as the unit of measurement since it's a reasonable
  179. * approximation of 1us and fast to compute.
  180. */
  181. delta >>= 10;
  182. if (!delta)
  183. return 0;
  184. sa->last_update_time += delta << 10;
  185. /*
  186. * running is a subset of runnable (weight) so running can't be set if
  187. * runnable is clear. But there are some corner cases where the current
  188. * se has been already dequeued but cfs_rq->curr still points to it.
  189. * This means that weight will be 0 but not running for a sched_entity
  190. * but also for a cfs_rq if the latter becomes idle. As an example,
  191. * this happens during sched_balance_newidle() which calls
  192. * sched_balance_update_blocked_averages().
  193. *
  194. * Also see the comment in accumulate_sum().
  195. */
  196. if (!load)
  197. runnable = running = 0;
  198. /*
  199. * Now we know we crossed measurement unit boundaries. The *_avg
  200. * accrues by two steps:
  201. *
  202. * Step 1: accumulate *_sum since last_update_time. If we haven't
  203. * crossed period boundaries, finish.
  204. */
  205. if (!accumulate_sum(delta, sa, load, runnable, running))
  206. return 0;
  207. return 1;
  208. }
  209. /*
  210. * When syncing *_avg with *_sum, we must take into account the current
  211. * position in the PELT segment otherwise the remaining part of the segment
  212. * will be considered as idle time whereas it's not yet elapsed and this will
  213. * generate unwanted oscillation in the range [1002..1024[.
  214. *
  215. * The max value of *_sum varies with the position in the time segment and is
  216. * equals to :
  217. *
  218. * LOAD_AVG_MAX*y + sa->period_contrib
  219. *
  220. * which can be simplified into:
  221. *
  222. * LOAD_AVG_MAX - 1024 + sa->period_contrib
  223. *
  224. * because LOAD_AVG_MAX*y == LOAD_AVG_MAX-1024
  225. *
  226. * The same care must be taken when a sched entity is added, updated or
  227. * removed from a cfs_rq and we need to update sched_avg. Scheduler entities
  228. * and the cfs rq, to which they are attached, have the same position in the
  229. * time segment because they use the same clock. This means that we can use
  230. * the period_contrib of cfs_rq when updating the sched_avg of a sched_entity
  231. * if it's more convenient.
  232. */
  233. static __always_inline void
  234. ___update_load_avg(struct sched_avg *sa, unsigned long load)
  235. {
  236. u32 divider = get_pelt_divider(sa);
  237. /*
  238. * Step 2: update *_avg.
  239. */
  240. sa->load_avg = div_u64(load * sa->load_sum, divider);
  241. sa->runnable_avg = div_u64(sa->runnable_sum, divider);
  242. WRITE_ONCE(sa->util_avg, sa->util_sum / divider);
  243. }
  244. /*
  245. * sched_entity:
  246. *
  247. * task:
  248. * se_weight() = se->load.weight
  249. * se_runnable() = !!on_rq
  250. *
  251. * group: [ see update_cfs_group() ]
  252. * se_weight() = tg->weight * grq->load_avg / tg->load_avg
  253. * se_runnable() = grq->h_nr_queued
  254. *
  255. * runnable_sum = se_runnable() * runnable = grq->runnable_sum
  256. * runnable_avg = runnable_sum
  257. *
  258. * load_sum := runnable
  259. * load_avg = se_weight(se) * load_sum
  260. *
  261. * cfq_rq:
  262. *
  263. * runnable_sum = \Sum se->avg.runnable_sum
  264. * runnable_avg = \Sum se->avg.runnable_avg
  265. *
  266. * load_sum = \Sum se_weight(se) * se->avg.load_sum
  267. * load_avg = \Sum se->avg.load_avg
  268. */
  269. int __update_load_avg_blocked_se(u64 now, struct sched_entity *se)
  270. {
  271. if (___update_load_sum(now, &se->avg, 0, 0, 0)) {
  272. ___update_load_avg(&se->avg, se_weight(se));
  273. trace_pelt_se_tp(se);
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. int __update_load_avg_se(u64 now, struct cfs_rq *cfs_rq, struct sched_entity *se)
  279. {
  280. if (___update_load_sum(now, &se->avg, !!se->on_rq, se_runnable(se),
  281. cfs_rq->curr == se)) {
  282. ___update_load_avg(&se->avg, se_weight(se));
  283. cfs_se_util_change(&se->avg);
  284. trace_pelt_se_tp(se);
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. int __update_load_avg_cfs_rq(u64 now, struct cfs_rq *cfs_rq)
  290. {
  291. if (___update_load_sum(now, &cfs_rq->avg,
  292. scale_load_down(cfs_rq->load.weight),
  293. cfs_rq->h_nr_queued - cfs_rq->h_nr_delayed,
  294. cfs_rq->curr != NULL)) {
  295. ___update_load_avg(&cfs_rq->avg, 1);
  296. trace_pelt_cfs_tp(cfs_rq);
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. /*
  302. * rt_rq:
  303. *
  304. * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
  305. * util_sum = cpu_scale * load_sum
  306. * runnable_sum = util_sum
  307. *
  308. * load_avg and runnable_avg are not supported and meaningless.
  309. *
  310. */
  311. int update_rt_rq_load_avg(u64 now, struct rq *rq, int running)
  312. {
  313. if (___update_load_sum(now, &rq->avg_rt,
  314. running,
  315. running,
  316. running)) {
  317. ___update_load_avg(&rq->avg_rt, 1);
  318. trace_pelt_rt_tp(rq);
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * dl_rq:
  325. *
  326. * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
  327. * util_sum = cpu_scale * load_sum
  328. * runnable_sum = util_sum
  329. *
  330. * load_avg and runnable_avg are not supported and meaningless.
  331. *
  332. */
  333. int update_dl_rq_load_avg(u64 now, struct rq *rq, int running)
  334. {
  335. if (___update_load_sum(now, &rq->avg_dl,
  336. running,
  337. running,
  338. running)) {
  339. ___update_load_avg(&rq->avg_dl, 1);
  340. trace_pelt_dl_tp(rq);
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. #ifdef CONFIG_SCHED_HW_PRESSURE
  346. /*
  347. * hardware:
  348. *
  349. * load_sum = \Sum se->avg.load_sum but se->avg.load_sum is not tracked
  350. *
  351. * util_avg and runnable_load_avg are not supported and meaningless.
  352. *
  353. * Unlike rt/dl utilization tracking that track time spent by a cpu
  354. * running a rt/dl task through util_avg, the average HW pressure is
  355. * tracked through load_avg. This is because HW pressure signal is
  356. * time weighted "delta" capacity unlike util_avg which is binary.
  357. * "delta capacity" = actual capacity -
  358. * capped capacity a cpu due to a HW event.
  359. */
  360. int update_hw_load_avg(u64 now, struct rq *rq, u64 capacity)
  361. {
  362. if (___update_load_sum(now, &rq->avg_hw,
  363. capacity,
  364. capacity,
  365. capacity)) {
  366. ___update_load_avg(&rq->avg_hw, 1);
  367. trace_pelt_hw_tp(rq);
  368. return 1;
  369. }
  370. return 0;
  371. }
  372. #endif
  373. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  374. /*
  375. * IRQ:
  376. *
  377. * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
  378. * util_sum = cpu_scale * load_sum
  379. * runnable_sum = util_sum
  380. *
  381. * load_avg and runnable_avg are not supported and meaningless.
  382. *
  383. */
  384. int update_irq_load_avg(struct rq *rq, u64 running)
  385. {
  386. int ret = 0;
  387. /*
  388. * We can't use clock_pelt because IRQ time is not accounted in
  389. * clock_task. Instead we directly scale the running time to
  390. * reflect the real amount of computation
  391. */
  392. running = cap_scale(running, arch_scale_freq_capacity(cpu_of(rq)));
  393. running = cap_scale(running, arch_scale_cpu_capacity(cpu_of(rq)));
  394. /*
  395. * We know the time that has been used by interrupt since last update
  396. * but we don't when. Let be pessimistic and assume that interrupt has
  397. * happened just before the update. This is not so far from reality
  398. * because interrupt will most probably wake up task and trig an update
  399. * of rq clock during which the metric is updated.
  400. * We start to decay with normal context time and then we add the
  401. * interrupt context time.
  402. * We can safely remove running from rq->clock because
  403. * rq->clock += delta with delta >= running
  404. */
  405. ret = ___update_load_sum(rq->clock - running, &rq->avg_irq,
  406. 0,
  407. 0,
  408. 0);
  409. ret += ___update_load_sum(rq->clock, &rq->avg_irq,
  410. 1,
  411. 1,
  412. 1);
  413. if (ret) {
  414. ___update_load_avg(&rq->avg_irq, 1);
  415. trace_pelt_irq_tp(rq);
  416. }
  417. return ret;
  418. }
  419. #endif
  420. /*
  421. * Load avg and utiliztion metrics need to be updated periodically and before
  422. * consumption. This function updates the metrics for all subsystems except for
  423. * the fair class. @rq must be locked and have its clock updated.
  424. */
  425. bool update_other_load_avgs(struct rq *rq)
  426. {
  427. u64 now = rq_clock_pelt(rq);
  428. const struct sched_class *curr_class = rq->curr->sched_class;
  429. unsigned long hw_pressure = arch_scale_hw_pressure(cpu_of(rq));
  430. lockdep_assert_rq_held(rq);
  431. /* hw_pressure doesn't care about invariance */
  432. return update_rt_rq_load_avg(now, rq, curr_class == &rt_sched_class) |
  433. update_dl_rq_load_avg(now, rq, curr_class == &dl_sched_class) |
  434. update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure) |
  435. update_irq_load_avg(rq, 0);
  436. }