windows-10python-3.6gpunvml

How to measure GPU usage per process in Windows using python?


I would like to measure the GPU usage per process as done in Windows taskmgr.exe, but I have encountered several problems when attempting to use the pyNVML library. As a result, I have a few questions.

First, is it currently possible to measure the exact GPU usage per process in Windows using Python? I have already tried the nvidia-smi query, but this doesn't seem to show memory used and utilization percent for each process.

Second, if it is possible to measure GPU usage in this way using Python, I would like to measure and show it in a similar fashion as done in the Windows taskmgr.exe of Windows 10.

Here is my code so far:

nvmlInit()
deviceCount = nvmlDeviceGetCount()
#print(deviceCount)
for device_id in range(deviceCount):
    hd = nvmlDeviceGetHandleByIndex(device_id)
    #print(handle)

cps = nvmlDeviceGetGraphicsRunningProcesses(hd)

for ps in cps :
    pp = ps.pid
    #print(pp)
    try :
        name = str(nvmlSystemGetProcessName(ps.pid))
        n = name.split("\\")
        #print(n[len(n)-1][:-1])
        process_name = n[len(n)-1][:-1]
        if process_name == 'chrome.exe':
            print(process_name, pp, ps.usedGpuMemory)
    except:
        pass

and my result:

chrome.exe 16688 None

nvidia-smi

As you can see, this does not reveal the GPU memory usage per process, but I need the information shown in taskmgr's GPU section. (I have no need of visualization.)

taskmgr

My computer specs are Windows 10 pro, GTX 950, i5-6600

If this is impossible in Python at the moment, do you have any other recommendations to automatically collect GPU usage per process.

Thank you.


Solution

  • Check the answer of Jonathan DEKHTIAR Here explaining the reason why it doesn't work. As a workaround, you can try fetching values from powershell Get-Counter -Counter "\GPU Engine(*)\Utilization Percentage"