1 /*
2 * Copyright (C) 2020 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 <stdlib.h>
18 #include <sys/types.h>
19
20 #include <string>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/android_utils.h"
24 #include "perfetto/tracing/core/data_source_config.h"
25 #include "src/base/test/test_task_runner.h"
26 #include "test/android_test_utils.h"
27 #include "test/gtest_and_gmock.h"
28 #include "test/test_helper.h"
29
30 #include "protos/perfetto/config/profiling/perf_event_config.gen.h"
31 #include "protos/perfetto/trace/profiling/profile_common.gen.h"
32 #include "protos/perfetto/trace/profiling/profile_packet.gen.h"
33 #include "protos/perfetto/trace/trace_packet.gen.h"
34
35 namespace perfetto {
36 namespace {
37
38 // Skip these tests if the device in question doesn't have the necessary kernel
39 // LSM hooks in perf_event_open. This comes up when a device with an older
40 // kernel upgrades to R.
HasPerfLsmHooks()41 bool HasPerfLsmHooks() {
42 return base::GetAndroidProp("sys.init.perf_lsm_hooks") == "1";
43 }
44
RandomSessionName()45 std::string RandomSessionName() {
46 std::random_device rd;
47 std::default_random_engine generator(rd());
48 std::uniform_int_distribution<> distribution('a', 'z');
49
50 constexpr size_t kSessionNameLen = 20;
51 std::string result(kSessionNameLen, '\0');
52 for (size_t i = 0; i < kSessionNameLen; ++i)
53 result[i] = static_cast<char>(distribution(generator));
54 return result;
55 }
56
ProfileSystemWide(std::string app_name)57 std::vector<protos::gen::TracePacket> ProfileSystemWide(std::string app_name) {
58 base::TestTaskRunner task_runner;
59
60 // (re)start the target app's main activity
61 if (IsAppRunning(app_name)) {
62 StopApp(app_name, "old.app.stopped", &task_runner);
63 task_runner.RunUntilCheckpoint("old.app.stopped", 10000 /*ms*/);
64 }
65 StartAppActivity(app_name, "BusyWaitActivity", "target.app.running",
66 &task_runner,
67 /*delay_ms=*/100);
68 task_runner.RunUntilCheckpoint("target.app.running", 10000 /*ms*/);
69
70 // set up tracing
71 TestHelper helper(&task_runner);
72 helper.ConnectConsumer();
73 helper.WaitForConsumerConnect();
74
75 TraceConfig trace_config;
76 trace_config.add_buffers()->set_size_kb(20 * 1024);
77 trace_config.set_duration_ms(3000);
78 trace_config.set_data_source_stop_timeout_ms(8000);
79 trace_config.set_unique_session_name(RandomSessionName().c_str());
80
81 auto* ds_config = trace_config.add_data_sources()->mutable_config();
82 ds_config->set_name("linux.perf");
83 ds_config->set_target_buffer(0);
84
85 protos::gen::PerfEventConfig perf_config;
86
87 perf_config.set_all_cpus(true);
88 perf_config.set_sampling_frequency(10); // Hz
89 ds_config->set_perf_event_config_raw(perf_config.SerializeAsString());
90
91 // start tracing
92 helper.StartTracing(trace_config);
93 helper.WaitForTracingDisabled(15000 /*ms*/);
94 helper.ReadData();
95 helper.WaitForReadData();
96
97 return helper.trace();
98 }
99
AssertHasSampledStacksForPid(std::vector<protos::gen::TracePacket> packets,int pid)100 void AssertHasSampledStacksForPid(std::vector<protos::gen::TracePacket> packets,
101 int pid) {
102 uint32_t target_pid = static_cast<uint32_t>(pid);
103 ASSERT_GT(packets.size(), 0u);
104
105 int total_perf_packets = 0;
106 int lost_records_packets = 0;
107 int full_samples = 0;
108 int target_samples = 0;
109 int target_skipped_samples = 0;
110 for (const auto& packet : packets) {
111 if (!packet.has_perf_sample())
112 continue;
113
114 total_perf_packets++;
115 EXPECT_GT(packet.timestamp(), 0u) << "all packets should have a timestamp";
116 const auto& sample = packet.perf_sample();
117 if (sample.has_kernel_records_lost()) {
118 lost_records_packets++;
119 continue;
120 }
121 if (sample.has_sample_skipped_reason()) {
122 if (sample.pid() == target_pid)
123 target_skipped_samples++;
124 continue;
125 }
126
127 full_samples++;
128 EXPECT_GT(sample.tid(), 0u);
129 EXPECT_GT(sample.callstack_iid(), 0u);
130
131 if (sample.pid() == target_pid)
132 target_samples++;
133 }
134
135 EXPECT_GT(target_samples, 0)
136 << "target_pid: " << target_pid << ", packets.size(): " << packets.size()
137 << ", total_perf_packets: " << total_perf_packets
138 << ", full_samples: " << full_samples
139 << ", lost_records_packets: " << lost_records_packets
140 << ", target_skipped_samples: " << target_skipped_samples << "\n";
141 }
142
AssertNoStacksForPid(std::vector<protos::gen::TracePacket> packets,int pid)143 void AssertNoStacksForPid(std::vector<protos::gen::TracePacket> packets,
144 int pid) {
145 uint32_t target_pid = static_cast<uint32_t>(pid);
146 // The process can still be sampled, but the stacks should be discarded
147 // without unwinding.
148 for (const auto& packet : packets) {
149 if (packet.perf_sample().pid() == target_pid) {
150 EXPECT_EQ(packet.perf_sample().callstack_iid(), 0u);
151 EXPECT_TRUE(packet.perf_sample().has_sample_skipped_reason());
152 }
153 }
154 }
155
TEST(TracedPerfCtsTest,SystemWideDebuggableApp)156 TEST(TracedPerfCtsTest, SystemWideDebuggableApp) {
157 if (!HasPerfLsmHooks())
158 GTEST_SKIP() << "skipped due to lack of perf_event_open LSM hooks";
159
160 std::string app_name = "android.perfetto.cts.app.debuggable";
161 const auto& packets = ProfileSystemWide(app_name);
162 int app_pid = PidForProcessName(app_name);
163 ASSERT_GT(app_pid, 0) << "failed to find pid for target process";
164
165 AssertHasSampledStacksForPid(packets, app_pid);
166 PERFETTO_CHECK(IsAppRunning(app_name));
167 StopApp(app_name);
168 }
169
TEST(TracedPerfCtsTest,SystemWideProfileableApp)170 TEST(TracedPerfCtsTest, SystemWideProfileableApp) {
171 if (!HasPerfLsmHooks())
172 GTEST_SKIP() << "skipped due to lack of perf_event_open LSM hooks";
173
174 std::string app_name = "android.perfetto.cts.app.profileable";
175 const auto& packets = ProfileSystemWide(app_name);
176 int app_pid = PidForProcessName(app_name);
177 ASSERT_GT(app_pid, 0) << "failed to find pid for target process";
178
179 AssertHasSampledStacksForPid(packets, app_pid);
180 PERFETTO_CHECK(IsAppRunning(app_name));
181 StopApp(app_name);
182 }
183
TEST(TracedPerfCtsTest,SystemWideNonProfileableApp)184 TEST(TracedPerfCtsTest, SystemWideNonProfileableApp) {
185 if (!HasPerfLsmHooks())
186 GTEST_SKIP() << "skipped due to lack of perf_event_open LSM hooks";
187
188 std::string app_name = "android.perfetto.cts.app.nonprofileable";
189 const auto& packets = ProfileSystemWide(app_name);
190 int app_pid = PidForProcessName(app_name);
191 ASSERT_GT(app_pid, 0) << "failed to find pid for target process";
192
193 if (!IsUserBuild())
194 AssertHasSampledStacksForPid(packets, app_pid);
195 else
196 AssertNoStacksForPid(packets, app_pid);
197 PERFETTO_CHECK(IsAppRunning(app_name));
198 StopApp(app_name);
199 }
200
TEST(TracedPerfCtsTest,SystemWideReleaseApp)201 TEST(TracedPerfCtsTest, SystemWideReleaseApp) {
202 if (!HasPerfLsmHooks())
203 GTEST_SKIP() << "skipped due to lack of perf_event_open LSM hooks";
204
205 std::string app_name = "android.perfetto.cts.app.release";
206 const auto& packets = ProfileSystemWide(app_name);
207 int app_pid = PidForProcessName(app_name);
208 ASSERT_GT(app_pid, 0) << "failed to find pid for target process";
209
210 if (!IsUserBuild())
211 AssertHasSampledStacksForPid(packets, app_pid);
212 else
213 AssertNoStacksForPid(packets, app_pid);
214
215 PERFETTO_CHECK(IsAppRunning(app_name));
216 StopApp(app_name);
217 }
218
219 } // namespace
220 } // namespace perfetto
221