pelt.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #ifdef CONFIG_SMP
  2. #include "sched-pelt.h"
  3. int __update_load_avg_blocked_se(u64 now, struct sched_entity *se);
  4. int __update_load_avg_se(u64 now, struct cfs_rq *cfs_rq, struct sched_entity *se);
  5. int __update_load_avg_cfs_rq(u64 now, struct cfs_rq *cfs_rq);
  6. int update_rt_rq_load_avg(u64 now, struct rq *rq, int running);
  7. int update_dl_rq_load_avg(u64 now, struct rq *rq, int running);
  8. #ifdef CONFIG_SCHED_THERMAL_PRESSURE
  9. int update_thermal_load_avg(u64 now, struct rq *rq, u64 capacity);
  10. static inline u64 thermal_load_avg(struct rq *rq)
  11. {
  12. return READ_ONCE(rq->avg_thermal.load_avg);
  13. }
  14. #else
  15. static inline int
  16. update_thermal_load_avg(u64 now, struct rq *rq, u64 capacity)
  17. {
  18. return 0;
  19. }
  20. static inline u64 thermal_load_avg(struct rq *rq)
  21. {
  22. return 0;
  23. }
  24. #endif
  25. #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
  26. int update_irq_load_avg(struct rq *rq, u64 running);
  27. #else
  28. static inline int
  29. update_irq_load_avg(struct rq *rq, u64 running)
  30. {
  31. return 0;
  32. }
  33. #endif
  34. #define PELT_MIN_DIVIDER (LOAD_AVG_MAX - 1024)
  35. static inline u32 get_pelt_divider(struct sched_avg *avg)
  36. {
  37. return PELT_MIN_DIVIDER + avg->period_contrib;
  38. }
  39. static inline void cfs_se_util_change(struct sched_avg *avg)
  40. {
  41. unsigned int enqueued;
  42. if (!sched_feat(UTIL_EST))
  43. return;
  44. /* Avoid store if the flag has been already reset */
  45. enqueued = avg->util_est.enqueued;
  46. if (!(enqueued & UTIL_AVG_UNCHANGED))
  47. return;
  48. /* Reset flag to report util_avg has been updated */
  49. enqueued &= ~UTIL_AVG_UNCHANGED;
  50. WRITE_ONCE(avg->util_est.enqueued, enqueued);
  51. }
  52. static inline u64 rq_clock_task_mult(struct rq *rq)
  53. {
  54. lockdep_assert_rq_held(rq);
  55. assert_clock_updated(rq);
  56. return rq->clock_task_mult;
  57. }
  58. static inline u64 rq_clock_pelt(struct rq *rq)
  59. {
  60. lockdep_assert_rq_held(rq);
  61. assert_clock_updated(rq);
  62. return rq->clock_pelt - rq->lost_idle_time;
  63. }
  64. /* The rq is idle, we can sync to clock_task */
  65. static inline void _update_idle_rq_clock_pelt(struct rq *rq)
  66. {
  67. rq->clock_pelt = rq_clock_task_mult(rq);
  68. u64_u32_store(rq->clock_idle, rq_clock(rq));
  69. /* Paired with smp_rmb in migrate_se_pelt_lag() */
  70. smp_wmb();
  71. u64_u32_store(rq->clock_pelt_idle, rq_clock_pelt(rq));
  72. }
  73. /*
  74. * The clock_pelt scales the time to reflect the effective amount of
  75. * computation done during the running delta time but then sync back to
  76. * clock_task when rq is idle.
  77. *
  78. *
  79. * absolute time | 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16
  80. * @ max capacity ------******---------------******---------------
  81. * @ half capacity ------************---------************---------
  82. * clock pelt | 1| 2| 3| 4| 7| 8| 9| 10| 11|14|15|16
  83. *
  84. */
  85. static inline void update_rq_clock_pelt(struct rq *rq, s64 delta)
  86. {
  87. if (unlikely(is_idle_task(rq->curr))) {
  88. _update_idle_rq_clock_pelt(rq);
  89. return;
  90. }
  91. /*
  92. * When a rq runs at a lower compute capacity, it will need
  93. * more time to do the same amount of work than at max
  94. * capacity. In order to be invariant, we scale the delta to
  95. * reflect how much work has been really done.
  96. * Running longer results in stealing idle time that will
  97. * disturb the load signal compared to max capacity. This
  98. * stolen idle time will be automatically reflected when the
  99. * rq will be idle and the clock will be synced with
  100. * rq_clock_task.
  101. */
  102. /*
  103. * Scale the elapsed time to reflect the real amount of
  104. * computation
  105. */
  106. delta = cap_scale(delta, arch_scale_cpu_capacity(cpu_of(rq)));
  107. delta = cap_scale(delta, arch_scale_freq_capacity(cpu_of(rq)));
  108. rq->clock_pelt += delta;
  109. }
  110. extern unsigned int sched_pelt_lshift;
  111. /*
  112. * absolute time |1 |2 |3 |4 |5 |6 |
  113. * @ mult = 1 --------****************--------****************-
  114. * @ mult = 2 --------********----------------********---------
  115. * @ mult = 4 --------****--------------------****-------------
  116. * clock task mult
  117. * @ mult = 2 | | |2 |3 | | | | |5 |6 | | |
  118. * @ mult = 4 | | | | |2|3| | | | | | | | | | |5|6| | | | | | |
  119. *
  120. */
  121. static inline void update_rq_clock_task_mult(struct rq *rq, s64 delta)
  122. {
  123. delta <<= READ_ONCE(sched_pelt_lshift);
  124. rq->clock_task_mult += delta;
  125. update_rq_clock_pelt(rq, delta);
  126. }
  127. /*
  128. * When rq becomes idle, we have to check if it has lost idle time
  129. * because it was fully busy. A rq is fully used when the /Sum util_sum
  130. * is greater or equal to:
  131. * (LOAD_AVG_MAX - 1024 + rq->cfs.avg.period_contrib) << SCHED_CAPACITY_SHIFT;
  132. * For optimization and computing rounding purpose, we don't take into account
  133. * the position in the current window (period_contrib) and we use the higher
  134. * bound of util_sum to decide.
  135. */
  136. static inline void update_idle_rq_clock_pelt(struct rq *rq)
  137. {
  138. u32 divider = ((LOAD_AVG_MAX - 1024) << SCHED_CAPACITY_SHIFT) - LOAD_AVG_MAX;
  139. u32 util_sum = rq->cfs.avg.util_sum;
  140. util_sum += rq->avg_rt.util_sum;
  141. util_sum += rq->avg_dl.util_sum;
  142. /*
  143. * Reflecting stolen time makes sense only if the idle
  144. * phase would be present at max capacity. As soon as the
  145. * utilization of a rq has reached the maximum value, it is
  146. * considered as an always running rq without idle time to
  147. * steal. This potential idle time is considered as lost in
  148. * this case. We keep track of this lost idle time compare to
  149. * rq's clock_task.
  150. */
  151. if (util_sum >= divider)
  152. rq->lost_idle_time += rq_clock_task_mult(rq) - rq->clock_pelt;
  153. _update_idle_rq_clock_pelt(rq);
  154. }
  155. #ifdef CONFIG_CFS_BANDWIDTH
  156. static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
  157. {
  158. u64 throttled;
  159. if (unlikely(cfs_rq->throttle_count))
  160. throttled = U64_MAX;
  161. else
  162. throttled = cfs_rq->throttled_clock_pelt_time;
  163. u64_u32_store(cfs_rq->throttled_pelt_idle, throttled);
  164. }
  165. /* rq->task_clock normalized against any time this cfs_rq has spent throttled */
  166. static inline u64 cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
  167. {
  168. if (unlikely(cfs_rq->throttle_count))
  169. return cfs_rq->throttled_clock_pelt - cfs_rq->throttled_clock_pelt_time;
  170. return rq_clock_pelt(rq_of(cfs_rq)) - cfs_rq->throttled_clock_pelt_time;
  171. }
  172. #else
  173. static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq) { }
  174. static inline u64 cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
  175. {
  176. return rq_clock_pelt(rq_of(cfs_rq));
  177. }
  178. #endif
  179. #else
  180. static inline int
  181. update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
  182. {
  183. return 0;
  184. }
  185. static inline int
  186. update_rt_rq_load_avg(u64 now, struct rq *rq, int running)
  187. {
  188. return 0;
  189. }
  190. static inline int
  191. update_dl_rq_load_avg(u64 now, struct rq *rq, int running)
  192. {
  193. return 0;
  194. }
  195. static inline int
  196. update_thermal_load_avg(u64 now, struct rq *rq, u64 capacity)
  197. {
  198. return 0;
  199. }
  200. static inline u64 thermal_load_avg(struct rq *rq)
  201. {
  202. return 0;
  203. }
  204. static inline int
  205. update_irq_load_avg(struct rq *rq, u64 running)
  206. {
  207. return 0;
  208. }
  209. static inline u64 rq_clock_task_mult(struct rq *rq)
  210. {
  211. return rq_clock_task(rq);
  212. }
  213. static inline u64 rq_clock_pelt(struct rq *rq)
  214. {
  215. return rq_clock_task_mult(rq);
  216. }
  217. static inline void
  218. update_rq_clock_task_mult(struct rq *rq, s64 delta) { }
  219. static inline void
  220. update_idle_rq_clock_pelt(struct rq *rq) { }
  221. static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq) { }
  222. #endif