pelt.h 6.0 KB

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