From 5e6c3cb965313a2a5389057c18085dd87ccc270e Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 10 Aug 2021 16:54:25 +0200 Subject: [PATCH] [v0.01] Initialize repository reusing stuff from cpufetch --- .github/workflows/lockdown.yml | 36 +++++++++++++ .gitignore | 1 + LICENSE | 21 ++++++++ Makefile | 46 ++++++++++++++++ README.md | 2 + src/common/args.c | 99 ++++++++++++++++++++++++++++++++++ src/common/args.h | 19 +++++++ src/common/global.c | 95 ++++++++++++++++++++++++++++++++ src/common/global.h | 17 ++++++ src/common/main.c | 51 ++++++++++++++++++ 10 files changed, 387 insertions(+) create mode 100644 .github/workflows/lockdown.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/common/args.c create mode 100644 src/common/args.h create mode 100644 src/common/global.c create mode 100644 src/common/global.h create mode 100644 src/common/main.c diff --git a/.github/workflows/lockdown.yml b/.github/workflows/lockdown.yml new file mode 100644 index 0000000..064b26a --- /dev/null +++ b/.github/workflows/lockdown.yml @@ -0,0 +1,36 @@ +name: 'Disable PR in gpufetch' + +on: + issues: + types: opened + pull_request_target: + types: opened + +permissions: + issues: write + pull-requests: write + +jobs: + action: + runs-on: ubuntu-latest + steps: + - uses: dessant/repo-lockdown@v2 + with: + github-token: ${{ github.token }} + exclude-issue-created-before: '' + exclude-issue-labels: '' + issue-labels: '' + issue-comment: '' + skip-closed-issue-comment: false + close-issue: false + lock-issue: true + issue-lock-reason: '' + exclude-pr-created-before: '' + exclude-pr-labels: '' + pr-labels: '' + pr-comment: 'gpufetch does not accept pull requests, see [the contributing guidelines](https://github.com/Dr-Noob/gpufetch/blob/master/CONTRIBUTING.md) for details' + skip-closed-pr-comment: false + close-pr: true + lock-pr: false + pr-lock-reason: '' + process-only: 'prs' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..17a84ce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gpufetch diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2650f3c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Dr-Noob + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3054930 --- /dev/null +++ b/Makefile @@ -0,0 +1,46 @@ +CXX ?= g++ + +CXXFLAGS+=-Wall -Wextra -pedantic -fstack-protector-all -pedantic +SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith + +PREFIX ?= /usr + +SRC_COMMON=src/common/ + +COMMON_SRC = $(SRC_COMMON)main.c $(SRC_COMMON)args.c $(SRC_COMMON)global.c +COMMON_HDR = $(SRC_COMMON)args.h $(SRC_COMMON)global.h + +SOURCE += $(COMMON_SRC) +HEADERS += $(COMMON_HDR) +OUTPUT=gpufetch + +all: CXXFLAGS += -O3 +all: $(OUTPUT) + +debug: CXXFLAGS += -g -O0 +debug: $(OUTPUT) + +static: CXXFLAGS += -static -O3 +static: $(OUTPUT) + +strict: CXXFLAGS += -O3 -Werror -fsanitize=undefined -D_FORTIFY_SOURCE=2 +strict: $(OUTPUT) + +$(OUTPUT): Makefile $(SOURCE) $(HEADERS) + $(CXX) $(CXXFLAGS) $(SANITY_FLAGS) $(SOURCE) -o $(OUTPUT) + +run: $(OUTPUT) + ./$(OUTPUT) + +clean: + @rm -f $(OUTPUT) + +install: $(OUTPUT) + install -Dm755 "gpufetch" "$(DESTDIR)$(PREFIX)/bin/gpufetch" + install -Dm644 "LICENSE" "$(DESTDIR)$(PREFIX)/share/licenses/gpufetch-git/LICENSE" + install -Dm644 "gpufetch.1" "$(DESTDIR)$(PREFIX)/share/man/man1/gpufetch.1.gz" + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)/bin/gpufetch" + rm -f "$(DESTDIR)$(PREFIX)/share/licenses/gpufetch-git/LICENSE" + rm -f "$(DESTDIR)$(PREFIX)/share/man/man1/gpufetch.1.gz" diff --git a/README.md b/README.md new file mode 100644 index 0000000..57e7255 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# gpufetch +WIP... diff --git a/src/common/args.c b/src/common/args.c new file mode 100644 index 0000000..4c238dc --- /dev/null +++ b/src/common/args.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#include "args.h" +#include "global.h" + +struct args_struct { + bool help_flag; + bool version_flag; +}; + +const char args_chr[] = { + /* [ARG_CHAR_HELP] = */ 'h', + /* [ARG_CHAR_VERSION] = */ 'V', +}; + +const char *args_str[] = { + /* [ARG_CHAR_HELP] = */ "help", + /* [ARG_CHAR_VERSION] = */ "version", +}; + +static struct args_struct args; + +bool show_help() { + return args.help_flag; +} + +bool show_version() { + return args.version_flag; +} + +int max_arg_str_length() { + int max_len = -1; + int len = sizeof(args_str) / sizeof(args_str[0]); + for(int i=0; i < len; i++) { + max_len = max(max_len, (int) strlen(args_str[i])); + } + return max_len; +} + +char* build_short_options() { + const char *c = args_chr; + int len = sizeof(args_chr) / sizeof(args_chr[0]); + char* str = (char *) emalloc(sizeof(char) * (len*2 + 1)); + memset(str, 0, sizeof(char) * (len*2 + 1)); + + sprintf(str, "%c%c", + c[ARG_HELP], c[ARG_VERSION]); + + return str; +} + +bool parse_args(int argc, char* argv[]) { + int opt; + int option_index = 0; + opterr = 0; + + args.version_flag = false; + args.help_flag = false; + + const struct option long_options[] = { + {args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] }, + {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, + {0, 0, 0, 0} + }; + + char* short_options = build_short_options(); + opt = getopt_long(argc, argv, short_options, long_options, &option_index); + + while (!args.help_flag && !args.version_flag && opt != -1) { + if(opt == args_chr[ARG_HELP]) { + args.help_flag = true; + } + else if(opt == args_chr[ARG_VERSION]) { + args.version_flag = true; + } + else { + printWarn("Invalid options"); + args.help_flag = true; + } + + option_index = 0; + opt = getopt_long(argc, argv, short_options, long_options, &option_index); + } + + if(optind < argc) { + printWarn("Invalid options"); + args.help_flag = true; + } + + if((args.help_flag + args.version_flag) > 1) { + printWarn("You should specify just one option"); + args.help_flag = true; + } + + return true; +} diff --git a/src/common/args.h b/src/common/args.h new file mode 100644 index 0000000..f83c0da --- /dev/null +++ b/src/common/args.h @@ -0,0 +1,19 @@ +#ifndef __ARGS__ +#define __ARGS__ + +#include + +enum { + ARG_HELP, + ARG_VERSION +}; + +extern const char args_chr[]; +extern const char *args_str[]; + +int max_arg_str_length(); +bool parse_args(int argc, char* argv[]); +bool show_help(); +bool show_version(); + +#endif diff --git a/src/common/global.c b/src/common/global.c new file mode 100644 index 0000000..507d101 --- /dev/null +++ b/src/common/global.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +#include "global.h" + +#ifdef _WIN32 + +#define RED "" +#define BOLD "" +#define RESET "" + +#else + +#define RED "\x1b[31;1m" +#define BOLD "\x1b[;1m" +#define RESET "\x1b[0m" + +#endif + +enum { + LOG_LEVEL_NORMAL, + LOG_LEVEL_VERBOSE +}; + +int LOG_LEVEL; + +void printWarn(const char *fmt, ...) { + if(LOG_LEVEL == LOG_LEVEL_VERBOSE) { + int buffer_size = 4096; + char* buffer = new char[buffer_size]; + va_list args; + va_start(args, fmt); + vsnprintf(buffer,buffer_size, fmt, args); + va_end(args); + fprintf(stderr, BOLD "[WARNING]: " RESET "%s\n",buffer); + delete [] buffer; + } +} + +void printErr(const char *fmt, ...) { + int buffer_size = 4096; + char* buffer = new char[buffer_size]; + va_list args; + va_start(args, fmt); + vsnprintf(buffer,buffer_size, fmt, args); + va_end(args); + fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer); + delete [] buffer; +} + +void printBug(const char *fmt, ...) { + int buffer_size = 4096; + char* buffer = new char[buffer_size]; + va_list args; + va_start(args, fmt); + vsnprintf(buffer,buffer_size, fmt, args); + va_end(args); + fprintf(stderr, RED "[ERROR]: " RESET "%s\n",buffer); + fprintf(stderr,"Please, create a new issue with this error message in https://github.com/Dr-Noob/gpufetch/issues\n"); + delete [] buffer; +} + +void set_log_level(bool verbose) { + if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE; + else LOG_LEVEL = LOG_LEVEL_NORMAL; +} + +int max(int a, int b) { + return a > b ? a : b; +} + +void* emalloc(size_t size) { + void* ptr = malloc(size); + + if(ptr == NULL) { + printErr("malloc failed: %s", strerror(errno)); + exit(1); + } + + return ptr; +} + +void* ecalloc(size_t nmemb, size_t size) { + void* ptr = calloc(nmemb, size); + + if(ptr == NULL) { + printErr("calloc failed: %s", strerror(errno)); + exit(1); + } + + return ptr; +} diff --git a/src/common/global.h b/src/common/global.h new file mode 100644 index 0000000..f0e9118 --- /dev/null +++ b/src/common/global.h @@ -0,0 +1,17 @@ +#ifndef __GLOBAL__ +#define __GLOBAL__ + +#include +#include + +#define STRING_UNKNOWN "Unknown" + +void set_log_level(bool verbose); +void printWarn(const char *fmt, ...); +void printErr(const char *fmt, ...); +void printBug(const char *fmt, ...); +int max(int a, int b); +void* emalloc(size_t size); +void* ecalloc(size_t nmemb, size_t size); + +#endif diff --git a/src/common/main.c b/src/common/main.c new file mode 100644 index 0000000..6b72c1d --- /dev/null +++ b/src/common/main.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "args.h" +#include "global.h" + +static const char* VERSION = "0.01"; + +void print_help(char *argv[]) { + const char **t = args_str; + const char *c = args_chr; + int max_len = max_arg_str_length(); + + printf("Usage: %s [OPTION]...\n", argv[0]); + printf("Simple yet fancy GPU architecture fetching tool\n\n"); + + 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("\nBUGS: \n"); + printf(" Report bugs to https://github.com/Dr-Noob/gpufetch/issues\n"); + + printf("\nNOTE: \n"); + printf(" Peak performance information is NOT accurate. gpufetch computes peak performance using the max\n"); + printf(" frequency. However, to properly compute peak performance, you need to know the frequency of the\n"); + printf(" GPU running real code.\n"); + printf(" For peak performance measurement see: https://github.com/Dr-Noob/peakperf\n"); +} + +void print_version() { + printf("gpufetch v%s\n", VERSION); +} + +int main(int argc, char* argv[]) { + if(!parse_args(argc,argv)) + return EXIT_FAILURE; + + if(show_help()) { + print_help(argv); + return EXIT_SUCCESS; + } + + if(show_version()) { + print_version(); + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; +}