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
/// Enhanced platform-specific hardware detection for macOS
use crate::{HardwareQueryError, Result};
use std::collections::HashMap;
use std::process::Command;

/// macOS-specific CPU information
#[derive(Debug, Clone)]
pub struct MacOSCPUInfo {
    pub brand_string: String,
    pub vendor: String,
    pub cpu_type: String,
    pub cpu_subtype: String,
    pub physical_cpu: u32,
    pub logical_cpu: u32,
    pub cpu_freq: u64,
    pub cpu_freq_max: u64,
    pub cpu_freq_min: u64,
    pub l1_icache_size: u64,
    pub l1_dcache_size: u64,
    pub l2_cache_size: u64,
    pub l3_cache_size: u64,
    pub cache_line_size: u64,
    pub features: Vec<String>,
}

impl MacOSCPUInfo {
    /// Query detailed CPU information from macOS sysctl
    pub fn query() -> Result<Self> {
        let mut cpu_info = MacOSCPUInfo {
            brand_string: String::new(),
            vendor: String::new(),
            cpu_type: String::new(),
            cpu_subtype: String::new(),
            physical_cpu: 0,
            logical_cpu: 0,
            cpu_freq: 0,
            cpu_freq_max: 0,
            cpu_freq_min: 0,
            l1_icache_size: 0,
            l1_dcache_size: 0,
            l2_cache_size: 0,
            l3_cache_size: 0,
            cache_line_size: 0,
            features: Vec::new(),
        };

        // Get CPU information using sysctl
        let sysctl_queries = [
            ("machdep.cpu.brand_string", "brand_string"),
            ("machdep.cpu.vendor", "vendor"),
            ("hw.cputype", "cpu_type"),
            ("hw.cpusubtype", "cpu_subtype"),
            ("hw.physicalcpu", "physical_cpu"),
            ("hw.logicalcpu", "logical_cpu"),
            ("hw.cpufrequency", "cpu_freq"),
            ("hw.cpufrequency_max", "cpu_freq_max"),
            ("hw.cpufrequency_min", "cpu_freq_min"),
            ("hw.l1icachesize", "l1_icache_size"),
            ("hw.l1dcachesize", "l1_dcache_size"),
            ("hw.l2cachesize", "l2_cache_size"),
            ("hw.l3cachesize", "l3_cache_size"),
            ("hw.cachelinesize", "cache_line_size"),
        ];

        for (sysctl_key, field_name) in &sysctl_queries {
            if let Ok(output) = Command::new("sysctl").args(["-n", sysctl_key]).output() {
                let value = String::from_utf8_lossy(&output.stdout).trim().to_string();

                match *field_name {
                    "brand_string" => cpu_info.brand_string = value,
                    "vendor" => cpu_info.vendor = value,
                    "cpu_type" => cpu_info.cpu_type = value,
                    "cpu_subtype" => cpu_info.cpu_subtype = value,
                    "physical_cpu" => cpu_info.physical_cpu = value.parse().unwrap_or(0),
                    "logical_cpu" => cpu_info.logical_cpu = value.parse().unwrap_or(0),
                    "cpu_freq" => cpu_info.cpu_freq = value.parse().unwrap_or(0),
                    "cpu_freq_max" => cpu_info.cpu_freq_max = value.parse().unwrap_or(0),
                    "cpu_freq_min" => cpu_info.cpu_freq_min = value.parse().unwrap_or(0),
                    "l1_icache_size" => cpu_info.l1_icache_size = value.parse().unwrap_or(0),
                    "l1_dcache_size" => cpu_info.l1_dcache_size = value.parse().unwrap_or(0),
                    "l2_cache_size" => cpu_info.l2_cache_size = value.parse().unwrap_or(0),
                    "l3_cache_size" => cpu_info.l3_cache_size = value.parse().unwrap_or(0),
                    "cache_line_size" => cpu_info.cache_line_size = value.parse().unwrap_or(0),
                    _ => {}
                }
            }
        }

        // Get CPU features
        cpu_info.features = Self::get_cpu_features()?;

        Ok(cpu_info)
    }

    /// Get CPU features from macOS sysctl
    fn get_cpu_features() -> Result<Vec<String>> {
        let mut features = Vec::new();

        let feature_queries = [
            "machdep.cpu.features",
            "machdep.cpu.leaf7_features",
            "machdep.cpu.extfeatures",
        ];

        for query in &feature_queries {
            if let Ok(output) = Command::new("sysctl").args(["-n", query]).output() {
                let feature_str = String::from_utf8_lossy(&output.stdout);
                let query_features: Vec<String> = feature_str
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                features.extend(query_features);
            }
        }

        Ok(features)
    }

    /// Get CPU temperature using powermetrics (requires sudo)
    pub fn get_temperature() -> Result<Option<f32>> {
        // Try to get temperature using powermetrics
        if let Ok(output) = Command::new("powermetrics")
            .args([
                "--samplers",
                "smc",
                "-n",
                "1",
                "--hide-cpu-duty-cycle",
                "--show-process-coalition",
            ])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            for line in output_str.lines() {
                if line.contains("CPU die temperature") {
                    if let Some(temp_str) = line.split(':').nth(1) {
                        let temp_str = temp_str.trim().replace("C", "");
                        if let Ok(temp) = temp_str.parse::<f32>() {
                            return Ok(Some(temp));
                        }
                    }
                }
            }
        }

        // Alternative: try using iStat or other tools
        Ok(None)
    }

    /// Get detailed CPU architecture information
    pub fn get_architecture_info() -> Result<HashMap<String, String>> {
        let mut arch_info = HashMap::new();

        let arch_queries = [
            ("hw.targettype", "target_type"),
            ("hw.machine", "machine"),
            ("hw.model", "model"),
            ("machdep.cpu.family", "cpu_family"),
            ("machdep.cpu.model", "cpu_model"),
            ("machdep.cpu.stepping", "stepping"),
            ("machdep.cpu.microcode_version", "microcode_version"),
        ];

        for (sysctl_key, info_key) in &arch_queries {
            if let Ok(output) = Command::new("sysctl").args(["-n", sysctl_key]).output() {
                let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
                arch_info.insert(info_key.to_string(), value);
            }
        }

        Ok(arch_info)
    }
}

/// macOS-specific GPU information
#[derive(Debug, Clone)]
pub struct MacOSGPUInfo {
    pub device_name: String,
    pub vendor_name: String,
    pub device_id: String,
    pub vendor_id: String,
    pub pci_class: String,
    pub metal_support: bool,
    pub memory_mb: u64,
}

impl MacOSGPUInfo {
    /// Query GPU information from macOS system_profiler
    pub fn query_all() -> Result<Vec<Self>> {
        let mut gpus = Vec::new();

        if let Ok(output) = Command::new("system_profiler")
            .args(["SPDisplaysDataType", "-xml"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            // Parse system_profiler XML output
            // This is a simplified implementation - a full implementation would parse XML

            if output_str.contains("Graphics/Displays:") {
                // Extract GPU information from system_profiler output
                // This would require proper XML parsing
            }
        }

        // Alternative: use ioreg command
        if let Ok(output) = Command::new("ioreg")
            .args(["-r", "-d", "1", "-w", "0", "-c", "IOPCIDevice"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            // Parse ioreg output for GPU devices
            // This is a simplified implementation
        }

        Ok(gpus)
    }

    /// Check Metal support and capabilities
    pub fn get_metal_info() -> Result<HashMap<String, String>> {
        let mut metal_info = HashMap::new();

        // Use Metal command-line tools if available
        if let Ok(output) = Command::new("xcrun").args(["metal", "-version"]).output() {
            let version_str = String::from_utf8_lossy(&output.stdout);
            metal_info.insert("metal_version".to_string(), version_str.trim().to_string());
        }

        Ok(metal_info)
    }
}

/// macOS-specific memory information
#[derive(Debug, Clone)]
pub struct MacOSMemoryInfo {
    pub physical_memory: u64,
    pub user_memory: u64,
    pub wired_memory: u64,
    pub compressed_memory: u64,
    pub memory_pressure: String,
    pub swap_usage: u64,
}

impl MacOSMemoryInfo {
    /// Query memory information from macOS system tools
    pub fn query() -> Result<Self> {
        let mut mem_info = MacOSMemoryInfo {
            physical_memory: 0,
            user_memory: 0,
            wired_memory: 0,
            compressed_memory: 0,
            memory_pressure: String::new(),
            swap_usage: 0,
        };

        // Get physical memory
        if let Ok(output) = Command::new("sysctl").args(["-n", "hw.memsize"]).output() {
            let mem_str = String::from_utf8_lossy(&output.stdout);
            mem_info.physical_memory = mem_str.trim().parse().unwrap_or(0);
        }

        // Get memory usage details
        if let Ok(output) = Command::new("vm_stat").output() {
            let vm_stat_str = String::from_utf8_lossy(&output.stdout);

            for line in vm_stat_str.lines() {
                if let Some((key, value)) = line.split_once(':') {
                    let key = key.trim();
                    let value = value
                        .trim()
                        .replace(".", "")
                        .replace("pages", "")
                        .trim()
                        .to_string();

                    if let Ok(pages) = value.parse::<u64>() {
                        // Assuming 4KB page size
                        let bytes = pages * 4096;

                        match key {
                            "Pages free" => {
                                // Calculate available memory
                            }
                            "Pages wired down" => mem_info.wired_memory = bytes,
                            "Pages active" => mem_info.user_memory += bytes,
                            "Pages inactive" => mem_info.user_memory += bytes,
                            "Pages occupied by compressor" => mem_info.compressed_memory = bytes,
                            _ => {}
                        }
                    }
                }
            }
        }

        // Get memory pressure
        if let Ok(output) = Command::new("memory_pressure").output() {
            let pressure_str = String::from_utf8_lossy(&output.stdout);
            mem_info.memory_pressure = pressure_str.trim().to_string();
        }

        // Get swap usage
        if let Ok(output) = Command::new("sysctl").args(["-n", "vm.swapusage"]).output() {
            let swap_str = String::from_utf8_lossy(&output.stdout);
            // Parse swap usage string
            if let Some(used_start) = swap_str.find("used = ") {
                if let Some(used_end) = swap_str[used_start + 7..].find("M") {
                    let used_str = &swap_str[used_start + 7..used_start + 7 + used_end];
                    if let Ok(used_mb) = used_str.parse::<u64>() {
                        mem_info.swap_usage = used_mb * 1024 * 1024; // Convert to bytes
                    }
                }
            }
        }

        Ok(mem_info)
    }

    /// Get detailed memory module information using system_profiler
    pub fn get_memory_modules() -> Result<Vec<MacOSMemoryModule>> {
        let mut modules = Vec::new();

        if let Ok(output) = Command::new("system_profiler")
            .args(["SPMemoryDataType", "-xml"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            // Parse system_profiler XML output for memory modules
            // This would require proper XML parsing
        }

        Ok(modules)
    }
}

#[derive(Debug, Clone)]
pub struct MacOSMemoryModule {
    pub size_gb: u64,
    pub speed_mhz: u32,
    pub memory_type: String,
    pub manufacturer: String,
    pub part_number: String,
    pub serial_number: String,
    pub slot: String,
}

/// macOS-specific system information
pub struct MacOSSystemInfo;

impl MacOSSystemInfo {
    /// Get system version and build information
    pub fn get_system_version() -> Result<HashMap<String, String>> {
        let mut version_info = HashMap::new();

        if let Ok(output) = Command::new("sw_vers").output() {
            let version_str = String::from_utf8_lossy(&output.stdout);

            for line in version_str.lines() {
                if let Some((key, value)) = line.split_once(':') {
                    let key = key.trim();
                    let value = value.trim();

                    match key {
                        "ProductName" => {
                            version_info.insert("product_name".to_string(), value.to_string())
                        }
                        "ProductVersion" => {
                            version_info.insert("product_version".to_string(), value.to_string())
                        }
                        "BuildVersion" => {
                            version_info.insert("build_version".to_string(), value.to_string())
                        }
                        _ => None,
                    };
                }
            }
        }

        Ok(version_info)
    }

    /// Get hardware model information
    pub fn get_hardware_model() -> Result<String> {
        if let Ok(output) = Command::new("sysctl").args(["-n", "hw.model"]).output() {
            Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
        } else {
            Err(HardwareQueryError::system_info_unavailable(
                "Cannot get hardware model",
            ))
        }
    }

    /// Get system uptime
    pub fn get_uptime() -> Result<u64> {
        if let Ok(output) = Command::new("sysctl")
            .args(["-n", "kern.boottime"])
            .output()
        {
            let boottime_str = String::from_utf8_lossy(&output.stdout);
            // Parse boottime and calculate uptime
            // This would require parsing the boottime format
            Ok(0) // Placeholder
        } else {
            Err(HardwareQueryError::system_info_unavailable(
                "Cannot get uptime",
            ))
        }
    }
}