[v0.21] Add verbose option. Fix CUDA driver initialization message when verbose output is used

This commit is contained in:
Dr-Noob
2021-12-27 22:35:47 +01:00
parent 59f2715149
commit 8fbf97c47a
8 changed files with 35 additions and 7 deletions

View File

@@ -23,6 +23,7 @@
struct args_struct { struct args_struct {
bool help_flag; bool help_flag;
bool verbose_flag;
bool version_flag; bool version_flag;
bool list_gpus; bool list_gpus;
int gpu_idx; int gpu_idx;
@@ -38,6 +39,7 @@ const char args_chr[] = {
/* [ARG_GPU] = */ 'g', /* [ARG_GPU] = */ 'g',
/* [ARG_LIST] = */ 'l', /* [ARG_LIST] = */ 'l',
/* [ARG_HELP] = */ 'h', /* [ARG_HELP] = */ 'h',
/* [ARG_VERBOSE] = */ 'v',
/* [ARG_VERSION] = */ 'V', /* [ARG_VERSION] = */ 'V',
}; };
@@ -46,6 +48,7 @@ const char *args_str[] = {
/* [ARG_GPU] = */ "gpu", /* [ARG_GPU] = */ "gpu",
/* [ARG_LIST] = */ "list-gpus", /* [ARG_LIST] = */ "list-gpus",
/* [ARG_HELP] = */ "help", /* [ARG_HELP] = */ "help",
/* [ARG_VERBOSE] = */ "verbose",
/* [ARG_VERSION] = */ "version", /* [ARG_VERSION] = */ "version",
}; };
@@ -116,6 +119,10 @@ bool show_version() {
return args.version_flag; return args.version_flag;
} }
bool verbose_enabled() {
return args.verbose_flag;
}
int max_arg_str_length() { int max_arg_str_length() {
int max_len = -1; int max_len = -1;
int len = sizeof(args_str) / sizeof(args_str[0]); int len = sizeof(args_str) / sizeof(args_str[0]);
@@ -131,9 +138,9 @@ char* build_short_options() {
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1)); char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
memset(str, 0, sizeof(char) * (len*2 + 1)); memset(str, 0, sizeof(char) * (len*2 + 1));
sprintf(str, "%c:%c:%c%c%c", c[ARG_GPU], sprintf(str, "%c:%c:%c%c%c%c", c[ARG_GPU],
c[ARG_COLOR], c[ARG_HELP], c[ARG_LIST], c[ARG_COLOR], c[ARG_HELP], c[ARG_LIST],
c[ARG_VERSION]); c[ARG_VERBOSE], c[ARG_VERSION]);
return str; return str;
} }
@@ -208,6 +215,7 @@ bool parse_args(int argc, char* argv[]) {
{args_str[ARG_GPU], required_argument, 0, args_chr[ARG_GPU] }, {args_str[ARG_GPU], required_argument, 0, args_chr[ARG_GPU] },
{args_str[ARG_LIST], no_argument, 0, args_chr[ARG_LIST] }, {args_str[ARG_LIST], no_argument, 0, args_chr[ARG_LIST] },
{args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] }, {args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] },
{args_str[ARG_VERBOSE], no_argument, 0, args_chr[ARG_VERBOSE] },
{args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@@ -237,6 +245,9 @@ bool parse_args(int argc, char* argv[]) {
else if(opt == args_chr[ARG_HELP]) { else if(opt == args_chr[ARG_HELP]) {
args.help_flag = true; args.help_flag = true;
} }
else if(opt == args_chr[ARG_VERBOSE]) {
args.verbose_flag = true;
}
else if(opt == args_chr[ARG_VERSION]) { else if(opt == args_chr[ARG_VERSION]) {
args.version_flag = true; args.version_flag = true;
} }

View File

@@ -21,6 +21,7 @@ enum {
ARG_GPU, ARG_GPU,
ARG_LIST, ARG_LIST,
ARG_HELP, ARG_HELP,
ARG_VERBOSE,
ARG_VERSION ARG_VERSION
}; };
@@ -34,6 +35,7 @@ bool parse_args(int argc, char* argv[]);
bool show_help(); bool show_help();
bool list_gpus(); bool list_gpus();
bool show_version(); bool show_version();
bool verbose_enabled();
void free_colors_struct(struct color** cs); void free_colors_struct(struct color** cs);
int get_gpu_idx(); int get_gpu_idx();
struct color** get_colors(); struct color** get_colors();

View File

@@ -26,6 +26,7 @@ enum {
}; };
int LOG_LEVEL; int LOG_LEVEL;
bool clean;
void printWarn(const char *fmt, ...) { void printWarn(const char *fmt, ...) {
if(LOG_LEVEL == LOG_LEVEL_VERBOSE) { if(LOG_LEVEL == LOG_LEVEL_VERBOSE) {
@@ -37,6 +38,7 @@ void printWarn(const char *fmt, ...) {
va_end(args); va_end(args);
fprintf(stderr, BOLD "[WARNING]: " RESET "%s\n",buffer); fprintf(stderr, BOLD "[WARNING]: " RESET "%s\n",buffer);
delete [] buffer; delete [] buffer;
clean = false;
} }
} }
@@ -49,6 +51,7 @@ void printErr(const char *fmt, ...) {
va_end(args); va_end(args);
fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer); fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer);
delete [] buffer; delete [] buffer;
clean = false;
} }
void printBug(const char *fmt, ...) { void printBug(const char *fmt, ...) {
@@ -61,11 +64,17 @@ void printBug(const char *fmt, ...) {
fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer); fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer);
fprintf(stderr,"Please, create a new issue with this error message on https://github.com/Dr-Noob/gpufetch/issues\n"); fprintf(stderr,"Please, create a new issue with this error message on https://github.com/Dr-Noob/gpufetch/issues\n");
delete [] buffer; delete [] buffer;
clean = false;
} }
void set_log_level(bool verbose) { void set_log_level(bool verbose) {
if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE; if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE;
else LOG_LEVEL = LOG_LEVEL_NORMAL; else LOG_LEVEL = LOG_LEVEL_NORMAL;
clean = true;
}
bool clean_output() {
return clean;
} }
int max(int a, int b) { int max(int a, int b) {

View File

@@ -9,6 +9,7 @@ void set_log_level(bool verbose);
void printWarn(const char *fmt, ...); void printWarn(const char *fmt, ...);
void printErr(const char *fmt, ...); void printErr(const char *fmt, ...);
void printBug(const char *fmt, ...); void printBug(const char *fmt, ...);
bool clean_output();
int max(int a, int b); int max(int a, int b);
int min(int a, int b); int min(int a, int b);
void* emalloc(size_t size); void* emalloc(size_t size);

View File

@@ -22,6 +22,7 @@ void print_help(char *argv[]) {
printf(" -%c, --%s %*s Set the color scheme (by default, gpufetch uses the system color scheme) See COLORS section for a more detailed explanation\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), ""); printf(" -%c, --%s %*s Set the color scheme (by default, gpufetch uses the system color scheme) See COLORS section for a more detailed explanation\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), "");
printf(" -%c, --%s %*s List the available GPUs in the system\n", c[ARG_LIST], t[ARG_LIST], (int) (max_len-strlen(t[ARG_LIST])), ""); printf(" -%c, --%s %*s List the available GPUs in the system\n", c[ARG_LIST], t[ARG_LIST], (int) (max_len-strlen(t[ARG_LIST])), "");
printf(" -%c, --%s %*s Select the GPU to use (default: 0)\n", c[ARG_GPU], t[ARG_GPU], (int) (max_len-strlen(t[ARG_GPU])), ""); printf(" -%c, --%s %*s Select the GPU to use (default: 0)\n", c[ARG_GPU], t[ARG_GPU], (int) (max_len-strlen(t[ARG_GPU])), "");
printf(" -%c, --%s %*s Enable verbose output\n", c[ARG_VERBOSE], t[ARG_VERBOSE], (int) (max_len-strlen(t[ARG_VERBOSE])), "");
printf(" -%c, --%s %*s Print this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), ""); printf(" -%c, --%s %*s Print this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), "");
printf(" -%c, --%s %*s Print gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), ""); printf(" -%c, --%s %*s Print gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
@@ -66,6 +67,8 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
set_log_level(verbose_enabled());
struct gpu_list* list = get_gpu_list(); struct gpu_list* list = get_gpu_list();
if(list_gpus()) { if(list_gpus()) {
return print_gpus_list(list); return print_gpus_list(list);
@@ -80,13 +83,11 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
set_log_level(true);
struct gpu_info* gpu = get_gpu_info(list, get_gpu_idx()); struct gpu_info* gpu = get_gpu_info(list, get_gpu_idx());
if(gpu == NULL) if(gpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
printf("[WARNING]: gpufetch is in beta. The provided information may be incomplete or wrong.\n\ printf("[NOTE]: gpufetch is in beta. The provided information may be incomplete or wrong.\n\
If you want to help to improve gpufetch, please compare the output of the program\n\ If you want to help to improve gpufetch, please compare the output of the program\n\
with a reliable source which you know is right (e.g, techpowerup.com) and report\n\ with a reliable source which you know is right (e.g, techpowerup.com) and report\n\
any inconsistencies to https://github.com/Dr-Noob/gpufetch/issues\n"); any inconsistencies to https://github.com/Dr-Noob/gpufetch/issues\n");

View File

@@ -507,12 +507,14 @@ struct terminal* get_terminal_size() {
bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs) { bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs) {
struct terminal* term = get_terminal_size(); struct terminal* term = get_terminal_size();
if(gpu->vendor == GPU_VENDOR_NVIDIA) if(gpu->vendor == GPU_VENDOR_NVIDIA) {
#ifdef BACKEND_CUDA #ifdef BACKEND_CUDA
if(clean_output()) printf("%*s", (int) strlen(CUDA_DRIVER_START_WARNING), " ");
return print_gpufetch_cuda(gpu, s, cs, term); return print_gpufetch_cuda(gpu, s, cs, term);
#else #else
return false; return false;
#endif #endif
}
else { else {
#ifdef BACKEND_INTEL #ifdef BACKEND_INTEL
return print_gpufetch_intel(gpu, s, cs, term); return print_gpufetch_intel(gpu, s, cs, term);

View File

@@ -100,7 +100,7 @@ struct gpu_info* get_gpu_info_cuda(int gpu_idx) {
} }
if(gpu_idx == 0) { if(gpu_idx == 0) {
printf("Waiting for CUDA driver to start..."); printf("%s", CUDA_DRIVER_START_WARNING);
fflush(stdout); fflush(stdout);
} }
@@ -113,6 +113,7 @@ struct gpu_info* get_gpu_info_cuda(int gpu_idx) {
if(gpu_idx == 0) { if(gpu_idx == 0) {
printf("\r"); printf("\r");
fflush(stdout);
} }
if(num_gpus <= 0) { if(num_gpus <= 0) {

View File

@@ -2,6 +2,7 @@
#define __CUDA_GPU__ #define __CUDA_GPU__
#include "../common/gpu.hpp" #include "../common/gpu.hpp"
#define CUDA_DRIVER_START_WARNING "Waiting for CUDA driver to start..."
struct gpu_info* get_gpu_info_cuda(int gpu_idx); struct gpu_info* get_gpu_info_cuda(int gpu_idx);
bool print_gpu_cuda(struct gpu_info* gpu); bool print_gpu_cuda(struct gpu_info* gpu);