|
| 1 | +#include "cpu.h" |
| 2 | +#include "common/sysctl.h" |
| 3 | +#include <sys/sysctl.h> |
| 4 | + |
| 5 | +static const char* detectCpuTemp(double* current) |
| 6 | +{ |
| 7 | + int temp = ffSysctlGetInt("dev.cpu.0.temperature", -999999); |
| 8 | + if (temp == -999999) |
| 9 | + return "ffSysctlGetInt(\"dev.cpu.0.temperature\") failed"; |
| 10 | + |
| 11 | + // In tenth of degrees Kelvin |
| 12 | + *current = (double) temp / 10 - 273.15; |
| 13 | + return NULL; |
| 14 | +} |
| 15 | + |
| 16 | +static const char* detectThermalTemp(double* current) |
| 17 | +{ |
| 18 | + int temp = ffSysctlGetInt("hw.acpi.thermal.tz0.temperature", -999999); |
| 19 | + if (temp == -999999) |
| 20 | + return "ffSysctlGetInt(\"hw.acpi.thermal.tz0.temperature\") failed"; |
| 21 | + |
| 22 | + // In tenth of degrees Kelvin |
| 23 | + *current = (double) temp / 10 - 273.15; |
| 24 | + return NULL; |
| 25 | +} |
| 26 | + |
| 27 | +const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) |
| 28 | +{ |
| 29 | + if (ffSysctlGetString("machdep.cpu_brand", &cpu->name) != NULL) |
| 30 | + { |
| 31 | + if (ffSysctlGetString("machdep.dmi.processor-version", &cpu->name) != NULL) |
| 32 | + return "sysctlbyname(machdep.cpu_brand) failed"; |
| 33 | + } |
| 34 | + |
| 35 | + cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); |
| 36 | + cpu->coresLogical = cpu->coresPhysical; |
| 37 | + cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.ncpuonline", cpu->coresLogical); |
| 38 | + |
| 39 | + ffCPUDetectSpeedByCpuid(cpu); |
| 40 | + |
| 41 | + { |
| 42 | + struct clockinfo info; |
| 43 | + size_t length = sizeof(info); |
| 44 | + if (sysctl((int[]) {CTL_KERN, KERN_CLOCKRATE}, 2, &info, &length, NULL, 0) == 0) |
| 45 | + cpu->frequencyBase = (uint32_t) info.hz / 1000; |
| 46 | + } |
| 47 | + |
| 48 | + cpu->temperature = FF_CPU_TEMP_UNSET; |
| 49 | + |
| 50 | + if (options->temp) |
| 51 | + { |
| 52 | + if (detectCpuTemp(&cpu->temperature) != NULL) |
| 53 | + detectThermalTemp(&cpu->temperature); |
| 54 | + } |
| 55 | + |
| 56 | + return NULL; |
| 57 | +} |
0 commit comments