ohmu
·
2026-10-15
1#include "cpucache.h"
2#include "common/debug.h"
3#include "common/mallocHelper.h"
4#include "common/windows/nt.h"
5
6const char* ffDetectCPUCache(FFCPUCacheResult* result) {
7 LOGICAL_PROCESSOR_RELATIONSHIP lpr = RelationCache;
8 DWORD length = 0;
9 NtQuerySystemInformationEx(SystemLogicalProcessorAndGroupInformation, &lpr, sizeof(lpr), nullptr, 0, &length);
10 if (length == 0) {
11 FF_DEBUG("GetLogicalProcessorInformationEx(RelationCache, nullptr, &length) failed");
12 return "GetLogicalProcessorInformationEx(RelationCache, nullptr, &length) failed";
13 }
14
15 FF_AUTO_FREE SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*
16 pProcessorInfo = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) malloc(length);
17
18 NTSTATUS status = NtQuerySystemInformationEx(SystemLogicalProcessorAndGroupInformation, &lpr, sizeof(lpr), pProcessorInfo, length, &length);
19 if (!NT_SUCCESS(status)) {
20 FF_DEBUG("GetLogicalProcessorInformationEx(RelationCache, pProcessorInfo, &length) failed: %s", ffDebugNtStatus(status));
21 return "GetLogicalProcessorInformationEx(RelationCache, pProcessorInfo, &length) failed";
22 }
23
24 for (
25 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* ptr = pProcessorInfo;
26 (uint8_t*) ptr < ((uint8_t*) pProcessorInfo) + length;
27 ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) (((uint8_t*) ptr) + ptr->Size)) {
28 if (__builtin_expect(ptr->Relationship == RelationCache && ptr->Cache.Level > 0 && ptr->Cache.Level <= 4, true)) {
29 FFCPUCacheType cacheType = 0;
30 switch (ptr->Cache.Type) {
31 case CacheUnified:
32 cacheType = FF_CPU_CACHE_TYPE_UNIFIED;
33 break;
34 case CacheInstruction:
35 cacheType = FF_CPU_CACHE_TYPE_INSTRUCTION;
36 break;
37 case CacheData:
38 cacheType = FF_CPU_CACHE_TYPE_DATA;
39 break;
40 case CacheTrace:
41 cacheType = FF_CPU_CACHE_TYPE_TRACE;
42 break;
43 default:
44 break;
45 }
46 ffCPUCacheAddItem(result, ptr->Cache.Level, ptr->Cache.CacheSize, ptr->Cache.LineSize, cacheType);
47 }
48 }
49
50 return nullptr;
51}