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

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