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