🏁 init

This commit is contained in:
Alexander Popov 2025-02-27 23:45:56 +03:00
commit 0061cb0878
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
8 changed files with 107 additions and 0 deletions

9
.clang-format Normal file
View File

@ -0,0 +1,9 @@
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 2
ColumnLimit: 132
SortIncludes: true
AlignAfterOpenBracket: DontAlign
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
# AllowShortNamespacesOnASingleLine: true

16
.editorconfig Normal file
View File

@ -0,0 +1,16 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true
# for all files
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
# C
[{*.c,*.h}]
indent_style = space
indent_size = 2

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# binary
nv-info

6
README.md Normal file
View File

@ -0,0 +1,6 @@
---
gitea: none
include_toc: true
---
![Screenshot](./assets/image.png)

BIN
assets/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

9
build.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
INCLUDE="/usr/local/cuda/targets/x86_64-linux/include/"
clang-18 -Os \
-I${INCLUDE} \
-o nv-info \
src/nv-info.c \
-lnvidia-ml

24
src/colors.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef COLORS_H_
#define COLORS_H_
#define COLORSLIB_VERSION "1.0.0"
#ifndef NO_COLOR // With colors
#define COLOR_RED(string) "\x1b[31m" string "\x1b[0m"
#define COLOR_GREEN(string) "\x1b[32m" string "\x1b[0m"
#define COLOR_YELLOW(string) "\x1b[33m" string "\x1b[0m"
#define COLOR_BLUE(string) "\x1b[34m" string "\x1b[0m"
#define COLOR_MAGENTA(string) "\x1b[35m" string "\x1b[0m"
#define COLOR_CYAN(string) "\x1b[36m" string "\x1b[0m"
#define COLOR_RESET(string) "\x1b[0m" string "\x1b[0m"
#else // Without colors
#define COLOR_RED(string) string
#define COLOR_GREEN(string) string
#define COLOR_YELLOW(string) string
#define COLOR_BLUE(string) string
#define COLOR_MAGENTA(string) string
#define COLOR_CYAN(string) string
#define COLOR_RESET(string) string
#endif
#endif // #ifndef COLORS_H_

41
src/nv-info.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <nvml.h>
#define NVML_INIT_FLAG_NO_GPUS 1 // Don't fail nvmlInit() when no GPUs are found
#include "colors.h"
int main() {
nvmlReturn_t result; // Don't used
unsigned int deviceCount;
nvmlMemory_t memory;
unsigned int power;
unsigned int temperature;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
if (nvmlInit_v2() != NVML_SUCCESS) return 1;
if (nvmlDeviceGetCount(&deviceCount) == NVML_SUCCESS) {
printf(COLOR_CYAN("Devices:") " %d\n\n", deviceCount);
for (unsigned int i = 0; i < deviceCount; ++i) {
nvmlDevice_t device;
result = nvmlDeviceGetHandleByIndex(0, &device);
printf(COLOR_GREEN("%d: "), i);
if (nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE) == NVML_SUCCESS) printf(COLOR_GREEN("%s") "\n", name);
if (nvmlDeviceGetPowerUsage(device, &power) == NVML_SUCCESS) printf(COLOR_RED(" Power:") " %d milliwatts\n", power);
if (nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temperature) == NVML_SUCCESS)
printf(COLOR_MAGENTA(" Temperature:") " %d °C\n", temperature);
if (nvmlDeviceGetMemoryInfo(device, &memory) == NVML_SUCCESS) {
printf(COLOR_YELLOW(" Total Memory:") " %llu MB\n", memory.total / (1024 * 1024));
printf(COLOR_YELLOW(" Used Memory:") " %llu MB\n", memory.used / (1024 * 1024));
printf(COLOR_YELLOW(" Free Memory:") " %llu MB\n", memory.free / (1024 * 1024));
}
}
}
if (nvmlShutdown() == NVML_SUCCESS) return 0;
else return 1;
}