From 28cd4705bb1ac54e8b907cf0a11dcfcd06191c9e Mon Sep 17 00:00:00 2001 From: saintdev Date: Wed, 11 Mar 2009 00:06:11 +0000 Subject: Clean up CPU count detection. -No longer use 'grep' on linux to detect CPU count! -Use sysctls on OSX. -SunOS and CYGWIN are unmodified. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@2254 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- libhb/ports.c | 109 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 54 deletions(-) (limited to 'libhb') diff --git a/libhb/ports.c b/libhb/ports.c index 3754a4529..2c3884436 100644 --- a/libhb/ports.c +++ b/libhb/ports.c @@ -4,22 +4,41 @@ Homepage: . It may be used under the terms of the GNU General Public License. */ -#include -#include +#ifdef USE_PTHREAD +#ifdef SYS_LINUX +#define _GNU_SOURCE +#include +#endif +#include +#endif -#if defined( SYS_BEOS ) -#include -#include -#elif defined( SYS_CYGWIN ) +#ifdef SYS_BEOS +#include +#endif + +#if defined(SYS_DARWIN) || defined(SYS_FREEBSD) +#include +#include +#endif + +#ifdef SYS_OPENBSD +#include +#include +#include +#endif + +#ifdef SYS_CYGWIN #include -#elif defined( SYS_SunOS ) -#include #endif -#if USE_PTHREAD -#include +#ifdef SYS_SunOS +#include #endif +#include +#include + + //#ifdef SYS_CYGWIN //#include //#include @@ -100,54 +119,36 @@ int hb_get_cpu_count() } cpu_count = 1; -#if defined( SYS_BEOS ) - { - system_info info; - get_system_info( &info ); - cpu_count = info.cpu_count; - } - -#elif defined( SYS_DARWIN ) || defined( SYS_FREEBSD ) - FILE * info; - char buffer[16]; - - if( ( info = popen( "/usr/sbin/sysctl hw.ncpu", "r" ) ) ) - { - memset( buffer, 0, 16 ); - if( fgets( buffer, 15, info ) ) - { - if( sscanf( buffer, "hw.ncpu: %d", &cpu_count ) != 1 ) - { - cpu_count = 1; - } - } - fclose( info ); - } +#if defined(SYS_CYGWIN) + SYSTEM_INFO cpuinfo; + GetSystemInfo( &cpuinfo ); + cpu_count = cpuinfo.dwNumberOfProcessors; -#elif defined( SYS_LINUX ) +#elif defined(SYS_LINUX) + unsigned int bit; + cpu_set_t p_aff; + memset( &p_aff, 0, sizeof(p_aff) ); + sched_getaffinity( 0, sizeof(p_aff), &p_aff ); + for( cpu_count = 0, bit = 0; bit < sizeof(p_aff); bit++ ) + cpu_count += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1; + +#elif defined(SYS_BEOS) + system_info info; + get_system_info( &info ); + cpu_count = info.cpu_count; + +#elif defined(SYS_DARWIN) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD) + size_t length = sizeof( numberOfCPUs ); +#ifdef SYS_OPENBSD + int mib[2] = { CTL_HW, HW_NCPU }; + if( sysctl(mib, 2, &cpu_count, &length, NULL, 0) ) +#else + if( sysctlbyname("hw.ncpu", &cpu_count, &length, NULL, 0) ) +#endif { - FILE * info; - char buffer[8]; - - if( ( info = popen( "grep -c '^processor' /proc/cpuinfo", - "r" ) ) ) - { - memset( buffer, 0, 8 ); - if( fgets( buffer, 7, info ) ) - { - if( sscanf( buffer, "%d", &cpu_count ) != 1 ) - { - cpu_count = 1; - } - } - fclose( info ); - } + cpu_count = 1; } -#elif defined( SYS_CYGWIN ) - SYSTEM_INFO cpuinfo; - GetSystemInfo( &cpuinfo ); - cpu_count = cpuinfo.dwNumberOfProcessors; #elif defined( SYS_SunOS ) { processorid_t cpumax; -- cgit v1.2.3