mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 07:50:40 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92992be225 | ||
|
|
698274e44c | ||
|
|
7fee305e8b | ||
|
|
1ce6b97bec | ||
|
|
8211f24f46 | ||
|
|
0725e9d876 |
32
src/args.c
32
src/args.c
@@ -5,24 +5,27 @@
|
||||
|
||||
#define ARG_STR_STYLE "style"
|
||||
#define ARG_STR_HELP "help"
|
||||
#define ARG_STR_LEVELS "levels"
|
||||
#define ARG_STR_VERSION "version"
|
||||
#define ARG_CHAR_STYLE 's'
|
||||
#define ARG_CHAR_HELP 'h'
|
||||
#define ARG_CHAR_LEVELS 'l'
|
||||
#define ARG_CHAR_VERSION 'v'
|
||||
#define STYLE_STR_1 "default"
|
||||
#define STYLE_STR_2 "dark"
|
||||
#define STYLE_STR_3 "none"
|
||||
|
||||
struct args_struct {
|
||||
int help_flag;
|
||||
int version_flag;
|
||||
bool levels_flag;
|
||||
bool help_flag;
|
||||
bool version_flag;
|
||||
STYLE style;
|
||||
};
|
||||
|
||||
static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2, STYLE_STR_3 };
|
||||
static struct args_struct args;
|
||||
|
||||
STYLE parseStyle(char* style) {
|
||||
STYLE parse_style(char* style) {
|
||||
int i = 0;
|
||||
while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0)
|
||||
i++;
|
||||
@@ -32,34 +35,40 @@ STYLE parseStyle(char* style) {
|
||||
return i;
|
||||
}
|
||||
|
||||
STYLE getStyle() {
|
||||
STYLE get_style() {
|
||||
return args.style;
|
||||
}
|
||||
|
||||
int showHelp() {
|
||||
bool show_help() {
|
||||
return args.help_flag;
|
||||
}
|
||||
|
||||
int showVersion() {
|
||||
bool show_version() {
|
||||
return args.version_flag;
|
||||
}
|
||||
|
||||
bool show_levels() {
|
||||
return args.levels_flag;
|
||||
}
|
||||
|
||||
bool verbose_enabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool parseArgs(int argc, char* argv[]) {
|
||||
bool parse_args(int argc, char* argv[]) {
|
||||
int c;
|
||||
int digit_optind = 0;
|
||||
int option_index = 0;
|
||||
opterr = 0;
|
||||
|
||||
args.levels_flag = false;
|
||||
args.help_flag = false;
|
||||
args.style = STYLE_EMPTY;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE },
|
||||
{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 },
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
@@ -72,7 +81,7 @@ bool parseArgs(int argc, char* argv[]) {
|
||||
printf("ERROR: Style option specified more than once\n");
|
||||
return false;
|
||||
}
|
||||
args.style = parseStyle(optarg);
|
||||
args.style = parse_style(optarg);
|
||||
if(args.style == STYLE_INVALID) {
|
||||
printf("ERROR: Invalid style '%s'\n",optarg);
|
||||
return false;
|
||||
@@ -85,6 +94,13 @@ bool parseArgs(int argc, char* argv[]) {
|
||||
}
|
||||
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) {
|
||||
if(args.version_flag) {
|
||||
printf("ERROR: Version option specified more than once\n");
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
#include <stdbool.h>
|
||||
#include "printer.h"
|
||||
|
||||
bool parseArgs(int argc, char* argv[]);
|
||||
STYLE getStyle();
|
||||
int showHelp();
|
||||
int showVersion();
|
||||
bool parse_args(int argc, char* argv[]);
|
||||
STYLE get_style();
|
||||
bool show_help();
|
||||
bool show_levels();
|
||||
bool show_version();
|
||||
bool verbose_enabled();
|
||||
|
||||
#endif
|
||||
|
||||
30
src/main.c
30
src/main.c
@@ -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\
|
||||
Options: \n\
|
||||
--style Set logo style color\n\
|
||||
default: Default style color\n\
|
||||
dark: Dark style color\n\
|
||||
none: Don't use colors\n\
|
||||
--help Print this help and exit\n\
|
||||
--version Print cpufetch version and exit\n",
|
||||
--help Prints this help and exit\n\
|
||||
--levels Prints CPU model and cpuid levels (debug purposes)\n\
|
||||
--version Prints cpufetch version and exit\n",
|
||||
argv[0]);
|
||||
}
|
||||
|
||||
@@ -44,15 +45,15 @@ void print_version() {
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if(!parseArgs(argc,argv))
|
||||
if(!parse_args(argc,argv))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if(showHelp()) {
|
||||
print_help(argc, argv);
|
||||
if(show_help()) {
|
||||
print_help(argv);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if(showVersion()) {
|
||||
if(show_version()) {
|
||||
print_version();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -62,6 +63,13 @@ int main(int argc, char* argv[]) {
|
||||
struct cpuInfo* cpu = get_cpu_info();
|
||||
if(cpu == NULL)
|
||||
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);
|
||||
if(cach == NULL)
|
||||
@@ -75,11 +83,10 @@ int main(int argc, char* argv[]) {
|
||||
if(topo == NULL)
|
||||
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)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
char* cpuName = get_str_cpu_name();
|
||||
|
||||
char* maxFrequency = get_str_freq(freq);
|
||||
char* nCores = get_str_topology(topo);
|
||||
char* avx = get_str_avx(cpu);
|
||||
@@ -123,6 +130,7 @@ int main(int argc, char* argv[]) {
|
||||
free(cpu);
|
||||
free(art);
|
||||
free_cache_struct(cach);
|
||||
free_topo_struct(topo);
|
||||
free_freq_struct(freq);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -5,14 +5,17 @@
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include "udev.h"
|
||||
#endif
|
||||
|
||||
#include "standart.h"
|
||||
#include "cpuid.h"
|
||||
#include "global.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include "udev.h"
|
||||
#endif
|
||||
|
||||
#define VENDOR_INTEL_STRING "GenuineIntel"
|
||||
#define VENDOR_AMD_STRING "AuthenticAMD"
|
||||
|
||||
@@ -29,6 +32,7 @@
|
||||
|
||||
/*
|
||||
* cpuid reference: http://www.sandpile.org/x86/cpuid.htm
|
||||
* cpuid amd: https://www.amd.com/system/files/TechDocs/25481.pdf
|
||||
*/
|
||||
|
||||
struct cpuInfo {
|
||||
@@ -68,9 +72,11 @@ struct frequency {
|
||||
};
|
||||
|
||||
struct topology {
|
||||
int64_t total_cores;
|
||||
uint32_t physical_cores;
|
||||
uint32_t logical_cores;
|
||||
uint32_t smt;
|
||||
uint32_t smt;
|
||||
uint32_t sockets;
|
||||
bool ht;
|
||||
};
|
||||
|
||||
@@ -91,7 +97,7 @@ void init_cpu_info(struct cpuInfo* cpu) {
|
||||
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>>8) & MASK;
|
||||
name[__COUNTER__] = (ebx>>16) & MASK;
|
||||
@@ -123,7 +129,7 @@ struct cpuInfo* get_cpu_info() {
|
||||
//Fill vendor
|
||||
char name[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)
|
||||
cpu->cpu_vendor = VENDOR_INTEL;
|
||||
@@ -197,7 +203,7 @@ struct cpuInfo* get_cpu_info() {
|
||||
}
|
||||
|
||||
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 ebx = 0;
|
||||
uint32_t ecx = 0;
|
||||
@@ -226,8 +232,7 @@ struct topology* get_topology_info(struct cpuInfo* cpu) {
|
||||
printBug("Unexpected type in cpuid 0x0000000B (expected 1, got %d)", type);
|
||||
return NULL;
|
||||
}
|
||||
topo->smt = ebx & 0xFFFF;
|
||||
|
||||
topo->smt = ebx & 0xFFFF;
|
||||
|
||||
eax = 0x0000000B;
|
||||
ecx = 0x00000001;
|
||||
@@ -248,27 +253,80 @@ struct topology* get_topology_info(struct cpuInfo* cpu) {
|
||||
}
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
printBug("Cant get topology because VENDOR is empty");
|
||||
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;
|
||||
}
|
||||
|
||||
// see https://stackoverflow.com/questions/12594208/c-program-to-determine-levels-size-of-cache
|
||||
struct cache* get_cache_info(struct cpuInfo* cpu) {
|
||||
struct cache* cach = malloc(sizeof(struct cache));
|
||||
uint32_t eax = 0;
|
||||
uint32_t ebx = 0;
|
||||
uint32_t ecx = 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)
|
||||
for(int i=0; i < 4; i++) {
|
||||
eax = 4; // get cache info
|
||||
eax = level; // get cache info
|
||||
ebx = 0;
|
||||
ecx = i; // cache id
|
||||
edx = 0;
|
||||
@@ -659,6 +717,16 @@ char* get_str_freq(struct frequency* freq) {
|
||||
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) {
|
||||
free(cach);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
void print_levels(struct cpuInfo* cpu, char* cpu_name);
|
||||
|
||||
void free_cpuinfo_struct(struct cpuInfo* cpu);
|
||||
void free_cache_struct(struct cache* cach);
|
||||
void free_topo_struct(struct topology* topo);
|
||||
void free_freq_struct(struct frequency* freq);
|
||||
|
||||
void debug_cpu_info(struct cpuInfo* cpu);
|
||||
|
||||
Reference in New Issue
Block a user