Skip to content

Commit 09f3bd5

Browse files
committed
GPU: correct detection of Apple M1 GPU
Don't cheat
1 parent 413d126 commit 09f3bd5

File tree

7 files changed

+43
-52
lines changed

7 files changed

+43
-52
lines changed

presets/verbose

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
--terminal-font-format Raw: {}; Name: {}; Size: {}; Styles: {}; Pretty: {}
1818
--cpu-format Name: {}, Vendor: {}, CoresPhysical: {}, CoresLogical: {}, CoresOnline: {}, FrequencyMin: {}, FrequencyMax: {}, Temperature: {}
1919
--cpu-usage-format Percentage: {}
20-
--gpu-format Vendor: {}; Name: {}; Driver: {}; Temperature: {}
20+
--gpu-format Vendor: {}; Name: {}; Driver: {}; Temperature: {}; CoreCount: {}
2121
--memory-format Used: {}; Total: {}; Percentage: {}
2222
--disk-format Used: {}; Total: {}; Files: {}; Percentage: {}
2323
--battery-format Manufactor: {}; Model: {}; Technology: {}; Capacity: {}; Status: {}

src/detection/gpu/gpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include "fastfetch.h"
77

88
#define FF_GPU_TEMP_UNSET (0/0.0)
9+
#define FF_GPU_CORE_COUNT_UNSET -1
910

1011
typedef struct FFGPUResult
1112
{
1213
FFstrbuf vendor;
1314
FFstrbuf name;
1415
FFstrbuf driver;
1516
double temperature;
17+
int coreCount;
1618
} FFGPUResult;
1719

1820
const FFlist* ffDetectGPU(const FFinstance* instance);

src/detection/gpu/gpu_android.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "gpu.h"
22

3-
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
3+
const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
44
{
55
FF_UNUSED(gpus, instance);
6+
return "Unimplemented";
67
}

src/detection/gpu/gpu_apple.c

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
#include "gpu.h"
22
#include "common/library.h"
33
#include "detection/cpu/cpu.h"
4+
#include "util/apple/cfdict_helpers.h"
45

5-
#include <CoreFoundation/CoreFoundation.h>
66
#include <IOKit/graphics/IOGraphicsLib.h>
77

8-
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
8+
const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
99
{
1010
FF_UNUSED(instance);
1111

12-
const FFCPUResult* cpu = ffDetectCPU();
13-
if(ffStrbufStartsWithIgnCaseS(&cpu->name, "Apple M"))
14-
{
15-
FFGPUResult* gpu = ffListAdd(gpus);
16-
17-
ffStrbufInit(&gpu->vendor);
18-
ffStrbufAppendS(&gpu->vendor, "Apple");
19-
20-
ffStrbufInit(&gpu->name);
21-
ffStrbufAppendS(&gpu->name, cpu->name.chars + 6); //Cut "Apple "
22-
23-
ffStrbufInitA(&gpu->driver, 0);
24-
gpu->temperature = FF_GPU_TEMP_UNSET;
25-
}
26-
27-
CFMutableDictionaryRef matchDict = IOServiceMatching("IOPCIDevice");
12+
CFMutableDictionaryRef matchDict = IOServiceMatching(kIOAcceleratorClassName);
2813
io_iterator_t iterator;
2914
if(IOServiceGetMatchingServices(0, matchDict, &iterator) != kIOReturnSuccess)
30-
return;
15+
return "IOServiceGetMatchingServices() failed";
3116

3217
io_registry_entry_t registryEntry;
3318
while((registryEntry = IOIteratorNext(iterator)) != 0)
@@ -39,30 +24,25 @@ void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
3924
continue;
4025
}
4126

42-
CFStringRef key = CFStringCreateWithCStringNoCopy(NULL, "model", kCFStringEncodingASCII, kCFAllocatorNull);
43-
CFStringRef model = CFDictionaryGetValue(properties, key);
44-
if(model == NULL || CFGetTypeID(model) != CFDataGetTypeID())
45-
{
46-
CFRelease(properties);
47-
IOObjectRelease(registryEntry);
48-
continue;
49-
}
50-
5127
FFGPUResult* gpu = ffListAdd(gpus);
5228

53-
uint32_t modelLength = (uint32_t) CFStringGetLength(model);
54-
ffStrbufInitA(&gpu->name, modelLength + 1);
55-
CFStringGetCString(model, gpu->name.chars, modelLength + 1, kCFStringEncodingASCII);
56-
gpu->name.length = modelLength;
57-
gpu->name.chars[gpu->name.length] = '\0';
29+
ffStrbufInit(&gpu->name);
30+
ffCfDictGetString(properties, "model", &gpu->name);
31+
32+
if (!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount))
33+
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
5834

