main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. cputhrottle - a utility to throttle the CPU speed from a default "powersave"
  3. configuration to a performance one to its full capacity - without the need
  4. to reboot or overclock the machine!
  5. This utility was written with a Raspberry Pi 4 in mind, but other SoCs could
  6. benefit from it, like the beefier RockPro or etc. Same principle. And TBH
  7. any computer running Linux should work, actually, given its an interface of
  8. the Linux kernel.
  9. "Dude you could've written a shell script to do the same darn thing in 5min."
  10. - and also not have learnt anything new either. This is another opportunity
  11. to learn C and string formatting!
  12. Copyright 2022 kzimmermann <https://tilde.town/~kzimmermann> all rights
  13. reserved.
  14. This program is Free Software licensed under the terms and conditions of the
  15. GNU GPL v3. See <https://www.gnu.org/licenses/> for more information.
  16. */
  17. #define _GNU_SOURCE
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "specs.h"
  22. #include "funcs.h"
  23. // The main command to be run (as root) is:
  24. // cpupower frequency-set --max 1500000 --governor performance
  25. // You can check the clock from here:
  26. // cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq
  27. int
  28. main(int argc, char* argv[])
  29. {
  30. long freq;
  31. // read content from file. It(should be about 20char max)
  32. freq = query_file(CPU_CUR_FILE);
  33. if (freq == -1) {
  34. printf("Unable to open '%s'\nDoes the file exist?\n", CPU_CUR_FILE);
  35. printf("Alternatively, are you root?\n");
  36. return 1;
  37. }
  38. printf("The current CPU frequency is %ld kHz\n", freq);
  39. freq = query_file(CPU_MAX_FILE);
  40. if (freq == -1) {
  41. printf("Unable to open '%s'\nDoes the file exist?\n", CPU_MAX_FILE);
  42. printf("Alternatively, are you root?\n");
  43. return 1;
  44. }
  45. // asprintf anyone??
  46. char *stmt;
  47. asprintf(&stmt, QUERY_COMMAND, freq);
  48. // execute the query and free up resources regardless of failure.
  49. int sys_status = system(stmt);
  50. free(stmt);
  51. if (sys_status != 0) {
  52. printf("Error: could not change CPU frequency to %ld kHz.\n", freq);
  53. printf("This is most likely due to the lack of permissions (i.e. no root).\n");
  54. return 1;
  55. }
  56. printf("CPU frequency now set to %ld kHz.\n", freq);
  57. return 0;
  58. }