1 /*
2 * Copyright (C) 2019 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 #include <sys/wait.h>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/android_utils.h"
24 #include "perfetto/ext/base/string_utils.h"
25 #include "perfetto/tracing/core/data_source_config.h"
26
27 #include "src/base/test/test_task_runner.h"
28 #include "test/android_test_utils.h"
29 #include "test/cts/heapprofd_test_helper.h"
30 #include "test/gtest_and_gmock.h"
31 #include "test/test_helper.h"
32
33 #include "protos/perfetto/config/profiling/java_hprof_config.gen.h"
34 #include "protos/perfetto/trace/profiling/heap_graph.gen.h"
35 #include "protos/perfetto/trace/profiling/profile_common.gen.h"
36 #include "protos/perfetto/trace/trace_packet.gen.h"
37
38 namespace perfetto {
39 namespace {
40
41 constexpr uint64_t kTestSamplingInterval = 4096;
42
43 // Activity that runs a java thread that repeatedly constructs small java
44 // objects.
45 static char kJavaAllocActivity[] = "JavaAllocActivity";
46
47 // Even though ART is a mainline module, there are dependencies on perfetto for
48 // OOM heap dumps to work correctly.
SupportsOomHeapDump()49 bool SupportsOomHeapDump() {
50 auto sdk = base::StringToInt32(base::GetAndroidProp("ro.build.version.sdk"));
51 if (sdk && *sdk >= 34) {
52 PERFETTO_LOG("SDK supports OOME heap dumps");
53 return true;
54 }
55 if (base::GetAndroidProp("ro.build.version.codename") == "UpsideDownCake") {
56 PERFETTO_LOG("Codename supports OOME heap dumps");
57 return true;
58 }
59 PERFETTO_LOG("OOME heap dumps not supported");
60 return false;
61 }
62
63 // TODO(rsavitski): deduplicate with heapprofd_test_helper.cc:ProfileRuntime.
ProfileHeapGraphRuntime(std::string app_name)64 std::vector<protos::gen::TracePacket> ProfileHeapGraphRuntime(
65 std::string app_name) {
66 base::TestTaskRunner task_runner;
67
68 // (re)start the target app's main activity
69 if (IsAppRunning(app_name)) {
70 StopApp(app_name, "old.app.stopped", &task_runner);
71 task_runner.RunUntilCheckpoint("old.app.stopped", 10000 /*ms*/);
72 }
73 StartAppActivity(app_name, "NoopActivity", "target.app.running", &task_runner,
74 /*delay_ms=*/100);
75 task_runner.RunUntilCheckpoint("target.app.running", 10000 /*ms*/);
76 // If we try to dump too early in app initialization, we sometimes deadlock.
77 sleep(1);
78
79 // set up tracing
80 TestHelper helper(&task_runner);
81 helper.ConnectConsumer();
82 helper.WaitForConsumerConnect();
83
84 TraceConfig trace_config;
85 trace_config.add_buffers()->set_size_kb(40 * 1024);
86 trace_config.set_duration_ms(3000);
87 trace_config.set_data_source_stop_timeout_ms(20000);
88 trace_config.set_unique_session_name(RandomSessionName().c_str());
89
90 auto* ds_config = trace_config.add_data_sources()->mutable_config();
91 ds_config->set_name("android.java_hprof");
92 ds_config->set_target_buffer(0);
93
94 protos::gen::JavaHprofConfig java_hprof_config;
95 java_hprof_config.add_process_cmdline(app_name.c_str());
96 ds_config->set_java_hprof_config_raw(java_hprof_config.SerializeAsString());
97
98 // start tracing
99 helper.StartTracing(trace_config);
100 helper.WaitForTracingDisabled();
101 helper.ReadData();
102 helper.WaitForReadData();
103 PERFETTO_CHECK(IsAppRunning(app_name));
104 StopApp(app_name, "new.app.stopped", &task_runner);
105 task_runner.RunUntilCheckpoint("new.app.stopped", 10000 /*ms*/);
106 return helper.trace();
107 }
108
TriggerOomHeapDump(std::string app_name,std::string heap_dump_target)109 std::vector<protos::gen::TracePacket> TriggerOomHeapDump(
110 std::string app_name,
111 std::string heap_dump_target) {
112 base::TestTaskRunner task_runner;
113
114 // (re)start the target app's main activity
115 if (IsAppRunning(app_name)) {
116 StopApp(app_name, "old.app.stopped", &task_runner);
117 task_runner.RunUntilCheckpoint("old.app.stopped", 10000 /*ms*/);
118 }
119
120 // set up tracing
121 TestHelper helper(&task_runner);
122 helper.ConnectConsumer();
123 helper.WaitForConsumerConnect();
124
125 TraceConfig trace_config;
126 trace_config.add_buffers()->set_size_kb(40 * 1024);
127 trace_config.set_unique_session_name(RandomSessionName().c_str());
128 trace_config.set_data_source_stop_timeout_ms(60000);
129
130 auto* trigger_config = trace_config.mutable_trigger_config();
131 trigger_config->set_trigger_mode(
132 perfetto::protos::gen::TraceConfig::TriggerConfig::START_TRACING);
133 trigger_config->set_trigger_timeout_ms(60000);
134 auto* oom_trigger = trigger_config->add_triggers();
135 oom_trigger->set_name("com.android.telemetry.art-outofmemory");
136 oom_trigger->set_stop_delay_ms(1000);
137
138 auto* ds_config = trace_config.add_data_sources()->mutable_config();
139 ds_config->set_name("android.java_hprof.oom");
140 ds_config->set_target_buffer(0);
141
142 protos::gen::JavaHprofConfig java_hprof_config;
143 java_hprof_config.add_process_cmdline(heap_dump_target.c_str());
144 ds_config->set_java_hprof_config_raw(java_hprof_config.SerializeAsString());
145
146 // start tracing
147 helper.StartTracing(trace_config);
148 StartAppActivity(app_name, "JavaOomActivity", "target.app.running",
149 &task_runner,
150 /*delay_ms=*/100);
151 task_runner.RunUntilCheckpoint("target.app.running", 10000 /*ms*/);
152
153 if (SupportsOomHeapDump()) {
154 helper.WaitForTracingDisabled();
155 helper.ReadData();
156 helper.WaitForReadData();
157 }
158
159 PERFETTO_CHECK(IsAppRunning(app_name));
160 StopApp(app_name, "new.app.stopped", &task_runner);
161 task_runner.RunUntilCheckpoint("new.app.stopped", 10000 /*ms*/);
162 return helper.trace();
163 }
164
AssertGraphPresent(std::vector<protos::gen::TracePacket> packets)165 void AssertGraphPresent(std::vector<protos::gen::TracePacket> packets) {
166 ASSERT_GT(packets.size(), 0u);
167
168 size_t objects = 0;
169 size_t roots = 0;
170 for (const auto& packet : packets) {
171 objects += static_cast<size_t>(packet.heap_graph().objects_size());
172 roots += static_cast<size_t>(packet.heap_graph().roots_size());
173 }
174 ASSERT_GT(objects, 0u);
175 ASSERT_GT(roots, 0u);
176 }
177
AssertNoProfileContents(std::vector<protos::gen::TracePacket> packets)178 void AssertNoProfileContents(std::vector<protos::gen::TracePacket> packets) {
179 // If profile packets are present, they must be empty.
180 for (const auto& packet : packets) {
181 ASSERT_EQ(packet.heap_graph().roots_size(), 0);
182 ASSERT_EQ(packet.heap_graph().objects_size(), 0);
183 ASSERT_EQ(packet.heap_graph().types_size(), 0);
184 ASSERT_EQ(packet.heap_graph().field_names_size(), 0);
185 }
186 }
187
188 //
189 // Tests for the NDK custom allocator API, as used by ART's java heap sampler.
190 //
191
TEST(HeapprofdJavaCtsTest,ArtHeapCustomAllocatorRuntime)192 TEST(HeapprofdJavaCtsTest, ArtHeapCustomAllocatorRuntime) {
193 std::string app_name = "android.perfetto.cts.app.debuggable";
194 const auto& packets =
195 ProfileRuntime(app_name, kJavaAllocActivity, kTestSamplingInterval,
196 /*heap_names=*/{"com.android.art"});
197 AssertHasSampledAllocs(packets);
198 StopApp(app_name);
199 }
200
TEST(HeapprofdJavaCtsTest,ArtHeapCustomAllocatorStartup)201 TEST(HeapprofdJavaCtsTest, ArtHeapCustomAllocatorStartup) {
202 std::string app_name = "android.perfetto.cts.app.debuggable";
203 const auto& packets =
204 ProfileStartup(app_name, kJavaAllocActivity, kTestSamplingInterval,
205 /*heap_names=*/{"com.android.art"});
206 AssertHasSampledAllocs(packets);
207 StopApp(app_name);
208 }
209
210 //
211 // Tests for the java heap graph plugin in ART.
212 //
213
TEST(HeapprofdJavaCtsTest,DebuggableAppRuntime)214 TEST(HeapprofdJavaCtsTest, DebuggableAppRuntime) {
215 std::string app_name = "android.perfetto.cts.app.debuggable";
216 const auto& packets = ProfileHeapGraphRuntime(app_name);
217 AssertGraphPresent(packets);
218 }
219
TEST(HeapprofdJavaCtsTest,ProfileableAppRuntime)220 TEST(HeapprofdJavaCtsTest, ProfileableAppRuntime) {
221 std::string app_name = "android.perfetto.cts.app.profileable";
222 const auto& packets = ProfileHeapGraphRuntime(app_name);
223 AssertGraphPresent(packets);
224 }
225
TEST(HeapprofdJavaCtsTest,ReleaseAppRuntime)226 TEST(HeapprofdJavaCtsTest, ReleaseAppRuntime) {
227 std::string app_name = "android.perfetto.cts.app.release";
228 const auto& packets = ProfileHeapGraphRuntime(app_name);
229
230 if (!IsUserBuild())
231 AssertGraphPresent(packets);
232 else
233 AssertNoProfileContents(packets);
234 }
235
TEST(HeapprofdJavaCtsTest,DebuggableAppRuntimeByPid)236 TEST(HeapprofdJavaCtsTest, DebuggableAppRuntimeByPid) {
237 std::string app_name = "android.perfetto.cts.app.debuggable";
238
239 base::TestTaskRunner task_runner;
240
241 // (re)start the target app's main activity
242 if (IsAppRunning(app_name)) {
243 StopApp(app_name, "old.app.stopped", &task_runner);
244 task_runner.RunUntilCheckpoint("old.app.stopped", 10000 /*ms*/);
245 }
246 StartAppActivity(app_name, "NoopActivity", "target.app.running", &task_runner,
247 /*delay_ms=*/100);
248 task_runner.RunUntilCheckpoint("target.app.running", 10000 /*ms*/);
249 // If we try to dump too early in app initialization, we sometimes deadlock.
250 sleep(1);
251
252 int target_pid = PidForProcessName(app_name);
253 ASSERT_NE(target_pid, -1);
254
255 // set up tracing
256 TestHelper helper(&task_runner);
257 helper.ConnectConsumer();
258 helper.WaitForConsumerConnect();
259
260 TraceConfig trace_config;
261 trace_config.add_buffers()->set_size_kb(40 * 1024);
262 trace_config.set_duration_ms(3000);
263 trace_config.set_data_source_stop_timeout_ms(20000);
264 trace_config.set_unique_session_name(RandomSessionName().c_str());
265
266 auto* ds_config = trace_config.add_data_sources()->mutable_config();
267 ds_config->set_name("android.java_hprof");
268 ds_config->set_target_buffer(0);
269
270 protos::gen::JavaHprofConfig java_hprof_config;
271 java_hprof_config.add_pid(static_cast<uint64_t>(target_pid));
272 ds_config->set_java_hprof_config_raw(java_hprof_config.SerializeAsString());
273
274 // start tracing
275 helper.StartTracing(trace_config);
276 helper.WaitForTracingDisabled();
277 helper.ReadData();
278 helper.WaitForReadData();
279 PERFETTO_CHECK(IsAppRunning(app_name));
280 StopApp(app_name, "new.app.stopped", &task_runner);
281 task_runner.RunUntilCheckpoint("new.app.stopped", 10000 /*ms*/);
282
283 const auto& packets = helper.trace();
284 AssertGraphPresent(packets);
285 }
286
TEST(HeapprofdJavaCtsTest,DebuggableAppOom)287 TEST(HeapprofdJavaCtsTest, DebuggableAppOom) {
288 std::string app_name = "android.perfetto.cts.app.debuggable";
289 const auto& packets = TriggerOomHeapDump(app_name, "*");
290 if (SupportsOomHeapDump()) {
291 AssertGraphPresent(packets);
292 }
293 }
294
TEST(HeapprofdJavaCtsTest,ProfileableAppOom)295 TEST(HeapprofdJavaCtsTest, ProfileableAppOom) {
296 std::string app_name = "android.perfetto.cts.app.profileable";
297 const auto& packets = TriggerOomHeapDump(app_name, "*");
298 if (SupportsOomHeapDump()) {
299 AssertGraphPresent(packets);
300 }
301 }
302
TEST(HeapprofdJavaCtsTest,ReleaseAppOom)303 TEST(HeapprofdJavaCtsTest, ReleaseAppOom) {
304 std::string app_name = "android.perfetto.cts.app.release";
305 const auto& packets = TriggerOomHeapDump(app_name, "*");
306 if (IsUserBuild()) {
307 AssertNoProfileContents(packets);
308 } else if (SupportsOomHeapDump()) {
309 AssertGraphPresent(packets);
310 }
311 }
312
TEST(HeapprofdJavaCtsTest,DebuggableAppOomNotSelected)313 TEST(HeapprofdJavaCtsTest, DebuggableAppOomNotSelected) {
314 std::string app_name = "android.perfetto.cts.app.debuggable";
315 const auto& packets = TriggerOomHeapDump(app_name, "not.this.app");
316 AssertNoProfileContents(packets);
317 }
318
319 } // namespace
320 } // namespace perfetto
321