[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

@@ -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,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
};

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

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

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

79
src/common/udev.c Normal file
View File

@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.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"
#define _PATH_ONE_CPU _PATH_SYS_CPU"/cpu0"
#define _PATH_FREQUENCY _PATH_ONE_CPU"/cpufreq"
#define _PATH_FREQUENCY_MAX _PATH_FREQUENCY"/cpuinfo_max_freq"
#define _PATH_FREQUENCY_MIN _PATH_FREQUENCY"/cpuinfo_min_freq"
#define DEFAULT_FILE_SIZE 4096
long get_freq_from_file(char* path) {
int fd = open(path, O_RDONLY);
if(fd == -1) {
perror("open");
printBug("Could not open '%s'", path);
return UNKNOWN_FREQ;
}
//File exists, read it
int bytes_read = 0;
int offset = 0;
int block = 1;
char* buf = malloc(sizeof(char)*DEFAULT_FILE_SIZE);
memset(buf, 0, sizeof(char)*DEFAULT_FILE_SIZE);
while ( (bytes_read = read(fd, buf+offset, block)) > 0 ) {
offset += bytes_read;
}
char* end;
errno = 0;
long ret = strtol(buf, &end, 10);
if(errno != 0) {
perror("strtol");
printBug("Failed parsing '%s' file. Read data was: '%s'", path, buf);
free(buf);
return UNKNOWN_FREQ;
}
// We will be getting the frequency in KHz
// We consider it is an error if frequency is
// greater than 10 GHz or less than 100 MHz
if(ret > 10000 * 1000 || ret < 100 * 1000) {
printBug("Invalid data was read from file '%s': %ld\n", path, ret);
return UNKNOWN_FREQ;
}
free(buf);
if (close(fd) == -1) {
perror("close");
printErr("Closing '%s' failed\n", path);
}
return ret/1000;
}
long get_max_freq_from_file() {
return get_freq_from_file(_PATH_FREQUENCY_MAX);
}
long get_min_freq_from_file() {
return get_freq_from_file(_PATH_FREQUENCY_MIN);
}

7
src/common/udev.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef __UDEV__
#define __UDEV__
long get_max_freq_from_file();
long get_min_freq_from_file();
#endif