trueno-gpu 0.4.29

Pure Rust PTX generation for NVIDIA CUDA - no LLVM, no nvcc
Documentation
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
//! Core device tests: Device ID, Device Type, CPU Device basics,
//! Device Snapshot, Throttle Reason, and MockDevice (H001-H012)

use super::*;

// =========================================================================
// H001: Device ID Tests
// =========================================================================

#[test]
fn h001_device_id_cpu() {
    let id = DeviceId::cpu();
    assert_eq!(id.device_type, DeviceType::Cpu);
    assert_eq!(id.index, 0);
    assert_eq!(format!("{}", id), "CPU");
}

#[test]
fn h001_device_id_nvidia() {
    let id = DeviceId::nvidia(0);
    assert_eq!(id.device_type, DeviceType::NvidiaGpu);
    assert_eq!(id.index, 0);
    assert_eq!(format!("{}", id), "NVIDIA:0");

    let id2 = DeviceId::nvidia(1);
    assert_eq!(format!("{}", id2), "NVIDIA:1");
}

#[test]
fn h001_device_id_amd() {
    let id = DeviceId::amd(0);
    assert_eq!(id.device_type, DeviceType::AmdGpu);
    assert_eq!(id.index, 0);
    assert_eq!(format!("{}", id), "AMD:0");
}

#[test]
fn h001_device_id_equality() {
    let id1 = DeviceId::nvidia(0);
    let id2 = DeviceId::nvidia(0);
    let id3 = DeviceId::nvidia(1);
    let id4 = DeviceId::amd(0);

    assert_eq!(id1, id2);
    assert_ne!(id1, id3);
    assert_ne!(id1, id4);
}

// =========================================================================
// H002: Device Type Tests
// =========================================================================

#[test]
fn h002_device_type_display() {
    assert_eq!(format!("{}", DeviceType::Cpu), "CPU");
    assert_eq!(format!("{}", DeviceType::NvidiaGpu), "NVIDIA GPU");
    assert_eq!(format!("{}", DeviceType::AmdGpu), "AMD GPU");
    assert_eq!(format!("{}", DeviceType::IntelGpu), "Intel GPU");
    assert_eq!(format!("{}", DeviceType::AppleSilicon), "Apple Silicon");
}

// =========================================================================
// H003: CPU Device Tests
// =========================================================================

#[test]
fn h003_cpu_device_creation() {
    let cpu = CpuDevice::new();
    assert_eq!(cpu.device_type(), DeviceType::Cpu);
    assert_eq!(cpu.device_id(), DeviceId::cpu());
    assert!(cpu.compute_unit_count() > 0);
}

#[test]
fn h003_cpu_device_default() {
    let cpu = CpuDevice::default();
    assert!(cpu.compute_unit_count() > 0);
}

#[test]
fn h003_cpu_device_name() {
    let cpu = CpuDevice::new();
    // Name should be non-empty
    assert!(!cpu.device_name().is_empty());
}

#[test]
fn h003_cpu_device_memory_total() {
    let cpu = CpuDevice::new();
    // Should have some memory (at least 1GB in practice)
    let total = cpu.memory_total_bytes().unwrap_or(0);
    assert!(total > 0, "CPU should report total memory");
}

#[test]
fn h003_cpu_device_refresh() {
    let mut cpu = CpuDevice::new();
    assert!(cpu.refresh().is_ok());
    // After refresh, metrics should be populated
}

// =========================================================================
// H004: Device Snapshot Tests
// =========================================================================

#[test]
fn h004_device_snapshot_capture() {
    let cpu = CpuDevice::new();
    let snapshot = DeviceSnapshot::capture(&cpu);
    assert!(snapshot.is_ok());

    let snap = snapshot.unwrap();
    assert_eq!(snap.device_id, DeviceId::cpu());
    assert!(snap.timestamp_ms > 0);
}

#[test]
fn h004_device_snapshot_memory_percent() {
    let snap = DeviceSnapshot {
        device_id: DeviceId::cpu(),
        timestamp_ms: 0,
        compute_utilization: 50.0,
        memory_used_bytes: 50 * 1024 * 1024 * 1024, // 50 GB
        memory_total_bytes: 100 * 1024 * 1024 * 1024, // 100 GB
        temperature_c: 45.0,
        power_watts: 100.0,
        clock_mhz: 3000,
    };

    assert!((snap.memory_usage_percent() - 50.0).abs() < 0.01);
}

#[test]
fn h004_device_snapshot_memory_percent_zero_total() {
    let snap = DeviceSnapshot {
        device_id: DeviceId::cpu(),
        timestamp_ms: 0,
        compute_utilization: 0.0,
        memory_used_bytes: 0,
        memory_total_bytes: 0, // Division by zero case
        temperature_c: 0.0,
        power_watts: 0.0,
        clock_mhz: 0,
    };

    assert!((snap.memory_usage_percent() - 0.0).abs() < 0.01);
}

// =========================================================================
// H005: Throttle Reason Tests
// =========================================================================

#[test]
fn h005_throttle_reason_display() {
    assert_eq!(format!("{}", ThrottleReason::None), "None");
    assert_eq!(format!("{}", ThrottleReason::Thermal), "Thermal");
    assert_eq!(format!("{}", ThrottleReason::Power), "Power");
}

// =========================================================================
// H006: Derived Metrics Tests (ComputeDevice trait defaults)
// =========================================================================

#[test]
fn h006_memory_usage_percent() {
    let cpu = CpuDevice::new();
    let percent = cpu.memory_usage_percent();
    // On Linux, this should always succeed
    assert!(percent.is_ok());
    let p = percent.unwrap();
    assert!(p >= 0.0 && p <= 100.0);
}

#[test]
fn h006_memory_available_bytes() {
    let cpu = CpuDevice::new();
    // On Linux, these should always succeed
    let avail = cpu.memory_available_bytes().unwrap();
    let total = cpu.memory_total_bytes().unwrap();
    assert!(avail <= total);
}

#[test]
fn h006_memory_mb_helpers() {
    let cpu = CpuDevice::new();
    // On Linux, these should always succeed
    let used_mb = cpu.memory_used_mb().unwrap();
    let total_mb = cpu.memory_total_mb().unwrap();
    assert!(used_mb <= total_mb);
}

#[test]
fn h006_memory_gb_helper() {
    let cpu = CpuDevice::new();
    // On Linux, this should always succeed
    let total_gb = cpu.memory_total_gb().unwrap();
    // Should be positive (most systems have > 1GB)
    assert!(total_gb > 0.0);
}

// =========================================================================
// H007: Thermal Throttling Detection
// =========================================================================

#[test]
fn h007_thermal_throttling_detection() {
    let cpu = CpuDevice::new();
    // Just verify it doesn't panic
    let _ = cpu.is_thermal_throttling();
}

// =========================================================================
// H008: Power Throttling Detection
// =========================================================================

#[test]
fn h008_power_throttling_detection() {
    let cpu = CpuDevice::new();
    // CPU doesn't support power metrics, but shouldn't panic
    let _ = cpu.is_power_throttling();
}

// =========================================================================
// H009: Edge Cases
// =========================================================================

#[test]
fn h009_cpu_unsupported_metrics() {
    let cpu = CpuDevice::new();

    // PCIe metrics should return NotSupported
    assert!(cpu.pcie_tx_bytes_per_sec().is_err());
    assert!(cpu.pcie_rx_bytes_per_sec().is_err());
    assert_eq!(cpu.pcie_generation(), 0);
    assert_eq!(cpu.pcie_width(), 0);
}

#[test]
fn h009_device_id_hash() {
    use std::collections::HashSet;

    let mut set = HashSet::new();
    set.insert(DeviceId::cpu());
    set.insert(DeviceId::nvidia(0));
    set.insert(DeviceId::nvidia(1));
    set.insert(DeviceId::amd(0));

    assert_eq!(set.len(), 4);

    // Duplicate should not increase size
    set.insert(DeviceId::cpu());
    assert_eq!(set.len(), 4);
}

