Skip to content

Instantly share code, notes, and snippets.

@slowpeek
Last active November 17, 2024 20:02
Show Gist options
  • Save slowpeek/006680bb06e671bc489d4a3274ff3d19 to your computer and use it in GitHub Desktop.
Save slowpeek/006680bb06e671bc489d4a3274ff3d19 to your computer and use it in GitHub Desktop.
nvidia-smi dmon adapter: 525, 530, 535, 545
#!/usr/bin/env bash
# MIT license (c) 2022-2024 https://github.com/slowpeek
# Homepage: https://gist.github.com/slowpeek/006680bb06e671bc489d4a3274ff3d19
: <<'README'
--- THIS SCRIPT IS NO LONGER MAINTAINED ---
Applicable nvidia driver versions: 525, 530, 535, 545, 550
ABOUT --
Since nvidia driver 525, field parsing in KDE system monitor stumbled twice:
- 525: `nvidia-smi dmon` appends a tailing space on each line printed.
Fixed in plasma 5.27 [1]
- 535: There are extra columns in `nvidia-smi dmon -s pucm` output.
Fixed in plasma 5.27.7 [2]
Some systems won't receive the fixes like ubuntu 22.04 running plasma
5.24. This tool can be used instead to normalize `nvidia-smi dmon` output
before passing it to the parser.
The tool only changes anything either when called by ksystemstats or when
there is NVIDIA_SMI_ADAPTER=y env var. Otherwise, it just runs the nvidia-smi
binary as-is.
[1] https://invent.kde.org/plasma/ksystemstats/-/commit/d42ab99437
[2] https://invent.kde.org/plasma/ksystemstats/-/commit/7f9ead6bdd
INSTALLATION --
Save the script as 'nvidia-smi' under /usr/local/bin/, make it executable and
reboot or relog.
TECHNICAL DETAILS --
`dmon -s pucm` cols expected by KDE system monitor: gpu pwr gtemp mtemp sm mem
enc dec mclk pclk fb bar1
Since 535:
- extra cols in `dmon -s u`: jpg, ofa
Since 535 (excluding some 535.x.y and 545):
- extra cols in `dmon -s m`: ccpm
README
set -eu
is_comment () {
[[ $1 == '#'* ]]
}
puts () {
printf -- '%s\n' "$1"
}
# Option: set custom nvidia-smi binary with NVIDIA_SMI env var
nvidia_smi=${NVIDIA_SMI:-/usr/bin/nvidia-smi}
# Option: force the adapter, even if not called by ksystemstats, with
# NVIDIA_SMI_ADAPTER=y env var
force_adapter=${NVIDIA_SMI_ADAPTER:-n}
pass=y
# Only do something in 'dmon' mode
if [[ ${1-} == dmon ]]; then
# Only do something if called by ksystemstats OR forced
# shellcheck disable=SC2046
if [[ $(ps -o comm= $(ps -o ppid= $$)) == ksystemstats || $force_adapter == y ]]; then
pass=n
fi
fi
[[ $pass == n ]] || exec "$nvidia_smi" "$@"
adapter () {
read -r h1 || return
if ! is_comment "$h1" || ! read -r h2; then
puts "$h1"
return
fi
if ! is_comment "$h2"; then
puts "$h1"
puts "$h2"
return
fi
read -ra h1_list <<< "${h1:1}"
# Figure out indices for cols added in 535 and collect $header out of the
# rest
header=() del=() col=0
for el in "${h1_list[@]}"; do
if [[ $el == @(jpg|ofa|ccpm) ]]; then
del+=("$col")
else
header+=("$el")
fi
(( ++col ))
done
# Header with 535 cols removed
puts "# ${header[*]}"
while read -r line; do
! is_comment "$line" || continue
read -ra row <<< "$line"
# Remove 535 cols from $row
for col in "${del[@]}"; do
unset -v 'row[col]'
done
puts "${row[*]}"
done
}
"$nvidia_smi" "$@" | {
adapter
tee
}
exit "${PIPESTATUS[0]}"
@Haui1112
Copy link

Thanks for making this. Sadly, it doesnt change anything for me (debian bookworm with KDE on X11). Still not GPU stats on the systemmonitor and desktop widget. when I use the nvidia controls app or smi, it shows the correct stats.

@slowpeek
Copy link
Author

slowpeek commented Jul 26, 2024

@Haui1112

It should work for you. Assuming you're in the dir where this script is stored, could you show me output of such commands:

./nvidia-smi dmon -s pucm -c 1

and

NVIDIA_SMI_ADAPTER=y ./nvidia-smi dmon -s pucm -c 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment