[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
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

View File

@@ -4,12 +4,7 @@
#include <assert.h>
#include <stdbool.h>
#include "cpuid.h"
struct frequency {
int64_t base;
int64_t max;
};
#include "midr.h"
struct cpuInfo* get_cpu_info() {
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));

View File

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

View File

@@ -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) { }

View File

@@ -3,14 +3,11 @@
#include <stdint.h>
#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);

View File

@@ -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");

View File

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

View File

@@ -5,8 +5,12 @@
#include <stdbool.h>
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,16 +55,20 @@ 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;
@@ -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 sockets;
struct cache* cach;
#ifdef _ARCH_X86
#ifdef ARCH_X86
struct apic* apic;
#endif
};

View File

@@ -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";
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;
}
@@ -67,11 +76,13 @@ int main(int argc, char* argv[]) {
if(cpu == NULL)
return EXIT_FAILURE;
#ifdef ARCH_X86
if(show_levels()) {
print_version();
print_levels(cpu);
return EXIT_SUCCESS;
}
#endif
struct frequency* freq = get_frequency_info(cpu);
if(freq == NULL)

View File

@@ -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/midr.h"
#endif
#ifdef _WIN32
@@ -448,6 +448,7 @@ 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);
@@ -455,3 +456,4 @@ void print_levels(struct cpuInfo* cpu) {
free_cpuinfo_struct(cpu);
}
#endif

View File

@@ -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

View File

@@ -5,8 +5,13 @@
#include <fcntl.h>
#include <errno.h>
#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"

View File

@@ -1,7 +1,7 @@
#ifdef _WIN32
#include <windows.h>
#else
#include "udev.h"
#include "../common/udev.h"
#include <unistd.h>
#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;