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
17using namespace lldb_private;
18using namespace process_linux;
19using namespace llvm;
20
21TEST(Perf, HardcodedLogicalCoreIDs) {
22 Expected<std::vector<lldb::cpu_id_t>> cpu_ids =
23 GetAvailableLogicalCoreIDs(cpuinfo: R"(processor : 13
24vendor_id : GenuineIntel
25cpu family : 6
26model : 85
27model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz
28stepping : 4
29microcode : 0x2000065
30cpu MHz : 2886.370
31cache size : 28160 KB
32physical id : 1
33siblings : 40
34core id : 19
35cpu cores : 20
36apicid : 103
37initial apicid : 103
38fpu : yes
39fpu_exception : yes
40cpuid level : 22
41power management:
42
43processor : 24
44vendor_id : GenuineIntel
45cpu family : 6
46model : 85
47model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz
48stepping : 4
49microcode : 0x2000065
50cpu MHz : 2768.494
51cache size : 28160 KB
52physical id : 1
53siblings : 40
54core id : 20
55cpu cores : 20
56apicid : 105
57power management:
58
59processor : 35
60vendor_id : GenuineIntel
61cpu family : 6
62model : 85
63model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz
64stepping : 4
65microcode : 0x2000065
66cpu MHz : 2884.703
67cache size : 28160 KB
68physical id : 1
69siblings : 40
70core id : 24
71cpu cores : 20
72apicid : 113
73
74processor : 79
75vendor_id : GenuineIntel
76cpu family : 6
77model : 85
78model name : Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz
79stepping : 4
80microcode : 0x2000065
81cpu MHz : 3073.955
82cache size : 28160 KB
83physical id : 1
84siblings : 40
85core id : 28
86cpu cores : 20
87apicid : 121
88power management:
89)");
90
91 ASSERT_TRUE((bool)cpu_ids);
92 ASSERT_THAT(*cpu_ids, ::testing::ElementsAre(13, 24, 35, 79));
93}
94
95TEST(Perf, RealLogicalCoreIDs) {
96 // We first check we can read /proc/cpuinfo
97 auto buffer_or_error = errorOrToExpected(EO: getProcFile(file: "cpuinfo"));
98 if (!buffer_or_error)
99 GTEST_SKIP() << toString(E: 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
107TEST(Perf, RealPtraceScopeWhenExist) {
108 // We first check we can read /proc/sys/kernel/yama/ptrace_scope
109 auto buffer_or_error =
110 errorOrToExpected(EO: getProcFile(file: "sys/kernel/yama/ptrace_scope"));
111 if (!buffer_or_error)
112 GTEST_SKIP() << toString(E: 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
123TEST(Perf, RealPtraceScopeWhenNotExist) {
124 // We first check we can NOT read /proc/sys/kernel/yama/ptrace_scope
125 auto buffer_or_error =
126 errorOrToExpected(EO: getProcFile(file: "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(Err: 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(Err: ptrace_scope.takeError());
136}
137
138#ifdef LLVM_ENABLE_THREADING
139TEST(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 */
145

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of lldb/unittests/Process/Linux/ProcfsTests.cpp