hardware-query 0.2.1

Cross-platform Rust library for comprehensive hardware detection, real-time monitoring, power management, and AI/ML optimization
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
# Hardware Query


A cross-platform Rust library for querying detailed system hardware information with advanced monitoring and power management capabilities.

## 🚀 Features


### Core Hardware Detection

- ✅ Cross-platform hardware detection (Windows, Linux, macOS)
- ✅ Detailed CPU information (cores, threads, cache, features)
- ✅ GPU detection and capabilities (CUDA, ROCm, DirectML support)
- ✅ Memory configuration and status
- ✅ Storage device enumeration and properties
- ✅ Network interface detection and capabilities
- ✅ Hardware acceleration support detection (NPU, TPU, FPGA)
- ✅ PCI/USB device enumeration
- ✅ ARM-specific hardware detection (Raspberry Pi, Jetson, etc.)

### 🔄 Real-time Monitoring (NEW!)

- ✅ Continuous hardware metrics monitoring
- ✅ Configurable update intervals and thresholds
- ✅ Event-driven notifications for thermal/power alerts
- ✅ Background monitoring with async support
- ✅ Comprehensive monitoring statistics

### ⚡ Power Management & Efficiency

- ✅ Real-time power consumption tracking
- ✅ Battery life estimation and health monitoring
- ✅ Power efficiency scoring and optimization
- ✅ Thermal throttling risk assessment
- ✅ Power optimization recommendations

### 🌡️ Enhanced Thermal Management

- ✅ Advanced temperature monitoring with history
- ✅ Thermal throttling prediction algorithms
- ✅ Cooling optimization recommendations
- ✅ Sustained performance capability analysis
- ✅ Fan curve analysis and optimization

### 🐳 Virtualization & Container Detection

- ✅ Comprehensive virtualization environment detection
- ✅ Container runtime identification (Docker, Kubernetes, etc.)
- ✅ Resource limits and restrictions analysis
- ✅ GPU passthrough capability detection
- ✅ Performance impact assessment
- ✅ Security feature analysis

### 🎯 AI/ML Optimization

- ✅ Hardware suitability assessment for AI workloads
- ✅ Performance scoring for different AI tasks
- ✅ Memory and compute requirement analysis
- ✅ Acceleration framework compatibility
- ✅ Optimization recommendations

## Quick Start


Add this to your `Cargo.toml`:

```toml
[dependencies]
hardware-query = "0.2.0"

# For real-time monitoring features

hardware-query = { version = "0.2.0", features = ["monitoring"] }
```

## 🔧 Feature Flags


### When to Use the `monitoring` Feature


**Enable `monitoring` if you need:**
- **Continuous hardware monitoring** - Track temperatures, power, and performance over time
-**Real-time alerts** - Get notified when hardware exceeds thresholds
-**Background monitoring** - Async monitoring that doesn't block your application
-**Event-driven architecture** - React to hardware changes as they happen
-**Server/system monitoring** - Long-running applications that need to track hardware health

**Skip `monitoring` if you only need:**
- ❌ One-time hardware queries (use `SystemOverview::quick()`)
- ❌ Static hardware information for configuration
- ❌ Simple compatibility checks
- ❌ Lightweight applications where async dependencies are unwanted

```toml
# Lightweight - no async dependencies

hardware-query = "0.2.0"

# Full featured - includes real-time monitoring

hardware-query = { version = "0.2.0", features = ["monitoring"] }
```

## 🚀 Simplified API (Recommended for New Users)


For most use cases, start with the simplified API:

```rust
use hardware_query::SystemOverview;

// Get essential system info in one line - no hardware expertise needed!
let overview = SystemOverview::quick()?;
println!("CPU: {} ({} cores)", overview.cpu.name, overview.cpu.cores);
println!("Memory: {:.1} GB", overview.memory_gb);
println!("Performance Score: {}/100", overview.performance_score);
println!("AI Ready: {}", overview.is_ai_ready());
```

For domain-specific needs:

```rust
use hardware_query::HardwarePresets;

// AI/ML assessment with built-in expertise
let ai_assessment = HardwarePresets::ai_assessment()?;
println!("AI Score: {}/100", ai_assessment.ai_score);

// Gaming performance recommendations
let gaming = HardwarePresets::gaming_assessment()?;
println!("Recommended: {} at {:?}", 
    gaming.recommended_settings.resolution,
    gaming.recommended_settings.quality_preset);
```

## Advanced Usage


