sysfs.c 8.7 KB

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