aboutsummaryrefslogtreecommitdiffstats
path: root/cputemp2maxfreq.c
blob: b023b79c0fb972c23d9dbfb497c31245ba1c0a52 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include "sysfs.h"
#include "cpufreq.h"
#include "debug.h"
#include "cputemp2maxfreq.h"
#include "failsafe.h"
#include "version.h"
#include "argparse.h"

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

void signal_handler(int signum)
{
 printf("Received signal %d, exiting.\n",signum);
 failsafe(0);
}

int main(int argc,char **argv)
{
 long int diff;
 long int newfreq;

 printf("Version %s, buildtime %s %s\n",version(),__DATE__,__TIME__);
 argparse(argc,argv);

// Print configuration
 printf("Configuration:\n");
 printf("Governor: %s\n",config.governor);
 printf("Temperature: %ld\n",config.max_temp);
 printf("Temp input: %s\n",config.temp_input);
 printf("Frequency step: %ld\n",config.freq_step);
 printf("Fallback frquency: %ld\n",config.fallback_freq);
 printf("Interval: %d\n",config.interval);
 printf("\n");

 if ((config.max_temp<VALID_TEMP_MIN) || (config.max_temp>VALID_TEMP_MAX))
 {
  printf("Invalid temperature, range is %d-%d\n",VALID_TEMP_MIN,VALID_TEMP_MAX);
  exit(1);
 }

 if ((config.freq_step<VALID_STEP_MIN) || (config.freq_step>VALID_STEP_MAX))
 {
  printf("Invalid frequency step, range is %d-%d\n",VALID_STEP_MIN,VALID_STEP_MAX);
  exit(1);
 }

 if ((config.interval<VALID_INTERVAL_MIN) || (config.interval>VALID_INTERVAL_MAX))
 {
  printf("Invalid poll interval, range is %d-%d\n",VALID_INTERVAL_MIN,VALID_INTERVAL_MAX);
  exit(1);

 }

// Get and validate CPU data
 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");

 printf("CPU data:\n");
 printf("Minimum frequency: %ld\n",cpudata.min_freq);
 printf("Maximum frequency: %ld\n",cpudata.max_freq);
 printf("Scaling maximum frequency: %ld\n",cpudata.scale_max);
 printf("\n");

 if ((cpudata.min_freq<VALID_FREQ_MIN) || (cpudata.min_freq>VALID_FREQ_MAX) ||
     (cpudata.max_freq<VALID_FREQ_MIN) || (cpudata.max_freq>VALID_FREQ_MAX) ||
     (cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
 {
// If we have to fail now, there's not much we can do because we have no data
  printf("Invalid CPU data, exiting.\n");
  exit(1);
 }

// Modify fallback frequency is needed
 if (config.fallback_freq<cpudata.min_freq)
 {
  printf("Fallback frequency is lower than CPU's minimum frequency, setting to %ld.\n",cpudata.min_freq);
  config.fallback_freq=cpudata.min_freq;
 }
 if (config.fallback_freq>cpudata.max_freq)
 {
  printf("Fallback frequency is higher than CPU's maximum frequency, setting to %ld.\n",cpudata.max_freq);
  config.fallback_freq=cpudata.max_freq;
 }

// Set the governor
 if (cpufreq_set_str("scaling_governor",config.governor,0)<0)
 {
  printf("Failed to set governor, error %d (%s).\n",errno,strerror(errno));

// We failed to set the governor, call the failsafe
  failsafe(1);
 }

// Set signal handlers
 signal(SIGTERM,signal_handler);
 signal(SIGINT,signal_handler);
 signal(SIGQUIT,signal_handler);

 while(1)
 {
// Get new measurements
  cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq");
  if ((cpudata.cur_freq<VALID_FREQ_MIN) || (cpudata.cur_freq>VALID_FREQ_MAX))
  {
   printf("Invalid current frequency reported by CPU, exiting.\n");
   failsafe(1);
  }

  cpudata.cur_temp=sysfs_read_long_int(config.temp_input);
  if ((cpudata.cur_temp<VALID_TEMP_MIN) || (cpudata.cur_temp>VALID_TEMP_MAX))
  {
   printf("Invalid current tempature reported by CPU, exiting.\n");
   failsafe(1);
  }

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

// Check if we should increase
  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);

// Set new value and validate
   if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
   {
    printf("Failed to set scaling_max_freq, exiting.\n");
    failsafe(1);
   }
   cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
   if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
   {
    printf("Invalid scale_max_freq reported, exiting,\n");
    failsafe(1);
   }
  }

// Check if we should decrease
  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);

// Set new value and validate
   if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
   {
    printf("Failed to set scaling_max_freq, exiting.\n");
    failsafe(1);
   }
   cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
   if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
   {
    printf("Invalid scale_max_freq reported, exiting,\n");
    failsafe(1);
   }
  }

  sleep(config.interval);
 }
}