• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
112   TraceConfig trace_config;
113   trace_config.add_buffers()->set_size_kb(1024);
114   trace_config.set_duration_ms(3000);
115 
116   auto* ds_config = trace_config.add_data_sources()->mutable_config();
117   ds_config->set_name("linux.ftrace");
118   ds_config->set_target_buffer(0);
119 
120   protos::gen::FtraceConfig ftrace_config;
121   ftrace_config.add_ftrace_events("sched_switch");
122   ftrace_config.add_ftrace_events("bar");
123   ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
124 
125   helper.StartTracing(trace_config);
126   helper.WaitForTracingDisabled();
127 
128   helper.ReadData();
129   helper.WaitForReadData();
130 
131   const auto& packets = helper.trace();
132   ASSERT_GT(packets.size(), 0u);
133 
134   for (const auto& packet : packets) {
135     for (int ev = 0; ev < packet.ftrace_events().event_size(); ev++) {
136       ASSERT_TRUE(packet.ftrace_events()
137                       .event()[static_cast<size_t>(ev)]
138                       .has_sched_switch());
139     }
140   }
141 }
142 
TEST_F(PerfettoFtraceIntegrationTest,TestFtraceFlush)143 TEST_F(PerfettoFtraceIntegrationTest, TestFtraceFlush) {
144   base::TestTaskRunner task_runner;
145 
146   TestHelper helper(&task_runner);
147   helper.StartServiceIfRequired();
148 
149 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
150   ProbesProducerThread probes(GetTestProducerSockName());
151   probes.Connect();
152 #endif
153 
154   helper.ConnectConsumer();
155   helper.WaitForConsumerConnect();
156 
157   // Wait for the traced_probes service to connect. We want to start tracing
158   // only after it connects, otherwise we'll start a tracing session with 0
159   // producers connected (which is valid but not what we want here).
160   helper.WaitForDataSourceConnected("linux.ftrace");
161 
162   TraceConfig trace_config;
163   trace_config.add_buffers()->set_size_kb(32);
164   trace_config.set_duration_ms(kDefaultTestTimeoutMs);
165 
166   auto* ds_config = trace_config.add_data_sources()->mutable_config();
167   ds_config->set_name("linux.ftrace");
168 
169   protos::gen::FtraceConfig ftrace_config;
170   ftrace_config.add_ftrace_events("print");
171   ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
172 
173   helper.StartTracing(trace_config);
174 
175   // Wait for traced_probes to start.
176   helper.WaitFor([&] { return ftrace_procfs_->IsTracingEnabled(); }, "ftrace");
177 
178   // Do a first flush just to synchronize with the producer. The problem here
179   // is that, on a Linux workstation, the producer can take several seconds just
180   // to get to the point where it is fully ready. We use the flush ack as a
181   // synchronization point.
182   helper.FlushAndWait(kDefaultTestTimeoutMs);
183 
184   const char kMarker[] = "just_one_event";
185   EXPECT_TRUE(ftrace_procfs_->WriteTraceMarker(kMarker));
186 
187   // This is the real flush we are testing.
188   helper.FlushAndWait(kDefaultTestTimeoutMs);
189 
190   helper.DisableTracing();
191   helper.WaitForTracingDisabled(kDefaultTestTimeoutMs);
192 
193   helper.ReadData();
194   helper.WaitForReadData();
195 
196   int marker_found = 0;
197   for (const auto& packet : helper.trace()) {
198     for (int i = 0; i < packet.ftrace_events().event_size(); i++) {
199       const auto& ev = packet.ftrace_events().event()[static_cast<size_t>(i)];
200       if (ev.has_print() && ev.print().buf().find(kMarker) != std::string::npos)
201         marker_found++;
202     }
203   }
204   ASSERT_EQ(marker_found, 1);
205 }
206 
207 // Disable this test:
208 // 1. On cuttlefish (x86-kvm). It's too slow when running on GCE (b/171771440).
209 //    We cannot change the length of the production code in
210 //    CanReadKernelSymbolAddresses() to deal with it.
211 // 2. On user (i.e. non-userdebug) builds. As that doesn't work there by design.
212 // 3. On ARM builds, because they fail on our CI.
213 #if (PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && defined(__i386__)) || \
214     defined(__arm__)
215 #define MAYBE_KernelAddressSymbolization DISABLED_KernelAddressSymbolization
216 #else
217 #define MAYBE_KernelAddressSymbolization KernelAddressSymbolization
218 #endif
TEST_F(PerfettoFtraceIntegrationTest,MAYBE_KernelAddressSymbolization)219 TEST_F(PerfettoFtraceIntegrationTest, MAYBE_KernelAddressSymbolization) {
220   // On Android in-tree builds (TreeHugger): this test must always run to
221   // prevent selinux / property-related regressions. However it can run only on
222   // userdebug.
223   // On standalone builds and Linux, this can be optionally skipped because
224   // there it requires root to lower kptr_restrict.
225 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
226   if (!IsDebuggableBuild())
227     GTEST_SKIP();
228 #else
229   if (geteuid() != 0)
230     GTEST_SKIP();
231 #endif
232 
233   base::TestTaskRunner task_runner;
234 
235   TestHelper helper(&task_runner);
236   helper.StartServiceIfRequired();
237 
238 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
239   ProbesProducerThread probes(GetTestProducerSockName());
240   probes.Connect();
241 #endif
242 
243   helper.ConnectConsumer();
244   helper.WaitForConsumerConnect();
245 
246   TraceConfig trace_config;
247   trace_config.add_buffers()->set_size_kb(1024);
248 
249   auto* ds_config = trace_config.add_data_sources()->mutable_config();
250   ds_config->set_name("linux.ftrace");
251   protos::gen::FtraceConfig ftrace_cfg;
252   ftrace_cfg.set_symbolize_ksyms(true);
253   ftrace_cfg.set_initialize_ksyms_synchronously_for_testing(true);
254   ds_config->set_ftrace_config_raw(ftrace_cfg.SerializeAsString());
255 
256   helper.StartTracing(trace_config);
257 
258   // Synchronize with the ftrace data source. The kernel symbol map is loaded
259   // at this point.
260   helper.FlushAndWait(kDefaultTestTimeoutMs);
261   helper.DisableTracing();
262   helper.WaitForTracingDisabled();
263   helper.ReadData();
264   helper.WaitForReadData();
265 
266   const auto& packets = helper.trace();
267   ASSERT_GT(packets.size(), 0u);
268 
269   int symbols_parsed = -1;
270   for (const auto& packet : packets) {
271     if (!packet.has_ftrace_stats())
272       continue;
273     if (packet.ftrace_stats().phase() != protos::gen::FtraceStats::END_OF_TRACE)
274       continue;
275     symbols_parsed =
276         static_cast<int>(packet.ftrace_stats().kernel_symbols_parsed());
277   }
278   ASSERT_GT(symbols_parsed, 100);
279 }
280 
TEST_F(PerfettoFtraceIntegrationTest,ReportFtraceFailuresInStats)281 TEST_F(PerfettoFtraceIntegrationTest, ReportFtraceFailuresInStats) {
282   base::TestTaskRunner task_runner;
283 
284   TestHelper helper(&task_runner);
285   helper.StartServiceIfRequired();
286 
287 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
288   ProbesProducerThread probes(GetTestProducerSockName());
289   probes.Connect();
290 #endif
291 
292   helper.ConnectConsumer();
293   helper.WaitForConsumerConnect();
294 
295   // Wait for the traced_probes service to connect. We want to start tracing
296   // only after it connects, otherwise we'll start a tracing session with 0
297   // producers connected (which is valid but not what we want here).
298   helper.WaitForDataSourceConnected("linux.ftrace");
299 
300   TraceConfig trace_config;
301   trace_config.add_buffers()->set_size_kb(32);
302   trace_config.set_duration_ms(1);
303 
304   auto* ds_config = trace_config.add_data_sources()->mutable_config();
305   ds_config->set_name("linux.ftrace");
306 
307   protos::gen::FtraceConfig ftrace_config;
308   ftrace_config.add_ftrace_events("sched/sched_process_fork");    // Good.
309   ftrace_config.add_ftrace_events("sched/does_not_exist");        // Bad.
310   ftrace_config.add_ftrace_events("foobar/i_just_made_this_up");  // Bad.
311   ftrace_config.add_atrace_categories("madeup_atrace_cat");       // Bad.
312   ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString());
313 
314   helper.StartTracing(trace_config);
315   helper.WaitForTracingDisabled(kDefaultTestTimeoutMs);
316 
317   helper.ReadData();
318   helper.WaitForReadData();
319   const auto& packets = helper.trace();
320   ASSERT_GT(packets.size(), 0u);
321 
322   base::Optional<protos::gen::FtraceStats> stats;
323   for (const auto& packet : packets) {
324     if (!packet.has_ftrace_stats() ||
325         packet.ftrace_stats().phase() !=
326             protos::gen::FtraceStats::START_OF_TRACE) {
327       continue;
328     }
329     stats = packet.ftrace_stats();
330   }
331   ASSERT_TRUE(stats.has_value());
332   EXPECT_THAT(stats->unknown_ftrace_events(),
333               UnorderedElementsAreArray(
334                   {"sched/does_not_exist", "foobar/i_just_made_this_up"}));
335 
336   // Atrace is not available on Linux and on the O-based emulator on the CI.
337   if (base::FileExists("/system/bin/atrace")) {
338     EXPECT_THAT(stats->atrace_errors(), HasSubstr("madeup_atrace_cat"));
339   }
340 }
341 
342 }  // namespace perfetto
343 #endif  // OS_ANDROID || OS_LINUX
344