cpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include <stdint.h>
  19. #include "cpu.h"
  20. #include "cpu_internal.h"
  21. #include "config.h"
  22. #include "opt.h"
  23. #include "common.h"
  24. #if HAVE_SCHED_GETAFFINITY
  25. #define _GNU_SOURCE
  26. #include <sched.h>
  27. #endif
  28. #if HAVE_GETPROCESSAFFINITYMASK
  29. #include <windows.h>
  30. #endif
  31. #if HAVE_SYSCTL
  32. #if HAVE_SYS_PARAM_H
  33. #include <sys/param.h>
  34. #endif
  35. #include <sys/types.h>
  36. #include <sys/sysctl.h>
  37. #endif
  38. #if HAVE_SYSCONF
  39. #include <unistd.h>
  40. #endif
  41. static int cpuflags_mask = -1, checked;
  42. int av_get_cpu_flags(void)
  43. {
  44. static int flags;
  45. if (checked)
  46. return flags;
  47. if (ARCH_AARCH64)
  48. flags = ff_get_cpu_flags_aarch64();
  49. if (ARCH_ARM)
  50. flags = ff_get_cpu_flags_arm();
  51. if (ARCH_PPC)
  52. flags = ff_get_cpu_flags_ppc();
  53. if (ARCH_X86)
  54. flags = ff_get_cpu_flags_x86();
  55. flags &= cpuflags_mask;
  56. checked = 1;
  57. return flags;
  58. }
  59. void av_set_cpu_flags_mask(int mask)
  60. {
  61. cpuflags_mask = mask;
  62. checked = 0;
  63. }
  64. int av_parse_cpu_flags(const char *s)
  65. {
  66. #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
  67. #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
  68. #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
  69. #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
  70. #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
  71. #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
  72. #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
  73. #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
  74. #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
  75. #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
  76. #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
  77. #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
  78. #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
  79. #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
  80. #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
  81. #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
  82. #define CPUFLAG_BMI1 (AV_CPU_FLAG_BMI1)
  83. #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | CPUFLAG_BMI1)
  84. static const AVOption cpuflags_opts[] = {
  85. { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
  86. #if ARCH_PPC
  87. { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
  88. #elif ARCH_X86
  89. { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
  90. { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
  91. { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
  92. { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
  93. { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
  94. { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
  95. { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
  96. { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
  97. { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
  98. { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
  99. { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
  100. { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
  101. { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
  102. { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
  103. { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
  104. { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
  105. { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI1 }, .unit = "flags" },
  106. { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI2 }, .unit = "flags" },
  107. { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
  108. { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
  109. { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
  110. #elif ARCH_ARM
  111. { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
  112. { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
  113. { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
  114. { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
  115. { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
  116. { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
  117. #elif ARCH_AARCH64
  118. { "armv8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8 }, .unit = "flags" },
  119. { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
  120. { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
  121. #endif
  122. { NULL },
  123. };
  124. static const AVClass class = {
  125. .class_name = "cpuflags",
  126. .item_name = av_default_item_name,
  127. .option = cpuflags_opts,
  128. .version = LIBAVUTIL_VERSION_INT,
  129. };
  130. int flags = 0, ret;
  131. const AVClass *pclass = &class;
  132. if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
  133. return ret;
  134. return flags & INT_MAX;
  135. }
  136. int av_cpu_count(void)
  137. {
  138. int nb_cpus = 1;
  139. #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
  140. cpu_set_t cpuset;
  141. CPU_ZERO(&cpuset);
  142. if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
  143. nb_cpus = CPU_COUNT(&cpuset);
  144. #elif HAVE_GETPROCESSAFFINITYMASK
  145. DWORD_PTR proc_aff, sys_aff;
  146. if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
  147. nb_cpus = av_popcount64(proc_aff);
  148. #elif HAVE_SYSCTL && defined(HW_NCPU)
  149. int mib[2] = { CTL_HW, HW_NCPU };
  150. size_t len = sizeof(nb_cpus);
  151. if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
  152. nb_cpus = 0;
  153. #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
  154. nb_cpus = sysconf(_SC_NPROC_ONLN);
  155. #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
  156. nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  157. #endif
  158. return nb_cpus;
  159. }
  160. #ifdef TEST
  161. #include <stdio.h>
  162. #if HAVE_UNISTD_H
  163. #include <unistd.h>
  164. #endif
  165. #include "avstring.h"
  166. #if !HAVE_GETOPT
  167. #include "compat/getopt.c"
  168. #endif
  169. static const struct {
  170. int flag;
  171. const char *name;
  172. } cpu_flag_tab[] = {
  173. #if ARCH_AARCH64
  174. { AV_CPU_FLAG_ARMV8, "armv8" },
  175. { AV_CPU_FLAG_NEON, "neon" },
  176. { AV_CPU_FLAG_VFP, "vfp" },
  177. #elif ARCH_ARM
  178. { AV_CPU_FLAG_ARMV5TE, "armv5te" },
  179. { AV_CPU_FLAG_ARMV6, "armv6" },
  180. { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
  181. { AV_CPU_FLAG_VFP, "vfp" },
  182. { AV_CPU_FLAG_VFPV3, "vfpv3" },
  183. { AV_CPU_FLAG_NEON, "neon" },
  184. #elif ARCH_PPC
  185. { AV_CPU_FLAG_ALTIVEC, "altivec" },
  186. #elif ARCH_X86
  187. { AV_CPU_FLAG_MMX, "mmx" },
  188. { AV_CPU_FLAG_MMXEXT, "mmxext" },
  189. { AV_CPU_FLAG_SSE, "sse" },
  190. { AV_CPU_FLAG_SSE2, "sse2" },
  191. { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
  192. { AV_CPU_FLAG_SSE3, "sse3" },
  193. { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
  194. { AV_CPU_FLAG_SSSE3, "ssse3" },
  195. { AV_CPU_FLAG_ATOM, "atom" },
  196. { AV_CPU_FLAG_SSE4, "sse4.1" },
  197. { AV_CPU_FLAG_SSE42, "sse4.2" },
  198. { AV_CPU_FLAG_AVX, "avx" },
  199. { AV_CPU_FLAG_XOP, "xop" },
  200. { AV_CPU_FLAG_FMA3, "fma3" },
  201. { AV_CPU_FLAG_FMA4, "fma4" },
  202. { AV_CPU_FLAG_3DNOW, "3dnow" },
  203. { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
  204. { AV_CPU_FLAG_CMOV, "cmov" },
  205. { AV_CPU_FLAG_AVX2, "avx2" },
  206. { AV_CPU_FLAG_BMI1, "bmi1" },
  207. { AV_CPU_FLAG_BMI2, "bmi2" },
  208. #endif
  209. { 0 }
  210. };
  211. static void print_cpu_flags(int cpu_flags, const char *type)
  212. {
  213. int i;
  214. fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
  215. fprintf(stderr, "cpu_flags_str(%s) =", type);
  216. for (i = 0; cpu_flag_tab[i].flag; i++)
  217. if (cpu_flags & cpu_flag_tab[i].flag)
  218. fprintf(stderr, " %s", cpu_flag_tab[i].name);
  219. fprintf(stderr, "\n");
  220. }
  221. int main(int argc, char **argv)
  222. {
  223. int cpu_flags_raw = av_get_cpu_flags();
  224. int cpu_flags_eff;
  225. int cpu_count = av_cpu_count();
  226. char threads[5] = "auto";
  227. if (cpu_flags_raw < 0)
  228. return 1;
  229. for (;;) {
  230. int c = getopt(argc, argv, "c:t:");
  231. if (c == -1)
  232. break;
  233. switch (c) {
  234. case 'c':
  235. {
  236. int cpuflags = av_parse_cpu_flags(optarg);
  237. if (cpuflags < 0)
  238. return 2;
  239. av_set_cpu_flags_mask(cpuflags);
  240. break;
  241. }
  242. case 't':
  243. {
  244. int len = av_strlcpy(threads, optarg, sizeof(threads));
  245. if (len >= sizeof(threads)) {
  246. fprintf(stderr, "Invalid thread count '%s'\n", optarg);
  247. return 2;
  248. }
  249. }
  250. }
  251. }
  252. cpu_flags_eff = av_get_cpu_flags();
  253. if (cpu_flags_eff < 0)
  254. return 3;
  255. print_cpu_flags(cpu_flags_raw, "raw");
  256. print_cpu_flags(cpu_flags_eff, "effective");
  257. fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count);
  258. return 0;
  259. }
  260. #endif