diff options
author | xAlpharax <42233094+xAlpharax@users.noreply.github.com> | 2023-09-06 20:19:08 +0300 |
---|---|---|
committer | xAlpharax <42233094+xAlpharax@users.noreply.github.com> | 2023-09-06 20:19:08 +0300 |
commit | debe2e06f3a4b809e16913b11e1ce7460dffdf39 (patch) | |
tree | 9f303283f9133d48af2aca0ab081330e7841cc61 | |
parent | 13322f58bae5aafc0dbdd47b25836ed44a0d3464 (diff) |
Integrating a VRAM monitoring comonent
Changes to be committed:
modified: Makefile
modified: README
new file: components/vram.c
modified: config.h
modified: slstatus.h
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | components/vram.c | 92 | ||||
-rw-r--r-- | config.h | 2 | ||||
-rw-r--r-- | slstatus.h | 3 |
5 files changed, 99 insertions, 0 deletions
@@ -21,6 +21,7 @@ COM =\ components/netspeeds\ components/num_files\ components/ram\ + components/vram\ components/run_command\ components/swap\ components/temperature\ @@ -26,6 +26,7 @@ Features - Network speeds (RX and TX) - Number of files in a directory (hint: Maildir) - Memory status (free memory, percentage, total memory and used memory) +- GPU Memory status (percentage used) - Swap status (free swap, percentage, total swap and used swap) - Temperature - Uptime diff --git a/components/vram.c b/components/vram.c new file mode 100644 index 0000000..3148d01 --- /dev/null +++ b/components/vram.c @@ -0,0 +1,92 @@ +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +#include "../slstatus.h" +#include "../util.h" + +// Function to run a command and capture its output +char* runCommand(const char* command) { + char buffer[128]; + char* result = NULL; + FILE* pipe = popen(command, "r"); + + if (!pipe) { + perror("popen"); + exit(1); + } + + while (fgets(buffer, sizeof(buffer), pipe) != NULL) { + if (result == NULL) { + result = strdup(buffer); + } else { + char* old_result = result; + result = (char*)malloc(strlen(old_result) + strlen(buffer) + 1); + strcpy(result, old_result); + strcat(result, buffer); + free(old_result); + } + } + + pclose(pipe); + return result; +} + +// Funtion to gather used VRAM information (MiB) +float GetUsedVRAM() { + char* command_output; + float vram_used = -1.0; + + // Check if an NVIDIA card is present + command_output = runCommand("nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits"); + if (command_output != NULL) { + sscanf(command_output, "%f", &vram_used); + free(command_output); + } else { + // If no NVIDIA card is found, check for an AMD Radeon card + // todo intel: "intel-gpu-top -b -s 1 -o - | grep 'used:'" + command_output = runCommand("cat /sys/class/drm/card0/device/mem_info_vram_used"); + if (command_output != NULL) { + sscanf(command_output, "%f", &vram_used); + free(command_output); + } + } + + return vram_used; +} + +// Funtion to gather total VRAM information (MiB) +float GetTotalVRAM() { + char* command_output; + float vram_total = -1.0; + + // Check if an NVIDIA card is present + command_output = runCommand("nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits"); + if (command_output != NULL) { + sscanf(command_output, "%f", &vram_total); + free(command_output); + } else { + // If no NVIDIA card is found, check for an AMD Radeon card + // todo intel: "intel-gpu-top -b -s 1 -o - | grep 'total:'" + command_output = runCommand("cat /sys/class/drm/card0/device/mem_info_vram_total"); + if (command_output != NULL) { + sscanf(command_output, "%f", &vram_total); + free(command_output); + } + } + + return vram_total; +} + +const char * +vram_perc(const char *unused) { + float vram_used = GetUsedVRAM(); + float vram_total = GetTotalVRAM(); + + if (vram_used >= 0.0 && vram_total >= 0.0) { + int vram_percentage = (int) vram_used / vram_total * 100; + return bprintf("%d", vram_percentage); + } else { + return NULL; + } +} @@ -16,6 +16,7 @@ const struct arg args[] = { { cpu_perc, " <%s cpu>", NULL }, { cpu_freq, " %sHz", NULL }, { ram_perc, " <%s ram>", NULL }, + { vram_perc, " <%s vram>", NULL }, { disk_perc, " <%s nvme>", "/" }, { disk_perc, " <%s sata>", "/home/alphara/exfatssd" }, { wifi_perc, " <%s wifi>", "wlp5s0" }, @@ -57,6 +58,7 @@ const struct arg args[] = { * ram_perc memory usage in percent NULL * ram_total total memory size in GB NULL * ram_used used memory in GB NULL + * vram_perc gpu memory usage in percent NULL * run_command custom shell command command (echo foo) * separator string to echo NULL * swap_free free swap in GB NULL @@ -50,6 +50,9 @@ const char *netspeed_tx(const char *interface); /* num_files */ const char *num_files(const char *path); +/* vram */ +const char *vram_perc(const char *unused); + /* ram */ const char *ram_free(const char *unused); const char *ram_perc(const char *unused); |