-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSensors.c
386 lines (305 loc) · 14.6 KB
/
getSensors.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* File: getSensors.c
* Project: arduino-resource-monitor
* Created Date: 2024-05-18 13:48:34
* Author: 3urobeat
*
* Last Modified: 2024-10-02 22:27:34
* Modified By: 3urobeat
*
* Copyright (c) 2024 3urobeat <https://github.com/3urobeat>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "sensors.h"
#include <dirent.h>
#include <errno.h>
// Stores filesystem paths for all sensors we've found
struct SensorTypes sensorPaths;
/**
* Checks if a directory exists. Outputs error to stdout if directory could not be opened
*/
bool _fileExists(const char *path)
{
errno = 0;
FILE* sensorFileP = fopen(path, "r");
if (sensorFileP)
{
(void) fclose(sensorFileP);
return true;
}
else
{
printf("\033[91mError:\033[0m Failed to open sensor '%s'! Ignoring it. Error: %s\n", path, strerror(errno));
return false;
}
}
// Persistent data for _processSensorName()
bool cpuTempAutoDiscovered = false; // Used for printing warning when multiple CPU/GPUs were auto-discovered
bool gpuLoadAutoDiscovered = false;
bool gpuTempAutoDiscovered = false;
/**
* Checks for known sensor names and populates sensorPaths
*/
void _processSensorName(const char *sensorPath, const char *sensorName)
{
// Check if path is too long to store and ignore it
if (strlen(sensorPath) > pathSize - 16)
{
logDebug("\033[33mWarn:\033[0m Path of sensor name '%s (%s)' is too long (%i chars) to store! Ignoring it...\n", sensorName, sensorPath, strlen(sensorPath));
}
// HwMon CPU Temp: Check if sensor matches a known name
if (strStartsWith("k10temp", sensorName) // AMD
|| strStartsWith("coretemp", sensorName) // Intel
|| strStartsWith("cpu_thermal", sensorName)) // Raspberry Pi
{
if (strlen(sensorPaths.cpuTemp) == 0) // Check if user already configured this sensor
{
strcpy(sensorPaths.cpuTemp, sensorPath);
strcat(sensorPaths.cpuTemp, "/temp1_input");
// Attempt to open to check if it exists. Log success message or reset sensor path on failure
bool sensorPathExists = _fileExists(sensorPaths.cpuTemp);
if (sensorPathExists)
{
printf("\033[92mFound CPU Temperature sensor '%s' at '%s'!\033[0m\n", sensorPaths.cpuTemp, sensorName);
cpuTempAutoDiscovered = true;
}
else strcpy(sensorPaths.cpuTemp, "");
}
else
{
if (cpuTempAutoDiscovered) printf("\033[33mWarn:\033[0m Your system has multiple CPU hwmon's! If you have multiple CPUs and the wrong chip's temperature sensor has been chosen, please configure it manually.\n");
}
}
// ThermalZone CPU Temp: Check if sensor matches a known name
if (strStartsWith("CPU-therm", sensorName) // Nvidia Jetson Nano
|| strStartsWith("soc_thermal_0", sensorName)) // Milk-V Duo
{
if (strlen(sensorPaths.cpuTemp) == 0) // Check if user already configured this sensor
{
strcpy(sensorPaths.cpuTemp, sensorPath);
strcat(sensorPaths.cpuTemp, "/temp");
// Attempt to open to check if it exists. Log success message or reset sensor path on failure
bool sensorPathExists = _fileExists(sensorPaths.cpuTemp);
if (sensorPathExists)
{
printf("\033[92mFound CPU Temperature sensor '%s' at '%s'!\033[0m\n", sensorPaths.cpuTemp, sensorName);
cpuTempAutoDiscovered = true;
}
else strcpy(sensorPaths.cpuTemp, "");
}
else
{
if (cpuTempAutoDiscovered) printf("\033[33mWarn:\033[0m Your system has multiple CPU hwmon's! If you have multiple CPUs and the wrong chip's temperature sensor has been chosen, please configure it manually.\n");
}
}
// HwMon GPU Load & Temp: Check if sensor matches a known name
if (strStartsWith("amdgpu", sensorName) && config.gpuType == AMD) // AMD // TODO: I don't know how nvidia sensors are called
{
if (strlen(sensorPaths.gpuLoad) == 0) // Check if user already configured this sensor
{
strcpy(sensorPaths.gpuLoad, sensorPath);
strcat(sensorPaths.gpuLoad, "/device/gpu_busy_percent"); // TODO: Does this exist for NVIDIA cards?
// Attempt to open to check if it exists. Log success message or reset sensor path on failure
bool sensorPathExists = _fileExists(sensorPaths.gpuLoad);
if (sensorPathExists)
{
printf("\033[92mFound GPU Load sensor '%s' at '%s'!\033[0m\n", sensorPaths.gpuLoad, sensorName);
gpuLoadAutoDiscovered = true;
} else strcpy(sensorPaths.gpuLoad, "");
}
else
{
if (gpuLoadAutoDiscovered) printf("\033[33mWarn:\033[0m Your system has multiple GPU hwmon's! If you have multiple GPUs and the wrong card's load sensor has been chosen, please configure it manually.\n");
}
if (strlen(sensorPaths.gpuTemp) == 0) // Check if user already configured this sensor
{
strcpy(sensorPaths.gpuTemp, sensorPath);
strcat(sensorPaths.gpuTemp, "/temp1_input");
// Attempt to open to check if it exists. Log success message or reset sensor path on failure
bool sensorPathExists = _fileExists(sensorPaths.gpuTemp);
if (sensorPathExists)
{
printf("\033[92mFound GPU Temperature sensor '%s' at '%s'!\033[0m\n", sensorPaths.gpuTemp, sensorName);
gpuTempAutoDiscovered = true;
} else strcpy(sensorPaths.gpuTemp, "");
}
else
{
if (gpuTempAutoDiscovered) printf("\033[33mWarn:\033[0m Your system has multiple GPU hwmon's! If you have multiple GPUs and the wrong card's load sensor has been chosen, please configure it manually.\n");
}
}
// ThermalZone GPU Temp: Check if sensor matches a known name
if (strStartsWith("GPU-therm", sensorName)) // Nvidia Jetson Nano
{
if (strlen(sensorPaths.gpuTemp) == 0) // Check if user already configured this sensor
{
strcpy(sensorPaths.gpuTemp, sensorPath);
strcat(sensorPaths.gpuTemp, "/temp");
// Attempt to open to check if it exists. Log success message or reset sensor path on failure
bool sensorPathExists = _fileExists(sensorPaths.gpuTemp);
if (sensorPathExists)
{
printf("\033[92mFound GPU Temperature sensor '%s' at '%s'!\033[0m\n", sensorPaths.gpuTemp, sensorName);
gpuTempAutoDiscovered = true;
} else strcpy(sensorPaths.gpuTemp, "");
}
else
{
if (gpuTempAutoDiscovered) printf("\033[33mWarn:\033[0m Your system has multiple GPU hwmon's! If you have multiple GPUs and the wrong card's load sensor has been chosen, please configure it manually.\n");
}
}
}
/**
* Attempts to find relevant sensors in '/sys/class/hwmon'
*/
void _findHwmonSensors()
{
const char *hwmonDirStr = "/sys/class/hwmon/";
// Get all dirs in '/sys/class/hwmon/'
DIR *hwmonDirP;
struct dirent *ep;
hwmonDirP = opendir(hwmonDirStr);
if (hwmonDirP == NULL) printf("\033[91mError:\033[0m Failed to open '/sys/class/hwmon/' to probe all sensors!\n");
// Collect all valid 'hwmon*' directories and check their 'name' files
FILE *hwmonNameFileP = NULL;
char pathBuffer[128] = "";
while ((ep = readdir(hwmonDirP)) != NULL)
{
if (strncmp(ep->d_name, "hwmon", 5) == 0)
{
// Construct path
strcpy(pathBuffer, hwmonDirStr); // Reset content to base string
strncat(pathBuffer, ep->d_name, 16); // Append directory name, limit to 16 chars
strcat(pathBuffer, "/name\0"); // Append '/name', the file we are interested in
logDebug("Constructed hwmon name path '%s'. Attempting to open...", pathBuffer);
// Attempt to open path and read its content
errno = 0;
hwmonNameFileP = fopen(pathBuffer, "r");
char *contentBuffer = NULL; // TODO: Is this cool? I'd rather use reusable buffer with a fixed size (I think)
size_t contentLen = 0;
ssize_t bytes_read = -1;
if (hwmonNameFileP)
{
logDebug("File opened, attempting to read...");
// Read content into contentBuffer, close file again when done
bytes_read = getdelim(&contentBuffer, &contentLen, '\0', hwmonNameFileP); // https://stackoverflow.com/a/174743
fclose(hwmonNameFileP);
}
else
{
logDebug("Failed to open/read file! Error: %s", strerror(errno));
}
// If something was read, check if sensor has an interesting name
if (bytes_read != -1)
{
// Move null terminator in contentBuffer by 1 byte if the last char is a newline (this was always the case in my testing)
if (contentBuffer[bytes_read - 1] == '\n') contentBuffer[bytes_read - 1] = '\0';
// Move null terminator in pathBuffer to before '/name' to cut it off and pass _processSensorName() the "raw" path
pathBuffer[strlen(pathBuffer) - 5] = '\0';
// Let _processSensorName() take a look at it
logDebug("Found sensor at '%s' with name '%s'! Checking if we care about it...", pathBuffer, contentBuffer);
_processSensorName(pathBuffer, contentBuffer);
}
else
{
logDebug("Error: Sensor '%s' name has no content!", ep->d_name);
}
free(contentBuffer);
}
}
(void) closedir(hwmonDirP);
}
/**
* Attempts to find relevant sensors in '/sys/devices/virtual/thermal/'
*/
void _findThermalZoneSensors()
{
const char *thermalDirStr = "/sys/devices/virtual/thermal/";
// Get all dirs in '/sys/devices/virtual/thermal/'
DIR *thermalDirP;
struct dirent *ep;
thermalDirP = opendir(thermalDirStr);
if (thermalDirP == NULL) printf("\033[91mError:\033[0m Failed to open '/sys/devices/virtual/thermal/' to probe all sensors!\n");
// Collect all valid 'thermal_zone*' directories and check their 'name' files
FILE *thermalNameFileP = NULL;
char pathBuffer[128] = "";
while ((ep = readdir(thermalDirP)) != NULL)
{
if (strncmp(ep->d_name, "thermal_zone", 12) == 0)
{
// Construct path
strcpy(pathBuffer, thermalDirStr); // Reset content to base string
strncat(pathBuffer, ep->d_name, 16); // Append directory name, limit to 16 chars
strcat(pathBuffer, "/type\0"); // Append '/type', the file we are interested in
logDebug("Constructed thermal_zone type path '%s'. Attempting to open...", pathBuffer);
// Attempt to open path and read its content
errno = 0;
thermalNameFileP = fopen(pathBuffer, "r");
char *contentBuffer = NULL; // TODO: Is this cool? I'd rather use reusable buffer with a fixed size (I think)
size_t contentLen = 0;
ssize_t bytes_read = -1;
if (thermalNameFileP)
{
logDebug("File opened, attempting to read...");
// Read content into contentBuffer, close file again when done
bytes_read = getdelim(&contentBuffer, &contentLen, '\0', thermalNameFileP); // https://stackoverflow.com/a/174743
fclose(thermalNameFileP);
}
else
{
logDebug("Failed to open/read file! Error: %s", strerror(errno));
}
// If something was read, check if sensor has an interesting name
if (bytes_read != -1)
{
// Move null terminator in contentBuffer by 1 byte if the last char is a newline (this was always the case in my testing)
if (contentBuffer[bytes_read - 1] == '\n') contentBuffer[bytes_read - 1] = '\0';
// Move null terminator in pathBuffer to before '/name' to cut it off and pass _processSensorName() the "raw" path
pathBuffer[strlen(pathBuffer) - 5] = '\0';
// Let _processSensorName() take a look at it
logDebug("Found sensor at '%s' with name '%s'! Checking if we care about it...", pathBuffer, contentBuffer);
_processSensorName(pathBuffer, contentBuffer);
}
else
{
logDebug("Error: Sensor '%s' name has no content!", ep->d_name);
}
free(contentBuffer);
}
}
(void) closedir(thermalDirP);
}
/**
* Attempts to discover sensor paths and populates variables in sensorPaths
*/
void getSensors()
{
printf("Attempting to discover hardware sensors...\n");
// Find CPU & GPU sensors
_findHwmonSensors();
// Couldn't find all sensors in hwmon? Check thermal_zone next, some ARM devices (like the Nvidia Jetson Nano) use that instead
if (strlen(sensorPaths.cpuTemp) == 0 || strlen(sensorPaths.gpuLoad) == 0 || strlen(sensorPaths.gpuTemp) == 0)
{
logDebug("Didn't find all sensors in hwmon directory, searching thermal_zones next...");
_findThermalZoneSensors();
}
// Log warnings for missing sensors
if (strlen(sensorPaths.cpuTemp) == 0)
{
printf("\033[33mWarn:\033[0m I could not automatically find any 'CPU Temperature' sensor! If you have one, please configure it manually.\n");
strcpy(measurements.cpuTemp, "");
}
if (strlen(sensorPaths.gpuLoad) == 0 && config.gpuType == AMD)
{
printf("\033[33mWarn:\033[0m I could not automatically find any 'GPU Load' sensor! If you have one, please configure it manually.\n");
strcpy(measurements.gpuLoad, "");
}
if (strlen(sensorPaths.gpuTemp) == 0 && config.gpuType == AMD)
{
printf("\033[33mWarn:\033[0m I could not automatically find any 'GPU Temperature' sensor! If you have one, please configure it manually.\n");
strcpy(measurements.gpuTemp, "");
}
}