It will be useful in lavfi, and could conceivably be useful to the user applications as well.
Unified · Split
| @@ -13,6 +13,9 @@ libavutil: 2012-10-22 · 12 unmodified lines | ||
| 13 | 13 | |
| 14 | 14 | API changes, most recent first: |
| 15 | 15 | |
| 16 | +2013-xx-xx - xxxxxxx - lavu 52.12.0 - cpu.h | |
| 17 | + Add av_cpu_count() function for getting the number of logical CPUs. | |
| 18 | + | |
| 16 | 19 | 2013-05-xx - xxxxxxx - lavc 55.7.0 - avcodec.h |
| 17 | 20 | Add picture_structure to AVCodecParserContext. |
| 18 | 21 | |
| @@ -31,29 +31,12 @@ · 30 unmodified lines | ||
| 31 | 31 | |
| 32 | 32 | #include "config.h" |
| 33 | 33 | |
| 34 | −#if HAVE_SCHED_GETAFFINITY | |
| 35 | −#define _GNU_SOURCE | |
| 36 | −#include <sched.h> | |
| 37 | −#endif | |
| 38 | −#if HAVE_GETPROCESSAFFINITYMASK | |
| 39 | −#include <windows.h> | |
| 40 | −#endif | |
| 41 | −#if HAVE_SYSCTL | |
| 42 | −#if HAVE_SYS_PARAM_H | |
| 43 | −#include <sys/param.h> | |
| 44 | −#endif | |
| 45 | −#include <sys/types.h> | |
| 46 | −#include <sys/sysctl.h> | |
| 47 | −#endif | |
| 48 | −#if HAVE_SYSCONF | |
| 49 | −#include <unistd.h> | |
| 50 | −#endif | |
| 51 | − | |
| 52 | 34 | #include "avcodec.h" |
| 53 | 35 | #include "internal.h" |
| 54 | 36 | #include "thread.h" |
| 55 | 37 | #include "libavutil/avassert.h" |
| 56 | 38 | #include "libavutil/common.h" |
| 39 | +#include "libavutil/cpu.h" | |
| 57 | 40 | |
| 58 | 41 | #if HAVE_PTHREADS |
| 59 | 42 | #include <pthread.h> |
| @@ -153,40 +136,6 @@ typedef struct FrameThreadContext { · 93 unmodified lines | ||
| 153 | 136 | * limit the number of threads to 16 for automatic detection */ |
| 154 | 137 | #define MAX_AUTO_THREADS 16 |
| 155 | 138 | |
| 156 | −static int get_logical_cpus(AVCodecContext *avctx) | |
| 157 | −{ | |
| 158 | − int ret, nb_cpus = 1; | |
| 159 | −#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) | |
| 160 | − cpu_set_t cpuset; | |
| 161 | − | |
| 162 | − CPU_ZERO(&cpuset); | |
| 163 | − | |
| 164 | − ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); | |
| 165 | − if (!ret) { | |
| 166 | − nb_cpus = CPU_COUNT(&cpuset); | |
| 167 | − } | |
| 168 | −#elif HAVE_GETPROCESSAFFINITYMASK | |
| 169 | − DWORD_PTR proc_aff, sys_aff; | |
| 170 | − ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); | |
| 171 | − if (ret) | |
| 172 | − nb_cpus = av_popcount64(proc_aff); | |
| 173 | −#elif HAVE_SYSCTL && defined(HW_NCPU) | |
| 174 | − int mib[2] = { CTL_HW, HW_NCPU }; | |
| 175 | − size_t len = sizeof(nb_cpus); | |
| 176 | − | |
| 177 | − ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); | |
| 178 | − if (ret == -1) | |
| 179 | − nb_cpus = 0; | |
| 180 | −#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) | |
| 181 | − nb_cpus = sysconf(_SC_NPROC_ONLN); | |
| 182 | −#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) | |
| 183 | − nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
| 184 | −#endif | |
| 185 | − av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); | |
| 186 | − return nb_cpus; | |
| 187 | −} | |
| 188 | − | |
| 189 | − | |
| 190 | 139 | static void* attribute_align_arg worker(void *v) |
| 191 | 140 | { |
| 192 | 141 | AVCodecContext *avctx = v; |
| @@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx) · 99 unmodified lines | ||
| 292 | 241 | int thread_count = avctx->thread_count; |
| 293 | 242 | |
| 294 | 243 | if (!thread_count) { |
| 295 | − int nb_cpus = get_logical_cpus(avctx); | |
| 244 | + int nb_cpus = av_cpu_count(); | |
| 245 | + av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); | |
| 296 | 246 | // use number of cores + 1 as thread count if there is more than one |
| 297 | 247 | if (nb_cpus > 1) |
| 298 | 248 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |
| @@ -790,7 +740,8 @@ static int frame_thread_init(AVCodecContext *avctx) · 491 unmodified lines | ||
| 790 | 740 | int i, err = 0; |
| 791 | 741 | |
| 792 | 742 | if (!thread_count) { |
| 793 | − int nb_cpus = get_logical_cpus(avctx); | |
| 743 | + int nb_cpus = av_cpu_count(); | |
| 744 | + av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); | |
| 794 | 745 | // use number of cores + 1 as thread count if there is more than one |
| 795 | 746 | if (nb_cpus > 1) |
| 796 | 747 | thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS); |
| @@ -20,6 +20,24 @@ · 19 unmodified lines | ||
| 20 | 20 | #include "config.h" |
| 21 | 21 | #include "opt.h" |
| 22 | 22 | |
| 23 | +#if HAVE_SCHED_GETAFFINITY | |
| 24 | +#define _GNU_SOURCE | |
| 25 | +#include <sched.h> | |
| 26 | +#endif | |
| 27 | +#if HAVE_GETPROCESSAFFINITYMASK | |
| 28 | +#include <windows.h> | |
| 29 | +#endif | |
| 30 | +#if HAVE_SYSCTL | |
| 31 | +#if HAVE_SYS_PARAM_H | |
| 32 | +#include <sys/param.h> | |
| 33 | +#endif | |
| 34 | +#include <sys/types.h> | |
| 35 | +#include <sys/sysctl.h> | |
| 36 | +#endif | |
| 37 | +#if HAVE_SYSCONF | |
| 38 | +#include <unistd.h> | |
| 39 | +#endif | |
| 40 | + | |
| 23 | 41 | static int cpuflags_mask = -1, checked; |
| 24 | 42 | |
| 25 | 43 | int av_get_cpu_flags(void) |
| @@ -109,6 +127,39 @@ int av_parse_cpu_flags(const char *s) · 83 unmodified lines | ||
| 109 | 127 | return flags & INT_MAX; |
| 110 | 128 | } |
| 111 | 129 | |
| 130 | +int av_cpu_count(void) | |
| 131 | +{ | |
| 132 | + int ret, nb_cpus = 1; | |
| 133 | +#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT) | |
| 134 | + cpu_set_t cpuset; | |
| 135 | + | |
| 136 | + CPU_ZERO(&cpuset); | |
| 137 | + | |
| 138 | + ret = sched_getaffinity(0, sizeof(cpuset), &cpuset); | |
| 139 | + if (!ret) { | |
| 140 | + nb_cpus = CPU_COUNT(&cpuset); | |
| 141 | + } | |
| 142 | +#elif HAVE_GETPROCESSAFFINITYMASK | |
| 143 | + DWORD_PTR proc_aff, sys_aff; | |
| 144 | + ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff); | |
| 145 | + if (ret) | |
| 146 | + nb_cpus = av_popcount64(proc_aff); | |
| 147 | +#elif HAVE_SYSCTL && defined(HW_NCPU) | |
| 148 | + int mib[2] = { CTL_HW, HW_NCPU }; | |
| 149 | + size_t len = sizeof(nb_cpus); | |
| 150 | + | |
| 151 | + ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0); | |
| 152 | + if (ret == -1) | |
| 153 | + nb_cpus = 0; | |
| 154 | +#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN) | |
| 155 | + nb_cpus = sysconf(_SC_NPROC_ONLN); | |
| 156 | +#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) | |
| 157 | + nb_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
| 158 | +#endif | |
| 159 | + | |
| 160 | + return nb_cpus; | |
| 161 | +} | |
| 162 | + | |
| 112 | 163 | #ifdef TEST |
| 113 | 164 | |
| 114 | 165 | #include <stdio.h> |
| @@ -76,6 +76,11 @@ void av_set_cpu_flags_mask(int mask); · 75 unmodified lines | ||
| 76 | 76 | */ |
| 77 | 77 | int av_parse_cpu_flags(const char *s); |
| 78 | 78 | |
| 79 | +/** | |
| 80 | + * @return the number of logical CPU cores present. | |
| 81 | + */ | |
| 82 | +int av_cpu_count(void); | |
| 83 | + | |
| 79 | 84 | /* The following CPU-specific functions shall not be called directly. */ |
| 80 | 85 | int ff_get_cpu_flags_arm(void); |
| 81 | 86 | int ff_get_cpu_flags_ppc(void); |
| @@ -37,7 +37,7 @@ · 36 unmodified lines | ||
| 37 | 37 | */ |
| 38 | 38 | |
| 39 | 39 | #define LIBAVUTIL_VERSION_MAJOR 52 |
| 40 | −#define LIBAVUTIL_VERSION_MINOR 11 | |
| 40 | +#define LIBAVUTIL_VERSION_MINOR 12 | |
| 41 | 41 | #define LIBAVUTIL_VERSION_MICRO 0 |
| 42 | 42 | |
| 43 | 43 | #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |