cpugovctl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (c) 2017, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "logging.h"
  28. #include <ctype.h>
  29. #include <dirent.h>
  30. #include <linux/limits.h>
  31. #include <sys/types.h>
  32. #define MAX_GOVERNORS 128
  33. #define MAX_GOVERNOR_LENGTH PATH_MAX + 1
  34. // Governers are located at /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
  35. static int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
  36. {
  37. const char *cpu_base_path = "/sys/devices/system/cpu/";
  38. DIR *dir = opendir(cpu_base_path);
  39. if (!dir) {
  40. FATAL_ERRORNO("cpu device path not found");
  41. }
  42. int num_governors = 0;
  43. // Explore the directory
  44. struct dirent *ent;
  45. while ((ent = readdir(dir)) && num_governors < MAX_GOVERNORS) {
  46. // CPU directories all start with "cpu"
  47. if (strncmp(ent->d_name, "cpu", 3) == 0) {
  48. // Check if this matches "cpu\d+"
  49. const int len = strlen(ent->d_name);
  50. if (len > 3 && len < 5 && isdigit(ent->d_name[3])) {
  51. // Construct the full path
  52. char path[PATH_MAX] = {};
  53. snprintf(path,
  54. sizeof(path),
  55. "%s%s/cpufreq/scaling_governor",
  56. cpu_base_path,
  57. ent->d_name);
  58. // Get the real path to the file
  59. // Traditionally cpufreq symlinks to a policy directory that can be
  60. // shared So let's prevent duplicates
  61. char fullpath[PATH_MAX] = {};
  62. const char *ptr = realpath(path, fullpath);
  63. if (fullpath != ptr) {
  64. continue;
  65. }
  66. // Only add if unique
  67. for (int i = 0; i < num_governors; i++) {
  68. if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) {
  69. continue;
  70. }
  71. }
  72. strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
  73. num_governors++;
  74. }
  75. }
  76. }
  77. closedir(dir);
  78. return num_governors;
  79. }
  80. // Get the current governor state
  81. const char *get_gov_state()
  82. {
  83. // To be returned
  84. static char governor[64] = { 0 };
  85. // State of all the overnors
  86. char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
  87. int num = fetch_governors(governors);
  88. // Check the list
  89. for (int i = 0; i < num; i++) {
  90. const char *gov = governors[i];
  91. FILE *f = fopen(gov, "r");
  92. if (!f) {
  93. LOG_ERROR("Failed to open file for read %s\n", gov);
  94. continue;
  95. }
  96. // Pull out the file contents
  97. fseek(f, 0, SEEK_END);
  98. int length = ftell(f);
  99. fseek(f, 0, SEEK_SET);
  100. char contents[length];
  101. if (fread(contents, 1, length, f) > 0) {
  102. // Files have a newline
  103. strtok(contents, "\n");
  104. if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
  105. // Don't handle the mixed case, this shouldn't ever happen
  106. // But it is a clear sign we shouldn't carry on
  107. LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
  108. return "malformed";
  109. }
  110. strncpy(governor, contents, sizeof(governor));
  111. } else {
  112. LOG_ERROR("Failed to read contents of %s\n", gov);
  113. }
  114. fclose(f);
  115. }
  116. return governor;
  117. }
  118. // Sets all governors to a value
  119. void set_gov_state(const char *value)
  120. {
  121. char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
  122. int num = fetch_governors(governors);
  123. LOG_MSG("Setting governors to %s\n", value);
  124. for (int i = 0; i < num; i++) {
  125. const char *gov = governors[i];
  126. FILE *f = fopen(gov, "w");
  127. if (!f) {
  128. LOG_ERROR("Failed to open file for write %s\n", gov);
  129. continue;
  130. }
  131. fprintf(f, "%s\n", value);
  132. fclose(f);
  133. }
  134. }
  135. // Main entry point
  136. int main(int argc, char *argv[])
  137. {
  138. if (argc < 2) {
  139. fprintf(stderr, "usage: cpugovctl [get] [set VALUE]\n");
  140. exit(EXIT_FAILURE);
  141. }
  142. if (strncmp(argv[1], "get", 3) == 0) {
  143. printf("%s", get_gov_state());
  144. } else if (strncmp(argv[1], "set", 3) == 0) {
  145. const char *value = argv[2];
  146. set_gov_state(value);
  147. } else {
  148. exit(EXIT_FAILURE);
  149. }
  150. }