diff --git a/src/detection/cpu/cpu_bsd.c b/src/detection/cpu/cpu_bsd.c index bf1f29394..5c74ebf54 100644 --- a/src/detection/cpu/cpu_bsd.c +++ b/src/detection/cpu/cpu_bsd.c @@ -1,5 +1,6 @@ #include "cpu.h" #include "common/sysctl.h" +#include "util/stringUtils.h" #include #if __has_include() @@ -34,35 +35,60 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) if (ffSysctlGetString("hw.model", &cpu->name) != NULL) return "sysctlbyname(hw.model) failed"; - cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); - cpu->coresLogical = cpu->coresPhysical; + cpu->coresLogical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); + cpu->coresPhysical = 0; cpu->coresOnline = (uint16_t) ffSysctlGetInt("kern.smp.cpus", cpu->coresLogical); FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); if (ffSysctlGetString("kern.sched.topology_spec", &buffer) == NULL && buffer.length > 0) { // - // - // 0, 1, 2, 3 - // - // - // 0, 1 - // THREAD groupSMT group - // - // - // 2, 3 - // THREAD groupSMT group - // - // - // + // + // 0, 1, 2, 3 + // + // + // 0, 1 + // THREAD groupSMT group + // + // + // 2, 3 + // THREAD groupSMT group + // + // + // // - uint32_t i = 0; - while (true) + char* line = NULL; + size_t len = 0; + bool inLvl2 = false, threadGroup = false; + uint32_t cpuCount = 0; + while (ffStrbufGetline(&line, &len, &buffer)) { - i = ffStrbufNextIndexS(&buffer, i, "THREAD group"); // Find physical core with hyper-threading enabled - if (i >= buffer.length) break; - cpu->coresPhysical--; - i += (uint32_t) strlen("THREAD group"); + if (!inLvl2) + { + if (ffStrStartsWith(line, " packages++; + } + else + { + if (ffStrEquals(line, " ")) + { + cpu->coresPhysical += threadGroup ? 1 : cpuCount; + inLvl2 = false; + } + else if (cpuCount == 0 && ffStrStartsWith(line, " 0 && ffStrStartsWith(line, " ")) + { + if (ffStrContains(line, "THREAD group")) + threadGroup = true; + } + } } }