File: Host.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (519 lines) | stat: -rw-r--r-- 18,654 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/Host.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Threading.h"

#include "gtest/gtest.h"

#define ASSERT_NO_ERROR(x)                                                     \
  if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
    SmallString<128> MessageStorage;                                           \
    raw_svector_ostream Message(MessageStorage);                               \
    Message << #x ": did not return errc::success.\n"                          \
            << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
            << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
    GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
  } else {                                                                     \
  }

using namespace llvm;

class HostTest : public testing::Test {
  Triple Host;

protected:
  bool isSupportedArchAndOS() {
    // Initially this is only testing detection of the number of
    // physical cores, which is currently only supported/tested on
    // some systems.
    return (Host.isOSWindows() && llvm_is_multithreaded()) ||
           Host.isOSDarwin() || (Host.isX86() && Host.isOSLinux()) ||
           (Host.isPPC64() && Host.isOSLinux()) ||
           (Host.isSystemZ() && (Host.isOSLinux() || Host.isOSzOS()));
  }

  HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
};

TEST_F(HostTest, NumPhysicalCores) {
  int Num = sys::getHostNumPhysicalCores();

  if (isSupportedArchAndOS())
    ASSERT_GT(Num, 0);
  else
    ASSERT_EQ(Num, -1);
}

TEST(getLinuxHostCPUName, ARM) {
  StringRef CortexA9ProcCpuinfo = R"(
processor       : 0
model name      : ARMv7 Processor rev 10 (v7l)
BogoMIPS        : 1393.66
Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x2
CPU part        : 0xc09
CPU revision    : 10

processor       : 1
model name      : ARMv7 Processor rev 10 (v7l)
BogoMIPS        : 1393.66
Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x2
CPU part        : 0xc09
CPU revision    : 10

Hardware        : Generic OMAP4 (Flattened Device Tree)
Revision        : 0000
Serial          : 0000000000000000
)";

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
            "cortex-a9");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
                                              "CPU part        : 0xc0f"),
            "cortex-a15");
  // Verify that both CPU implementer and CPU part are checked:
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
                                              "CPU part        : 0xc0f"),
            "generic");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0x06f"),
            "krait");
}

TEST(getLinuxHostCPUName, AArch64) {
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
                                              "CPU part        : 0xd03"),
            "cortex-a53");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
                                              "CPU part        : 0xd0c"),
            "neoverse-n1");
  // Verify that both CPU implementer and CPU part are checked:
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
                                              "CPU part        : 0xd03"),
            "generic");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0x201"),
            "kryo");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0x800"),
            "cortex-a73");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0x801"),
            "cortex-a73");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0xc00"),
            "falkor");
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
                                              "CPU part        : 0xc01"),
            "saphira");

  // MSM8992/4 weirdness
  StringRef MSM8992ProcCpuInfo = R"(
Processor       : AArch64 Processor rev 3 (aarch64)
processor       : 0
processor       : 1
processor       : 2
processor       : 3
processor       : 4
processor       : 5
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 3

Hardware        : Qualcomm Technologies, Inc MSM8992
)";

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
            "cortex-a53");

  // Exynos big.LITTLE weirdness
  const std::string ExynosProcCpuInfo = R"(
processor       : 0
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd05

processor       : 1
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x53
CPU architecture: 8
)";

  // Verify default for Exynos.
  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
                                              "CPU variant     : 0xc\n"
                                              "CPU part        : 0xafe"),
            "exynos-m3");
  // Verify Exynos M3.
  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
                                              "CPU variant     : 0x1\n"
                                              "CPU part        : 0x002"),
            "exynos-m3");
  // Verify Exynos M4.
  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
                                              "CPU variant     : 0x1\n"
                                              "CPU part        : 0x003"),
            "exynos-m4");

  const std::string ThunderX2T99ProcCpuInfo = R"(
processor	: 0
BogoMIPS	: 400.00
Features	: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
CPU implementer	: 0x43
CPU architecture: 8
CPU variant	: 0x1
CPU part	: 0x0af
)";

  // Verify different versions of ThunderX2T99.
  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x42\n"
                                              "CPU part	: 0x516"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x42\n"
                                              "CPU part	: 0x0516"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0x516"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0x0516"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x42\n"
                                              "CPU part	: 0xaf"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x42\n"
                                              "CPU part	: 0x0af"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0xaf"),
            "thunderx2t99");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0x0af"),
            "thunderx2t99");

  // Verify ThunderXT88.
  const std::string ThunderXT88ProcCpuInfo = R"(
