Skip to content

Commit 877833d

Browse files
committed
Dont fetch if smt is enabled if its not supported (AMD). Dont guess cache topology, fetch it from CPUID (AMD)
1 parent 5cca6df commit 877833d

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

src/cpuid.c

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ void init_cpu_info(struct cpuInfo* cpu) {
5959
cpu->AES = false;
6060
cpu->SHA = false;
6161
}
62-
/*
63-
void init_topology_struct(struct topology* topo, struct cache* cach) {
64-
(*topo)->total_cores = 0;
65-
(*topo)->physical_cores = 0;
66-
(*topo)->logical_cores = 0;
67-
(*topo)->smt_available = 0;
68-
(*topo)->smt_supported = 0;
69-
(*topo)->sockets = 0;
70-
(*topo)->apic = malloc(sizeof(struct apic));
71-
(*topo)->cach = cach;
72-
}*/
7362

7463
void init_topology_struct(struct topology* topo, struct cache* cach) {
7564
topo->total_cores = 0;
@@ -282,34 +271,79 @@ struct cpuInfo* get_cpu_info() {
282271
return cpu;
283272
}
284273

285-
uint8_t get_number_llc_amd(struct topology* topo) {
286-
uint32_t eax = 0x8000001D;
287-
uint32_t ebx = 0;
288-
uint32_t ecx = 3; // LLC Level
289-
uint32_t edx = 0;
290-
uint32_t num_sharing_cache = 0;
291-
292-
cpuid(&eax, &ebx, &ecx, &edx);
293-
294-
num_sharing_cache = ((eax >> 14) & 0xFFF) + 1;
295-
296-
return topo->logical_cores / num_sharing_cache;
297-
}
298-
299-
void guess_cache_topology_amd(struct cpuInfo* cpu, struct topology* topo) {
300-
topo->cach->L1i->num_caches = topo->physical_cores;
301-
topo->cach->L1d->num_caches = topo->physical_cores;
302-
topo->cach->L2->num_caches = topo->physical_cores;
303-
304-
if(topo->cach->L3->exists) {
305-
if(cpu->maxExtendedLevels >= 0x8000001D) {
306-
topo->cach->L3->num_caches = get_number_llc_amd(topo);
274+
bool get_cache_topology_amd(struct cpuInfo* cpu, struct topology* topo) {
275+
if(cpu->maxExtendedLevels >= 0x8000001D) {
276+
uint32_t i, eax, ebx, ecx, edx, num_sharing_cache, cache_type, cache_level;
277+
278+
i = 0;
279+
do {
280+
eax = 0x8000001D;
281+
ebx = 0;
282+
ecx = i; // cache id
283+
edx = 0;
284+
285+
cpuid(&eax, &ebx, &ecx, &edx);
286+
287+
cache_type = eax & 0x1F;
288+
289+
if(cache_type > 0) {
290+
num_sharing_cache = ((eax >> 14) & 0xFFF) + 1;
291+
cache_level = (eax >>= 5) & 0x7;
292+
293+
switch (cache_type) {
294+
case 1: // Data Cache (We assume this is L1d)
295+
if(cache_level != 1) {
296+
printBug("Found data cache at level %d (expected 1)", cache_level);
297+
return false;
298+
}
299+
topo->cach->L1d->num_caches = topo->logical_cores / num_sharing_cache;
300+
break;
301+
302+
case 2: // Instruction Cache (We assume this is L1i)
303+
if(cache_level != 1) {
304+
printBug("Found instruction cache at level %d (expected 1)", cache_level);
305+
return false;
306+
}
307+
topo->cach->L1i->num_caches = topo->logical_cores / num_sharing_cache;
308+
break;
309+
310+
case 3: // Unified Cache (This may be L2 or L3)
311+
if(cache_level == 2) {
312+
topo->cach->L2->num_caches = topo->logical_cores / num_sharing_cache;
313+
}
314+
else if(cache_level == 3) {
315+
topo->cach->L3->num_caches = topo->logical_cores / num_sharing_cache;
316+
}
317+
else {
318+
printBug("Found unified cache at level %d (expected == 2 or 3)", cache_level);
319+
return false;
320+
}
321+
break;
322+
323+
default: // Unknown Type Cache
324+
printBug("Unknown Type Cache found at ID %d", i);
325+
return false;
326+
}
327+
}
328+
329+
i++;
330+
} while (cache_type > 0);
331+
}
332+
else {
333+
printWarn("Can't read topology information from cpuid (needed extended level is 0x%.8X, max is 0x%.8X). Guessing cache sizes", 0x8000001D, cpu->maxExtendedLevels);
334+
topo->cach->L1i->num_caches = topo->physical_cores;
335+
topo->cach->L1d->num_caches = topo->physical_cores;
336+
337+
if(topo->cach->L3->exists) {
338+
topo->cach->L2->num_caches = topo->physical_cores;
339+
topo->cach->L3->num_caches = 1;
307340
}
308341
else {
309-
printWarn("Can't read topology information from cpuid (needed extended level is 0x%.8X, max is 0x%.8X)", 0x8000001D, cpu->maxExtendedLevels);
310-
topo->cach->L3->num_caches = 1;
342+
topo->cach->L2->num_caches = 1;
311343
}
312344
}
345+
346+
return true;
313347
}
314348

315349
// Main reference: https://software.intel.com/content/www/us/en/develop/articles/intel-64-architecture-processor-topology-enumeration.html
@@ -375,7 +409,10 @@ struct topology* get_topology_info(struct cpuInfo* cpu, struct cache* cach) {
375409
}
376410

377411
if (cpu->maxLevels >= 0x00000001) {
378-
topo->smt_available = is_smt_enabled_amd(topo);
412+
if(topo->smt_supported > 1)
413+
topo->smt_available = is_smt_enabled_amd(topo);
414+
else
415+
topo->smt_available = 1;
379416
}
380417
else {
381418
printWarn("Can't read topology information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x0000000B, cpu->maxLevels);
@@ -388,7 +425,7 @@ struct topology* get_topology_info(struct cpuInfo* cpu, struct cache* cach) {
388425
else
389426
topo->sockets = topo->total_cores / topo->physical_cores;
390427

391-
guess_cache_topology_amd(cpu, topo);
428+
get_cache_topology_amd(cpu, topo);
392429

393430
break;
394431

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "cpuid.h"
77
#include "global.h"
88

9-
static const char* VERSION = "0.66";
9+
static const char* VERSION = "0.67";
1010

1111
void print_help(char *argv[]) {
1212
printf("Usage: %s [--version] [--help] [--levels] [--style \"fancy\"|\"retro\"|\"legacy\"] [--color \"intel\"|\"amd\"|'R,G,B:R,G,B:R,G,B:R,G,B']\n\n\

0 commit comments

Comments
 (0)