blob: 23bba737238d92bfea12246cc48b36f8ccd0eddc [file] [log] [blame]
Florian Mayere8599d42019-10-14 14:58:15 +01001/*
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/cts/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.pb.h"
30
31namespace perfetto {
32namespace {
33
34std::vector<protos::TracePacket> ProfileRuntime(std::string app_name) {
35 base::TestTaskRunner task_runner;
36
37 // (re)start the target app's main activity
38 if (IsAppRunning(app_name)) {
39 StopApp(app_name, "old.app.stopped", &task_runner);
40 task_runner.RunUntilCheckpoint("old.app.stopped", 1000 /*ms*/);
41 }
42 StartAppActivity(app_name, "target.app.running", &task_runner,
43 /*delay_ms=*/100);
44 task_runner.RunUntilCheckpoint("target.app.running", 1000 /*ms*/);
Florian Mayer710ac8b2019-10-28 17:33:04 +000045 // TODO(b/143467832): Remove this workaround.
46 // If we try to dump too early in app initialization, we sometimes deadlock.
47 sleep(1);
Florian Mayere8599d42019-10-14 14:58:15 +010048
49 // set up tracing
50 TestHelper helper(&task_runner);
51 helper.ConnectConsumer();
52 helper.WaitForConsumerConnect();
53
54 TraceConfig trace_config;
Florian Mayer9ae3d742019-10-25 15:00:35 +010055 trace_config.add_buffers()->set_size_kb(20 * 1024);
Florian Mayer626e47c2019-10-18 14:46:15 +010056 trace_config.set_duration_ms(6000);
Florian Mayere8599d42019-10-14 14:58:15 +010057
58 auto* ds_config = trace_config.add_data_sources()->mutable_config();
59 ds_config->set_name("android.java_hprof");
60 ds_config->set_target_buffer(0);
61
62 protos::JavaHprofConfig java_hprof_config;
63 java_hprof_config.add_process_cmdline(app_name.c_str());
64 ds_config->set_java_hprof_config_raw(java_hprof_config.SerializeAsString());
65
66 // start tracing
67 helper.StartTracing(trace_config);
68 helper.WaitForTracingDisabled(10000 /*ms*/);
69 helper.ReadData();
70 helper.WaitForReadData();
Florian Mayer9ae3d742019-10-25 15:00:35 +010071 StopApp(app_name, "new.app.stopped", &task_runner);
72 task_runner.RunUntilCheckpoint("new.app.stopped", 1000 /*ms*/);
Florian Mayere8599d42019-10-14 14:58:15 +010073 return helper.trace();
74}
75
76void AssertGraphPresent(std::vector<protos::TracePacket> packets) {
Primiano Tuccifbf4a732019-12-11 00:32:15 +000077 ASSERT_GT(packets.size(), 0u);
Florian Mayere8599d42019-10-14 14:58:15 +010078
79 size_t objects = 0;
Florian Mayer6e158442019-10-14 15:18:38 +010080 size_t roots = 0;
81 for (const auto& packet : packets) {
Primiano Tuccifbf4a732019-12-11 00:32:15 +000082 objects += static_cast<size_t>(packet.heap_graph().objects_size());
83 roots += static_cast<size_t>(packet.heap_graph().roots_size());
Florian Mayer6e158442019-10-14 15:18:38 +010084 }
Primiano Tuccifbf4a732019-12-11 00:32:15 +000085 ASSERT_GT(objects, 0u);
86 ASSERT_GT(roots, 0u);
Florian Mayere8599d42019-10-14 14:58:15 +010087}
88
89void AssertNoProfileContents(std::vector<protos::TracePacket> packets) {
90 // If profile packets are present, they must be empty.
91 for (const auto& packet : packets) {
Florian Mayer6e158442019-10-14 15:18:38 +010092 ASSERT_EQ(packet.heap_graph().roots_size(), 0);
Florian Mayere8599d42019-10-14 14:58:15 +010093 ASSERT_EQ(packet.heap_graph().objects_size(), 0);
94 ASSERT_EQ(packet.heap_graph().type_names_size(), 0);
95 ASSERT_EQ(packet.heap_graph().field_names_size(), 0);
96 }
97}
98
Florian Mayer710ac8b2019-10-28 17:33:04 +000099TEST(HeapprofdJavaCtsTest, DebuggableAppRuntime) {
Florian Mayere8599d42019-10-14 14:58:15 +0100100 std::string app_name = "android.perfetto.cts.app.debuggable";
101 const auto& packets = ProfileRuntime(app_name);
102 AssertGraphPresent(packets);
Florian Mayere8599d42019-10-14 14:58:15 +0100103}
104
Florian Mayer710ac8b2019-10-28 17:33:04 +0000105TEST(HeapprofdJavaCtsTest, ProfileableAppRuntime) {
Florian Mayere8599d42019-10-14 14:58:15 +0100106 std::string app_name = "android.perfetto.cts.app.profileable";
107 const auto& packets = ProfileRuntime(app_name);
108 AssertGraphPresent(packets);
Florian Mayere8599d42019-10-14 14:58:15 +0100109}
110
Florian Mayer710ac8b2019-10-28 17:33:04 +0000111TEST(HeapprofdJavaCtsTest, ReleaseAppRuntime) {
Florian Mayere8599d42019-10-14 14:58:15 +0100112 std::string app_name = "android.perfetto.cts.app.release";
113 const auto& packets = ProfileRuntime(app_name);
114
115 if (IsDebuggableBuild())
116 AssertGraphPresent(packets);
117 else
118 AssertNoProfileContents(packets);
Florian Mayere8599d42019-10-14 14:58:15 +0100119}
120
121} // namespace
122} // namespace perfetto