forked from sczhou/CodeFormer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVRAMUsageMonitor.py
41 lines (33 loc) · 1.08 KB
/
VRAMUsageMonitor.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
import pynvml
import threading
import time
class VRAMUsageMonitor(threading.Thread):
stop_flag = False
max_usage = 0
total = -1
def __init__(self):
threading.Thread.__init__(self)
def run(self):
try:
pynvml.nvmlInit()
except:
print(f"Unable to initialize NVIDIA management. No memory stats. \n")
return
print(f"Recording max memory usage...\n")
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
self.total = pynvml.nvmlDeviceGetMemoryInfo(handle).total
print(f"Total memory available {self.total}")
while not self.stop_flag:
m = pynvml.nvmlDeviceGetMemoryInfo(handle)
self.max_usage = max(self.max_usage, m.used)
# print(self.max_usage)
time.sleep(0.1)
print(f"Stopped recording.\n")
pynvml.nvmlShutdown()
def read(self):
return self.max_usage, self.total
def stop(self):
self.stop_flag = True
def read_and_stop(self):
self.stop_flag = True
return self.max_usage, self.total