// =========================================================================
// H010: Additional Display Coverage
// =========================================================================

#[test]
fn h010_device_id_intel_display() {
    let id = DeviceId::new(DeviceType::IntelGpu, 0);
    assert_eq!(format!("{}", id), "Intel:0");
    assert_eq!(id.device_type, DeviceType::IntelGpu);

    let id2 = DeviceId::new(DeviceType::IntelGpu, 2);
    assert_eq!(format!("{}", id2), "Intel:2");
}

#[test]
fn h010_device_id_apple_display() {
    let id = DeviceId::new(DeviceType::AppleSilicon, 0);
    assert_eq!(format!("{}", id), "Apple:0");
    assert_eq!(id.device_type, DeviceType::AppleSilicon);
}

#[test]
fn h010_throttle_reason_all_variants() {
    // Cover all ThrottleReason Display variants
    assert_eq!(format!("{}", ThrottleReason::None), "None");
    assert_eq!(format!("{}", ThrottleReason::Thermal), "Thermal");
    assert_eq!(format!("{}", ThrottleReason::Power), "Power");
    assert_eq!(format!("{}", ThrottleReason::ApplicationClocks), "AppClocks");
    assert_eq!(format!("{}", ThrottleReason::SwPowerCap), "SwPowerCap");
    assert_eq!(format!("{}", ThrottleReason::HwSlowdown), "HwSlowdown");
    assert_eq!(format!("{}", ThrottleReason::SyncBoost), "SyncBoost");
}

// =========================================================================
// H011: Default Trait Impl Edge Cases
// =========================================================================

/// Mock device to test default trait implementations with controlled values
struct MockDevice {
    mem_used: u64,
    mem_total: u64,
    power_current: f64,
    power_limit: f64,
    temperature: f64,
}

impl MockDevice {
    fn new(
        mem_used: u64,
        mem_total: u64,
        power_current: f64,
        power_limit: f64,
        temperature: f64,
    ) -> Self {
        Self { mem_used, mem_total, power_current, power_limit, temperature }
    }
}

impl ComputeDevice for MockDevice {
    fn device_id(&self) -> DeviceId {
        DeviceId::cpu()
    }
    fn device_name(&self) -> &str {
        "Mock"
    }
    fn device_type(&self) -> DeviceType {
        DeviceType::Cpu
    }
    fn compute_utilization(&self) -> Result<f64, GpuError> {
        Ok(50.0)
    }
    fn compute_clock_mhz(&self) -> Result<u32, GpuError> {
        Ok(3000)
    }
    fn compute_temperature_c(&self) -> Result<f64, GpuError> {
        Ok(self.temperature)
    }
    fn compute_power_watts(&self) -> Result<f64, GpuError> {
        Ok(self.power_current)
    }
    fn compute_power_limit_watts(&self) -> Result<f64, GpuError> {
        Ok(self.power_limit)
    }
    fn memory_used_bytes(&self) -> Result<u64, GpuError> {
        Ok(self.mem_used)
    }
    fn memory_total_bytes(&self) -> Result<u64, GpuError> {
        Ok(self.mem_total)
    }
    fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError> {
        Err(GpuError::NotSupported("mock".into()))
    }
    fn compute_unit_count(&self) -> u32 {
        8
    }
    fn active_compute_units(&self) -> Result<u32, GpuError> {
        Ok(8)
    }
    fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError> {
        Err(GpuError::NotSupported("mock".into()))
    }
    fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError> {
        Err(GpuError::NotSupported("mock".into()))
    }
    fn pcie_generation(&self) -> u8 {
        0
    }
    fn pcie_width(&self) -> u8 {
        0
    }
    fn refresh(&mut self) -> Result<(), GpuError> {
        Ok(())
    }
}

