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/tracing/core/data_source_config.h"
24 #include "src/base/test/test_task_runner.h"
25 #include "test/android_test_utils.h"
26 #include "test/gtest_and_gmock.h"
27 #include "test/test_helper.h"
28 
29 #include "protos/perfetto/config/profiling/java_hprof_config.gen.h"
30 #include "protos/perfetto/trace/profiling/heap_graph.gen.h"
31 #include "protos/perfetto/trace/profiling/profile_common.gen.h"
32 #include "protos/perfetto/trace/trace_packet.gen.h"
33 
34 namespace perfetto {
35 namespace {
36 
RandomSessionName()37 std::string RandomSessionName() {
38   std::random_device rd;
39   std::default_random_engine generator(rd());
40   std::uniform_int_distribution<char> distribution('a', 'z');
41 
42   constexpr size_t kSessionNameLen = 20;
43   std::string result(kSessionNameLen, '\0');
44   for (size_t i = 0; i < kSessionNameLen; ++i)
45     result[i] = distribution(generator);
46   return result;
47 }
48 
ProfileRuntime(std::string app_name)49 std::vector<protos::gen::TracePacket> ProfileRuntime(std::string app_name) {
50   base::TestTaskRunner task_runner;
51 
52   // (re)start the target app's main activity
53   if (IsAppRunning(app_name)) {
54     StopApp(app_name, "old.app.stopped", &task_runner);
55     task_runner.RunUntilCheckpoint("old.app.stopped", 1000 /*ms*/);
56   }
57   StartAppActivity(app_name, "MainActivity", "target.app.running", &task_runner,
58                    /*delay_ms=*/100);
59   task_runner.RunUntilCheckpoint("target.app.running", 1000 /*ms*/);
60   // If we try to dump too early in app initialization, we sometimes deadlock.
61   sleep(1);
62 
63   // set up tracing
64   TestHelper helper(&task_runner);
65   helper.ConnectConsumer();
66   helper.WaitForConsumerConnect();
67 
68   TraceConfig trace_config;
69   trace_config.add_buffers()->set_size_kb(40 * 1024);
70   trace_config.set_duration_ms(6000);
71   trace_config.set_unique_session_name(RandomSessionName().c_str());
72 
73   auto* ds_config = trace_config.add_data_sources()->mutable_config();
74   ds_config->set_name("android.java_hprof");
75   ds_config->set_target_buffer(0);
76 
77   protos::gen::JavaHprofConfig java_hprof_config;
78   java_hprof_config.add_process_cmdline(app_name.c_str());
79   ds_config->set_java_hprof_config_raw(java_hprof_config.SerializeAsString());
80 
81   // start tracing
82   helper.StartTracing(trace_config);
83   helper.WaitForTracingDisabled();
84   helper.ReadData();
85   helper.WaitForReadData();
86   PERFETTO_CHECK(IsAppRunning(app_name));
87   StopApp(app_name, "new.app.stopped", &task_runner);
88   task_runner.RunUntilCheckpoint("new.app.stopped", 1000 /*ms*/);
89   return helper.trace();
90 }
91 
AssertGraphPresent(std::vector<protos::gen::TracePacket> packets)92 void AssertGraphPresent(std::vector<protos::gen::TracePacket> packets) {
93   ASSERT_GT(packets.size(), 0u);
94 
95   size_t objects = 0;
96   size_t roots = 0;
97   for (const auto& packet : packets) {
98     objects += static_cast<size_t>(packet.heap_graph().objects_size());
99     roots += static_cast<size_t>(packet.heap_graph().roots_size());
100   }
101   ASSERT_GT(objects, 0u);
102   ASSERT_GT(roots, 0u);
103 }
104 
AssertNoProfileContents(std::vector<protos::gen::TracePacket> packets)105 void AssertNoProfileContents(std::vector<protos::gen::TracePacket> packets) {
106   // If profile packets are present, they must be empty.
107   for (const auto& packet : packets) {
108     ASSERT_EQ(packet.heap_graph().roots_size(), 0);
109     ASSERT_EQ(packet.heap_graph().objects_size(), 0);
110     ASSERT_EQ(packet.heap_graph().types_size(), 0);
111     ASSERT_EQ(packet.heap_graph().field_names_size(), 0);
112   }
113 }
114 
TEST(HeapprofdJavaCtsTest,DebuggableAppRuntime)115 TEST(HeapprofdJavaCtsTest, DebuggableAppRuntime) {
116   std::string app_name = "android.perfetto.cts.app.debuggable";
117   const auto& packets = ProfileRuntime(app_name);
118   AssertGraphPresent(packets);
119 }
120 
TEST(HeapprofdJavaCtsTest,ProfileableAppRuntime)121 TEST(HeapprofdJavaCtsTest, ProfileableAppRuntime) {
122   std::string app_name = "android.perfetto.cts.app.profileable";
123   const auto& packets = ProfileRuntime(app_name);
124   AssertGraphPresent(packets);
125 }
126 
TEST(HeapprofdJavaCtsTest,ReleaseAppRuntime)127 TEST(HeapprofdJavaCtsTest, ReleaseAppRuntime) {
128   std::string app_name = "android.perfetto.cts.app.release";
129   const auto& packets = ProfileRuntime(app_name);
130 
131   if (!IsUserBuild())
132     AssertGraphPresent(packets);
133   else
134     AssertNoProfileContents(packets);
135 }
136 
137 }  // namespace
138 }  // namespace perfetto
139