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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! GPU monitoring and telemetry via Nutanix Prism Central
//!
//! Provides real-time GPU metrics collection, health assessment, utilization
//! history tracking, and capacity forecasting for cuda-wasm workloads running
//! on Nutanix clusters.
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::error::CudaRustError;
use super::config::NutanixConfig;
/// GPU metrics snapshot for a single GPU device
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GpuMetrics {
/// GPU utilization percentage (0-100)
pub utilization_percent: f64,
/// GPU memory currently in use (bytes)
pub memory_used_bytes: u64,
/// Total GPU memory (bytes)
pub memory_total_bytes: u64,
/// GPU temperature in Celsius
pub temperature_celsius: f64,
/// GPU power draw in Watts
pub power_watts: f64,
/// GPU core clock speed in MHz
pub clock_speed_mhz: u32,
/// Fan speed percentage (0-100)
pub fan_speed_percent: f64,
/// ECC error count (single-bit + double-bit)
pub ecc_errors: u64,
}
impl GpuMetrics {
/// Memory utilization as a percentage
pub fn memory_utilization_percent(&self) -> f64 {
if self.memory_total_bytes == 0 {
return 0.0;
}
(self.memory_used_bytes as f64 / self.memory_total_bytes as f64) * 100.0
}
/// Whether the GPU is thermally throttling (above 85C)
pub fn is_throttling(&self) -> bool {
self.temperature_celsius > 85.0
}
}
/// Alert severity levels
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AlertSeverity {
/// Informational alert
Info,
/// Warning - requires attention
Warning,
/// Critical - immediate action needed
Critical,
}
impl std::fmt::Display for AlertSeverity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AlertSeverity::Info => write!(f, "INFO"),
AlertSeverity::Warning => write!(f, "WARNING"),
AlertSeverity::Critical => write!(f, "CRITICAL"),
}
}
}
/// An alert generated from GPU metric analysis
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Alert {
/// Alert severity
pub severity: AlertSeverity,
/// Human-readable alert message
pub message: String,
/// Unix timestamp (seconds) when the alert was generated
pub timestamp: u64,
/// GPU device ID that triggered the alert (if applicable)
pub gpu_id: Option<String>,
}
/// Overall health status for a node
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum HealthStatus {
/// All GPUs operating normally
Healthy,
/// Some GPUs have warnings (high temp, high utilization)
Warning,
/// One or more GPUs have critical issues
Critical,
}
impl std::fmt::Display for HealthStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HealthStatus::Healthy => write!(f, "HEALTHY"),
HealthStatus::Warning => write!(f, "WARNING"),
HealthStatus::Critical => write!(f, "CRITICAL"),
}
}
}
/// Health assessment for a GPU node
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeHealth {
/// Node UUID
pub node_id: String,
/// Overall health status
pub overall_health: HealthStatus,
/// Per-GPU metrics (keyed by GPU device ID)
pub gpu_metrics: Vec<(String, GpuMetrics)>,
/// Active alerts
pub alerts: Vec<Alert>,
}
/// Capacity forecast for a cluster
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CapacityForecast {
/// Cluster ID
pub cluster_id: String,
/// Current average GPU utilization across the cluster
pub current_utilization_percent: f64,
/// Projected utilization at the forecast horizon
pub projected_utilization_percent: f64,
/// Hours until capacity is projected to reach 90%
pub hours_until_90_percent: Option<u32>,
/// Hours until capacity is projected to reach 100%
pub hours_until_full: Option<u32>,
/// Recommended action based on the forecast
pub recommendation: String,
}
/// GPU monitoring client for collecting metrics from Nutanix clusters
pub struct GpuMonitor {
/// Prism Central connection configuration
#[allow(dead_code)]
config: NutanixConfig,
/// HTTP client (when nutanix feature is available)
#[cfg(feature = "nutanix")]
#[allow(dead_code)]
client: reqwest::Client,
}
impl GpuMonitor {
/// Create a new GpuMonitor with the given configuration
pub fn new(config: NutanixConfig) -> Result<Self, CudaRustError> {
#[cfg(feature = "nutanix")]
{
let builder = reqwest::Client::builder().timeout(config.timeout);
let client = builder.build().map_err(|e| {
CudaRustError::RuntimeError(format!("Failed to create HTTP client: {}", e))
})?;
Ok(Self { config, client })
}
#[cfg(not(feature = "nutanix"))]
{
Ok(Self { config })
}
}
/// Collect current GPU metrics for all GPUs on a node
///
/// Polls the Prism Central API for the latest GPU telemetry data.
pub async fn collect_metrics(
&self,
node_id: &str,
) -> Result<Vec<GpuMetrics>, CudaRustError> {
#[cfg(feature = "nutanix")]
{
let _ = node_id;
Err(CudaRustError::RuntimeError(
"Live metrics collection requires Prism Central connection".to_string(),
))
}
#[cfg(not(feature = "nutanix"))]
{
Ok(self.local_metrics(node_id))
}
}
/// Perform a health assessment of a node's GPUs
///
/// Collects metrics and evaluates them against thresholds to determine
/// overall node health and generate any alerts.
pub async fn check_health(
&self,
node_id: &str,
) -> Result<NodeHealth, CudaRustError> {
let metrics = self.collect_metrics(node_id).await?;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let mut alerts = Vec::new();
let mut worst_health = HealthStatus::Healthy;
let gpu_metrics: Vec<(String, GpuMetrics)> = metrics
.into_iter()
.enumerate()
.map(|(i, m)| {
let gpu_id = format!("{}-gpu-{}", node_id, i);
// Temperature checks
if m.temperature_celsius > 90.0 {
alerts.push(Alert {
severity: AlertSeverity::Critical,
message: format!(
"GPU {} temperature critical: {:.1}C",
gpu_id, m.temperature_celsius
),
timestamp: now,
gpu_id: Some(gpu_id.clone()),
});
worst_health = HealthStatus::Critical;
} else if m.temperature_celsius > 80.0 {
alerts.push(Alert {
severity: AlertSeverity::Warning,
message: format!(
"GPU {} temperature high: {:.1}C",
gpu_id, m.temperature_celsius
),
timestamp: now,
gpu_id: Some(gpu_id.clone()),
});
if worst_health != HealthStatus::Critical {
worst_health = HealthStatus::Warning;
}
}
// Memory utilization checks
let mem_pct = m.memory_utilization_percent();
if mem_pct > 95.0 {
alerts.push(Alert {
severity: AlertSeverity::Critical,
message: format!(
"GPU {} memory nearly exhausted: {:.1}%",
gpu_id, mem_pct
),
timestamp: now,
gpu_id: Some(gpu_id.clone()),
});
worst_health = HealthStatus::Critical;
} else if mem_pct > 85.0 {
alerts.push(Alert {
severity: AlertSeverity::Warning,
message: format!(
"GPU {} memory utilization high: {:.1}%",
gpu_id, mem_pct
),
timestamp: now,
gpu_id: Some(gpu_id.clone()),
});
if worst_health != HealthStatus::Critical {
worst_health = HealthStatus::Warning;
}
}
// ECC error checks
if m.ecc_errors > 0 {
let severity = if m.ecc_errors > 10 {
worst_health = HealthStatus::Critical;
AlertSeverity::Critical
} else {
if worst_health != HealthStatus::Critical {
worst_health = HealthStatus::Warning;
}
AlertSeverity::Warning
};
alerts.push(Alert {
severity,
message: format!(
"GPU {} has {} ECC errors",
gpu_id, m.ecc_errors
),
timestamp: now,
gpu_id: Some(gpu_id.clone()),
});
}
(gpu_id, m)
})
.collect();
Ok(NodeHealth {
node_id: node_id.to_string(),
overall_health: worst_health,
gpu_metrics,
alerts,
})
}
/// Retrieve utilization history for GPUs on a node
///
/// Returns timestamped metric snapshots over the requested duration.
pub async fn get_utilization_history(
&self,
node_id: &str,
duration_minutes: u32,
) -> Result<Vec<(u64, GpuMetrics)>, CudaRustError> {
#[cfg(feature = "nutanix")]
{
let _ = (node_id, duration_minutes);
Err(CudaRustError::RuntimeError(
"History collection requires Prism Central connection".to_string(),
))
}
#[cfg(not(feature = "nutanix"))]
{
Ok(self.local_utilization_history(node_id, duration_minutes))
}
}
/// Predict future capacity usage for a cluster using linear projection
///
/// Analyzes recent utilization trends to estimate when the cluster
/// will reach capacity thresholds (90% and 100%).
pub async fn predict_capacity(
&self,
cluster_id: &str,
hours_ahead: u32,
) -> Result<CapacityForecast, CudaRustError> {
#[cfg(feature = "nutanix")]
{
let _ = (cluster_id, hours_ahead);
Err(CudaRustError::RuntimeError(
"Capacity prediction requires Prism Central connection".to_string(),
))
}
#[cfg(not(feature = "nutanix"))]
{
Ok(self.local_capacity_forecast(cluster_id, hours_ahead))
}
}
// --- Local system probing for non-nutanix builds ---
/// Collect GPU metrics by querying `nvidia-smi` on the local system.
///
/// Returns an empty vector if no NVIDIA GPUs or nvidia-smi is available.
#[cfg(not(feature = "nutanix"))]
fn local_metrics(&self, _node_id: &str) -> Vec<GpuMetrics> {
if let Ok(output) = std::process::Command::new("nvidia-smi")
.args([
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu,power.draw,clocks.current.graphics,fan.speed",
"--format=csv,noheader,nounits",
])
.output()
{
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
return stdout
.lines()
.filter_map(|line| {
let parts: Vec<&str> = line.split(", ").collect();
if parts.len() >= 7 {
Some(GpuMetrics {
utilization_percent: parts[0]
.trim()
.parse()
.unwrap_or(0.0),
memory_used_bytes: parts[1]
.trim()
.parse::<u64>()
.unwrap_or(0)
* 1024
* 1024,
memory_total_bytes: parts[2]
.trim()
.parse::<u64>()
.unwrap_or(0)
* 1024
* 1024,
temperature_celsius: parts[3]
.trim()
.parse()
.unwrap_or(0.0),
power_watts: parts[4]
.trim()
.parse()
.unwrap_or(0.0),
clock_speed_mhz: parts[5]
.trim()
.parse()
.unwrap_or(0),
fan_speed_percent: parts[6]
.trim()
.parse()
.unwrap_or(0.0),
ecc_errors: 0,
})
} else {
None
}
})
.collect();
}
}
// No GPU metrics available
Vec::new()
}
/// Return utilization history as a single current-time snapshot.
///
/// Without a time-series database we cannot provide true history,
/// so we return one data point at the current timestamp per GPU.
#[cfg(not(feature = "nutanix"))]
fn local_utilization_history(
&self,
node_id: &str,
_duration_minutes: u32,
) -> Vec<(u64, GpuMetrics)> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
self.local_metrics(node_id)
.into_iter()
.map(|m| (now, m))
.collect()
}
/// Generate a capacity forecast based on current local GPU utilization.
///
/// Uses a simple linear projection with a 0.5%/hour growth assumption.
/// Returns a 0% utilization forecast when no GPU metrics are available.
#[cfg(not(feature = "nutanix"))]
fn local_capacity_forecast(
&self,
cluster_id: &str,
hours_ahead: u32,
) -> CapacityForecast {
let metrics = self.local_metrics(cluster_id);
let current_util = metrics
.first()
.map(|m| m.utilization_percent)
.unwrap_or(0.0);
let growth_rate = 0.5; // 0.5% per hour assumption
let projected =
(current_util + growth_rate * hours_ahead as f64).min(100.0);
CapacityForecast {
cluster_id: cluster_id.to_string(),
current_utilization_percent: current_util,
projected_utilization_percent: projected,
hours_until_90_percent: if current_util < 90.0 {
Some(((90.0 - current_util) / growth_rate) as u32)
} else {
Some(0)
},
hours_until_full: if current_util < 100.0 {
Some(((100.0 - current_util) / growth_rate) as u32)
} else {
Some(0)
},
recommendation: if projected > 90.0 {
"Consider adding GPU nodes".to_string()
} else if projected > 75.0 {
"Monitor closely".to_string()
} else {
"Capacity sufficient".to_string()
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_monitor() -> GpuMonitor {
let config = NutanixConfig::new("https://prism.example.com:9440", "test-key");
GpuMonitor::new(config).unwrap()
}
#[test]
fn test_gpu_metrics_memory_utilization() {
let m = GpuMetrics {
utilization_percent: 50.0,
memory_used_bytes: 40 * 1024 * 1024 * 1024,
memory_total_bytes: 80 * 1024 * 1024 * 1024,
temperature_celsius: 70.0,
power_watts: 250.0,
clock_speed_mhz: 1400,
fan_speed_percent: 50.0,
ecc_errors: 0,
};
let pct = m.memory_utilization_percent();
assert!((pct - 50.0).abs() < 0.01);
}
#[test]
fn test_gpu_metrics_throttling() {
let normal = GpuMetrics {
utilization_percent: 80.0,
memory_used_bytes: 0,
memory_total_bytes: 80 * 1024 * 1024 * 1024,
temperature_celsius: 75.0,
power_watts: 250.0,
clock_speed_mhz: 1400,
fan_speed_percent: 50.0,
ecc_errors: 0,
};
assert!(!normal.is_throttling());
let hot = GpuMetrics {
temperature_celsius: 92.0,
..normal
};
assert!(hot.is_throttling());
}
#[test]
fn test_alert_severity_display() {
assert_eq!(AlertSeverity::Info.to_string(), "INFO");
assert_eq!(AlertSeverity::Warning.to_string(), "WARNING");
assert_eq!(AlertSeverity::Critical.to_string(), "CRITICAL");
}
#[test]
fn test_health_status_display() {
assert_eq!(HealthStatus::Healthy.to_string(), "HEALTHY");
assert_eq!(HealthStatus::Warning.to_string(), "WARNING");
assert_eq!(HealthStatus::Critical.to_string(), "CRITICAL");
}
#[tokio::test]
async fn test_local_collect_metrics() {
let monitor = make_monitor();
let metrics = monitor.collect_metrics("node-001").await.unwrap();
// On systems without GPUs this will be empty -- that is correct.
// On GPU systems each metric should have sensible values.
for m in &metrics {
assert!(m.utilization_percent >= 0.0 && m.utilization_percent <= 100.0);
assert!(m.memory_total_bytes >= m.memory_used_bytes);
}
}
#[tokio::test]
async fn test_local_check_health() {
let monitor = make_monitor();
let health = monitor.check_health("node-001").await.unwrap();
assert_eq!(health.node_id, "node-001");
// On systems without GPUs, gpu_metrics will be empty and health is Healthy
// On GPU systems, health depends on actual temperature/utilization
if health.gpu_metrics.is_empty() {
assert_eq!(health.overall_health, HealthStatus::Healthy);
assert!(health.alerts.is_empty());
}
}
#[tokio::test]
async fn test_local_utilization_history() {
let monitor = make_monitor();
let history = monitor
.get_utilization_history("node-001", 30)
.await
.unwrap();
// Without GPUs this is empty; with GPUs we get one snapshot per GPU
for (ts, _metrics) in &history {
assert!(*ts > 0);
}
}
#[tokio::test]
async fn test_local_predict_capacity() {
let monitor = make_monitor();
let forecast = monitor
.predict_capacity("cluster-001", 48)
.await
.unwrap();
assert_eq!(forecast.cluster_id, "cluster-001");
// Projected should always be >= current (growth rate is positive)
assert!(
forecast.projected_utilization_percent
>= forecast.current_utilization_percent
);
assert!(forecast.hours_until_90_percent.is_some());
assert!(forecast.hours_until_full.is_some());
}
#[tokio::test]
async fn test_local_predict_capacity_long_horizon() {
let monitor = make_monitor();
let forecast = monitor
.predict_capacity("cluster-001", 500)
.await
.unwrap();
// With 0.5%/hr growth over 500 hours, projected should cap at 100
assert!(forecast.projected_utilization_percent <= 100.0);
}
#[test]
fn test_memory_utilization_zero_total() {
let m = GpuMetrics {
utilization_percent: 0.0,
memory_used_bytes: 0,
memory_total_bytes: 0,
temperature_celsius: 0.0,
power_watts: 0.0,
clock_speed_mhz: 0,
fan_speed_percent: 0.0,
ecc_errors: 0,
};
assert!((m.memory_utilization_percent()).abs() < 0.01);
}
#[test]
fn test_monitor_creation() {
let config = NutanixConfig::new("https://prism.example.com:9440", "key");
let monitor = GpuMonitor::new(config);
assert!(monitor.is_ok());
}
}