How to show AMD VRAM on waybar or any other status bar

I am currently using waybar as my status bar on hyprland. Previously I was using polybar on i3. I found that both of these bars, along with many other applications and desktops do not provide a built-in way to show VRAM usage. Here's a solution I came up with years ago and how to apply it.

I start with a dedicated script to pull the data from the kernel.


#!/bin/bash
DIR1="$(sudo /bin/ls /sys/kernel/debug/dri/ | head -n 1)"
USEDVRAM="$(sudo cat /sys/kernel/debug/dri/${DIR1}/amdgpu_vram_mm | grep -E "^ *usage:" | cut -d ' ' -f 4)"
TOTALVRAM="$(sudo cat /sys/kernel/debug/dri/${DIR1}/amdgpu_vram_mm | grep -E "^ *size:"  | cut -d ' ' -f 4)"
VRAMRAT=$(bc -l <<< "scale=1;100*$USEDVRAM/$TOTALVRAM" | sed 's/$/%/')
echo $VRAMRAT

Which means in /etc/sudoers I had to add:


## For radeon statistics
myuser ALL = (ALL) NOPASSWD: /bin/cat /sys/kernel/debug/dri/[0-9]*/amdgpu_pm_info
myuser ALL = (ALL) NOPASSWD: /bin/cat /sys/kernel/debug/dri/[0-9]*/amdgpu_vram_mm
myuser ALL = (ALL) NOPASSWD: /bin/ls /sys/kernel/debug/dri/

Then finally in waybar, I configure custom widget with an image unicode symbol.


"custom/vram": {
    "exec": "$HOME/scripts/in_terminal/gpustats.sh",
    "format": "{icon} {text}",
    "format-icons": {
        "default": "\uf03e"
    },
    "interval": 5
}

Or in polybar with fontawesome prefix glyphs, use the following code block. I believe arch linux packages otf-font-awesome and woff2-font-awesome provide this.


[module/vmemory]
type = custom/script
interval = 2
exec = if [ "$USER" = "myuser" ]; then $HOME/scripts/in_terminal/gpustats.sh; fi
tail=true
format-prefix = "   "
format-prefix-foreground = ${colors.foreground-alt}
format-underline = #851b8f8f