menu.c 10 KB

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