[v0.8][Refactoring] Refactoring ARM code and source code tree

This commit is contained in:
Dr-Noob
2020-11-05 11:01:46 +01:00
parent 1fad4fd10b
commit f4f68287aa
14 changed files with 109 additions and 67 deletions

View File

@@ -3,28 +3,28 @@ CXX=gcc
CXXFLAGS=-Wall -Wextra -Werror -pedantic -fstack-protector-all -pedantic -std=c99 CXXFLAGS=-Wall -Wextra -Werror -pedantic -fstack-protector-all -pedantic -std=c99
SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2 SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2
COMMON_DIR=src/common/ SRC_COMMON=src/common/
ifneq ($(OS),Windows_NT) ifneq ($(OS),Windows_NT)
arch := $(shell uname -m) arch := $(shell uname -m)
ifeq ($(arch), x86_64) ifeq ($(arch), x86_64)
SRC_DIR=src/x86/ 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 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_DIR)udev.h $(SRC_DIR)uarch.h $(COMMON_DIR)ascii.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h 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 += -D_ARCH_X86 CXXFLAGS += -DARCH_X86
else else
SRC_DIR=src/arm/ 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 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 += $(COMMON_DIR)ascii.h $(SRC_DIR)uarch.h $(SRC_DIR)cpuid.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h 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 += -D_ARCH_ARM -Wno-unused-parameter CXXFLAGS += -DARCH_ARM -Wno-unused-parameter
endif endif
OUTPUT=cpufetch OUTPUT=cpufetch
else else
# Assume x86_64 # Assume x86_64
SRC_DIR=src/x86/ 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 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 $(COMMON_DIR)ascii.h $(COMMON_DIR)printer.h $(COMMON_DIR)args.h $(COMMON_DIR)global.h 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 CXXFLAGS += -D_ARCH_X86
SANITY_FLAGS += -Wno-pedantic-ms-format SANITY_FLAGS += -Wno-pedantic-ms-format
OUTPUT=cpufetch.exe OUTPUT=cpufetch.exe

View File

@@ -4,12 +4,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#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)); struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));

View File

@@ -1,5 +1,5 @@
#ifndef __CPUID__ #ifndef __MIDR__
#define __CPUID__ #define __MIDR__
#include "../common/cpu.h" #include "../common/cpu.h"

View File

@@ -7,9 +7,6 @@
#include "../common/global.h" #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; } 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_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; } 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) { } void free_uarch_struct(struct uarch* arch) { }

View File

@@ -3,14 +3,11 @@
#include <stdint.h> #include <stdint.h>
#include "cpuid.h" #include "midr.h"
struct uarch; 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); 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_uarch(struct cpuInfo* cpu);
char* get_str_process(struct cpuInfo* cpu); char* get_str_process(struct cpuInfo* cpu);
void free_uarch_struct(struct uarch* arch); void free_uarch_struct(struct uarch* arch);

View File

