Linux Audio

Check our new training course

Linux BSP development engineering services

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