1 //========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Host.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Program.h"
17 #include "gtest/gtest.h"
19 #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()); \
32 class HostTest
: public testing::Test
{
36 bool isSupportedArchAndOS() {
37 // Initially this is only testing detection of the number of
38 // physical cores, which is currently only supported/tested for
39 // x86_64 Linux and Darwin.
40 return (Host
.getArch() == Triple::x86_64
&&
41 (Host
.isOSDarwin() || Host
.getOS() == Triple::Linux
));
44 HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
47 TEST_F(HostTest
, NumPhysicalCores
) {
48 int Num
= sys::getHostNumPhysicalCores();
50 if (isSupportedArchAndOS())
56 TEST(getLinuxHostCPUName
, ARM
) {
57 StringRef CortexA9ProcCpuinfo
= R
"(
59 model name : ARMv7 Processor rev 10 (v7l)
61 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
62 CPU implementer : 0x41
69 model name : ARMv7 Processor rev 10 (v7l)
71 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
72 CPU implementer : 0x41
78 Hardware : Generic OMAP4 (Flattened Device Tree)
80 Serial : 0000000000000000
83 EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo
),
85 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
88 // Verify that both CPU implementer and CPU part are checked:
89 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
92 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
97 TEST(getLinuxHostCPUName
, AArch64
) {
98 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
101 // Verify that both CPU implementer and CPU part are checked:
102 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
105 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
108 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
111 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
114 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
117 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
121 // MSM8992/4 weirdness
122 StringRef MSM8992ProcCpuInfo
= R
"(
123 Processor : AArch64 Processor rev 3 (aarch64)
130 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
131 CPU implementer : 0x41
137 Hardware : Qualcomm Technologies, Inc MSM8992
140 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo
),
143 // Exynos big.LITTLE weirdness
144 const std::string ExynosProcCpuInfo
= R
"(
146 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
147 CPU implementer : 0x41
153 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
154 CPU implementer : 0x53
158 // Verify default for Exynos.
159 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo
+
160 "CPU variant : 0xc\n"
164 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo
+
165 "CPU variant : 0x1\n"
169 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo
+
170 "CPU variant : 0x4\n"
174 const std::string ThunderX2T99ProcCpuInfo
= R
"(
177 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
178 CPU implementer : 0x43
184 // Verify different versions of ThunderX2T99.
185 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
186 "CPU implementer : 0x42\n"
190 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
191 "CPU implementer : 0x42\n"
192 "CPU part : 0x0516"),
195 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
196 "CPU implementer : 0x43\n"
200 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
201 "CPU implementer : 0x43\n"
202 "CPU part : 0x0516"),
205 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
206 "CPU implementer : 0x42\n"
210 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
211 "CPU implementer : 0x42\n"
215 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
216 "CPU implementer : 0x43\n"
220 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo
+
221 "CPU implementer : 0x43\n"
225 // Verify ThunderXT88.
226 const std::string ThunderXT88ProcCpuInfo
= R
"(
229 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
230 CPU implementer : 0x43
236 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo
+
237 "CPU implementer : 0x43\n"
241 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo
+
242 "CPU implementer : 0x43\n"
247 #if defined(__APPLE__)
248 TEST_F(HostTest
, getMacOSHostVersion
) {
249 using namespace llvm::sys
;
250 llvm::Triple
HostTriple(getProcessTriple());
251 if (!HostTriple
.isMacOSX())
254 SmallString
<128> TestDirectory
;
255 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory
));
256 SmallString
<128> OutputFile(TestDirectory
);
257 path::append(OutputFile
, "out");
259 const char *SwVersPath
= "/usr/bin/sw_vers";
260 StringRef argv
[] = {SwVersPath
, "-productVersion"};
261 StringRef OutputPath
= OutputFile
.str();
262 const Optional
<StringRef
> Redirects
[] = {/*STDIN=*/None
,
263 /*STDOUT=*/OutputPath
,
265 int RetCode
= ExecuteAndWait(SwVersPath
, argv
, /*env=*/llvm::None
, Redirects
);
266 ASSERT_EQ(0, RetCode
);
269 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath
, FD
));
270 off_t Size
= ::lseek(FD
, 0, SEEK_END
);
272 ::lseek(FD
, 0, SEEK_SET
);
273 std::unique_ptr
<char[]> Buffer
= llvm::make_unique
<char[]>(Size
);
274 ASSERT_EQ(::read(FD
, Buffer
.get(), Size
), Size
);
277 // Ensure that the two versions match.
278 StringRef
SystemVersion(Buffer
.get(), Size
);
279 unsigned SystemMajor
, SystemMinor
, SystemMicro
;
280 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion
))
281 .getMacOSXVersion(SystemMajor
, SystemMinor
, SystemMicro
),
283 unsigned HostMajor
, HostMinor
, HostMicro
;
284 ASSERT_EQ(HostTriple
.getMacOSXVersion(HostMajor
, HostMinor
, HostMicro
), true);
286 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
288 ASSERT_EQ(std::tie(SystemMajor
, SystemMinor
), std::tie(HostMajor
, HostMinor
));
290 ASSERT_NO_ERROR(fs::remove(OutputPath
));
291 ASSERT_NO_ERROR(fs::remove(TestDirectory
.str()));