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
1313
1414 API changes, most recent first:
1515
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+
1619 2013-05-xx - xxxxxxx - lavc 55.7.0 - avcodec.h
1720 Add picture_structure to AVCodecParserContext.
1821
libavcodec/pthread.c+5−54
@@ -31,29 +31,12 @@ · 30 unmodified lines
3131
3232 #include "config.h"
3333
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−
5234 #include "avcodec.h"
5335 #include "internal.h"
5436 #include "thread.h"
5537 #include "libavutil/avassert.h"
5638 #include "libavutil/common.h"
39+#include "libavutil/cpu.h"
5740
5841 #if HAVE_PTHREADS
5942 #include <pthread.h>
@@ -153,40 +136,6 @@ typedef struct FrameThreadContext { · 93 unmodified lines
153136 * limit the number of threads to 16 for automatic detection */
154137 #define MAX_AUTO_THREADS 16
155138
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−
190139 static void* attribute_align_arg worker(void *v)
191140 {
192141 AVCodecContext *avctx = v;
@@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx) · 99 unmodified lines
292241 int thread_count = avctx->thread_count;
293242
294243 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);
296246 // use number of cores + 1 as thread count if there is more than one
297247 if (nb_cpus > 1)
298248 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
790740 int i, err = 0;
791741
792742 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);
794745 // use number of cores + 1 as thread count if there is more than one
795746 if (nb_cpus > 1)
796747 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
libavutil/cpu.c+51−0
@@ -20,6 +20,24 @@ · 19 unmodified lines
2020 #include "config.h"
2121 #include "opt.h"
2222
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+
2341 static int cpuflags_mask = -1, checked;
2442
2543 int av_get_cpu_flags(void)
@@ -109,6 +127,39 @@ int av_parse_cpu_flags(const char *s) · 83 unmodified lines
109127 return flags & INT_MAX;
110128 }
111129
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+
112163 #ifdef TEST
113164
114165 #include <stdio.h>
libavutil/cpu.h+5−0
@@ -76,6 +76,11 @@ void av_set_cpu_flags_mask(int mask); · 75 unmodified lines
7676 */
7777 int av_parse_cpu_flags(const char *s);
7878
79+/**
80+ * @return the number of logical CPU cores present.
81+ */
82+int av_cpu_count(void);
83+
7984 /* The following CPU-specific functions shall not be called directly. */
8085 int ff_get_cpu_flags_arm(void);
8186 int ff_get_cpu_flags_ppc(void);
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
3737 */
3838
3939 #define LIBAVUTIL_VERSION_MAJOR 52
40−#define LIBAVUTIL_VERSION_MINOR 11
40+#define LIBAVUTIL_VERSION_MINOR 12
4141 #define LIBAVUTIL_VERSION_MICRO 0
4242
4343 #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