mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 16:00:39 +01:00
Support for more than one thread in a core
This commit is contained in:
48
01h.c
48
01h.c
@@ -16,6 +16,8 @@
|
|||||||
***/
|
***/
|
||||||
|
|
||||||
struct cpuInfo {
|
struct cpuInfo {
|
||||||
|
/*** BOOLEAN VALUES ***/
|
||||||
|
|
||||||
/* (256 bits) */
|
/* (256 bits) */
|
||||||
int AVX;
|
int AVX;
|
||||||
int AVX2;
|
int AVX2;
|
||||||
@@ -38,8 +40,13 @@ struct cpuInfo {
|
|||||||
int AES;
|
int AES;
|
||||||
int SHA;
|
int SHA;
|
||||||
|
|
||||||
int nCores;
|
/*** Number of threads ***/
|
||||||
|
int nThreads;
|
||||||
|
/*** Threads per core(Intel HyperThreading) ***/
|
||||||
|
int HT;
|
||||||
|
/*** Max CPUIDs levels ***/
|
||||||
int maxLevels;
|
int maxLevels;
|
||||||
|
/*** Max CPUIDs extended levels ***/
|
||||||
int maxExtendedLevels;
|
int maxExtendedLevels;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -64,8 +71,8 @@ struct cpuInfo* getCPUInfo() {
|
|||||||
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));
|
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));
|
||||||
initializeCpuInfo(cpu);
|
initializeCpuInfo(cpu);
|
||||||
|
|
||||||
cpu->nCores = sysconf( _SC_NPROCESSORS_ONLN );
|
|
||||||
unsigned eax, ebx, ecx, edx;
|
unsigned eax, ebx, ecx, edx;
|
||||||
|
|
||||||
eax = 0x0000000;
|
eax = 0x0000000;
|
||||||
cpuid(&eax, &ebx, &ecx, &edx);
|
cpuid(&eax, &ebx, &ecx, &edx);
|
||||||
cpu->maxLevels = eax;
|
cpu->maxLevels = eax;
|
||||||
@@ -73,7 +80,27 @@ struct cpuInfo* getCPUInfo() {
|
|||||||
cpuid(&eax, &ebx, &ecx, &edx);
|
cpuid(&eax, &ebx, &ecx, &edx);
|
||||||
cpu->maxExtendedLevels = eax;
|
cpu->maxExtendedLevels = eax;
|
||||||
|
|
||||||
|
//Fill cores and threads
|
||||||
|
cpu->nThreads = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
|
|
||||||
//Always check we can fetch data
|
//Always check we can fetch data
|
||||||
|
if (cpu->maxLevels >= 0x0000000B) {
|
||||||
|
eax = 0x0000000B;
|
||||||
|
ecx = 0x00000000;
|
||||||
|
cpuid(&eax, &ebx, &ecx, &edx);
|
||||||
|
cpu->HT = ebx & 0xF;
|
||||||
|
if(cpu->HT == 0) {
|
||||||
|
//AMD should not work with this, returning 0
|
||||||
|
//Suppose we have 1
|
||||||
|
cpu->HT = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//We can afford this check, assume 1
|
||||||
|
cpu->HT = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fill instructions support
|
||||||
if (cpu->maxLevels >= 0x00000001){
|
if (cpu->maxLevels >= 0x00000001){
|
||||||
eax = 0x00000001;
|
eax = 0x00000001;
|
||||||
cpuid(&eax, &ebx, &ecx, &edx);
|
cpuid(&eax, &ebx, &ecx, &edx);
|
||||||
@@ -154,7 +181,7 @@ char* getPeakPerformance(struct cpuInfo* cpu, long freq) {
|
|||||||
//7 for GFLOP/s and 6 for digits,eg 412.14
|
//7 for GFLOP/s and 6 for digits,eg 412.14
|
||||||
int size = 7+6+1+1;
|
int size = 7+6+1+1;
|
||||||
char* string = malloc(sizeof(char)*size);
|
char* string = malloc(sizeof(char)*size);
|
||||||
float flops = cpu->nCores*freq*2;
|
float flops = (cpu->nThreads/cpu->HT)*freq*2;
|
||||||
|
|
||||||
if(cpu->FMA3 || cpu->FMA4)
|
if(cpu->FMA3 || cpu->FMA4)
|
||||||
flops = flops*2;
|
flops = flops*2;
|
||||||
@@ -175,11 +202,22 @@ char* getPeakPerformance(struct cpuInfo* cpu, long freq) {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//4 cores(8 threads)
|
||||||
char* getString_NumberCores(struct cpuInfo* cpu) {
|
char* getString_NumberCores(struct cpuInfo* cpu) {
|
||||||
char* string = malloc(sizeof(char)*2+1);
|
if(cpu->HT > 1) {
|
||||||
snprintf(string,2+1,"%d",cpu->nCores);
|
//2(N.Cores)7(' cores(')2(N.Threads)9(' threads)')
|
||||||
|
int size = 2+7+2+9+1;
|
||||||
|
char* string = malloc(sizeof(char)*size);
|
||||||
|
snprintf(string,size,"%d cores(%d threads)",cpu->nThreads,cpu->nThreads/cpu->HT);
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
char* string = malloc(sizeof(char)*2+1);
|
||||||
|
snprintf(string,2+1,"%d cores",cpu->nThreads);
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
char* getString_AVX(struct cpuInfo* cpu) {
|
char* getString_AVX(struct cpuInfo* cpu) {
|
||||||
//If all AVX are available, it will use up to 15
|
//If all AVX are available, it will use up to 15
|
||||||
|
|||||||
24
topology.c
Normal file
24
topology.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
void cpuid(unsigned int *eax, unsigned int *ebx,
|
||||||
|
unsigned int *ecx, unsigned int *edx)
|
||||||
|
{
|
||||||
|
asm volatile("cpuid"
|
||||||
|
: "=a" (*eax),
|
||||||
|
"=b" (*ebx),
|
||||||
|
"=c" (*ecx),
|
||||||
|
"=d" (*edx)
|
||||||
|
: "0" (*eax), "2" (*ecx));
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
unsigned eax, ebx, ecx, edx;
|
||||||
|
|
||||||
|
eax = 0x0000000B;
|
||||||
|
ecx = 0x00000000;
|
||||||
|
cpuid(&eax, &ebx, &ecx, &edx);
|
||||||
|
|
||||||
|
printf("Threads per core=%d\n", ebx & 0xF);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user