1 /*
2  * Copyright © 2018, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include "config.h"
28 
29 #include <stdint.h>
30 
31 #include "src/cpu.h"
32 #include "src/log.h"
33 
34 #ifdef _WIN32
35 #include <windows.h>
36 #elif defined(__APPLE__)
37 #include <sys/sysctl.h>
38 #include <sys/types.h>
39 #else
40 #include <pthread.h>
41 #include <unistd.h>
42 #endif
43 
44 #ifdef HAVE_PTHREAD_NP_H
45 #include <pthread_np.h>
46 #endif
47 #if defined(__FreeBSD__)
48 #define cpu_set_t cpuset_t
49 #endif
50 
51 static unsigned flags = 0;
52 static unsigned flags_mask = -1;
53 
dav1d_init_cpu(void)54 COLD void dav1d_init_cpu(void) {
55 #if HAVE_ASM && !__has_feature(memory_sanitizer)
56 // memory sanitizer is inherently incompatible with asm
57 #if ARCH_AARCH64 || ARCH_ARM
58     flags = dav1d_get_cpu_flags_arm();
59 #elif ARCH_PPC64LE
60     flags = dav1d_get_cpu_flags_ppc();
61 #elif ARCH_X86
62     flags = dav1d_get_cpu_flags_x86();
63 #endif
64 #endif
65 }
66 
dav1d_get_cpu_flags(void)67 COLD unsigned dav1d_get_cpu_flags(void) {
68     return flags & flags_mask;
69 }
70 
dav1d_set_cpu_flags_mask(const unsigned mask)71 COLD void dav1d_set_cpu_flags_mask(const unsigned mask) {
72     flags_mask = mask;
73 }
74 
dav1d_num_logical_processors(Dav1dContext * const c)75 COLD int dav1d_num_logical_processors(Dav1dContext *const c) {
76 #ifdef _WIN32
77 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
78     GROUP_AFFINITY affinity;
79     if (GetThreadGroupAffinity(GetCurrentThread(), &affinity)) {
80         int num_processors = 1;
81         while (affinity.Mask &= affinity.Mask - 1)
82             num_processors++;
83         return num_processors;
84     }
85 #else
86     SYSTEM_INFO system_info;
87     GetNativeSystemInfo(&system_info);
88     return system_info.dwNumberOfProcessors;
89 #endif
90 #elif defined(HAVE_PTHREAD_GETAFFINITY_NP) && defined(CPU_COUNT)
91     cpu_set_t affinity;
92     if (!pthread_getaffinity_np(pthread_self(), sizeof(affinity), &affinity))
93         return CPU_COUNT(&affinity);
94 #elif defined(__APPLE__)
95     int num_processors;
96     size_t length = sizeof(num_processors);
97     if (!sysctlbyname("hw.logicalcpu", &num_processors, &length, NULL, 0))
98         return num_processors;
99 #elif defined(_SC_NPROCESSORS_ONLN)
100     return (int)sysconf(_SC_NPROCESSORS_ONLN);
101 #endif
102     dav1d_log(c, "Unable to detect thread count, defaulting to single-threaded mode\n");
103     return 1;
104 }
105