Guest User

fandaemon example

a guest
Sep 9th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.55 KB | Source Code | 0 0
  1. /*            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  2.                     Version 2, December 2004
  3.  
  4.  Copyright (C) 2023 mittorn <mittorn@disroot.org>
  5.  
  6.  Everyone is permitted to copy and distribute verbatim or modified
  7.  copies of this license document, and changing it is allowed as long
  8.  as the name is changed.
  9.  
  10.             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  11.    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  12.  
  13.   0. You just DO WHAT THE FUCK YOU WANT TO.
  14. */
  15.  
  16.  
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23.  
  24. #define err(fmt, ...) fprintf(stderr, fmt ": %s\n", __VA_ARGS__, strerror(errno) )
  25.  
  26. static int read_int(const char *path)
  27. {
  28.         char buf[32];
  29.         int fd = open(path, O_RDONLY);
  30.         int r;
  31.         if(fd < 0)
  32.         {
  33.                 err("open(r, \"%s\")", path);
  34.                 return -1;
  35.         }
  36.         r = read(fd,buf,31);
  37.         if(r <= 0)
  38.         {
  39.                 err("read(%s)", path);
  40.                 r = -1;
  41.         }
  42.         else
  43.         {
  44.                 buf[r] = 0;
  45.                 r = atoi(buf);
  46.         }
  47.         close(fd);
  48.         return r;
  49. }
  50.  
  51. static int write_int(const char *path, int val)
  52. {
  53.         char buf[32];
  54.         int fd = open(path, O_WRONLY);
  55.         int len;
  56. //      printf("write_int(\"%s\", %d);\n", path, val);
  57.         if(fd < 0)
  58.         {
  59.                 err("open(w, \"%s\", %d)", path, val);
  60.                 return -1;
  61.         }
  62.         len = snprintf(buf, 31, "%d\n", val);
  63.         if(len < 0 || len > 31)
  64.                 return -1;
  65.         len = write(fd, buf, len);
  66.         if(len <= 0)
  67.                 err("write(%s, %d)", path, val);
  68.         close(fd);
  69.         return len > 0;
  70. }
  71.  
  72. static int write_str_raw(const char *path, const char *val, size_t len)
  73. {
  74.         char buf[32];
  75.         int fd = open(path, O_WRONLY);
  76. //      printf("write_str(\"%s\", %d);\n", path, atoi(val));
  77.         if(fd < 0)
  78.         {
  79.                 err("open(w, \"%s\", %d)", path, atoi(val));
  80.                 return -1;
  81.         }
  82.         len = write(fd, val, len);
  83.         if(len <= 0)
  84.                 err("write(%s, %d, %lu)", path, atoi(val), len);
  85.         close(fd);
  86.         return len > 0;
  87. }
  88.  
  89. #define write_cstr(x,y) write_str_raw(x,y "\n", sizeof(y "\n"))
  90.  
  91. #define SMBUS "/sys/class/hwmon/hwmon2/"
  92. #define GPU "/sys/class/hwmon/hwmon3/"
  93.  
  94. // setup smbus to always run fan at max speed on high temperatures
  95. void initialize_critical(void)
  96. {
  97.         write_cstr(SMBUS "pwm5_enable", "2");
  98.         write_cstr(SMBUS "pwm5_temp_sel", "8");
  99.         write_cstr(SMBUS "pwm5_target_temp", "68000");
  100.         write_cstr(SMBUS "pwm1_enable", "2");
  101.         write_cstr(SMBUS "pwm1_temp_sel", "7");
  102.         write_cstr(SMBUS "pwm1_target_temp", "60000");
  103.         write_cstr(SMBUS "pwm6_enable", "2");
  104.         write_cstr(SMBUS "pwm6_temp_sel", "8");
  105.         write_cstr(SMBUS "pwm6_target_temp", "68000");
  106. }
  107.  
  108. // change pwm floor, but 255 value will be enabled on high temperatures
  109. #define set_pwm_slow(x,y, z) do{if(z != y){write_int(SMBUS x "_floor", y);z = y;}}while(0)
  110.  
  111. // this resets mode to 1 to skip fan stepping
  112. #define set_pwm_fast(x, y, z) do{ if(z!=y) { \
  113.                                                         write_cstr(SMBUS x "_enable", "1"); \
  114.                                                         write_int(SMBUS x "_floor", y); \
  115.                                                         write_int(SMBUS x, y); \
  116.                                                         write_cstr(SMBUS x "_enable", "2"); \
  117.                                                         z = y; \
  118.                                                 }} while(0)
  119.  
  120. #define UPDATE_GPU 5
  121. #define UPDATE_CPU 2
  122. #define UPDATE_CRIT 8
  123.  
  124. #define SMBUS_MIN 185
  125. #define FRONT_MIN 90
  126. #define BACK_MIN 100
  127. #define SMBUS_RANGE (255 - SMBUS_MIN)
  128. #define FRONT_RANGE (255 - FRONT_MIN)
  129. #define BACK_RANGE (255 - BACK_MIN)
  130. #define UPDATE_SLOW 50
  131.  
  132.  
  133. int main()
  134. {
  135.         unsigned int counter = 0;
  136.         int last_gpu, gpu = 0;
  137.         int last_cpu, cpu = 0;
  138.         int front_crit, back_crit;
  139.         int front = FRONT_MIN,back = BACK_MIN, smbus = SMBUS_MIN;
  140.         int front_set = front, back_set = back, smbus_set = smbus;
  141.         initialize_critical();
  142.  
  143. // slow config
  144.         write_int(SMBUS "pwm5_floor", SMBUS_MIN);
  145.         set_pwm_fast( "pwm6", FRONT_MIN, front_set);
  146.         write_int(SMBUS "pwm1_floor", BACK_MIN);
  147.         last_gpu = read_int(GPU "temp2_input");
  148.         last_cpu = read_int(SMBUS "temp11_input");
  149.         while(1)
  150.         {
  151.                 int gpu_power = read_int(GPU "power1_average");
  152.                 if(!(counter % UPDATE_GPU))
  153.                 {
  154.                         int new_gpu = read_int(GPU "temp2_input");
  155.                         int gpu_diff = new_gpu - last_gpu;
  156.                         gpu = new_gpu;
  157.                         if(gpu_diff > 0)
  158.                                 gpu += gpu_diff * 3;
  159. //                      printf("gpu %d %d\n", new_gpu, gpu);
  160.                         last_gpu = new_gpu;
  161.                 }
  162.                 if(!(counter % UPDATE_CPU))
  163.                 {
  164.                         int new_cpu = read_int(SMBUS "temp11_input");
  165.                         int cpu_diff = new_cpu - last_cpu;
  166.                         cpu = new_cpu;
  167.                         if(cpu_diff > 0)
  168.                                 cpu += cpu_diff;
  169. //                      printf("cpu %d %d\n", new_cpu, cpu);
  170.  
  171.                         last_cpu = new_cpu;
  172.                 }
  173.                 if(!(counter % UPDATE_CRIT))
  174.                 {
  175.                         front_crit = read_int(SMBUS "temp8_input");
  176.                         back_crit = read_int(SMBUS "temp7_input");
  177.                 }
  178.                 int gpu_score = (gpu - 40000) / 1000;
  179.                 if(gpu_score < 0)
  180.                         gpu_score = 0;
  181.                 gpu_score += gpu_power/1000000;
  182.  
  183.                 int cpu_score = (cpu - 50000) / 200;
  184.                 if(cpu_score < 0)
  185.                         cpu_score = 0;
  186.                 int front_score = gpu_score *2 + cpu_score * 1.8;
  187.                 int back_score = gpu_score + cpu_score * 2;
  188.                 int smbus_score = gpu_score * 3;
  189.                 //printf("state: %d %d %d %d %d %d %d %d\n", cpu, gpu, gpu_power/1000000, cpu_score, gpu_score, front_score, back_score, smbus_score);
  190.                 int front_new = FRONT_MIN + front_score * FRONT_RANGE / 300;
  191.                 int back_new =  BACK_MIN + back_score * BACK_RANGE / 250;
  192.                 int smbus_inp = (front_crit - 60000)/100;
  193.                         if(smbus_inp < 0)smbus_inp = 0;
  194.                 int smbus_new = SMBUS_MIN + smbus_score * SMBUS_RANGE / 400 + smbus_inp;
  195.  
  196.                 if(front_new > 255)
  197.                         front_new = 255;
  198.                 if(back_new > 255)
  199.                         back_new = 255;
  200.                 if(smbus_new > 255)
  201.                         smbus_new = 255;
  202.  
  203. //              printf("%d %d %d\n", front_new, back_new, smbus_new);
  204.  
  205.                 if(front_new > front)
  206.                 {
  207.                         if(front_new > 135 && !(front == 255 && front_crit >= 68000))
  208.                                 set_pwm_fast("pwm6", front_new, front_set);
  209.                         else if((front_new / 5 < front / 5) || !(counter % UPDATE_SLOW))
  210.                                 set_pwm_slow("pwm6", front_new, front_set);
  211.                 }
  212.                 if((front_new/ 6 < front / 6) || !(counter % UPDATE_SLOW) )
  213.                 {
  214.                         set_pwm_slow("pwm6", front_new, front_set);
  215.                 }
  216.                 front = front_new;
  217.                 if(back_new > back)
  218.                 {
  219.                         if(back_new > 125 && !(back == 255 && back_crit >= 60000))
  220.                         {
  221.                                         set_pwm_fast("pwm1", back_new, back_set);
  222.                         }
  223.                         else if((back_new / 5 < back / 5) || !(counter % UPDATE_SLOW))
  224.                                 set_pwm_slow("pwm1", back_new, back_set);
  225.                 }
  226.                 if((back_new/ 6 < back / 6) || !(counter % UPDATE_SLOW) )
  227.                 {
  228.                         set_pwm_slow("pwm1", back_new, back_set);
  229.                 }
  230.                 back = back_new;
  231.                 if(!(counter % UPDATE_SLOW) || (smbus_new / 6 != smbus / 6))
  232.                         set_pwm_slow("pwm5", smbus_new, smbus_set);
  233.                 smbus = smbus_new;
  234.                 counter++;
  235.                 usleep(250000);
  236.         }
  237. }
Add Comment
Please, Sign In to add comment