1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Per Entity Load Tracking
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 #include <linux/sched.h>
28 #include "sched.h"
29 #include "pelt.h"
30
31 int pelt_load_avg_period = PELT32_LOAD_AVG_PERIOD;
32 int pelt_load_avg_max = PELT32_LOAD_AVG_MAX;
33 const u32 *pelt_runnable_avg_yN_inv = pelt32_runnable_avg_yN_inv;
34
set_pelt(char * str)35 static int __init set_pelt(char *str)
36 {
37 int rc, num;
38
39 rc = kstrtoint(str, 0, &num);
40 if (rc) {
41 pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
42 return 0;
43 }
44
45 switch (num) {
46 case PELT8_LOAD_AVG_PERIOD:
47 pelt_load_avg_period = PELT8_LOAD_AVG_PERIOD;
48 pelt_load_avg_max = PELT8_LOAD_AVG_MAX;
49 pelt_runnable_avg_yN_inv = pelt8_runnable_avg_yN_inv;
50 pr_info("PELT half life is set to %dms\n", num);
51 break;
52 case PELT32_LOAD_AVG_PERIOD:
53 pelt_load_avg_period = PELT32_LOAD_AVG_PERIOD;
54 pelt_load_avg_max = PELT32_LOAD_AVG_MAX;
55 pelt_runnable_avg_yN_inv = pelt32_runnable_avg_yN_inv;
56 pr_info("PELT half life is set to %dms\n", num);
57 break;
58 default:
59 pr_err("Default PELT half life is 32ms\n");
60 }
61
62 return 0;
63 }
64
65 early_param("pelt", set_pelt);
66
67 /*
68 * Approximate:
69 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
70 */
decay_load(u64 val,u64 n)71 static u64 decay_load(u64 val, u64 n)
72 {
73 unsigned int local_n;
74
75 if (unlikely(n > LOAD_AVG_PERIOD * 0x3f)) {
76 return 0;
77 }
78
79 /* after bounds checking we can collapse to 32-bit */
80 local_n = n;
81
82 /*
83 * As y^PERIOD = 1/2, we can combine
84 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
85 * With a look-up table which covers y^n (n<PERIOD)
86 *
87 * To achieve constant time decay_load.
88 */
89 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
90 val >>= local_n / LOAD_AVG_PERIOD;
91 local_n %= LOAD_AVG_PERIOD;
92 }
93
94 val = mul_u64_u32_shr(val, pelt_runnable_avg_yN_inv[local_n], 0x20);
95 return val;
96 }
97
__accumulate_pelt_segments(u64 periods,u32 d1,u32 d3)98 static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3)
99 {
100 u32 c1, c2, c3 = d3; /* y^0 == 1 */
101
102 /*
103 * c1 = d1 y^p
104 */
105 c1 = decay_load((u64)d1, periods);
106
107 /*
108 * p-1
109 * c2 = 1024 \Sum y^n
110 * n=1
111 *
112 * inf inf
113 * = 1024 ( \Sum y^n - \Sum y^n - y^0 )
114 * n=0 n=p
115 */
116 c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 0x400;
117
118 return c1 + c2 + c3;
119 }
120
121 /*
122 * Accumulate the three separate parts of the sum; d1 the remainder
123 * of the last (incomplete) period, d2 the span of full periods and d3
124 * the remainder of the (incomplete) current period.
125 *
126 * d1 d2 d3
127 * ^ ^ ^
128 * | | |
129 * |<->|<----------------->|<--->|
130 * ... |---x---|------| ... |------|-----x (now)
131 *
132 * p-1
133 * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0
134 * n=1
135 *
136 * = u y^p + (Step 1)
137 *
138 * p-1
139 * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2)
140 * n=1
141 */
accumulate_sum(u64 delta,struct sched_avg * sa,unsigned long load,unsigned long runnable,int running)142 static __always_inline u32 accumulate_sum(u64 delta, struct sched_avg *sa, unsigned long load, unsigned long runnable,
143 int running)
144 {
145 u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */
146 u64 periods;
147
148 delta += sa->period_contrib;
149 periods = delta / 0x400; /* A period is 1024us (~1ms) */
150
151 /*
152 * Step 1: decay old *_sum if we crossed period boundaries.
153 */
154 if (periods) {
155 sa->load_sum = decay_load(sa->load_sum, periods);
156 sa->runnable_sum = decay_load(sa->runnable_sum, periods);
157 sa->util_sum = decay_load((u64)(sa->util_sum), periods);
158
159 /*
160 * Step 2
161 */
162 delta %= 0x400;
163 if (load) {
164 /*
165 * This relies on the:
166 *
167 * if (!load)
168 * runnable = running = 0;
169 *
170 * clause from ___update_load_sum(); this results in
171 * the below usage of @contrib to dissapear entirely,
172 * so no point in calculating it.
173 */
174 contrib = __accumulate_pelt_segments(periods, 0x400 - sa->period_contrib, delta);
175 }
176 }
177 sa->period_contrib = delta;
178
179 if (load) {
180 sa->load_sum += load * contrib;
181 }
182 if (runnable) {
183 sa->runnable_sum += runnable * contrib << SCHED_CAPACITY_SHIFT;
184 }
185 if (running) {
186 sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
187 }
188
189 return periods;
190 }
191
192 /*
193 * We can represent the historical contribution to runnable average as the
194 * coefficients of a geometric series. To do this we sub-divide our runnable
195 * history into segments of approximately 1ms (1024us); label the segment that
196 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
197 *
198 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
199 * p0 p1 p2
200 * (now) (~1ms ago) (~2ms ago)
201 *
202 * Let u_i denote the fraction of p_i that the entity was runnable.
203 *
204 * We then designate the fractions u_i as our co-efficients, yielding the
205 * following representation of historical load:
206 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
207 *
208 * We choose y based on the with of a reasonably scheduling period, fixing:
209 * y^32 = 0.5
210 *
211 * This means that the contribution to load ~32ms ago (u_32) will be weighted
212 * approximately half as much as the contribution to load within the last ms
213 * (u_0).
214 *
215 * When a period "rolls over" and we have new u_0`, multiplying the previous
216 * sum again by y is sufficient to update:
217 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
218 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
219 */
___update_load_sum(u64 now,struct sched_avg * sa,unsigned long load,unsigned long runnable,int running)220 static __always_inline int ___update_load_sum(u64 now, struct sched_avg *sa, unsigned long load, unsigned long runnable,
221 int running)
222 {
223 u64 delta;
224
225 delta = now - sa->last_update_time;
226 /*
227 * This should only happen when time goes backwards, which it
228 * unfortunately does during sched clock init when we swap over to TSC.
229 */
230 if ((s64)delta < 0) {
231 sa->last_update_time = now;
232 return 0;
233 }
234
235 /*
236 * Use 1024ns as the unit of measurement since it's a reasonable
237 * approximation of 1us and fast to compute.
238 */
239 delta >>= 0xa;
240 if (!delta) {
241 return 0;
242 }
243
244 sa->last_update_time += delta << 0xa;
245
246 /*
247 * running is a subset of runnable (weight) so running can't be set if
248 * runnable is clear. But there are some corner cases where the current
249 * se has been already dequeued but cfs_rq->curr still points to it.
250 * This means that weight will be 0 but not running for a sched_entity
251 * but also for a cfs_rq if the latter becomes idle. As an example,
252 * this happens during idle_balance() which calls
253 * update_blocked_averages().
254 *
255 * Also see the comment in accumulate_sum().
256 */
257 if (!load) {
258 runnable = running = 0;
259 }
260
261 /*
262 * Now we know we crossed measurement unit boundaries. The *_avg
263 * accrues by two steps:
264 *
265 * Step 1: accumulate *_sum since last_update_time. If we haven't
266 * crossed period boundaries, finish.
267 */
268 if (!accumulate_sum(delta, sa, load, runnable, running)) {
269 return 0;
270 }
271
272 return 1;
273 }
274
275 /*
276 * When syncing *_avg with *_sum, we must take into account the current
277 * position in the PELT segment otherwise the remaining part of the segment
278 * will be considered as idle time whereas it's not yet elapsed and this will
279 * generate unwanted oscillation in the range [1002..1024[.
280 *
281 * The max value of *_sum varies with the position in the time segment and is
282 * equals to :
283 *
284 * LOAD_AVG_MAX*y + sa->period_contrib
285 *
286 * which can be simplified into:
287 *
288 * LOAD_AVG_MAX - 1024 + sa->period_contrib
289 *
290 * because LOAD_AVG_MAX*y == LOAD_AVG_MAX-1024
291 *
292 * The same care must be taken when a sched entity is added, updated or
293 * removed from a cfs_rq and we need to update sched_avg. Scheduler entities
294 * and the cfs rq, to which they are attached, have the same position in the
295 * time segment because they use the same clock. This means that we can use
296 * the period_contrib of cfs_rq when updating the sched_avg of a sched_entity
297 * if it's more convenient.
298 */
___update_load_avg(struct sched_avg * sa,unsigned long load)299 static __always_inline void ___update_load_avg(struct sched_avg *sa, unsigned long load)
300 {
301 u32 divider = get_pelt_divider(sa);
302
303 /*
304 * Step 2: update *_avg.
305 */
306 sa->load_avg = div_u64(load * sa->load_sum, divider);
307 sa->runnable_avg = div_u64(sa->runnable_sum, divider);
308 WRITE_ONCE(sa->util_avg, sa->util_sum / divider);
309 }
310
311 /*
312 * sched_entity
313 *
314 * task:
315 * se_weight() = se->load.weight
316 * se_runnable() = !!on_rq
317 *
318 * group: [ see update_cfs_group() ]
319 * se_weight() = tg->weight * grq->load_avg / tg->load_avg
320 * se_runnable() = grq->h_nr_running
321 *
322 * runnable_sum = se_runnable() * runnable = grq->runnable_sum
323 * runnable_avg = runnable_sum
324 *
325 * load_sum := runnable
326 * load_avg = se_weight(se) * load_sum
327 *
328 * cfq_rq
329 *
330 * runnable_sum = \Sum se->avg.runnable_sum
331 * runnable_avg = \Sum se->avg.runnable_avg
332 *
333 * load_sum = \Sum se_weight(se) * se->avg.load_sum
334 * load_avg = \Sum se->avg.load_avg
335 */
336
__update_load_avg_blocked_se(u64 now,struct sched_entity * se)337 int __update_load_avg_blocked_se(u64 now, struct sched_entity *se)
338 {
339 if (___update_load_sum(now, &se->avg, 0, 0, 0)) {
340 ___update_load_avg(&se->avg, se_weight(se));
341 trace_pelt_se_tp(se);
342 return 1;
343 }
344
345 return 0;
346 }
347
__update_load_avg_se(u64 now,struct cfs_rq * cfs_rq,struct sched_entity * se)348 int __update_load_avg_se(u64 now, struct cfs_rq *cfs_rq, struct sched_entity *se)
349 {
350 if (___update_load_sum(now, &se->avg, !!se->on_rq, se_runnable(se), cfs_rq->curr == se)) {
351 ___update_load_avg(&se->avg, se_weight(se));
352 cfs_se_util_change(&se->avg);
353 trace_pelt_se_tp(se);
354 return 1;
355 }
356
357 return 0;
358 }
359
__update_load_avg_cfs_rq(u64 now,struct cfs_rq * cfs_rq)360 int __update_load_avg_cfs_rq(u64 now, struct cfs_rq *cfs_rq)
361 {
362 if (___update_load_sum(now, &cfs_rq->avg, scale_load_down(cfs_rq->load.weight), cfs_rq->h_nr_running,
363 cfs_rq->curr != NULL)) {
364 ___update_load_avg(&cfs_rq->avg, 1);
365 trace_pelt_cfs_tp(cfs_rq);
366 return 1;
367 }
368
369 return 0;
370 }
371
372 /*
373 * rt_rq
374 *
375 * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
376 * util_sum = cpu_scale * load_sum
377 * runnable_sum = util_sum
378 *
379 * load_avg and runnable_avg are not supported and meaningless.
380 *
381 */
382
update_rt_rq_load_avg(u64 now,struct rq * rq,int running)383 int update_rt_rq_load_avg(u64 now, struct rq *rq, int running)
384 {
385 if (___update_load_sum(now, &rq->avg_rt, running, running, running)) {
386 ___update_load_avg(&rq->avg_rt, 1);
387 trace_pelt_rt_tp(rq);
388 return 1;
389 }
390
391 return 0;
392 }
393
394 /*
395 * dl_rq
396 *
397 * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
398 * util_sum = cpu_scale * load_sum
399 * runnable_sum = util_sum
400 *
401 * load_avg and runnable_avg are not supported and meaningless.
402 *
403 */
404
update_dl_rq_load_avg(u64 now,struct rq * rq,int running)405 int update_dl_rq_load_avg(u64 now, struct rq *rq, int running)
406 {
407 if (___update_load_sum(now, &rq->avg_dl, running, running, running)) {
408 ___update_load_avg(&rq->avg_dl, 1);
409 trace_pelt_dl_tp(rq);
410 return 1;
411 }
412
413 return 0;
414 }
415
416 #ifdef CONFIG_SCHED_THERMAL_PRESSURE
417 /*
418 * thermal
419 *
420 * load_sum = \Sum se->avg.load_sum but se->avg.load_sum is not tracked
421 *
422 * util_avg and runnable_load_avg are not supported and meaningless.
423 *
424 * Unlike rt/dl utilization tracking that track time spent by a cpu
425 * running a rt/dl task through util_avg, the average thermal pressure is
426 * tracked through load_avg. This is because thermal pressure signal is
427 * time weighted "delta" capacity unlike util_avg which is binary.
428 * "delta capacity" = actual capacity -
429 * capped capacity a cpu due to a thermal event.
430 */
431
update_thermal_load_avg(u64 now,struct rq * rq,u64 capacity)432 int update_thermal_load_avg(u64 now, struct rq *rq, u64 capacity)
433 {
434 if (___update_load_sum(now, &rq->avg_thermal, capacity, capacity, capacity)) {
435 ___update_load_avg(&rq->avg_thermal, 1);
436 trace_pelt_thermal_tp(rq);
437 return 1;
438 }
439
440 return 0;
441 }
442 #endif
443
444 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
445 /*
446 * irq
447 *
448 * util_sum = \Sum se->avg.util_sum but se->avg.util_sum is not tracked
449 * util_sum = cpu_scale * load_sum
450 * runnable_sum = util_sum
451 *
452 * load_avg and runnable_avg are not supported and meaningless.
453 *
454 */
455
update_irq_load_avg(struct rq * rq,u64 running)456 int update_irq_load_avg(struct rq *rq, u64 running)
457 {
458 int ret = 0;
459
460 /*
461 * We can't use clock_pelt because irq time is not accounted in
462 * clock_task. Instead we directly scale the running time to
463 * reflect the real amount of computation
464 */
465 running = cap_scale(running, arch_scale_freq_capacity(cpu_of(rq)));
466 running = cap_scale(running, arch_scale_cpu_capacity(cpu_of(rq)));
467
468 /*
469 * We know the time that has been used by interrupt since last update
470 * but we don't when. Let be pessimistic and assume that interrupt has
471 * happened just before the update. This is not so far from reality
472 * because interrupt will most probably wake up task and trig an update
473 * of rq clock during which the metric is updated.
474 * We start to decay with normal context time and then we add the
475 * interrupt context time.
476 * We can safely remove running from rq->clock because
477 * rq->clock += delta with delta >= running
478 */
479 ret = ___update_load_sum(rq->clock - running, &rq->avg_irq, 0, 0, 0);
480 ret += ___update_load_sum(rq->clock, &rq->avg_irq, 1, 1, 1);
481 if (ret) {
482 ___update_load_avg(&rq->avg_irq, 1);
483 trace_pelt_irq_tp(rq);
484 }
485
486 return ret;
487 }
488 #endif
489