blob: 3dabe97235014539c779925a28ab5892ca3f2dfa [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");
Yi Kong57019dc2017-04-04 19:06:04 +0000114
115 // MSM8992/4 weirdness
116 StringRef MSM8992ProcCpuInfo = R"(
117Processor : AArch64 Processor rev 3 (aarch64)
118processor : 0
119processor : 1
120processor : 2
121processor : 3
122processor : 4
123processor : 5
124Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
125CPU implementer : 0x41
126CPU architecture: 8
127CPU variant : 0x0
128CPU part : 0xd03
129CPU revision : 3
130
131Hardware : Qualcomm Technologies, Inc MSM8992
132)";
133
134 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
135 "cortex-a53");
136}
Alex Lorenz3803df32017-07-07 09:53:47 +0000137
138#if defined(__APPLE__)
139TEST_F(HostTest, getMacOSHostVersion) {
140 using namespace llvm::sys;
141 llvm::Triple HostTriple(getProcessTriple());
142 if (!HostTriple.isMacOSX())
143 return;
144
145 SmallString<128> TestDirectory;
146 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
147 SmallString<128> OutputFile(TestDirectory);
148 path::append(OutputFile, "out");
149
150 const char *SwVersPath = "/usr/bin/sw_vers";
151 const char *argv[] = {SwVersPath, "-productVersion", nullptr};
152 StringRef OutputPath = OutputFile.str();
Vedant Kumarbc26f722017-09-13 18:00:22 +0000153 const Optional<StringRef> Redirects[] = {/*STDIN=*/None,
154 /*STDOUT=*/OutputPath,
155 /*STDERR=*/None};
Alex Lorenz3803df32017-07-07 09:53:47 +0000156 int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/nullptr, Redirects);
157 ASSERT_EQ(0, RetCode);
158
159 int FD = 0;
160 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
161 off_t Size = ::lseek(FD, 0, SEEK_END);
162 ASSERT_NE(-1, Size);
163 ::lseek(FD, 0, SEEK_SET);
164 std::unique_ptr<char[]> Buffer = llvm::make_unique<char[]>(Size);
165 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
166 ::close(FD);
167
168 // Ensure that the two versions match.
169 StringRef SystemVersion(Buffer.get(), Size);
170 unsigned SystemMajor, SystemMinor, SystemMicro;
171 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
172 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
173 true);
174 unsigned HostMajor, HostMinor, HostMicro;
175 ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
176
177 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
178 // triples.
179 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
180
181 ASSERT_NO_ERROR(fs::remove(OutputPath));
182 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
183}
184#endif