Skip to content

Instantly share code, notes, and snippets.

@georg-wolflein
Last active August 21, 2022 09:32
Show Gist options
  • Save georg-wolflein/d674dd31f391b3d2daf95522a0ade0e9 to your computer and use it in GitHub Desktop.
Save georg-wolflein/d674dd31f391b3d2daf95522a0ade0e9 to your computer and use it in GitHub Desktop.
For each GPU, list used memory by process (and find associated docker container)
#!/bin/bash
for i in $(nvidia-smi --query-gpu=index --format=csv,noheader); do
echo GPU "#$i": $(nvidia-smi --query-gpu=memory.used --format=csv,noheader -i $i) used
while IFS="," read -r pid name used; do
pid_container_name=
for container in $(docker ps -q); do
if [[ $(docker top $container | awk '{print $2}'| grep $pid) ]]; then
pid_container_name=$(docker inspect $container --format '{{.Name}}')
fi
done
cmd=$(ps -p $pid -o cmd=)
if [ -z "$cmd" ]; then
cmd=$name
fi
if [[ $pid_container_name ]]; then
pid_container_name="in $pid_container_name"
fi
echo " " $used used by PID $pid $pid_container_name running $cmd
done < <(nvidia-smi --query-compute-apps=pid,process_name,used_gpu_memory -i $i --format=csv,noheader)
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment