-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get gpu usage by pid #21
Comments
Using If you are interested in getting a pid breakdown of gpu-memory consomption for a given device, yout can use import pynvml
pynvml.nvmlInit()
for dev_id in range(pynvml.nvmlDeviceGetCount()):
handle = pynvml.nvmlDeviceGetHandleByIndex(dev_id)
for proc in pynvml.nvmlDeviceGetComputeRunningProcesses(handle):
print(
"pid %d using %d bytes of memory on device %d."
% (proc.pid, proc.usedGpuMemory, dev_id)
) I will try to take a harder look into this later and will update here if I find anything. |
I'd love that feature as well! Calling |
This is the code I'm using now. It only works for type "C" processes, not type "G" in the list when calling nvidia-smi directly in the terminal (e.g. my neural network in python was a type "C" process, Xorg type "G") but I think it should suffice: import os
from pynvml.smi import nvidia_smi
from bs4 import BeautifulSoup as bs
def get_gpu_usage(pid=None):
if pid is None:
# get current process id
pid = os.getpid()
pid = str(pid)
# find associated gpu usage
soup = bs(nvidia_smi.getInstance().XmlDeviceQuery(), "lxml")
for p in soup.find("processes").find_all("process_info"):
if p.find("pid").get_text() == pid:
return p.find("used_memory").get_text()
# process not found
return "0 MiB" |
@cod3licious Why use beautiful soup and xml when you can simply call |
Will it be possible to get gpu usage from a given pid from this binding?
The text was updated successfully, but these errors were encountered: