From dd3b3f22bac7d58ab15d21ed4c017820bb49330f Mon Sep 17 00:00:00 2001 From: Carter Li Date: Tue, 26 Dec 2023 11:20:40 +0800 Subject: [PATCH] CPU (Linux): detect multi cpu models by lscpu Supports asahi linux --- src/detection/cpu/cpu_linux.c | 47 ++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index 4b190cfc7..05f5590cd 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef __ANDROID__ #include "common/settings.h" @@ -171,10 +172,50 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) if (cpu->name.length == 0) { FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); - if (!ffProcessAppendStdOut(&buffer, (char *const[]) { "lscpu", NULL })) + if (ffProcessAppendStdOut(&buffer, (char *const[]) { "lscpu", NULL }) == NULL) { - ffParsePropLines(buffer.chars, "Model name:", &cpu->name); - if (ffStrbufEqualS(&cpu->name, "-")) ffStrbufClear(&cpu->name); + char* pstart = buffer.chars; + while ((pstart = strstr(pstart, "Model name:"))) + { + pstart += strlen("Model name:"); + while (isspace(*pstart)) ++pstart; + if (*pstart == '\0') + break; + + if (cpu->name.length > 0) + ffStrbufAppendS(&cpu->name, " + "); + + char* pend = strchr(pstart, '\n'); + if (pend != NULL) + ffStrbufAppendNS(&cpu->name, (uint32_t) (pend - pstart), pstart); + else + { + ffStrbufAppendS(&cpu->name, pstart); + break; + } + + pstart = pend + 1; + } + + if (cpu->vendor.length == 0) + { + pstart = strstr(buffer.chars, "Vendor ID:"); + if (pstart) + { + pstart += strlen("Vendor ID:"); + while (isspace(*pstart)) ++pstart; + if (*pstart) + { + char* pend = strchr(pstart, '\n'); + if (pend != NULL) + ffStrbufAppendNS(&cpu->vendor, (uint32_t) (pend - pstart), pstart); + else + { + ffStrbufAppendS(&cpu->vendor, pstart); + } + } + } + } } } #endif