diff --git a/Makefile b/Makefile index 9b14db7..0ee4f8a 100644 --- a/Makefile +++ b/Makefile @@ -3,28 +3,28 @@ CXX=gcc CXXFLAGS=-Wall -Wextra -Werror -pedantic -fstack-protector-all -pedantic -std=c99 SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2 -COMMON_DIR=src/common/ +SRC_COMMON=src/common/ ifneq ($(OS),Windows_NT) arch := $(shell uname -m) ifeq ($(arch), x86_64) SRC_DIR=src/x86/ - SOURCE += $(COMMON_DIR)main.c $(SRC_DIR)cpuid.c $(SRC_DIR)apic.c $(SRC_DIR)cpuid_asm.c $(SRC_DIR)udev.c $(SRC_DIR)uarch.c $(COMMON_DIR)printer.c $(COMMON_DIR)args.c $(COMMON_DIR)global.c - HEADERS += $(SRC_DIR)cpuid.h $(SRC_DIR)apic.h $(SRC_DIR)cpuid_asm.h $(SRC_DIR)udev.h $(SRC_DIR)uarch.h $(COMMON_DIR)ascii.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h - CXXFLAGS += -D_ARCH_X86 + SOURCE += $(SRC_COMMON)main.c $(SRC_DIR)cpuid.c $(SRC_DIR)apic.c $(SRC_DIR)cpuid_asm.c $(SRC_COMMON)udev.c $(SRC_DIR)uarch.c $(SRC_COMMON)printer.c $(SRC_COMMON)args.c $(SRC_COMMON)global.c + HEADERS += $(SRC_DIR)cpuid.h $(SRC_DIR)apic.h $(SRC_DIR)cpuid_asm.h $(SRC_COMMON)udev.h $(SRC_DIR)uarch.h $(SRC_COMMON)ascii.h $(SRC_COMMON)printer.h $(SRC_COMMON)args.h $(SRC_COMMON)global.h + CXXFLAGS += -DARCH_X86 else SRC_DIR=src/arm/ - SOURCE += $(COMMON_DIR)main.c $(SRC_DIR)cpuid.c $(SRC_DIR)uarch.c $(COMMON_DIR)printer.c $(COMMON_DIR)args.c $(COMMON_DIR)global.c - HEADERS += $(COMMON_DIR)ascii.h $(SRC_DIR)uarch.h $(SRC_DIR)cpuid.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h - CXXFLAGS += -D_ARCH_ARM -Wno-unused-parameter + SOURCE += $(SRC_COMMON)main.c $(SRC_DIR)midr.c $(SRC_DIR)uarch.c $(SRC_COMMON)printer.c $(SRC_COMMON)args.c $(SRC_COMMON)global.c + HEADERS += $(SRC_COMMON)ascii.h $(SRC_DIR)uarch.h $(SRC_DIR)midr.h $(SRC_COMMON)printer.h $(SRC_COMMON)args.h $(SRC_COMMON)global.h + CXXFLAGS += -DARCH_ARM -Wno-unused-parameter endif OUTPUT=cpufetch else # Assume x86_64 SRC_DIR=src/x86/ - SOURCE += $(COMMON_DIR)main.c $(SRC_DIR)cpuid.c $(SRC_DIR)apic.c $(SRC_DIR)cpuid_asm.c $(SRC_DIR)udev.c $(SRC_DIR)uarch.c $(COMMON_DIR)printer.c $(COMMON_DIR)args.c $(COMMON_DIR)global.c - HEADERS += $(SRC_DIR)cpuid.h $(SRC_DIR)apic.h $(SRC_DIR)cpuid_asm.h $(SRC_DIR)udev.h $(SRC_DIR)uarch.h $(COMMON_DIR)ascii.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h + SOURCE += $(SRC_COMMON)main.c $(SRC_DIR)cpuid.c $(SRC_DIR)apic.c $(SRC_DIR)cpuid_asm.c $(SRC_DIR)udev.c $(SRC_DIR)uarch.c $(SRC_COMMON)printer.c $(SRC_COMMON)args.c $(SRC_COMMON)global.c + HEADERS += $(SRC_DIR)cpuid.h $(SRC_DIR)apic.h $(SRC_DIR)cpuid_asm.h $(SRC_DIR)udev.h $(SRC_DIR)uarch.h $(SRC_COMMON)ascii.h $(SRC_COMMON)printer.h $(SRC_COMMON)args.h $(SRC_COMMON)global.h CXXFLAGS += -D_ARCH_X86 SANITY_FLAGS += -Wno-pedantic-ms-format OUTPUT=cpufetch.exe diff --git a/src/arm/cpuid.c b/src/arm/midr.c similarity index 97% rename from src/arm/cpuid.c rename to src/arm/midr.c index 814e1e3..69aa169 100644 --- a/src/arm/cpuid.c +++ b/src/arm/midr.c @@ -4,23 +4,18 @@ #include #include -#include "cpuid.h" +#include "midr.h" -struct frequency { - int64_t base; - int64_t max; -}; - -struct cpuInfo* get_cpu_info() { +struct cpuInfo* get_cpu_info() { struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo)); - + cpu->cpu_vendor = CPU_VENDOR_UNKNOWN; cpu->cpu_name = malloc(sizeof(char) * CPU_NAME_MAX_LENGTH); strcpy(cpu->cpu_name, "Unknown"); cpu->arch = NULL; cpu->hv = malloc(sizeof(struct hypervisor)); cpu->hv->present = false; - + return cpu; } diff --git a/src/arm/cpuid.h b/src/arm/midr.h similarity index 97% rename from src/arm/cpuid.h rename to src/arm/midr.h index 3768638..86eaa48 100644 --- a/src/arm/cpuid.h +++ b/src/arm/midr.h @@ -1,5 +1,5 @@ -#ifndef __CPUID__ -#define __CPUID__ +#ifndef __MIDR__ +#define __MIDR__ #include "../common/cpu.h" diff --git a/src/arm/uarch.c b/src/arm/uarch.c index 845010f..140715c 100644 --- a/src/arm/uarch.c +++ b/src/arm/uarch.c @@ -7,9 +7,6 @@ #include "../common/global.h" struct uarch* get_uarch_from_cpuid(struct cpuInfo* cpu, uint32_t ef, uint32_t f, uint32_t em, uint32_t m, int s) { return NULL; } -bool vpus_are_AVX512(struct cpuInfo* cpu) { return false; } -bool is_knights_landing(struct cpuInfo* cpu) { return false; } -int get_number_of_vpus(struct cpuInfo* cpu) { return 0; } char* get_str_uarch(struct cpuInfo* cpu) { char* tmp = malloc(sizeof(char) * 10); strcpy(tmp, "Unknown"); return tmp; } char* get_str_process(struct cpuInfo* cpu) { char* tmp = malloc(sizeof(char) * 10); strcpy(tmp, "Unknown"); return tmp; } void free_uarch_struct(struct uarch* arch) { } diff --git a/src/arm/uarch.h b/src/arm/uarch.h index 0fc3267..61d86f4 100644 --- a/src/arm/uarch.h +++ b/src/arm/uarch.h @@ -3,14 +3,11 @@ #include -#include "cpuid.h" +#include "midr.h" struct uarch; struct uarch* get_uarch_from_cpuid(struct cpuInfo* cpu, uint32_t ef, uint32_t f, uint32_t em, uint32_t m, int s); -bool vpus_are_AVX512(struct cpuInfo* cpu); -bool is_knights_landing(struct cpuInfo* cpu); -int get_number_of_vpus(struct cpuInfo* cpu); char* get_str_uarch(struct cpuInfo* cpu); char* get_str_process(struct cpuInfo* cpu); void free_uarch_struct(struct uarch* arch); diff --git a/src/common/args.c b/src/common/args.c index 0aba3b6..afc6b62 100644 --- a/src/common/args.c +++ b/src/common/args.c @@ -20,7 +20,9 @@ enum { ARG_CHAR_STYLE, ARG_CHAR_COLOR, ARG_CHAR_HELP, +#ifdef ARCH_X86 ARG_CHAR_LEVELS, +#endif ARG_CHAR_VERBOSE, ARG_CHAR_VERSION }; @@ -168,7 +170,9 @@ bool parse_args(int argc, char* argv[]) { {"style", required_argument, 0, ARG_CHAR_STYLE }, {"color", required_argument, 0, ARG_CHAR_COLOR }, {"help", no_argument, 0, ARG_CHAR_HELP }, +#ifdef ARCH_X86 {"levels", no_argument, 0, ARG_CHAR_LEVELS }, +#endif {"verbose", no_argument, 0, ARG_CHAR_VERBOSE }, {"version", no_argument, 0, ARG_CHAR_VERSION }, {0, 0, 0, 0} @@ -213,6 +217,7 @@ bool parse_args(int argc, char* argv[]) { } args.verbose_flag = true; } +#ifdef ARCH_X86 else if(c == ARG_CHAR_LEVELS) { if(args.levels_flag) { printErr("Levels option specified more than once"); @@ -220,6 +225,7 @@ bool parse_args(int argc, char* argv[]) { } args.levels_flag = true; } +#endif else if (c == ARG_CHAR_VERSION) { if(args.version_flag) { printErr("Version option specified more than once"); diff --git a/src/common/ascii.h b/src/common/ascii.h index 6d7e1b5..6b0c05e 100644 --- a/src/common/ascii.h +++ b/src/common/ascii.h @@ -46,6 +46,25 @@ ########## ################ \ ############################### " +#define ARM_ASCII \ +" \ + \ + \ + \ + \ + ####### #### ########## #### ###### ######## \ + ############### ######### ####################### \ + #### #### #### ##### ####### ##### \ + #### #### #### #### ##### #### \ + #### #### #### #### #### #### \ + #### ##### #### #### #### #### \ + ############### #### #### #### #### \ + ######## #### ### #### #### #### \ + \ + \ + \ + " + #define UNKNOWN_ASCII \ " \ \ diff --git a/src/common/cpu.h b/src/common/cpu.h index a298f33..8149a8b 100644 --- a/src/common/cpu.h +++ b/src/common/cpu.h @@ -5,8 +5,12 @@ #include enum { +// ARCH_X86 CPU_VENDOR_INTEL, CPU_VENDOR_AMD, +// ARCH_ARM + CPU_VENDOR_ARM, +// OTHERS CPU_VENDOR_UNKNOWN, CPU_VENDOR_INVALID }; @@ -26,7 +30,10 @@ enum { typedef int32_t VENDOR; -struct frequency; +struct frequency { + int64_t base; + int64_t max; +}; struct hypervisor { bool present; @@ -35,6 +42,7 @@ struct hypervisor { }; struct cpuInfo { +#ifdef ARCH_X86 bool AVX; bool AVX2; bool AVX512; @@ -47,17 +55,21 @@ struct cpuInfo { bool SSE4_2; bool FMA3; bool FMA4; +#endif bool AES; bool SHA; VENDOR cpu_vendor; - + char* cpu_name; + +#ifdef ARCH_X86 // Max cpuids levels uint32_t maxLevels; // Max cpuids extended levels uint32_t maxExtendedLevels; - +#endif + struct uarch* arch; struct hypervisor* hv; }; @@ -75,7 +87,7 @@ struct cache { struct cach* L2; struct cach* L3; struct cach** cach_arr; - + uint8_t max_cache_level; }; @@ -83,11 +95,11 @@ struct topology { int64_t total_cores; uint32_t physical_cores; uint32_t logical_cores; - uint32_t smt_available; // Number of SMT that is currently enabled + uint32_t smt_available; // Number of SMT that is currently enabled uint32_t smt_supported; // Number of SMT that CPU supports (equal to smt_available if SMT is enabled) - uint32_t sockets; + uint32_t sockets; struct cache* cach; -#ifdef _ARCH_X86 +#ifdef ARCH_X86 struct apic* apic; #endif }; diff --git a/src/common/main.c b/src/common/main.c index d3860b2..5907795 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -5,19 +5,24 @@ #include "printer.h" #include "global.h" -#ifdef _ARCH_X86 +#ifdef ARCH_X86 static const char* ARCH_STR = "x86_64 build"; #include "../x86/cpuid.h" #else static const char* ARCH_STR = "ARM build"; - #include "../arm/cpuid.h" + #include "../arm/midr.h" #endif - static const char* VERSION = "0.8"; +static const char* VERSION = "0.8"; void print_help(char *argv[]) { - 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\ -Options: \n\ +#ifdef ARCH_X86 + 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", argv[0]); +#else + printf("Usage: %s [--version] [--help] [--style \"fancy\"|\"retro\"|\"legacy\"] [--color \"intel\"|\"amd\"|'R,G,B:R,G,B:R,G,B:R,G,B']\n\n", argv[0]); +#endif + + printf("Options: \n\ --color Set the color scheme. By default, cpufetch uses the system color scheme. This option \n\ lets the user use different colors to print the CPU art: \n\ * \"intel\": Use intel default color scheme \n\ @@ -29,9 +34,13 @@ Options: \n\ --style Set the style of CPU art: \n\ * \"fancy\": Default style \n\ * \"retro\": Old cpufetch style \n\ - * \"legacy\": Fallback style for terminals that does not support colors \n\n\ - --levels Prints CPU model and cpuid levels (debug purposes)\n\n\ - --verbose Prints extra information (if available) about how cpufetch tried fetching information\n\n\ + * \"legacy\": Fallback style for terminals that does not support colors \n\n"); + +#ifdef ARCH_X86 + printf(" --levels Prints CPU model and cpuid levels (debug purposes)\n\n"); +#endif + + printf(" --verbose Prints extra information (if available) about how cpufetch tried fetching information\n\n\ --help Prints this help and exit\n\n\ --version Prints cpufetch version and exit\n\n\ \n\ @@ -39,8 +48,7 @@ NOTES: \n\ - Bugs or improvements should be submitted to: github.com/Dr-Noob/cpufetch/issues \n\ - Peak performance information is NOT accurate. cpufetch computes peak performance using the max \n\ frequency. However, to properly compute peak performance, you need to know the frequency of the \n\ - CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. \n", - argv[0]); + CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. \n"); } void print_version() { @@ -52,6 +60,7 @@ int main(int argc, char* argv[]) { return EXIT_FAILURE; if(show_help()) { + print_version(); print_help(argv); return EXIT_SUCCESS; } @@ -60,32 +69,34 @@ int main(int argc, char* argv[]) { print_version(); return EXIT_SUCCESS; } - + set_log_level(verbose_enabled()); struct cpuInfo* cpu = get_cpu_info(); if(cpu == NULL) return EXIT_FAILURE; - + +#ifdef ARCH_X86 if(show_levels()) { print_version(); print_levels(cpu); - return EXIT_SUCCESS; - } - + return EXIT_SUCCESS; + } +#endif + struct frequency* freq = get_frequency_info(cpu); if(freq == NULL) return EXIT_FAILURE; - + struct cache* cach = get_cache_info(cpu); if(cach == NULL) return EXIT_FAILURE; - + struct topology* topo = get_topology_info(cpu, cach); if(topo == NULL) return EXIT_FAILURE; - - if(print_cpufetch(cpu, cach, freq, topo, get_style(), get_colors())) + + if(print_cpufetch(cpu, cach, freq, topo, get_style(), get_colors())) return EXIT_SUCCESS; else return EXIT_FAILURE; diff --git a/src/common/printer.c b/src/common/printer.c index 0bd7edd..2dcf9bd 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -7,12 +7,12 @@ #include "ascii.h" #include "../common/global.h" -#ifdef _ARCH_X86 +#ifdef ARCH_X86 #include "../x86/uarch.h" #include "../x86/cpuid.h" #else - #include "../arm/cpuid.h" - #include "../arm/uarch.h" + #include "../arm/uarch.h" + #include "../arm/midr.h" #endif #ifdef _WIN32 @@ -448,10 +448,12 @@ bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* f return true; } +#ifdef ARCH_X86 void print_levels(struct cpuInfo* cpu) { printf("%s\n", cpu->cpu_name); printf("- Max standart level: 0x%.8X\n", cpu->maxLevels); printf("- Max extended level: 0x%.8X\n", cpu->maxExtendedLevels); - + free_cpuinfo_struct(cpu); } +#endif diff --git a/src/common/printer.h b/src/common/printer.h index de55062..03a57e7 100644 --- a/src/common/printer.h +++ b/src/common/printer.h @@ -5,16 +5,19 @@ typedef int STYLE; #include "args.h" -#ifdef _ARCH_X86 +#ifdef ARCH_X86 #include "../x86/cpuid.h" #else - #include "../arm/cpuid.h" + #include "../arm/midr.h" #endif #define COLOR_DEFAULT_INTEL "15,125,194:230,230,230:40,150,220:230,230,230" #define COLOR_DEFAULT_AMD "250,250,250:0,154,102:250,250,250:0,154,102" +#ifdef ARCH_X86 void print_levels(struct cpuInfo* cpu); +#endif + bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct colors* cs); #endif diff --git a/src/x86/udev.c b/src/common/udev.c similarity index 94% rename from src/x86/udev.c rename to src/common/udev.c index b6a2d30..4e96929 100644 --- a/src/x86/udev.c +++ b/src/common/udev.c @@ -5,8 +5,13 @@ #include #include -#include "../common/global.h" -#include "cpuid.h" +#include "global.h" + +#ifdef ARCH_X86 + #include "../x86/cpuid.h" +#else + #include "../arm/cpuid.h" +#endif #define _PATH_SYS_SYSTEM "/sys/devices/system" #define _PATH_SYS_CPU _PATH_SYS_SYSTEM"/cpu" diff --git a/src/x86/udev.h b/src/common/udev.h similarity index 100% rename from src/x86/udev.h rename to src/common/udev.h diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index 027b927..f2ad3b6 100755 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -1,7 +1,7 @@ #ifdef _WIN32 #include #else - #include "udev.h" + #include "../common/udev.h" #include #endif @@ -58,11 +58,6 @@ static char *hv_vendors_name[] = { * cpuid amd: https://www.amd.com/system/files/TechDocs/25481.pdf */ -struct frequency { - int64_t base; - int64_t max; -}; - void init_cpu_info(struct cpuInfo* cpu) { cpu->AVX = false; cpu->AVX2 = false;