summaryrefslogtreecommitdiffstats
path: root/src/detection/cpu/cpu_sunos.c
blob: 9b1cab503ddd26384c867a5c7c7bd4cae7fb5e55 (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
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
#include "cpu.h"
#include "common/processing.h"
#include "common/strutil.h"
#include <kstat.h>

static const char* detectCPUTempByKstat(const FFCPUOptions* options, kstat_ctl_t* kc, FFCPUResult* cpu) {
    const char* possibleModules[] = { "temperature", "cpu_temp", "acpi_thermal", NULL };

    if (options->tempSensor.length > 0) {
        possibleModules[0] = options->tempSensor.chars;
        possibleModules[1] = NULL;
    }

    for (int i = 0; possibleModules[i] != NULL; i++) {
        kstat_t* ks = kstat_lookup(kc, possibleModules[i], -1, NULL);
        if (ks && kstat_read(kc, ks, NULL) >= 0) {
            kstat_named_t* kn = kstat_data_lookup(ks, "temperature");
            if (kn) {
                switch (kn->data_type) {
                    case KSTAT_DATA_INT32:
                        cpu->temperature = (float) kn->value.i32;
                        return NULL;
                    case KSTAT_DATA_UINT32:
                        cpu->temperature = (float) kn->value.ui32;
                        return NULL;
                    case KSTAT_DATA_FLOAT:
                        cpu->temperature = kn->value.f;
                        return NULL;
                }
            }
        }
    }

    return "Failed to find CPU temperature using kstat";
}

static const char* detectCPUTempByIpmiTool(FFCPUResult* cpu) {
    FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
    const char* error = ffProcessAppendStdOut(&buffer, (char* const[]) { "ipmitool", "-c", "sdr", "list", NULL });

    if (error) {
        return error;
    }

    char* line = NULL;
    size_t len = 0;
    while (ffStrbufGetline(&line, &len, &buffer)) {
        if (sscanf(line, "CPU%*d Temp,%lf,degrees C,ok", &cpu->temperature) == 1) {
            return NULL;
        }
    }

    return "ipmitool sdr list failed to find CPU temperature";
}

static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
    assert(pkc);
    if (*pkc) {
        kstat_close(*pkc);
    }
}

static inline uint16_t countTypeId(kstat_ctl_t* kc, const char* type) {
    uint64_t low = 0, high = 0;
    for (kstat_t* ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
        if (ffStrStartsWith(ksp->ks_module, "cpu_info")) {
            if (kstat_read(kc, ksp, NULL) < 0) {
                continue;
            }

            kstat_named_t* stat = kstat_data_lookup(ksp, type);
            if (!stat) {
                continue;
            }

            uint32_t id = 0;
            switch (stat->data_type) {
#ifdef _INT64_TYPE
                case KSTAT_DATA_INT64:
                case KSTAT_DATA_UINT64:
                    id = (uint32_t) stat->value.ui64;
                    break;
#endif
                case KSTAT_DATA_INT32:
                case KSTAT_DATA_UINT32:
                    id = stat->value.ui32;
                    break;
                default:
                    continue;
            }
            if (__builtin_expect(id > 64, false)) {
                high |= 1ULL << (id - 64);
            } else {
                low |= 1ULL << id;
            }
        }
    }
    return (uint16_t) (__builtin_popcountll(low) + __builtin_popcountll(high));
}

const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) {
    FF_A_CLEANUP(kstatFreeWrap) kstat_ctl_t* kc = kstat_open();
    if (!kc) {
        return "kstat_open() failed";
    }

    kstat_t* ks = kstat_lookup(kc, "cpu_info", -1, NULL);
    if (!ks) {
        return "kstat_lookup() failed";
    }

    if (kstat_read(kc, ks, NULL) < 0) {
        return "kstat_read() failed";
    }

    {
        kstat_named_t* kn = kstat_data_lookup(ks, "brand");
        if (kn) {
            ffStrbufSetNS(&cpu->name, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
        }
    }
    {
        kstat_named_t* kn = kstat_data_lookup(ks, "vendor_id");
        if (kn) {
            ffStrbufSetNS(&cpu->vendor, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn));
        }
    }
    ffCPUDetectByCpuid(cpu);
    {
        kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz");
        if (kn && kn->value.ui32 > cpu->frequencyBase) {
            cpu->frequencyBase = kn->value.ui32;
        }
    }

    ks = kstat_lookup(kc, "unix", -1, "system_misc");
    if (ks && kstat_read(kc, ks, NULL) >= 0) {
        kstat_named_t* kn = kstat_data_lookup(ks, "ncpus");
        if (kn) {
            cpu->coresLogical = cpu->coresOnline = (uint16_t) kn->value.ui32;
        }
    }

    cpu->packages = countTypeId(kc, "chip_id");
    cpu->coresPhysical = countTypeId(kc, "core_id");

    if (options->temp) {
        if (detectCPUTempByKstat(options, kc, cpu) != NULL) {
            detectCPUTempByIpmiTool(cpu);
        }
    }

    return NULL;
}