```rust
use hardware_query::HardwareInfo;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Get complete system information
    let hw_info = HardwareInfo::query()?;
    
    // Access CPU information
    let cpu = hw_info.cpu();
    println!("CPU: {} {} with {} cores, {} threads",
        cpu.vendor(),
        cpu.model_name(),
        cpu.physical_cores(),
        cpu.logical_cores()
    );
    
    // Check virtualization environment
    let virt = hw_info.virtualization();
    if virt.is_virtualized() {
        println!("Running in: {} (Performance impact: {:.1}%)", 
            virt.environment_type,
            (1.0 - virt.get_performance_factor()) * 100.0
        );
    }
    
    // Power management
    if let Some(power) = hw_info.power_profile() {
        println!("Power State: {}", power.power_state);
        if let Some(power_draw) = power.total_power_draw {
            println!("Current Power Draw: {:.1}W", power_draw);
        }
        
        // Get optimization recommendations
        let optimizations = power.suggest_power_optimizations();
        for opt in optimizations {
            println!("💡 {}", opt.recommendation);
        }
    }
    
    // Thermal analysis
    let thermal = hw_info.thermal();
    if let Some(max_temp) = thermal.max_temperature() {
        println!("Max Temperature: {:.1}°C (Status: {})", 
            max_temp, thermal.thermal_status());
        
        // Predict thermal throttling
        let prediction = thermal.predict_thermal_throttling(1.0);
        if prediction.will_throttle {
            println!("⚠️  Thermal throttling predicted: {}", prediction.severity);
        }
        
        // Get cooling recommendations
        let cooling_recs = thermal.suggest_cooling_optimizations();
        for rec in cooling_recs.iter().take(3) {
            println!("🌡️ {}", rec.description);
        }
    }
    
    Ok(())
}
```

## Advanced Real-time Monitoring


```rust
use hardware_query::{HardwareMonitor, MonitoringConfig};
use std::time::Duration;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Configure monitoring
    let mut config = MonitoringConfig::default();
    config.update_interval = Duration::from_secs(2);
    config.thermal_threshold = 75.0;
    
    let monitor = HardwareMonitor::with_config(config);
    
    // Add event callbacks
    monitor.on_event(|event| {
        match event {
            MonitoringEvent::ThermalAlert { sensor_name, temperature, .. } => {
                println!("🚨 Thermal Alert: {} at {:.1}°C", sensor_name, temperature);
            }
            MonitoringEvent::PowerAlert { current_power, .. } => {
                println!("⚡ Power Alert: {:.1}W", current_power);
            }
            _ => {}
        }
    }).await;
    
    // Start monitoring
    monitor.start_monitoring().await?;
    
    // Monitor for 60 seconds
    tokio::time::sleep(Duration::from_secs(60)).await;
    
    // Stop and get statistics
    monitor.stop_monitoring().await;
    let stats = monitor.get_stats().await;
    println!("Generated {} events ({} thermal alerts)", 
        stats.total_events, stats.thermal_alerts);
    
    Ok(())
}
```
    
    // Get GPU information
    for (i, gpu) in hw_info.gpus().iter().enumerate() {
        println!("GPU {}: {} {} with {} GB VRAM",
            i + 1,
            gpu.vendor(),
            gpu.model_name(),
            gpu.memory_gb()
        );
        
        // Check GPU capabilities
        println!("  CUDA support: {}", gpu.supports_cuda());
        println!("  ROCm support: {}", gpu.supports_rocm());
        println!("  DirectML support: {}", gpu.supports_directml());
    }
    
    // Memory information
    let mem = hw_info.memory();
    println!("System memory: {:.1} GB total, {:.1} GB available",
        mem.total_gb(),
        mem.available_gb()
    );
    
    // Storage information
    for (i, disk) in hw_info.storage_devices().iter().enumerate() {
        println!("Disk {}: {} {:.1} GB ({:?} type)",
            i + 1,
            disk.model,
            disk.capacity_gb,
            disk.storage_type
        );
    }
    
    // Network interfaces
    for interface in hw_info.network_interfaces() {
        println!("Network: {} - {}", 
            interface.name,
            interface.mac_address
        );
    }
    
    // Check specialized hardware
    for npu in hw_info.npus() {
        println!("NPU detected: {} {}", npu.vendor(), npu.model_name());
    }
    
    for tpu in hw_info.tpus() {
        println!("TPU detected: {} {}", tpu.vendor(), tpu.model_name());
    }
    
    // Check ARM-specific hardware (Raspberry Pi, Jetson, etc.)
    if let Some(arm) = hw_info.arm_hardware() {
        println!("ARM System: {}", arm.system_type);
    }
    
    // Check FPGA hardware
    for fpga in hw_info.fpgas() {
        println!("FPGA: {} {} with {} logic elements",
            fpga.vendor,
            fpga.family,
            fpga.logic_elements.unwrap_or(0)
        );
    }
    
    // Serialize hardware information to JSON
    let hw_json = hw_info.to_json()?;
    println!("Hardware JSON: {}", hw_json);
    
    Ok(())
}
```

## Specialized Hardware Support


The library provides comprehensive detection for AI/ML-oriented hardware:

- **NPUs**: Intel Movidius, GNA, XDNA; Apple Neural Engine; Qualcomm Hexagon
- **TPUs**: Google Cloud TPU and Edge TPU; Intel Habana
- **ARM Systems**: Raspberry Pi, NVIDIA Jetson, Apple Silicon with power management
- **FPGAs**: Intel/Altera and Xilinx families with AI optimization scoring

```rust
use hardware_query::HardwareInfo;

