-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveGPUinfo.py
173 lines (149 loc) · 6.02 KB
/
saveGPUinfo.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import time
import pickle
import paramiko
from multiprocessing.pool import ThreadPool as Pool
from datetime import datetime
import xml.etree.ElementTree as ET
def get_gpu_infos(nvidiasmi_output):
gpus = nvidiasmi_output.findall('gpu')
gpu_infos = []
for idx, gpu in enumerate(gpus):
model = gpu.find('product_name').text
total_memory = gpu.find('fb_memory_usage').find('total').text
used_memory = gpu.find('fb_memory_usage').find('used').text
gpu_util = gpu.find('utilization').find('gpu_util').text
processes = gpu.findall('processes')[0]
pids = [process.find('pid').text for process in processes]
gpu_infos.append({'idx': idx, 'model': model, 'pids': pids, 'gpu_util': gpu_util,
'used_mem': used_memory, 'total_mem': total_memory})
return gpu_infos
def get_users_by_pid(ps_output):
users_by_pid = {}
for line in ps_output.strip().split('\n'):
pid, user = line.split()
users_by_pid[pid] = user
return users_by_pid
def get_program_by_pid(ps_output):
program_by_pid = {}
for line in ps_output.strip().split('\n'):
line = line.split()
program_by_pid[line[0]] = ' '.join(line[1:])
return program_by_pid
def start_connections(server_list):
clients = []
servers = []
for server in server_list:
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
# client.connect(server, gss_auth=True, gss_kex=True)
client.connect(server)
clients.append(client)
servers.append(server)
except:
print('Cannot connect to ', server)
return clients, servers
def end_connections(clients):
for client in clients:
client.close()
def run_cmd(client, cmd):
_, stdout, _ = client.exec_command(cmd)
stdout = stdout.read().decode(encoding='UTF-8')
return stdout
def get_server_info(server, client):
try:
gpu_infos = ET.fromstring(run_cmd(client, 'nvidia-smi -q -x'))
gpu_infos = get_gpu_infos(gpu_infos)
except Exception as e:
print(' => Server error! Cannot fetch from {} with error:\n\t\t{}'.format(server, e))
return server, [], []
pids = [pid for gpu_info in gpu_infos for pid in gpu_info['pids']]
if len(pids) > 0:
PS_CMD = 'ps -o pid= -o ruser= -p {pids}'.format(pids=','.join(pids))
ps = run_cmd(client, PS_CMD)
users_by_pid = get_users_by_pid(ps)
PS_CMD = 'ps -o pid= -o args= -p {pids}'.format(pids=','.join(pids))
ps = run_cmd(client, PS_CMD)
program_by_pid = get_program_by_pid(ps)
else:
users_by_pid = {}
program_by_pid = {}
results = []
details = []
for gpu_info in gpu_infos:
users = set((users_by_pid[pid] for pid in gpu_info['pids']))
if len(gpu_info['pids']) == 0:
status = 'Free'
else:
used_mem = gpu_info['used_mem']
total_mem = gpu_info['total_mem']
gpu_util = gpu_info['gpu_util']
status = '{} out of {} used by {} (GPU utilization: {})'.format(used_mem, total_mem,
', '.join(users), gpu_util)
for pid in gpu_info['pids']:
user = users_by_pid[pid]
program = program_by_pid[pid]
details.append((server.split('.')[0], gpu_info['idx'], user, program))
results.append('GPU {} ({}): {}'.format(gpu_info['idx'],
gpu_info['model'],
status))
return server, results, details
def get_servers_info(servers, clients):
server_infos = {}
user_infos = []
pool_results = []
with Pool(processes=len(servers)) as pool:
for server, client in zip(servers, clients):
r = pool.apply_async(get_server_info, (server, client))
pool_results.append(r)
for r in pool_results:
r.wait(10)
if r.ready():
server, results, details = r.get(1)
print(' => Got {}'.format(server))
server_infos[server] = results
user_infos += details
return server_infos, user_infos
def gpu_monitor_server(servers, clients, servers_all):
server_info, user_infos = get_servers_info(servers, clients)
results = []
for server in servers_all:
tmp = [server]
try:
if server_info[server]:
tmp.append(server_info[server])
else:
tmp.append(["This server is down! Keep calm and email Stephen."])
except:
tmp.append(["This server is down! Keep calm and email Stephen."])
results.append(tmp)
timestamp = time.time()
timestamp = datetime.fromtimestamp(timestamp)
return {'server_info' : results,
'user_info' : user_infos,
'timestamp' : timestamp}
if __name__ == '__main__':
servers_all = ['nescafe.cs.washington.edu', 'sanka.cs.washington.edu',
'arabica.cs.washington.edu', 'chemex.cs.washington.edu',
'lungo.cs.washington.edu', 'ristretto.cs.washington.edu',
'latte.cs.washington.edu', 'sidamo.cs.washington.edu',
'caffeine.cs.washington.edu','cortado.cs.washington.edu']
clients, servers = start_connections(servers_all)
print('=> Established connections')
connect_t = time.time()
while True:
try:
print('=> Trying to get server info')
info = gpu_monitor_server(servers, clients, servers_all)
print('=> Server info gathered')
with open('/tmp/info.pkl', 'wb') as f:
pickle.dump(info, f)
print('=> Server info saved')
except Exception as e:
print(e)
if time.time() - connect_t > 600:
end_connections(clients)
clients, servers = start_connections(servers_all)
print('=> Re-established connections')
connect_t = time.time()
time.sleep(20)