menu.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. int needs_update;
  96. unsigned int expected_us;
  97. u64 predicted_us;
  98. unsigned int measured_us;
  99. unsigned int exit_us;
  100. unsigned int bucket;
  101. u64 correction_factor[BUCKETS];
  102. };
  103. #define LOAD_INT(x) ((x) >> FSHIFT)
  104. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  105. static int get_loadavg(void)
  106. {
  107. unsigned long this = this_cpu_load();
  108. return LOAD_INT(this) * 10 + LOAD_FRAC(this) / 10;
  109. }
  110. static inline int which_bucket(unsigned int duration)
  111. {
  112. int bucket = 0;
  113. /*
  114. * We keep two groups of stats; one with no
  115. * IO pending, one without.
  116. * This allows us to calculate
  117. * E(duration)|iowait
  118. */
  119. if (nr_iowait_cpu())
  120. bucket = BUCKETS/2;
  121. if (duration < 10)
  122. return bucket;
  123. if (duration < 100)
  124. return bucket + 1;
  125. if (duration < 1000)
  126. return bucket + 2;
  127. if (duration < 10000)
  128. return bucket + 3;
  129. if (duration < 100000)
  130. return bucket + 4;
  131. return bucket + 5;
  132. }
  133. /*
  134. * Return a multiplier for the exit latency that is intended
  135. * to take performance requirements into account.
  136. * The more performance critical we estimate the system
  137. * to be, the higher this multiplier, and thus the higher
  138. * the barrier to go to an expensive C state.
  139. */
  140. static inline int performance_multiplier(void)
  141. {
  142. int mult = 1;
  143. /* for higher loadavg, we are more reluctant */
  144. mult += 2 * get_loadavg();
  145. /* for IO wait tasks (per cpu!) we add 5x each */
  146. mult += 10 * nr_iowait_cpu();
  147. return mult;
  148. }
  149. static DEFINE_PER_CPU(struct menu_device, menu_devices);
  150. static void menu_update(struct cpuidle_device *dev);
  151. /**
  152. * menu_select - selects the next idle state to enter
  153. * @dev: the CPU
  154. */
  155. static int menu_select(struct cpuidle_device *dev)
  156. {
  157. struct menu_device *data = &__get_cpu_var(menu_devices);
  158. int latency_req = pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY);
  159. int i;
  160. int multiplier;
  161. data->last_state_idx = 0;
  162. data->exit_us = 0;
  163. if (data->needs_update) {
  164. menu_update(dev);
  165. data->needs_update = 0;
  166. }
  167. /* Special case when user has set very strict latency requirement */
  168. if (unlikely(latency_req == 0))
  169. return 0;
  170. /* determine the expected residency time, round up */
  171. data->expected_us =
  172. DIV_ROUND_UP((u32)ktime_to_ns(tick_nohz_get_sleep_length()), 1000);
  173. data->bucket = which_bucket(data->expected_us);
  174. multiplier = performance_multiplier();
  175. /*
  176. * if the correction factor is 0 (eg first time init or cpu hotplug
  177. * etc), we actually want to start out with a unity factor.
  178. */
  179. if (data->correction_factor[data->bucket] == 0)
  180. data->correction_factor[data->bucket] = RESOLUTION * DECAY;
  181. /* Make sure to round up for half microseconds */
  182. data->predicted_us = DIV_ROUND_CLOSEST(
  183. data->expected_us * data->correction_factor[data->bucket],
  184. RESOLUTION * DECAY);
  185. /*
  186. * We want to default to C1 (hlt), not to busy polling
  187. * unless the timer is happening really really soon.
  188. */
  189. if (data->expected_us > 5)
  190. data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
  191. /* find the deepest idle state that satisfies our constraints */
  192. for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++) {
  193. struct cpuidle_state *s = &dev->states[i];
  194. if (s->target_residency > data->predicted_us)
  195. break;
  196. if (s->exit_latency > latency_req)
  197. break;
  198. if (s->exit_latency * multiplier > data->predicted_us)
  199. break;
  200. data->exit_us = s->exit_latency;
  201. data->last_state_idx = i;
  202. }
  203. return data->last_state_idx;
  204. }
  205. /**
  206. * menu_reflect - records that data structures need update
  207. * @dev: the CPU
  208. *
  209. * NOTE: it's important to be fast here because this operation will add to
  210. * the overall exit latency.
  211. */
  212. static void menu_reflect(struct cpuidle_device *dev)
  213. {
  214. struct menu_device *data = &__get_cpu_var(menu_devices);
  215. data->needs_update = 1;
  216. }
  217. /**
  218. * menu_update - attempts to guess what happened after entry
  219. * @dev: the CPU
  220. */
  221. static void menu_update(struct cpuidle_device *dev)
  222. {
  223. struct menu_device *data = &__get_cpu_var(menu_devices);
  224. int last_idx = data->last_state_idx;
  225. unsigned int last_idle_us = cpuidle_get_last_residency(dev);
  226. struct cpuidle_state *target = &dev->states[last_idx];
  227. unsigned int measured_us;
  228. u64 new_factor;
  229. /*
  230. * Ugh, this idle state doesn't support residency measurements, so we
  231. * are basically lost in the dark. As a compromise, assume we slept
  232. * for the whole expected time.
  233. */
  234. if (unlikely(!(target->flags & CPUIDLE_FLAG_TIME_VALID)))
  235. last_idle_us = data->expected_us;
  236. measured_us = last_idle_us;
  237. /*
  238. * We correct for the exit latency; we are assuming here that the
  239. * exit latency happens after the event that we're interested in.
  240. */
  241. if (measured_us > data->exit_us)
  242. measured_us -= data->exit_us;
  243. /* update our correction ratio */
  244. new_factor = data->correction_factor[data->bucket]
  245. * (DECAY - 1) / DECAY;
  246. if (data->expected_us > 0 && data->measured_us < MAX_INTERESTING)
  247. new_factor += RESOLUTION * measured_us / data->expected_us;
  248. else
  249. /*
  250. * we were idle so long that we count it as a perfect
  251. * prediction
  252. */
  253. new_factor += RESOLUTION;
  254. /*
  255. * We don't want 0 as factor; we always want at least
  256. * a tiny bit of estimated time.
  257. */
  258. if (new_factor == 0)
  259. new_factor = 1;
  260. data->correction_factor[data->bucket] = new_factor;
  261. }
  262. /**
  263. * menu_enable_device - scans a CPU's states and does setup
  264. * @dev: the CPU
  265. */
  266. static int menu_enable_device(struct cpuidle_device *dev)
  267. {
  268. struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
  269. memset(data, 0, sizeof(struct menu_device));
  270. return 0;
  271. }
  272. static struct cpuidle_governor menu_governor = {
  273. .name = "menu",
  274. .rating = 20,
  275. .enable = menu_enable_device,
  276. .select = menu_select,
  277. .reflect = menu_reflect,
  278. .owner = THIS_MODULE,
  279. };
  280. /**
  281. * init_menu - initializes the governor
  282. */
  283. static int __init init_menu(void)
  284. {
  285. return cpuidle_register_governor(&menu_governor);
  286. }
  287. /**
  288. * exit_menu - exits the governor
  289. */
  290. static void __exit exit_menu(void)
  291. {
  292. cpuidle_unregister_governor(&menu_governor);
  293. }
  294. MODULE_LICENSE("GPL");
  295. module_init(init_menu);
  296. module_exit(exit_menu);