cpu-test: Add unistd.h #include for getopt()
[libav.git] / libavutil / cpu.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdint.h>
20
21 #include "cpu.h"
22 #include "cpu_internal.h"
23 #include "config.h"
24 #include "opt.h"
25 #include "common.h"
26
27 #if HAVE_SCHED_GETAFFINITY
28 #define _GNU_SOURCE
29 #include <sched.h>
30 #endif
31 #if HAVE_GETPROCESSAFFINITYMASK
32 #include <windows.h>
33 #endif
34 #if HAVE_SYSCTL
35 #if HAVE_SYS_PARAM_H
36 #include <sys/param.h>
37 #endif
38 #include <sys/types.h>
39 #include <sys/sysctl.h>
40 #endif
41 #if HAVE_SYSCONF
42 #include <unistd.h>
43 #endif
44
45 static int cpuflags_mask = -1, checked;
46
47 int av_get_cpu_flags(void)
48 {
49 static int flags;
50
51 if (checked)
52 return flags;
53
54 if (ARCH_AARCH64)
55 flags = ff_get_cpu_flags_aarch64();
56 if (ARCH_ARM)
57 flags = ff_get_cpu_flags_arm();
58 if (ARCH_PPC)
59 flags = ff_get_cpu_flags_ppc();
60 if (ARCH_X86)
61 flags = ff_get_cpu_flags_x86();
62
63 flags &= cpuflags_mask;
64 checked = 1;
65
66 return flags;
67 }
68
69 void av_set_cpu_flags_mask(int mask)
70 {
71 cpuflags_mask = mask;
72 checked = 0;
73 }
74
75 int av_parse_cpu_flags(const char *s)
76 {
77 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
78 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
79 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
80 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
81 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
82 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
83 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
84 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
85 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
86 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
87 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
88 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
89 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
90 #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
91 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
92 #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
93 #define CPUFLAG_BMI1 (AV_CPU_FLAG_BMI1)
94 #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | CPUFLAG_BMI1)
95 static const AVOption cpuflags_opts[] = {
96 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
97 #if ARCH_PPC
98 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
99 #elif ARCH_X86
100 { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
101 { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
102 { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
103 { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
104 { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
105 { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
106 { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
107 { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
108 { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
109 { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
110 { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
111 { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
112 { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
113 { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
114 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
115 { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
116 { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI1 }, .unit = "flags" },
117 { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI2 }, .unit = "flags" },
118 { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
119 { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
120 { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
121 #elif ARCH_ARM
122 { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
123 { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
124 { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
125 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
126 { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
127 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
128 #elif ARCH_AARCH64
129 { "armv8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8 }, .unit = "flags" },
130 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
131 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
132 #endif
133 { NULL },
134 };
135 static const AVClass class = {
136 .class_name = "cpuflags",
137 .item_name = av_default_item_name,
138 .option = cpuflags_opts,
139 .version = LIBAVUTIL_VERSION_INT,
140 };
141
142 int flags = 0, ret;
143 const AVClass *pclass = &class;
144
145 if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
146 return ret;
147
148 return flags & INT_MAX;
149 }
150
151 int av_cpu_count(void)
152 {
153 int nb_cpus = 1;
154 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
155 cpu_set_t cpuset;
156
157 CPU_ZERO(&cpuset);
158
159 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
160 nb_cpus = CPU_COUNT(&cpuset);
161 #elif HAVE_GETPROCESSAFFINITYMASK
162 DWORD_PTR proc_aff, sys_aff;
163 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
164 nb_cpus = av_popcount64(proc_aff);
165 #elif HAVE_SYSCTL && defined(HW_NCPU)
166 int mib[2] = { CTL_HW, HW_NCPU };
167 size_t len = sizeof(nb_cpus);
168
169 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
170 nb_cpus = 0;
171 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
172 nb_cpus = sysconf(_SC_NPROC_ONLN);
173 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
174 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
175 #endif
176
177 return nb_cpus;
178 }
179
180 #ifdef TEST
181
182 #include <stdio.h>
183 #if HAVE_UNISTD_H
184 #include <unistd.h>
185 #endif
186
187 #include "avstring.h"
188
189 #if !HAVE_GETOPT
190 #include "compat/getopt.c"
191 #endif
192
193 static const struct {
194 int flag;
195 const char *name;
196 } cpu_flag_tab[] = {
197 #if ARCH_AARCH64
198 { AV_CPU_FLAG_ARMV8, "armv8" },
199 { AV_CPU_FLAG_NEON, "neon" },
200 { AV_CPU_FLAG_VFP, "vfp" },
201 #elif ARCH_ARM
202 { AV_CPU_FLAG_ARMV5TE, "armv5te" },
203 { AV_CPU_FLAG_ARMV6, "armv6" },
204 { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
205 { AV_CPU_FLAG_VFP, "vfp" },
206 { AV_CPU_FLAG_VFPV3, "vfpv3" },
207 { AV_CPU_FLAG_NEON, "neon" },
208 #elif ARCH_PPC
209 { AV_CPU_FLAG_ALTIVEC, "altivec" },
210 #elif ARCH_X86
211 { AV_CPU_FLAG_MMX, "mmx" },
212 { AV_CPU_FLAG_MMXEXT, "mmxext" },
213 { AV_CPU_FLAG_SSE, "sse" },
214 { AV_CPU_FLAG_SSE2, "sse2" },
215 { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
216 { AV_CPU_FLAG_SSE3, "sse3" },
217 { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
218 { AV_CPU_FLAG_SSSE3, "ssse3" },
219 { AV_CPU_FLAG_ATOM, "atom" },
220 { AV_CPU_FLAG_SSE4, "sse4.1" },
221 { AV_CPU_FLAG_SSE42, "sse4.2" },
222 { AV_CPU_FLAG_AVX, "avx" },
223 { AV_CPU_FLAG_XOP, "xop" },
224 { AV_CPU_FLAG_FMA3, "fma3" },
225 { AV_CPU_FLAG_FMA4, "fma4" },
226 { AV_CPU_FLAG_3DNOW, "3dnow" },
227 { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
228 { AV_CPU_FLAG_CMOV, "cmov" },
229 { AV_CPU_FLAG_AVX2, "avx2" },
230 { AV_CPU_FLAG_BMI1, "bmi1" },
231 { AV_CPU_FLAG_BMI2, "bmi2" },
232 #endif
233 { 0 }
234 };
235
236 static void print_cpu_flags(int cpu_flags, const char *type)
237 {
238 int i;
239
240 fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
241 fprintf(stderr, "cpu_flags_str(%s) =", type);
242 for (i = 0; cpu_flag_tab[i].flag; i++)
243 if (cpu_flags & cpu_flag_tab[i].flag)
244 fprintf(stderr, " %s", cpu_flag_tab[i].name);
245 fprintf(stderr, "\n");
246 }
247
248
249 int main(int argc, char **argv)
250 {
251 int cpu_flags_raw = av_get_cpu_flags();
252 int cpu_flags_eff;
253 int cpu_count = av_cpu_count();
254 char threads[5] = "auto";
255
256 if (cpu_flags_raw < 0)
257 return 1;
258
259 for (;;) {
260 int c = getopt(argc, argv, "c:t:");
261 if (c == -1)
262 break;
263 switch (c) {
264 case 'c':
265 {
266 int cpuflags = av_parse_cpu_flags(optarg);
267 if (cpuflags < 0)
268 return 2;
269 av_set_cpu_flags_mask(cpuflags);
270 break;
271 }
272 case 't':
273 {
274 int len = av_strlcpy(threads, optarg, sizeof(threads));
275 if (len >= sizeof(threads)) {
276 fprintf(stderr, "Invalid thread count '%s'\n", optarg);
277 return 2;
278 }
279 }
280 }
281 }
282
283 cpu_flags_eff = av_get_cpu_flags();
284
285 if (cpu_flags_eff < 0)
286 return 3;
287
288 print_cpu_flags(cpu_flags_raw, "raw");
289 print_cpu_flags(cpu_flags_eff, "effective");
290 fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count);
291
292 return 0;
293 }
294
295 #endif