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
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
#![allow(dead_code)] // Many helper functions are for future implementation

use crate::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// FPGA vendor information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FPGAVendor {
    Intel,
    Xilinx,
    Microsemi,
    Lattice,
    Altera, // Legacy, now part of Intel
    Unknown(String),
}

impl std::fmt::Display for FPGAVendor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FPGAVendor::Intel => write!(f, "Intel"),
            FPGAVendor::Xilinx => write!(f, "Xilinx"),
            FPGAVendor::Microsemi => write!(f, "Microsemi"),
            FPGAVendor::Lattice => write!(f, "Lattice"),
            FPGAVendor::Altera => write!(f, "Altera"),
            FPGAVendor::Unknown(name) => write!(f, "{name}"),
        }
    }
}

/// FPGA family/series information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FPGAFamily {
    // Intel FPGA families
    IntelArria10,
    IntelStratix10,
    IntelCyclone5,
    IntelAgilex,
    
    // Xilinx FPGA families
    XilinxKintex7,
    XilinxVirtex7,
    XilinxZynq7000,
    XilinxZynqUltraScale,
    XilinxKintexUltraScale,
    XilinxVirtexUltraScale,
    XilinxVersal,
    
    // Other vendors
    MicrosemiPolarFire,
    LatticeECP5,
    
    Unknown(String),
}

impl std::fmt::Display for FPGAFamily {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FPGAFamily::IntelArria10 => write!(f, "Intel Arria 10"),
            FPGAFamily::IntelStratix10 => write!(f, "Intel Stratix 10"),
            FPGAFamily::IntelCyclone5 => write!(f, "Intel Cyclone V"),
            FPGAFamily::IntelAgilex => write!(f, "Intel Agilex"),
            FPGAFamily::XilinxKintex7 => write!(f, "Xilinx Kintex-7"),
            FPGAFamily::XilinxVirtex7 => write!(f, "Xilinx Virtex-7"),
            FPGAFamily::XilinxZynq7000 => write!(f, "Xilinx Zynq-7000"),
            FPGAFamily::XilinxZynqUltraScale => write!(f, "Xilinx Zynq UltraScale+"),
            FPGAFamily::XilinxKintexUltraScale => write!(f, "Xilinx Kintex UltraScale+"),
            FPGAFamily::XilinxVirtexUltraScale => write!(f, "Xilinx Virtex UltraScale+"),
            FPGAFamily::XilinxVersal => write!(f, "Xilinx Versal"),
            FPGAFamily::MicrosemiPolarFire => write!(f, "Microsemi PolarFire"),
            FPGAFamily::LatticeECP5 => write!(f, "Lattice ECP5"),
            FPGAFamily::Unknown(name) => write!(f, "{name}"),
        }
    }
}

/// FPGA interface type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FPGAInterface {
    PCIe,
    USB,
    Ethernet,
    SPI,
    I2C,
    JTAG,
    Embedded,
    Unknown(String),
}

/// FPGA accelerator information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FPGAInfo {
    /// FPGA vendor
    pub vendor: FPGAVendor,
    
    /// FPGA family/series
    pub family: FPGAFamily,
    
    /// Device model/part number
    pub model: String,
    
    /// Device ID (PCI, USB, etc.)
    pub device_id: Option<String>,
    
    /// Vendor ID
    pub vendor_id: Option<String>,
    
    /// Interface type
    pub interface: FPGAInterface,
    
    /// Logic elements/cells count
    pub logic_elements: Option<u64>,
    
    /// Block RAM (BRAM) size in bits
    pub block_ram_bits: Option<u64>,
    
    /// DSP slices/blocks count
    pub dsp_blocks: Option<u32>,
    
    /// Maximum operating frequency (MHz)
    pub max_frequency_mhz: Option<u32>,
    
    /// Power consumption (watts)
    pub power_consumption: Option<f32>,
    
    /// AI/ML acceleration capabilities
    pub ml_capabilities: HashMap<String, String>,
    
    /// Supported development tools
    pub development_tools: Vec<String>,
    
    /// Current configuration/bitstream
    pub current_config: Option<String>,
    
    /// Driver version
    pub driver_version: Option<String>,
    
    /// Temperature sensors (if available)
    pub temperature: Option<f32>,
}

