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