#[test]
fn h011_memory_usage_percent_zero_total() {
    let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
    // Zero total should return 0.0, not divide by zero
    assert!((mock.memory_usage_percent().unwrap() - 0.0).abs() < 0.01);
}

#[test]
fn h011_memory_usage_percent_normal() {
    let mock = MockDevice::new(50 * 1024 * 1024 * 1024, 100 * 1024 * 1024 * 1024, 0.0, 0.0, 0.0);
    // 50% usage
    assert!((mock.memory_usage_percent().unwrap() - 50.0).abs() < 0.01);
}

#[test]
fn h011_memory_available_bytes() {
    let mock = MockDevice::new(30 * 1024 * 1024 * 1024, 100 * 1024 * 1024 * 1024, 0.0, 0.0, 0.0);
    // 70GB available
    let available = mock.memory_available_bytes().unwrap();
    assert_eq!(available, 70 * 1024 * 1024 * 1024);
}

#[test]
fn h011_memory_mb_gb_conversions() {
    let mock = MockDevice::new(1024 * 1024 * 1024, 16 * 1024 * 1024 * 1024, 0.0, 0.0, 0.0);
    // 1GB used = 1024MB
    assert_eq!(mock.memory_used_mb().unwrap(), 1024);
    // 16GB total = 16384MB
    assert_eq!(mock.memory_total_mb().unwrap(), 16384);
    // 16GB as f64
    assert!((mock.memory_total_gb().unwrap() - 16.0).abs() < 0.01);
}

#[test]
fn h011_power_usage_percent_zero_limit() {
    let mock = MockDevice::new(0, 0, 100.0, 0.0, 0.0);
    // Zero limit should return 0.0, not divide by zero
    assert!((mock.power_usage_percent().unwrap() - 0.0).abs() < 0.01);
}

#[test]
fn h011_power_usage_percent_normal() {
    let mock = MockDevice::new(0, 0, 150.0, 300.0, 0.0);
    // 50% power usage
    assert!((mock.power_usage_percent().unwrap() - 50.0).abs() < 0.01);
}

#[test]
fn h011_thermal_throttling_below_threshold() {
    let mock = MockDevice::new(0, 0, 0.0, 0.0, 75.0);
    // Below 80C - no throttling
    assert!(!mock.is_thermal_throttling().unwrap());
}

#[test]
fn h011_thermal_throttling_above_threshold() {
    let mock = MockDevice::new(0, 0, 0.0, 0.0, 85.0);
    // Above 80C - throttling
    assert!(mock.is_thermal_throttling().unwrap());
}

#[test]
fn h011_power_throttling_below_threshold() {
    let mock = MockDevice::new(0, 0, 90.0, 100.0, 0.0);
    // 90% - below 95% threshold
    assert!(!mock.is_power_throttling().unwrap());
}

#[test]
fn h011_power_throttling_above_threshold() {
    let mock = MockDevice::new(0, 0, 98.0, 100.0, 0.0);
    // 98% - above 95% threshold
    assert!(mock.is_power_throttling().unwrap());
}

// =========================================================================
// H012: DeviceSnapshot Edge Cases
// =========================================================================

#[test]
fn h012_device_snapshot_from_mock() {
    let mock = MockDevice::new(8 * 1024 * 1024 * 1024, 16 * 1024 * 1024 * 1024, 150.0, 300.0, 65.0);
    let snapshot = DeviceSnapshot::capture(&mock).unwrap();

    assert_eq!(snapshot.device_id, DeviceId::cpu());
    assert!((snapshot.compute_utilization - 50.0).abs() < 0.01);
    assert_eq!(snapshot.memory_used_bytes, 8 * 1024 * 1024 * 1024);
    assert_eq!(snapshot.memory_total_bytes, 16 * 1024 * 1024 * 1024);
    assert!((snapshot.temperature_c - 65.0).abs() < 0.01);
    assert!((snapshot.power_watts - 150.0).abs() < 0.01);
    assert_eq!(snapshot.clock_mhz, 3000);
}