5935
ffStrbufInitA(&gpu->vendor, 0);
60-
ffStrbufInitA(&gpu->driver, 0);
36+
37+
ffStrbufInit(&gpu->driver);
38+
ffCfDictGetString(properties, "CFBundleIdentifier", &gpu->driver);
39+
6140
gpu->temperature = FF_GPU_TEMP_UNSET;
6241

6342
CFRelease(properties);
6443
IOObjectRelease(registryEntry);
6544
}
6645

6746
IOObjectRelease(iterator);
47+
return NULL;
6848
}

src/detection/gpu/gpu_linux.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,26 @@ static void pciHandleDevice(FFlist* results, PCIData* pci, struct pci_dev* devic
168168
ffStrbufInit(&gpu->driver);
169169
pciDetectDriverName(gpu, pci, device);
170170

171+
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
172+
171173
gpu->temperature = FF_GPU_TEMP_UNSET;
172174
pciDetectTemperatur(gpu, device);
173175
}
174176

175-
static void pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
177+
static const char* pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
176178
{
177179
PCIData pci;
178180

179-
FF_LIBRARY_LOAD(libpci, instance->config.libPCI, , "libpci.so", 4)
180-
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_alloc, )
181-
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_init, )
182-
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_scan_bus, )
183-
FF_LIBRARY_LOAD_SYMBOL(libpci, pci_cleanup, )
181+
FF_LIBRARY_LOAD(libpci, instance->config.libPCI, "dlopen libpci.so failed", "libpci.so", 4)
182+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_alloc)
183+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_init)
184+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_scan_bus)
185+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libpci, pci_cleanup)
184186

185-
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_read_byte, )
186-
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_read_word, )
187-
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_lookup_name, )
188-
FF_LIBRARY_LOAD_SYMBOL_VAR(libpci, pci, pci_get_param, )
187+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_read_byte)
188+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_read_word)
189+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_lookup_name)
190+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libpci, pci, pci_get_param)
189191

190192
pci.access = ffpci_alloc();
191193
ffpci_init(pci.access);
@@ -200,13 +202,14 @@ static void pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
200202

201203
ffpci_cleanup(pci.access);
202204
dlclose(libpci);
205+
return NULL;
203206
}
204207

205208
#endif
206209

207-
void ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
210+
const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
208211
{
209212
#ifdef FF_HAVE_LIBPCI
210-
pciDetectGPUs(instance, gpus);
213+
return pciDetectGPUs(instance, gpus);
211214
#endif
212215
}

src/fastfetch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,12 @@ static inline void printCommandHelp(const char* command)
274274
}
275275
else if(strcasecmp(command, "gpu-format") == 0)
276276
{
277-
constructAndPrintCommandHelpFormat("gpu", "{} {}", 4,
277+
constructAndPrintCommandHelpFormat("gpu", "{} {}", 5,
278278
"GPU vendor",
279279
"GPU name",
280280
"GPU driver",
281-
"GPU temperature"
281+
"GPU temperature",
282+
"GPU core count"
282283
);
283284
}
284285
else if(strcasecmp(command, "memory-format") == 0)

src/modules/gpu.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <stdlib.h>
88

99
#define FF_GPU_MODULE_NAME "GPU"
10-
#define FF_GPU_NUM_FORMAT_ARGS 4
10+
#define FF_GPU_NUM_FORMAT_ARGS 5
1111

1212
static void printGPUResult(FFinstance* instance, uint8_t index, FFcache* cache, FFGPUResult* gpu)
1313
{
@@ -22,11 +22,15 @@ static void printGPUResult(FFinstance* instance, uint8_t index, FFcache* cache,
2222

2323
ffStrbufAppend(&output, &gpu->name);
2424

25+
if(gpu->coreCount != FF_GPU_CORE_COUNT_UNSET)
26+
ffStrbufAppendF(&output, " (%d)", gpu->coreCount);
27+
2528
ffPrintAndAppendToCache(instance, FF_GPU_MODULE_NAME, index, &instance->config.gpu, cache, &output, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){
2629
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor},
2730
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name},
2831
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver},
29-
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature}
32+
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature},
33+
{FF_FORMAT_ARG_TYPE_INT, &gpu->coreCount},
3034
});
3135

3236
ffStrbufDestroy(&output);

0 commit comments

Comments
 (0)