From 229d5f899c9af643f0398404628e5e7207a68ee6 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Wed, 30 Oct 2024 23:29:05 +0800 Subject: [PATCH] CPU (NetBSD): add support --- CMakeLists.txt | 2 +- src/detection/cpu/cpu_bsd.c | 5 ---- src/detection/cpu/cpu_nbsd.c | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src/detection/cpu/cpu_nbsd.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f024d55a6..17568c9af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -682,7 +682,7 @@ elseif(NetBSD) src/detection/brightness/brightness_bsd.c src/detection/btrfs/btrfs_nosupport.c src/detection/chassis/chassis_nbsd.c - src/detection/cpu/cpu_bsd.c + src/detection/cpu/cpu_nbsd.c src/detection/cpucache/cpucache_nosupport.c src/detection/cpuusage/cpuusage_bsd.c src/detection/cursor/cursor_linux.c diff --git a/src/detection/cpu/cpu_bsd.c b/src/detection/cpu/cpu_bsd.c index 7bf464072..7a5b72144 100644 --- a/src/detection/cpu/cpu_bsd.c +++ b/src/detection/cpu/cpu_bsd.c @@ -25,13 +25,8 @@ static const char* detectThermalTemp(double* current) const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) { - #if __NetBSD__ - if (ffSysctlGetString("machdep.cpu_brand", &cpu->name) != NULL) - return "sysctlbyname(machdep.cpu_brand) failed"; - #else if (ffSysctlGetString("hw.model", &cpu->name) != NULL) return "sysctlbyname(hw.model) failed"; - #endif cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); cpu->coresLogical = cpu->coresPhysical; diff --git a/src/detection/cpu/cpu_nbsd.c b/src/detection/cpu/cpu_nbsd.c new file mode 100644 index 000000000..0a78ed146 --- /dev/null +++ b/src/detection/cpu/cpu_nbsd.c @@ -0,0 +1,57 @@ +#include "cpu.h" +#include "common/sysctl.h" +#include + +static const char* detectCpuTemp(double* current) +{ + int temp = ffSysctlGetInt("dev.cpu.0.temperature", -999999); + if (temp == -999999) + return "ffSysctlGetInt(\"dev.cpu.0.temperature\") failed"; + + // In tenth of degrees Kelvin + *current = (double) temp / 10 - 273.15; + return NULL; +} + +static const char* detectThermalTemp(double* current) +{ + int temp = ffSysctlGetInt("hw.acpi.thermal.tz0.temperature", -999999); + if (temp == -999999) + return "ffSysctlGetInt(\"hw.acpi.thermal.tz0.temperature\") failed"; + + // In tenth of degrees Kelvin + *current = (double) temp / 10 - 273.15; + return NULL; +} + +const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) +{ + if (ffSysctlGetString("machdep.cpu_brand", &cpu->name) != NULL) + { + if (ffSysctlGetString("machdep.dmi.processor-version", &cpu->name) != NULL) + return "sysctlbyname(machdep.cpu_brand) failed"; + } + + cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); + cpu->coresLogical = cpu->coresPhysical; + cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.ncpuonline", cpu->coresLogical); + + ffCPUDetectSpeedByCpuid(cpu); + + { + struct clockinfo info; + size_t length = sizeof(info); + if (sysctl((int[]) {CTL_KERN, KERN_CLOCKRATE}, 2, &info, &length, NULL, 0) == 0) + cpu->frequencyBase = (uint32_t) info.hz / 1000; + } + + cpu->temperature = FF_CPU_TEMP_UNSET; + + if (options->temp) + { + if (detectCpuTemp(&cpu->temperature) != NULL) + detectThermalTemp(&cpu->temperature); + } + + return NULL; +}