summaryrefslogtreecommitdiffstats
path: root/src/detection/cpu/cpu_obsd.c
blob: e5b8d2fdf10cd486be0d1fbdd16002f2389b60ec (plain) (blame)
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
#include "cpu.h"
#include "common/sysctl.h"
#include "common/strutil.h"

#include <errno.h>
#include <sys/time.h>
#include <sys/sensors.h>

static const char* detectCPUTemp(const FFCPUOptions* options, FFCPUResult* cpu) {
    int mib[5] = { CTL_HW, HW_SENSORS, 0, SENSOR_TEMP, 0 };

    for (mib[2] = 0; mib[2] < 1024; mib[2]++) {
        struct sensordev sensordev;
        size_t sdlen = sizeof(struct sensordev);
        if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) < 0) {
            if (errno == ENOENT) {
                break;
            }
            if (errno == ENXIO) {
                continue;
            }
            return "sysctl(sensordev) failed";
        }

        if (options->tempSensor.length > 0) {
            if (!ffStrbufEqualS(&options->tempSensor, sensordev.xname)) {
                continue;
            }
        } else {
            if (!ffStrStartsWith(sensordev.xname, "cpu")) {
                continue;
            }
        }

        for (mib[4] = 0; mib[4] < sensordev.maxnumt[SENSOR_TEMP]; mib[4]++) {
            struct sensor sensor;
            size_t slen = sizeof(struct sensor);
            if (sysctl(mib, 5, &sensor, &slen, NULL, 0) < 0) {
                if (errno != ENOENT) {
                    return "sysctl(sensor) failed";
                }
                continue;
            }
            if (sensor.flags & SENSOR_FINVALID) {
                continue;
            }

            cpu->temperature = (double) (sensor.value - 273150000) / 1E6;
            return NULL;
        }
    }

    return "No sensor for CPU temp found";
}

const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) {
    if (ffSysctlGetString(CTL_HW, HW_MODEL, &cpu->name)) {
        return "sysctl(hw.model) failed";
    }

    cpu->coresPhysical = (uint16_t) ffSysctlGetInt(CTL_HW, HW_NCPU, 1);
    cpu->coresLogical = cpu->coresPhysical;
    cpu->coresOnline = (uint16_t) ffSysctlGetInt(CTL_HW, HW_NCPUONLINE, cpu->coresLogical);

    ffCPUDetectByCpuid(cpu);

    uint32_t cpuspeed = (uint32_t) ffSysctlGetInt(CTL_HW, HW_CPUSPEED, 0);
    if (cpuspeed > cpu->frequencyBase) {
        cpu->frequencyBase = cpuspeed;
    }

    cpu->temperature = FF_CPU_TEMP_UNSET;
    if (options->temp) {
        detectCPUTemp(options, cpu);
    }

    return NULL;
}