common-governors.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (c) 2017-2019, 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 "common-governors.h"
  28. #include "common-logging.h"
  29. #include <assert.h>
  30. #include <glob.h>
  31. /**
  32. * Discover all governers on the system.
  33. *
  34. * Located at /sys/devices/system/cpu/cpu(*)/cpufreq/scaling_governor
  35. */
  36. int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
  37. {
  38. glob_t glo = { 0 };
  39. static const char *path = "/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor";
  40. /* Assert some sanity on this glob */
  41. if (glob(path, GLOB_NOSORT, NULL, &glo) != 0) {
  42. LOG_ERROR("glob failed for cpu governors: (%s)\n", strerror(errno));
  43. return 0;
  44. }
  45. if (glo.gl_pathc < 1) {
  46. globfree(&glo);
  47. LOG_ERROR("no cpu governors found\n");
  48. return 0;
  49. }
  50. int num_governors = 0;
  51. /* Walk the glob set */
  52. for (size_t i = 0; i < glo.gl_pathc; i++) {
  53. if (i >= MAX_GOVERNORS) {
  54. break;
  55. }
  56. /* Get the real path to the file.
  57. * Traditionally cpufreq symlinks to a policy directory that can
  58. * be shared, so let's prevent duplicates.
  59. */
  60. char fullpath[PATH_MAX] = { 0 };
  61. const char *ptr = realpath(glo.gl_pathv[i], fullpath);
  62. if (fullpath != ptr) {
  63. continue;
  64. }
  65. /* Only add this governor if it is unique */
  66. for (int j = 0; j < num_governors; j++) {
  67. if (strncmp(fullpath, governors[i], PATH_MAX) == 0) {
  68. continue;
  69. }
  70. }
  71. /* Copy this governor into the output set */
  72. static_assert(MAX_GOVERNOR_LENGTH > PATH_MAX, "possible string truncation");
  73. strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
  74. num_governors++;
  75. }
  76. globfree(&glo);
  77. return num_governors;
  78. }
  79. /**
  80. * Return the current governor state
  81. */
  82. const char *get_gov_state(void)
  83. {
  84. /* Persistent governor state */
  85. static char governor[64] = { 0 };
  86. memset(governor, 0, sizeof(governor));
  87. /* State for all governors */
  88. char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } };
  89. int num = fetch_governors(governors);
  90. /* Check the list */
  91. for (int i = 0; i < num; i++) {
  92. const char *gov = governors[i];
  93. FILE *f = fopen(gov, "r");
  94. if (!f) {
  95. LOG_ERROR("Failed to open file for read %s\n", gov);
  96. continue;
  97. }
  98. /* Grab the file length */
  99. fseek(f, 0, SEEK_END);
  100. long length = ftell(f);
  101. fseek(f, 0, SEEK_SET);
  102. if (length == -1)
  103. {
  104. LOG_ERROR("Failed to seek file %s\n", gov);
  105. }
  106. else
  107. {
  108. char contents[length];
  109. if (fread(contents, 1, (size_t)length, f) > 0) {
  110. /* Files have a newline */
  111. strtok(contents, "\n");
  112. if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
  113. /* Don't handle the mixed case, this shouldn't ever happen
  114. * But it is a clear sign we shouldn't carry on */
  115. LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
  116. fclose(f);
  117. return "malformed";
  118. }
  119. strncpy(governor, contents, sizeof(governor) - 1);
  120. } else {
  121. LOG_ERROR("Failed to read contents of %s\n", gov);
  122. }
  123. }
  124. fclose(f);
  125. }
  126. return governor;
  127. }