GPU: correct detection of Apple M1 GPU

Don't cheat
This commit is contained in:
李通洲 2022-09-16 18:11:39 +08:00 committed by Carter Li
parent 413d126bcc
commit 09f3bd5a79
7 changed files with 43 additions and 52 deletions

View File

@ -17,7 +17,7 @@
--terminal-font-format Raw: {}; Name: {}; Size: {}; Styles: {}; Pretty: {} --terminal-font-format Raw: {}; Name: {}; Size: {}; Styles: {}; Pretty: {}
--cpu-format Name: {}, Vendor: {}, CoresPhysical: {}, CoresLogical: {}, CoresOnline: {}, FrequencyMin: {}, FrequencyMax: {}, Temperature: {} --cpu-format Name: {}, Vendor: {}, CoresPhysical: {}, CoresLogical: {}, CoresOnline: {}, FrequencyMin: {}, FrequencyMax: {}, Temperature: {}
--cpu-usage-format Percentage: {} --cpu-usage-format Percentage: {}
--gpu-format Vendor: {}; Name: {}; Driver: {}; Temperature: {} --gpu-format Vendor: {}; Name: {}; Driver: {}; Temperature: {}; CoreCount: {}
--memory-format Used: {}; Total: {}; Percentage: {} --memory-format Used: {}; Total: {}; Percentage: {}
--disk-format Used: {}; Total: {}; Files: {}; Percentage: {} --disk-format Used: {}; Total: {}; Files: {}; Percentage: {}
--battery-format Manufactor: {}; Model: {}; Technology: {}; Capacity: {}; Status: {} --battery-format Manufactor: {}; Model: {}; Technology: {}; Capacity: {}; Status: {}

View File

@ -6,6 +6,7 @@
#include "fastfetch.h" #include "fastfetch.h"
#define FF_GPU_TEMP_UNSET (0/0.0) #define FF_GPU_TEMP_UNSET (0/0.0)
#define FF_GPU_CORE_COUNT_UNSET -1
typedef struct FFGPUResult typedef struct FFGPUResult
{ {
@ -13,6 +14,7 @@ typedef struct FFGPUResult
FFstrbuf name; FFstrbuf name;
FFstrbuf driver; FFstrbuf driver;
double temperature; double temperature;
int coreCount;
} FFGPUResult; } FFGPUResult;
const FFlist* ffDetectGPU(const FFinstance* instance); const FFlist* ffDetectGPU(const FFinstance* instance);

View File

@ -1,6 +1,7 @@
#include "gpu.h" #include "gpu.h"
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
{ {
FF_UNUSED(gpus, instance); FF_UNUSED(gpus, instance);
return "Unimplemented";
} }

View File

@ -1,33 +1,18 @@
#include "gpu.h" #include "gpu.h"
#include "common/library.h" #include "common/library.h"
#include "detection/cpu/cpu.h" #include "detection/cpu/cpu.h"
#include "util/apple/cfdict_helpers.h"
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/graphics/IOGraphicsLib.h> #include <IOKit/graphics/IOGraphicsLib.h>
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
{ {
FF_UNUSED(instance); FF_UNUSED(instance);
const FFCPUResult* cpu = ffDetectCPU(); CFMutableDictionaryRef matchDict = IOServiceMatching(kIOAcceleratorClassName);
if(ffStrbufStartsWithIgnCaseS(&cpu->name, "Apple M"))
{
FFGPUResult* gpu = ffListAdd(gpus);
ffStrbufInit(&gpu->vendor);
ffStrbufAppendS(&gpu->vendor, "Apple");
ffStrbufInit(&gpu->name);
ffStrbufAppendS(&gpu->name, cpu->name.chars + 6); //Cut "Apple "
ffStrbufInitA(&gpu->driver, 0);
gpu->temperature = FF_GPU_TEMP_UNSET;
}
CFMutableDictionaryRef matchDict = IOServiceMatching("IOPCIDevice");
io_iterator_t iterator; io_iterator_t iterator;
if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess) if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess)
return; return "IOServiceGetMatchingServices() failed";
io_registry_entry_t registryEntry; io_registry_entry_t registryEntry;
while((registryEntry = IOIteratorNext(iterator)) != 0) while((registryEntry = IOIteratorNext(iterator)) != 0)
@ -39,25 +24,19 @@ void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
continue; continue;
} }
CFStringRef key = CFStringCreateWithCStringNoCopy(NULL, "model", kCFStringEncodingASCII, kCFAllocatorNull);
CFStringRef model = CFDictionaryGetValue(properties, key);
if(model == NULL || CFGetTypeID(model) != CFDataGetTypeID())
{
CFRelease(properties);
IOObjectRelease(registryEntry);
continue;
}
FFGPUResult* gpu = ffListAdd(gpus); FFGPUResult* gpu = ffListAdd(gpus);
uint32_t modelLength = (uint32_t) CFStringGetLength(model); ffStrbufInit(&gpu->name);
ffStrbufInitA(&gpu->name, modelLength + 1); ffCfDictGetString(properties, "model", &gpu->name);
CFStringGetCString(model, gpu->name.chars, modelLength + 1, kCFStringEncodingASCII);
gpu->name.length = modelLength; if (!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount))
gpu->name.chars[gpu->name.length] = '\0'; gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
ffStrbufInitA(&gpu->vendor, 0); ffStrbufInitA(&gpu->vendor, 0);
ffStrbufInitA(&gpu->driver, 0);
ffStrbufInit(&gpu->driver);
ffCfDictGetString(properties, "CFBundleIdentifier", &gpu->driver);
gpu->temperature = FF_GPU_TEMP_UNSET; gpu->temperature = FF_GPU_TEMP_UNSET;
CFRelease(properties); CFRelease(properties);
@ -65,4 +44,5 @@ void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
} }
IOObjectRelease(iterator); IOObjectRelease(iterator);
return NULL;
} }