impl FPGAInfo {
    /// Detect FPGA accelerators in the system
    pub fn detect_fpgas() -> Result<Vec<FPGAInfo>> {
        let mut fpgas = Vec::new();
        
        // Detect PCIe-based FPGAs
        fpgas.extend(Self::detect_pcie_fpgas()?);
        
        // Detect USB-based FPGAs
        fpgas.extend(Self::detect_usb_fpgas()?);
        
        // Detect embedded FPGAs (for ARM systems)
        fpgas.extend(Self::detect_embedded_fpgas()?);
        
        Ok(fpgas)
    }
    
    fn detect_pcie_fpgas() -> Result<Vec<FPGAInfo>> {
        let mut fpgas = Vec::new();
        
        #[cfg(target_os = "linux")]
        {
            // Read from /sys/bus/pci/devices
            if let Ok(entries) = fs::read_dir("/sys/bus/pci/devices") {
                for entry in entries.flatten() {
                    if let Some(fpga) = Self::check_pci_device_for_fpga(&entry.path())? {
                        fpgas.push(fpga);
                    }
                }
            }
        }
        
        #[cfg(target_os = "windows")]
        {
            // Use WMI to detect PCIe devices
            fpgas.extend(Self::detect_fpgas_windows_wmi()?);
        }
        
        #[cfg(target_os = "macos")]
        {
            // Use system_profiler to detect PCIe devices
            fpgas.extend(Self::detect_fpgas_macos()?);
        }
        
        Ok(fpgas)
    }
    
    #[cfg(target_os = "linux")]
    fn check_pci_device_for_fpga(device_path: &Path) -> Result<Option<FPGAInfo>> {
        let vendor_id = Self::read_hex_file(&device_path.join("vendor"))?;
        let device_id = Self::read_hex_file(&device_path.join("device"))?;
        
        if let (Some(vendor), Some(device)) = (vendor_id, device_id) {
            // Check known FPGA vendor/device IDs
            if let Some(fpga_info) = Self::identify_fpga_by_ids(vendor, device) {
                return Ok(Some(fpga_info));
            }
            
            // Check device class for FPGA-like devices
            if let Some(class) = Self::read_hex_file(&device_path.join("class"))? {
                // Class 0x120000 is often used for FPGA devices
                if class == 0x120000 || class == 0x058000 {
                    return Ok(Some(Self::create_generic_fpga_info(vendor, device)));
                }
            }
        }
        
        Ok(None)
    }
    
    fn read_hex_file(path: &Path) -> Result<Option<u32>> {
        if let Ok(content) = fs::read_to_string(path) {
            if let Ok(value) = u32::from_str_radix(content.trim().trim_start_matches("0x"), 16) {
                return Ok(Some(value));
            }
        }
        Ok(None)
    }
    
    fn identify_fpga_by_ids(vendor_id: u32, device_id: u32) -> Option<FPGAInfo> {
        match vendor_id {
            0x1172 => { // Altera/Intel FPGA vendor ID
                Some(Self::create_intel_fpga_info(device_id))
            },
            0x10EE => { // Xilinx vendor ID
                Some(Self::create_xilinx_fpga_info(device_id))
            },
            0x11F8 => { // PMC-Sierra/Microsemi
                Some(Self::create_microsemi_fpga_info(device_id))
            },
            0x1204 => { // Lattice Semiconductor
                Some(Self::create_lattice_fpga_info(device_id))
            },
            _ => None,
        }
    }
    
