123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #define _GNU_SOURCE
- #include "logging.h"
- #include "external-helper.h"
- #include "gpu-control.h"
- static const char *usage_text =
- "usage: gpuclockctl PCI_ID DEVICE [get] [set CORE MEM [PERF_LEVEL]]]";
- static void print_usage_and_exit(void)
- {
- fprintf(stderr, "%s\n", usage_text);
- exit(EXIT_FAILURE);
- }
- int get_gpu_state(struct GameModeGPUInfo *info)
- {
- fprintf(stderr, "Fetching GPU state is currently unimplemented!\n");
- return info != NULL;
- }
- int set_gpu_state_nv(struct GameModeGPUInfo *info)
- {
- if (info->vendor != Vendor_NVIDIA)
- return -1;
-
-
- char core_arg[64];
- snprintf(core_arg,
- 64,
- "[gpu:%ld]/GPUGraphicsClockOffset[%ld]=%ld",
- info->device,
- info->nv_perf_level,
- info->core);
- const char *exec_args_core[] = { "/usr/bin/nvidia-settings", "-a", core_arg, NULL };
- if (run_external_process(exec_args_core) != 0) {
- LOG_ERROR("Failed to set %s!\n", core_arg);
- return -1;
- }
-
- char mem_arg[64];
- snprintf(mem_arg,
- 64,
- "[gpu:%ld]/GPUMemoryTransferRateOffset[%ld]=%ld",
- info->device,
- info->nv_perf_level,
- info->mem);
- const char *exec_args_mem[] = { "/usr/bin/nvidia-settings", "-a", mem_arg, NULL };
- if (run_external_process(exec_args_mem) != 0) {
- LOG_ERROR("Failed to set %s!\n", mem_arg);
- return -1;
- }
- return 0;
- }
- static int set_gpu_state_amd_file(const char *filename, long device, long value)
- {
- const char *drm_path = "/sys/class/drm/card%ld/device/%s";
- char path[64];
- snprintf(path, 64, drm_path, device, filename);
- FILE *file = fopen(path, "w");
- if (!file) {
- LOG_ERROR("Could not open %s for write (%s)!\n", path, strerror(errno));
- return -1;
- }
- if (fprintf(file, "%ld", value) < 0) {
- LOG_ERROR("Could not write to %s (%s)!\n", path, strerror(errno));
- return -1;
- }
- if (fclose(file) != 0) {
- LOG_ERROR("Could not close %s after writing (%s)!\n", path, strerror(errno));
- return -1;
- }
- return 0;
- }
- int set_gpu_state_amd(struct GameModeGPUInfo *info)
- {
- if (info->vendor != Vendor_AMD)
- return -1;
-
- if (set_gpu_state_amd_file("pp_sclk_od", info->device, info->core) != 0)
- return -1;
- if (set_gpu_state_amd_file("pp_mclk_od", info->device, info->mem) != 0)
- return -1;
- return 0;
- }
- static long get_vendor(const char *val)
- {
- char *end;
- long ret = strtol(val, &end, 0);
- if (!GPUVendorValid(ret) || end == val) {
- LOG_ERROR("Invalid GPU Vendor passed (0x%04x)!\n", (unsigned short)ret);
- print_usage_and_exit();
- }
- return ret;
- }
- static long get_device(const char *val)
- {
- char *end;
- long ret = strtol(val, &end, 10);
- if (ret < 0 || end == val) {
- LOG_ERROR("Invalid GPU device passed (%ld)!\n", ret);
- print_usage_and_exit();
- }
- return ret;
- }
- static long get_generic_value(const char *val)
- {
- char *end;
- long ret = strtol(val, &end, 10);
- if (ret < 0 || end == val) {
- LOG_ERROR("Invalid value passed (%ld)!\n", ret);
- print_usage_and_exit();
- }
- return ret;
- }
- int main(int argc, char *argv[])
- {
- if (argc == 4 && strncmp(argv[3], "get", 3) == 0) {
-
- struct GameModeGPUInfo info;
- memset(&info, 0, sizeof(info));
- info.vendor = get_vendor(argv[1]);
- info.device = get_device(argv[2]);
-
- get_gpu_state(&info);
- printf("%ld %ld\n", info.core, info.mem);
- } else if (argc >= 6 && argc <= 7 && strncmp(argv[3], "set", 3) == 0) {
-
- if (geteuid() != 0) {
- fprintf(stderr, "gpuclockctl must be run as root to set values\n");
- print_usage_and_exit();
- }
-
- struct GameModeGPUInfo info;
- memset(&info, 0, sizeof(info));
- info.vendor = get_vendor(argv[1]);
- info.device = get_device(argv[2]);
- info.core = get_generic_value(argv[4]);
- info.mem = get_generic_value(argv[5]);
- if (info.vendor == Vendor_NVIDIA)
- info.nv_perf_level = get_generic_value(argv[6]);
- printf("gpuclockctl setting core:%ld mem:%ld on device:%ld with vendor 0x%04x\n",
- info.core,
- info.mem,
- info.device,
- (unsigned short)info.vendor);
- if (info.vendor == Vendor_NVIDIA)
- printf("on Performance Level %ld\n", info.nv_perf_level);
- switch (info.vendor) {
- case Vendor_NVIDIA:
- return set_gpu_state_nv(&info);
- case Vendor_AMD:
- return set_gpu_state_amd(&info);
- default:
- printf("Currently unsupported GPU vendor 0x%04x, doing nothing!\n", (short)info.vendor);
- break;
- }
- } else {
- print_usage_and_exit();
- }
- return EXIT_SUCCESS;
- }
|