diff --git a/doc/APIchanges b/doc/APIchanges index 4229453b9bcc153648c479cfd7a7547cc465ae7f..f7279c6c52b7e9d5dbf8f7e109fea0b62120d315 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -13,6 +13,9 @@ libavutil: 2012-10-22 API changes, most recent first: +2013-xx-xx - xxxxxxx - lavu 52.12.0 - cpu.h + Add av_cpu_count() function for getting the number of logical CPUs. + 2013-05-xx - xxxxxxx - lavc 55.7.0 - avcodec.h Add picture_structure to AVCodecParserContext. diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c index 7eac335f74128036eb764d92fc853e3f46df5610..152335358019ee9aca492722cde6eee41053f2ae 100644 --- a/libavcodec/pthread.c +++ b/libavcodec/pthread.c @@ -31,29 +31,12 @@ #include "config.h" -#if HAVE_SCHED_GETAFFINITY -#define _GNU_SOURCE -#include <sched.h> -#endif -#if HAVE_GETPROCESSAFFINITYMASK -#include <windows.h> -#endif -#if HAVE_SYSCTL -#if HAVE_SYS_PARAM_H -#include <sys/param.h> -#endif -#include <sys/types.h> -#include <sys/sysctl.h> -#endif -#if HAVE_SYSCONF -#include <unistd.h> -#endif - #include "avcodec.h" #include "internal.h" #include "thread.h" #include "libavutil/avassert.h" #include "libavutil/common.h" +#include "libavutil/cpu.h" #if HAVE_PTHREADS #include <pthread.h> @@ -153,40 +136,6 @@ typedef struct FrameThreadContext { * limit the number of threads to 16 for automatic detection */ #define MAX_AUTO_THREADS 16 -static int get_logical_cpus(AVCodecContext *avctx) -{ - int ret, nb_cpus = 1; -#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) - cpu_set_t cpuset; - - CPU_ZERO(&cpuset); - - ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); - if (!ret) { - nb_cpus = CPU_COUNT(&cpuset); - } -#elif HAVE_GETPROCESSAFFINITYMASK - DWORD_PTR proc_aff, sys_aff; - ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); - if (ret) - nb_cpus = av_popcount64(proc_aff); -#elif HAVE_SYSCTL && defined(HW_NCPU) - int mib[2] = { CTL_HW, HW_NCPU }; - size_t len = sizeof(nb_cpus); - - ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); - if (ret == -1) - nb_cpus = 0; -#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) - nb_cpus = sysconf(_SC_NPROC_ONLN); -#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) - nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); -#endif - av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); - return nb_cpus; -} - - static void* attribute_align_arg worker(void *v) { AVCodecContext *avctx = v; @@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx) int thread_count = avctx->thread_count; if (!thread_count) { - int nb_cpus = get_logical_cpus(avctx); + int nb_cpus = av_cpu_count(); + av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); // use number of cores + 1 as thread count if there is more than one if (nb_cpus > 1) thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); @@ -790,7 +740,8 @@ static int frame_thread_init(AVCodecContext *avctx) int i, err = 0; if (!thread_count) { - int nb_cpus = get_logical_cpus(avctx); + int nb_cpus = av_cpu_count(); + av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); // use number of cores + 1 as thread count if there is more than one if (nb_cpus > 1) thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 23016d68a9b55dcd20e11d56ee104b408358458c..b95158b054b58d14bef3f66ba2871bebde168f93 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -20,6 +20,24 @@ #include "config.h" #include "opt.h" +#if HAVE_SCHED_GETAFFINITY +#define _GNU_SOURCE +#include <sched.h> +#endif +#if HAVE_GETPROCESSAFFINITYMASK +#include <windows.h> +#endif +#if HAVE_SYSCTL +#if HAVE_SYS_PARAM_H +#include <sys/param.h> +#endif +#include <sys/types.h> +#include <sys/sysctl.h> +#endif +#if HAVE_SYSCONF +#include <unistd.h> +#endif + static int cpuflags_mask = -1, checked; int av_get_cpu_flags(void) @@ -109,6 +127,39 @@ int av_parse_cpu_flags(const char *s) return flags & INT_MAX; } +int av_cpu_count(void) +{ + int ret, nb_cpus = 1; +#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) + cpu_set_t cpuset; + + CPU_ZERO(&cpuset); + + ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); + if (!ret) { + nb_cpus = CPU_COUNT(&cpuset); + } +#elif HAVE_GETPROCESSAFFINITYMASK + DWORD_PTR proc_aff, sys_aff; + ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); + if (ret) + nb_cpus = av_popcount64(proc_aff); +#elif HAVE_SYSCTL && defined(HW_NCPU) + int mib[2] = { CTL_HW, HW_NCPU }; + size_t len = sizeof(nb_cpus); + + ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); + if (ret == -1) + nb_cpus = 0; +#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) + nb_cpus = sysconf(_SC_NPROC_ONLN); +#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) + nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); +#endif + + return nb_cpus; +} + #ifdef TEST #include <stdio.h> diff --git a/libavutil/cpu.h b/libavutil/cpu.h index 4929512c66f37458cb1d50c7e1829d3783505237..f2c9c0f20e04062282400f1ba185541485630337 100644 --- a/libavutil/cpu.h +++ b/libavutil/cpu.h @@ -76,6 +76,11 @@ void av_set_cpu_flags_mask(int mask); */ int av_parse_cpu_flags(const char *s); +/** + * @return the number of logical CPU cores present. + */ +int av_cpu_count(void); + /* The following CPU-specific functions shall not be called directly. */ int ff_get_cpu_flags_arm(void); int ff_get_cpu_flags_ppc(void); diff --git a/libavutil/version.h b/libavutil/version.h index 066c06160f8a4d7c85e8931144786014e62d103b..9f0a976d9f2b35bd8d523ce5da7bee97e4cbbe49 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -37,7 +37,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 11 +#define LIBAVUTIL_VERSION_MINOR 12 #define LIBAVUTIL_VERSION_MICRO 0 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \