CPU (SunOS): optimise ncpu detection

This commit is contained in:
Carter Li
2024-06-17 15:32:39 +08:00
parent 74a364e3f2
commit 255c6a09ac
+15 -8
View File
@@ -1,9 +1,16 @@
#include "cpu.h"
#include <kstat.h>
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;
}