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