View File

@ -168,24 +168,26 @@ static void pciHandleDevice(FFlist* results, PCIData* pci, struct pci_dev* devic
ffStrbufInit(&gpu->driver); ffStrbufInit(&gpu->driver);
pciDetectDriverName(gpu, pci, device); pciDetectDriverName(gpu, pci, device);
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET; gpu->temperature = FF_GPU_TEMP_UNSET;
pciDetectTemperatur(gpu, device); pciDetectTemperatur(gpu, device);
} }
static void pciDetectGPUs(const FFinstance* instance, FFlist* gpus) static const char* pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
{ {
PCIData pci; PCIData pci;
FF_LIBRARY_LOAD(libpci, instance->config.libPCI, , "libpci.so", 4) FF_LIBRARY_LOAD(libpci, instance->config.libPCI, "dlopen libpci.so failed", "libpci.so", 4)
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_alloc, ) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_alloc)
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_init, ) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_init)
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_scan_bus, ) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_scan_bus)
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_cleanup, ) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_cleanup)
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_read_byte, ) FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_read_byte)
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_read_word, ) FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_read_word)
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_lookup_name, ) FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_lookup_name)
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_get_param, ) FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_get_param)
pci.access = ffpci_alloc(); pci.access = ffpci_alloc();
ffpci_init(pci.access); ffpci_init(pci.access);
@ -200,13 +202,14 @@ static void pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
ffpci_cleanup(pci.access); ffpci_cleanup(pci.access);
dlclose(libpci); dlclose(libpci);
return NULL;
} }
#endif #endif
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
{ {
#ifdef FF_HAVE_LIBPCI #ifdef FF_HAVE_LIBPCI
pciDetectGPUs(instance, gpus); return pciDetectGPUs(instance, gpus);
#endif #endif
} }

View File

@ -274,11 +274,12 @@ static inline void printCommandHelp(const char* command)
} }
else if(strcasecmp(command, "gpu-format") == 0) else if(strcasecmp(command, "gpu-format") == 0)
{ {
constructAndPrintCommandHelpFormat("gpu", "{} {}", 4, constructAndPrintCommandHelpFormat("gpu", "{} {}", 5,
"GPU vendor", "GPU vendor",
"GPU name", "GPU name",
"GPU driver", "GPU driver",
"GPU temperature" "GPU temperature",
"GPU core count"
); );
} }
else if(strcasecmp(command, "memory-format") == 0) else if(strcasecmp(command, "memory-format") == 0)

View File

@ -7,7 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#define FF_GPU_MODULE_NAME "GPU" #define FF_GPU_MODULE_NAME "GPU"
#define FF_GPU_NUM_FORMAT_ARGS 4 #define FF_GPU_NUM_FORMAT_ARGS 5
static void printGPUResult(FFinstance* instance, uint8_t index, FFcache* cache, FFGPUResult* gpu) static void printGPUResult(FFinstance* instance, uint8_t index, FFcache* cache, FFGPUResult* gpu)
{ {
@ -22,11 +22,15 @@ static void printGPUResult(FFinstance* instance, uint8_t index, FFcache* cache,
ffStrbufAppend(&output, &gpu->name); ffStrbufAppend(&output, &gpu->name);
if(gpu->coreCount != FF_GPU_CORE_COUNT_UNSET)
ffStrbufAppendF(&output, " (%d)", gpu->coreCount);
ffPrintAndAppendToCache(instance, FF_GPU_MODULE_NAME, index, &instance->config.gpu, cache, &output, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){ ffPrintAndAppendToCache(instance, FF_GPU_MODULE_NAME, index, &instance->config.gpu, cache, &output, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor}, {FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name}, {FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver}, {FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver},
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature} {FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature},
{FF_FORMAT_ARG_TYPE_INT, &gpu->coreCount},
}); });
ffStrbufDestroy(&output); ffStrbufDestroy(&output);