@@ -20,7 +20,9 @@ enum {
ARG_CHAR_STYLE, ARG_CHAR_STYLE,
ARG_CHAR_COLOR, ARG_CHAR_COLOR,
ARG_CHAR_HELP, ARG_CHAR_HELP,
#ifdef ARCH_X86
ARG_CHAR_LEVELS, ARG_CHAR_LEVELS,
#endif
ARG_CHAR_VERBOSE, ARG_CHAR_VERBOSE,
ARG_CHAR_VERSION ARG_CHAR_VERSION
}; };
@@ -168,7 +170,9 @@ bool parse_args(int argc, char* argv[]) {
{"style", required_argument, 0, ARG_CHAR_STYLE }, {"style", required_argument, 0, ARG_CHAR_STYLE },
{"color", required_argument, 0, ARG_CHAR_COLOR }, {"color", required_argument, 0, ARG_CHAR_COLOR },
{"help", no_argument, 0, ARG_CHAR_HELP }, {"help", no_argument, 0, ARG_CHAR_HELP },
#ifdef ARCH_X86
{"levels", no_argument, 0, ARG_CHAR_LEVELS }, {"levels", no_argument, 0, ARG_CHAR_LEVELS },
#endif
{"verbose", no_argument, 0, ARG_CHAR_VERBOSE }, {"verbose", no_argument, 0, ARG_CHAR_VERBOSE },
{"version", no_argument, 0, ARG_CHAR_VERSION }, {"version", no_argument, 0, ARG_CHAR_VERSION },
{0, 0, 0, 0} {0, 0, 0, 0}
@@ -213,6 +217,7 @@ bool parse_args(int argc, char* argv[]) {
} }
args.verbose_flag = true; args.verbose_flag = true;
} }
#ifdef ARCH_X86
else if(c == ARG_CHAR_LEVELS) { else if(c == ARG_CHAR_LEVELS) {
if(args.levels_flag) { if(args.levels_flag) {
printErr("Levels option specified more than once"); printErr("Levels option specified more than once");
@@ -220,6 +225,7 @@ bool parse_args(int argc, char* argv[]) {
} }
args.levels_flag = true; args.levels_flag = true;
} }
#endif
else if (c == ARG_CHAR_VERSION) { else if (c == ARG_CHAR_VERSION) {
if(args.version_flag) { if(args.version_flag) {
printErr("Version option specified more than once"); printErr("Version option specified more than once");

View File

@@ -46,6 +46,25 @@
########## ################ \ ########## ################ \
############################### " ############################### "
#define ARM_ASCII \
" \
\
\
\
\
####### #### ########## #### ###### ######## \
############### ######### ####################### \
#### #### #### ##### ####### ##### \
#### #### #### #### ##### #### \
#### #### #### #### #### #### \
#### ##### #### #### #### #### \
############### #### #### #### #### \
######## #### ### #### #### #### \
\
\
\
"
#define UNKNOWN_ASCII \ #define UNKNOWN_ASCII \
" \ " \
\ \

View File

@@ -5,8 +5,12 @@
#include <stdbool.h> #include <stdbool.h>
enum { enum {
// ARCH_X86
CPU_VENDOR_INTEL, CPU_VENDOR_INTEL,
CPU_VENDOR_AMD, CPU_VENDOR_AMD,
// ARCH_ARM
CPU_VENDOR_ARM,
// OTHERS
CPU_VENDOR_UNKNOWN, CPU_VENDOR_UNKNOWN,
CPU_VENDOR_INVALID CPU_VENDOR_INVALID
}; };
@@ -26,7 +30,10 @@ enum {
typedef int32_t VENDOR; typedef int32_t VENDOR;
struct frequency; struct frequency {
int64_t base;
int64_t max;
};
struct hypervisor { struct hypervisor {
bool present; bool present;
@@ -35,6 +42,7 @@ struct hypervisor {
}; };
struct cpuInfo { struct cpuInfo {
#ifdef ARCH_X86
bool AVX; bool AVX;
bool AVX2; bool AVX2;
bool AVX512; bool AVX512;
@@ -47,16 +55,20 @@ struct cpuInfo {
bool SSE4_2; bool SSE4_2;
bool FMA3; bool FMA3;
bool FMA4; bool FMA4;
#endif
bool AES; bool AES;
bool SHA; bool SHA;
VENDOR cpu_vendor; VENDOR cpu_vendor;
char* cpu_name; char* cpu_name;
#ifdef ARCH_X86
// Max cpuids levels // Max cpuids levels
uint32_t maxLevels; uint32_t maxLevels;
// Max cpuids extended levels // Max cpuids extended levels
uint32_t maxExtendedLevels; uint32_t maxExtendedLevels;
#endif
struct uarch* arch; struct uarch* arch;
struct hypervisor* hv; struct hypervisor* hv;
@@ -87,7 +99,7 @@ struct topology {
uint32_t smt_supported; // Number of SMT that CPU supports (equal to smt_available if SMT is 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; struct cache* cach;
#ifdef _ARCH_X86 #ifdef ARCH_X86
struct apic* apic; struct apic* apic;
#endif #endif
}; };

View File

@@ -5,19 +5,24 @@
#include "printer.h" #include "printer.h"
#include "global.h" #include "global.h"
#ifdef _ARCH_X86 #ifdef ARCH_X86
static const char* ARCH_STR = "x86_64 build"; static const char* ARCH_STR = "x86_64 build";
#include "../x86/cpuid.h" #include "../x86/cpuid.h"
#else #else
static const char* ARCH_STR = "ARM build"; static const char* ARCH_STR = "ARM build";
#include "../arm/cpuid.h" #include "../arm/midr.h"
#endif #endif
static const char* VERSION = "0.8"; static const char* VERSION = "0.8";
void print_help(char *argv[]) { 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\ #ifdef ARCH_X86
Options: \n\ 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\ --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\ lets the user use different colors to print the CPU art: \n\
* \"intel\": Use intel default color scheme \n\ * \"intel\": Use intel default color scheme \n\
@@ -29,9 +34,13 @@ Options: \n\
--style Set the style of CPU art: \n\ --style Set the style of CPU art: \n\
* \"fancy\": Default style \n\ * \"fancy\": Default style \n\
* \"retro\": Old cpufetch style \n\ * \"retro\": Old cpufetch style \n\
* \"legacy\": Fallback style for terminals that does not support colors \n\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\ #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\ --help Prints this help and exit\n\n\
--version Prints cpufetch version and exit\n\n\ --version Prints cpufetch version and exit\n\n\
\n\ \n\
@@ -39,8 +48,7 @@ NOTES: \n\
- Bugs or improvements should be submitted to: github.com/Dr-Noob/cpufetch/issues \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\ - 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\ 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", CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. \n");
argv[0]);
} }
void print_version() { void print_version() {
@@ -52,6 +60,7 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
if(show_help()) { if(show_help()) {
print_version();
print_help(argv); print_help(argv);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@@ -67,11 +76,13 @@ int main(int argc, char* argv[]) {
if(cpu == NULL) if(cpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
#ifdef ARCH_X86
if(show_levels()) { if(show_levels()) {
print_version(); print_version();
print_levels(cpu); print_levels(cpu);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#endif
struct frequency* freq = get_frequency_info(cpu); struct frequency* freq = get_frequency_info(cpu);
if(freq == NULL) if(freq == NULL)

View File

@@ -7,12 +7,12 @@
#include "ascii.h" #include "ascii.h"
#include "../common/global.h" #include "../common/global.h"
#ifdef _ARCH_X86 #ifdef ARCH_X86
#include "../x86/uarch.h" #include "../x86/uarch.h"
#include "../x86/cpuid.h" #include "../x86/cpuid.h"
#else #else
#include "../arm/cpuid.h"
#include "../arm/uarch.h" #include "../arm/uarch.h"
#include "../arm/midr.h"
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
@@ -448,6 +448,7 @@ bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* f
return true; return true;
} }
#ifdef ARCH_X86
void print_levels(struct cpuInfo* cpu) { void print_levels(struct cpuInfo* cpu) {
printf("%s\n", cpu->cpu_name); printf("%s\n", cpu->cpu_name);
printf("- Max standart level: 0x%.8X\n", cpu->maxLevels); printf("- Max standart level: 0x%.8X\n", cpu->maxLevels);
@@ -455,3 +456,4 @@ void print_levels(struct cpuInfo* cpu) {
free_cpuinfo_struct(cpu); free_cpuinfo_struct(cpu);
} }
#endif

View File

@@ -5,16 +5,19 @@ typedef int STYLE;
#include "args.h" #include "args.h"
#ifdef _ARCH_X86 #ifdef ARCH_X86
#include "../x86/cpuid.h" #include "../x86/cpuid.h"
#else #else
#include "../arm/cpuid.h" #include "../arm/midr.h"
#endif #endif
#define COLOR_DEFAULT_INTEL "15,125,194:230,230,230:40,150,220:230,230,230" #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" #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); 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); bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct colors* cs);
#endif #endif

View File

@@ -5,8 +5,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "../common/global.h" #include "global.h"
#include "cpuid.h"
#ifdef ARCH_X86
#include "../x86/cpuid.h"
#else
#include "../arm/cpuid.h"
#endif
#define _PATH_SYS_SYSTEM "/sys/devices/system" #define _PATH_SYS_SYSTEM "/sys/devices/system"
#define _PATH_SYS_CPU _PATH_SYS_SYSTEM"/cpu" #define _PATH_SYS_CPU _PATH_SYS_SYSTEM"/cpu"

View File

@@ -1,7 +1,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#else #else
#include "udev.h" #include "../common/udev.h"
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -58,11 +58,6 @@ static char *hv_vendors_name[] = {
* cpuid amd: https://www.amd.com/system/files/TechDocs/25481.pdf * 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) { void init_cpu_info(struct cpuInfo* cpu) {
cpu->AVX = false; cpu->AVX = false;
cpu->AVX2 = false; cpu->AVX2 = false;