-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp.c
249 lines (219 loc) · 6.97 KB
/
temp.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* chairman/temp.c
*
* Temperature control for X9 based Supermicro boards.
* Using Bang–bang control with hysteresis and f=1/s
* Δx = 2*1K
* t=1s
* PT1 first order lag element.
*
* Relational approach g(x) = e^((x - 17.33793493) / 15) + 7.65
*
* Copyright (c) Fabian Druschke 2020
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the project's author nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_VALUES 24
// Fan specific data, assuming the default Supermicro fan
#define MAX_FANSPEED 12000
#define MAX_PWM_VAL 255
#define FAN_DIFF MAX_FANSPEED / MAX_PWM_VAL
#define INT_MIN 1 // Minimum hysteresis time in seconds.
int tachoControl; // Hex 0 - 255 PWM frequency in kHz.
int a[MAX_VALUES]; // Max amount of sensors we want to read in
int interval; // Our interval to repeat execution every x seconds
int debug;
void printfVals() {
for (float i = 0.0; i <= 100.0; i++) // From 0 to 100 degrees
{
float pwm_frequency = (exp((i - 17.33793493) / 15.0) + 7.65);
printf("Hunk for %0.f °C: %f = %f 1/60s\n", i, pwm_frequency,
(pwm_frequency * FAN_DIFF));
}
}
float hysteresisControl(int temp) {
// g(x) = e^((x - 17.33793493) / 15) + 7.65
float pwm_frequency;
return pwm_frequency = exp((temp - 17.33793493) / 15) + 7.65;
}
int interpolateFanSpeed(float pwm_frequency) {
return tachoControl = (int)(pwm_frequency < 0 ? (pwm_frequency - 0.5)
: (pwm_frequency + 0.5));
}
void swap(int *keys, int n, int m) {
int tmp = keys[n];
keys[n] = keys[m];
keys[m] = tmp;
}
void __heapsort(int *keys, int n_keys) {
keys -= 1;
for (int last = 1; last <= n_keys; ++last) {
int n = last;
while (n > 1) {
int parent = n / 2;
if (keys[parent] > keys[n])
break;
swap(keys, parent, n);
n = parent;
}
}
for (int last = n_keys - 1; last >= 1; --last) {
swap(keys, 1, last + 1);
int n = 1;
while (1) {
int max = n;
int left = n * 2;
int right = left + 1;
if (left <= last && keys[left] > keys[max])
max = left;
if (right <= last && keys[right] > keys[max])
max = right;
if (n == max)
break;
swap(keys, max, n);
n = max;
}
}
}
void setFanSpeed()
{
// When there is no interval specified.
FILE *fp; // Our file handler
char path[1035];
// Open the command for reading.
fp = popen("sysctl -a | grep temperature", "r");
if (fp == NULL) {
printf("Failed to run command\n");
exit(1);
}
// Read the output a line at a time and fill into array.
int i = 0;
while (fgets(path, sizeof(path), fp) != NULL) {
char *discardChar;
discardChar = (char *)calloc(sizeof(path), sizeof(char));
char discard[sizeof(path)];
sscanf(path, "%s %i", &discard[i], &a[i]);
free(discardChar);
i++;
}
int n_a = sizeof(a) / sizeof(int);
__heapsort(a, n_a);
for (int k = sizeof(a); k > 0; k--) {
if (a[k] > 0) {
int max = a[k];
int targetFanSpeed = interpolateFanSpeed(hysteresisControl(max));
unsigned char buffer[32];
sprintf(buffer, "ipmitool raw 0x30 0x91 0x5A 0x3 0x10 0x%x",
targetFanSpeed); // Issued command
system(buffer);
sprintf(buffer, "ipmitool raw 0x30 0x91 0x5A 0x3 0x11 0x%x",
targetFanSpeed); // Issued command
system(buffer);
if (debug == 1) {
printf("Highest temp: %d\n", a[k]);
float realFanSpeed = (targetFanSpeed * FAN_DIFF);
printf("Target fan speed: 0x%x = %f 1/60s\n\n", targetFanSpeed,
realFanSpeed);
}
break;
}
}
// Close file handler
pclose(fp);
}
int main(int argc, char *argv[]) {
debug = 0;
if (argc == 1) {
printf("Fanspeed set.\n");
setFanSpeed();
return 0;
}
if (argc == 2) {
if (strcmp(argv[1], "--help") == 0) {
printf("Execute: %s ... [INTERVAL] ... [OPTION] \n\n", argv[0]);
printf(" --table, prints scaled fan speed information");
printf("Temperature control for X9 based Supermicro boards.\n");
printf("Using Bang�^`^sbang control with hysteresis and f=1/s\n");
printf("�^tx = 2*1K\n");
printf("t=1s\n");
printf("PT1 first order lag element.\n");
printf("\n");
printf("Relational approach g(x) = e^((x - 17.33793493) / 15) + 7.65\n");
printf("\n");
printf("Copyright (c) Fabian Druschke 2020\n");
printf("All rights reserved.\n");
return 0;
}
if (strcmp(argv[1], "--table") == 0) {
printfVals();
return 0;
}
if (strcmp(argv[1], "--debug") == 0) {
printf("Fanspeed set.\n");
debug = 1;
setFanSpeed();
return 0;
}
}
if ((argc == 2) || (argc == 3 && strcmp(argv[2], "--debug") == 0)) {
// Check if a valid interval has been entered
if (strcmp(argv[2], "--debug") == 0) {
debug = 1;
}
char *p;
errno = 0;
long conv = strtol(argv[1], &p, 10);
// Check for errors: e.g., the string does not represent an integer
// or the integer is less than int
if (errno != 0 || *p != '\0' || conv < INT_MIN) {
// Put here the handling of the error, like exiting the program with
// an error message
printf("Invalid input specified.");
} else {
// No error
interval = conv;
printf("Hysteresis: %d seconds. This program will continue until being "
"interrupted.\n",
interval);
// Block
LOOP:
setFanSpeed();
sleep(1);
goto LOOP;
}
}
return 0;
}