^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * menu.c - the menu idle governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2009 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Arjan van de Ven <arjan@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sched/loadavg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sched/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define BUCKETS 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define INTERVAL_SHIFT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define INTERVALS (1UL << INTERVAL_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define RESOLUTION 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DECAY 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define MAX_INTERESTING (50000 * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Concepts and ideas behind the menu governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * For the menu governor, there are 3 decision factors for picking a C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * 1) Energy break even point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * 2) Performance impact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * 3) Latency tolerance (from pmqos infrastructure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * These these three factors are treated independently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * Energy break even point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * -----------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * C state entry and exit have an energy cost, and a certain amount of time in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * the C state is required to actually break even on this cost. CPUIDLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * provides us this duration in the "target_residency" field. So all that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * need is a good prediction of how long we'll be idle. Like the traditional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * menu governor, we start with the actual known "next timer event" time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Since there are other source of wakeups (interrupts for example) than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * the next timer event, this estimation is rather optimistic. To get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * more realistic estimate, a correction factor is applied to the estimate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * that is based on historic behavior. For example, if in the past the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * duration always was 50% of the next timer tick, the correction factor will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * be 0.5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * menu uses a running average for this correction factor, however it uses a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * set of factors, not just a single factor. This stems from the realization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * that the ratio is dependent on the order of magnitude of the expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * duration; if we expect 500 milliseconds of idle time the likelihood of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * getting an interrupt very early is much higher than if we expect 50 micro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * seconds of idle time. A second independent factor that has big impact on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * the actual factor is if there is (disk) IO outstanding or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * (as a special twist, we consider every sleep longer than 50 milliseconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * as perfect; there are no power gains for sleeping longer than this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * For these two reasons we keep an array of 12 independent factors, that gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * indexed based on the magnitude of the expected duration as well as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * "is IO outstanding" property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Repeatable-interval-detector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * ----------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * There are some cases where "next timer" is a completely unusable predictor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Those cases where the interval is fixed, for example due to hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * interrupt mitigation, but also due to fixed transfer rate devices such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * mice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * For this, we use a different predictor: We track the duration of the last 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * intervals and if the stand deviation of these 8 intervals is below a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * threshold value, we use the average of these intervals as prediction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * Limiting Performance Impact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * ---------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * C states, especially those with large exit latencies, can have a real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * noticeable impact on workloads, which is not acceptable for most sysadmins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * and in addition, less performance has a power price of its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * As a general rule of thumb, menu assumes that the following heuristic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * holds:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * The busier the system, the less impact of C states is acceptable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * This rule-of-thumb is implemented using a performance-multiplier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * If the exit latency times the performance multiplier is longer than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * the predicted duration, the C state is not considered a candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * for selection due to a too high performance impact. So the higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * this multiplier is, the longer we need to be idle to pick a deep C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * state, and thus the less likely a busy CPU will hit such a deep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * C state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Two factors are used in determing this multiplier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * a value of 10 is added for each point of "per cpu load average" we have.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * a value of 5 points is added for each process that is waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * IO on this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * (these values are experimentally determined)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * The load average factor gives a longer term (few seconds) input to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * decision, while the iowait value gives a cpu local instantanious input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * The iowait factor may look low, but realize that this is also already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * represented in the system load average.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct menu_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int tick_wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) u64 next_timer_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned int bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int correction_factor[BUCKETS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int intervals[INTERVALS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int interval_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline int which_bucket(u64 duration_ns, unsigned long nr_iowaiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int bucket = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * We keep two groups of stats; one with no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * IO pending, one without.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * This allows us to calculate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * E(duration)|iowait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (nr_iowaiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) bucket = BUCKETS/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (duration_ns < 10ULL * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (duration_ns < 100ULL * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return bucket + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (duration_ns < 1000ULL * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return bucket + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (duration_ns < 10000ULL * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return bucket + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (duration_ns < 100000ULL * NSEC_PER_USEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return bucket + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return bucket + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Return a multiplier for the exit latency that is intended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * to take performance requirements into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * The more performance critical we estimate the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * to be, the higher this multiplier, and thus the higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * the barrier to go to an expensive C state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static inline int performance_multiplier(unsigned long nr_iowaiters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* for IO wait tasks (per cpu!) we add 10x each */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 1 + 10 * nr_iowaiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static DEFINE_PER_CPU(struct menu_device, menu_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * Try detecting repeating patterns by keeping track of the last 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * intervals, and checking if the standard deviation of that set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * of points is below a threshold. If it is... then use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * average of these 8 points as the estimated value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static unsigned int get_typical_interval(struct menu_device *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unsigned int predicted_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int i, divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned int min, max, thresh, avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) uint64_t sum, variance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) thresh = INT_MAX; /* Discard outliers above this value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* First calculate the average of past intervals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) min = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) divisor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) for (i = 0; i < INTERVALS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned int value = data->intervals[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (value <= thresh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sum += value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) divisor++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (value > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) max = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (value < min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) min = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * If the result of the computation is going to be discarded anyway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * avoid the computation altogether.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (min >= predicted_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (divisor == INTERVALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) avg = sum >> INTERVAL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) avg = div_u64(sum, divisor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* Then try to determine variance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) variance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) for (i = 0; i < INTERVALS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) unsigned int value = data->intervals[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (value <= thresh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int64_t diff = (int64_t)value - avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) variance += diff * diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (divisor == INTERVALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) variance >>= INTERVAL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) do_div(variance, divisor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * The typical interval is obtained when standard deviation is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * small (stddev <= 20 us, variance <= 400 us^2) or standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * deviation is small compared to the average interval (avg >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * 6*stddev, avg^2 > 36*variance). The average is smaller than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * UINT_MAX aka U32_MAX, so computing its square does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * overflow a u64. We simply reject this candidate average if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * the standard deviation is greater than 715 s (which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * rather unlikely).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Use this result only if there is no timer to wake us up sooner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (likely(variance <= U64_MAX/36)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if ((((u64)avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) || variance <= 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * If we have outliers to the upside in our distribution, discard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * those by setting the threshold to exclude these outliers, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * calculate the average and standard deviation again. Once we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * down to the bottom 3/4 of our samples, stop excluding samples.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * This can deal with workloads that have long pauses interspersed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * with sporadic activity with a bunch of short pauses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if ((divisor * 4) <= INTERVALS * 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) thresh = max - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * menu_select - selects the next idle state to enter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @drv: cpuidle driver containing state data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * @stop_tick: indication on whether or not to stop the tick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bool *stop_tick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct menu_device *data = this_cpu_ptr(&menu_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) unsigned int predicted_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) u64 predicted_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) u64 interactivity_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) unsigned long nr_iowaiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ktime_t delta_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int i, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (data->needs_update) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) menu_update(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) data->needs_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* determine the expected residency time, round up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) data->next_timer_ns = tick_nohz_get_sleep_length(&delta_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) nr_iowaiters = nr_iowait_cpu(dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) data->bucket = which_bucket(data->next_timer_ns, nr_iowaiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (unlikely(drv->state_count <= 1 || latency_req == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ((data->next_timer_ns < drv->states[1].target_residency_ns ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) latency_req < drv->states[1].exit_latency_ns) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) !dev->states_usage[0].disable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * In this case state[0] will be used no matter what, so return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * it right away and keep the tick running if state[0] is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * polling one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *stop_tick = !(drv->states[0].flags & CPUIDLE_FLAG_POLLING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Round up the result for half microseconds. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) predicted_us = div_u64(data->next_timer_ns *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) data->correction_factor[data->bucket] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) (RESOLUTION * DECAY * NSEC_PER_USEC) / 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) RESOLUTION * DECAY * NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Use the lowest expected idle interval to pick the idle state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) predicted_ns = (u64)min(predicted_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) get_typical_interval(data, predicted_us)) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (tick_nohz_tick_stopped()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * If the tick is already stopped, the cost of possible short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * idle duration misprediction is much higher, because the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * may be stuck in a shallow idle state for a long time as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * result of it. In that case say we might mispredict and use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * the known time till the closest timer event for the idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * state selection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (predicted_ns < TICK_NSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) predicted_ns = delta_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Use the performance multiplier and the user-configurable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * latency_req to determine the maximum exit latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) interactivity_req = div64_u64(predicted_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) performance_multiplier(nr_iowaiters));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (latency_req > interactivity_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) latency_req = interactivity_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Find the idle state with the lowest power while satisfying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * our constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) for (i = 0; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct cpuidle_state *s = &drv->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (idx == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) idx = i; /* first enabled state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (s->target_residency_ns > predicted_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Use a physical idle state, not busy polling, unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * a timer is going to trigger soon enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) s->exit_latency_ns <= latency_req &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) s->target_residency_ns <= data->next_timer_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) predicted_ns = s->target_residency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (predicted_ns < TICK_NSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!tick_nohz_tick_stopped()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * If the state selected so far is shallow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * waking up early won't hurt, so retain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * tick in that case and let the governor run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * again in the next iteration of the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) predicted_ns = drv->states[idx].target_residency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * If the state selected so far is shallow and this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * state's target residency matches the time till the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * closest timer event, select this one to avoid getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * stuck in the shallow one for too long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (drv->states[idx].target_residency_ns < TICK_NSEC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) s->target_residency_ns <= delta_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (s->exit_latency_ns > latency_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (idx == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) idx = 0; /* No states enabled. Must use 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Don't stop the tick if the selected state is a polling one or if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * expected idle duration is shorter than the tick period length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) predicted_ns < TICK_NSEC) && !tick_nohz_tick_stopped()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *stop_tick = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (idx > 0 && drv->states[idx].target_residency_ns > delta_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * The tick is not going to be stopped and the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * residency of the state to be returned is not within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * the time until the next timer event including the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * tick, so try to correct that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) for (i = idx - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (drv->states[i].target_residency_ns <= delta_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * menu_reflect - records that data structures need update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @index: the index of actual entered state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * NOTE: it's important to be fast here because this operation will add to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * the overall exit latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static void menu_reflect(struct cpuidle_device *dev, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct menu_device *data = this_cpu_ptr(&menu_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) dev->last_state_idx = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) data->needs_update = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) data->tick_wakeup = tick_nohz_idle_got_tick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * menu_update - attempts to guess what happened after entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * @drv: cpuidle driver containing state data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct menu_device *data = this_cpu_ptr(&menu_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int last_idx = dev->last_state_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct cpuidle_state *target = &drv->states[last_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) u64 measured_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) unsigned int new_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Try to figure out how much time passed between entry to low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * power state and occurrence of the wakeup event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * If the entered idle state didn't support residency measurements,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * we use them anyway if they are short, and if long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * truncate to the whole expected time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Any measured amount of time will include the exit latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * Since we are interested in when the wakeup begun, not when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * was completed, we must subtract the exit latency. However, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * the measured amount of time is less than the exit latency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * assume the state was never reached and the exit latency is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (data->tick_wakeup && data->next_timer_ns > TICK_NSEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * The nohz code said that there wouldn't be any events within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * the tick boundary (if the tick was stopped), but the idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * duration predictor had a differing opinion. Since the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * was woken up by a tick (that wasn't stopped after all), the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * predictor was not quite right, so assume that the CPU could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * have been idle long (but not forever) to help the idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * duration predictor do a better job next time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) measured_ns = 9 * MAX_INTERESTING / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) } else if ((drv->states[last_idx].flags & CPUIDLE_FLAG_POLLING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) dev->poll_time_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * The CPU exited the "polling" state due to a time limit, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * the idle duration prediction leading to the selection of that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * state was inaccurate. If a better prediction had been made,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * the CPU might have been woken up from idle by the next timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * Assume that to be the case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) measured_ns = data->next_timer_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /* measured value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) measured_ns = dev->last_residency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Deduct exit latency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (measured_ns > 2 * target->exit_latency_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) measured_ns -= target->exit_latency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) measured_ns /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* Make sure our coefficients do not exceed unity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (measured_ns > data->next_timer_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) measured_ns = data->next_timer_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /* Update our correction ratio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) new_factor = data->correction_factor[data->bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) new_factor -= new_factor / DECAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (data->next_timer_ns > 0 && measured_ns < MAX_INTERESTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) new_factor += div64_u64(RESOLUTION * measured_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) data->next_timer_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * we were idle so long that we count it as a perfect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * prediction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) new_factor += RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * We don't want 0 as factor; we always want at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * a tiny bit of estimated time. Fortunately, due to rounding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * new_factor will stay nonzero regardless of measured_us values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * and the compiler can eliminate this test as long as DECAY > 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (DECAY == 1 && unlikely(new_factor == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) new_factor = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) data->correction_factor[data->bucket] = new_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* update the repeating-pattern data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) data->intervals[data->interval_ptr++] = ktime_to_us(measured_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (data->interval_ptr >= INTERVALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) data->interval_ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * menu_enable_device - scans a CPU's states and does setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * @drv: cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static int menu_enable_device(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) memset(data, 0, sizeof(struct menu_device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * if the correction factor is 0 (eg first time init or cpu hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * etc), we actually want to start out with a unity factor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) for(i = 0; i < BUCKETS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) data->correction_factor[i] = RESOLUTION * DECAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static struct cpuidle_governor menu_governor = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) .name = "menu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) .rating = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) .enable = menu_enable_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) .select = menu_select,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) .reflect = menu_reflect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * init_menu - initializes the governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int __init init_menu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return cpuidle_register_governor(&menu_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) postcore_initcall(init_menu);