blob: 89080f350aaf063530f2a7e94bbf0dde051c1274 [file] [log] [blame]
Teresa Johnson7943fec2016-10-13 17:43:20 +00001//========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Teresa Johnson7943fec2016-10-13 17:43:20 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/Host.h"
Hubert Tong72db2ab2019-03-13 00:12:43 +000010#include "llvm/Config/llvm-config.h"
Teresa Johnson7943fec2016-10-13 17:43:20 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/Triple.h"
Alex Lorenz3803df32017-07-07 09:53:47 +000013#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/Path.h"
15#include "llvm/Support/Program.h"
Teresa Johnson7943fec2016-10-13 17:43:20 +000016
17#include "gtest/gtest.h"
18
Alex Lorenz3803df32017-07-07 09:53:47 +000019#define ASSERT_NO_ERROR(x) \
20 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
21 SmallString<128> MessageStorage; \
22 raw_svector_ostream Message(MessageStorage); \
23 Message << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
27 } else { \
28 }
29
Teresa Johnson7943fec2016-10-13 17:43:20 +000030using namespace llvm;
31
32class HostTest : public testing::Test {
33 Triple Host;
Teresa Johnson7943fec2016-10-13 17:43:20 +000034
35protected:
36 bool isSupportedArchAndOS() {
Teresa Johnson7943fec2016-10-13 17:43:20 +000037 // Initially this is only testing detection of the number of
Mehdi Aminidb46b7d2016-10-19 22:36:07 +000038 // physical cores, which is currently only supported/tested for
39 // x86_64 Linux and Darwin.
Ahmed Bougachaf0fe1a82017-02-04 00:46:59 +000040 return (Host.getArch() == Triple::x86_64 &&
41 (Host.isOSDarwin() || Host.getOS() == Triple::Linux));
Teresa Johnson7943fec2016-10-13 17:43:20 +000042 }
Ahmed Bougachaf0fe1a82017-02-04 00:46:59 +000043
44 HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
Teresa Johnson7943fec2016-10-13 17:43:20 +000045};
46
47TEST_F(HostTest, NumPhysicalCores) {
48 int Num = sys::getHostNumPhysicalCores();
49
50 if (isSupportedArchAndOS())
51 ASSERT_GT(Num, 0);
52 else
53 ASSERT_EQ(Num, -1);
54}
Kristof Beyls9e463962017-03-30 07:24:49 +000055
56TEST(getLinuxHostCPUName, ARM) {
57 StringRef CortexA9ProcCpuinfo = R"(
58processor : 0
59model name : ARMv7 Processor rev 10 (v7l)
60BogoMIPS : 1393.66
61Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
62CPU implementer : 0x41
63CPU architecture: 7
64CPU variant : 0x2
65CPU part : 0xc09
66CPU revision : 10
67
68processor : 1
69model name : ARMv7 Processor rev 10 (v7l)
70BogoMIPS : 1393.66
71Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
72CPU implementer : 0x41
73CPU architecture: 7
74CPU variant : 0x2
75CPU part : 0xc09
76CPU revision : 10
77
78Hardware : Generic OMAP4 (Flattened Device Tree)
79Revision : 0000
80Serial : 0000000000000000
81)";
82
Kristof Beyls77ce4f62017-03-31 13:06:40 +000083 EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
Kristof Beyls9e463962017-03-30 07:24:49 +000084 "cortex-a9");
Kristof Beyls77ce4f62017-03-31 13:06:40 +000085 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
86 "CPU part : 0xc0f"),
87 "cortex-a15");
Kristof Beyls9e463962017-03-30 07:24:49 +000088 // Verify that both CPU implementer and CPU part are checked:
Kristof Beyls77ce4f62017-03-31 13:06:40 +000089 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
90 "CPU part : 0xc0f"),
91 "generic");
92 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
93 "CPU part : 0x06f"),
94 "krait");
Kristof Beyls9e463962017-03-30 07:24:49 +000095}
Yi Kong57019dc2017-04-04 19:06:04 +000096
97TEST(getLinuxHostCPUName, AArch64) {
98 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
99 "CPU part : 0xd03"),
100 "cortex-a53");
101 // Verify that both CPU implementer and CPU part are checked:
102 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
103 "CPU part : 0xd03"),
104 "generic");
105 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
106 "CPU part : 0x201"),
107 "kryo");
Eli Friedmanbde9fc72017-09-13 21:48:00 +0000108 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
109 "CPU part : 0x800"),
110 "cortex-a73");
111 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
112 "CPU part : 0x801"),
113 "cortex-a73");
Balaram Makama1e7ecc72017-09-22 17:46:36 +0000114 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
115 "CPU part : 0xc00"),
116 "falkor");
Chad Rosier71070852017-09-25 14:05:00 +0000117 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
118 "CPU part : 0xc01"),
119 "saphira");
Yi Kong57019dc2017-04-04 19:06:04 +0000120
121 // MSM8992/4 weirdness
122 StringRef MSM8992ProcCpuInfo = R"(
123Processor : AArch64 Processor rev 3 (aarch64)
124processor : 0
125processor : 1
126processor : 2
127processor : 3
128processor : 4
129processor : 5
130Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
131CPU implementer : 0x41
132CPU architecture: 8
133CPU variant : 0x0
134CPU part : 0xd03
135CPU revision : 3
136
137Hardware : Qualcomm Technologies, Inc MSM8992
138)";
139
140 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
141 "cortex-a53");
Evandro Menezes5d7a9e62017-12-08 21:09:59 +0000142
143 // Exynos big.LITTLE weirdness
144 const std::string ExynosProcCpuInfo = R"(
145processor : 0
146Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
147CPU implementer : 0x41
148CPU architecture: 8
149CPU variant : 0x0
150CPU part : 0xd03
151
152processor : 1
153Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
154CPU implementer : 0x53
155CPU architecture: 8
156)";
157
158 // Verify default for Exynos.
159 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
160 "CPU variant : 0xc\n"
161 "CPU part : 0xafe"),
162 "exynos-m1");
163 // Verify Exynos M1.
164 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
165 "CPU variant : 0x1\n"
166 "CPU part : 0x001"),
167 "exynos-m1");
168 // Verify Exynos M2.
169 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
170 "CPU variant : 0x4\n"
171 "CPU part : 0x001"),
172 "exynos-m2");
Joel Jones0a6c0002018-10-05 22:23:21 +0000173
174 const std::string ThunderX2T99ProcCpuInfo = R"(
175processor : 0
176BogoMIPS : 400.00
177Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
178CPU implementer : 0x43
179CPU architecture: 8
180CPU variant : 0x1
181CPU part : 0x0af
182)";
183
184 // Verify different versions of ThunderX2T99.
185 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
186 "CPU implementer : 0x42\n"
187 "CPU part : 0x516"),
188 "thunderx2t99");
189
190 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
191 "CPU implementer : 0x42\n"
192 "CPU part : 0x0516"),
193 "thunderx2t99");
194
195 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
196 "CPU implementer : 0x43\n"
197 "CPU part : 0x516"),
198 "thunderx2t99");
199
200 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
201 "CPU implementer : 0x43\n"
202 "CPU part : 0x0516"),
203 "thunderx2t99");
204
205 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
206 "CPU implementer : 0x42\n"
207 "CPU part : 0xaf"),
208 "thunderx2t99");
209
210 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
211 "CPU implementer : 0x42\n"
212 "CPU part : 0x0af"),
213 "thunderx2t99");
214
215 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
216 "CPU implementer : 0x43\n"
217 "CPU part : 0xaf"),
218 "thunderx2t99");
219
220 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
221 "CPU implementer : 0x43\n"
222 "CPU part : 0x0af"),
223 "thunderx2t99");
224
225 // Verify ThunderXT88.
226 const std::string ThunderXT88ProcCpuInfo = R"(
227processor : 0
228BogoMIPS : 200.00
229Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
230CPU implementer : 0x43
231CPU architecture: 8
232CPU variant : 0x1
233CPU part : 0x0a1
234)";
235
236 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
237 "CPU implementer : 0x43\n"
238 "CPU part : 0x0a1"),
239 "thunderxt88");
240
241 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
242 "CPU implementer : 0x43\n"
243 "CPU part : 0xa1"),
244 "thunderxt88");
Bryan Chan12355392018-11-09 19:32:08 +0000245
246 // Verify HiSilicon processors.
247 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x48\n"
248 "CPU part : 0xd01"),
249 "tsv110");
Yi Kong57019dc2017-04-04 19:06:04 +0000250}
Alex Lorenz3803df32017-07-07 09:53:47 +0000251
Hubert Tong72db2ab2019-03-13 00:12:43 +0000252static bool runAndGetCommandOutput(
253 const char *ExePath, ArrayRef<llvm::StringRef> argv,
254 std::unique_ptr<char[]> &Buffer, off_t &Size) {
255 bool Success = false;
256 [ExePath, argv, &Buffer, &Size, &Success] {
257 using namespace llvm::sys;
258 SmallString<128> TestDirectory;
259 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
260
261 SmallString<128> OutputFile(TestDirectory);
262 path::append(OutputFile, "out");
263 StringRef OutputPath = OutputFile.str();
264
265 const Optional<StringRef> Redirects[] = {
266 /*STDIN=*/None, /*STDOUT=*/OutputPath, /*STDERR=*/None};
267 int RetCode = ExecuteAndWait(ExePath, argv, /*env=*/llvm::None, Redirects);
268 ASSERT_EQ(0, RetCode);
269
270 int FD = 0;
271 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
272 Size = ::lseek(FD, 0, SEEK_END);
273 ASSERT_NE(-1, Size);
274 ::lseek(FD, 0, SEEK_SET);
275 Buffer = llvm::make_unique<char[]>(Size);
276 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
277 ::close(FD);
278
279 ASSERT_NO_ERROR(fs::remove(OutputPath));
280 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
281 Success = true;
282 }();
283 return Success;
284}
285
286TEST_F(HostTest, DummyRunAndGetCommandOutputUse) {
287 // Suppress defined-but-not-used warnings when the tests using the helper are
288 // disabled.
289 (void) runAndGetCommandOutput;
290}
291
Alex Lorenz3803df32017-07-07 09:53:47 +0000292#if defined(__APPLE__)
293TEST_F(HostTest, getMacOSHostVersion) {
294 using namespace llvm::sys;
295 llvm::Triple HostTriple(getProcessTriple());
296 if (!HostTriple.isMacOSX())
297 return;
298
Alex Lorenz3803df32017-07-07 09:53:47 +0000299 const char *SwVersPath = "/usr/bin/sw_vers";
Zachary Turner08426e12018-06-12 17:43:52 +0000300 StringRef argv[] = {SwVersPath, "-productVersion"};
Hubert Tong72db2ab2019-03-13 00:12:43 +0000301 std::unique_ptr<char[]> Buffer;
302 off_t Size;
303 ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true);
304 StringRef SystemVersion(Buffer.get(), Size);
Alex Lorenz3803df32017-07-07 09:53:47 +0000305
306 // Ensure that the two versions match.
Alex Lorenz3803df32017-07-07 09:53:47 +0000307 unsigned SystemMajor, SystemMinor, SystemMicro;
308 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
309 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
310 true);
311 unsigned HostMajor, HostMinor, HostMicro;
312 ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
313
314 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
315 // triples.
316 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
Hubert Tong72db2ab2019-03-13 00:12:43 +0000317}
318#endif
Alex Lorenz3803df32017-07-07 09:53:47 +0000319
Hubert Tong72db2ab2019-03-13 00:12:43 +0000320#if defined(_AIX)
321TEST_F(HostTest, AIXVersionDetect) {
322 using namespace llvm::sys;
323
324 llvm::Triple HostTriple(getProcessTriple());
325 ASSERT_EQ(HostTriple.getOS(), Triple::AIX);
326
327 llvm::Triple ConfiguredHostTriple(LLVM_HOST_TRIPLE);
328 ASSERT_EQ(ConfiguredHostTriple.getOS(), Triple::AIX);
329
330 const char *ExePath = "/usr/bin/oslevel";
331 StringRef argv[] = {ExePath};
332 std::unique_ptr<char[]> Buffer;
333 off_t Size;
334 ASSERT_EQ(runAndGetCommandOutput(ExePath, argv, Buffer, Size), true);
335 StringRef SystemVersion(Buffer.get(), Size);
336
337 unsigned SystemMajor, SystemMinor, SystemMicro;
338 llvm::Triple((Twine("powerpc-ibm-aix") + SystemVersion))
339 .getOSVersion(SystemMajor, SystemMinor, SystemMicro);
340
341 // Ensure that the host triple version (major) and release (minor) numbers,
342 // unless explicitly configured, match with those of the current system.
343 if (!ConfiguredHostTriple.getOSMajorVersion()) {
344 unsigned HostMajor, HostMinor, HostMicro;
345 HostTriple.getOSVersion(HostMajor, HostMinor, HostMicro);
346 ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
347 std::tie(HostMajor, HostMinor));
348 }
349
350 llvm::Triple TargetTriple(getDefaultTargetTriple());
351 if (TargetTriple.getOS() != Triple::AIX)
352 return;
353
354 // Ensure that the target triple version (major) and release (minor) numbers
355 // match with those of the current system.
356 llvm::Triple ConfiguredTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE);
357 if (ConfiguredTargetTriple.getOSMajorVersion())
358 return; // The version was configured explicitly; skip.
359
360 unsigned TargetMajor, TargetMinor, TargetMicro;
361 TargetTriple.getOSVersion(TargetMajor, TargetMinor, TargetMicro);
362 ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
363 std::tie(TargetMajor, TargetMinor));
Alex Lorenz3803df32017-07-07 09:53:47 +0000364}
365#endif