Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
json formatting of nvidia-settings
#!/usr/bin/env python
# dump some gpu stats as a line of json
# {"util":{"PCIe":"0", "memory":"11", "video":"0", "graphics":"13"}, "used_mem":"161"}
import json, socket, subprocess, sys, time
delay = int(sys.argv[1])
delay = 1
count = int(sys.argv[2])
count = None
i = 0
while True:
GPU = socket.gethostname() + ":0[gpu:0]"
cmd = ['nvidia-settings', '-t', '-q', GPU + '/GPUUtilization']
gpu_util = subprocess.check_output(cmd).strip().split(",")
gpu_util = dict([f.strip().split("=") for f in gpu_util])
cmd[-1] = GPU + '/UsedDedicatedGPUMemory'
gpu_used_mem = subprocess.check_output(cmd).strip()
print json.dumps({"used_mem": gpu_used_mem, "util": gpu_util, "time": int(time.time())})
if count != None:
i += 1
if i == count:
Copy link

Ahwar commented Apr 6, 2020

I am running it on google Colab and it is showing this error.

Unable to init server: Could not connect: Connection refused

ERROR: The control display is undefined; please run `nvidia-settings
       --help` for usage information.

Copy link

SysOverdrive commented May 28, 2020

I am running it on a local machine and the subprocess cannot find the specified path
['nvidia-settings', '-t', '-q', 'TudPC:0[gpu:0]/GPUUtilization']


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