    fn create_intel_fpga_info(device_id: u32) -> FPGAInfo {
        let (family, model, specs) = match device_id {
            0x09C4 => (FPGAFamily::IntelArria10, "Arria 10 GX", (1150000, 53248000, 1518, 800)),
            0x09C5 => (FPGAFamily::IntelArria10, "Arria 10 GT", (1150000, 65536000, 1518, 800)),
            0x1D1C => (FPGAFamily::IntelStratix10, "Stratix 10 GX", (2753000, 229376000, 5760, 1000)),
            0x1D1D => (FPGAFamily::IntelStratix10, "Stratix 10 TX", (2753000, 229376000, 5760, 1000)),
            0x4350 => (FPGAFamily::IntelAgilex, "Agilex F-Series", (2700000, 270000000, 5760, 1100)),
            _ => {
                let family = FPGAFamily::Unknown(format!("Intel Device 0x{device_id:04X}"));
                let model = format!("Intel FPGA Device 0x{device_id:04X}");
                return FPGAInfo {
                    vendor: FPGAVendor::Intel,
                    family,
                    model,
                    device_id: Some(format!("0x{device_id:04X}")),
                    vendor_id: Some("0x1172".to_string()),
                    interface: FPGAInterface::PCIe,
                    logic_elements: None,
                    block_ram_bits: None,
                    dsp_blocks: None,
                    max_frequency_mhz: None,
                    power_consumption: None,
                    ml_capabilities: HashMap::from([
                        ("openCL_support".to_string(), "true".to_string()),
                        ("oneAPI_support".to_string(), "true".to_string()),
                        ("dsp_optimization".to_string(), "true".to_string()),
                    ]),
                    development_tools: vec![
                        "Intel Quartus Prime".to_string(),
                        "Intel OpenCL SDK".to_string(),
                        "Intel oneAPI".to_string(),
                    ],
                    current_config: None,
                    driver_version: None,
                    temperature: None,
                };
            }
        };
        
        FPGAInfo {
            vendor: FPGAVendor::Intel,
            family,
            model: model.to_string(),
            device_id: Some(format!("0x{device_id:04X}")),
            vendor_id: Some("0x1172".to_string()),
            interface: FPGAInterface::PCIe,
            logic_elements: if specs.0 > 0 { Some(specs.0) } else { None },
            block_ram_bits: if specs.1 > 0 { Some(specs.1) } else { None },
            dsp_blocks: if specs.2 > 0 { Some(specs.2) } else { None },
            max_frequency_mhz: if specs.3 > 0 { Some(specs.3) } else { None },
            power_consumption: None,
            ml_capabilities: HashMap::from([
                ("openCL_support".to_string(), "true".to_string()),
                ("oneAPI_support".to_string(), "true".to_string()),
                ("dsp_optimization".to_string(), "true".to_string()),
            ]),
            development_tools: vec![
                "Intel Quartus Prime".to_string(),
                "Intel OpenCL SDK".to_string(),
                "Intel oneAPI".to_string(),
            ],
            current_config: None,
            driver_version: None,
            temperature: None,
        }
    }
    
