[v0.01] Initialize repository reusing stuff from cpufetch

This commit is contained in:
Dr-Noob
2021-08-10 16:54:25 +02:00
commit 5e6c3cb965
10 changed files with 387 additions and 0 deletions

36
.github/workflows/lockdown.yml vendored Normal file
View File

@@ -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'

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
gpufetch

21
LICENSE Normal file
View File

@@ -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.

46
Makefile Normal file
View File

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

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
# gpufetch
WIP...

99
src/common/args.c Normal file
View File

@@ -0,0 +1,99 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
}

19
src/common/args.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef __ARGS__
#define __ARGS__
#include <stdbool.h>
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

95
src/common/global.c Normal file
View File

@@ -0,0 +1,95 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#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;
}

17
src/common/global.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef __GLOBAL__
#define __GLOBAL__
#include <stdbool.h>
#include <stddef.h>
#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

51
src/common/main.c Normal file
View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}