1
0

gpuclockctl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright (c) 2017-2018, 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 "external-helper.h"
  29. #include "gpu-control.h"
  30. // TODO
  31. // Apply Nvidia GPU settings (CoolBits will be needed)
  32. // Apply AMD GPU settings (Will need user changing pwm1_enable)
  33. // Provide documentation on optimisations
  34. // Intel?
  35. // Gather GPU type and information automatically if possible
  36. /* Helper to quit with usage */
  37. static const char *usage_text =
  38. "usage: gpuclockctl PCI_ID DEVICE [get] [set CORE MEM [PERF_LEVEL]]]";
  39. static void print_usage_and_exit(void)
  40. {
  41. fprintf(stderr, "%s\n", usage_text);
  42. exit(EXIT_FAILURE);
  43. }
  44. /**
  45. * Get the gpu state
  46. * Populates the struct with the GPU info on the system
  47. */
  48. int get_gpu_state(struct GameModeGPUInfo *info)
  49. {
  50. fprintf(stderr, "Fetching GPU state is currently unimplemented!\n");
  51. return info != NULL;
  52. }
  53. /**
  54. * Set the gpu state based on input parameters on Nvidia
  55. */
  56. int set_gpu_state_nv(struct GameModeGPUInfo *info)
  57. {
  58. if (info->vendor != Vendor_NVIDIA)
  59. return -1;
  60. // These commands don't technically even need root
  61. /* Set the GPUGraphicsClockOffset parameter */
  62. char core_arg[64];
  63. snprintf(core_arg,
  64. 64,
  65. "[gpu:%ld]/GPUGraphicsClockOffset[%ld]=%ld",
  66. info->device,
  67. info->nv_perf_level,
  68. info->core);
  69. const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-a", core_arg, NULL };
  70. if (run_external_process(exec_args_core) != 0) {
  71. LOG_ERROR("ERROR: Failed to set %s!\n", core_arg);
  72. return -1;
  73. }
  74. /* Set the GPUMemoryTransferRateOffset parameter */
  75. char mem_arg[64];
  76. snprintf(mem_arg,
  77. 64,
  78. "[gpu:%ld]/GPUMemoryTransferRateOffset[%ld]=%ld",
  79. info->device,
  80. info->nv_perf_level,
  81. info->mem);
  82. const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-a", mem_arg, NULL };
  83. if (run_external_process(exec_args_mem) != 0) {
  84. LOG_ERROR("ERROR: Failed to set %s!\n", mem_arg);
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. /**
  90. * Set the gpu state based on input parameters on amd
  91. */
  92. int set_gpu_state_amd(struct GameModeGPUInfo *info)
  93. {
  94. if(info->vendor != Vendor_AMD)
  95. return -1;
  96. // We'll want to set both the following:
  97. // core: device/pp_sclk_od (0%+ additional)
  98. // mem: device/pp_mclk_od (0%+ additional)
  99. // Guide from https://www.maketecheasier.com/overclock-amd-gpu-linux/
  100. fprintf(stderr, "Setting GPU parameters on AMD is currently unimplemented!\n");
  101. return 0;
  102. }
  103. /* Helper to get and verify vendor value */
  104. static long get_vendor(const char *val)
  105. {
  106. char *end;
  107. long ret = strtol(val, &end, 0);
  108. if (!GPUVendorValid(ret) || end == val) {
  109. LOG_ERROR("ERROR: Invalid GPU Vendor passed (0x%04x)!\n", (unsigned short)ret);
  110. print_usage_and_exit();
  111. }
  112. return ret;
  113. }
  114. /* Helper to get and verify device value */
  115. static long get_device(const char *val)
  116. {
  117. char *end;
  118. long ret = strtol(val, &end, 10);
  119. if (ret < 0 || end == val) {
  120. LOG_ERROR("ERROR: Invalid GPU device passed (%ld)!\n", ret);
  121. print_usage_and_exit();
  122. }
  123. return ret;
  124. }
  125. /* Helper to get and verify core and mem value */
  126. static long get_generic_value(const char *val)
  127. {
  128. char *end;
  129. long ret = strtol(val, &end, 10);
  130. if (ret < 0 || end == val) {
  131. LOG_ERROR("ERROR: Invalid value passed (%ld)!\n", ret);
  132. print_usage_and_exit();
  133. }
  134. return ret;
  135. }
  136. /**
  137. * Main entry point, dispatch to the appropriate helper
  138. */
  139. int main(int argc, char *argv[])
  140. {
  141. if (argc == 4 && strncmp(argv[3], "get", 3) == 0) {
  142. /* Get and verify the vendor and device */
  143. struct GameModeGPUInfo info;
  144. memset(&info, 0, sizeof(info));
  145. info.vendor = get_vendor(argv[1]);
  146. info.device = get_device(argv[2]);
  147. /* Fetch the state and print it out */
  148. get_gpu_state(&info);
  149. printf("%ld %ld\n", info.core, info.mem);
  150. } else if (argc >= 6 && argc <= 7 && strncmp(argv[3], "set", 3) == 0) {
  151. /* Must be root to set the state */
  152. if (geteuid() != 0) {
  153. fprintf(stderr, "gpuclockctl must be run as root to set values\n");
  154. print_usage_and_exit();
  155. }
  156. /* Get and verify the vendor and device */
  157. struct GameModeGPUInfo info;
  158. memset(&info, 0, sizeof(info));
  159. info.vendor = get_vendor(argv[1]);
  160. info.device = get_device(argv[2]);
  161. info.core = get_generic_value(argv[4]);
  162. info.mem = get_generic_value(argv[5]);
  163. if (info.vendor == Vendor_NVIDIA)
  164. info.nv_perf_level = get_generic_value(argv[6]);
  165. printf("gpuclockctl setting core:%ld mem:%ld on device:%ld with vendor 0x%04x\n",
  166. info.core,
  167. info.mem,
  168. info.device,
  169. (unsigned short)info.vendor);
  170. if (info.vendor == Vendor_NVIDIA)
  171. printf("on Performance Level %ld\n", info.nv_perf_level);
  172. switch (info.vendor) {
  173. case Vendor_NVIDIA:
  174. return set_gpu_state_nv(&info);
  175. case Vendor_AMD:
  176. return set_gpu_state_amd(&info);
  177. default:
  178. printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
  179. break;
  180. }
  181. } else {
  182. print_usage_and_exit();
  183. }
  184. return EXIT_SUCCESS;
  185. }