History

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.

Anton Khirnov committed 13 years ago

Commit 2a6eaea1 parent 90f9a585 files · +65 −55Browse files

Unified · Split

5 files
  1. doc/APIchanges +3 −0
  2. libavcodec/pthread.c +5 −54
  3. libavutil/cpu.c +51 −0
  4. libavutil/cpu.h +5 −0
  5. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2012-10-22 · 12 unmodified lines
13 13
14API changes, most recent first:14API changes, most recent first:
15 15
162013-xx-xx - xxxxxxx - lavu 52.12.0 - cpu.h
17 Add av_cpu_count() function for getting the number of logical CPUs.
18
162013-05-xx - xxxxxxx - lavc 55.7.0 - avcodec.h192013-05-xx - xxxxxxx - lavc 55.7.0 - avcodec.h
17 Add picture_structure to AVCodecParserContext.20 Add picture_structure to AVCodecParserContext.
18 21
libavcodec/pthread.c+5−54
@@ -31,29 +31,12 @@ · 30 unmodified lines
31 31
32#include "config.h"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#include "avcodec.h"34#include "avcodec.h"
53#include "internal.h"35#include "internal.h"
54#include "thread.h"36#include "thread.h"
55#include "libavutil/avassert.h"37#include "libavutil/avassert.h"
56#include "libavutil/common.h"38#include "libavutil/common.h"
39#include "libavutil/cpu.h"
57 40
58#if HAVE_PTHREADS41#if HAVE_PTHREADS
59#include <pthread.h>42#include <pthread.h>
@@ -153,40 +136,6 @@ typedef struct FrameThreadContext { · 93 unmodified lines
153 * limit the number of threads to 16 for automatic detection */136 * limit the number of threads to 16 for automatic detection */
154#define MAX_AUTO_THREADS 16137#define MAX_AUTO_THREADS 16
155 138
156static 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
190static void* attribute_align_arg worker(void *v)139static void* attribute_align_arg worker(void *v)
191{140{
192 AVCodecContext *avctx = v;141 AVCodecContext *avctx = v;
@@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx) · 99 unmodified lines
292 int thread_count = avctx->thread_count;241 int thread_count = avctx->thread_count;
293 242
294 if (!thread_count) {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 // use number of cores + 1 as thread count if there is more than one246 // use number of cores + 1 as thread count if there is more than one
297 if (nb_cpus > 1)247 if (nb_cpus > 1)
298 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);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 int i, err = 0;740 int i, err = 0;
791 741
792 if (!thread_count) {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 // use number of cores + 1 as thread count if there is more than one745 // use number of cores + 1 as thread count if there is more than one
795 if (nb_cpus > 1)746 if (nb_cpus > 1)
796 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);747 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
libavutil/cpu.c+51−0
@@ -20,6 +20,24 @@ · 19 unmodified lines
20#include "config.h"20#include "config.h"
21#include "opt.h"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
23static int cpuflags_mask = -1, checked;41static int cpuflags_mask = -1, checked;
24 42
25int av_get_cpu_flags(void)43int av_get_cpu_flags(void)
@@ -109,6 +127,39 @@ int av_parse_cpu_flags(const char *s) · 83 unmodified lines
109 return flags & INT_MAX;127 return flags & INT_MAX;
110}128}
111 129
130int 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#ifdef TEST163#ifdef TEST
113 164
114#include <stdio.h>165#include <stdio.h>
libavutil/cpu.h+5−0
@@ -76,6 +76,11 @@ void av_set_cpu_flags_mask(int mask); · 75 unmodified lines
76 */76 */
77int av_parse_cpu_flags(const char *s);77int av_parse_cpu_flags(const char *s);
78 78
79/**
80 * @return the number of logical CPU cores present.
81 */
82int av_cpu_count(void);
83
79/* The following CPU-specific functions shall not be called directly. */84/* The following CPU-specific functions shall not be called directly. */
80int ff_get_cpu_flags_arm(void);85int ff_get_cpu_flags_arm(void);
81int ff_get_cpu_flags_ppc(void);86int ff_get_cpu_flags_ppc(void);
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
37 */37 */
38 38
39#define LIBAVUTIL_VERSION_MAJOR 5239#define LIBAVUTIL_VERSION_MAJOR 52
40#define LIBAVUTIL_VERSION_MINOR 1140#define LIBAVUTIL_VERSION_MINOR 12
41#define LIBAVUTIL_VERSION_MICRO 041#define LIBAVUTIL_VERSION_MICRO 0
42 42
43#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \43#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t