sysfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "helpers/sysfs.h"
  16. unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
  17. {
  18. int fd;
  19. ssize_t numread;
  20. fd = open(path, O_RDONLY);
  21. if (fd == -1)
  22. return 0;
  23. numread = read(fd, buf, buflen - 1);
  24. if (numread < 1) {
  25. close(fd);
  26. return 0;
  27. }
  28. buf[numread] = '\0';
  29. close(fd);
  30. return (unsigned int) numread;
  31. }
  32. static unsigned int sysfs_write_file(const char *path,
  33. const char *value, size_t len)
  34. {
  35. int fd;
  36. ssize_t numwrite;
  37. fd = open(path, O_WRONLY);
  38. if (fd == -1)
  39. return 0;
  40. numwrite = write(fd, value, len);
  41. if (numwrite < 1) {
  42. close(fd);
  43. return 0;
  44. }
  45. close(fd);
  46. return (unsigned int) numwrite;
  47. }
  48. /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
  49. /*
  50. * helper function to read file from /sys into given buffer
  51. * fname is a relative path under "cpuX/cpuidle/stateX/" dir
  52. * cstates starting with 0, C0 is not counted as cstate.
  53. * This means if you want C1 info, pass 0 as idlestate param
  54. */
  55. unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate,
  56. const char *fname, char *buf, size_t buflen)
  57. {
  58. char path[SYSFS_PATH_MAX];
  59. int fd;
  60. ssize_t numread;
  61. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  62. cpu, idlestate, fname);
  63. fd = open(path, O_RDONLY);
  64. if (fd == -1)
  65. return 0;
  66. numread = read(fd, buf, buflen - 1);
  67. if (numread < 1) {
  68. close(fd);
  69. return 0;
  70. }
  71. buf[numread] = '\0';
  72. close(fd);
  73. return (unsigned int) numread;
  74. }
  75. /* read access to files which contain one numeric value */
  76. enum idlestate_value {
  77. IDLESTATE_USAGE,
  78. IDLESTATE_POWER,
  79. IDLESTATE_LATENCY,
  80. IDLESTATE_TIME,
  81. MAX_IDLESTATE_VALUE_FILES
  82. };
  83. static const char *idlestate_value_files[MAX_IDLESTATE_VALUE_FILES] = {
  84. [IDLESTATE_USAGE] = "usage",
  85. [IDLESTATE_POWER] = "power",
  86. [IDLESTATE_LATENCY] = "latency",
  87. [IDLESTATE_TIME] = "time",
  88. };
  89. static unsigned long long sysfs_idlestate_get_one_value(unsigned int cpu,
  90. unsigned int idlestate,
  91. enum idlestate_value which)
  92. {
  93. unsigned long long value;
  94. unsigned int len;
  95. char linebuf[MAX_LINE_LEN];
  96. char *endp;
  97. if (which >= MAX_IDLESTATE_VALUE_FILES)
  98. return 0;
  99. len = sysfs_idlestate_read_file(cpu, idlestate,
  100. idlestate_value_files[which],
  101. linebuf, sizeof(linebuf));
  102. if (len == 0)
  103. return 0;
  104. value = strtoull(linebuf, &endp, 0);
  105. if (endp == linebuf || errno == ERANGE)
  106. return 0;
  107. return value;
  108. }
  109. /* read access to files which contain one string */
  110. enum idlestate_string {
  111. IDLESTATE_DESC,
  112. IDLESTATE_NAME,
  113. MAX_IDLESTATE_STRING_FILES
  114. };
  115. static const char *idlestate_string_files[MAX_IDLESTATE_STRING_FILES] = {
  116. [IDLESTATE_DESC] = "desc",
  117. [IDLESTATE_NAME] = "name",
  118. };
  119. static char *sysfs_idlestate_get_one_string(unsigned int cpu,
  120. unsigned int idlestate,
  121. enum idlestate_string which)
  122. {
  123. char linebuf[MAX_LINE_LEN];
  124. char *result;
  125. unsigned int len;
  126. if (which >= MAX_IDLESTATE_STRING_FILES)
  127. return NULL;
  128. len = sysfs_idlestate_read_file(cpu, idlestate,
  129. idlestate_string_files[which],
  130. linebuf, sizeof(linebuf));
  131. if (len == 0)
  132. return NULL;
  133. result = strdup(linebuf);
  134. if (result == NULL)
  135. return NULL;
  136. if (result[strlen(result) - 1] == '\n')
  137. result[strlen(result) - 1] = '\0';
  138. return result;
  139. }
  140. unsigned long sysfs_get_idlestate_latency(unsigned int cpu,
  141. unsigned int idlestate)
  142. {
  143. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_LATENCY);
  144. }
  145. unsigned long sysfs_get_idlestate_usage(unsigned int cpu,
  146. unsigned int idlestate)
  147. {
  148. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_USAGE);
  149. }
  150. unsigned long long sysfs_get_idlestate_time(unsigned int cpu,
  151. unsigned int idlestate)
  152. {
  153. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_TIME);
  154. }
  155. char *sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate)
  156. {
  157. return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_NAME);
  158. }
  159. char *sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate)
  160. {
  161. return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_DESC);
  162. }
  163. /*
  164. * Returns number of supported C-states of CPU core cpu
  165. * Negativ in error case
  166. * Zero if cpuidle does not export any C-states
  167. */
  168. int sysfs_get_idlestate_count(unsigned int cpu)
  169. {
  170. char file[SYSFS_PATH_MAX];
  171. struct stat statbuf;
  172. int idlestates = 1;
  173. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle");
  174. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  175. return -ENODEV;
  176. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu);
  177. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  178. return 0;
  179. while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
  180. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU
  181. "cpu%u/cpuidle/state%d", cpu, idlestates);
  182. idlestates++;
  183. }
  184. idlestates--;
  185. return idlestates;
  186. }
  187. /* CPUidle general /sys/devices/system/cpu/cpuidle/ sysfs access ********/
  188. /*
  189. * helper function to read file from /sys into given buffer
  190. * fname is a relative path under "cpu/cpuidle/" dir
  191. */
  192. static unsigned int sysfs_cpuidle_read_file(const char *fname, char *buf,
  193. size_t buflen)
  194. {
  195. char path[SYSFS_PATH_MAX];
  196. snprintf(path, sizeof(path), PATH_TO_CPU "cpuidle/%s", fname);
  197. return sysfs_read_file(path, buf, buflen);
  198. }
  199. /* read access to files which contain one string */
  200. enum cpuidle_string {
  201. CPUIDLE_GOVERNOR,
  202. CPUIDLE_GOVERNOR_RO,
  203. CPUIDLE_DRIVER,
  204. MAX_CPUIDLE_STRING_FILES
  205. };
  206. static const char *cpuidle_string_files[MAX_CPUIDLE_STRING_FILES] = {
  207. [CPUIDLE_GOVERNOR] = "current_governor",
  208. [CPUIDLE_GOVERNOR_RO] = "current_governor_ro",
  209. [CPUIDLE_DRIVER] = "current_driver",
  210. };
  211. static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)
  212. {
  213. char linebuf[MAX_LINE_LEN];
  214. char *result;
  215. unsigned int len;
  216. if (which >= MAX_CPUIDLE_STRING_FILES)
  217. return NULL;
  218. len = sysfs_cpuidle_read_file(cpuidle_string_files[which],
  219. linebuf, sizeof(linebuf));
  220. if (len == 0)
  221. return NULL;
  222. result = strdup(linebuf);
  223. if (result == NULL)
  224. return NULL;
  225. if (result[strlen(result) - 1] == '\n')
  226. result[strlen(result) - 1] = '\0';
  227. return result;
  228. }
  229. char *sysfs_get_cpuidle_governor(void)
  230. {
  231. char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO);
  232. if (!tmp)
  233. return sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR);
  234. else
  235. return tmp;
  236. }
  237. char *sysfs_get_cpuidle_driver(void)
  238. {
  239. return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER);
  240. }
  241. /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
  242. /*
  243. * Get sched_mc or sched_smt settings
  244. * Pass "mc" or "smt" as argument
  245. *
  246. * Returns negative value on failure
  247. */
  248. int sysfs_get_sched(const char *smt_mc)
  249. {
  250. unsigned long value;
  251. char linebuf[MAX_LINE_LEN];
  252. char *endp;
  253. char path[SYSFS_PATH_MAX];
  254. if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc))
  255. return -EINVAL;
  256. snprintf(path, sizeof(path),
  257. PATH_TO_CPU "sched_%s_power_savings", smt_mc);
  258. if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
  259. return -1;
  260. value = strtoul(linebuf, &endp, 0);
  261. if (endp == linebuf || errno == ERANGE)
  262. return -1;
  263. return value;
  264. }
  265. /*
  266. * Get sched_mc or sched_smt settings
  267. * Pass "mc" or "smt" as argument
  268. *
  269. * Returns negative value on failure
  270. */
  271. int sysfs_set_sched(const char *smt_mc, int val)
  272. {
  273. char linebuf[MAX_LINE_LEN];
  274. char path[SYSFS_PATH_MAX];
  275. struct stat statbuf;
  276. if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc))
  277. return -EINVAL;
  278. snprintf(path, sizeof(path),
  279. PATH_TO_CPU "sched_%s_power_savings", smt_mc);
  280. sprintf(linebuf, "%d", val);
  281. if (stat(path, &statbuf) != 0)
  282. return -ENODEV;
  283. if (sysfs_write_file(path, linebuf, MAX_LINE_LEN) == 0)
  284. return -1;
  285. return 0;
  286. }