blob: e07d4151bb659c4abdce6906d0ed7c884c0b02be [file] [log] [blame]
Teresa Johnson7943fec2016-10-13 17:43:20 +00001//========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/Host.h"
11#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");
Yi Kong57019dc2017-04-04 19:06:04 +0000173}
Alex Lorenz3803df32017-07-07 09:53:47 +0000174
175#if defined(__APPLE__)
176TEST_F(HostTest, getMacOSHostVersion) {
177 using namespace llvm::sys;
178 llvm::Triple HostTriple(getProcessTriple());
179 if (!HostTriple.isMacOSX())
180 return;
181
182 SmallString<128> TestDirectory;
183 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
184 SmallString<128> OutputFile(TestDirectory);
185 path::append(OutputFile, "out");
186
187 const char *SwVersPath = "/usr/bin/sw_vers";
Zachary Turner08426e12018-06-12 17:43:52 +0000188 StringRef argv[] = {SwVersPath, "-productVersion"};
Alex Lorenz3803df32017-07-07 09:53:47 +0000189 StringRef OutputPath = OutputFile.str();
Vedant Kumarbc26f722017-09-13 18:00:22 +0000190 const Optional<StringRef> Redirects[] = {/*STDIN=*/None,
191 /*STDOUT=*/OutputPath,
192 /*STDERR=*/None};
Zachary Turner08426e12018-06-12 17:43:52 +0000193 int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/llvm::None, Redirects);
Alex Lorenz3803df32017-07-07 09:53:47 +0000194 ASSERT_EQ(0, RetCode);
195
196 int FD = 0;
197 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
198 off_t Size = ::lseek(FD, 0, SEEK_END);
199 ASSERT_NE(-1, Size);
200 ::lseek(FD, 0, SEEK_SET);
201 std::unique_ptr<char[]> Buffer = llvm::make_unique<char[]>(Size);
202 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
203 ::close(FD);
204
205 // Ensure that the two versions match.
206 StringRef SystemVersion(Buffer.get(), Size);
207 unsigned SystemMajor, SystemMinor, SystemMicro;
208 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
209 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
210 true);
211 unsigned HostMajor, HostMinor, HostMicro;
212 ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
213
214 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
215 // triples.
216 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
217
218 ASSERT_NO_ERROR(fs::remove(OutputPath));
219 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
220}
221#endif