From cfcc4c83751e407a3d3351d509209a35f583c07f Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 4 Sep 2022 14:12:04 -0700 Subject: [PATCH] MacOS cpu support --- src/common/settings.c | 18 ++++++++++++++++ src/common/settings.h | 2 ++ src/detection/cpu/cpu_apple.c | 39 ++++++++++++++++++++++++++++------- src/fastfetch.c | 25 ++++++++-------------- 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/common/settings.c b/src/common/settings.c index de79ce131..1e718de8b 100644 --- a/src/common/settings.c +++ b/src/common/settings.c @@ -374,4 +374,22 @@ void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result) result->chars[result->length] = '\0'; } + +int ffSettingsGetAppleInt(const char* propName, int defaultValue) +{ + int result; + size_t neededLength = sizeof(result); + if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0) + return defaultValue; + return result; +} + +int64_t ffSettingsGetAppleInt64(const char* propName, int64_t defaultValue) +{ + int64_t result; + size_t neededLength = sizeof(result); + if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0) + return defaultValue; + return result; +} #endif //__APPLE__ diff --git a/src/common/settings.h b/src/common/settings.h index 836901d06..acc299d67 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -38,6 +38,8 @@ void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result); #ifdef __APPLE__ void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result); +int ffSettingsGetAppleInt(const char* propName, int defaultValue); +int64_t ffSettingsGetAppleInt64(const char* propName, int64_t defaultValue); #endif #endif diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index b5b89e8e9..6a318585e 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -1,17 +1,42 @@ #include "cpu.h" +#include "common/settings.h" + +static double getFrequency(const char* propName) +{ + double herz = (double) ffSettingsGetAppleInt64(propName, 0); + if(herz <= 0.0) + return herz; + + herz /= 1000.0; //to KHz + herz /= 1000.0; //to MHz + return herz / 1000.0; //to GHz +} void ffDetectCPUImpl(FFCPUResult* cpu) { ffStrbufInit(&cpu->name); + ffSettingsGetAppleProperty("machdep.cpu.brand_string", &cpu->name); + ffStrbufInit(&cpu->vendor); + ffSettingsGetAppleProperty("machdep.cpu.vendor", &cpu->vendor); - cpu->coresPhysical = 0; - cpu->coresLogical = 0; - cpu->coresOnline = 0; + cpu->coresPhysical = (uint16_t) ffSettingsGetAppleInt("hw.physicalcpu_max", 1); + if(cpu->coresPhysical == 1) + cpu->coresPhysical = (uint16_t) ffSettingsGetAppleInt("hw.physicalcpu", 1); - cpu->frequencyCurrent = 0.0; - cpu->frequencyMin = 0.0; - cpu->frequencyMax = 0.0; + cpu->coresLogical = (uint16_t) ffSettingsGetAppleInt("hw.logicalcpu_max", 1); + if(cpu->coresLogical == 1) + cpu->coresLogical = (uint16_t) ffSettingsGetAppleInt("hw.ncpu", 1); - cpu->temperature = FF_CPU_TEMP_UNKNOWN; + cpu->coresOnline = (uint16_t) ffSettingsGetAppleInt("hw.logicalcpu", 1); + if(cpu->coresOnline == 1) + cpu->coresOnline = (uint16_t) ffSettingsGetAppleInt("hw.activecpu", 1); + + cpu->frequencyMin = getFrequency("hw.cpufrequency_min"); + cpu->frequencyMax = getFrequency("hw.cpufrequency_max"); + if(cpu->frequencyMax == 0.0) + cpu->frequencyMax = getFrequency("hw.cpufrequency"); + + //TODO find a way to detect this + cpu->temperature = FF_CPU_TEMP_UNSET; } diff --git a/src/fastfetch.c b/src/fastfetch.c index f6a274672..f15a4f025 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -247,22 +247,15 @@ static inline void printCommandHelp(const char* command) } else if(strcasecmp(command, "cpu-format") == 0) { - constructAndPrintCommandHelpFormat("cpu", "{2} ({7}) @ {15}GHz", 15, - "CPU name", - "Prettified CPU name", - "CPU Vendor name (Vendor ID)", - "CPU logical core count online", - "CPU logical core count configured", - "CPU physical core count", - "Always set core count", - "CPU package temperature", - "frequency bios limit", - "frequency scaling max", - "frequency scaling min", - "frequency info max", - "frequency info min", - "frequeny from /proc/cpuinfo", - "most accurate frequeny" + constructAndPrintCommandHelpFormat("cpu", "{1} ({5}) @ {7}GHz", 8, + "Name", + "Vendor", + "Physical core count", + "Logical core count", + "Online core count", + "Min frequency", + "Max frequency", + "Temperature" ); } else if(strcasecmp(command, "cpu-usage-format") == 0)