-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetResource.py
35 lines (30 loc) · 1.08 KB
/
GetResource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import psutil
import subprocess
DEFAULT_ATTRIBUTES = (
'index',
'uuid',
'name',
'timestamp',
'memory.total',
'memory.free',
'memory.used',
'utilization.gpu',
'utilization.memory',
'temperature.gpu'
)
def get_gpu_info(nvidia_smi_path='nvidia-smi', keys=DEFAULT_ATTRIBUTES, no_units=True):
nu_opt = '' if not no_units else ',nounits'
cmd = f'%s --query-gpu=%s --format=csv,noheader%s' % (nvidia_smi_path, ','.join(keys), nu_opt)
output = subprocess.check_output(cmd, shell=True)
lines = output.decode().split('\n')
lines = [ line.strip() for line in lines if line.strip() != '' ]
return [ { k: v for k, v in zip(keys, line.split(', ')) } for line in lines ]
def get_resource():
mem = psutil.virtual_memory().percent
cpu = psutil.cpu_percent(interval=1)
gpu_info = get_gpu_info()
gpu_mem = int(gpu_info[0]["memory.used"])/int(gpu_info[0]["memory.total"])
gpu_temp = gpu_info[0]["temperature.gpu"]
return f"cpu:{cpu},mem:{mem},gpu_mem:{gpu_mem},gpu_temp:{gpu_temp}"
if __name__ == "__main__":
print(get_resource())