processor	: 0
BogoMIPS	: 200.00
Features	: fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer	: 0x43
CPU architecture: 8
CPU variant	: 0x1
CPU part	: 0x0a1
)";

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0x0a1"),
            "thunderxt88");

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
                                              "CPU implementer	: 0x43\n"
                                              "CPU part	: 0xa1"),
            "thunderxt88");

  // Verify HiSilicon processors.
  EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x48\n"
                                              "CPU part        : 0xd01"),
            "tsv110");

  // Verify A64FX.
  const std::string A64FXProcCpuInfo = R"(
processor       : 0
BogoMIPS        : 200.00
Features        : fp asimd evtstrm sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm fcma dcpop sve
CPU implementer : 0x46
CPU architecture: 8
CPU variant     : 0x1
CPU part        : 0x001
)";

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(A64FXProcCpuInfo), "a64fx");

  // Verify Nvidia Carmel.
  const std::string CarmelProcCpuInfo = R"(
processor       : 0
model name      : ARMv8 Processor rev 0 (v8l)
BogoMIPS        : 62.50
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm dcpop
CPU implementer : 0x4e
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0x004
CPU revision    : 0
)";

  EXPECT_EQ(sys::detail::getHostCPUNameForARM(CarmelProcCpuInfo), "carmel");

  // Snapdragon mixed implementer quirk
  const std::string Snapdragon865ProcCPUInfo = R"(
processor       : 0
BogoMIPS        : 38.40
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x51
CPU architecture: 8
CPU variant     : 0xd
CPU part        : 0x805
CPU revision    : 14
processor       : 1
processor       : 2
processor       : 3
processor       : 4
processor       : 5
processor       : 6
BogoMIPS        : 38.40
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x1
CPU part        : 0xd0d
CPU revision    : 0
)";
  EXPECT_EQ(sys::detail::getHostCPUNameForARM(Snapdragon865ProcCPUInfo), "cortex-a77");
}

TEST(getLinuxHostCPUName, s390x) {
  SmallVector<std::string> ModelIDs(
      {"3931", "8561", "3906", "2964", "2827", "2817", "2097", "2064"});
  SmallVector<std::string> VectorSupport({"", "vx"});
  SmallVector<StringRef> ExpectedCPUs;

  // Model Id: 3931
  ExpectedCPUs.push_back("zEC12");
  ExpectedCPUs.push_back("arch14");

  // Model Id: 8561
  ExpectedCPUs.push_back("zEC12");
  ExpectedCPUs.push_back("z15");

  // Model Id: 3906
  ExpectedCPUs.push_back("zEC12");
  ExpectedCPUs.push_back("z14");

  // Model Id: 2964
  ExpectedCPUs.push_back("zEC12");
  ExpectedCPUs.push_back("z13");

  // Model Id: 2827
  ExpectedCPUs.push_back("zEC12");
  ExpectedCPUs.push_back("zEC12");

  // Model Id: 2817
  ExpectedCPUs.push_back("z196");
  ExpectedCPUs.push_back("z196");

  // Model Id: 2097
  ExpectedCPUs.push_back("z10");
  ExpectedCPUs.push_back("z10");

  // Model Id: 2064
  ExpectedCPUs.push_back("generic");
  ExpectedCPUs.push_back("generic");

  const std::string DummyBaseVectorInfo =
      "features : esan3 zarch stfle msa ldisp eimm dfp edat etf3eh highgprs "
      "te ";
  const std::string DummyBaseMachineInfo =
      "processor 0: version = FF,  identification = 059C88,  machine = ";

  int CheckIndex = 0;
  for (size_t I = 0; I < ModelIDs.size(); I++) {
    for (size_t J = 0; J < VectorSupport.size(); J++) {
      const std::string DummyCPUInfo = DummyBaseVectorInfo + VectorSupport[J] +
                                       "\n" + DummyBaseMachineInfo +
                                       ModelIDs[I];
      EXPECT_EQ(sys::detail::getHostCPUNameForS390x(DummyCPUInfo),
                ExpectedCPUs[CheckIndex++]);
    }
  }
}

#if defined(__APPLE__) || defined(_AIX)
static bool runAndGetCommandOutput(
    const char *ExePath, ArrayRef<llvm::StringRef> argv,
    std::unique_ptr<char[]> &Buffer, off_t &Size) {
  bool Success = false;
  [ExePath, argv, &Buffer, &Size, &Success] {
    using namespace llvm::sys;
    SmallString<128> TestDirectory;
    ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));

    SmallString<128> OutputFile(TestDirectory);
    path::append(OutputFile, "out");
    StringRef OutputPath = OutputFile.str();

    const Optional<StringRef> Redirects[] = {
        /*STDIN=*/None, /*STDOUT=*/OutputPath, /*STDERR=*/None};
    int RetCode = ExecuteAndWait(ExePath, argv, /*env=*/llvm::None, Redirects);
    ASSERT_EQ(0, RetCode);

    int FD = 0;
    ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
    Size = ::lseek(FD, 0, SEEK_END);
    ASSERT_NE(-1, Size);
    ::lseek(FD, 0, SEEK_SET);
    Buffer = std::make_unique<char[]>(Size);
    ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
    ::close(FD);

    ASSERT_NO_ERROR(fs::remove(OutputPath));
    ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
    Success = true;
  }();
  return Success;
}

