Compare commits

..

6 Commits

Author SHA1 Message Date
Dr-Noob
92992be225 Support for multi socket systems (only information gathering, not printing) 2020-06-22 18:04:24 +02:00
Dr-Noob
698274e44c Fix bug and memory leak 2020-06-22 18:00:45 +02:00
Dr-Noob
7fee305e8b Little fixes 2020-06-22 15:30:05 +02:00
Dr-Noob
1ce6b97bec Fix topology in AMD, using extended level to fetch it 2020-06-22 15:04:32 +02:00
Dr-Noob
8211f24f46 Fix cache in AMD, using extended level 2020-06-22 13:50:00 +02:00
Dr-Noob
0725e9d876 Add --levels option 2020-06-22 13:17:00 +02:00
5 changed files with 132 additions and 36 deletions

View File

@@ -5,24 +5,27 @@
#define ARG_STR_STYLE "style" #define ARG_STR_STYLE "style"
#define ARG_STR_HELP "help" #define ARG_STR_HELP "help"
#define ARG_STR_LEVELS "levels"
#define ARG_STR_VERSION "version" #define ARG_STR_VERSION "version"
#define ARG_CHAR_STYLE 's' #define ARG_CHAR_STYLE 's'
#define ARG_CHAR_HELP 'h' #define ARG_CHAR_HELP 'h'
#define ARG_CHAR_LEVELS 'l'
#define ARG_CHAR_VERSION 'v' #define ARG_CHAR_VERSION 'v'
#define STYLE_STR_1 "default" #define STYLE_STR_1 "default"
#define STYLE_STR_2 "dark" #define STYLE_STR_2 "dark"
#define STYLE_STR_3 "none" #define STYLE_STR_3 "none"
struct args_struct { struct args_struct {
int help_flag; bool levels_flag;
int version_flag; bool help_flag;
bool version_flag;
STYLE style; STYLE style;
}; };
static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2, STYLE_STR_3 }; static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2, STYLE_STR_3 };
static struct args_struct args; static struct args_struct args;
STYLE parseStyle(char* style) { STYLE parse_style(char* style) {
int i = 0; int i = 0;
while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0) while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0)
i++; i++;
@@ -32,34 +35,40 @@ STYLE parseStyle(char* style) {
return i; return i;
} }
STYLE getStyle() { STYLE get_style() {
return args.style; return args.style;
} }
int showHelp() { bool show_help() {
return args.help_flag; return args.help_flag;
} }
int showVersion() { bool show_version() {
return args.version_flag; return args.version_flag;
} }
bool show_levels() {
return args.levels_flag;
}
bool verbose_enabled() { bool verbose_enabled() {
return false; return false;
} }
bool parseArgs(int argc, char* argv[]) { bool parse_args(int argc, char* argv[]) {
int c; int c;
int digit_optind = 0; int digit_optind = 0;
int option_index = 0; int option_index = 0;
opterr = 0; opterr = 0;
args.levels_flag = false;
args.help_flag = false; args.help_flag = false;
args.style = STYLE_EMPTY; args.style = STYLE_EMPTY;
static struct option long_options[] = { static struct option long_options[] = {
{ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE }, {ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE },
{ARG_STR_HELP, no_argument, 0, ARG_CHAR_HELP }, {ARG_STR_HELP, no_argument, 0, ARG_CHAR_HELP },
{ARG_STR_LEVELS, no_argument, 0, ARG_CHAR_LEVELS },
{ARG_STR_VERSION, no_argument, 0, ARG_CHAR_VERSION }, {ARG_STR_VERSION, no_argument, 0, ARG_CHAR_VERSION },
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@@ -72,7 +81,7 @@ bool parseArgs(int argc, char* argv[]) {
printf("ERROR: Style option specified more than once\n"); printf("ERROR: Style option specified more than once\n");
return false; return false;
} }
args.style = parseStyle(optarg); args.style = parse_style(optarg);
if(args.style == STYLE_INVALID) { if(args.style == STYLE_INVALID) {
printf("ERROR: Invalid style '%s'\n",optarg); printf("ERROR: Invalid style '%s'\n",optarg);
return false; return false;
@@ -85,6 +94,13 @@ bool parseArgs(int argc, char* argv[]) {
} }
args.help_flag = true; args.help_flag = true;
} }
else if(c == ARG_CHAR_LEVELS) {
if(args.levels_flag) {
printf("ERROR: Levels option specified more than once\n");
return false;
}
args.levels_flag = true;
}
else if (c == ARG_CHAR_VERSION) { else if (c == ARG_CHAR_VERSION) {
if(args.version_flag) { if(args.version_flag) {
printf("ERROR: Version option specified more than once\n"); printf("ERROR: Version option specified more than once\n");

View File

@@ -4,10 +4,11 @@
#include <stdbool.h> #include <stdbool.h>
#include "printer.h" #include "printer.h"
bool parseArgs(int argc, char* argv[]); bool parse_args(int argc, char* argv[]);
STYLE getStyle(); STYLE get_style();
int showHelp(); bool show_help();
int showVersion(); bool show_levels();
bool show_version();
bool verbose_enabled(); bool verbose_enabled();
#endif #endif

View File

@@ -25,17 +25,18 @@ Peak FLOPS: 512 GFLOP/s(in simple precision)
***/ ***/
static const char* VERSION = "0.47"; static const char* VERSION = "0.410";
void print_help(int argc, char *argv[]) { void print_help(char *argv[]) {
printf("Usage: %s [--version] [--help] [--style STYLE]\n\ printf("Usage: %s [--version] [--help] [--style STYLE]\n\
Options: \n\ Options: \n\
--style Set logo style color\n\ --style Set logo style color\n\
default: Default style color\n\ default: Default style color\n\
dark: Dark style color\n\ dark: Dark style color\n\
none: Don't use colors\n\ none: Don't use colors\n\
--help Print this help and exit\n\ --help Prints this help and exit\n\
--version Print cpufetch version and exit\n", --levels Prints CPU model and cpuid levels (debug purposes)\n\
--version Prints cpufetch version and exit\n",
argv[0]); argv[0]);
} }
@@ -44,15 +45,15 @@ void print_version() {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if(!parseArgs(argc,argv)) if(!parse_args(argc,argv))
return EXIT_FAILURE; return EXIT_FAILURE;
if(showHelp()) { if(show_help()) {
print_help(argc, argv); print_help(argv);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if(showVersion()) { if(show_version()) {
print_version(); print_version();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@@ -62,6 +63,13 @@ int main(int argc, char* argv[]) {
struct cpuInfo* cpu = get_cpu_info(); struct cpuInfo* cpu = get_cpu_info();
if(cpu == NULL) if(cpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
char* cpuName = get_str_cpu_name();
if(show_levels()) {
print_version();
print_levels(cpu, cpuName);
return EXIT_SUCCESS;
}
struct cache* cach = get_cache_info(cpu); struct cache* cach = get_cache_info(cpu);
if(cach == NULL) if(cach == NULL)
@@ -75,11 +83,10 @@ int main(int argc, char* argv[]) {
if(topo == NULL) if(topo == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
struct ascii* art = set_ascii(get_cpu_vendor(cpu),getStyle()); struct ascii* art = set_ascii(get_cpu_vendor(cpu),get_style());
if(art == NULL) if(art == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
char* cpuName = get_str_cpu_name();
char* maxFrequency = get_str_freq(freq); char* maxFrequency = get_str_freq(freq);
char* nCores = get_str_topology(topo); char* nCores = get_str_topology(topo);
char* avx = get_str_avx(cpu); char* avx = get_str_avx(cpu);
@@ -123,6 +130,7 @@ int main(int argc, char* argv[]) {
free(cpu); free(cpu);
free(art); free(art);
free_cache_struct(cach); free_cache_struct(cach);
free_topo_struct(topo);
free_freq_struct(freq); free_freq_struct(freq);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@@ -5,14 +5,17 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include "udev.h"
#endif
#include "standart.h" #include "standart.h"
#include "cpuid.h" #include "cpuid.h"
#include "global.h" #include "global.h"
#ifndef _WIN32
#include "udev.h"
#endif
#define VENDOR_INTEL_STRING "GenuineIntel" #define VENDOR_INTEL_STRING "GenuineIntel"
#define VENDOR_AMD_STRING "AuthenticAMD" #define VENDOR_AMD_STRING "AuthenticAMD"
@@ -29,6 +32,7 @@
/* /*
* cpuid reference: http://www.sandpile.org/x86/cpuid.htm * cpuid reference: http://www.sandpile.org/x86/cpuid.htm
* cpuid amd: https://www.amd.com/system/files/TechDocs/25481.pdf
*/ */
struct cpuInfo { struct cpuInfo {
@@ -68,9 +72,11 @@ struct frequency {
}; };
struct topology { struct topology {
int64_t total_cores;
uint32_t physical_cores; uint32_t physical_cores;
uint32_t logical_cores; uint32_t logical_cores;
uint32_t smt; uint32_t smt;
uint32_t sockets;
bool ht; bool ht;
}; };
@@ -91,7 +97,7 @@ void init_cpu_info(struct cpuInfo* cpu) {
cpu->SHA = false; cpu->SHA = false;
} }
void get_cpu_vendor_internal(char* name, uint32_t eax,uint32_t ebx,uint32_t ecx,uint32_t edx) { void get_cpu_vendor_internal(char* name, uint32_t ebx,uint32_t ecx,uint32_t edx) {
name[__COUNTER__] = ebx & MASK; name[__COUNTER__] = ebx & MASK;
name[__COUNTER__] = (ebx>>8) & MASK; name[__COUNTER__] = (ebx>>8) & MASK;
name[__COUNTER__] = (ebx>>16) & MASK; name[__COUNTER__] = (ebx>>16) & MASK;
@@ -123,7 +129,7 @@ struct cpuInfo* get_cpu_info() {
//Fill vendor //Fill vendor
char name[13]; char name[13];
memset(name,0,13); memset(name,0,13);
get_cpu_vendor_internal(name, eax,ebx,ecx,edx); get_cpu_vendor_internal(name, ebx, ecx, edx);
if(strcmp(VENDOR_INTEL_STRING,name) == 0) if(strcmp(VENDOR_INTEL_STRING,name) == 0)
cpu->cpu_vendor = VENDOR_INTEL; cpu->cpu_vendor = VENDOR_INTEL;
@@ -197,7 +203,7 @@ struct cpuInfo* get_cpu_info() {
} }
struct topology* get_topology_info(struct cpuInfo* cpu) { struct topology* get_topology_info(struct cpuInfo* cpu) {
struct topology* topo = malloc(sizeof(struct cache)); struct topology* topo = malloc(sizeof(struct topology));
uint32_t eax = 0; uint32_t eax = 0;
uint32_t ebx = 0; uint32_t ebx = 0;
uint32_t ecx = 0; uint32_t ecx = 0;
@@ -228,7 +234,6 @@ struct topology* get_topology_info(struct cpuInfo* cpu) {
} }
topo->smt = ebx & 0xFFFF; topo->smt = ebx & 0xFFFF;
eax = 0x0000000B; eax = 0x0000000B;
ecx = 0x00000001; ecx = 0x00000001;
cpuid(&eax, &ebx, &ecx, &edx); cpuid(&eax, &ebx, &ecx, &edx);
@@ -248,27 +253,80 @@ struct topology* get_topology_info(struct cpuInfo* cpu) {
} }
break; break;
case VENDOR_AMD: case VENDOR_AMD:
printBug("Unimplemented!"); if (cpu->maxExtendedLevels >= 0x80000008) {
eax = 0x80000008;
cpuid(&eax, &ebx, &ecx, &edx);
topo->logical_cores = (ecx & 0xFF) + 1;
if (cpu->maxExtendedLevels >= 0x8000001E) {
eax = 0x8000001E;
cpuid(&eax, &ebx, &ecx, &edx);
topo->smt = ((ebx >> 8) & 0x03) + 1;
}
else {
printWarn("Can't read topology information from cpuid (needed extended level is 0x%.8X, max is 0x%.8X)", 0x8000001E, cpu->maxLevels);
topo->smt = 1;
}
topo->physical_cores = topo->logical_cores / topo->smt;
}
else {
printWarn("Can't read topology information from cpuid (needed extended level is 0x%.8X, max is 0x%.8X)", 0x80000008, cpu->maxLevels);
topo->physical_cores = 1;
topo->logical_cores = 1;
topo->smt = 1;
}
break; break;
default: default:
printBug("Cant get topology because VENDOR is empty"); printBug("Cant get topology because VENDOR is empty");
return NULL; return NULL;
} }
// Ask the OS the total number of cores it sees
// If we have one socket, it will be same as the cpuid,
// but in dual socket it will not!
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
topo->total_cores = info.dwNumberOfProcessors;
#else
if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
perror("sysconf");
topo->total_cores = topo->logical_cores; // fallback
}
#endif
topo->sockets = topo->total_cores / topo->smt / topo->physical_cores; // Idea borrowed from lscpu
return topo; return topo;
} }
// see https://stackoverflow.com/questions/12594208/c-program-to-determine-levels-size-of-cache
struct cache* get_cache_info(struct cpuInfo* cpu) { struct cache* get_cache_info(struct cpuInfo* cpu) {
struct cache* cach = malloc(sizeof(struct cache)); struct cache* cach = malloc(sizeof(struct cache));
uint32_t eax = 0; uint32_t eax = 0;
uint32_t ebx = 0; uint32_t ebx = 0;
uint32_t ecx = 0; uint32_t ecx = 0;
uint32_t edx = 0; uint32_t edx = 0;
uint32_t level;
// We use standart 0x00000004 for Intel
// We use extended 0x8000001D for AMD
if(cpu->cpu_vendor == VENDOR_INTEL) {
level = 0x00000004;
if(cpu->maxLevels < level) {
printErr("Can't read cache information from cpuid (needed level is %d, max is %d)", level, cpu->maxLevels);
return NULL;
}
}
else {
level = 0x8000001D;
if(cpu->maxExtendedLevels < level) {
printErr("Can't read cache information from cpuid (needed extended level is %d, max is %d)", level, cpu->maxExtendedLevels);
return NULL;
}
}
// We suppose there are 4 caches (at most) // We suppose there are 4 caches (at most)
for(int i=0; i < 4; i++) { for(int i=0; i < 4; i++) {
eax = 4; // get cache info eax = level; // get cache info
ebx = 0; ebx = 0;
ecx = i; // cache id ecx = i; // cache id
edx = 0; edx = 0;
@@ -659,6 +717,16 @@ char* get_str_freq(struct frequency* freq) {
return string; return string;
} }
void print_levels(struct cpuInfo* cpu, char* cpu_name) {
printf("%s\n", cpu_name);
printf("- Max standart level: 0x%.8X\n", cpu->maxLevels);
printf("- Max extended level: 0x%.8X\n", cpu->maxExtendedLevels);
}
void free_topo_struct(struct topology* topo) {
free(topo);
}
void free_cache_struct(struct cache* cach) { void free_cache_struct(struct cache* cach) {
free(cach); free(cach);
} }

View File

@@ -41,8 +41,11 @@ char* get_str_topology(struct topology* topo);
char* get_str_peak_performance(struct cpuInfo* cpu, struct topology* topo, int64_t freq); char* get_str_peak_performance(struct cpuInfo* cpu, struct topology* topo, int64_t freq);
void print_levels(struct cpuInfo* cpu, char* cpu_name);
void free_cpuinfo_struct(struct cpuInfo* cpu); void free_cpuinfo_struct(struct cpuInfo* cpu);
void free_cache_struct(struct cache* cach); void free_cache_struct(struct cache* cach);
void free_topo_struct(struct topology* topo);
void free_freq_struct(struct frequency* freq); void free_freq_struct(struct frequency* freq);
void debug_cpu_info(struct cpuInfo* cpu); void debug_cpu_info(struct cpuInfo* cpu);