|
2 | 2 | #include "common/io/io.h" |
3 | 3 | #include "common/processing.h" |
4 | 4 | #include "common/properties.h" |
5 | | -#include "detection/temps/temps_linux.h" |
6 | 5 | #include "util/mallocHelper.h" |
7 | 6 | #include "util/stringUtils.h" |
8 | 7 |
|
9 | 8 | #include <sys/sysinfo.h> |
10 | 9 | #include <stdlib.h> |
11 | 10 | #include <unistd.h> |
| 11 | +#include <dirent.h> |
| 12 | + |
| 13 | +static double parseHwmonDir(FFstrbuf* dir, FFstrbuf* buffer) |
| 14 | +{ |
| 15 | + //https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 16 | + uint32_t dirLength = dir->length; |
| 17 | + ffStrbufAppendS(dir, "temp1_input"); |
| 18 | + |
| 19 | + if(!ffReadFileBuffer(dir->chars, buffer)) |
| 20 | + { |
| 21 | + // Some badly implemented system put temp file in /hwmonN/device |
| 22 | + ffStrbufSubstrBefore(dir, dirLength); |
| 23 | + ffStrbufAppendS(dir, "device/"); |
| 24 | + dirLength = dir->length; |
| 25 | + ffStrbufAppendS(dir, "temp1_input"); |
| 26 | + |
| 27 | + if(!ffReadFileBuffer(dir->chars, buffer)) |
| 28 | + return 0.0/0.0; |
| 29 | + } |
| 30 | + |
| 31 | + ffStrbufSubstrBefore(dir, dirLength); |
| 32 | + |
| 33 | + double value = ffStrbufToDouble(buffer);// millidegree Celsius |
| 34 | + |
| 35 | + if(value != value) |
| 36 | + return 0.0/0.0; |
| 37 | + |
| 38 | + ffStrbufAppendS(dir, "name"); |
| 39 | + if (!ffReadFileBuffer(dir->chars, buffer)) |
| 40 | + return 0.0/0.0; |
| 41 | + |
| 42 | + ffStrbufTrimRightSpace(buffer); |
| 43 | + |
| 44 | + if( |
| 45 | + ffStrbufContainS(buffer, "cpu") || |
| 46 | + ffStrbufEqualS(buffer, "k10temp") || // AMD |
| 47 | + ffStrbufEqualS(buffer, "coretemp") // Intel |
| 48 | + ) return value / 1000.; |
| 49 | + |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +static double detectCPUTemp(void) |
| 54 | +{ |
| 55 | + FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateA(64); |
| 56 | + ffStrbufAppendS(&baseDir, "/sys/class/hwmon/"); |
| 57 | + |
| 58 | + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); |
| 59 | + |
| 60 | + uint32_t baseDirLength = baseDir.length; |
| 61 | + |
| 62 | + FF_AUTO_CLOSE_DIR DIR* dirp = opendir(baseDir.chars); |
| 63 | + if(dirp == NULL) |
| 64 | + return 0.0/0.0; |
| 65 | + |
| 66 | + struct dirent* entry; |
| 67 | + while((entry = readdir(dirp)) != NULL) |
| 68 | + { |
| 69 | + if(entry->d_name[0] == '.') |
| 70 | + continue; |
| 71 | + |
| 72 | + ffStrbufAppendS(&baseDir, entry->d_name); |
| 73 | + ffStrbufAppendC(&baseDir, '/'); |
| 74 | + |
| 75 | + double result = parseHwmonDir(&baseDir, &buffer); |
| 76 | + if (result == result) |
| 77 | + return result; |
| 78 | + |
| 79 | + ffStrbufSubstrBefore(&baseDir, baseDirLength); |
| 80 | + } |
| 81 | + |
| 82 | + return 0.0/0.0; |
| 83 | +} |
12 | 84 |
|
13 | 85 | #ifdef __ANDROID__ |
14 | 86 | #include "common/settings.h" |
@@ -358,7 +430,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) |
358 | 430 | if(cpuinfo == NULL) |
359 | 431 | return "fopen(\"/proc/cpuinfo\", \"r\") failed"; |
360 | 432 |
|
361 | | - cpu->temperature = options->temp ? ffDetectCPUTemp() : FF_CPU_TEMP_UNSET; |
| 433 | + cpu->temperature = options->temp ? detectCPUTemp() : FF_CPU_TEMP_UNSET; |
362 | 434 |
|
363 | 435 | FF_STRBUF_AUTO_DESTROY physicalCoresBuffer = ffStrbufCreate(); |
364 | 436 | FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate(); |
|
0 commit comments