    fn create_xilinx_fpga_info(device_id: u32) -> FPGAInfo {
        let (family, model, specs) = match device_id {
            0x7028 => (FPGAFamily::XilinxKintex7, "Kintex-7 K325T", (326080, 16020000, 840, 464)),
            0x7034 => (FPGAFamily::XilinxVirtex7, "Virtex-7 V485T", (485760, 37080000, 2800, 600)),
            0x7020 => (FPGAFamily::XilinxZynq7000, "Zynq-7000 Z020", (85000, 4900000, 220, 766)),
            0x9038 => (FPGAFamily::XilinxZynqUltraScale, "Zynq UltraScale+ ZU19EG", (1143000, 75900000, 1968, 850)),
            0x906C => (FPGAFamily::XilinxKintexUltraScale, "Kintex UltraScale+ KU15P", (1451000, 75900000, 1968, 925)),
            0x9058 => (FPGAFamily::XilinxVirtexUltraScale, "Virtex UltraScale+ VU19P", (8938000, 270000000, 12288, 750)),
            0x5008 => (FPGAFamily::XilinxVersal, "Versal Prime VP1202", (899000, 57600000, 1968, 1300)),
            _ => {
                let family = FPGAFamily::Unknown(format!("Xilinx Device 0x{device_id:04X}"));
                let model = format!("Xilinx FPGA Device 0x{device_id:04X}");
                return FPGAInfo {
                    vendor: FPGAVendor::Xilinx,
                    family,
                    model,
                    device_id: Some(format!("0x{device_id:04X}")),
                    vendor_id: Some("0x10EE".to_string()),
                    interface: FPGAInterface::PCIe,
                    logic_elements: None,
                    block_ram_bits: None,
                    dsp_blocks: None,
                    max_frequency_mhz: None,
                    power_consumption: None,
                    ml_capabilities: HashMap::from([
                        ("vitis_ai_support".to_string(), "true".to_string()),
                        ("dpu_acceleration".to_string(), "true".to_string()),
                        ("hls_support".to_string(), "true".to_string()),
                    ]),
                    development_tools: vec![
                        "Xilinx Vivado".to_string(),
                        "Xilinx Vitis".to_string(),
                        "Xilinx Vitis AI".to_string(),
                    ],
                    current_config: None,
                    driver_version: None,
                    temperature: None,
                };
            }
        };
        
        FPGAInfo {
            vendor: FPGAVendor::Xilinx,
            family,
            model: model.to_string(),
            device_id: Some(format!("0x{device_id:04X}")),
            vendor_id: Some("0x10EE".to_string()),
            interface: FPGAInterface::PCIe,
            logic_elements: if specs.0 > 0 { Some(specs.0) } else { None },
            block_ram_bits: if specs.1 > 0 { Some(specs.1) } else { None },
            dsp_blocks: if specs.2 > 0 { Some(specs.2) } else { None },
            max_frequency_mhz: if specs.3 > 0 { Some(specs.3) } else { None },
            power_consumption: None,
            ml_capabilities: HashMap::from([
                ("vitis_ai_support".to_string(), "true".to_string()),
                ("dpu_acceleration".to_string(), "true".to_string()),
                ("hls_support".to_string(), "true".to_string()),
            ]),
            development_tools: vec![
                "Xilinx Vivado".to_string(),
                "Xilinx Vitis".to_string(),
                "Xilinx Vitis AI".to_string(),
            ],
            current_config: None,
            driver_version: None,
            temperature: None,
        }
    }
    
    fn create_microsemi_fpga_info(device_id: u32) -> FPGAInfo {
        FPGAInfo {
            vendor: FPGAVendor::Microsemi,
            family: FPGAFamily::MicrosemiPolarFire,
            model: format!("Microsemi Device 0x{device_id:04X}"),
            device_id: Some(format!("0x{device_id:04X}")),
            vendor_id: Some("0x11F8".to_string()),
            interface: FPGAInterface::PCIe,
            logic_elements: None,
            block_ram_bits: None,
            dsp_blocks: None,
            max_frequency_mhz: None,
            power_consumption: None,
            ml_capabilities: HashMap::from([
                ("low_power_inference".to_string(), "true".to_string()),
            ]),
            development_tools: vec!["Libero SoC".to_string()],
            current_config: None,
            driver_version: None,
            temperature: None,
        }
    }
    
    fn create_lattice_fpga_info(device_id: u32) -> FPGAInfo {
        FPGAInfo {
            vendor: FPGAVendor::Lattice,
            family: FPGAFamily::LatticeECP5,
            model: format!("Lattice Device 0x{device_id:04X}"),
            device_id: Some(format!("0x{device_id:04X}")),
            vendor_id: Some("0x1204".to_string()),
            interface: FPGAInterface::PCIe,
            logic_elements: None,
            block_ram_bits: None,
            dsp_blocks: None,
            max_frequency_mhz: None,
            power_consumption: None,
            ml_capabilities: HashMap::from([
                ("edge_ai_inference".to_string(), "true".to_string()),
                ("low_power".to_string(), "true".to_string()),
            ]),
            development_tools: vec!["Lattice Diamond".to_string(), "Lattice Radiant".to_string()],
            current_config: None,
            driver_version: None,
            temperature: None,
        }
    }
    
    fn create_generic_fpga_info(vendor_id: u32, device_id: u32) -> FPGAInfo {
        FPGAInfo {
            vendor: FPGAVendor::Unknown(format!("0x{vendor_id:04X}")),
            family: FPGAFamily::Unknown("Unknown".to_string()),
            model: format!("FPGA Device 0x{vendor_id:04X}:0x{device_id:04X}"),
            device_id: Some(format!("0x{device_id:04X}")),
            vendor_id: Some(format!("0x{vendor_id:04X}")),
            interface: FPGAInterface::PCIe,
            logic_elements: None,
            block_ram_bits: None,
            dsp_blocks: None,
            max_frequency_mhz: None,
            power_consumption: None,
            ml_capabilities: HashMap::new(),
            development_tools: Vec::new(),
            current_config: None,
            driver_version: None,
            temperature: None,
        }
    }
    
    fn detect_usb_fpgas() -> Result<Vec<FPGAInfo>> {
        // Placeholder for USB FPGA detection (like some development boards)
        Ok(Vec::new())
    }
    
    fn detect_embedded_fpgas() -> Result<Vec<FPGAInfo>> {
        // Placeholder for embedded FPGA detection (like Zynq SoCs)
        Ok(Vec::new())
    }
    
    #[cfg(target_os = "windows")]
    fn detect_fpgas_windows_wmi() -> Result<Vec<FPGAInfo>> {
        // Placeholder for Windows WMI detection
        Ok(Vec::new())
    }
    
    #[cfg(target_os = "macos")]
    fn detect_fpgas_macos() -> Result<Vec<FPGAInfo>> {
        // Placeholder for macOS detection
        Ok(Vec::new())
    }
    
    /// Calculate theoretical AI performance metrics for the FPGA
    pub fn calculate_ai_performance(&self) -> HashMap<String, f64> {
        let mut metrics = HashMap::new();
        
        if let (Some(dsp_blocks), Some(freq_mhz)) = (self.dsp_blocks, self.max_frequency_mhz) {
            // Theoretical operations per second
            let ops_per_second = (dsp_blocks as f64) * (freq_mhz as f64) * 1_000_000.0;
            metrics.insert("theoretical_ops_per_second".to_string(), ops_per_second);
            
            // Estimate for different precisions
            metrics.insert("int8_ops_per_second".to_string(), ops_per_second * 4.0);
            metrics.insert("int16_ops_per_second".to_string(), ops_per_second * 2.0);
            metrics.insert("fp32_ops_per_second".to_string(), ops_per_second);
        }
        
        if let Some(logic_elements) = self.logic_elements {
            // Logic utilization efficiency for AI workloads
            let efficiency_score = (logic_elements as f64) / 1_000_000.0; // Normalize to millions
            metrics.insert("logic_efficiency_score".to_string(), efficiency_score);
        }
        
        metrics
    }
    
    /// Get AI framework compatibility
    pub fn get_ai_framework_support(&self) -> Vec<String> {
        let mut frameworks = Vec::new();
        
        match self.vendor {
            FPGAVendor::Intel => {
                frameworks.extend(vec![
                    "Intel OpenVINO".to_string(),
                    "Intel oneAPI".to_string(),
                    "OpenCL".to_string(),
                ]);
            },
            FPGAVendor::Xilinx => {
                frameworks.extend(vec![
                    "Xilinx Vitis AI".to_string(),
                    "TensorFlow".to_string(),
                    "PyTorch".to_string(),
                    "ONNX".to_string(),
                ]);
            },
            _ => {
                frameworks.push("Custom FPGA frameworks".to_string());
            }
        }
        
        frameworks
    }
}