From 255c6a09ac6a17c3b7789b0a8bc23dc890d744df Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 17 Jun 2024 15:32:39 +0800 Subject: [PATCH] CPU (SunOS): optimise ncpu detection --- src/detection/cpu/cpu_sunos.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/detection/cpu/cpu_sunos.c b/src/detection/cpu/cpu_sunos.c index 716dc68b9..1f3d772d9 100644 --- a/src/detection/cpu/cpu_sunos.c +++ b/src/detection/cpu/cpu_sunos.c @@ -1,9 +1,16 @@ #include "cpu.h" #include +static inline void kstatFreeWrap(kstat_ctl_t** pkc) +{ + assert(pkc); + if (*pkc) + kstat_close(*pkc); +} + const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPUResult* cpu) { - kstat_ctl_t* kc = kstat_open(); + __attribute__((__cleanup__(kstatFreeWrap))) kstat_ctl_t* kc = kstat_open(); if (!kc) return "kstat_open() failed"; @@ -11,9 +18,6 @@ const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPURe if (!ks) return "kstat_lookup() failed"; - if (ks->ks_type != KSTAT_TYPE_NAMED) - return "Invalid ks_type found"; - if (kstat_read(kc, ks, NULL) < 0) return "kstat_read() failed"; @@ -27,12 +31,15 @@ const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPURe } { kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz"); - cpu->frequencyBase = kn->value.i32 / 1000.; + cpu->frequencyBase = kn->value.ui32 / 1000.; } - cpu->coresOnline = 1; - while ((ks = kstat_lookup(kc, "cpu_info", ks->ks_instance + 1, NULL))) - cpu->coresOnline++; + 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"); + cpu->coresLogical = cpu->coresPhysical = cpu->coresOnline = (uint16_t) kn->value.ui32; + } return NULL; }