-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_control.py
executable file
·341 lines (295 loc) · 11.7 KB
/
gpu_control.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python
import re
import os
import json
from enum import Enum
class GPUProcess(object):
"""
Process Runing on GPU
"""
def __init__(self):
self.pid = None
self.command = None
self.sm_utilization = None
self.memory_utilization = None
def set_info(self, pid, command, sm_utilization, memory_utilization):
self.pid = pid
self.command = command
self.sm_utilization = sm_utilization
self.memory_utilization = memory_utilization
class GPUDevice(object):
"""
GPU devices class definition
Attributes:
Device ID
Total Memory
...
"""
graphics_mode = Enum("INSTANT", "APPLICATION", "APPLICATION_DEFAULT", "MAX")
sm_free_threshold = 0.03
memory_free_threshold = 0.05
def __init__(self, hostname, gpuid):
"""
Args:
GPUID: gpu device N.O. as gpuid
"""
self.hostname = hostname
self.gpuid = gpuid
self.gpu_model = None
self.gpu_uuid = None
self.processes = []
self.blocked = False
self.listener = None
self.get_gpu_model()
self.task_queues = []
self.email = 'nobody'
def sudo_wrapper(self, command):
"""
Returns:
sudo + command as strings
"""
return "sudo " + command
def get_gpu_model(self):
"""
get gpu model
Returns:
GPU Model Name
"""
command = "nvidia-smi -L"
comamnd = self.sudo_wrapper(command)
fp = os.popen(command)
for line in fp:
line = line.strip()
m = re.match("GPU(.*)\:(.*)\(UUID:(.*)\)", line)
gpuid = int(m.group(1))
gpu_model = m.group(2)
gpu_uuid = m.group(3)
if gpuid == self.gpuid:
self.gpu_model = gpu_model.strip()
self.gpu_uuid = gpu_uuid.strip()
return (self.gpu_model, self.gpu_uuid)
return None
def get_email(self):
return self.email
def set_email(self, email):
self.email = email
def get_core_frequency(self, mode):
"""
get core frequency as int
Args:
Mode: enum type in Enum("INSTANT", "APPLICATION", "APPLICATION_DEFAULT", "MAX")
e.g.
e = Enum("INSTANT", "APPLICATION", "APPLICATION_DEFAULT", "MAX")
instance.get_memory_frequency(e.INSTANT)
"""
command = "nvidia-smi -q -i %d -d CLOCK | grep -i Graphics | \
awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(command)
freq = fp.readlines()[mode.index]
return int(freq)
def get_memory_frequency(self, mode):
"""
get memory frequency as int
Args:
Mode: enum type in Enum("INSTANT", "APPLICATION", "APPLICATION_DEFAULT", "MAX")
e.g.
e = Enum("INSTANT", "APPLICATION", "APPLICATION_DEFAULT", "MAX")
instance.get_memory_frequency(e.INSTANT)
Returns:
return memory frequency
"""
command = "nvidia-smi -q -i %d -d CLOCK | grep -i Memory | \
awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(command)
freq = fp.readlines()[mode.index]
return int(freq)
def set_persistant_mode(self, mode):
"""
Set/Unset GPU frequency
Args:
Mode: int, 1 as persistant on and 0 as persistant off
Returns:
return True or False
"""
command = "nvidia-smi -i %d -pm %d" % (self.gpuid, mode)
command = self.sudo_wrapper(command)
#return True as placeholder
return True
def set_frequency(self, core_frequency, memory_frequency):
"""
set GPU frequency
Args:
core_frequency: int, unit is MHZ
memory_freqency: int, unit is MHZ
Returns:
True when successfully setting the clocks
"""
command = "nvidia-smi -i %d -ac %d,%d" % (self.gpuid, memory_frequency, core_frequency)
command = self.sudo_wrapper(command)
os.system(command)
application_core_freq = self.get_core_frequency(self.__class__.graphics_mode.APPLICATION)
application_memory_freq = self.get_memory_frequency(self.__class__.graphics_mode.APPLICATION)
return (application_core_freq == core_frequency) and (memory_frequency == application_memory_freq)
def get_autoboost(self):
"""
get autoboost Status
Returns:
True if autoboost on else False
"""
command = "nvidia-smi -q -i %d | grep -i 'Auto Boost' | head -n 1 | awk '{print $NF}'" % (self.gpuid, )
fp = os.popen(command)
results = fp.read()
return True if "On" in results else False
def set_autoboost(self, mode):
"""
set autoboost
Args:
Mode: int, 1 as autoboost on and 0 as autoboost off
Returns:
True when successfully setting the clocks
"""
command = "nvidia-smi -i %d --auto-boost-default=%d" % (self.gpuid, mode)
command = self.sudo_wrapper(command)
os.system(command)
return True if self.get_autoboost() == mode else False
def get_utlization(self):
"""
get utilization
Args:
Mode: int, 1 as autoboost on and 0 as autoboost off
Returns:
True when successfully setting the clocks
"""
core_command = "nvidia-smi -q -d utilization -i %d | grep Gpu | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(core_command))
core_utilization = float(0.01 * int(fp.read()))
mem_command = "nvidia-smi -q -d utilization -i %d | grep Memory | grep -vi sam | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(mem_command))
mem_utilization = float(0.01 * int(fp.read()))
return (core_utilization, mem_utilization)
def get_sm_utilization(self):
core_command = "nvidia-smi -q -d utilization -i %d | grep Gpu | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(core_command))
core_utilization = float(0.01 * int(fp.read()))
return core_utilization
def get_memory_utilization(self):
mem_command = "nvidia-smi -q -d utilization -i %d | grep Memory | grep -vi sam | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(mem_command))
mem_utilization = float(0.01 * int(fp.read()))
return mem_utilization
def is_gpu_free(self):
"""
judge whether GPU is free or not
Returns:
True when GPU is free
"""
(core_utilization, mem_utilization) = self.get_utlization()
if core_utilization < self.__class__.sm_free_threshold and \
mem_utilization < self.__class__.memory_free_threshold:
return True
else:
return False
def get_running_process(self):
"""
get information of GPU running process
"""
command = "nvidia-smi pmon -i %d --count 1 | grep -v '^#'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(command))
self.processes = []
for line in fp:
line = line.strip()
info = re.split(' +', line)[0:8]
(gpu, pid, proces_type, sm, mem, enc, dec, command) = info
gp = GPUProcess()
gp.set_info(pid, command, sm, mem)
self.processes.append(gp)
return self.processes
def get_gpu_temperature(self):
command = "nvidia-smi -i %d -q -d TEMPERATURE | grep 'Current Temp' | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(command))
temperature = int(fp.read().strip())
return temperature
def get_gpu_power(self):
command = "nvidia-smi -i %d -q -d POWER | grep -i 'draw' | awk '{print $(NF-1)}'" % (self.gpuid, )
fp = os.popen(self.sudo_wrapper(command))
power = float(fp.read().strip())
return power
def response_status_as_json(self, request):
status = {}
status['gpu_model'] = self.gpu_model
status['instant_core_freq'] = self.get_core_frequency(self.__class__.graphics_mode.INSTANT)
status['instant_mem_freq'] = self.get_memory_frequency(self.__class__.graphics_mode.INSTANT)
status['application_core_freq'] = self.get_core_frequency(self.__class__.graphics_mode.APPLICATION)
status['application_mem_freq'] = self.get_memory_frequency(self.__class__.graphics_mode.APPLICATION)
status['autoboost'] = 'On' if self.get_autoboost() else 'Off'
status['sm_utilization'] = "%.2f" % self.get_sm_utilization()
status['mem_utilization'] = "%.2f" % self.get_memory_utilization()
status['core_freq_ratio'] = int(100.0 * status['instant_core_freq'] / status['application_core_freq'])
status['temperature'] = self.get_gpu_temperature()
status['power'] = self.get_gpu_power()
if request is not None:
request['history_freq'].append(status['instant_core_freq'])
request['history_temperature'].append(status['temperature'])
request['history_power'].append(status['power'])
return json.dumps(status)
class GPUMonitor(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(GPUMonitor, cls).__new__(cls)
return cls.instance
def sudo_wrapper(self, command):
"""
Returns:
sudo + command as strings
"""
return "sudo " + command
def __init__(self):
self.gpulists = []
self.gpu_models_set = set()
self.gpu_uuid_set = set()
def has_gpu(self, gpu):
return gpu.gpu_uuid in self.gpu_uuid_set
def get_gpu_from_model(self, gpu_model):
if gpu_model in self.gpu_models_set:
for g in self.gpulists:
if g.gpu_model == gpu_model:
return g
return None
def register_listener(self, listener):
for g in self.gpulists:
g.listener = listener
def init_local_gpu_lists(self):
command = "nvidia-smi -L | awk -F':' '{print $1}' | awk -F' ' '{print $2}'"
command = self.sudo_wrapper(command)
fp = os.popen(command)
for gpu_id in fp:
gpu_id.strip()
self.add_gpu('127.0.0.1', int(gpu_id))
def add_gpu(self, hostname, gpuid):
gpu = GPUDevice(hostname, gpuid)
if self.has_gpu(gpu):
pass
else:
self.gpulists.append(gpu)
self.gpu_models_set = set([g.gpu_model for g in self.gpulists])
self.gpu_uuid_set = set([g.gpu_uuid for g in self.gpulists])
def del_gpu(self, uuid):
pass
if __name__ == '__main__':
gpu = GPUDevice("127.0.0.1", 0)
graphics_mode = gpu.__class__.graphics_mode
print(gpu.set_persistant_mode(1))
print(gpu.set_frequency(1002, 3505))
print(gpu.get_core_frequency(graphics_mode.INSTANT))
print(gpu.get_memory_frequency(graphics_mode.INSTANT))
print(gpu.set_autoboost(1))
print(gpu.get_autoboost())
print(gpu.get_utlization())
print(gpu.is_gpu_free())
print(gpu.get_running_process())
print(gpu.get_gpu_model())
print('----------')
gm = GPUMonitor()
gm.init_local_gpu_lists()
print(gm.gpu_uuid_set)
print(gm.gpu_models_set)