/src/highwayhash/highwayhash/os_specific.cc
Line | Count | Source |
1 | | // Copyright 2017 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "highwayhash/os_specific.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <cstdio> |
21 | | #include <cstdlib> |
22 | | #include <ctime> |
23 | | #include <random> |
24 | | |
25 | | #include "highwayhash/arch_specific.h" |
26 | | |
27 | | #if defined(_WIN32) || defined(_WIN64) |
28 | | #define OS_WIN 1 |
29 | | #define NOMINMAX |
30 | | #include <windows.h> |
31 | | #else |
32 | | #define OS_WIN 0 |
33 | | #endif |
34 | | |
35 | | #ifdef __linux__ |
36 | | #define OS_LINUX 1 |
37 | | #include <sched.h> |
38 | | #include <sys/time.h> |
39 | | #else |
40 | | #define OS_LINUX 0 |
41 | | #endif |
42 | | |
43 | | #if defined(__APPLE__) && \ |
44 | | defined(__MACH__) // __MACH__ also defined for GNU/Hurd |
45 | | #define OS_MAC 1 |
46 | | #include <mach/mach.h> |
47 | | #include <mach/mach_time.h> |
48 | | // |
49 | | #include "highwayhash/os_mac.cc" |
50 | | #else |
51 | | #define OS_MAC 0 |
52 | | #endif |
53 | | |
54 | | #ifdef __FreeBSD__ |
55 | | #define OS_FREEBSD 1 |
56 | | // clang-format off |
57 | | #include <sys/param.h> |
58 | | #include <sys/cpuset.h> /* must come after sys/param.h */ |
59 | | // clang-format on |
60 | | #include <unistd.h> |
61 | | #else |
62 | | #define OS_FREEBSD 0 |
63 | | #endif |
64 | | |
65 | | namespace highwayhash { |
66 | | |
67 | | #define CHECK(condition) \ |
68 | 0 | while (!(condition)) { \ |
69 | 0 | printf("os_specific CHECK failed at line %d\n", __LINE__); \ |
70 | 0 | abort(); \ |
71 | 0 | } |
72 | | |
73 | 0 | double Now() { |
74 | | #if OS_WIN |
75 | | LARGE_INTEGER counter; |
76 | | (void)QueryPerformanceCounter(&counter); |
77 | | static const double rcp_freq = []() { |
78 | | LARGE_INTEGER freq; |
79 | | (void)QueryPerformanceFrequency(&freq); |
80 | | return 1.0 / freq.QuadPart; |
81 | | }(); |
82 | | return counter.QuadPart * rcp_freq; |
83 | | #elif OS_MAC |
84 | | const auto t = mach_absolute_time(); |
85 | | // On OSX/iOS platform the elapsed time is cpu time unit |
86 | | // We have to query the time base information to convert it back |
87 | | // See https://developer.apple.com/library/mac/qa/qa1398/_index.html |
88 | | static mach_timebase_info_data_t timebase; |
89 | | if (timebase.denom == 0) { |
90 | | (void)mach_timebase_info(&timebase); |
91 | | } |
92 | | return double(t) * timebase.numer / timebase.denom * 1E-9; |
93 | | #else |
94 | 0 | timespec t; |
95 | 0 | clock_gettime(CLOCK_REALTIME, &t); |
96 | 0 | return t.tv_sec + t.tv_nsec * 1E-9; |
97 | 0 | #endif |
98 | 0 | } |
99 | | |
100 | 0 | void RaiseThreadPriority() { |
101 | | #if OS_WIN |
102 | | BOOL ok = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |
103 | | CHECK(ok); |
104 | | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); |
105 | | CHECK(ok); |
106 | | #elif OS_LINUX |
107 | | // omit: SCHED_RR and SCHED_FIFO with sched_priority max, max-1 and max/2 |
108 | | // lead to 2-3x runtime and higher variability! |
109 | | #elif OS_FREEBSD || OS_MAC |
110 | | #else |
111 | | #error "port" |
112 | | #endif |
113 | 0 | } |
114 | | |
115 | | struct ThreadAffinity { |
116 | | #if OS_WIN |
117 | | DWORD_PTR mask; |
118 | | #elif OS_LINUX || OS_MAC |
119 | | cpu_set_t set; |
120 | | #elif OS_FREEBSD |
121 | | cpuset_t set; |
122 | | #endif |
123 | | }; |
124 | | |
125 | 0 | ThreadAffinity* GetThreadAffinity() { |
126 | 0 | ThreadAffinity* affinity = |
127 | 0 | static_cast<ThreadAffinity*>(malloc(sizeof(ThreadAffinity))); |
128 | | #if OS_WIN |
129 | | DWORD_PTR system_affinity; |
130 | | const BOOL ok = GetProcessAffinityMask(GetCurrentProcess(), &affinity->mask, |
131 | | &system_affinity); |
132 | | CHECK(ok); |
133 | | #elif OS_LINUX |
134 | | const pid_t pid = 0; // current thread |
135 | 0 | const int err = sched_getaffinity(pid, sizeof(cpu_set_t), &affinity->set); |
136 | 0 | CHECK(err == 0); |
137 | | #elif OS_FREEBSD |
138 | | const pid_t pid = getpid(); // current thread |
139 | | const int err = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, |
140 | | sizeof(cpuset_t), &affinity->set); |
141 | | CHECK(err == 0); |
142 | | #elif OS_MAC |
143 | | const int err = mac_getaffinity(&affinity->set); |
144 | | CHECK(err == 0); |
145 | | #endif |
146 | 0 | return affinity; |
147 | 0 | } |
148 | | |
149 | | namespace { |
150 | | |
151 | 0 | ThreadAffinity* OriginalThreadAffinity() { |
152 | 0 | static ThreadAffinity* original = GetThreadAffinity(); |
153 | 0 | return original; |
154 | 0 | } |
155 | | |
156 | | } // namespace |
157 | | |
158 | 0 | void SetThreadAffinity(ThreadAffinity* affinity) { |
159 | | // Ensure original is initialized before changing. |
160 | 0 | const ThreadAffinity* const original = OriginalThreadAffinity(); |
161 | 0 | CHECK(original != nullptr); |
162 | |
|
163 | | #if OS_WIN |
164 | | const HANDLE hThread = GetCurrentThread(); |
165 | | const DWORD_PTR prev = SetThreadAffinityMask(hThread, affinity->mask); |
166 | | CHECK(prev != 0); |
167 | | #elif OS_LINUX |
168 | | const pid_t pid = 0; // current thread |
169 | 0 | const int err = sched_setaffinity(pid, sizeof(cpu_set_t), &affinity->set); |
170 | 0 | CHECK(err == 0); |
171 | | #elif OS_FREEBSD |
172 | | const pid_t pid = getpid(); // current thread |
173 | | const int err = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, |
174 | | sizeof(cpuset_t), &affinity->set); |
175 | | CHECK(err == 0); |
176 | | #elif OS_MAC |
177 | | const int err = mac_setaffinity(&affinity->set); |
178 | | CHECK(err == 0); |
179 | | #else |
180 | | #error "port" |
181 | | #endif |
182 | 0 | } |
183 | | |
184 | 0 | std::vector<int> AvailableCPUs() { |
185 | 0 | std::vector<int> cpus; |
186 | 0 | cpus.reserve(64); |
187 | 0 | const ThreadAffinity* const affinity = OriginalThreadAffinity(); |
188 | | #if OS_WIN |
189 | | for (int cpu = 0; cpu < 64; ++cpu) { |
190 | | if (affinity->mask & (1ULL << cpu)) { |
191 | | cpus.push_back(cpu); |
192 | | } |
193 | | } |
194 | | #elif OS_LINUX |
195 | 0 | for (size_t cpu = 0; cpu < sizeof(cpu_set_t) * 8; ++cpu) { |
196 | 0 | if (CPU_ISSET(cpu, &affinity->set)) { |
197 | 0 | cpus.push_back(cpu); |
198 | 0 | } |
199 | 0 | } |
200 | | #elif OS_FREEBSD |
201 | | for (size_t cpu = 0; cpu < sizeof(cpuset_t) * 8; ++cpu) { |
202 | | if (CPU_ISSET(cpu, &affinity->set)) { |
203 | | cpus.push_back(cpu); |
204 | | } |
205 | | } |
206 | | #elif OS_MAC |
207 | | for (int cpu = 0; cpu < sizeof(cpu_set_t) * 8; ++cpu) { |
208 | | if (CPU_ISSET(cpu, &affinity->set)) { |
209 | | cpus.push_back(cpu); |
210 | | } |
211 | | } |
212 | | #else |
213 | | #error "port" |
214 | | #endif |
215 | 0 | return cpus; |
216 | 0 | } |
217 | | |
218 | 0 | void PinThreadToCPU(const int cpu) { |
219 | 0 | ThreadAffinity affinity; |
220 | | #if OS_WIN |
221 | | affinity.mask = 1ULL << cpu; |
222 | | #elif OS_LINUX |
223 | 0 | CPU_ZERO(&affinity.set); |
224 | 0 | CPU_SET(cpu, &affinity.set); |
225 | | #elif OS_FREEBSD |
226 | | CPU_ZERO(&affinity.set); |
227 | | CPU_SET(cpu, &affinity.set); |
228 | | #elif OS_MAC |
229 | | CPU_ZERO(&affinity.set); |
230 | | CPU_SET(cpu, &affinity.set); |
231 | | #else |
232 | | #error "port" |
233 | | #endif |
234 | 0 | SetThreadAffinity(&affinity); |
235 | 0 | } |
236 | | |
237 | 0 | void PinThreadToRandomCPU() { |
238 | 0 | std::vector<int> cpus = AvailableCPUs(); |
239 | | |
240 | | // Remove first two CPUs because interrupts are often pinned to them. |
241 | 0 | CHECK(cpus.size() > 2); |
242 | 0 | cpus.erase(cpus.begin(), cpus.begin() + 2); |
243 | | |
244 | | // Random choice to prevent burning up the same core. |
245 | 0 | std::random_device device; |
246 | 0 | std::ranlux48 generator(device()); |
247 | 0 | std::shuffle(cpus.begin(), cpus.end(), generator); |
248 | 0 | const int cpu = cpus.front(); |
249 | |
|
250 | 0 | PinThreadToCPU(cpu); |
251 | |
|
252 | 0 | #if HH_ARCH_X64 |
253 | | // After setting affinity, we should be running on the desired CPU. |
254 | 0 | printf("Running on CPU #%d, APIC ID %02x\n", cpu, ApicId()); |
255 | | #else |
256 | | printf("Running on CPU #%d\n", cpu); |
257 | | #endif |
258 | 0 | } |
259 | | |
260 | | } // namespace highwayhash |