let hw_info = HardwareInfo::query()?;

// Check for specialized AI hardware
for npu in hw_info.npus() {
    println!("NPU: {} {}", 
        npu.vendor(), 
        npu.model_name()
    );
}

for tpu in hw_info.tpus() {
    println!("TPU: {} {}", 
        tpu.vendor(),
        tpu.model_name()
    );
}
```

## Platform Support


| Platform | CPU | GPU | Memory | Storage | Network | Battery | Thermal | PCI | USB |
|----------|-----|-----|--------|---------|---------|---------|---------|-----|-----|
| Windows  ||||||||||
| Linux    ||||||||||
| macOS    ||||||||||

## Optional Features


Enable additional GPU support with feature flags:

```toml
[dependencies]
hardware-query = { version = "0.2.0", features = ["nvidia", "amd", "intel"] }
```

- `nvidia`: NVIDIA GPU support via NVML
- `amd`: AMD GPU support via ROCm
- `intel`: Intel GPU support

## Examples


See the [examples](examples/) directory for complete usage examples:

- 🚀 [**Simplified API (Start Here!)**]examples/01_simplified_api.rs - Easy-to-use API for common scenarios
- [Basic Hardware Detection]examples/02_basic_hardware.rs - Fundamental hardware querying
- [Enhanced Hardware]examples/enhanced_hardware.rs - Advanced hardware analysis
- [Comprehensive Hardware]examples/comprehensive_hardware.rs - Complete system analysis
- [Advanced Usage]examples/advanced_usage.rs - Expert-level features
- [Comprehensive AI Hardware]examples/comprehensive_ai_hardware.rs - AI/ML-specific analysis
- [Enhanced Monitoring Demo]examples/enhanced_monitoring_demo.rs - Real-time monitoring (requires `monitoring` feature)

## Building


```bash
# Build with default features

cargo build

# Build with GPU support

cargo build --features="nvidia,amd,intel"

# Run tests

cargo test

# Try the simplified API first (recommended for new users!)

cargo run --example 01_simplified_api

# Or run the basic hardware example

cargo run --example 02_basic_hardware
```

## Dependencies


- `sysinfo` - Cross-platform system information
- `serde` - Serialization framework
- `thiserror` - Error handling
- `num_cpus` - CPU core detection

### Optional Dependencies


- `nvml-wrapper` - NVIDIA GPU support (feature: nvidia)
- `wmi` - Windows Management Instrumentation (Windows only)
- `libc` - Linux system calls (Linux only)
- `core-foundation` - macOS system APIs (macOS only)

## Performance


The library is designed for performance with:

- Lazy evaluation of hardware information
- Minimal system calls
- Efficient data structures
- Optional caching for repeated queries

Typical query times:

- Complete hardware scan: 10-50ms
- CPU information: 1-5ms
- GPU information: 5-20ms
- Memory information: 1-3ms

## Error Handling


The library uses comprehensive error handling:

```rust
use hardware_query::{HardwareInfo, HardwareQueryError};

match HardwareInfo::query() {
    Ok(hw_info) => {
        // Use hardware information
    }
    Err(HardwareQueryError::SystemInfoUnavailable(msg)) => {
        eprintln!("System info unavailable: {}", msg);
    }
    Err(HardwareQueryError::PermissionDenied(msg)) => {
        eprintln!("Permission denied: {}", msg);
    }
    Err(e) => {
        eprintln!("Hardware query error: {}", e);
    }
}
```

## Contributing


1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License


This project is licensed under the MIT OR Apache-2.0 License - see the [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE) files for details.

## Acknowledgments


- Built on top of the excellent [`sysinfo`]https://github.com/GuillaumeGomez/sysinfo crate
- Inspired by the need for better hardware detection in AI workload placement
- Thanks to all contributors who helped improve cross-platform compatibility