11#include "cpu.h"
22#include "common/sysctl.h"
3+ #include "detection/temps/temps_apple.h"
34
45static double getFrequency (const char * propName )
56{
@@ -12,33 +13,64 @@ static double getFrequency(const char* propName)
1213 return herz / 1000.0 ; //to GHz
1314}
1415
16+ static double detectCpuTemp (const FFstrbuf * cpuName )
17+ {
18+ FFlist temps ;
19+ ffListInit (& temps , sizeof (FFTempValue ));
20+
21+ if (ffStrbufStartsWithS (cpuName , "Apple M1" ))
22+ ffDetectCoreTemps (FF_TEMP_CPU_M1X , & temps );
23+ else if (ffStrbufStartsWithS (cpuName , "Apple M2" ))
24+ ffDetectCoreTemps (FF_TEMP_CPU_M2X , & temps );
25+ else //TODO: PPC?
26+ ffDetectCoreTemps (FF_TEMP_CPU_X64 , & temps );
27+
28+ if (temps .length == 0 )
29+ return FF_CPU_TEMP_UNSET ;
30+
31+ double result = 0 ;
32+ for (uint32_t i = 0 ; i < temps .length ; ++ i )
33+ {
34+ FFTempValue * tempValue = (FFTempValue * )ffListGet (& temps , i );
35+ result += tempValue -> value ;
36+ //TODO: do we really need this?
37+ ffStrbufDestroy (& tempValue -> name );
38+ ffStrbufDestroy (& tempValue -> deviceClass );
39+ }
40+ result /= temps .length ;
41+ ffListDestroy (& temps );
42+ return result ;
43+ }
44+
1545void ffDetectCPUImpl (const FFinstance * instance , FFCPUResult * cpu , bool cached )
1646{
1747 FF_UNUSED (instance );
1848
19- //TODO find a way to detect this
20- cpu -> temperature = FF_CPU_TEMP_UNSET ;
21-
22- if (cached )
23- return ;
49+ if (!cached )
50+ {
51+ ffSysctlGetString ("machdep.cpu.brand_string" , & cpu -> name );
52+ ffSysctlGetString ("machdep.cpu.vendor" , & cpu -> vendor );
2453
25- ffSysctlGetString ("machdep.cpu.brand_string" , & cpu -> name );
26- ffSysctlGetString ("machdep.cpu.vendor" , & cpu -> vendor );
54+ cpu -> coresPhysical = (uint16_t ) ffSysctlGetInt ("hw.physicalcpu_max" , 1 );
55+ if (cpu -> coresPhysical == 1 )
56+ cpu -> coresPhysical = (uint16_t ) ffSysctlGetInt ("hw.physicalcpu" , 1 );
2757
28- cpu -> coresPhysical = (uint16_t ) ffSysctlGetInt ("hw.physicalcpu_max " , 1 );
29- if (cpu -> coresPhysical == 1 )
30- cpu -> coresPhysical = (uint16_t ) ffSysctlGetInt ("hw.physicalcpu " , 1 );
58+ cpu -> coresLogical = (uint16_t ) ffSysctlGetInt ("hw.logicalcpu_max " , 1 );
59+ if (cpu -> coresLogical == 1 )
60+ cpu -> coresLogical = (uint16_t ) ffSysctlGetInt ("hw.ncpu " , 1 );
3161
32- cpu -> coresLogical = (uint16_t ) ffSysctlGetInt ("hw.logicalcpu_max " , 1 );
33- if (cpu -> coresLogical == 1 )
34- cpu -> coresLogical = (uint16_t ) ffSysctlGetInt ("hw.ncpu " , 1 );
62+ cpu -> coresOnline = (uint16_t ) ffSysctlGetInt ("hw.logicalcpu " , 1 );
63+ if (cpu -> coresOnline == 1 )
64+ cpu -> coresOnline = (uint16_t ) ffSysctlGetInt ("hw.activecpu " , 1 );
3565
36- cpu -> coresOnline = (uint16_t ) ffSysctlGetInt ("hw.logicalcpu" , 1 );
37- if (cpu -> coresOnline == 1 )
38- cpu -> coresOnline = (uint16_t ) ffSysctlGetInt ("hw.activecpu" , 1 );
66+ cpu -> frequencyMin = getFrequency ("hw.cpufrequency_min" );
67+ cpu -> frequencyMax = getFrequency ("hw.cpufrequency_max" );
68+ if (cpu -> frequencyMax == 0.0 )
69+ cpu -> frequencyMax = getFrequency ("hw.cpufrequency" );
70+ }
3971
40- cpu -> frequencyMin = getFrequency ( "hw.cpufrequency_min" );
41- cpu -> frequencyMax = getFrequency ( "hw.cpufrequency_max" );
42- if ( cpu -> frequencyMax == 0.0 )
43- cpu -> frequencyMax = getFrequency ( "hw.cpufrequency" ) ;
72+ if ( instance -> config . cpuTemp )
73+ cpu -> temperature = detectCpuTemp ( & cpu -> name );
74+ else
75+ cpu -> temperature = FF_CPU_TEMP_UNSET ;
4476}
0 commit comments