diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-05-24 13:13:03 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-05-24 13:24:28 +0200 |
commit | fe40a9f98f599699b0989d8c8cb35cb24eb2e52f (patch) | |
tree | f4ee45ae1a0762edc121ea98ce03d39557424fc7 /libavutil/cpu.c | |
parent | a4d3757b29b5e9affd97cbc4e6fc2d202378610b (diff) | |
parent | 2a6eaeaa85d17b27ee0dd449183ec197c35c9675 (diff) | |
download | ffmpeg-fe40a9f98f599699b0989d8c8cb35cb24eb2e52f.tar.gz |
Merge commit '2a6eaeaa85d17b27ee0dd449183ec197c35c9675'
* commit '2a6eaeaa85d17b27ee0dd449183ec197c35c9675':
Move get_logical_cpus() from lavc/pthread to lavu/cpu.
Conflicts:
doc/APIchanges
libavcodec/pthread.c
libavutil/cpu.c
libavutil/cpu.h
libavutil/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavutil/cpu.c')
-rw-r--r-- | libavutil/cpu.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/libavutil/cpu.c b/libavutil/cpu.c index a1d1547033..0560ddbaf4 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -20,6 +20,27 @@ #include "config.h" #include "opt.h" +#if HAVE_SCHED_GETAFFINITY +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif +#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/param.h> +#include <sys/sysctl.h> +#endif +#if HAVE_SYSCONF +#include <unistd.h> +#endif + static int flags, checked; void av_force_cpu_flags(int arg){ @@ -173,6 +194,40 @@ int av_parse_cpu_caps(unsigned *flags, const char *s) return av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, flags); } + +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> |