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