sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
  76. /*
  77. * helper function to check whether a file under "../cpuX/cpuidle/stateX/" dir
  78. * exists.
  79. * For example the functionality to disable c-states was introduced in later
  80. * kernel versions, this function can be used to explicitly check for this
  81. * feature.
  82. *
  83. * returns 1 if the file exists, 0 otherwise.
  84. */
  85. unsigned int sysfs_idlestate_file_exists(unsigned int cpu,
  86. unsigned int idlestate,
  87. const char *fname)
  88. {
  89. char path[SYSFS_PATH_MAX];
  90. struct stat statbuf;
  91. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  92. cpu, idlestate, fname);
  93. if (stat(path, &statbuf) != 0)
  94. return 0;
  95. return 1;
  96. }
  97. /*
  98. * helper function to read file from /sys into given buffer
  99. * fname is a relative path under "cpuX/cpuidle/stateX/" dir
  100. * cstates starting with 0, C0 is not counted as cstate.
  101. * This means if you want C1 info, pass 0 as idlestate param
  102. */
  103. unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate,
  104. const char *fname, char *buf, size_t buflen)
  105. {
  106. char path[SYSFS_PATH_MAX];
  107. int fd;
  108. ssize_t numread;
  109. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  110. cpu, idlestate, fname);
  111. fd = open(path, O_RDONLY);
  112. if (fd == -1)
  113. return 0;
  114. numread = read(fd, buf, buflen - 1);
  115. if (numread < 1) {
  116. close(fd);
  117. return 0;
  118. }
  119. buf[numread] = '\0';
  120. close(fd);
  121. return (unsigned int) numread;
  122. }
  123. /*
  124. * helper function to write a new value to a /sys file
  125. * fname is a relative path under "../cpuX/cpuidle/cstateY/" dir
  126. *
  127. * Returns the number of bytes written or 0 on error
  128. */
  129. static
  130. unsigned int sysfs_idlestate_write_file(unsigned int cpu,
  131. unsigned int idlestate,
  132. const char *fname,
  133. const char *value, size_t len)
  134. {
  135. char path[SYSFS_PATH_MAX];
  136. int fd;
  137. ssize_t numwrite;
  138. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s",
  139. cpu, idlestate, fname);
  140. fd = open(path, O_WRONLY);
  141. if (fd == -1)
  142. return 0;
  143. numwrite = write(fd, value, len);
  144. if (numwrite < 1) {
  145. close(fd);
  146. return 0;
  147. }
  148. close(fd);
  149. return (unsigned int) numwrite;
  150. }
  151. /* read access to files which contain one numeric value */
  152. enum idlestate_value {
  153. IDLESTATE_USAGE,
  154. IDLESTATE_POWER,
  155. IDLESTATE_LATENCY,
  156. IDLESTATE_TIME,
  157. IDLESTATE_DISABLE,
  158. MAX_IDLESTATE_VALUE_FILES
  159. };
  160. static const char *idlestate_value_files[MAX_IDLESTATE_VALUE_FILES] = {
  161. [IDLESTATE_USAGE] = "usage",
  162. [IDLESTATE_POWER] = "power",
  163. [IDLESTATE_LATENCY] = "latency",
  164. [IDLESTATE_TIME] = "time",
  165. [IDLESTATE_DISABLE] = "disable",
  166. };
  167. static unsigned long long sysfs_idlestate_get_one_value(unsigned int cpu,
  168. unsigned int idlestate,
  169. enum idlestate_value which)
  170. {
  171. unsigned long long value;
  172. unsigned int len;
  173. char linebuf[MAX_LINE_LEN];
  174. char *endp;
  175. if (which >= MAX_IDLESTATE_VALUE_FILES)
  176. return 0;
  177. len = sysfs_idlestate_read_file(cpu, idlestate,
  178. idlestate_value_files[which],
  179. linebuf, sizeof(linebuf));
  180. if (len == 0)
  181. return 0;
  182. value = strtoull(linebuf, &endp, 0);
  183. if (endp == linebuf || errno == ERANGE)
  184. return 0;
  185. return value;
  186. }
  187. /* read access to files which contain one string */
  188. enum idlestate_string {
  189. IDLESTATE_DESC,
  190. IDLESTATE_NAME,
  191. MAX_IDLESTATE_STRING_FILES
  192. };
  193. static const char *idlestate_string_files[MAX_IDLESTATE_STRING_FILES] = {
  194. [IDLESTATE_DESC] = "desc",
  195. [IDLESTATE_NAME] = "name",
  196. };
  197. static char *sysfs_idlestate_get_one_string(unsigned int cpu,
  198. unsigned int idlestate,
  199. enum idlestate_string which)
  200. {
  201. char linebuf[MAX_LINE_LEN];
  202. char *result;
  203. unsigned int len;
  204. if (which >= MAX_IDLESTATE_STRING_FILES)
  205. return NULL;
  206. len = sysfs_idlestate_read_file(cpu, idlestate,
  207. idlestate_string_files[which],
  208. linebuf, sizeof(linebuf));
  209. if (len == 0)
  210. return NULL;
  211. result = strdup(linebuf);
  212. if (result == NULL)
  213. return NULL;
  214. if (result[strlen(result) - 1] == '\n')
  215. result[strlen(result) - 1] = '\0';
  216. return result;
  217. }
  218. /*
  219. * Returns:
  220. * 1 if disabled
  221. * 0 if enabled
  222. * -1 if idlestate is not available
  223. * -2 if disabling is not supported by the kernel
  224. */
  225. int sysfs_is_idlestate_disabled(unsigned int cpu,
  226. unsigned int idlestate)
  227. {
  228. if (sysfs_get_idlestate_count(cpu) < idlestate)
  229. return -1;
  230. if (!sysfs_idlestate_file_exists(cpu, idlestate,
  231. idlestate_value_files[IDLESTATE_DISABLE]))
  232. return -2;
  233. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_DISABLE);
  234. }
  235. /*
  236. * Pass 1 as last argument to disable or 0 to enable the state
  237. * Returns:
  238. * 0 on success
  239. * negative values on error, for example:
  240. * -1 if idlestate is not available
  241. * -2 if disabling is not supported by the kernel
  242. * -3 No write access to disable/enable C-states
  243. */
  244. int sysfs_idlestate_disable(unsigned int cpu,
  245. unsigned int idlestate,
  246. unsigned int disable)
  247. {
  248. char value[SYSFS_PATH_MAX];
  249. int bytes_written;
  250. if (sysfs_get_idlestate_count(cpu) < idlestate)
  251. return -1;
  252. if (!sysfs_idlestate_file_exists(cpu, idlestate,
  253. idlestate_value_files[IDLESTATE_DISABLE]))
  254. return -2;
  255. snprintf(value, SYSFS_PATH_MAX, "%u", disable);
  256. bytes_written = sysfs_idlestate_write_file(cpu, idlestate, "disable",
  257. value, sizeof(disable));
  258. if (bytes_written)
  259. return 0;
  260. return -3;
  261. }
  262. unsigned long sysfs_get_idlestate_latency(unsigned int cpu,
  263. unsigned int idlestate)
  264. {
  265. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_LATENCY);
  266. }
  267. unsigned long sysfs_get_idlestate_usage(unsigned int cpu,
  268. unsigned int idlestate)
  269. {
  270. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_USAGE);
  271. }
  272. unsigned long long sysfs_get_idlestate_time(unsigned int cpu,
  273. unsigned int idlestate)
  274. {
  275. return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_TIME);
  276. }
  277. char *sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate)
  278. {
  279. return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_NAME);
  280. }
  281. char *sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate)
  282. {
  283. return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_DESC);
  284. }
  285. /*
  286. * Returns number of supported C-states of CPU core cpu
  287. * Negativ in error case
  288. * Zero if cpuidle does not export any C-states
  289. */
  290. unsigned int sysfs_get_idlestate_count(unsigned int cpu)
  291. {
  292. char file[SYSFS_PATH_MAX];
  293. struct stat statbuf;
  294. int idlestates = 1;
  295. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle");
  296. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  297. return -ENODEV;
  298. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu);
  299. if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
  300. return 0;
  301. while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
  302. snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU
  303. "cpu%u/cpuidle/state%d", cpu, idlestates);
  304. idlestates++;
  305. }
  306. idlestates--;
  307. return idlestates;
  308. }
  309. /* CPUidle general /sys/devices/system/cpu/cpuidle/ sysfs access ********/
  310. /*
  311. * helper function to read file from /sys into given buffer
  312. * fname is a relative path under "cpu/cpuidle/" dir
  313. */
  314. static unsigned int sysfs_cpuidle_read_file(const char *fname, char *buf,
  315. size_t buflen)
  316. {
  317. char path[SYSFS_PATH_MAX];
  318. snprintf(path, sizeof(path), PATH_TO_CPU "cpuidle/%s", fname);
  319. return sysfs_read_file(path, buf, buflen);
  320. }
  321. /* read access to files which contain one string */
  322. enum cpuidle_string {
  323. CPUIDLE_GOVERNOR,
  324. CPUIDLE_GOVERNOR_RO,
  325. CPUIDLE_DRIVER,
  326. MAX_CPUIDLE_STRING_FILES
  327. };
  328. static const char *cpuidle_string_files[MAX_CPUIDLE_STRING_FILES] = {
  329. [CPUIDLE_GOVERNOR] = "current_governor",
  330. [CPUIDLE_GOVERNOR_RO] = "current_governor_ro",
  331. [CPUIDLE_DRIVER] = "current_driver",
  332. };
  333. static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)
  334. {
  335. char linebuf[MAX_LINE_LEN];
  336. char *result;
  337. unsigned int len;
  338. if (which >= MAX_CPUIDLE_STRING_FILES)
  339. return NULL;
  340. len = sysfs_cpuidle_read_file(cpuidle_string_files[which],
  341. linebuf, sizeof(linebuf));
  342. if (len == 0)
  343. return NULL;
  344. result = strdup(linebuf);
  345. if (result == NULL)
  346. return NULL;
  347. if (result[strlen(result) - 1] == '\n')
  348. result[strlen(result) - 1] = '\0';
  349. return result;
  350. }
  351. char *sysfs_get_cpuidle_governor(void)
  352. {
  353. char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO);
  354. if (!tmp)
  355. return sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR);
  356. else
  357. return tmp;
  358. }
  359. char *sysfs_get_cpuidle_driver(void)
  360. {
  361. return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER);
  362. }
  363. /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */
  364. /*
  365. * Get sched_mc or sched_smt settings
  366. * Pass "mc" or "smt" as argument
  367. *
  368. * Returns negative value on failure
  369. */
  370. int sysfs_get_sched(const char *smt_mc)
  371. {
  372. return -ENODEV;
  373. }
  374. /*
  375. * Get sched_mc or sched_smt settings
  376. * Pass "mc" or "smt" as argument
  377. *
  378. * Returns negative value on failure
  379. */
  380. int sysfs_set_sched(const char *smt_mc, int val)
  381. {
  382. return -ENODEV;
  383. }