-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_dashboard.py
More file actions
287 lines (231 loc) · 9.26 KB
/
Copy pathgpu_dashboard.py
File metadata and controls
287 lines (231 loc) · 9.26 KB
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
#!/usr/bin/env python3
"""
GPU Dashboard - Web-based real-time GPU monitoring
"""
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import threading
import time
import queue
try:
import nvidia_ml_py as pynvml
except ImportError:
import pynvml
# Global data storage
metrics_queue = queue.Queue(maxsize=1000)
gpu_handle = None
def initialize_gpu():
"""Initialize GPU monitoring"""
global gpu_handle
try:
pynvml.nvmlInit()
gpu_handle = pynvml.nvmlDeviceGetHandleByIndex(0) # Use first GPU
return True
except:
return False
def collect_metrics():
"""Background thread to collect GPU metrics"""
global gpu_handle, metrics_queue
while True:
try:
if gpu_handle is None:
time.sleep(1)
continue
# Collect metrics
util = pynvml.nvmlDeviceGetUtilizationRates(gpu_handle)
mem_info = pynvml.nvmlDeviceGetMemoryInfo(gpu_handle)
temp = pynvml.nvmlDeviceGetTemperature(gpu_handle, pynvml.NVML_TEMPERATURE_GPU)
power = pynvml.nvmlDeviceGetPowerUsage(gpu_handle) // 1000
metrics = {
'timestamp': datetime.now(),
'gpu_util': util.gpu,
'memory_util': util.memory,
'memory_used_mb': mem_info.used // (1024**2),
'memory_total_mb': mem_info.total // (1024**2),
'temperature': temp,
'power': power
}
# Add to queue (remove old if full)
if metrics_queue.full():
try:
metrics_queue.get_nowait()
except:
pass
metrics_queue.put(metrics)
except Exception as e:
print(f"Error collecting metrics: {e}")
time.sleep(1) # Collect every second
def get_recent_metrics(minutes=5):
"""Get recent metrics as DataFrame"""
metrics_list = []
temp_queue = queue.Queue()
# Extract all metrics from queue
while not metrics_queue.empty():
try:
metric = metrics_queue.get_nowait()
metrics_list.append(metric)
temp_queue.put(metric)
except:
break
# Put metrics back
while not temp_queue.empty():
try:
metrics_queue.put(temp_queue.get_nowait())
except:
break
if not metrics_list:
return pd.DataFrame()
# Filter to recent data
cutoff_time = datetime.now() - timedelta(minutes=minutes)
recent_metrics = [m for m in metrics_list if m['timestamp'] > cutoff_time]
return pd.DataFrame(recent_metrics)
# Initialize Dash app
app = dash.Dash(__name__)
app.title = "GPU Utilization Optimizer Dashboard"
# Layout
app.layout = html.Div([
html.H1("🖥️ GPU Utilization Optimizer", style={'textAlign': 'center', 'color': '#2E86AB'}),
html.H3("Real-time GPU Performance Dashboard", style={'textAlign': 'center', 'color': '#A23B72'}),
# Current metrics cards
html.Div([
html.Div([
html.H4("GPU Utilization", style={'textAlign': 'center'}),
html.H2(id="gpu-util-display", style={'textAlign': 'center', 'color': '#F18F01'})
], className="metric-card", style={'width': '23%', 'display': 'inline-block', 'margin': '1%', 'padding': '20px', 'backgroundColor': '#f0f0f0', 'borderRadius': '10px'}),
html.Div([
html.H4("Memory Usage", style={'textAlign': 'center'}),
html.H2(id="memory-display", style={'textAlign': 'center', 'color': '#C73E1D'})
], className="metric-card", style={'width': '23%', 'display': 'inline-block', 'margin': '1%', 'padding': '20px', 'backgroundColor': '#f0f0f0', 'borderRadius': '10px'}),
html.Div([
html.H4("Temperature", style={'textAlign': 'center'}),
html.H2(id="temp-display", style={'textAlign': 'center', 'color': '#2E86AB'})
], className="metric-card", style={'width': '23%', 'display': 'inline-block', 'margin': '1%', 'padding': '20px', 'backgroundColor': '#f0f0f0', 'borderRadius': '10px'}),
html.Div([
html.H4("Power Draw", style={'textAlign': 'center'}),
html.H2(id="power-display", style={'textAlign': 'center', 'color': '#A23B72'})
], className="metric-card", style={'width': '23%', 'display': 'inline-block', 'margin': '1%', 'padding': '20px', 'backgroundColor': '#f0f0f0', 'borderRadius': '10px'}),
]),
# Charts
html.Div([
html.Div([
dcc.Graph(id="utilization-chart")
], style={'width': '50%', 'display': 'inline-block'}),
html.Div([
dcc.Graph(id="temperature-chart")
], style={'width': '50%', 'display': 'inline-block'}),
]),
html.Div([
html.Div([
dcc.Graph(id="memory-chart")
], style={'width': '50%', 'display': 'inline-block'}),
html.Div([
dcc.Graph(id="power-chart")
], style={'width': '50%', 'display': 'inline-block'}),
]),
# Auto-refresh
dcc.Interval(
id='interval-component',
interval=2*1000, # Update every 2 seconds
n_intervals=0
),
html.Footer([
html.P("GPU Utilization Optimizer Dashboard - Real-time monitoring",
style={'textAlign': 'center', 'marginTop': '20px', 'color': '#666'})
])
])
# Callbacks
@app.callback(
[Output('gpu-util-display', 'children'),
Output('memory-display', 'children'),
Output('temp-display', 'children'),
Output('power-display', 'children')],
[Input('interval-component', 'n_intervals')]
)
def update_current_metrics(n):
df = get_recent_metrics(1) # Last minute
if df.empty:
return "N/A", "N/A", "N/A", "N/A"
latest = df.iloc[-1]
return (
f"{latest['gpu_util']}%",
f"{latest['memory_used_mb']:,} MB ({latest['memory_util']}%)",
f"{latest['temperature']}°C",
f"{latest['power']} W"
)
@app.callback(
Output('utilization-chart', 'figure'),
[Input('interval-component', 'n_intervals')]
)
def update_utilization_chart(n):
df = get_recent_metrics()
if df.empty:
return go.Figure().add_annotation(text="No data available", x=0.5, y=0.5)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['gpu_util'],
mode='lines', name='GPU Utilization', line=dict(color='#F18F01')))
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['memory_util'],
mode='lines', name='Memory Utilization', line=dict(color='#C73E1D')))
fig.update_layout(title='GPU & Memory Utilization (%)',
xaxis_title='Time', yaxis_title='Utilization (%)',
yaxis=dict(range=[0, 100]))
return fig
@app.callback(
Output('temperature-chart', 'figure'),
[Input('interval-component', 'n_intervals')]
)
def update_temperature_chart(n):
df = get_recent_metrics()
if df.empty:
return go.Figure().add_annotation(text="No data available", x=0.5, y=0.5)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['temperature'],
mode='lines', name='Temperature', line=dict(color='#2E86AB')))
fig.update_layout(title='GPU Temperature (°C)',
xaxis_title='Time', yaxis_title='Temperature (°C)')
return fig
@app.callback(
Output('memory-chart', 'figure'),
[Input('interval-component', 'n_intervals')]
)
def update_memory_chart(n):
df = get_recent_metrics()
if df.empty:
return go.Figure().add_annotation(text="No data available", x=0.5, y=0.5)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['memory_used_mb'],
mode='lines', name='Memory Used', line=dict(color='#A23B72')))
fig.update_layout(title='GPU Memory Usage (MB)',
xaxis_title='Time', yaxis_title='Memory (MB)')
return fig
@app.callback(
Output('power-chart', 'figure'),
[Input('interval-component', 'n_intervals')]
)
def update_power_chart(n):
df = get_recent_metrics()
if df.empty:
return go.Figure().add_annotation(text="No data available", x=0.5, y=0.5)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['power'],
mode='lines', name='Power Draw', line=dict(color='#F18F01')))
fig.update_layout(title='GPU Power Draw (W)',
xaxis_title='Time', yaxis_title='Power (W)')
return fig
if __name__ == '__main__':
# Initialize GPU
if not initialize_gpu():
print("❌ Failed to initialize GPU monitoring")
exit(1)
print("✅ GPU monitoring initialized")
# Start background metrics collection
metrics_thread = threading.Thread(target=collect_metrics, daemon=True)
metrics_thread.start()
print("🚀 Starting dashboard on http://localhost:8050")
print("Press Ctrl+C to stop")
# Run dashboard
app.run_server(debug=False, host='localhost', port=8050)