• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========- 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"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Program.h"
16 
17 #include "gtest/gtest.h"
18 
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());                              \
27   } else {                                                                     \
28   }
29 
30 using namespace llvm;
31 
32 class HostTest : public testing::Test {
33   Triple Host;
34 
35 protected:
isSupportedArchAndOS()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));
42   }
43 
HostTest()44   HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
45 };
46 
TEST_F(HostTest,NumPhysicalCores)47 TEST_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 }
55 
TEST(getLinuxHostCPUName,ARM)56 TEST(getLinuxHostCPUName, ARM) {
57   StringRef CortexA9ProcCpuinfo = R"(
58 processor       : 0
59 model name      : ARMv7 Processor rev 10 (v7l)
60 BogoMIPS        : 1393.66
61 Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
62 CPU implementer : 0x41
63 CPU architecture: 7
64 CPU variant     : 0x2
65 CPU part        : 0xc09
66 CPU revision    : 10
67 
68 processor       : 1
69 model name      : ARMv7 Processor rev 10 (v7l)
70 BogoMIPS        : 1393.66
71 Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
72 CPU implementer : 0x41
73 CPU architecture: 7
74 CPU variant     : 0x2
75 CPU part        : 0xc09
76 CPU revision    : 10
77 
78 Hardware        : Generic OMAP4 (Flattened Device Tree)
79 Revision        : 0000
80 Serial          : 0000000000000000
81 )";
82 
83   EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
84             "cortex-a9");
85   EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
86                                               "CPU part        : 0xc0f"),
87             "cortex-a15");
88   // Verify that both CPU implementer and CPU part are checked:
89   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");
95 }
96 
TEST(getLinuxHostCPUName,AArch64)97 TEST(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");
108   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");
114   EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
115                                               "CPU part        : 0xc00"),
116             "falkor");
117   EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
118                                               "CPU part        : 0xc01"),
119             "saphira");
120 
121   // MSM8992/4 weirdness
122   StringRef MSM8992ProcCpuInfo = R"(
123 Processor       : AArch64 Processor rev 3 (aarch64)
124 processor       : 0
125 processor       : 1
126 processor       : 2
127 processor       : 3
128 processor       : 4
129 processor       : 5
130 Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
131 CPU implementer : 0x41
132 CPU architecture: 8
133 CPU variant     : 0x0
134 CPU part        : 0xd03
135 CPU revision    : 3
136 
137 Hardware        : Qualcomm Technologies, Inc MSM8992
138 )";
139 
140   EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
141             "cortex-a53");
142 
143   // Exynos big.LITTLE weirdness
144   const std::string ExynosProcCpuInfo = R"(
145 processor       : 0
146 Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
147 CPU implementer : 0x41
148 CPU architecture: 8
149 CPU variant     : 0x0
150 CPU part        : 0xd03
151 
152 processor       : 1
153 Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
154 CPU implementer : 0x53
155 CPU 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");
173 }
174 
175 #if defined(__APPLE__)
TEST_F(HostTest,getMacOSHostVersion)176 TEST_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";
188   StringRef argv[] = {SwVersPath, "-productVersion"};
189   StringRef OutputPath = OutputFile.str();
190   const Optional<StringRef> Redirects[] = {/*STDIN=*/None,
191                                            /*STDOUT=*/OutputPath,
192                                            /*STDERR=*/None};
193   int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/llvm::None, Redirects);
194   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
222