Use the hardware dictionary to specify the model of mac being used.

Move to sysctl instead of gestalt since it's being used for hw.model already.
This commit is contained in:
Steve Checkoway
2007-07-23 05:18:38 +00:00
parent 242fd6076e
commit 73485cb56a
@@ -6,6 +6,8 @@
#include "archutils/Unix/SignalHandler.h" #include "archutils/Unix/SignalHandler.h"
#include "ProductInfo.h" #include "ProductInfo.h"
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/mach.h> #include <mach/mach.h>
#include <mach/mach_time.h> #include <mach/mach_time.h>
#include <IOKit/IOKitLib.h> #include <IOKit/IOKitLib.h>
@@ -211,7 +213,7 @@ void ArchHooks_darwin::DumpDebugInfo()
{ {
/* Get system version */ /* Get system version */
RString systemVersion; RString sSystemVersion;
{ {
long major = 0, minor = 0, bugFix = 0; long major = 0, minor = 0, bugFix = 0;
@@ -219,79 +221,96 @@ void ArchHooks_darwin::DumpDebugInfo()
Gestalt( gestaltSystemVersionMinor, &minor ); Gestalt( gestaltSystemVersionMinor, &minor );
Gestalt( gestaltSystemVersionBugFix, &bugFix ); Gestalt( gestaltSystemVersionBugFix, &bugFix );
if( bugFix ) if( bugFix )
systemVersion = ssprintf( "Mac OS X %ld.%ld.%ld", major, minor, bugFix ); sSystemVersion = ssprintf( "Mac OS X %ld.%ld.%ld", major, minor, bugFix );
else else
systemVersion = ssprintf( "Mac OS X %ld.%ld", major, minor ); sSystemVersion = ssprintf( "Mac OS X %ld.%ld", major, minor );
} }
size_t size;
#define GET_PARAM( name, var ) (size = sizeof(var), sysctlbyname(name, &var, &size, NULL, 0) )
/* Get memory */ /* Get memory */
long ram; float fRam;
long vRam; char ramPower;
OSErr err = Gestalt( gestaltLogicalRAMSize, &vRam );
if (err != noErr)
vRam = 0;
err = Gestalt( gestaltPhysicalRAMSize, &ram );
if( err == noErr )
{ {
vRam -= ram; uint64_t iRam = 0;
if( vRam < 0 ) GET_PARAM( "hw.memsize", iRam );
vRam = 0; if( iRam >= 1073741824 )
ram >>= 20; {
vRam >>= 20; fRam = iRam / 1073741824.0f;
} ramPower = 'G';
else }
{ else
ram = 0; {
vRam = 0; fRam = iRam / 1048576.0f;
ramPower = 'M';
}
} }
/* Get processor information */ /* Get processor information */
RString processor; int iMaxCPUs = 0;
int numProcessors; int iCPUs = 0;
struct host_basic_info info; float fFreq;
mach_port_t host = mach_host_self(); char freqPower;
mach_msg_type_number_t size = HOST_BASIC_INFO_COUNT; RString sModel;
kern_return_t ret = host_info( host, HOST_BASIC_INFO, (host_info_t)&info, &size ); do {
char szModel[128];
uint64_t iFreq;
if( ret ) GET_PARAM( "hw.logicalcpu_max", iMaxCPUs );
{ GET_PARAM( "hw.logicalcpu", iCPUs );
numProcessors = -1; GET_PARAM( "hw.cpufrequency", iFreq );
LOG->Warn("Couldn't get host info.");
}
else
{
char *cpu_type, *cpu_subtype;
numProcessors = info.avail_cpus; if( iFreq >= 1000000000 )
slot_name( info.cpu_type, info.cpu_subtype, &cpu_type, &cpu_subtype ); {
processor = cpu_subtype; fFreq = iFreq / 1000000000.0f;
} freqPower = 'G';
}
else
{
fFreq = iFreq / 1000000.0f;
freqPower = 'M';
}
/* Get processor speed */ if( GET_PARAM("hw.model", szModel) )
long code; {
err = Gestalt( gestaltProcClkSpeed, &code ); sModel = "Unknown";
if( err != noErr ) break;
code = 0; }
sModel = szModel;
CFURLRef urlRef = CFBundleCopyResourceURL( CFBundleGetMainBundle(), CFSTR("Hardware.plist"), NULL, NULL );
float speed; if( urlRef == NULL )
char power; break;
CFDataRef dataRef = NULL;
if( code >= 1000000000 ) SInt32 error;
{ CFURLCreateDataAndPropertiesFromResource( NULL, urlRef, &dataRef, NULL, NULL, &error );
speed = code / 1000000000.0f; CFRelease( urlRef );
power = 'G'; if( dataRef == NULL )
} break;
else // This also works with binary property lists for some reason.
{ CFPropertyListRef plRef = CFPropertyListCreateFromXMLData( NULL, dataRef, kCFPropertyListImmutable, NULL );
speed = code / 1000000.0f; CFRelease( dataRef );
power = 'M'; if( plRef == NULL )
} break;
if( CFGetTypeID(plRef) != CFDictionaryGetTypeID() )
{
CFRelease( plRef );
break;
}
CFStringRef keyRef = CFStringCreateWithCStringNoCopy( NULL, szModel, kCFStringEncodingMacRoman, kCFAllocatorNull );
CFStringRef modelRef = (CFStringRef)CFDictionaryGetValue( (CFDictionaryRef)plRef, keyRef );
if( modelRef )
sModel = CFStringGetCStringPtr( modelRef, kCFStringEncodingMacRoman );
CFRelease( keyRef );
CFRelease( plRef );
} while( false );
#undef GET_PARAM
/* Send all of the information to the log */ /* Send all of the information to the log */
LOG->Info( "Processor: %s (%d)", processor.c_str(), numProcessors ); LOG->Info( "Model: %s (%d/%d)", sModel.c_str(), iCPUs, iMaxCPUs );
LOG->Info( "Clock speed %.2f %cHz", speed, power ); LOG->Info( "Clock speed %.2f %cHz", fFreq, freqPower );
LOG->Info( "%s", systemVersion.c_str()); LOG->Info( "%s", sSystemVersion.c_str());
LOG->Info( "Memory: %ld MB total, %ld MB swap", ram, vRam ); LOG->Info( "Memory: %.2f %cB", fRam, ramPower );
} }
RString ArchHooks::GetPreferredLanguage() RString ArchHooks::GetPreferredLanguage()