1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fstream>
18 #include <set>
19 #include <sstream>
20 #include <string>
21
22 #include "perfetto/ext/base/file_utils.h"
23 #include "src/traced/probes/ftrace/ftrace_controller.h"
24 #include "src/traced/probes/ftrace/ftrace_procfs.h"
25 #include "test/gtest_and_gmock.h"
26
27 using testing::Contains;
28 using testing::HasSubstr;
29 using testing::IsEmpty;
30 using testing::Not;
31 using testing::UnorderedElementsAre;
32
33 // These tests run only on Android because on linux they require access to
34 // ftrace, which would be problematic in the CI when multiple tests run
35 // concurrently on the same machine. Android instead uses one emulator instance
36 // for each worker.
37 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
38 // On Android these tests conflict with traced_probes which expects to be the
39 // only one modifying tracing. This led to the Setup code which attempts to
40 // to skip these tests when traced_probes is using tracing. Unfortunately this
41 // is racey and we still see spurious failures in practice. For now disable
42 // these tests on Android also.
43 // TODO(b/150675975) Re-enable these tests.
44 #define ANDROID_ONLY_TEST(x) DISABLED_##x
45 #else
46 #define ANDROID_ONLY_TEST(x) DISABLED_##x
47 #endif
48
49 namespace perfetto {
50 namespace {
51
GetFtracePath()52 std::string GetFtracePath() {
53 auto ftrace_procfs = FtraceProcfs::CreateGuessingMountPoint();
54 if (!ftrace_procfs)
55 return "";
56 return ftrace_procfs->GetRootPath();
57 }
58
ReadFile(const std::string & name)59 std::string ReadFile(const std::string& name) {
60 std::string result;
61 PERFETTO_CHECK(base::ReadFile(GetFtracePath() + name, &result));
62 return result;
63 }
64
GetTraceOutput()65 std::string GetTraceOutput() {
66 std::string output = ReadFile("trace");
67 if (output.empty()) {
68 ADD_FAILURE() << "Could not read trace output";
69 }
70 return output;
71 }
72
73 class FtraceProcfsIntegrationTest : public testing::Test {
74 public:
75 void SetUp() override;
76 void TearDown() override;
77
78 std::unique_ptr<FtraceProcfs> ftrace_;
79 };
80
SetUp()81 void FtraceProcfsIntegrationTest::SetUp() {
82 ftrace_ = FtraceProcfs::Create(GetFtracePath());
83 ASSERT_TRUE(ftrace_);
84 if (ftrace_->IsTracingEnabled()) {
85 GTEST_SKIP() << "Something else is using ftrace, skipping";
86 }
87
88 ftrace_->DisableAllEvents();
89 ftrace_->ClearTrace();
90 ftrace_->EnableTracing();
91 }
92
TearDown()93 void FtraceProcfsIntegrationTest::TearDown() {
94 ftrace_->DisableAllEvents();
95 ftrace_->ClearTrace();
96 ftrace_->DisableTracing();
97 }
98
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (CreateWithBadPath))99 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(CreateWithBadPath)) {
100 EXPECT_FALSE(FtraceProcfs::Create(GetFtracePath() + std::string("bad_path")));
101 }
102
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (ClearTrace))103 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(ClearTrace)) {
104 ftrace_->WriteTraceMarker("Hello, World!");
105 ftrace_->ClearTrace();
106 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello, World!")));
107 }
108
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (TraceMarker))109 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(TraceMarker)) {
110 ftrace_->WriteTraceMarker("Hello, World!");
111 EXPECT_THAT(GetTraceOutput(), HasSubstr("Hello, World!"));
112 }
113
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (EnableDisableEvent))114 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(EnableDisableEvent)) {
115 ASSERT_TRUE(ftrace_->EnableEvent("sched", "sched_switch"));
116 sleep(1);
117 ASSERT_TRUE(ftrace_->DisableEvent("sched", "sched_switch"));
118
119 EXPECT_THAT(GetTraceOutput(), HasSubstr("sched_switch"));
120
121 ftrace_->ClearTrace();
122 sleep(1);
123 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("sched_switch")));
124 }
125
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (EnableDisableTracing))126 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(EnableDisableTracing)) {
127 EXPECT_TRUE(ftrace_->IsTracingEnabled());
128 ftrace_->WriteTraceMarker("Before");
129 ftrace_->DisableTracing();
130 EXPECT_FALSE(ftrace_->IsTracingEnabled());
131 ftrace_->WriteTraceMarker("During");
132 ftrace_->EnableTracing();
133 EXPECT_TRUE(ftrace_->IsTracingEnabled());
134 ftrace_->WriteTraceMarker("After");
135 EXPECT_THAT(GetTraceOutput(), HasSubstr("Before"));
136 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("During")));
137 EXPECT_THAT(GetTraceOutput(), HasSubstr("After"));
138 }
139
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (ReadFormatFile))140 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(ReadFormatFile)) {
141 std::string format = ftrace_->ReadEventFormat("ftrace", "print");
142 EXPECT_THAT(format, HasSubstr("name: print"));
143 EXPECT_THAT(format, HasSubstr("field:char buf"));
144 }
145
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (CanOpenTracePipeRaw))146 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(CanOpenTracePipeRaw)) {
147 EXPECT_TRUE(ftrace_->OpenPipeForCpu(0));
148 }
149
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (Clock))150 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(Clock)) {
151 std::set<std::string> clocks = ftrace_->AvailableClocks();
152 EXPECT_THAT(clocks, Contains("local"));
153 EXPECT_THAT(clocks, Contains("global"));
154
155 EXPECT_TRUE(ftrace_->SetClock("global"));
156 EXPECT_EQ(ftrace_->GetClock(), "global");
157 EXPECT_TRUE(ftrace_->SetClock("local"));
158 EXPECT_EQ(ftrace_->GetClock(), "local");
159 }
160
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (CanSetBufferSize))161 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(CanSetBufferSize)) {
162 EXPECT_TRUE(ftrace_->SetCpuBufferSizeInPages(4ul));
163 EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n"); // (4096 * 4) / 1024
164 EXPECT_TRUE(ftrace_->SetCpuBufferSizeInPages(5ul));
165 EXPECT_EQ(ReadFile("buffer_size_kb"), "20\n"); // (4096 * 5) / 1024
166 }
167
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (FtraceControllerHardReset))168 TEST_F(FtraceProcfsIntegrationTest,
169 ANDROID_ONLY_TEST(FtraceControllerHardReset)) {
170 ftrace_->SetCpuBufferSizeInPages(4ul);
171 ftrace_->EnableTracing();
172 ftrace_->EnableEvent("sched", "sched_switch");
173 ftrace_->WriteTraceMarker("Hello, World!");
174
175 EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n");
176 EXPECT_EQ(ReadFile("tracing_on"), "1\n");
177 EXPECT_EQ(ReadFile("events/enable"), "X\n");
178
179 HardResetFtraceState();
180
181 EXPECT_EQ(ReadFile("buffer_size_kb"), "4\n");
182 EXPECT_EQ(ReadFile("tracing_on"), "0\n");
183 EXPECT_EQ(ReadFile("events/enable"), "0\n");
184 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello")));
185 }
186
TEST_F(FtraceProcfsIntegrationTest,ANDROID_ONLY_TEST (ReadEnabledEvents))187 TEST_F(FtraceProcfsIntegrationTest, ANDROID_ONLY_TEST(ReadEnabledEvents)) {
188 EXPECT_THAT(ftrace_->ReadEnabledEvents(), IsEmpty());
189
190 ftrace_->EnableEvent("sched", "sched_switch");
191 ftrace_->EnableEvent("kmem", "kmalloc");
192
193 EXPECT_THAT(ftrace_->ReadEnabledEvents(),
194 UnorderedElementsAre("sched/sched_switch", "kmem/kmalloc"));
195
196 ftrace_->DisableEvent("sched", "sched_switch");
197 ftrace_->DisableEvent("kmem", "kmalloc");
198
199 EXPECT_THAT(ftrace_->ReadEnabledEvents(), IsEmpty());
200 }
201
202 } // namespace
203 } // namespace perfetto
204