From 7ad484b938eb9daed912a38dd9c62e9faaddef9a Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sun, 15 Aug 2021 23:23:36 +0200 Subject: [PATCH] [v0.03] Add printer backend from cpufetch (still printing CPU logo) --- Makefile | 4 +- src/common/args.cpp | 19 ++ src/common/args.hpp | 20 ++ src/common/ascii.hpp | 89 +++++++++ src/common/global.cpp | 4 + src/common/global.hpp | 1 + src/common/gpu.cpp | 4 + src/common/gpu.hpp | 1 + src/common/main.cpp | 25 +-- src/common/printer.cpp | 432 +++++++++++++++++++++++++++++++++++++++++ src/common/printer.hpp | 13 ++ src/cuda/cuda.cpp | 21 +- src/cuda/cuda.hpp | 6 +- 13 files changed, 609 insertions(+), 30 deletions(-) create mode 100644 src/common/ascii.hpp create mode 100644 src/common/printer.cpp create mode 100644 src/common/printer.hpp diff --git a/Makefile b/Makefile index 1efec45..67631c4 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ PREFIX ?= /usr SRC_COMMON=src/common/ SRC_CUDA=src/cuda/ -COMMON_SRC = $(SRC_COMMON)main.cpp $(SRC_COMMON)gpu.cpp $(SRC_COMMON)args.cpp $(SRC_COMMON)global.cpp -COMMON_HDR = $(SRC_COMMON)gpu.hpp $(SRC_COMMON)args.hpp $(SRC_COMMON)global.hpp +COMMON_SRC = $(SRC_COMMON)main.cpp $(SRC_COMMON)gpu.cpp $(SRC_COMMON)args.cpp $(SRC_COMMON)global.cpp $(SRC_COMMON)printer.cpp +COMMON_HDR = $(SRC_COMMON)ascii.hpp $(SRC_COMMON)gpu.hpp $(SRC_COMMON)args.hpp $(SRC_COMMON)global.hpp $(SRC_COMMON)printer.hpp CUDA_SRC = $(SRC_CUDA)cuda.cpp $(SRC_CUDA)uarch.cpp $(SRC_CUDA)pci.cpp $(SRC_CUDA)nvmlb.cpp CUDA_HDR = $(SRC_CUDA)cuda.hpp $(SRC_CUDA)uarch.hpp $(SRC_CUDA)pci.hpp $(SRC_CUDA)nvmlb.hpp $(SRC_CUDA)chips.hpp diff --git a/src/common/args.cpp b/src/common/args.cpp index 7022a14..bea2855 100644 --- a/src/common/args.cpp +++ b/src/common/args.cpp @@ -6,9 +6,13 @@ #include "args.hpp" #include "global.hpp" +#define NUM_COLORS 4 + struct args_struct { bool help_flag; bool version_flag; + STYLE style; + struct color** colors; }; const char args_chr[] = { @@ -23,6 +27,14 @@ const char *args_str[] = { static struct args_struct args; +STYLE get_style() { + return args.style; +} + +struct color** get_colors() { + return args.colors; +} + bool show_help() { return args.help_flag; } @@ -97,3 +109,10 @@ bool parse_args(int argc, char* argv[]) { return true; } + +void free_colors_struct(struct color** cs) { + for(int i=0; i < NUM_COLORS; i++) { + free(cs[i]); + } + free(cs); +} diff --git a/src/common/args.hpp b/src/common/args.hpp index f83c0da..a8be61f 100644 --- a/src/common/args.hpp +++ b/src/common/args.hpp @@ -3,6 +3,21 @@ #include +struct color { + int32_t R; + int32_t G; + int32_t B; +}; + +enum { + STYLE_EMPTY, + STYLE_FANCY, + STYLE_WILD, + STYLE_RETRO, + STYLE_LEGACY, + STYLE_INVALID +}; + enum { ARG_HELP, ARG_VERSION @@ -11,9 +26,14 @@ enum { extern const char args_chr[]; extern const char *args_str[]; +#include "printer.hpp" + int max_arg_str_length(); bool parse_args(int argc, char* argv[]); bool show_help(); bool show_version(); +void free_colors_struct(struct color** cs); +struct color** get_colors(); +STYLE get_style(); #endif diff --git a/src/common/ascii.hpp b/src/common/ascii.hpp new file mode 100644 index 0000000..fabfef8 --- /dev/null +++ b/src/common/ascii.hpp @@ -0,0 +1,89 @@ +#ifndef __ASCII__ +#define __ASCII__ + +#define COLOR_NONE "" +#define COLOR_FG_BLACK "\x1b[30;1m" +#define COLOR_FG_RED "\x1b[31;1m" +#define COLOR_FG_GREEN "\x1b[32;1m" +#define COLOR_FG_YELLOW "\x1b[33;1m" +#define COLOR_FG_BLUE "\x1b[34;1m" +#define COLOR_FG_MAGENTA "\x1b[35;1m" +#define COLOR_FG_CYAN "\x1b[36;1m" +#define COLOR_FG_WHITE "\x1b[37;1m" +#define COLOR_BG_BLACK "\x1b[40;1m" +#define COLOR_BG_RED "\x1b[41;1m" +#define COLOR_BG_GREEN "\x1b[42;1m" +#define COLOR_BG_YELLOW "\x1b[43;1m" +#define COLOR_BG_BLUE "\x1b[44;1m" +#define COLOR_BG_MAGENTA "\x1b[45;1m" +#define COLOR_BG_CYAN "\x1b[46;1m" +#define COLOR_BG_WHITE "\x1b[47;1m" +#define COLOR_FG_B_BLACK "\x1b[90;1m" +#define COLOR_FG_B_RED "\x1b[91;1m" +#define COLOR_FG_B_GREEN "\x1b[92;1m" +#define COLOR_FG_B_YELLOW "\x1b[93;1m" +#define COLOR_FG_B_BLUE "\x1b[94;1m" +#define COLOR_FG_B_MAGENTA "\x1b[95;1m" +#define COLOR_FG_B_CYAN "\x1b[96;1m" +#define COLOR_FG_B_WHITE "\x1b[97;1m" +#define COLOR_RESET "\x1b[m" + +struct ascii_logo { + char* art; + uint32_t width; + uint32_t height; + bool replace_blocks; + char color_ascii[8][100]; + char color_text[2][100]; +}; + +// SHORT LOGOS // +#define ASCII_NVIDIA \ +"$C1 .#################. \ +$C1 .#### ####. \ +$C1 .## ### \ +$C1 ## :## ### \ +$C1 # ## :## ## \ +$C1 ## ## ######. #### ###### :## ## \ +$C1 ## ## ##: ##: ## ## ### :## ### \ +$C1## ## ##: ##: ## :######## :## ## \ +$C1## ## ##: ##: ## ##. . :## #### \ +$C1## # ##: ##: #### #####: ## \ +$C1 ## \ +$C1 ###. ..o####. \ +$C1 ######oo... ..oo####### \ +$C1 o###############o " + +// LONG LOGOS // +#define ASCII_NVIDIA_L \ +"$C1 ###############@ \ +$C1 ######@ ######@ \ +$C1 ###@ ###@ \ +$C1 ##@ ###@ \ +$C1 ##@ ##@ \ +$C1 ##@ ##@ \ +$C1 @ ##@ ##@ ##@ \ +$C1 #@ ##@ ########@ #####@ #####@ ##@ ##@ \ +$C1 #@ ##@ ##@ ##@ ##@ ###@ ###@ ##@ ##@ \ +$C1 #@ ##@ ##@ ##@ ##@ ##@ ##@ ##@ ##@ \ +$C1 #@ ##@ ##@ ##@ ##@ #########@ ##@ ###@ \ +$C1 #@ ##@ ##@ ##@ ##@ ##@ ##@ ####@ \ +$C1 #@ #@ ##@ ##@ ####@ ########@ #@ ##@ \ +$C1 ##@ \ +$C1 ##@ \ +$C1 ###@ ###@ \ +$C1 ####@ #########@ \ +$C1 #########@ ###############@ \ +$C1 ##############################@ " + +typedef struct ascii_logo asciiL; + +// ----------------------------------------------------------------------------------------------------- +// | LOGO | W | H | REPLACE | COLORS LOGO (>0 && <10) | COLORS TEXT (=2) | +// ----------------------------------------------------------------------------------------------------- +asciiL logo_nvidia = { ASCII_NVIDIA, 48, 14, false, {COLOR_FG_CYAN}, {COLOR_FG_CYAN, COLOR_FG_WHITE} }; +// Long variants | --------------------------------------------------------------------------------------------------| +asciiL logo_nvidia_l = { ASCII_NVIDIA_L, 62, 19, true, {COLOR_BG_CYAN, COLOR_BG_WHITE}, {COLOR_FG_CYAN, COLOR_FG_WHITE} }; +asciiL logo_unknown = { NULL, 0, 0, false, {COLOR_NONE}, {COLOR_NONE, COLOR_NONE} }; + +#endif diff --git a/src/common/global.cpp b/src/common/global.cpp index e1c3108..81223d0 100644 --- a/src/common/global.cpp +++ b/src/common/global.cpp @@ -72,6 +72,10 @@ int max(int a, int b) { return a > b ? a : b; } +int min(int a, int b) { + return a < b ? a : b; +} + void* emalloc(size_t size) { void* ptr = malloc(size); diff --git a/src/common/global.hpp b/src/common/global.hpp index c6c328a..db250d8 100644 --- a/src/common/global.hpp +++ b/src/common/global.hpp @@ -12,6 +12,7 @@ void printWarn(const char *fmt, ...); void printErr(const char *fmt, ...); void printBug(const char *fmt, ...); int max(int a, int b); +int min(int a, int b); void* emalloc(size_t size); void* ecalloc(size_t nmemb, size_t size); diff --git a/src/common/gpu.cpp b/src/common/gpu.cpp index ce171e7..765e29a 100644 --- a/src/common/gpu.cpp +++ b/src/common/gpu.cpp @@ -27,6 +27,10 @@ static const char *memtype_str[] = { /*[MEMTYPE_HBM2] = */ "HBM2" }; +VENDOR get_gpu_vendor(struct gpu_info* gpu) { + return gpu->vendor; +} + int32_t get_value_as_smallest_unit(char ** str, uint64_t value) { int32_t ret; int max_len = 10; // Max is 8 for digits, 2 for units diff --git a/src/common/gpu.hpp b/src/common/gpu.hpp index 168d5ac..84f0c92 100644 --- a/src/common/gpu.hpp +++ b/src/common/gpu.hpp @@ -64,6 +64,7 @@ struct gpu_info { int64_t peak_performance; }; +VENDOR get_gpu_vendor(struct gpu_info* gpu); char* get_str_gpu_name(struct gpu_info* gpu); char* get_str_freq(struct gpu_info* gpu); char* get_str_memory_size(struct gpu_info* gpu); diff --git a/src/common/main.cpp b/src/common/main.cpp index 9aa66dc..6e7a1bf 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -7,7 +7,7 @@ #include "../cuda/cuda.hpp" #include "../cuda/uarch.hpp" -static const char* VERSION = "0.02"; +static const char* VERSION = "0.03"; void print_help(char *argv[]) { const char **t = args_str; @@ -19,7 +19,7 @@ void print_help(char *argv[]) { printf("Options: \n"); printf(" -%c, --%s %*s Prints this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), ""); - printf(" -%c, --%s %*s Prints cpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), ""); + printf(" -%c, --%s %*s Prints gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), ""); printf("\nBUGS: \n"); printf(" Report bugs to https://github.com/Dr-Noob/gpufetch/issues\n"); @@ -60,21 +60,8 @@ int main(int argc, char* argv[]) { if(gpu == NULL) return EXIT_FAILURE; - printf("Name: %s\n", get_str_gpu_name(gpu)); - printf("Microarchitecture: %s\n", get_str_uarch(gpu->arch)); - printf("GPU chip: %s\n", get_str_chip(gpu->arch)); - printf("Compute Capability: %s\n", get_str_cc(gpu->arch)); - printf("Technology: %s\n", get_str_process(gpu->arch)); - printf("Max Frequency: %s\n", get_str_freq(gpu)); - printf("SM: %d\n", get_str_sm(gpu)); - printf("Cores/MP: %d\n", get_str_cores_sm(gpu)); - printf("CUDA cores: %d\n", get_str_cuda_cores(gpu)); - printf("Memory size: %s\n", get_str_memory_size(gpu)); - printf("Memory type: %s\n", get_str_memory_type(gpu)); - printf("Memory frequency: %s\n", get_str_memory_clock(gpu)); - printf("Bus width: %s\n", get_str_bus_width(gpu)); - printf("L2 size: %s\n", get_str_l2(gpu)); - printf("Peak performance: %s\n", get_str_peak_performance(gpu)); - - return EXIT_FAILURE; + if(print_gpufetch(gpu, get_style(), get_colors())) + return EXIT_SUCCESS; + else + return EXIT_FAILURE; } diff --git a/src/common/printer.cpp b/src/common/printer.cpp new file mode 100644 index 0000000..b67da3b --- /dev/null +++ b/src/common/printer.cpp @@ -0,0 +1,432 @@ +#include +#include +#include +#include +#include + +#include "printer.hpp" +#include "ascii.hpp" +#include "../common/global.hpp" +#include "../common/gpu.hpp" + +#include "../cuda/cuda.hpp" +#include "../cuda/uarch.hpp" + +#ifdef _WIN32 + #define NOMINMAX + #include +#else + #ifdef __linux__ + #ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309L + #endif + #endif + #include + #include +#endif + +#define max(a,b) (((a)>(b))?(a):(b)) +#define MAX_ATTRIBUTES 100 + +enum { + ATTRIBUTE_NAME, + ATTRIBUTE_CHIP, + ATTRIBUTE_UARCH, + ATTRIBUTE_TECHNOLOGY, + ATTRIBUTE_FREQUENCY, + ATTRIBUTE_STREAMINGMP, + ATTRIBUTE_CORESPERMP, + ATTRIBUTE_CUDA_CORES, + ATTRIBUTE_L2, + ATTRIBUTE_MEMORY, + ATTRIBUTE_MEMORY_FREQ, + ATTRIBUTE_BUS_WIDTH, + ATTRIBUTE_PEAK +}; + +static const char* ATTRIBUTE_FIELDS [] = { + "Name:", + "GPU processor:", + "Microarchitecture:", + "Technology:", + "Max Frequency:", + "SMs:", + "Cores/SM:", + "CUDA cores:", + "L2 Size:", + "Memory:", + "Memory frequency:", + "Bus width:", + "Peak Performance:", +}; + +static const char* ATTRIBUTE_FIELDS_SHORT [] = { + "Name:", + "Processor:", + "uArch:", + "Technology:", + "Max Freq.:", + "SMs:", + "Cores/SM:", + "CUDA cores:", + "L2 Size:", + "Memory:", + "Memory freq.:", + "Bus width:", + "Peak Perf.:", +}; + +struct terminal { + int w; + int h; +}; + +struct attribute { + int type; + char* value; +}; + +struct ascii { + struct ascii_logo* art; + char reset[100]; + struct attribute** attributes; + uint32_t n_attributes_set; + uint32_t additional_spaces; + VENDOR vendor; + STYLE style; +}; + +void setAttribute(struct ascii* art, int type, char* value) { + art->attributes[art->n_attributes_set]->value = value; + art->attributes[art->n_attributes_set]->type = type; + art->n_attributes_set++; + + if(art->n_attributes_set > MAX_ATTRIBUTES) { + printBug("Set %d attributes, while max value is %d!", art->n_attributes_set, MAX_ATTRIBUTES); + } +} + +char* rgb_to_ansi(struct color* c, bool background, bool bold) { + char* str = (char *) emalloc(sizeof(char) * 100); + if(background) { + snprintf(str, 44, "\x1b[48;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + } + else { + if(bold) + snprintf(str, 48, "\x1b[1m\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + else + snprintf(str, 44, "\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + } + + return str; +} + +struct ascii* set_ascii(VENDOR vendor, STYLE style) { + struct ascii* art = (struct ascii*) emalloc(sizeof(struct ascii)); + + art->n_attributes_set = 0; + art->additional_spaces = 0; + art->vendor = vendor; + art->attributes = (struct attribute**) emalloc(sizeof(struct attribute *) * MAX_ATTRIBUTES); + for(uint32_t i=0; i < MAX_ATTRIBUTES; i++) { + art->attributes[i] = (struct attribute*) emalloc(sizeof(struct attribute)); + art->attributes[i]->type = 0; + art->attributes[i]->value = NULL; + } + + #ifdef _WIN32 + // Old Windows do not define the flag + #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING + #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 + #endif + + 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, + // or legacy style in other case + art->style = (console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) ? STYLE_FANCY : STYLE_LEGACY; + #else + art->style = STYLE_FANCY; + #endif + } + else { + art->style = style; + } + + return art; +} + +void parse_print_color(struct ascii* art, uint32_t* logo_pos) { + struct ascii_logo* logo = art->art; + char color_id_str = logo->art[*logo_pos + 2]; + + if(color_id_str == 'R') { + printf("%s", art->reset); + } + else { + int color_id = (color_id_str - '0') - 1; + printf("%s", logo->color_ascii[color_id]); + } + + *logo_pos += 3; +} + +bool ascii_fits_screen(int termw, struct ascii_logo logo, int lf) { + return termw - ((int) logo.width + lf) >= 0; +} + +// TODO: Instead of using a function to do so, change ascii.h +// and store an color ID that is converted to BG or FG depending +// on logo->replace_blocks +void replace_bgbyfg_color(struct ascii_logo* logo) { + // Replace background by foreground color + for(int i=0; i < 2; i++) { + if(logo->color_ascii[i] == NULL) break; + + if(strcmp(logo->color_ascii[i], COLOR_BG_BLACK) == 0) strcpy(logo->color_ascii[i], COLOR_FG_BLACK); + else if(strcmp(logo->color_ascii[i], COLOR_BG_RED) == 0) strcpy(logo->color_ascii[i], COLOR_FG_RED); + else if(strcmp(logo->color_ascii[i], COLOR_BG_GREEN) == 0) strcpy(logo->color_ascii[i], COLOR_FG_GREEN); + else if(strcmp(logo->color_ascii[i], COLOR_BG_YELLOW) == 0) strcpy(logo->color_ascii[i], COLOR_FG_YELLOW); + else if(strcmp(logo->color_ascii[i], COLOR_BG_BLUE) == 0) strcpy(logo->color_ascii[i], COLOR_FG_BLUE); + else if(strcmp(logo->color_ascii[i], COLOR_BG_MAGENTA) == 0) strcpy(logo->color_ascii[i], COLOR_FG_MAGENTA); + else if(strcmp(logo->color_ascii[i], COLOR_BG_CYAN) == 0) strcpy(logo->color_ascii[i], COLOR_FG_CYAN); + else if(strcmp(logo->color_ascii[i], COLOR_BG_WHITE) == 0) strcpy(logo->color_ascii[i], COLOR_FG_WHITE); + } +} + +void choose_ascii_art(struct ascii* art, struct color** cs, struct terminal* term, int lf) { + if(art->vendor == GPU_VENDOR_NVIDIA) { + if(term != NULL && ascii_fits_screen(term->w, logo_nvidia_l, lf)) + art->art = &logo_nvidia_l; + else + art->art = &logo_nvidia; + } + else { + art->art = &logo_unknown; + } + + // 2. Choose colors + struct ascii_logo* logo = art->art; + + switch(art->style) { + case STYLE_LEGACY: + logo->replace_blocks = false; + strcpy(logo->color_text[0], COLOR_NONE); + strcpy(logo->color_text[1], COLOR_NONE); + strcpy(logo->color_ascii[0], COLOR_NONE); + strcpy(logo->color_ascii[1], COLOR_NONE); + art->reset[0] = '\0'; + break; + case STYLE_RETRO: + logo->replace_blocks = false; + replace_bgbyfg_color(logo); + // fall through + case STYLE_FANCY: + if(cs != NULL) { + strcpy(logo->color_text[0], rgb_to_ansi(cs[2], false, true)); + strcpy(logo->color_text[1], rgb_to_ansi(cs[3], false, true)); + strcpy(logo->color_ascii[0], rgb_to_ansi(cs[0], logo->replace_blocks, true)); + strcpy(logo->color_ascii[1], rgb_to_ansi(cs[1], logo->replace_blocks, true)); + } + strcpy(art->reset, COLOR_RESET); + break; + case STYLE_INVALID: + default: + printBug("Found invalid style (%d)", art->style); + } +} + +uint32_t longest_attribute_length(struct ascii* art, const char** attribute_fields) { + uint32_t max = 0; + uint64_t len = 0; + + for(uint32_t i=0; i < art->n_attributes_set; i++) { + if(art->attributes[i]->value != NULL) { + len = strlen(attribute_fields[art->attributes[i]->type]); + if(len > max) max = len; + } + } + + return max; +} + +uint32_t longest_field_length(struct ascii* art, int la) { + uint32_t max = 0; + uint64_t len = 0; + + for(uint32_t i=0; i < art->n_attributes_set; i++) { + if(art->attributes[i]->value != NULL) { + // longest attribute + 1 (space) + longest value + len = la + 1 + strlen(art->attributes[i]->value); + + if(len > max) max = len; + } + } + + return max; +} + +void print_ascii_generic(struct ascii* art, uint32_t la, int32_t text_space, const char** attribute_fields) { + struct ascii_logo* logo = art->art; + int attr_to_print = 0; + int attr_type; + char* attr_value; + int32_t current_space; + int32_t space_right; + int32_t space_up = ((int)logo->height - (int)art->n_attributes_set)/2; + int32_t space_down = (int)logo->height - (int)art->n_attributes_set - (int)space_up; + uint32_t logo_pos = 0; + int32_t iters = max(logo->height, art->n_attributes_set); + + printf("\n"); + for(int32_t n=0; n < iters; n++) { + // 1. Print logo + if(space_up > 0 || (space_up + n >= 0 && space_up + n < (int)logo->height)) { + for(uint32_t i=0; i < logo->width; i++) { + if(logo->art[logo_pos] == '$') { + if(logo->replace_blocks) logo_pos += 3; + else parse_print_color(art, &logo_pos); + } + if(logo->replace_blocks && logo->art[logo_pos] != ' ') { + if(logo->art[logo_pos] == '#') printf("%s%c%s", logo->color_ascii[0], ' ', art->reset); + else if(logo->art[logo_pos] == '@') printf("%s%c%s", logo->color_ascii[1], ' ', art->reset); + else printf("%c", logo->art[logo_pos]); + } + else + printf("%c", logo->art[logo_pos]); + + logo_pos++; + } + printf("%s", art->reset); + } + else { + // If logo should not be printed, fill with spaces + printf("%*c", logo->width, ' '); + } + + // 2. Print text + if(space_up < 0 || (n > space_up-1 && n < (int)logo->height - space_down)) { + attr_type = art->attributes[attr_to_print]->type; + attr_value = art->attributes[attr_to_print]->value; + attr_to_print++; + + space_right = 1 + (la - strlen(attribute_fields[attr_type])); + current_space = max(0, text_space); + + printf("%s%.*s%s", logo->color_text[0], current_space, attribute_fields[attr_type], art->reset); + current_space = max(0, current_space - (int) strlen(attribute_fields[attr_type])); + printf("%*s", min(current_space, space_right), ""); + current_space = max(0, current_space - min(current_space, space_right)); + printf("%s%.*s%s", logo->color_text[1], current_space, attr_value, art->reset); + printf("\n"); + } + else printf("\n"); + } + printf("\n"); +} + +bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struct terminal* term) { + struct ascii* art = set_ascii(get_gpu_vendor(gpu), s); + + if(art == NULL) + return false; + + char* gpu_name = get_str_gpu_name(gpu); + char* gpu_chip = get_str_chip(gpu->arch); + char* uarch = get_str_uarch(gpu->arch); + char* manufacturing_process = get_str_process(gpu->arch); + char* sms = get_str_sm(gpu); + char* corespersm = get_str_cores_sm(gpu); + char* cores = get_str_cuda_cores(gpu); + char* max_frequency = get_str_freq(gpu); + char* l2 = get_str_l2(gpu); + char* mem_size = get_str_memory_size(gpu); + char* mem_type = get_str_memory_type(gpu); + char* mem_freq = get_str_memory_clock(gpu); + char* bus_width = get_str_bus_width(gpu); + char* pp = get_str_peak_performance(gpu); + + setAttribute(art, ATTRIBUTE_NAME, gpu_name); + setAttribute(art, ATTRIBUTE_CHIP, gpu_chip); + setAttribute(art, ATTRIBUTE_UARCH, uarch); + setAttribute(art, ATTRIBUTE_TECHNOLOGY, manufacturing_process); + setAttribute(art, ATTRIBUTE_FREQUENCY, max_frequency); + setAttribute(art, ATTRIBUTE_STREAMINGMP, sms); + setAttribute(art, ATTRIBUTE_CORESPERMP, corespersm); + setAttribute(art, ATTRIBUTE_CUDA_CORES, cores); + setAttribute(art, ATTRIBUTE_MEMORY, mem_size); + setAttribute(art, ATTRIBUTE_MEMORY_FREQ, mem_freq); + setAttribute(art, ATTRIBUTE_BUS_WIDTH, bus_width); + setAttribute(art, ATTRIBUTE_L2, l2); + setAttribute(art, ATTRIBUTE_PEAK, pp); + + const char** attribute_fields = ATTRIBUTE_FIELDS; + uint32_t longest_attribute = longest_attribute_length(art, attribute_fields); + uint32_t longest_field = longest_field_length(art, longest_attribute); + choose_ascii_art(art, cs, term, longest_field); + + if(!ascii_fits_screen(term->w, *art->art, longest_field)) { + // Despite of choosing the smallest logo, the output does not fit + // Choose the shorter field names and recalculate the longest attr + attribute_fields = ATTRIBUTE_FIELDS_SHORT; + longest_attribute = longest_attribute_length(art, attribute_fields); + } + + print_ascii_generic(art, longest_attribute, term->w - art->art->width, attribute_fields); + + free(manufacturing_process); + free(max_frequency); + free(l2); + free(pp); + + free(art->attributes); + free(art); + + /* if(cs != NULL) free_colors_struct(cs); + free_cache_struct(cpu->cach); + free_topo_struct(cpu->topo); + free_freq_struct(cpu->freq); + free_cpuinfo_struct(cpu);*/ + + return true; +} + +struct terminal* get_terminal_size() { + struct terminal* term = (struct terminal*) emalloc(sizeof(struct terminal)); + +#ifdef _WIN32 + CONSOLE_SCREEN_BUFFER_INFO csbi; + if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) == 0) { + printWarn("GetConsoleScreenBufferInfo failed"); + return NULL; + } + term->w = csbi.srWindow.Right - csbi.srWindow.Left + 1; + term->h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; +#else + struct winsize w; + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { + printErr("ioctl: %s", strerror(errno)); + return NULL; + } + term->h = w.ws_row; + term->w = w.ws_col; +#endif + + return term; +} + +bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs) { + struct terminal* term = get_terminal_size(); + + return print_gpufetch_cuda(gpu, s, cs, term); +} diff --git a/src/common/printer.hpp b/src/common/printer.hpp new file mode 100644 index 0000000..599de95 --- /dev/null +++ b/src/common/printer.hpp @@ -0,0 +1,13 @@ +#ifndef __PRINTER__ +#define __PRINTER__ + +typedef int STYLE; + +#include "args.hpp" +#include "../cuda/cuda.hpp" + +#define COLOR_DEFAULT_NVIDIA "15,125,194:230,230,230:40,150,220:230,230,230" + +bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs); + +#endif diff --git a/src/cuda/cuda.cpp b/src/cuda/cuda.cpp index a21be6d..7c89e14 100644 --- a/src/cuda/cuda.cpp +++ b/src/cuda/cuda.cpp @@ -95,15 +95,24 @@ struct gpu_info* get_gpu_info() { return gpu; } -int32_t get_str_sm(struct gpu_info* gpu) { - return gpu->topo->streaming_mp; +char* get_str_sm(struct gpu_info* gpu) { + uint32_t max_size = 10; + char* dummy = (char *) ecalloc(max_size, sizeof(char)); + snprintf(dummy, max_size, "%d", gpu->topo->streaming_mp); + return dummy; } -int32_t get_str_cores_sm(struct gpu_info* gpu) { - return gpu->topo->cores_per_mp; +char* get_str_cores_sm(struct gpu_info* gpu) { + uint32_t max_size = 10; + char* dummy = (char *) ecalloc(max_size, sizeof(char)); + snprintf(dummy, max_size, "%d", gpu->topo->cores_per_mp); + return dummy; } -int32_t get_str_cuda_cores(struct gpu_info* gpu) { - return gpu->topo->cuda_cores; +char* get_str_cuda_cores(struct gpu_info* gpu) { + uint32_t max_size = 10; + char* dummy = (char *) ecalloc(max_size, sizeof(char)); + snprintf(dummy, max_size, "%d", gpu->topo->cuda_cores); + return dummy; } diff --git a/src/cuda/cuda.hpp b/src/cuda/cuda.hpp index f77329e..72ea92a 100644 --- a/src/cuda/cuda.hpp +++ b/src/cuda/cuda.hpp @@ -4,8 +4,8 @@ #include "../common/gpu.hpp" struct gpu_info* get_gpu_info(); -int32_t get_str_sm(struct gpu_info* gpu); -int32_t get_str_cores_sm(struct gpu_info* gpu); -int32_t get_str_cuda_cores(struct gpu_info* gpu); +char* get_str_sm(struct gpu_info* gpu); +char* get_str_cores_sm(struct gpu_info* gpu); +char* get_str_cuda_cores(struct gpu_info* gpu); #endif