-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgputimekeeper.py
80 lines (69 loc) · 2.81 KB
/
gputimekeeper.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
import subprocess
import time
import threading
class GPUTimeKeeper:
def __init__(self):
self.gpu_type = self._detect_gpu_type()
self.worker_thread = None
self.running = False
self.execution_time_ms = 0
def _detect_gpu_type(self):
try:
# Check for NVIDIA GPU
subprocess.check_output(['nvidia-smi'])
return 'nvidia'
except FileNotFoundError:
# Check for AMD GPU (stub)
# You can add the actual detection logic for AMD GPUs here
return 'amd'
except Exception:
# Check for Intel GPU (stub)
# You can add the actual detection logic for Intel GPUs here
return 'intel'
def _get_nvidia_gpu_utilization(self):
# Run the nvidia-smi command and capture the output
output = subprocess.check_output(['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'])
output = output.decode('utf-8')
utilization_gpu = int(output.strip())
return utilization_gpu
def _get_amd_gpu_utilization(self):
# Stub for AMD GPU utilization
# You can add the actual logic to retrieve AMD GPU utilization here
return 0
def _get_intel_gpu_utilization(self):
# Stub for Intel GPU utilization
# You can add the actual logic to retrieve Intel GPU utilization here
return 0
def _monitor_gpu_utilization(self):
start_time = time.time()
while self.running:
if self.gpu_type == 'nvidia':
utilization_gpu = self._get_nvidia_gpu_utilization()
elif self.gpu_type == 'amd':
utilization_gpu = self._get_amd_gpu_utilization()
elif self.gpu_type == 'intel':
utilization_gpu = self._get_intel_gpu_utilization()
if utilization_gpu > self.threshold:
current_time = time.time()
self.execution_time_ms += (current_time - start_time) * 1000
start_time = current_time
time.sleep(0.1)
def start_timer(self, threshold):
if not self.running:
self.threshold = threshold
self.execution_time_ms = 0
self.running = True
self.worker_thread = threading.Thread(target=self._monitor_gpu_utilization)
self.worker_thread.start()
else:
print("Timer is already running. Stop the timer before starting a new one.")
def stop_timer(self):
if self.running:
self.running = False
self.worker_thread.join()
execution_time_ms = self.execution_time_ms
self.execution_time_ms = 0
return execution_time_ms
else:
print("Timer is not running. Start the timer before stopping it.")
return 0