-
Notifications
You must be signed in to change notification settings - Fork 2
/
device.py
81 lines (72 loc) · 2.56 KB
/
device.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import json
class GPU():
def __init__(self, index, name, memory_total, memory_free, memory_used, utilization_gpu):
self.index = index
self.name = name
self.memory_total = int(memory_total.split()[0])
self.memory_free = int(memory_free.split()[0])
self.memory_used = int(memory_used.split()[0])
self.utilization_gpu = int(utilization_gpu.split()[0])
def __repr__(self):
return f"GPU {self.index}: {self.name:20.20},{self.memory_used:>10}/{self.memory_total:<10},{self.utilization_gpu}"
def get_info(self):
return {
"index": self.index,
"name": self.name,
"memory_total": self.memory_total,
"memory_free": self.memory_free,
"memory_used": self.memory_used,
"utilization_gpu": self.utilization_gpu
}
class NvidiaSMI():
def __init__(self):
query = [
"index",
"name",
"memory.total",
"memory.free",
"memory.used",
"utilization.gpu",
]
self.gpu_list = list()
for gpu_info in os.popen(f"nvidia-smi --query-gpu={','.join(query)} --format=csv,noheader").readlines():
gpu_info = gpu_info.strip().split(',')
gpu = dict()
for c,info in zip(query, gpu_info):
c = c.replace(".","_")
gpu[c] = info.strip()
self.gpu_list.append(GPU(**gpu))
self.processes = list()
for process in os.popen("nvidia-smi --query-compute-apps=gpu_name,name,used_gpu_memory --format=csv,noheader").readlines():
p = process.split(',')
self.processes.append({
"gpu_name": p[0].strip(),
"process_name": p[1].strip(),
"memory_used": p[2].strip()
})
def show(self):
for gpu in self.gpu_list:
print(gpu)
def to_json(self):
return json.dumps({
"gpus": [gpu.get_info() for gpu in self.gpu_list],
"processes": self.processes
})
def get_info(self):
return {
"gpus": [gpu.get_info() for gpu in self.gpu_list],
"processes": self.processes
}
class RAM():
def __init__(self):
info = os.popen("free -g").read().split('\n')[1].split()
self.info = {
'total': int(info[1]),
'used': int(info[2]),
'available': int(info[6])
}
def to_json(self):
return self.info
def get_info(self):
return self.info