Host.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/Host.h"
  9. #include "llvm/Config/llvm-config.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/Triple.h"
  12. #include "llvm/Support/FileSystem.h"
  13. #include "llvm/Support/Path.h"
  14. #include "llvm/Support/Program.h"
  15. #include "gtest/gtest.h"
  16. #define ASSERT_NO_ERROR(x) \
  17. if (std::error_code ASSERT_NO_ERROR_ec = x) { \
  18. SmallString<128> MessageStorage; \
  19. raw_svector_ostream Message(MessageStorage); \
  20. Message << #x ": did not return errc::success.\n" \
  21. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  22. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  23. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  24. } else { \
  25. }
  26. using namespace llvm;
  27. class HostTest : public testing::Test {
  28. Triple Host;
  29. protected:
  30. bool isSupportedArchAndOS() {
  31. // Initially this is only testing detection of the number of
  32. // physical cores, which is currently only supported/tested for
  33. // x86_64 Linux and Darwin.
  34. return (Host.getArch() == Triple::x86_64 &&
  35. (Host.isOSDarwin() || Host.getOS() == Triple::Linux));
  36. }
  37. HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
  38. };
  39. TEST_F(HostTest, NumPhysicalCores) {
  40. int Num = sys::getHostNumPhysicalCores();
  41. if (isSupportedArchAndOS())
  42. ASSERT_GT(Num, 0);
  43. else
  44. ASSERT_EQ(Num, -1);
  45. }
  46. TEST(getLinuxHostCPUName, ARM) {
  47. StringRef CortexA9ProcCpuinfo = R"(
  48. processor : 0
  49. model name : ARMv7 Processor rev 10 (v7l)
  50. BogoMIPS : 1393.66
  51. Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
  52. CPU implementer : 0x41
  53. CPU architecture: 7
  54. CPU variant : 0x2
  55. CPU part : 0xc09
  56. CPU revision : 10
  57. processor : 1
  58. model name : ARMv7 Processor rev 10 (v7l)
  59. BogoMIPS : 1393.66
  60. Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
  61. CPU implementer : 0x41
  62. CPU architecture: 7
  63. CPU variant : 0x2
  64. CPU part : 0xc09
  65. CPU revision : 10
  66. Hardware : Generic OMAP4 (Flattened Device Tree)
  67. Revision : 0000
  68. Serial : 0000000000000000
  69. )";
  70. EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
  71. "cortex-a9");
  72. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
  73. "CPU part : 0xc0f"),
  74. "cortex-a15");
  75. // Verify that both CPU implementer and CPU part are checked:
  76. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
  77. "CPU part : 0xc0f"),
  78. "generic");
  79. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  80. "CPU part : 0x06f"),
  81. "krait");
  82. }
  83. TEST(getLinuxHostCPUName, AArch64) {
  84. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
  85. "CPU part : 0xd03"),
  86. "cortex-a53");
  87. // Verify that both CPU implementer and CPU part are checked:
  88. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
  89. "CPU part : 0xd03"),
  90. "generic");
  91. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  92. "CPU part : 0x201"),
  93. "kryo");
  94. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  95. "CPU part : 0x800"),
  96. "cortex-a73");
  97. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  98. "CPU part : 0x801"),
  99. "cortex-a73");
  100. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  101. "CPU part : 0xc00"),
  102. "falkor");
  103. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
  104. "CPU part : 0xc01"),
  105. "saphira");
  106. // MSM8992/4 weirdness
  107. StringRef MSM8992ProcCpuInfo = R"(
  108. Processor : AArch64 Processor rev 3 (aarch64)
  109. processor : 0
  110. processor : 1
  111. processor : 2
  112. processor : 3
  113. processor : 4
  114. processor : 5
  115. Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
  116. CPU implementer : 0x41
  117. CPU architecture: 8
  118. CPU variant : 0x0
  119. CPU part : 0xd03
  120. CPU revision : 3
  121. Hardware : Qualcomm Technologies, Inc MSM8992
  122. )";
  123. EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
  124. "cortex-a53");
  125. // Exynos big.LITTLE weirdness
  126. const std::string ExynosProcCpuInfo = R"(
  127. processor : 0
  128. Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
  129. CPU implementer : 0x41
  130. CPU architecture: 8
  131. CPU variant : 0x0
  132. CPU part : 0xd03
  133. processor : 1
  134. Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
  135. CPU implementer : 0x53
  136. CPU architecture: 8
  137. )";
  138. // Verify default for Exynos.
  139. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
  140. "CPU variant : 0xc\n"
  141. "CPU part : 0xafe"),
  142. "exynos-m1");
  143. // Verify Exynos M1.
  144. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
  145. "CPU variant : 0x1\n"
  146. "CPU part : 0x001"),
  147. "exynos-m1");
  148. // Verify Exynos M2.
  149. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
  150. "CPU variant : 0x4\n"
  151. "CPU part : 0x001"),
  152. "exynos-m2");
  153. const std::string ThunderX2T99ProcCpuInfo = R"(
  154. processor : 0
  155. BogoMIPS : 400.00
  156. Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
  157. CPU implementer : 0x43
  158. CPU architecture: 8
  159. CPU variant : 0x1
  160. CPU part : 0x0af
  161. )";
  162. // Verify different versions of ThunderX2T99.
  163. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  164. "CPU implementer : 0x42\n"
  165. "CPU part : 0x516"),
  166. "thunderx2t99");
  167. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  168. "CPU implementer : 0x42\n"
  169. "CPU part : 0x0516"),
  170. "thunderx2t99");
  171. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  172. "CPU implementer : 0x43\n"
  173. "CPU part : 0x516"),
  174. "thunderx2t99");
  175. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  176. "CPU implementer : 0x43\n"
  177. "CPU part : 0x0516"),
  178. "thunderx2t99");
  179. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  180. "CPU implementer : 0x42\n"
  181. "CPU part : 0xaf"),
  182. "thunderx2t99");
  183. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  184. "CPU implementer : 0x42\n"
  185. "CPU part : 0x0af"),
  186. "thunderx2t99");
  187. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  188. "CPU implementer : 0x43\n"
  189. "CPU part : 0xaf"),
  190. "thunderx2t99");
  191. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
  192. "CPU implementer : 0x43\n"
  193. "CPU part : 0x0af"),
  194. "thunderx2t99");
  195. // Verify ThunderXT88.
  196. const std::string ThunderXT88ProcCpuInfo = R"(
  197. processor : 0
  198. BogoMIPS : 200.00
  199. Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
  200. CPU implementer : 0x43
  201. CPU architecture: 8
  202. CPU variant : 0x1
  203. CPU part : 0x0a1
  204. )";
  205. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
  206. "CPU implementer : 0x43\n"
  207. "CPU part : 0x0a1"),
  208. "thunderxt88");
  209. EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
  210. "CPU implementer : 0x43\n"
  211. "CPU part : 0xa1"),
  212. "thunderxt88");
  213. // Verify HiSilicon processors.
  214. EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x48\n"
  215. "CPU part : 0xd01"),
  216. "tsv110");
  217. }
  218. #if defined(__APPLE__) || defined(_AIX)
  219. static bool runAndGetCommandOutput(
  220. const char *ExePath, ArrayRef<llvm::StringRef> argv,
  221. std::unique_ptr<char[]> &Buffer, off_t &Size) {
  222. bool Success = false;
  223. [ExePath, argv, &Buffer, &Size, &Success] {
  224. using namespace llvm::sys;
  225. SmallString<128> TestDirectory;
  226. ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
  227. SmallString<128> OutputFile(TestDirectory);
  228. path::append(OutputFile, "out");
  229. StringRef OutputPath = OutputFile.str();
  230. const Optional<StringRef> Redirects[] = {
  231. /*STDIN=*/None, /*STDOUT=*/OutputPath, /*STDERR=*/None};
  232. int RetCode = ExecuteAndWait(ExePath, argv, /*env=*/llvm::None, Redirects);
  233. ASSERT_EQ(0, RetCode);
  234. int FD = 0;
  235. ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
  236. Size = ::lseek(FD, 0, SEEK_END);
  237. ASSERT_NE(-1, Size);
  238. ::lseek(FD, 0, SEEK_SET);
  239. Buffer = std::make_unique<char[]>(Size);
  240. ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
  241. ::close(FD);
  242. ASSERT_NO_ERROR(fs::remove(OutputPath));
  243. ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
  244. Success = true;
  245. }();
  246. return Success;
  247. }
  248. TEST_F(HostTest, DummyRunAndGetCommandOutputUse) {
  249. // Suppress defined-but-not-used warnings when the tests using the helper are
  250. // disabled.
  251. (void) runAndGetCommandOutput;
  252. }
  253. #endif
  254. #if defined(__APPLE__)
  255. TEST_F(HostTest, getMacOSHostVersion) {
  256. using namespace llvm::sys;
  257. llvm::Triple HostTriple(getProcessTriple());
  258. if (!HostTriple.isMacOSX())
  259. return;
  260. const char *SwVersPath = "/usr/bin/sw_vers";
  261. StringRef argv[] = {SwVersPath, "-productVersion"};
  262. std::unique_ptr<char[]> Buffer;
  263. off_t Size;
  264. ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true);
  265. StringRef SystemVersion(Buffer.get(), Size);
  266. // Ensure that the two versions match.
  267. unsigned SystemMajor, SystemMinor, SystemMicro;
  268. ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
  269. .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
  270. true);
  271. unsigned HostMajor, HostMinor, HostMicro;
  272. ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
  273. // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
  274. // triples.
  275. ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
  276. }
  277. #endif
  278. #if defined(_AIX)
  279. TEST_F(HostTest, AIXVersionDetect) {
  280. using namespace llvm::sys;
  281. llvm::Triple HostTriple(getProcessTriple());
  282. ASSERT_EQ(HostTriple.getOS(), Triple::AIX);
  283. llvm::Triple ConfiguredHostTriple(LLVM_HOST_TRIPLE);
  284. ASSERT_EQ(ConfiguredHostTriple.getOS(), Triple::AIX);
  285. const char *ExePath = "/usr/bin/oslevel";
  286. StringRef argv[] = {ExePath};
  287. std::unique_ptr<char[]> Buffer;
  288. off_t Size;
  289. ASSERT_EQ(runAndGetCommandOutput(ExePath, argv, Buffer, Size), true);
  290. StringRef SystemVersion(Buffer.get(), Size);
  291. unsigned SystemMajor, SystemMinor, SystemMicro;
  292. llvm::Triple((Twine("powerpc-ibm-aix") + SystemVersion))
  293. .getOSVersion(SystemMajor, SystemMinor, SystemMicro);
  294. // Ensure that the host triple version (major) and release (minor) numbers,
  295. // unless explicitly configured, match with those of the current system.
  296. if (!ConfiguredHostTriple.getOSMajorVersion()) {
  297. unsigned HostMajor, HostMinor, HostMicro;
  298. HostTriple.getOSVersion(HostMajor, HostMinor, HostMicro);
  299. ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
  300. std::tie(HostMajor, HostMinor));
  301. }
  302. llvm::Triple TargetTriple(getDefaultTargetTriple());
  303. if (TargetTriple.getOS() != Triple::AIX)
  304. return;
  305. // Ensure that the target triple version (major) and release (minor) numbers
  306. // match with those of the current system.
  307. llvm::Triple ConfiguredTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE);
  308. if (ConfiguredTargetTriple.getOSMajorVersion())
  309. return; // The version was configured explicitly; skip.
  310. unsigned TargetMajor, TargetMinor, TargetMicro;
  311. TargetTriple.getOSVersion(TargetMajor, TargetMinor, TargetMicro);
  312. ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
  313. std::tie(TargetMajor, TargetMinor));
  314. }
  315. #endif