TEST_F(HostTest, DummyRunAndGetCommandOutputUse) {
  // Suppress defined-but-not-used warnings when the tests using the helper are
  // disabled.
  (void) runAndGetCommandOutput;
}
#endif

#if defined(__APPLE__)
TEST_F(HostTest, getMacOSHostVersion) {
  using namespace llvm::sys;
  llvm::Triple HostTriple(getProcessTriple());
  if (!HostTriple.isMacOSX())
    return;

  const char *SwVersPath = "/usr/bin/sw_vers";
  StringRef argv[] = {SwVersPath, "-productVersion"};
  std::unique_ptr<char[]> Buffer;
  off_t Size;
  ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true);
  StringRef SystemVersion(Buffer.get(), Size);

  // Ensure that the two versions match.
  unsigned SystemMajor, SystemMinor, SystemMicro;
  ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
                .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
            true);
  unsigned HostMajor, HostMinor, HostMicro;
  ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);

  if (SystemMajor > 10) {
    // Don't compare the 'Minor' and 'Micro' versions, as they're always '0' for
    // the 'Darwin' triples on 11.x.
    ASSERT_EQ(SystemMajor, HostMajor);
  } else {
    // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
    // triples.
    ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
  }
}
#endif

#if defined(_AIX)
TEST_F(HostTest, AIXVersionDetect) {
  using namespace llvm::sys;

  llvm::Triple HostTriple(getProcessTriple());
  ASSERT_EQ(HostTriple.getOS(), Triple::AIX);

  llvm::Triple ConfiguredHostTriple(LLVM_HOST_TRIPLE);
  ASSERT_EQ(ConfiguredHostTriple.getOS(), Triple::AIX);

  const char *ExePath = "/usr/bin/oslevel";
  StringRef argv[] = {ExePath};
  std::unique_ptr<char[]> Buffer;
  off_t Size;
  ASSERT_EQ(runAndGetCommandOutput(ExePath, argv, Buffer, Size), true);
  StringRef SystemVersion(Buffer.get(), Size);

  unsigned SystemMajor, SystemMinor, SystemMicro;
  llvm::Triple((Twine("powerpc-ibm-aix") + SystemVersion))
      .getOSVersion(SystemMajor, SystemMinor, SystemMicro);

  // Ensure that the host triple version (major) and release (minor) numbers,
  // unless explicitly configured, match with those of the current system.
  if (!ConfiguredHostTriple.getOSMajorVersion()) {
    unsigned HostMajor, HostMinor, HostMicro;
    HostTriple.getOSVersion(HostMajor, HostMinor, HostMicro);
    ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
              std::tie(HostMajor, HostMinor));
  }

  llvm::Triple TargetTriple(getDefaultTargetTriple());
  if (TargetTriple.getOS() != Triple::AIX)
    return;

  // Ensure that the target triple version (major) and release (minor) numbers
  // match with those of the current system.
  llvm::Triple ConfiguredTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE);
  if (ConfiguredTargetTriple.getOSMajorVersion())
    return; // The version was configured explicitly; skip.

  unsigned TargetMajor, TargetMinor, TargetMicro;
  TargetTriple.getOSVersion(TargetMajor, TargetMinor, TargetMicro);
  ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
            std::tie(TargetMajor, TargetMinor));
}

TEST_F(HostTest, AIXHostCPUDetect) {
  // Return a value based on the current processor implementation mode.
  const char *ExePath = "/usr/sbin/getsystype";
  StringRef argv[] = {ExePath, "-i"};
  std::unique_ptr<char[]> Buffer;
  off_t Size;
  ASSERT_EQ(runAndGetCommandOutput(ExePath, argv, Buffer, Size), true);
  StringRef CPU(Buffer.get(), Size);
  StringRef MCPU = StringSwitch<const char *>(CPU)
                       .Case("POWER 4\n", "pwr4")
                       .Case("POWER 5\n", "pwr5")
                       .Case("POWER 6\n", "pwr6")
                       .Case("POWER 7\n", "pwr7")
                       .Case("POWER 8\n", "pwr8")
                       .Case("POWER 9\n", "pwr9")
                       .Case("POWER 10\n", "pwr10")
                       .Default("unknown");

  StringRef HostCPU = sys::getHostCPUName();

  // Just do the comparison on the base implementation mode.
  if (HostCPU == "970")
    HostCPU = StringRef("pwr4");
  else
    HostCPU = HostCPU.rtrim('x');

  EXPECT_EQ(HostCPU, MCPU);
}
#endif