1 /*
2 * Copyright (C) 2022 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 "perfetto/base/build_config.h"
18
19 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
20 PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX)
21
22 #include "perfetto/base/build_config.h"
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/file_utils.h"
25 #include "perfetto/ext/base/scoped_file.h"
26 #include "perfetto/ext/base/string_utils.h"
27 #include "perfetto/ext/base/temp_file.h"
28 #include "perfetto/ext/base/utils.h"
29 #include "perfetto/ext/traced/traced.h"
30 #include "perfetto/ext/tracing/core/commit_data_request.h"
31 #include "perfetto/ext/tracing/core/trace_packet.h"
32 #include "perfetto/ext/tracing/core/tracing_service.h"
33 #include "perfetto/protozero/scattered_heap_buffer.h"
34 #include "perfetto/tracing/core/tracing_service_state.h"
35 #include "src/base/test/test_task_runner.h"
36 #include "src/base/test/utils.h"
37 #include "src/traced/probes/ftrace/ftrace_controller.h"
38 #include "src/traced/probes/ftrace/ftrace_procfs.h"
39 #include "test/gtest_and_gmock.h"
40 #include "test/test_helper.h"
41
42 #include "protos/perfetto/config/test_config.gen.h"
43 #include "protos/perfetto/config/trace_config.gen.h"
44 #include "protos/perfetto/trace/ftrace/ftrace.gen.h"
45 #include "protos/perfetto/trace/ftrace/ftrace_event.gen.h"
46 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h"
47 #include "protos/perfetto/trace/ftrace/ftrace_stats.gen.h"
48 #include "protos/perfetto/trace/perfetto/tracing_service_event.gen.h"
49 #include "protos/perfetto/trace/test_event.gen.h"
50 #include "protos/perfetto/trace/trace.gen.h"
51 #include "protos/perfetto/trace/trace_packet.gen.h"
52 #include "protos/perfetto/trace/trace_packet.pbzero.h"
53
54 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
55 #include "test/android_test_utils.h"
56 #endif
57
58 namespace perfetto {
59
60 namespace {
61
62 using ::testing::ContainsRegex;
63 using ::testing::Each;
64 using ::testing::ElementsAreArray;
65 using ::testing::HasSubstr;
66 using ::testing::Property;
67 using ::testing::SizeIs;
68 using ::testing::UnorderedElementsAreArray;
69
70 class PerfettoFtraceIntegrationTest : public ::testing::Test {
71 public:
SetUp()72 void SetUp() override {
73 ftrace_procfs_ = FtraceProcfs::CreateGuessingMountPoint();
74
75 // On android we do expect that tracefs is accessible, both in the case of
76 // running as part of traced/probes system daemons and shell. On Linux this is
77 // up to the system admin, don't hard fail.
78 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
79 if (!ftrace_procfs_) {
80 PERFETTO_ELOG(
81 "Cannot acces tracefs. On Linux you need to manually run `sudo chown "
82 "-R $USER /sys/kernel/tracing` to enable these tests. Skipping");
83 GTEST_SKIP();
84 } else {
85 // Recent kernels set tracing_on=1 by default. On Android this is
86 // disabled by initrc scripts. Be tolerant on Linux where we don't have
87 // that and force disable ftrace.
88 ftrace_procfs_->SetTracingOn(false);
89 }
90 #endif
91 }
92
93 std::unique_ptr<FtraceProcfs> ftrace_procfs_;
94 };
95
96 } // namespace
97
TEST_F(PerfettoFtraceIntegrationTest,TestFtraceProducer)98 TEST_F(PerfettoFtraceIntegrationTest, TestFtraceProducer) {
99 base::TestTaskRunner task_runner;
100
101 TestHelper helper(&task_runner);
102 helper.StartServiceIfRequired();
103
104 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
105 ProbesProducerThread probes(GetTestProducerSockName());
106 probes.Connect();
107 #endif
108
109 helper.ConnectConsumer();
110 helper.WaitForConsumerConnect();
111 helper.WaitForDataSourceConnected("linux.ftrace");
112
113 TraceConfig trace_config;
114 trace_config.add_buffers()->set_size_kb(64);
115 trace_config.set_duration_ms(3000);
116
117 auto* ds_config = trace_config.add_data_sources()->mutable_config();
118 ds_config->set_name("linux.ftrace");
119 ds_config->set_target_buffer(0);
120
121 protos::gen::FtraceConfig ftrace_config;
122 ftrace_config.add_ftrace_events("sched_switch");
123 ftrace_config.add_ftrace_events("bar");
124 ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
125
126 helper.StartTracing(trace_config);
127 helper.WaitForTracingDisabled();
128
129 helper.ReadData();
130 helper.WaitForReadData();
131
132 const auto& packets = helper.trace();
133 ASSERT_GT(packets.size(), 0u);
134
135 for (const auto& packet : packets) {
136 for (int ev = 0; ev < packet.ftrace_events().event_size(); ev++) {
137 ASSERT_TRUE(packet.ftrace_events()
138 .event()[static_cast<size_t>(ev)]
139 .has_sched_switch());
140 }
141 }
142 }
143
TEST_F(PerfettoFtraceIntegrationTest,TestFtraceFlush)144 TEST_F(PerfettoFtraceIntegrationTest, TestFtraceFlush) {
145 base::TestTaskRunner task_runner;
146
147 TestHelper helper(&task_runner);
148 helper.StartServiceIfRequired();
149
150 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
151 ProbesProducerThread probes(GetTestProducerSockName());
152 probes.Connect();
153 #endif
154
155 helper.ConnectConsumer();
156 helper.WaitForConsumerConnect();
157
158 // Wait for the traced_probes service to connect. We want to start tracing
159 // only after it connects, otherwise we'll start a tracing session with 0
160 // producers connected (which is valid but not what we want here).
161 helper.WaitForDataSourceConnected("linux.ftrace");
162
163 TraceConfig trace_config;
164 trace_config.add_buffers()->set_size_kb(32);
165 trace_config.set_duration_ms(kDefaultTestTimeoutMs);
166
167 auto* ds_config = trace_config.add_data_sources()->mutable_config();
168 ds_config->set_name("linux.ftrace");
169
170 protos::gen::FtraceConfig ftrace_config;
171 ftrace_config.add_ftrace_events("print");
172 ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
173
174 helper.StartTracing(trace_config);
175
176 // Wait for traced_probes to start.
177 helper.WaitFor([&] { return ftrace_procfs_->GetTracingOn(); }, "ftrace");
178
179 // Do a first flush just to synchronize with the producer. The problem here
180 // is that, on a Linux workstation, the producer can take several seconds just
181 // to get to the point where it is fully ready. We use the flush ack as a
182 // synchronization point.
183 helper.FlushAndWait(kDefaultTestTimeoutMs);
184
185 const char kMarker[] = "just_one_event";
186 EXPECT_TRUE(ftrace_procfs_->WriteTraceMarker(kMarker));
187
188 // This is the real flush we are testing.
189 helper.FlushAndWait(kDefaultTestTimeoutMs);
190
191 helper.DisableTracing();
192 helper.WaitForTracingDisabled(kDefaultTestTimeoutMs);
193
194 helper.ReadData();
195 helper.WaitForReadData();
196
197 int marker_found = 0;
198 for (const auto& packet : helper.trace()) {
199 for (int i = 0; i < packet.ftrace_events().event_size(); i++) {
200 const auto& ev = packet.ftrace_events().event()[static_cast<size_t>(i)];
201 if (ev.has_print() && ev.print().buf().find(kMarker) != std::string::npos)
202 marker_found++;
203 }
204 }
205 ASSERT_EQ(marker_found, 1);
206 }
207
208 // Disable this test:
209 // 1. On cuttlefish (x86-kvm). It's too slow when running on GCE (b/171771440).
210 // We cannot change the length of the production code in
211 // CanReadKernelSymbolAddresses() to deal with it.
212 // 2. On user (i.e. non-userdebug) builds. As that doesn't work there by design.
213 // 3. On ARM builds, because they fail on our CI.
214 #if (PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && defined(__i386__)) || \
215 defined(__arm__)
216 #define MAYBE_KernelAddressSymbolization DISABLED_KernelAddressSymbolization
217 #else
218 #define MAYBE_KernelAddressSymbolization KernelAddressSymbolization
219 #endif
TEST_F(PerfettoFtraceIntegrationTest,MAYBE_KernelAddressSymbolization)220 TEST_F(PerfettoFtraceIntegrationTest, MAYBE_KernelAddressSymbolization) {
221 // On Android in-tree builds (TreeHugger): this test must always run to
222 // prevent selinux / property-related regressions. However it can run only on
223 // userdebug.
224 // On standalone builds and Linux, this can be optionally skipped because
225 // there it requires root to lower kptr_restrict.
226 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
227 if (!IsDebuggableBuild())
228 GTEST_SKIP();
229 #else
230 if (geteuid() != 0)
231 GTEST_SKIP();
232 #endif
233
234 base::TestTaskRunner task_runner;
235
236 TestHelper helper(&task_runner);
237 helper.StartServiceIfRequired();
238
239 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
240 ProbesProducerThread probes(GetTestProducerSockName());
241 probes.Connect();
242 #endif
243
244 helper.ConnectConsumer();
245 helper.WaitForConsumerConnect();
246 helper.WaitForDataSourceConnected("linux.ftrace");
247
248 TraceConfig trace_config;
249 trace_config.add_buffers()->set_size_kb(64);
250
251 auto* ds_config = trace_config.add_data_sources()->mutable_config();
252 ds_config->set_name("linux.ftrace");
253 protos::gen::FtraceConfig ftrace_cfg;
254 ftrace_cfg.set_symbolize_ksyms(true);
255 ftrace_cfg.set_initialize_ksyms_synchronously_for_testing(true);
256 ds_config->set_ftrace_config_raw(ftrace_cfg.SerializeAsString());
257
258 helper.StartTracing(trace_config);
259
260 // Synchronize with the ftrace data source. The kernel symbol map is loaded
261 // at this point.
262 helper.WaitForAllDataSourceStarted();
263 helper.FlushAndWait(kDefaultTestTimeoutMs);
264 helper.DisableTracing();
265 helper.WaitForTracingDisabled();
266 helper.ReadData();
267 helper.WaitForReadData();
268
269 const auto& packets = helper.trace();
270 ASSERT_GT(packets.size(), 0u);
271
272 int symbols_parsed = -1;
273 for (const auto& packet : packets) {
274 if (!packet.has_ftrace_stats())
275 continue;
276 if (packet.ftrace_stats().phase() != protos::gen::FtraceStats::END_OF_TRACE)
277 continue;
278 symbols_parsed =
279 static_cast<int>(packet.ftrace_stats().kernel_symbols_parsed());
280 }
281 ASSERT_GT(symbols_parsed, 100);
282 }
283
TEST_F(PerfettoFtraceIntegrationTest,ReportFtraceFailuresInStats)284 TEST_F(PerfettoFtraceIntegrationTest, ReportFtraceFailuresInStats) {
285 base::TestTaskRunner task_runner;
286
287 TestHelper helper(&task_runner);
288 helper.StartServiceIfRequired();
289
290 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
291 ProbesProducerThread probes(GetTestProducerSockName());
292 probes.Connect();
293 #endif
294
295 helper.ConnectConsumer();
296 helper.WaitForConsumerConnect();
297
298 // Wait for the traced_probes service to connect. We want to start tracing
299 // only after it connects, otherwise we'll start a tracing session with 0
300 // producers connected (which is valid but not what we want here).
301 helper.WaitForDataSourceConnected("linux.ftrace");
302
303 TraceConfig trace_config;
304 TraceConfig::BufferConfig* buf = trace_config.add_buffers();
305 buf->set_size_kb(32);
306 buf->set_fill_policy(TraceConfig::BufferConfig::DISCARD);
307
308 auto* ds_config = trace_config.add_data_sources()->mutable_config();
309 ds_config->set_name("linux.ftrace");
310
311 protos::gen::FtraceConfig ftrace_config;
312 ftrace_config.add_ftrace_events("sched/sched_switch"); // Good.
313 ftrace_config.add_ftrace_events("sched/does_not_exist"); // Bad.
314 ftrace_config.add_ftrace_events("foobar/i_just_made_this_up"); // Bad.
315 ftrace_config.add_atrace_categories("madeup_atrace_cat"); // Bad.
316 ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
317
318 helper.StartTracing(trace_config);
319 helper.WaitForAllDataSourceStarted(kDefaultTestTimeoutMs);
320 helper.FlushAndWait(kDefaultTestTimeoutMs);
321 helper.DisableTracing();
322 helper.WaitForTracingDisabled(kDefaultTestTimeoutMs);
323
324 helper.ReadData();
325 helper.WaitForReadData();
326 const auto& packets = helper.trace();
327 ASSERT_GT(packets.size(), 0u);
328
329 std::optional<protos::gen::FtraceStats> stats;
330 for (const auto& packet : packets) {
331 if (!packet.has_ftrace_stats() ||
332 packet.ftrace_stats().phase() !=
333 protos::gen::FtraceStats::START_OF_TRACE) {
334 continue;
335 }
336 stats = packet.ftrace_stats();
337 }
338 ASSERT_TRUE(stats.has_value());
339 EXPECT_THAT(stats->unknown_ftrace_events(),
340 UnorderedElementsAreArray(
341 {"sched/does_not_exist", "foobar/i_just_made_this_up"}));
342
343 // Atrace is not available on Linux and on the O-based emulator on the CI.
344 if (base::FileExists("/system/bin/atrace")) {
345 EXPECT_THAT(stats->atrace_errors(), HasSubstr("madeup_atrace_cat"));
346 }
347 }
348
349 } // namespace perfetto
350 #endif // OS_ANDROID || OS_LINUX
351