gamemode-gpu.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "config.h"
  28. #include "external-helper.h"
  29. #include "helpers.h"
  30. #include "logging.h"
  31. #include "gamemode.h"
  32. #include "daemon_config.h"
  33. #include "gpu-control.h"
  34. /**
  35. * Attempts to identify the current in use GPU information
  36. */
  37. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info)
  38. {
  39. int status = 0;
  40. /* Verify input, this is programmer error */
  41. if (!info || *info)
  42. FATAL_ERROR("Invalid GameModeGPUInfo passed to %s", __func__);
  43. /* Early out if we have this feature turned off */
  44. char apply[CONFIG_VALUE_MAX];
  45. config_get_apply_gpu_optimisations(config, apply);
  46. if (strlen(apply) == 0) {
  47. return 0;
  48. } else if (strncmp(apply, "accept-responsibility", CONFIG_VALUE_MAX) != 0) {
  49. LOG_ERROR(
  50. "apply_gpu_optimisations set to value other than \"accept-responsibility\" (%s), will "
  51. "not apply GPU optimisations!\n",
  52. apply);
  53. return -1;
  54. }
  55. /* Create the context */
  56. GameModeGPUInfo *new_info = malloc(sizeof(GameModeGPUInfo));
  57. memset(new_info, 0, sizeof(GameModeGPUInfo));
  58. /* Get the config parameters */
  59. new_info->device = config_get_gpu_device(config);
  60. /* verify device ID */
  61. if (new_info->device == -1) {
  62. LOG_ERROR(
  63. "Invalid gpu_device value set in configuration, will not apply "
  64. "optimisations!\n");
  65. free(new_info);
  66. return -1;
  67. }
  68. /* Fill in GPU vendor */
  69. char path[64] = { 0 };
  70. if (snprintf(path, 64, "/sys/class/drm/card%ld/device/vendor", new_info->device) < 0) {
  71. LOG_ERROR("snprintf failed, will not apply gpu optimisations!\n");
  72. return -1;
  73. }
  74. FILE *vendor = fopen(path, "r");
  75. if (!vendor) {
  76. LOG_ERROR("Couldn't open vendor file at %s, will not apply gpu optimisations!\n", path);
  77. return -1;
  78. }
  79. char buff[64];
  80. if (fgets(buff, 64, vendor) != NULL) {
  81. new_info->vendor = strtol(buff, NULL, 0);
  82. } else {
  83. LOG_ERROR("Coudn't read contents of file %s, will not apply optimisations!\n", path);
  84. return -1;
  85. }
  86. /* verify GPU vendor */
  87. if (!GPUVendorValid(new_info->vendor)) {
  88. LOG_ERROR("Unknown vendor value (0x%04x) found, cannot apply optimisations!\n",
  89. (unsigned int)new_info->vendor);
  90. LOG_ERROR("Known values are: 0x%04x (NVIDIA) 0x%04x (AMD) 0x%04x (Intel)\n",
  91. Vendor_NVIDIA,
  92. Vendor_AMD,
  93. Vendor_Intel);
  94. free(new_info);
  95. return -1;
  96. }
  97. /* Load the config based on GPU and also verify the values are sane */
  98. switch (new_info->vendor) {
  99. case Vendor_NVIDIA:
  100. new_info->core = config_get_nv_core_clock_mhz_offset(config);
  101. new_info->mem = config_get_nv_mem_clock_mhz_offset(config);
  102. /* Reject values over some guessed values
  103. * If a user wants to go into very unsafe levels they can recompile
  104. */
  105. const int nv_core_hard_limit = 200;
  106. const int nv_mem_hard_limit = 2000;
  107. if (new_info->core > nv_core_hard_limit || new_info->mem > nv_mem_hard_limit) {
  108. LOG_ERROR(
  109. "NVIDIA Overclock value above safety levels of +%d (core) +%d (mem), will "
  110. "not overclock!\n",
  111. nv_core_hard_limit,
  112. nv_mem_hard_limit);
  113. LOG_ERROR("nv_core_clock_mhz_offset:%ld nv_mem_clock_mhz_offset:%ld\n",
  114. new_info->core,
  115. new_info->mem);
  116. free(new_info);
  117. return -1;
  118. }
  119. /* Sanity check the performance level value as well */
  120. new_info->nv_perf_level = config_get_nv_perf_level(config);
  121. if (new_info->nv_perf_level < 0 || new_info->nv_perf_level > 16) {
  122. LOG_ERROR(
  123. "NVIDIA Performance level value likely invalid (%ld), will not apply "
  124. "optimisations!\n",
  125. new_info->nv_perf_level);
  126. free(new_info);
  127. return -1;
  128. }
  129. break;
  130. case Vendor_AMD:
  131. new_info->core = config_get_amd_core_clock_percentage(config);
  132. new_info->mem = config_get_amd_mem_clock_percentage(config);
  133. /* Reject values over 20%
  134. * If a user wants to go into very unsafe levels they can recompile
  135. * As far as I can tell the driver doesn't allow values over 20 anyway
  136. */
  137. const int amd_hard_limit = 20;
  138. if (new_info->core > amd_hard_limit || new_info->mem > amd_hard_limit) {
  139. LOG_ERROR("AMD Overclock value above safety level of %d%%, will not overclock!\n",
  140. amd_hard_limit);
  141. LOG_ERROR("amd_core_clock_percentage:%ld amd_mem_clock_percentage:%ld\n",
  142. new_info->core,
  143. new_info->mem);
  144. free(new_info);
  145. return -1;
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. /* Give back the new gpu info */
  152. *info = new_info;
  153. return status;
  154. }
  155. /* Simply used to free the GPU info object */
  156. void game_mode_free_gpu(GameModeGPUInfo **info)
  157. {
  158. /* Simply free the object */
  159. free(*info);
  160. *info = NULL;
  161. }
  162. //#include <linux/limits.h>
  163. //#include <stdio.h>
  164. //#include <sys/wait.h>
  165. //#include <unistd.h>
  166. /**
  167. * Applies GPU optimisations when gamemode is active and removes them after
  168. */
  169. int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply)
  170. {
  171. // Null info means don't apply anything
  172. if (!info)
  173. return 0;
  174. LOG_MSG("Requesting GPU optimisations on device:%ld with settings core:%ld clock:%ld\n",
  175. info->device,
  176. info->core,
  177. info->mem);
  178. /* Generate the input strings */
  179. char vendor[7];
  180. snprintf(vendor, 7, "0x%04x", (short)info->vendor);
  181. char device[4];
  182. snprintf(device, 4, "%ld", info->device);
  183. char core[8];
  184. snprintf(core, 8, "%ld", info->core);
  185. char mem[8];
  186. snprintf(mem, 8, "%ld", info->mem);
  187. char nv_perf_level[4];
  188. snprintf(nv_perf_level, 4, "%ld", info->nv_perf_level);
  189. // Set up our command line to pass to gpuclockctl
  190. const char *const exec_args[] = {
  191. "/usr/bin/pkexec",
  192. LIBEXECDIR "/gpuclockctl",
  193. vendor,
  194. device,
  195. "set",
  196. apply ? core : "0",
  197. apply ? mem : "0",
  198. info->vendor == Vendor_NVIDIA ? nv_perf_level : NULL, /* Only use this if Nvidia */
  199. NULL,
  200. };
  201. if (run_external_process(exec_args, NULL, -1) != 0) {
  202. LOG_ERROR("Failed to call gpuclockctl, could not apply optimisations!\n");
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. int game_mode_get_gpu(GameModeGPUInfo *info)
  208. {
  209. if (!info)
  210. return 0;
  211. /* Generate the input strings */
  212. char vendor[7];
  213. snprintf(vendor, 7, "0x%04x", (short)info->vendor);
  214. char device[4];
  215. snprintf(device, 4, "%ld", info->device);
  216. char nv_perf_level[4];
  217. snprintf(nv_perf_level, 4, "%ld", info->nv_perf_level);
  218. // Set up our command line to pass to gpuclockctl
  219. // This doesn't need pkexec as get does not need elevated perms
  220. const char *const exec_args[] = {
  221. LIBEXECDIR "/gpuclockctl",
  222. vendor,
  223. device,
  224. "get",
  225. info->vendor == Vendor_NVIDIA ? nv_perf_level : NULL, /* Only use this if Nvidia */
  226. NULL,
  227. };
  228. char buffer[EXTERNAL_BUFFER_MAX] = { 0 };
  229. if (run_external_process(exec_args, buffer, -1) != 0) {
  230. LOG_ERROR("Failed to call gpuclockctl, could get values!\n");
  231. return -1;
  232. }
  233. int core = 0;
  234. int mem = 0;
  235. if (sscanf(buffer, "%i %i", &core, &mem) != 2) {
  236. LOG_ERROR("Failed to parse gpuclockctl output: %s\n", buffer);
  237. return -1;
  238. }
  239. info->core = core;
  240. info->mem = mem;
  241. return 0;
  242. }