| 1 |
//===-- ProcfsTests.cpp ---------------------------------------------------===// |
| 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 |
|
| 9 |
#include "Procfs.h" |
| 10 |
|
| 11 |
#include "lldb/Host/linux/Support.h" |
| 12 |
#include "lldb/Host/posix/Support.h" |
| 13 |
|
| 14 |
#include "gmock/gmock.h" |
| 15 |
#include "gtest/gtest.h" |
| 16 |
|
| 17 |
using namespace lldb_private; |
| 18 |
using namespace process_linux; |
| 19 |
using namespace llvm; |
| 20 |
|
| 21 |
TEST(Perf, HardcodedLogicalCoreIDs) { |
| 22 |
Expected<std::vector<lldb::cpu_id_t>> cpu_ids = |
| 23 |
GetAvailableLogicalCoreIDs(R"(processor : 13 |
| 24 |
vendor_id : GenuineIntel |
| 25 |
cpu family : 6 |
| 26 |
model : 85 |
| 27 |
model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz |
| 28 |
stepping : 4 |
| 29 |
microcode : 0x2000065 |
| 30 |
cpu MHz : 2886.370 |
| 31 |
cache size : 28160 KB |
| 32 |
physical id : 1 |
| 33 |
siblings : 40 |
| 34 |
core id : 19 |
| 35 |
cpu cores : 20 |
| 36 |
apicid : 103 |
| 37 |
initial apicid : 103 |
| 38 |
fpu : yes |
| 39 |
fpu_exception : yes |
| 40 |
cpuid level : 22 |
| 41 |
power management: |
| 42 |
|
| 43 |
processor : 24 |
| 44 |
vendor_id : GenuineIntel |
| 45 |
cpu family : 6 |
| 46 |
model : 85 |
| 47 |
model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz |
| 48 |
stepping : 4 |
| 49 |
microcode : 0x2000065 |
| 50 |
cpu MHz : 2768.494 |
| 51 |
cache size : 28160 KB |
| 52 |
physical id : 1 |
| 53 |
siblings : 40 |
| 54 |
core id : 20 |
| 55 |
cpu cores : 20 |
| 56 |
apicid : 105 |
| 57 |
power management: |
| 58 |
|
| 59 |
processor : 35 |
| 60 |
vendor_id : GenuineIntel |
| 61 |
cpu family : 6 |
| 62 |
model : 85 |
| 63 |
model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz |
| 64 |
stepping : 4 |
| 65 |
microcode : 0x2000065 |
| 66 |
cpu MHz : 2884.703 |
| 67 |
cache size : 28160 KB |
| 68 |
physical id : 1 |
| 69 |
siblings : 40 |
| 70 |
core id : 24 |
| 71 |
cpu cores : 20 |
| 72 |
apicid : 113 |
| 73 |
|
| 74 |
processor : 79 |
| 75 |
vendor_id : GenuineIntel |
| 76 |
cpu family : 6 |
| 77 |
model : 85 |
| 78 |
model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz |
| 79 |
stepping : 4 |
| 80 |
microcode : 0x2000065 |
| 81 |
cpu MHz : 3073.955 |
| 82 |
cache size : 28160 KB |
| 83 |
physical id : 1 |
| 84 |
siblings : 40 |
| 85 |
core id : 28 |
| 86 |
cpu cores : 20 |
| 87 |
apicid : 121 |
| 88 |
power management: |
| 89 |
)"); |
| 90 |
|
| 91 |
ASSERT_TRUE((bool)cpu_ids); |
| 92 |
ASSERT_THAT(*cpu_ids, ::testing::ElementsAre(13, 24, 35, 79)); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(Perf, RealLogicalCoreIDs) { |
| 96 |
// We first check we can read /proc/cpuinfo |
| 97 |
auto buffer_or_error = errorOrToExpected(getProcFile("cpuinfo")); |
| 98 |
if (!buffer_or_error) |
| 99 |
GTEST_SKIP() << toString(buffer_or_error.takeError()); |
| 100 |
|
| 101 |
// At this point we shouldn't fail parsing the core ids |
| 102 |
Expected<ArrayRef<lldb::cpu_id_t>> cpu_ids = GetAvailableLogicalCoreIDs(); |
| 103 |
ASSERT_TRUE((bool)cpu_ids); |
| 104 |
ASSERT_GT((int)cpu_ids->size(), 0) << "We must see at least one core"; |
| 105 |
} |
| 106 |
|
| 107 |
TEST(Perf, RealPtraceScopeWhenExist) { |
| 108 |
// We first check we can read /proc/sys/kernel/yama/ptrace_scope |
| 109 |
auto buffer_or_error = |
| 110 |
errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope")); |
| 111 |
if (!buffer_or_error) |
| 112 |
GTEST_SKIP() << toString(buffer_or_error.takeError()); |
| 113 |
|
| 114 |
// At this point we shouldn't fail parsing the ptrace_scope value. |
| 115 |
Expected<int> ptrace_scope = GetPtraceScope(); |
| 116 |
ASSERT_TRUE((bool)ptrace_scope) << ptrace_scope.takeError(); |
| 117 |
ASSERT_GE(*ptrace_scope, 0) |
| 118 |
<< "Sensible values of ptrace_scope are between 0 and 3"; |
| 119 |
ASSERT_LE(*ptrace_scope, 3) |
| 120 |
<< "Sensible values of ptrace_scope are between 0 and 3"; |
| 121 |
} |
| 122 |
|
| 123 |
TEST(Perf, RealPtraceScopeWhenNotExist) { |
| 124 |
// We first check we can NOT read /proc/sys/kernel/yama/ptrace_scope |
| 125 |
auto buffer_or_error = |
| 126 |
errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope")); |
| 127 |
if (buffer_or_error) |
| 128 |
GTEST_SKIP() << "In order for this test to run, " |
| 129 |
"/proc/sys/kernel/yama/ptrace_scope should not exist"; |
| 130 |
consumeError(buffer_or_error.takeError()); |
| 131 |
|
| 132 |
// At this point we should fail parsing the ptrace_scope value. |
| 133 |
Expected<int> ptrace_scope = GetPtraceScope(); |
| 134 |
ASSERT_FALSE((bool)ptrace_scope); |
| 135 |
consumeError(ptrace_scope.takeError()); |
| 136 |
} |
| 137 |
|
| 138 |
#ifdef LLVM_ENABLE_THREADING |
| 139 |
TEST(Support, getProcFile_Tid) { |
| 140 |
auto BufferOrError = getProcFile(getpid(), llvm::get_threadid(), "comm"); |
| 141 |
ASSERT_TRUE(BufferOrError); |
| 142 |
ASSERT_TRUE(*BufferOrError); |
| 143 |
} |
| 144 |
#endif /*ifdef LLVM_ENABLE_THREADING */ |