Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- Version 2, December 2004
- Copyright (C) 2023 mittorn <mittorn@disroot.org>
- Everyone is permitted to copy and distribute verbatim or modified
- copies of this license document, and changing it is allowed as long
- as the name is changed.
- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- 0. You just DO WHAT THE FUCK YOU WANT TO.
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #define err(fmt, ...) fprintf(stderr, fmt ": %s\n", __VA_ARGS__, strerror(errno) )
- static int read_int(const char *path)
- {
- char buf[32];
- int fd = open(path, O_RDONLY);
- int r;
- if(fd < 0)
- {
- err("open(r, \"%s\")", path);
- return -1;
- }
- r = read(fd,buf,31);
- if(r <= 0)
- {
- err("read(%s)", path);
- r = -1;
- }
- else
- {
- buf[r] = 0;
- r = atoi(buf);
- }
- close(fd);
- return r;
- }
- static int write_int(const char *path, int val)
- {
- char buf[32];
- int fd = open(path, O_WRONLY);
- int len;
- // printf("write_int(\"%s\", %d);\n", path, val);
- if(fd < 0)
- {
- err("open(w, \"%s\", %d)", path, val);
- return -1;
- }
- len = snprintf(buf, 31, "%d\n", val);
- if(len < 0 || len > 31)
- return -1;
- len = write(fd, buf, len);
- if(len <= 0)
- err("write(%s, %d)", path, val);
- close(fd);
- return len > 0;
- }
- static int write_str_raw(const char *path, const char *val, size_t len)
- {
- char buf[32];
- int fd = open(path, O_WRONLY);
- // printf("write_str(\"%s\", %d);\n", path, atoi(val));
- if(fd < 0)
- {
- err("open(w, \"%s\", %d)", path, atoi(val));
- return -1;
- }
- len = write(fd, val, len);
- if(len <= 0)
- err("write(%s, %d, %lu)", path, atoi(val), len);
- close(fd);
- return len > 0;
- }
- #define write_cstr(x,y) write_str_raw(x,y "\n", sizeof(y "\n"))
- #define SMBUS "/sys/class/hwmon/hwmon2/"
- #define GPU "/sys/class/hwmon/hwmon3/"
- // setup smbus to always run fan at max speed on high temperatures
- void initialize_critical(void)
- {
- write_cstr(SMBUS "pwm5_enable", "2");
- write_cstr(SMBUS "pwm5_temp_sel", "8");
- write_cstr(SMBUS "pwm5_target_temp", "68000");
- write_cstr(SMBUS "pwm1_enable", "2");
- write_cstr(SMBUS "pwm1_temp_sel", "7");
- write_cstr(SMBUS "pwm1_target_temp", "60000");
- write_cstr(SMBUS "pwm6_enable", "2");
- write_cstr(SMBUS "pwm6_temp_sel", "8");
- write_cstr(SMBUS "pwm6_target_temp", "68000");
- }
- // change pwm floor, but 255 value will be enabled on high temperatures
- #define set_pwm_slow(x,y, z) do{if(z != y){write_int(SMBUS x "_floor", y);z = y;}}while(0)
- // this resets mode to 1 to skip fan stepping
- #define set_pwm_fast(x, y, z) do{ if(z!=y) { \
- write_cstr(SMBUS x "_enable", "1"); \
- write_int(SMBUS x "_floor", y); \
- write_int(SMBUS x, y); \
- write_cstr(SMBUS x "_enable", "2"); \
- z = y; \
- }} while(0)
- #define UPDATE_GPU 5
- #define UPDATE_CPU 2
- #define UPDATE_CRIT 8
- #define SMBUS_MIN 185
- #define FRONT_MIN 90
- #define BACK_MIN 100
- #define SMBUS_RANGE (255 - SMBUS_MIN)
- #define FRONT_RANGE (255 - FRONT_MIN)
- #define BACK_RANGE (255 - BACK_MIN)
- #define UPDATE_SLOW 50
- int main()
- {
- unsigned int counter = 0;
- int last_gpu, gpu = 0;
- int last_cpu, cpu = 0;
- int front_crit, back_crit;
- int front = FRONT_MIN,back = BACK_MIN, smbus = SMBUS_MIN;
- int front_set = front, back_set = back, smbus_set = smbus;
- initialize_critical();
- // slow config
- write_int(SMBUS "pwm5_floor", SMBUS_MIN);
- set_pwm_fast( "pwm6", FRONT_MIN, front_set);
- write_int(SMBUS "pwm1_floor", BACK_MIN);
- last_gpu = read_int(GPU "temp2_input");
- last_cpu = read_int(SMBUS "temp11_input");
- while(1)
- {
- int gpu_power = read_int(GPU "power1_average");
- if(!(counter % UPDATE_GPU))
- {
- int new_gpu = read_int(GPU "temp2_input");
- int gpu_diff = new_gpu - last_gpu;
- gpu = new_gpu;
- if(gpu_diff > 0)
- gpu += gpu_diff * 3;
- // printf("gpu %d %d\n", new_gpu, gpu);
- last_gpu = new_gpu;
- }
- if(!(counter % UPDATE_CPU))
- {
- int new_cpu = read_int(SMBUS "temp11_input");
- int cpu_diff = new_cpu - last_cpu;
- cpu = new_cpu;
- if(cpu_diff > 0)
- cpu += cpu_diff;
- // printf("cpu %d %d\n", new_cpu, cpu);
- last_cpu = new_cpu;
- }
- if(!(counter % UPDATE_CRIT))
- {
- front_crit = read_int(SMBUS "temp8_input");
- back_crit = read_int(SMBUS "temp7_input");
- }
- int gpu_score = (gpu - 40000) / 1000;
- if(gpu_score < 0)
- gpu_score = 0;
- gpu_score += gpu_power/1000000;
- int cpu_score = (cpu - 50000) / 200;
- if(cpu_score < 0)
- cpu_score = 0;
- int front_score = gpu_score *2 + cpu_score * 1.8;
- int back_score = gpu_score + cpu_score * 2;
- int smbus_score = gpu_score * 3;
- //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);
- int front_new = FRONT_MIN + front_score * FRONT_RANGE / 300;
- int back_new = BACK_MIN + back_score * BACK_RANGE / 250;
- int smbus_inp = (front_crit - 60000)/100;
- if(smbus_inp < 0)smbus_inp = 0;
- int smbus_new = SMBUS_MIN + smbus_score * SMBUS_RANGE / 400 + smbus_inp;
- if(front_new > 255)
- front_new = 255;
- if(back_new > 255)
- back_new = 255;
- if(smbus_new > 255)
- smbus_new = 255;
- // printf("%d %d %d\n", front_new, back_new, smbus_new);
- if(front_new > front)
- {
- if(front_new > 135 && !(front == 255 && front_crit >= 68000))
- set_pwm_fast("pwm6", front_new, front_set);
- else if((front_new / 5 < front / 5) || !(counter % UPDATE_SLOW))
- set_pwm_slow("pwm6", front_new, front_set);
- }
- if((front_new/ 6 < front / 6) || !(counter % UPDATE_SLOW) )
- {
- set_pwm_slow("pwm6", front_new, front_set);
- }
- front = front_new;
- if(back_new > back)
- {
- if(back_new > 125 && !(back == 255 && back_crit >= 60000))
- {
- set_pwm_fast("pwm1", back_new, back_set);
- }
- else if((back_new / 5 < back / 5) || !(counter % UPDATE_SLOW))
- set_pwm_slow("pwm1", back_new, back_set);
- }
- if((back_new/ 6 < back / 6) || !(counter % UPDATE_SLOW) )
- {
- set_pwm_slow("pwm1", back_new, back_set);
- }
- back = back_new;
- if(!(counter % UPDATE_SLOW) || (smbus_new / 6 != smbus / 6))
- set_pwm_slow("pwm5", smbus_new, smbus_set);
- smbus = smbus_new;
- counter++;
- usleep(250000);
- }
- }
Add Comment
Please, Sign In to add comment