cpuidle.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
  3. * (C) 2011 Thomas Renninger <trenn@novell.com> Novell Inc.
  4. *
  5. * Licensed under the terms of the GNU GPL License version 2.
  6. */
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include "cpuidle.h"
  16. #include "cpupower_intern.h"
  17. /*
  18. * helper function to check whether a file under "../cpuX/cpuidle/stateX/" dir
  19. * exists.
  20. * For example the functionality to disable c-states was introduced in later
  21. * kernel versions, this function can be used to explicitly check for this
  22. * feature.
  23. *
  24. * returns 1 if the file exists, 0 otherwise.
  25. */
  26. static
  27. unsigned int cpuidle_state_file_exists(unsigned int cpu,
  28. unsigned int idlestate,
  29. const char *fname)
  30. {
  31. char path[SYSFS_PATH_MAX];
  32. struct stat statbuf;
  33. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  34. cpu, idlestate, fname);
  35. if (stat(path, &statbuf) != 0)
  36. return 0;
  37. return 1;
  38. }
  39. /*
  40. * helper function to read file from /sys into given buffer
  41. * fname is a relative path under "cpuX/cpuidle/stateX/" dir
  42. * cstates starting with 0, C0 is not counted as cstate.
  43. * This means if you want C1 info, pass 0 as idlestate param
  44. */
  45. static
  46. unsigned int cpuidle_state_read_file(unsigned int cpu,
  47. unsigned int idlestate,
  48. const char *fname, char *buf,
  49. size_t buflen)
  50. {
  51. char path[SYSFS_PATH_MAX];
  52. int fd;
  53. ssize_t numread;
  54. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  55. cpu, idlestate, fname);
  56. fd = open(path, O_RDONLY);
  57. if (fd == -1)
  58. return 0;
  59. numread = read(fd, buf, buflen - 1);
  60. if (numread < 1) {
  61. close(fd);
  62. return 0;
  63. }
  64. buf[numread] = '\0';
  65. close(fd);
  66. return (unsigned int) numread;
  67. }
  68. /*
  69. * helper function to write a new value to a /sys file
  70. * fname is a relative path under "../cpuX/cpuidle/cstateY/" dir
  71. *
  72. * Returns the number of bytes written or 0 on error
  73. */
  74. static
  75. unsigned int cpuidle_state_write_file(unsigned int cpu,
  76. unsigned int idlestate,
  77. const char *fname,
  78. const char *value, size_t len)
  79. {
  80. char path[SYSFS_PATH_MAX];
  81. int fd;
  82. ssize_t numwrite;
  83. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  84. cpu, idlestate, fname);
  85. fd = open(path, O_WRONLY);
  86. if (fd == -1)
  87. return 0;
  88. numwrite = write(fd, value, len);
  89. if (numwrite < 1) {
  90. close(fd);
  91. return 0;
  92. }
  93. close(fd);
  94. return (unsigned int) numwrite;
  95. }
  96. /* read access to files which contain one numeric value */
  97. enum idlestate_value {
  98. IDLESTATE_USAGE,
  99. IDLESTATE_POWER,
  100. IDLESTATE_LATENCY,
  101. IDLESTATE_TIME,
  102. IDLESTATE_DISABLE,
  103. MAX_IDLESTATE_VALUE_FILES
  104. };
  105. static const char *idlestate_value_files[MAX_IDLESTATE_VALUE_FILES] = {
  106. [IDLESTATE_USAGE] = "usage",
  107. [IDLESTATE_POWER] = "power",
  108. [IDLESTATE_LATENCY] = "latency",
  109. [IDLESTATE_TIME] = "time",
  110. [IDLESTATE_DISABLE] = "disable",
  111. };
  112. static
  113. unsigned long long cpuidle_state_get_one_value(unsigned int cpu,
  114. unsigned int idlestate,
  115. enum idlestate_value which)
  116. {
  117. unsigned long long value;
  118. unsigned int len;
  119. char linebuf[MAX_LINE_LEN];
  120. char *endp;
  121. if (which >= MAX_IDLESTATE_VALUE_FILES)
  122. return 0;
  123. len = cpuidle_state_read_file(cpu, idlestate,
  124. idlestate_value_files[which],
  125. linebuf, sizeof(linebuf));
  126. if (len == 0)
  127. return 0;
  128. value = strtoull(linebuf, &endp, 0);
  129. if (endp == linebuf || errno == ERANGE)
  130. return 0;
  131. return value;
  132. }
  133. /* read access to files which contain one string */
  134. enum idlestate_string {
  135. IDLESTATE_DESC,
  136. IDLESTATE_NAME,
  137. MAX_IDLESTATE_STRING_FILES
  138. };
  139. static const char *idlestate_string_files[MAX_IDLESTATE_STRING_FILES] = {
  140. [IDLESTATE_DESC] = "desc",
  141. [IDLESTATE_NAME] = "name",
  142. };
  143. static char *cpuidle_state_get_one_string(unsigned int cpu,
  144. unsigned int idlestate,
  145. enum idlestate_string which)
  146. {
  147. char linebuf[MAX_LINE_LEN];
  148. char *result;
  149. unsigned int len;
  150. if (which >= MAX_IDLESTATE_STRING_FILES)
  151. return NULL;
  152. len = cpuidle_state_read_file(cpu, idlestate,
  153. idlestate_string_files[which],
  154. linebuf, sizeof(linebuf));
  155. if (len == 0)
  156. return NULL;
  157. result = strdup(linebuf);
  158. if (result == NULL)
  159. return NULL;
  160. if (result[strlen(result) - 1] == '\n')
  161. result[strlen(result) - 1] = '\0';
  162. return result;
  163. }
  164. /*
  165. * Returns:
  166. * 1 if disabled
  167. * 0 if enabled
  168. * -1 if idlestate is not available
  169. * -2 if disabling is not supported by the kernel
  170. */
  171. int cpuidle_is_state_disabled(unsigned int cpu,
  172. unsigned int idlestate)
  173. {
  174. if (cpuidle_state_count(cpu) <= idlestate)
  175. return -1;
  176. if (!cpuidle_state_file_exists(cpu, idlestate,
  177. idlestate_value_files[IDLESTATE_DISABLE]))
  178. return -2;
  179. return cpuidle_state_get_one_value(cpu, idlestate, IDLESTATE_DISABLE);
  180. }
  181. /*
  182. * Pass 1 as last argument to disable or 0 to enable the state
  183. * Returns:
  184. * 0 on success
  185. * negative values on error, for example:
  186. * -1 if idlestate is not available
  187. * -2 if disabling is not supported by the kernel
  188. * -3 No write access to disable/enable C-states
  189. */
  190. int cpuidle_state_disable(unsigned int cpu,
  191. unsigned int idlestate,
  192. unsigned int disable)
  193. {
  194. char value[SYSFS_PATH_MAX];
  195. int bytes_written;
  196. if (cpuidle_state_count(cpu) <= idlestate)
  197. return -1;
  198. if (!cpuidle_state_file_exists(cpu, idlestate,
  199. idlestate_value_files[IDLESTATE_DISABLE]))
  200. return -2;
  201. snprintf(value, SYSFS_PATH_MAX, "%u", disable);
  202. bytes_written = cpuidle_state_write_file(cpu, idlestate, "disable",
  203. value, sizeof(disable));
  204. if (bytes_written)
  205. return 0;
  206. return -3;
  207. }
  208. unsigned long cpuidle_state_latency(unsigned int cpu,
  209. unsigned int idlestate)
  210. {
  211. return cpuidle_state_get_one_value(cpu, idlestate, IDLESTATE_LATENCY);
  212. }
  213. unsigned long cpuidle_state_usage(unsigned int cpu,
  214. unsigned int idlestate)
  215. {
  216. return cpuidle_state_get_one_value(cpu, idlestate, IDLESTATE_USAGE);
  217. }
  218. unsigned long long cpuidle_state_time(unsigned int cpu,
  219. unsigned int idlestate)
  220. {
  221. return cpuidle_state_get_one_value(cpu, idlestate, IDLESTATE_TIME);
  222. }
  223. char *cpuidle_state_name(unsigned int cpu, unsigned int idlestate)
  224. {
  225. return cpuidle_state_get_one_string(cpu, idlestate, IDLESTATE_NAME);
  226. }
  227. char *cpuidle_state_desc(unsigned int cpu, unsigned int idlestate)
  228. {
  229. return cpuidle_state_get_one_string(cpu, idlestate, IDLESTATE_DESC);
  230. }
  231. /*
  232. * Returns number of supported C-states of CPU core cpu
  233. * Negativ in error case
  234. * Zero if cpuidle does not export any C-states
  235. */
  236. unsigned int cpuidle_state_count(unsigned int cpu)
  237. {
  238. char file[SYSFS_PATH_MAX];
  239. struct stat statbuf;
  240. int idlestates = 1;
  241. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle");
  242. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  243. return 0;
  244. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu);
  245. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  246. return 0;
  247. while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
  248. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU
  249. "cpu%u/cpuidle/state%d", cpu, idlestates);
  250. idlestates++;
  251. }
  252. idlestates--;
  253. return idlestates;
  254. }
  255. /* CPUidle general /sys/devices/system/cpu/cpuidle/ sysfs access ********/
  256. /*
  257. * helper function to read file from /sys into given buffer
  258. * fname is a relative path under "cpu/cpuidle/" dir
  259. */
  260. static unsigned int sysfs_cpuidle_read_file(const char *fname, char *buf,
  261. size_t buflen)
  262. {
  263. char path[SYSFS_PATH_MAX];
  264. snprintf(path, sizeof(path), PATH_TO_CPU "cpuidle/%s", fname);
  265. return sysfs_read_file(path, buf, buflen);
  266. }
  267. /* read access to files which contain one string */
  268. enum cpuidle_string {
  269. CPUIDLE_GOVERNOR,
  270. CPUIDLE_GOVERNOR_RO,
  271. CPUIDLE_DRIVER,
  272. MAX_CPUIDLE_STRING_FILES
  273. };
  274. static const char *cpuidle_string_files[MAX_CPUIDLE_STRING_FILES] = {
  275. [CPUIDLE_GOVERNOR] = "current_governor",
  276. [CPUIDLE_GOVERNOR_RO] = "current_governor_ro",
  277. [CPUIDLE_DRIVER] = "current_driver",
  278. };
  279. static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)
  280. {
  281. char linebuf[MAX_LINE_LEN];
  282. char *result;
  283. unsigned int len;
  284. if (which >= MAX_CPUIDLE_STRING_FILES)
  285. return NULL;
  286. len = sysfs_cpuidle_read_file(cpuidle_string_files[which],
  287. linebuf, sizeof(linebuf));
  288. if (len == 0)
  289. return NULL;
  290. result = strdup(linebuf);
  291. if (result == NULL)
  292. return NULL;
  293. if (result[strlen(result) - 1] == '\n')
  294. result[strlen(result) - 1] = '\0';
  295. return result;
  296. }
  297. char *cpuidle_get_governor(void)
  298. {
  299. char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO);
  300. if (!tmp)
  301. return sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR);
  302. else
  303. return tmp;
  304. }
  305. char *cpuidle_get_driver(void)
  306. {
  307. return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER);
  308. }
  309. /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */