hardware_report 0.1.7

Hardware inventory reports for observability and cluster acceptance testing
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
/*
Copyright 2024 San Francisco Compute Company

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! CPU information parsing functions

use super::common::{clean_value, extract_dmidecode_value, parse_key_value};
use crate::domain::{CpuInfo, CpuTopology};
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
    static ref CPU_SPEED_RE: Regex = Regex::new(r"(\d+(?:\.\d+)?)\s*(MHz|GHz)").unwrap();
    static ref CORE_COUNT_RE: Regex = Regex::new(r"(\d+)").unwrap();
}

/// Parse sysfs frequency file (kHz to MHz)
///
/// # Arguments
///
/// * `content` - Content of frequency file (value in kHz)
///
/// # Returns
///
/// Frequency in MHz.
pub fn parse_sysfs_freq_khz(content: &str) -> Result<u32, String> {
    let khz: u64 = content
        .trim()
        .parse()
        .map_err(|e| format!("Failed to parse frequency: {}", e))?;
    Ok((khz / 1000) as u32)
}

/// Parse sysfs cache size (e.g., "32K" -> 32)
///
/// # Arguments
///
/// * `content` - Content of cache size file (e.g., "32K", "256K", "16M")
///
/// # Returns
///
/// Size in KB.
pub fn parse_sysfs_cache_size(content: &str) -> Result<u32, String> {
    let trimmed = content.trim();

    if trimmed.ends_with('K') || trimmed.ends_with('k') {
        let value: u32 = trimmed[..trimmed.len() - 1]
            .parse()
            .map_err(|e| format!("Failed to parse cache size: {}", e))?;
        Ok(value)
    } else if trimmed.ends_with('M') || trimmed.ends_with('m') {
        let value: u32 = trimmed[..trimmed.len() - 1]
            .parse()
            .map_err(|e| format!("Failed to parse cache size: {}", e))?;
        Ok(value * 1024)
    } else {
        // Assume bytes, convert to KB
        let value: u32 = trimmed
            .parse()
            .map_err(|e| format!("Failed to parse cache size: {}", e))?;
        Ok(value / 1024)
    }
}

/// Parse /proc/cpuinfo
///
/// Extracts vendor, flags, and other CPU information.
///
/// # Arguments
///
/// * `content` - Content of /proc/cpuinfo
pub fn parse_proc_cpuinfo(content: &str) -> Result<CpuInfo, String> {
    let mut cpu_info = CpuInfo::default();

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

            match key {
                "vendor_id" => cpu_info.vendor = value.to_string(),
                "model name" if cpu_info.model.is_empty() => {
                    cpu_info.model = value.to_string();
                }
                "flags" | "Features" => {
                    cpu_info.flags = value.split_whitespace().map(|s| s.to_string()).collect();
                }
                "CPU implementer" if cpu_info.vendor.is_empty() => {
                    // ARM CPU implementer code
                    cpu_info.vendor = match value {
                        "0x41" => "ARM".to_string(),
                        "0x4e" => "NVIDIA".to_string(),
                        "0x51" => "Qualcomm".to_string(),
                        "0x61" => "Apple".to_string(),
                        _ => value.to_string(),
                    };
                }
                _ => {}
            }
        }
    }

    Ok(cpu_info)
}

/// Parse CPU information from Linux lscpu output
///
/// # Arguments
/// * `lscpu_output` - Raw output from lscpu command
///
/// # Returns
/// * `Ok(CpuInfo)` - Parsed CPU information
/// * `Err(String)` - Parse error description
pub fn parse_lscpu_output(lscpu_output: &str) -> Result<CpuInfo, String> {
    let mut model = "Unknown CPU".to_string();
    let mut cores = 1u32;
    let mut threads = 1u32;
    let mut sockets = 1u32;
    let mut speed = "Unknown".to_string();

    for line in lscpu_output.lines() {
        if let Ok((key, value)) = parse_key_value(line, ':') {
            match key.as_str() {
                "Model name" => {
                    model = clean_value(&value);
                }
                "CPU(s)" => {
                    if let Ok(total_cpus) = value.parse::<u32>() {
                        // This gives us total logical CPUs
                        threads = total_cpus;
                    }
                }
                "Core(s) per socket" => {
                    if let Ok(cores_per_socket) = value.parse::<u32>() {
                        cores = cores_per_socket;
                    }
                }
                "Socket(s)" => {
                    if let Ok(socket_count) = value.parse::<u32>() {
                        sockets = socket_count;
                    }
                }
                "Thread(s) per core" => {
                    if let Ok(threads_per_core) = value.parse::<u32>() {
                        // Recalculate threads based on cores and threads per core
                        threads = threads_per_core;
                    }
                }
                "CPU MHz" | "CPU max MHz" => {
                    speed = format!("{} MHz", clean_value(&value));
                }
                _ => {}
            }
        }
    }

    Ok(CpuInfo {
        model,
        cores,
        threads,
        sockets,
        speed,
        ..Default::default()
    })
}

/// Parse CPU information from dmidecode processor output
///
/// # Arguments
/// * `dmidecode_output` - Raw output from dmidecode -t processor
///
/// # Returns
/// * `Ok(CpuInfo)` - Parsed CPU information
/// * `Err(String)` - Parse error description
pub fn parse_dmidecode_cpu(dmidecode_output: &str) -> Result<CpuInfo, String> {
    let model = extract_dmidecode_value(dmidecode_output, "Version")
        .unwrap_or_else(|_| "Unknown CPU".to_string());

    let speed = extract_dmidecode_value(dmidecode_output, "Current Speed")
        .or_else(|_| extract_dmidecode_value(dmidecode_output, "Max Speed"))
        .unwrap_or_else(|_| "Unknown".to_string());

    let core_count_str =
        extract_dmidecode_value(dmidecode_output, "Core Count").unwrap_or_else(|_| "1".to_string());
    let cores = core_count_str.parse::<u32>().unwrap_or(1);

    let thread_count_str = extract_dmidecode_value(dmidecode_output, "Thread Count")
        .unwrap_or_else(|_| "1".to_string());
    let threads = thread_count_str.parse::<u32>().unwrap_or(1);

    Ok(CpuInfo {
        model: clean_value(&model),
        cores,
        threads,
        sockets: 1, // dmidecode typically shows per-socket info
        speed: clean_value(&speed),
        ..Default::default()
    })
}

/// Parse CPU information from macOS system_profiler output
///
/// # Arguments
/// * `system_profiler_output` - Raw output from system_profiler SPHardwareDataType
///
/// # Returns
/// * `Ok(CpuInfo)` - Parsed CPU information
/// * `Err(String)` - Parse error description
pub fn parse_macos_cpu_info(system_profiler_output: &str) -> Result<CpuInfo, String> {
    let mut model = "Unknown CPU".to_string();
    let mut cores = 1u32;
    let mut speed = "Unknown".to_string();

    for line in system_profiler_output.lines() {
        let trimmed = line.trim();

        if trimmed.starts_with("Chip:") {
            model = trimmed
                .split(':')
                .nth(1)
                .unwrap_or("Unknown CPU")
                .trim()
                .to_string();
        } else if trimmed.starts_with("Processor Name:") {
            // Fallback for Intel Macs
            model = trimmed
                .split(':')
                .nth(1)
                .unwrap_or("Unknown CPU")
                .trim()
                .to_string();
        } else if trimmed.starts_with("Total Number of Cores:") {
            let core_str = trimmed
                .split(':')
                .nth(1)
                .unwrap_or("1")
                .split_whitespace()
                .next()
                .unwrap_or("1");
            cores = core_str.parse::<u32>().unwrap_or(1);
        } else if trimmed.starts_with("Processor Speed:") {
            speed = trimmed
                .split(':')
                .nth(1)
                .unwrap_or("Unknown")
                .trim()
                .to_string();
        }
    }

    Ok(CpuInfo {
        model: clean_value(&model),
        cores,
        threads: 1, // Apple Silicon doesn't expose thread count the same way
        sockets: 1, // Apple Silicon is single socket
        speed: clean_value(&speed),
        ..Default::default()
    })
}

/// Combine CPU information from multiple sources
///
/// # Arguments
/// * `primary` - Primary CPU info (e.g., from lscpu)
/// * `secondary` - Secondary CPU info (e.g., from dmidecode)
///
/// # Returns
/// * Combined and enhanced CPU information
pub fn combine_cpu_info(primary: CpuInfo, secondary: CpuInfo) -> CpuInfo {
    CpuInfo {
        model: if primary.model != "Unknown CPU" && !primary.model.is_empty() {
            primary.model
        } else {
            secondary.model
        },
        cores: if primary.cores > 0 {
            primary.cores
        } else {
            secondary.cores
        },
        threads: if primary.threads > 0 {
            primary.threads
        } else {
            secondary.threads
        },
        sockets: if primary.sockets > 0 {
            primary.sockets
        } else {
            secondary.sockets
        },
        speed: if primary.speed != "Unknown" && !primary.speed.is_empty() {
            primary.speed
        } else {
            secondary.speed
        },
        ..Default::default()
    }
}

/// Create CPU topology from CPU info
///
/// # Arguments
/// * `cpu_info` - Basic CPU information
/// * `numa_nodes` - Number of NUMA nodes (optional)
///
/// # Returns
/// * CPU topology information
pub fn create_cpu_topology(cpu_info: &CpuInfo, numa_nodes: Option<u32>) -> CpuTopology {
    let total_cores = cpu_info.cores * cpu_info.sockets;
    let total_threads = total_cores * cpu_info.threads;

    CpuTopology {
        total_cores,
        total_threads,
        sockets: cpu_info.sockets,
        cores_per_socket: cpu_info.cores,
        threads_per_core: cpu_info.threads,
        numa_nodes: numa_nodes.unwrap_or(1),
        cpu_model: cpu_info.model.clone(),
    }
}

/// Create CPU summary string
///
/// # Arguments
/// * `cpu_topology` - CPU topology information
///
/// # Returns
/// * Human-readable CPU summary string
pub fn create_cpu_summary(cpu_topology: &CpuTopology) -> String {
    format!(
        "{} ({} Socket{}, {} Core{}/Socket, {} Thread{}/Core, {} NUMA Node{})",
        cpu_topology.cpu_model,
        cpu_topology.sockets,
        if cpu_topology.sockets == 1 { "" } else { "s" },
        cpu_topology.cores_per_socket,
        if cpu_topology.cores_per_socket == 1 {
            ""
        } else {
            "s"
        },
        cpu_topology.threads_per_core,
        if cpu_topology.threads_per_core == 1 {
            ""
        } else {
            "s"
        },
        cpu_topology.numa_nodes,
        if cpu_topology.numa_nodes == 1 {
            ""
        } else {
            "s"
        }
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_lscpu_output() {
        let lscpu_output = r#"Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          16
On-line CPU(s) list:             0-15
Thread(s) per core:              2
Core(s) per socket:              8
Socket(s):                       1
Model name:                      Intel(R) Core(TM) i7-10875H CPU @ 2.30GHz
CPU family:                      6
Model:                           165
Stepping:                        2
CPU MHz:                         2300.000"#;

        let cpu_info = parse_lscpu_output(lscpu_output).unwrap();
        assert_eq!(cpu_info.model, "Intel(R) Core(TM) i7-10875H CPU @ 2.30GHz");
        assert_eq!(cpu_info.cores, 8);
        assert_eq!(cpu_info.threads, 2);
        assert_eq!(cpu_info.sockets, 1);
        assert_eq!(cpu_info.speed, "2300.000 MHz");
    }

    #[test]
    fn test_parse_macos_cpu_info() {
        let macos_output = r#"Hardware Overview:

      Model Name: MacBook Pro
      Model Identifier: MacBookPro18,2
      Chip: Apple M1 Max
      Total Number of Cores: 10 (8 performance and 2 efficiency)
      Memory: 32 GB
      System Firmware Version: 8419.121.2
      OS Loader Version: 8419.121.2"#;

        let cpu_info = parse_macos_cpu_info(macos_output).unwrap();
        assert_eq!(cpu_info.model, "Apple M1 Max");
        assert_eq!(cpu_info.cores, 10);
        assert_eq!(cpu_info.sockets, 1);
    }

    #[test]
    fn test_combine_cpu_info() {
        let primary = CpuInfo {
            model: "Intel Core i7".to_string(),
            cores: 8,
            threads: 2,
            sockets: 1,
            speed: "Unknown".to_string(),
            ..Default::default()
        };

        let secondary = CpuInfo {
            model: "Unknown CPU".to_string(),
            cores: 0,
            threads: 0,
            sockets: 0,
            speed: "2.3 GHz".to_string(),
            ..Default::default()
        };

        let combined = combine_cpu_info(primary, secondary);
        assert_eq!(combined.model, "Intel Core i7");
        assert_eq!(combined.cores, 8);
        assert_eq!(combined.speed, "2.3 GHz");
    }

    #[test]
    fn test_create_cpu_topology() {
        let cpu_info = CpuInfo {
            model: "Intel Core i7".to_string(),
            cores: 8,
            threads: 2,
            sockets: 1,
            speed: "2.3 GHz".to_string(),
            ..Default::default()
        };

        let topology = create_cpu_topology(&cpu_info, Some(1));
        assert_eq!(topology.total_cores, 8);
        assert_eq!(topology.total_threads, 16);
        assert_eq!(topology.sockets, 1);
        assert_eq!(topology.cores_per_socket, 8);
        assert_eq!(topology.threads_per_core, 2);
        assert_eq!(topology.numa_nodes, 1);
    }

    #[test]
    fn test_create_cpu_summary() {
        let topology = CpuTopology {
            total_cores: 16,
            total_threads: 32,
            sockets: 2,
            cores_per_socket: 8,
            threads_per_core: 2,
            numa_nodes: 2,
            cpu_model: "Intel Xeon Gold 6226R".to_string(),
        };

        let summary = create_cpu_summary(&topology);
        assert!(summary.contains("Intel Xeon Gold 6226R"));
        assert!(summary.contains("2 Sockets"));
        assert!(summary.contains("8 Cores/Socket"));
        assert!(summary.contains("2 Threads/Core"));
        assert!(summary.contains("2 NUMA Nodes"));
    }
}