@@ -431,45 +431,58 @@ pub fn get_soc_info() -> WithError<SocInfo> {
431431 let mut info = SocInfo :: default ( ) ;
432432
433433 // SPHardwareDataType.0.chip_type
434- let chip_name = out[ "SPHardwareDataType" ] [ 0 ] [ "chip_type" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
434+ let chip_name =
435+ out[ "SPHardwareDataType" ] [ 0 ] [ "chip_type" ] . as_str ( ) . unwrap_or ( "Unknown chip" ) . to_string ( ) ;
435436
436437 // SPHardwareDataType.0.machine_model
437- let mac_model = out[ "SPHardwareDataType" ] [ 0 ] [ "machine_model" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
438+ let mac_model =
439+ out[ "SPHardwareDataType" ] [ 0 ] [ "machine_model" ] . as_str ( ) . unwrap_or ( "Unknown model" ) . to_string ( ) ;
438440
439441 // SPHardwareDataType.0.physical_memory -> "x GB"
440- let mem_gb = out[ "SPHardwareDataType" ] [ 0 ] [ "physical_memory" ] . as_str ( ) ;
441- let mem_gb = mem_gb. expect ( "No memory found" ) . strip_suffix ( " GB" ) . unwrap ( ) ;
442- let mem_gb = mem_gb. parse :: < u64 > ( ) . unwrap ( ) ;
442+ let mem_gb = out[ "SPHardwareDataType" ] [ 0 ] [ "physical_memory" ]
443+ . as_str ( )
444+ . and_then ( |mem| mem. strip_suffix ( " GB" ) )
445+ . unwrap_or ( "0" )
446+ . parse :: < u64 > ( )
447+ . unwrap_or ( 0 ) ;
443448
444449 // SPHardwareDataType.0.number_processors -> "proc x:y:z"
445- let cpu_cores = out[ "SPHardwareDataType" ] [ 0 ] [ "number_processors" ] . as_str ( ) ;
446- let cpu_cores = cpu_cores. expect ( "No CPU cores found" ) . strip_prefix ( "proc " ) . unwrap ( ) ;
447- let cpu_cores = cpu_cores. split ( ':' ) . map ( |x| x. parse :: < u64 > ( ) . unwrap ( ) ) . collect :: < Vec < _ > > ( ) ;
448- assert_eq ! ( cpu_cores. len( ) , 3 , "Invalid number of CPU cores" ) ;
449- let ( ecpu_cores, pcpu_cores, _) = ( cpu_cores[ 2 ] , cpu_cores[ 1 ] , cpu_cores[ 0 ] ) ;
450-
451- let gpu_cores = match out[ "SPDisplaysDataType" ] [ 0 ] [ "sppci_cores" ] . as_str ( ) {
452- Some ( x) => x. parse :: < u64 > ( ) . unwrap ( ) ,
453- None => 0 ,
450+ let cpu_cores = out[ "SPHardwareDataType" ] [ 0 ] [ "number_processors" ]
451+ . as_str ( )
452+ . and_then ( |cores| cores. strip_prefix ( "proc " ) )
453+ . unwrap_or ( "" )
454+ . split ( ':' )
455+ . map ( |x| x. parse :: < u64 > ( ) . unwrap_or ( 0 ) )
456+ . collect :: < Vec < _ > > ( ) ;
457+ let ( ecpu_cores, pcpu_cores) = if cpu_cores. len ( ) == 3 {
458+ ( cpu_cores[ 2 ] , cpu_cores[ 1 ] )
459+ } else {
460+ ( 0 , 0 ) // Fallback in case of invalid data
454461 } ;
455462
463+ // SPDisplaysDataType.0.sppci_cores
464+ let gpu_cores =
465+ out[ "SPDisplaysDataType" ] [ 0 ] [ "sppci_cores" ] . as_str ( ) . unwrap_or ( "0" ) . parse :: < u64 > ( ) . unwrap_or ( 0 ) ;
466+
467+ // Determine scaling based on chip type
456468 let before_m4 = chip_name. contains ( "M1" ) || chip_name. contains ( "M2" ) || chip_name. contains ( "M3" ) ;
457469 let cpu_scale: u32 = if before_m4 { 1000 * 1000 } else { 1000 } ; // MHz before M4, KHz after
458470 let gpu_scale: u32 = 1000 * 1000 ; // MHz
459471
472+ // Assign parsed values to info
460473 info. chip_name = chip_name;
461474 info. mac_model = mac_model;
462475 info. memory_gb = mem_gb as u8 ;
463476 info. gpu_cores = gpu_cores as u8 ;
464477 info. ecpu_cores = ecpu_cores as u8 ;
465478 info. pcpu_cores = pcpu_cores as u8 ;
466479
467- // cpu frequencies
480+ // CPU frequencies
468481 for ( entry, name) in IOServiceIterator :: new ( "AppleARMIODevice" ) ? {
469482 if name == "pmgr" {
470483 let item = cfio_get_props ( entry, name) ?;
471- // 1) `strings /usr/bin/powermetrics | grep voltage-states` uses non sram keys
472- // but their values are zero, so sram used here, its looks valid
484+ // 1) `strings /usr/bin/powermetrics | grep voltage-states` uses non- sram keys
485+ // but their values are zero, so sram used here; it looks valid.
473486 // 2) sudo powermetrics --samplers cpu_power -i 1000 -n 1 | grep "active residency" | grep "Cluster"
474487 info. ecpu_freqs = to_mhz ( get_dvfs_mhz ( item, "voltage-states1-sram" ) . 1 , cpu_scale) ;
475488 info. pcpu_freqs = to_mhz ( get_dvfs_mhz ( item, "voltage-states5-sram" ) . 1 , cpu_scale) ;
@@ -478,8 +491,8 @@ pub fn get_soc_info() -> WithError<SocInfo> {
478491 }
479492 }
480493
481- if info. ecpu_freqs . len ( ) == 0 || info. pcpu_freqs . len ( ) == 0 {
482- return Err ( "No CPU cores found" . into ( ) ) ;
494+ if info. ecpu_freqs . is_empty ( ) || info. pcpu_freqs . is_empty ( ) {
495+ return Err ( "No CPU frequencies found" . into ( ) ) ;
483496 }
484497
485498 Ok ( info)
0 commit comments