From 9e9a869aa7dc3c39a883a5a5886c0bbd82e8b95c Mon Sep 17 00:00:00 2001 From: Loren Merritt Date: Tue, 1 Aug 2006 00:17:18 +0000 Subject: [PATCH] --threads=auto to detect number of cpus git-svn-id: svn://svn.videolan.org/x264/trunk@543 df754926-b1dd-0310-bc7b-ec298dee348c --- Makefile | 2 +- common/common.c | 7 ++++++- common/cpu.c | 31 +++++++++++++++++++++++++++++++ common/cpu.h | 1 + encoder/encoder.c | 2 ++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2f3ed343..fdce903a 100644 --- a/Makefile +++ b/Makefile @@ -117,7 +117,7 @@ fprofiled: mv config.mak config.mak2 sed -e 's/CFLAGS.*/& -fprofile-generate/; s/LDFLAGS.*/& -fprofile-generate/' config.mak2 > config.mak $(MAKE) x264$(EXE) - $(foreach V, $(VIDS), $(foreach I, 0 1 2, ./x264$(EXE) $(OPT$I) $(V) --progress -o $(DEVNULL) ;)) + $(foreach V, $(VIDS), $(foreach I, 0 1 2, ./x264$(EXE) $(OPT$I) $(V) --progress --threads auto -o $(DEVNULL) ;)) rm -f $(SRC2:%.c=%.o) sed -e 's/CFLAGS.*/& -fprofile-use/; s/LDFLAGS.*/& -fprofile-use/' config.mak2 > config.mak $(MAKE) diff --git a/common/common.c b/common/common.c index d67dc563..dd0825ed 100644 --- a/common/common.c +++ b/common/common.c @@ -200,7 +200,12 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) OPT("asm") p->cpu = atobool(value) ? x264_cpu_detect() : 0; OPT("threads") - p->i_threads = atoi(value); + { + if( !strcmp(value, "auto") ) + p->i_threads = 0; + else + p->i_threads = atoi(value); + } OPT("level") { if( atof(value) < 6 ) diff --git a/common/cpu.c b/common/cpu.c index be05806d..f4585d0c 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -21,6 +21,11 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/ +#if defined(HAVE_PTHREAD) && defined(SYS_LINUX) +#define _GNU_SOURCE +#include +#endif + #include #include "common.h" @@ -143,3 +148,29 @@ void x264_cpu_restore( uint32_t cpu ) } #endif + +#if defined(HAVE_PTHREAD) && ( defined(SYS_LINUX) || defined(WIN32) ) + +int x264_cpu_num_processors( void ) +{ + int np; +#if defined(WIN32) + uint32_t p_aff, s_aff; + GetProcessAffinityMask( GetCurrentProcess(), &p_aff, &s_aff ); +#else + uint64_t p_aff; + sched_getaffinity( 0, sizeof(p_aff), (cpu_set_t*)&p_aff ); +#endif + for( np = 0; p_aff != 0; p_aff >>= 1 ) + np += p_aff&1; + return np; +} + +#else + +int x264_cpu_num_processors( void ) +{ + return 1; +} + +#endif diff --git a/common/cpu.h b/common/cpu.h index a9df3f83..94101e52 100644 --- a/common/cpu.h +++ b/common/cpu.h @@ -25,6 +25,7 @@ #define _CPU_H 1 uint32_t x264_cpu_detect( void ); +int x264_cpu_num_processors( void ); /* probably MMX(EXT) centric but .... */ void x264_cpu_restore( uint32_t cpu ); diff --git a/encoder/encoder.c b/encoder/encoder.c index c9b464c6..edadd01f 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -331,6 +331,8 @@ static int x264_validate_parameters( x264_t *h ) return -1; } + if( h->param.i_threads == 0 ) + h->param.i_threads = x264_cpu_num_processors(); h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_SLICE_MAX ); h->param.i_threads = X264_MIN( h->param.i_threads, (h->param.i_height + 15) / 16 ); #ifndef HAVE_PTHREAD -- 2.40.0