aboutsummaryrefslogtreecommitdiffstats
path: root/cputemp2maxfreq.c
blob: 17609965310b23cd021fbc4eb9aab557df0f46f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "sysfs.h"
#include "cpufreq.h"
#include "debug.h"

struct s_cpudata {
 long int min_freq;
 long int max_freq;
 long int cur_freq;
 long int cur_temp;
 long int scale_max;
};

struct s_config {
 char *governor;
 long int max_temp;
 char *temp_input;
 long int freq_step;
 long int fallback_freq;
 unsigned int interval;
};

struct s_cpudata cpudata;
struct s_config config={"conservative",70000,"/sys/devices/virtual/thermal/thermal_zone0/temp",100000,2000000,10};

int main()
{
 long int diff;
 long int newfreq;
 
 printf("Governor: %s\nTemp input: %s\n",config.governor,config.temp_input);

 if (cpufreq_set_str("scaling_governor",config.governor,0)!=0)
 {
  printf("Failed to set governow\n");
  exit(1);
 }

 cpudata.min_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq");
 cpudata.max_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq");
 cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");

 while(1)
 {
  cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq");
  cpudata.cur_temp=sysfs_read_long_int(config.temp_input);


  DEBUG1_MAIN("Data: %ld %ld %ld %ld %ld\n",cpudata.cur_temp,config.max_temp,cpudata.max_freq,cpudata.scale_max,cpudata.cur_freq);

  if ((cpudata.cur_temp<config.max_temp) && (cpudata.scale_max<cpudata.max_freq))
  {
   diff=(config.max_temp-cpudata.cur_temp)/1000;
   newfreq=cpudata.scale_max+(config.freq_step*diff);
   if (newfreq>cpudata.max_freq) newfreq=cpudata.max_freq;
   DEBUG1_MAIN("Increase to %ld\n",newfreq);
   cpufreq_set_long_int("scaling_max_freq",newfreq,100);
   cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
  }

  if ((cpudata.cur_temp>config.max_temp) && (cpudata.scale_max>cpudata.min_freq))
  {
   diff=(cpudata.cur_temp-config.max_temp)/1000;
   newfreq=cpudata.scale_max-(config.freq_step*diff);
   if (newfreq<cpudata.min_freq) newfreq=cpudata.min_freq;
   DEBUG1_MAIN("Decrease to %ld\n",newfreq);
   cpufreq_set_long_int("scaling_max_freq",newfreq,100);
   cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
  }

  sleep(config.interval);
 }
}