cpucorectl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  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 <linux/limits.h>
  28. #include <sched.h>
  29. #include <unistd.h>
  30. #include "common-cpu.h"
  31. #include "common-logging.h"
  32. static int write_state(char *path, int state)
  33. {
  34. FILE *f = fopen(path, "w");
  35. if (!f) {
  36. LOG_ERROR("Couldn't open file at %s (%s)\n", path, strerror(errno));
  37. return 0;
  38. }
  39. if (putc(state, f) == EOF) {
  40. LOG_ERROR("Couldn't write to file at %s (%s)\n", path, strerror(errno));
  41. fclose(f);
  42. return 0;
  43. }
  44. fclose(f);
  45. return 1;
  46. }
  47. static void log_state(const int state, const long first, const long last)
  48. {
  49. if (state == '0') {
  50. if (first == last)
  51. LOG_MSG("parked core %ld\n", first);
  52. else
  53. LOG_MSG("parked cores %ld - %ld\n", first, last);
  54. } else {
  55. if (first == last)
  56. LOG_MSG("unparked core %ld\n", first);
  57. else
  58. LOG_MSG("unparked cores %ld - %ld\n", first, last);
  59. }
  60. }
  61. static int set_state(char *cpulist, int state)
  62. {
  63. char path[PATH_MAX];
  64. long from, to;
  65. char *list = cpulist;
  66. long first = -1, last = -1;
  67. while ((list = parse_cpulist(list, &from, &to))) {
  68. for (long cpu = from; cpu < to + 1; cpu++) {
  69. if (snprintf(path, PATH_MAX, "/sys/devices/system/cpu/cpu%ld/online", cpu) < 0) {
  70. LOG_ERROR("snprintf failed, will not apply cpu core parking!\n");
  71. return 0;
  72. }
  73. if (!write_state(path, state)) {
  74. /* on some systems one cannot park core #0 */
  75. if (cpu != 0) {
  76. if (state == '0') {
  77. LOG_ERROR("unable to park core #%ld, will not apply cpu core parking!\n",
  78. cpu);
  79. return -1;
  80. }
  81. LOG_ERROR("unable to unpark core #%ld\n", cpu);
  82. }
  83. } else {
  84. if (first == -1) {
  85. first = cpu;
  86. last = cpu;
  87. } else if (last + 1 == cpu) {
  88. last = cpu;
  89. } else {
  90. log_state(state, first, last);
  91. first = cpu;
  92. last = cpu;
  93. }
  94. }
  95. }
  96. }
  97. if (first != -1)
  98. log_state(state, first, last);
  99. return 1;
  100. }
  101. int main(int argc, char *argv[])
  102. {
  103. if (geteuid() != 0) {
  104. LOG_ERROR("This program must be run as root\n");
  105. return EXIT_FAILURE;
  106. }
  107. if (argc == 3 && strncmp(argv[1], "online", 6) == 0) {
  108. if (!set_state(argv[2], '1'))
  109. return EXIT_FAILURE;
  110. } else if (argc == 3 && strncmp(argv[1], "offline", 7) == 0) {
  111. if (!set_state(argv[2], '0'))
  112. return EXIT_FAILURE;
  113. } else {
  114. fprintf(stderr, "usage: cpucorectl [online]|[offline] VALUE]\n");
  115. return EXIT_FAILURE;
  116. }
  117. return EXIT_SUCCESS;
  118. }