Skip to content

Instantly share code, notes, and snippets.

@xychenunc
Forked from matpalm/gpu_stat.py
Created December 11, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xychenunc/9698ef67519a2f2a6fa0e1703ab91748 to your computer and use it in GitHub Desktop.
Save xychenunc/9698ef67519a2f2a6fa0e1703ab91748 to your computer and use it in GitHub Desktop.
json formatting of nvidia-settings
#!/usr/bin/env python
# gpu_stat.py [DELAY [COUNT]]
# 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
try:
delay = int(sys.argv[1])
except:
delay = 1
try:
count = int(sys.argv[2])
except:
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:
exit(0)
time.sleep(delay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment