gpuclockctl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "gpu-query.h"
  29. /* Helper to quit with usage */
  30. static const char *usage_text = "usage: gpuclockctl PCI_ID DEVICE [get] [set CORE MEM]";
  31. static void print_usage_and_exit(void)
  32. {
  33. fprintf(stderr, "%s\n", usage_text);
  34. exit(EXIT_FAILURE);
  35. }
  36. /* Helper to get and verify vendor value */
  37. static long get_vendor(const char *val)
  38. {
  39. char *end;
  40. long ret = strtol(val, &end, 0);
  41. if (!GPUVendorValid(ret) || end == val) {
  42. LOG_ERROR("ERROR: Invalid GPU Vendor passed (0x%04x)!\n", (unsigned short)ret);
  43. print_usage_and_exit();
  44. }
  45. return ret;
  46. }
  47. /* Helper to get and verify device value */
  48. static long get_device(const char *val)
  49. {
  50. char *end;
  51. long ret = strtol(val, &end, 10);
  52. if (ret < 0 || end == val) {
  53. LOG_ERROR("ERROR: Invalid GPU device passed (%ld)!\n", ret);
  54. print_usage_and_exit();
  55. }
  56. return ret;
  57. }
  58. /* Helper to get and verify core and mem value */
  59. static long get_coremem(const char *val)
  60. {
  61. char *end;
  62. long ret = strtol(val, &end, 10);
  63. if (ret < 0 || end == val) {
  64. LOG_ERROR("ERROR: Invalid core or mem value passed (%ld)!\n", ret);
  65. print_usage_and_exit();
  66. }
  67. return ret;
  68. }
  69. /**
  70. * Main entry point, dispatch to the appropriate helper
  71. */
  72. int main(int argc, char *argv[])
  73. {
  74. if (argc == 4 && strncmp(argv[3], "get", 3) == 0) {
  75. /* Get and verify the vendor and device */
  76. struct GameModeGPUInfo info;
  77. memset(&info, 0, sizeof(info));
  78. info.vendor = get_vendor(argv[1]);
  79. info.device = get_device(argv[2]);
  80. /* Fetch the state and print it out */
  81. get_gpu_state(&info);
  82. printf("%ld %ld\n", info.core, info.mem);
  83. } else if (argc == 6 && strncmp(argv[3], "set", 3) == 0) {
  84. /* Must be root to set the state */
  85. if (geteuid() != 0) {
  86. fprintf(stderr, "gpuclockctl must be run as root to set values\n");
  87. print_usage_and_exit();
  88. }
  89. /* Get and verify the vendor and device */
  90. struct GameModeGPUInfo info;
  91. memset(&info, 0, sizeof(info));
  92. info.vendor = get_vendor(argv[1]);
  93. info.device = get_device(argv[2]);
  94. info.core = get_coremem(argv[4]);
  95. info.mem = get_coremem(argv[4]);
  96. return set_gpu_state(&info);
  97. } else {
  98. print_usage_and_exit();
  99. }
  100. return EXIT_SUCCESS;
  101. }