menu.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * menu.c - the menu idle governor
  3. *
  4. * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
  5. * Copyright (C) 2009 Intel Corporation
  6. * Author:
  7. * Arjan van de Ven <arjan@linux.intel.com>
  8. *
  9. * This code is licenced under the GPL version 2 as described
  10. * in the COPYING file that acompanies the Linux Kernel.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/cpuidle.h>
  14. #include <linux/pm_qos_params.h>
  15. #include <linux/time.h>
  16. #include <linux/ktime.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/tick.h>
  19. #include <linux/sched.h>
  20. #define BUCKETS 12
  21. #define RESOLUTION 1024
  22. #define DECAY 4
  23. #define MAX_INTERESTING 50000
  24. /*
  25. * Concepts and ideas behind the menu governor
  26. *
  27. * For the menu governor, there are 3 decision factors for picking a C
  28. * state:
  29. * 1) Energy break even point
  30. * 2) Performance impact
  31. * 3) Latency tolerance (from pmqos infrastructure)
  32. * These these three factors are treated independently.
  33. *
  34. * Energy break even point
  35. * -----------------------
  36. * C state entry and exit have an energy cost, and a certain amount of time in
  37. * the C state is required to actually break even on this cost. CPUIDLE
  38. * provides us this duration in the "target_residency" field. So all that we
  39. * need is a good prediction of how long we'll be idle. Like the traditional
  40. * menu governor, we start with the actual known "next timer event" time.
  41. *
  42. * Since there are other source of wakeups (interrupts for example) than
  43. * the next timer event, this estimation is rather optimistic. To get a
  44. * more realistic estimate, a correction factor is applied to the estimate,
  45. * that is based on historic behavior. For example, if in the past the actual
  46. * duration always was 50% of the next timer tick, the correction factor will
  47. * be 0.5.
  48. *
  49. * menu uses a running average for this correction factor, however it uses a
  50. * set of factors, not just a single factor. This stems from the realization
  51. * that the ratio is dependent on the order of magnitude of the expected
  52. * duration; if we expect 500 milliseconds of idle time the likelihood of
  53. * getting an interrupt very early is much higher than if we expect 50 micro
  54. * seconds of idle time. A second independent factor that has big impact on
  55. * the actual factor is if there is (disk) IO outstanding or not.
  56. * (as a special twist, we consider every sleep longer than 50 milliseconds
  57. * as perfect; there are no power gains for sleeping longer than this)
  58. *
  59. * For these two reasons we keep an array of 12 independent factors, that gets
  60. * indexed based on the magnitude of the expected duration as well as the
  61. * "is IO outstanding" property.
  62. *
  63. * Limiting Performance Impact
  64. * ---------------------------
  65. * C states, especially those with large exit latencies, can have a real
  66. * noticable impact on workloads, which is not acceptable for most sysadmins,
  67. * and in addition, less performance has a power price of its own.
  68. *
  69. * As a general rule of thumb, menu assumes that the following heuristic
  70. * holds:
  71. * The busier the system, the less impact of C states is acceptable
  72. *
  73. * This rule-of-thumb is implemented using a performance-multiplier:
  74. * If the exit latency times the performance multiplier is longer than
  75. * the predicted duration, the C state is not considered a candidate
  76. * for selection due to a too high performance impact. So the higher
  77. * this multiplier is, the longer we need to be idle to pick a deep C
  78. * state, and thus the less likely a busy CPU will hit such a deep
  79. * C state.
  80. *
  81. * Two factors are used in determing this multiplier:
  82. * a value of 10 is added for each point of "per cpu load average" we have.
  83. * a value of 5 points is added for each process that is waiting for
  84. * IO on this CPU.
  85. * (these values are experimentally determined)
  86. *
  87. * The load average factor gives a longer term (few seconds) input to the
  88. * decision, while the iowait value gives a cpu local instantanious input.
  89. * The iowait factor may look low, but realize that this is also already
  90. * represented in the system load average.
  91. *
  92. */
  93. struct menu_device {
  94. int last_state_idx;
  95. unsigned int expected_us;
  96. u64 predicted_us;
  97. unsigned int measured_us;
  98. unsigned int exit_us;
  99. unsigned int bucket;
  100. u64 correction_factor[BUCKETS];
  101. };
  102. #define LOAD_INT(x) ((x) >> FSHIFT)
  103. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  104. static int get_loadavg(void)
  105. {
  106. unsigned long this = this_cpu_load();
  107. return LOAD_INT(this) * 10 + LOAD_FRAC(this) / 10;
  108. }
  109. static inline int which_bucket(unsigned int duration)
  110. {
  111. int bucket = 0;
  112. /*
  113. * We keep two groups of stats; one with no
  114. * IO pending, one without.
  115. * This allows us to calculate
  116. * E(duration)|iowait
  117. */
  118. if (nr_iowait_cpu())
  119. bucket = BUCKETS/2;
  120. if (duration < 10)
  121. return bucket;
  122. if (duration < 100)
  123. return bucket + 1;
  124. if (duration < 1000)
  125. return bucket + 2;
  126. if (duration < 10000)
  127. return bucket + 3;
  128. if (duration < 100000)
  129. return bucket + 4;
  130. return bucket + 5;
  131. }
  132. /*
  133. * Return a multiplier for the exit latency that is intended
  134. * to take performance requirements into account.
  135. * The more performance critical we estimate the system
  136. * to be, the higher this multiplier, and thus the higher
  137. * the barrier to go to an expensive C state.
  138. */
  139. static inline int performance_multiplier(void)
  140. {
  141. int mult = 1;
  142. /* for higher loadavg, we are more reluctant */
  143. mult += 2 * get_loadavg();
  144. /* for IO wait tasks (per cpu!) we add 5x each */
  145. mult += 10 * nr_iowait_cpu();
  146. return mult;
  147. }
  148. static DEFINE_PER_CPU(struct menu_device, menu_devices);
  149. /**
  150. * menu_select - selects the next idle state to enter
  151. * @dev: the CPU
  152. */
  153. static int menu_select(struct cpuidle_device *dev)
  154. {
  155. struct menu_device *data = &__get_cpu_var(menu_devices);
  156. int latency_req = pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY);
  157. int i;
  158. int multiplier;
  159. data->last_state_idx = 0;
  160. data->exit_us = 0;
  161. /* Special case when user has set very strict latency requirement */
  162. if (unlikely(latency_req == 0))
  163. return 0;
  164. /* determine the expected residency time, round up */
  165. data->expected_us =
  166. DIV_ROUND_UP((u32)ktime_to_ns(tick_nohz_get_sleep_length()), 1000);
  167. data->bucket = which_bucket(data->expected_us);
  168. multiplier = performance_multiplier();
  169. /*
  170. * if the correction factor is 0 (eg first time init or cpu hotplug
  171. * etc), we actually want to start out with a unity factor.
  172. */
  173. if (data->correction_factor[data->bucket] == 0)
  174. data->correction_factor[data->bucket] = RESOLUTION * DECAY;
  175. /* Make sure to round up for half microseconds */
  176. data->predicted_us = DIV_ROUND_CLOSEST(
  177. data->expected_us * data->correction_factor[data->bucket],
  178. RESOLUTION * DECAY);
  179. /*
  180. * We want to default to C1 (hlt), not to busy polling
  181. * unless the timer is happening really really soon.
  182. */
  183. if (data->expected_us > 5)
  184. data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
  185. /* find the deepest idle state that satisfies our constraints */
  186. for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++) {
  187. struct cpuidle_state *s = &dev->states[i];
  188. if (s->target_residency > data->predicted_us)
  189. break;
  190. if (s->exit_latency > latency_req)
  191. break;
  192. if (s->exit_latency * multiplier > data->predicted_us)
  193. break;
  194. data->exit_us = s->exit_latency;
  195. data->last_state_idx = i;
  196. }
  197. return data->last_state_idx;
  198. }
  199. /**
  200. * menu_reflect - attempts to guess what happened after entry
  201. * @dev: the CPU
  202. *
  203. * NOTE: it's important to be fast here because this operation will add to
  204. * the overall exit latency.
  205. */
  206. static void menu_reflect(struct cpuidle_device *dev)
  207. {
  208. struct menu_device *data = &__get_cpu_var(menu_devices);
  209. int last_idx = data->last_state_idx;
  210. unsigned int last_idle_us = cpuidle_get_last_residency(dev);
  211. struct cpuidle_state *target = &dev->states[last_idx];
  212. unsigned int measured_us;
  213. u64 new_factor;
  214. /*
  215. * Ugh, this idle state doesn't support residency measurements, so we
  216. * are basically lost in the dark. As a compromise, assume we slept
  217. * for the whole expected time.
  218. */
  219. if (unlikely(!(target->flags & CPUIDLE_FLAG_TIME_VALID)))
  220. last_idle_us = data->expected_us;
  221. measured_us = last_idle_us;
  222. /*
  223. * We correct for the exit latency; we are assuming here that the
  224. * exit latency happens after the event that we're interested in.
  225. */
  226. if (measured_us > data->exit_us)
  227. measured_us -= data->exit_us;
  228. /* update our correction ratio */
  229. new_factor = data->correction_factor[data->bucket]
  230. * (DECAY - 1) / DECAY;
  231. if (data->expected_us > 0 && data->measured_us < MAX_INTERESTING)
  232. new_factor += RESOLUTION * measured_us / data->expected_us;
  233. else
  234. /*
  235. * we were idle so long that we count it as a perfect
  236. * prediction
  237. */
  238. new_factor += RESOLUTION;
  239. /*
  240. * We don't want 0 as factor; we always want at least
  241. * a tiny bit of estimated time.
  242. */
  243. if (new_factor == 0)
  244. new_factor = 1;
  245. data->correction_factor[data->bucket] = new_factor;
  246. }
  247. /**
  248. * menu_enable_device - scans a CPU's states and does setup
  249. * @dev: the CPU
  250. */
  251. static int menu_enable_device(struct cpuidle_device *dev)
  252. {
  253. struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
  254. memset(data, 0, sizeof(struct menu_device));
  255. return 0;
  256. }
  257. static struct cpuidle_governor menu_governor = {
  258. .name = "menu",
  259. .rating = 20,
  260. .enable = menu_enable_device,
  261. .select = menu_select,
  262. .reflect = menu_reflect,
  263. .owner = THIS_MODULE,
  264. };
  265. /**
  266. * init_menu - initializes the governor
  267. */
  268. static int __init init_menu(void)
  269. {
  270. return cpuidle_register_governor(&menu_governor);
  271. }
  272. /**
  273. * exit_menu - exits the governor
  274. */
  275. static void __exit exit_menu(void)
  276. {
  277. cpuidle_unregister_governor(&menu_governor);
  278. }
  279. MODULE_LICENSE("GPL");
  280. module_init(init_menu);
  281. module_exit(exit_menu);