aboutsummaryrefslogtreecommitdiffstats
path: root/libavutil
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2013-05-23 08:59:07 +0200
committerAnton Khirnov <anton@khirnov.net>2013-05-24 09:28:00 +0200
commit2a6eaeaa85d17b27ee0dd449183ec197c35c9675 (patch)
treefe70d509dde284b0b625465ca6e81f4d72a30c4b /libavutil
parent90f9a5830b5d332de7ebb1ab45589f1870cbd65d (diff)
downloadffmpeg-2a6eaeaa85d17b27ee0dd449183ec197c35c9675.tar.gz
Move get_logical_cpus() from lavc/pthread to lavu/cpu.
It will be useful in lavfi, and could conceivably be useful to the user applications as well.
Diffstat (limited to 'libavutil')
-rw-r--r--libavutil/cpu.c51
-rw-r--r--libavutil/cpu.h5
-rw-r--r--libavutil/version.h2
3 files changed, 57 insertions, 1 deletions
diff --git a/libavutil/cpu.c b/libavutil/cpu.c
index 23016d68a9..b95158b054 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 4929512c66..f2c9c0f20e 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 066c06160f..9f0a976d9f 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, \