common-power.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  3. Copyright (c) 2019, Intel Corporation
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Feral Interactive nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define _GNU_SOURCE
  28. #include "common-power.h"
  29. #include "common-logging.h"
  30. #include <linux/limits.h>
  31. #include <assert.h>
  32. #include <ctype.h>
  33. #include <glob.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. static bool read_file_in_dir(const char *dir, const char *file, char *dest, size_t n)
  37. {
  38. char path[PATH_MAX];
  39. int ret = snprintf(path, sizeof(path), "%s/%s", dir, file);
  40. if (ret < 0 || ret >= (int)sizeof(path)) {
  41. LOG_ERROR("Path length overrun");
  42. return false;
  43. }
  44. FILE *f = fopen(path, "r");
  45. if (!f) {
  46. LOG_ERROR("Failed to open file for read %s\n", path);
  47. return false;
  48. }
  49. size_t read = fread(dest, 1, n, f);
  50. /* Close before we do any error checking */
  51. fclose(f);
  52. if (read <= 0) {
  53. LOG_ERROR("Failed to read contents of %s: (%s)\n", path, strerror(errno));
  54. return false;
  55. }
  56. if (read >= n) {
  57. LOG_ERROR("File contained more data than expected %s\n", path);
  58. return false;
  59. }
  60. /* Ensure we're null terminated */
  61. dest[read] = '\0';
  62. /* Trim whitespace off the end */
  63. while (read > 0 && isspace(dest[read - 1])) {
  64. dest[read - 1] = '\0';
  65. read--;
  66. }
  67. return true;
  68. }
  69. static bool get_energy_uj(const char *rapl_name, uint32_t *energy_uj)
  70. {
  71. glob_t glo = { 0 };
  72. static const char *path = "/sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:*";
  73. /* Assert some sanity on this glob */
  74. if (glob(path, GLOB_NOSORT, NULL, &glo) != 0) {
  75. LOG_ERROR("glob failed for RAPL paths: (%s)\n", strerror(errno));
  76. return false;
  77. }
  78. /* If the glob doesn't find anything, this most likely means we don't
  79. * have an Intel CPU or we have a kernel which does not support RAPL on
  80. * our CPU.
  81. */
  82. if (glo.gl_pathc < 1) {
  83. LOG_ONCE(MSG,
  84. "Intel RAPL interface not found in sysfs. "
  85. "This is only problematic if you expected Intel iGPU "
  86. "power threshold optimization.");
  87. globfree(&glo);
  88. return false;
  89. }
  90. /* Walk the glob set */
  91. for (size_t i = 0; i < glo.gl_pathc; i++) {
  92. char name[32];
  93. if (!read_file_in_dir(glo.gl_pathv[i], "name", name, sizeof(name))) {
  94. return false;
  95. }
  96. /* We're searching for the directory where the file named "name"
  97. * contains the contents rapl_name. */
  98. if (strncmp(name, rapl_name, sizeof(name)) != 0) {
  99. continue;
  100. }
  101. char energy_uj_str[32];
  102. if (!read_file_in_dir(glo.gl_pathv[i], "energy_uj", energy_uj_str, sizeof(energy_uj_str))) {
  103. return false;
  104. }
  105. char *end = NULL;
  106. long long energy_uj_ll = strtoll(energy_uj_str, &end, 10);
  107. if (end == energy_uj_str) {
  108. LOG_ERROR("Invalid energy_uj contents: %s\n", energy_uj_str);
  109. return false;
  110. }
  111. if (energy_uj_ll < 0) {
  112. LOG_ERROR("Value of energy_uj is out of expected bounds: %lld\n", energy_uj_ll);
  113. return false;
  114. }
  115. /* Go ahead and clamp to 32 bits. We assume 32 bits later when
  116. * taking deltas and wrapping at 32 bits is exactly what the Linux
  117. * kernel's turbostat utility does so it's probably right.
  118. */
  119. *energy_uj = (uint32_t)energy_uj_ll;
  120. return true;
  121. }
  122. /* If we got here then the CPU and Kernel support RAPL and all our file
  123. * access has succeeded but we failed to find an entry with the right
  124. * name. This most likely means we're asking for "uncore" but are on a
  125. * machine that doesn't have an integrated GPU.
  126. */
  127. return false;
  128. }
  129. bool get_cpu_energy_uj(uint32_t *energy_uj)
  130. {
  131. return get_energy_uj("core", energy_uj);
  132. }
  133. bool get_igpu_energy_uj(uint32_t *energy_uj)
  134. {
  135. return get_energy_uj("uncore", energy_uj);
  136. }