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