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
|
#include "gpu.h"
#include "common/io.h"
#include "common/strutil.h"
#include <fcntl.h>
static double parseTZDir(int dfd, FFstrbuf* buffer) {
if (!ffReadFileBufferRelative(dfd, "type", buffer) || !ffStrbufStartsWithS(buffer, "gpu")) {
return FF_GPU_TEMP_UNSET;
}
if (!ffReadFileBufferRelative(dfd, "temp", buffer)) {
return FF_GPU_TEMP_UNSET;
}
double value = ffStrbufToDouble(buffer, FF_GPU_TEMP_UNSET); // millidegree Celsius
if (value == FF_GPU_TEMP_UNSET) {
return FF_GPU_TEMP_UNSET;
}
return value / 1000.;
}
double ffGPUDetectTempFromTZ(void) {
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/thermal/");
if (dirp) {
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
int dfd = dirfd(dirp);
struct dirent* entry;
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_name[0] == '.') {
continue;
}
if (!ffStrStartsWith(entry->d_name, "thermal_zone")) {
continue;
}
FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (subfd < 0) {
continue;
}
double result = parseTZDir(subfd, &buffer);
if (result != FF_GPU_TEMP_UNSET) {
return result;
}
}
}
return FF_GPU_TEMP_UNSET;
}
const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) {
FF_UNUSED(options, gpus);
return "No permission. Fallbacks to Vulkan, OpenCL or OpenGL instead";
}
|