From 9c8e1695927d7fc324521c0eccaf06ea91e37183 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Fri, 6 Nov 2020 10:02:59 +0100 Subject: [PATCH] [v0.82][ARM][ASCII][Refactoring] ARM ascii changes. Remove the assumption that all sockets are equal in a ARM based SoC. Little more support for ARM processors. Add ARM color style --- src/arm/midr.c | 87 ++++++++++++++++++++++++++++++++++++++++++-- src/common/args.c | 6 +++ src/common/ascii.h | 4 +- src/common/cpu.h | 9 ++++- src/common/main.c | 9 +++-- src/common/printer.c | 28 ++++++++++++-- src/common/printer.h | 1 + 7 files changed, 130 insertions(+), 14 deletions(-) diff --git a/src/arm/midr.c b/src/arm/midr.c index c1e0570..686e7de 100644 --- a/src/arm/midr.c +++ b/src/arm/midr.c @@ -9,13 +9,92 @@ #define STRING_UNKNOWN "Unknown" +int count_distinct(uint32_t* arr, int n) { + int res = 1; + + for (int i = 1; i < n; i++) { + int j = 0; + for (j = 0; j < i; j++) { + if (arr[i] == arr[j]) + break; + } + + if (i == j) + res++; + } + return res; +} + +uint32_t fill_ids_from_midr(uint32_t* midr_array, uint32_t* ids_array, int len) { + uint32_t latest_id = 0; + bool found; + ids_array[0] = latest_id; + + for (int i = 1; i < len; i++) { + int j = 0; + found = false; + + for (j = 0; j < len && !found; j++) { + if (i != j && midr_array[i] == midr_array[j]) { + if(j > i) { + latest_id++; + ids_array[i] = latest_id; + } + else { + ids_array[i] = ids_array[j]; + } + found = true; + } + } + if(!found) { + latest_id++; + ids_array[i] = latest_id; + } + } + + return latest_id+1; +} + +VENDOR get_vendor_from_midr(uint32_t midr) { + return CPU_VENDOR_ARM; +} + +char* get_name_from_midr(uint32_t midr) { + char* name = malloc(sizeof(char) * CPU_NAME_MAX_LENGTH); + strcpy(name, "Unknown"); + return name; +} + struct cpuInfo* get_cpu_info() { struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo)); + cpu->next_cpu = NULL; + + int ncores = get_ncores_from_cpuinfo(); + uint32_t* midr_array = malloc(sizeof(uint32_t) * ncores); + uint32_t* ids_array = malloc(sizeof(uint32_t) * ncores); + + for(int i=0; i < ncores; i++) { + midr_array[i] = get_midr_from_cpuinfo(i); + } + uint32_t sockets = fill_ids_from_midr(midr_array, ids_array, ncores); + + struct cpuInfo* ptr = cpu; + int midr_idx = 0; + int tmp_midr_idx = 0; + for(uint32_t i=0; i < sockets; i++) { + if(i > 0) { + ptr->next_cpu = malloc(sizeof(struct cpuInfo)); + ptr = ptr->next_cpu; + + tmp_midr_idx = midr_idx; + while(midr_array[midr_idx] == midr_array[tmp_midr_idx]) tmp_midr_idx++; + midr_idx = tmp_midr_idx; + } + ptr->midr = midr_array[midr_idx]; + ptr->cpu_vendor = get_vendor_from_midr(ptr->midr); + ptr->cpu_name = get_name_from_midr(ptr->midr); + } - cpu->midr = get_midr_from_cpuinfo(0); - 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; diff --git a/src/common/args.c b/src/common/args.c index afc6b62..073c636 100644 --- a/src/common/args.c +++ b/src/common/args.c @@ -7,6 +7,7 @@ #define COLOR_STR_INTEL "intel" #define COLOR_STR_AMD "amd" +#define COLOR_STR_ARM "arm" static const char *SYTLES_STR_LIST[] = { [STYLE_EMPTY] = NULL, @@ -107,6 +108,11 @@ bool parse_color(char* optarg, struct colors** cs) { strcpy(str_to_parse, COLOR_DEFAULT_AMD); free_ptr = true; } + else if(strcmp(optarg, COLOR_STR_ARM) == 0) { + str_to_parse = malloc(sizeof(char) * 46); + strcpy(str_to_parse, COLOR_DEFAULT_ARM); + free_ptr = true; + } else { str_to_parse = optarg; free_ptr = false; diff --git a/src/common/ascii.h b/src/common/ascii.h index 6b0c05e..1dec889 100644 --- a/src/common/ascii.h +++ b/src/common/ascii.h @@ -52,14 +52,14 @@ \ \ \ - ####### #### ########## #### ###### ######## \ + ############ ########## #### ###### ######## \ ############### ######### ####################### \ #### #### #### ##### ####### ##### \ #### #### #### #### ##### #### \ #### #### #### #### #### #### \ #### ##### #### #### #### #### \ ############### #### #### #### #### \ - ######## #### ### #### #### #### \ + ######## #### #### #### #### #### \ \ \ \ diff --git a/src/common/cpu.h b/src/common/cpu.h index 0b1b0ed..c60b741 100644 --- a/src/common/cpu.h +++ b/src/common/cpu.h @@ -68,13 +68,20 @@ struct cpuInfo { uint32_t maxLevels; // Max cpuids extended levels uint32_t maxExtendedLevels; -#else +#elif ARCH_ARM // Main ID register uint32_t midr; #endif struct uarch* arch; struct hypervisor* hv; + +#ifdef ARCH_ARM + // If SoC contains more than one CPU and they + // are different, the others will be stored in + // the next_cpu field + struct cpuInfo* next_cpu; +#endif }; struct cach { diff --git a/src/common/main.c b/src/common/main.c index fd7a37e..64a850d 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -13,7 +13,7 @@ #include "../arm/midr.h" #endif -static const char* VERSION = "0.81"; +static const char* VERSION = "0.82"; void print_help(char *argv[]) { #ifdef ARCH_X86 @@ -25,9 +25,10 @@ void print_help(char *argv[]) { 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\ - * \"amd\": Use amd default color scheme \n\ - * custom: If color do not match \"intel\" or \"amd\", a custom scheme can be specified: \n\ + * \"intel\": Use Intel default color scheme \n\ + * \"amd\": Use AMD default color scheme \n\ + * \"arm\": Use ARM default color scheme \n\ + * custom: If color argument do not match \"Intel\", \"AMD\" or \"ARM\", a custom scheme can be specified: \n\ 4 colors must be given in RGB with the format: R,G,B:R,G,B:... \n\ These colors correspond to CPU art color (2 colors) and for the text colors (following 2) \n\ For example: --color 239,90,45:210,200,200:100,200,45:0,200,200 \n\n\ diff --git a/src/common/printer.c b/src/common/printer.c index 57c2033..d2cfeb6 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -28,12 +28,21 @@ #define COL_INTEL_FANCY_4 "\x1b[37;1m" #define COL_INTEL_RETRO_1 "\x1b[36;1m" #define COL_INTEL_RETRO_2 "\x1b[37;1m" + #define COL_AMD_FANCY_1 "\x1b[47;1m" #define COL_AMD_FANCY_2 "\x1b[42;1m" #define COL_AMD_FANCY_3 "\x1b[37;1m" #define COL_AMD_FANCY_4 "\x1b[32;1m" #define COL_AMD_RETRO_1 "\x1b[37;1m" #define COL_AMD_RETRO_2 "\x1b[32;1m" + +#define COL_ARM_FANCY_1 "\x1b[46;1m" +#define COL_ARM_FANCY_2 "\x1b[46;1m" +#define COL_ARM_FANCY_3 "\x1b[37;1m" +#define COL_ARM_FANCY_4 "\x1b[36;1m" +#define COL_ARM_RETRO_1 "\x1b[36;1m" +#define COL_ARM_RETRO_2 "\x1b[37;1m" + #define COL_UNKNOWN_FANCY_1 "\x1b[47;1m" #define COL_UNKNOWN_FANCY_2 "\x1b[47;1m" #define COL_UNKNOWN_FANCY_3 "\x1b[37;1m" @@ -182,6 +191,17 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) { COL_RETRO_4 = COL_AMD_RETRO_2; art->ascii_chars[0] = '@'; } + else if(art->vendor == CPU_VENDOR_ARM) { + COL_FANCY_1 = COL_ARM_FANCY_1; + COL_FANCY_2 = COL_ARM_FANCY_2; + COL_FANCY_3 = COL_ARM_FANCY_3; + COL_FANCY_4 = COL_ARM_FANCY_4; + COL_RETRO_1 = COL_ARM_RETRO_1; + COL_RETRO_2 = COL_ARM_RETRO_2; + COL_RETRO_3 = COL_ARM_RETRO_1; + COL_RETRO_4 = COL_ARM_RETRO_2; + art->ascii_chars[0] = '#'; + } else { COL_FANCY_1 = COL_UNKNOWN_FANCY_1; COL_FANCY_2 = COL_UNKNOWN_FANCY_2; @@ -190,7 +210,7 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) { COL_RETRO_1 = COL_UNKNOWN_RETRO; COL_RETRO_2 = COL_UNKNOWN_RETRO; COL_RETRO_3 = COL_UNKNOWN_RETRO; - COL_RETRO_4 = COL_UNKNOWN_RETRO; + COL_RETRO_4 = COL_UNKNOWN_RETRO; art->ascii_chars[0] = '#'; } art->ascii_chars[1] = '#'; @@ -198,14 +218,14 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) { #ifdef _WIN32 HANDLE std_handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD console_mode; - + // Attempt to enable the VT100-processing flag GetConsoleMode(std_handle, &console_mode); SetConsoleMode(std_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); // Get the console mode flag again, to see if it successfully enabled it GetConsoleMode(std_handle, &console_mode); #endif - + if(style == STYLE_EMPTY) { #ifdef _WIN32 // Use fancy style if VT100-processing is enabled, @@ -276,6 +296,8 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) { strcpy(tmp, INTEL_ASCII); else if(art->vendor == CPU_VENDOR_AMD) strcpy(tmp, AMD_ASCII); + else if(art->vendor == CPU_VENDOR_ARM) + strcpy(tmp, ARM_ASCII); else strcpy(tmp, UNKNOWN_ASCII); diff --git a/src/common/printer.h b/src/common/printer.h index 03a57e7..8e32d49 100644 --- a/src/common/printer.h +++ b/src/common/printer.h @@ -13,6 +13,7 @@ typedef int STYLE; #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_ARM "0,145,189:0,145,189:240,240,240:0,145,189" #ifdef ARCH_X86 void print_levels(struct cpuInfo* cpu);