-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfanController.c
More file actions
304 lines (269 loc) · 10.4 KB
/
Copy pathfanController.c
File metadata and controls
304 lines (269 loc) · 10.4 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* _ _ _ _ _ _ _
* | | _ _ _ __| | __ / \ _ __ __| | | ___ (_) |_ ___ _ __
* | | | | | | '__| |/ / / _ \ | '_ \ / _` | | / _ \| | __/ _ \ '__|
* | |__| |_| | | | < / ___ \| | | | (_| | |__| (_) | | || __/ |
* |_____\__,_|_| |_|\_\/_/ \_\_| |_|\__,_|_____\___/|_|\__\___|_|
* ____________________________________________________________________________
* ----------------------------------------------------------------------------
* Copyright 2025 LurkAndLoiter.
* ____________________________________________________________________________
* __ __ ___ _____ _ _
* | \/ |_ _|_ _| | | (_) ___ ___ _ __ ___ ___
* | |\/| || | | | | | | |/ __/ _ \ '_ \/ __|/ _ \
* | | | || | | | | |___| | (_| __/ | | \__ \ __/
* |_| |_|___| |_| |_____|_|\___\___|_| |_|___/\___|
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* ____________________________________________________________________________
* ----------------------------------------------------------------------------
* "Zetus Lupetus" "Omelette du fromage" "You're killing me smalls" "Ugh As If"
* "Hey. Listen!" "Do a barrel roll!" "Dear Darla, I hate your stinking guts."
* "If we listen to each other's hearts. We'll find we're never too far apart."
* ____________________________________________________________________________
*/
#include "nvml.h"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef DEBUG
#define DEBUG_PRINT(fmt, ...) \
fprintf(stderr, "DEBUG: %s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define DEBUG_PRINT(fmt, ...)
#endif
#define TEMP_THRESHOLD 2 // Degree celcius before action
#define MIN_TEMP 55 // Lowest value from TempTargets
#define MAX_TEMP 80 // Highest value from TempTargets
unsigned int FanSpeeds[MAX_TEMP - MIN_TEMP + 1];
static unsigned int deviceCount = 0;
static volatile int terminate = 0;
static pthread_t *threads = NULL;
typedef struct {
int id;
unsigned int prevFanSpeed;
unsigned int prevTemperature;
nvmlDevice_t handle;
unsigned int fanCount;
} Device;
void runTimeSanity(const unsigned int *TempTargets,
const unsigned int *FanTargets,
const unsigned int CountTargets) {
// Runtime sanity checks. These are here to protect you.
if (MIN_TEMP != TempTargets[0]) {
DEBUG_PRINT("ERROR: MIN_TEMP does not align with TempTargets.\n");
exit(EXIT_FAILURE);
}
if (MAX_TEMP != TempTargets[CountTargets - 1]) {
DEBUG_PRINT("ERROR: MAX_TEMP does not align with TempTargets.\n");
exit(EXIT_FAILURE);
}
if (TempTargets[CountTargets - 1] > 90) {
DEBUG_PRINT("ERROR: TempTargets maximum must not exceed 90\n");
exit(EXIT_FAILURE);
}
if (FanTargets[CountTargets - 1] > 100) {
DEBUG_PRINT("ERROR: FanTargets maximum must not exceed 100\n");
exit(EXIT_FAILURE);
}
for (unsigned int i = 0; i < CountTargets - 1; i++) {
if (FanTargets[i + 1] < FanTargets[i]) {
DEBUG_PRINT("ERROR: FanTargets must be ordered min to max\n");
exit(EXIT_FAILURE);
}
if (TempTargets[i + 1] < TempTargets[i]) {
DEBUG_PRINT("ERROR: TempTargets must be ordered min to max\n");
exit(EXIT_FAILURE);
}
}
}
void cleanup(const int signum) {
terminate = 1;
if (threads) {
for (unsigned int i = 0; i < deviceCount; i++) {
pthread_join(threads[i], NULL);
}
free(threads);
threads = NULL;
}
nvmlShutdown();
DEBUG_PRINT("Shutdown Complete\n");
exit(signum);
}
void signal_handler(const int signum) {
DEBUG_PRINT("Received signal %d, shutting down...\n", signum);
cleanup(signum);
}
static unsigned int fanspeedFromT(const unsigned int temperature,
const unsigned int *slopes,
const unsigned int *TempTargets,
const unsigned int *FanTargets,
const unsigned int CountTargets) {
if (CountTargets == 1)
return FanTargets[0];
if (temperature <= TempTargets[0])
return FanTargets[0];
if (temperature >= TempTargets[CountTargets - 1])
return FanTargets[CountTargets - 1];
int i;
for (i = 0; temperature > TempTargets[i]; i++) {
continue;
}
return FanTargets[i - 1] +
((temperature - TempTargets[i - 1]) * slopes[i - 1]) / 100;
}
void precalcFanSpeeds(void) {
const unsigned int TempTargets[] = {55, 80};
const unsigned int FanTargets[] = {40, 100};
const unsigned int CountTargets = sizeof(FanTargets) / sizeof(FanTargets[0]);
// Compile time sanity checks. These are here to protect you
_Static_assert(sizeof(TempTargets) / sizeof(TempTargets[0]) ==
sizeof(FanTargets) / sizeof(FanTargets[0]),
"TempTargets and FanTargets must have the same length");
runTimeSanity(TempTargets, FanTargets, CountTargets);
unsigned int slopes[CountTargets - 1];
for (unsigned int i = 0; i < CountTargets - 1; i++) {
slopes[i] = (FanTargets[i + 1] - FanTargets[i]) * 100 /
(TempTargets[i + 1] - TempTargets[i]);
}
for (int i = MIN_TEMP; i <= MAX_TEMP; i++) {
FanSpeeds[i - MIN_TEMP] =
fanspeedFromT(i, slopes, TempTargets, FanTargets, CountTargets);
}
}
unsigned int getFanSpeed(unsigned int temperature) {
if (temperature < MIN_TEMP)
temperature = MIN_TEMP;
if (temperature > MAX_TEMP)
temperature = MAX_TEMP;
return FanSpeeds[temperature - MIN_TEMP];
}
static void nvmlStart() {
nvmlReturn_t result;
result = nvmlInit_v2();
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to initialize NVML: %s\n", nvmlErrorString(result));
cleanup(EXIT_FAILURE);
}
result = nvmlDeviceGetCount_v2(&deviceCount);
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to get device count: %s\n", nvmlErrorString(result));
cleanup(EXIT_FAILURE);
} else if (deviceCount < 1) {
DEBUG_PRINT("Unsupported: No Nvidia Devices found.\n");
cleanup(EXIT_FAILURE);
}
}
void *deviceLoop(void *arg) {
Device *device = (Device *)arg;
nvmlReturn_t result;
unsigned int temperature;
const unsigned int polling_interval = 1000000;
result = nvmlDeviceGetHandleByIndex_v2(device->id, &device->handle);
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to get device %d handle: %s\n", device->id,
nvmlErrorString(result));
cleanup(EXIT_FAILURE);
}
result = nvmlDeviceGetNumFans(device->handle, &device->fanCount);
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to get fan count for device %d: %s\n", device->id,
nvmlErrorString(result));
cleanup(EXIT_FAILURE);
}
/* LOOP */
while (!terminate) {
result = nvmlDeviceGetTemperature(device->handle, NVML_TEMPERATURE_GPU,
&temperature);
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to get temperature for device %d: %s\n", device->id,
nvmlErrorString(result));
continue;
}
unsigned int temp_diff = device->prevTemperature > temperature
? device->prevTemperature - temperature
: temperature - device->prevTemperature;
if (temp_diff >= TEMP_THRESHOLD) {
unsigned int fanSpeed = getFanSpeed(temperature);
if (device->prevFanSpeed != fanSpeed) {
for (unsigned int i = 0; i < device->fanCount; i++) {
result = nvmlDeviceSetFanSpeed_v2(device->handle, i, fanSpeed);
if (result != NVML_SUCCESS) {
DEBUG_PRINT("Failed to set fan: %d to speed:%d for device:%d: %s\n",
i, fanSpeed, device->id, nvmlErrorString(result));
}
}
DEBUG_PRINT("Monitoring device: %d temp: %d->%d fans:%d@%d->%d\n",
device->id, device->prevTemperature, temperature,
device->fanCount, device->prevFanSpeed, fanSpeed);
device->prevTemperature = temperature;
device->prevFanSpeed = fanSpeed;
}
}
usleep((temp_diff > 5) ? polling_interval / 2 : polling_interval);
}
/* End LOOP */
/* Terminate signaled reset fan control to firmware */
for (unsigned int i = 0; i < device->fanCount; i++) {
result = nvmlDeviceSetDefaultFanSpeed_v2(device->handle, i);
if (result != NVML_SUCCESS) {
DEBUG_PRINT(
"Failed to set fan: %d to firmware default for device:%d: %s\n", i,
device->id, nvmlErrorString(result));
}
}
DEBUG_PRINT("Device %d thread terminated\n", device->id);
free(device);
return NULL;
}
void threadDevices() {
threads = malloc(sizeof(pthread_t) * deviceCount);
if (!threads) {
DEBUG_PRINT("Failed to allocate threads array\n");
cleanup(EXIT_FAILURE);
}
for (unsigned int i = 0; i < deviceCount; i++) {
Device *device = malloc(sizeof(Device));
if (!device) {
DEBUG_PRINT("Failed to allocate device index\n");
cleanup(EXIT_FAILURE);
}
device->id = i;
device->prevFanSpeed = 1; // 1 avoids gate overlap with 0 RPM
device->prevTemperature = 0;
if (pthread_create(&threads[i], NULL, deviceLoop, device) != 0) {
DEBUG_PRINT("Failed to create thread for device %d\n", i);
free(device);
cleanup(EXIT_FAILURE);
}
}
}
int main(void) {
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
precalcFanSpeeds();
nvmlStart();
threadDevices();
while (!terminate) {
pause();
}
cleanup(EXIT_SUCCESS);
return EXIT_SUCCESS;
}