Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 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/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 | |
| 31 | namespace perfetto { |
| 32 | namespace { |
| 33 | |
| 34 | std::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 Mayer | 710ac8b | 2019-10-28 17:33:04 +0000 | [diff] [blame] | 45 | // TODO(b/143467832): Remove this workaround. |
| 46 | // If we try to dump too early in app initialization, we sometimes deadlock. |
| 47 | sleep(1); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 48 | |
| 49 | // set up tracing |
| 50 | TestHelper helper(&task_runner); |
| 51 | helper.ConnectConsumer(); |
| 52 | helper.WaitForConsumerConnect(); |
| 53 | |
| 54 | TraceConfig trace_config; |
Florian Mayer | 9ae3d74 | 2019-10-25 15:00:35 +0100 | [diff] [blame] | 55 | trace_config.add_buffers()->set_size_kb(20 * 1024); |
Florian Mayer | 626e47c | 2019-10-18 14:46:15 +0100 | [diff] [blame] | 56 | trace_config.set_duration_ms(6000); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 57 | |
| 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 Mayer | 9ae3d74 | 2019-10-25 15:00:35 +0100 | [diff] [blame] | 71 | StopApp(app_name, "new.app.stopped", &task_runner); |
| 72 | task_runner.RunUntilCheckpoint("new.app.stopped", 1000 /*ms*/); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 73 | return helper.trace(); |
| 74 | } |
| 75 | |
| 76 | void AssertGraphPresent(std::vector<protos::TracePacket> packets) { |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame^] | 77 | ASSERT_GT(packets.size(), 0u); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 78 | |
| 79 | size_t objects = 0; |
Florian Mayer | 6e15844 | 2019-10-14 15:18:38 +0100 | [diff] [blame] | 80 | size_t roots = 0; |
| 81 | for (const auto& packet : packets) { |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame^] | 82 | objects += static_cast<size_t>(packet.heap_graph().objects_size()); |
| 83 | roots += static_cast<size_t>(packet.heap_graph().roots_size()); |
Florian Mayer | 6e15844 | 2019-10-14 15:18:38 +0100 | [diff] [blame] | 84 | } |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame^] | 85 | ASSERT_GT(objects, 0u); |
| 86 | ASSERT_GT(roots, 0u); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void AssertNoProfileContents(std::vector<protos::TracePacket> packets) { |
| 90 | // If profile packets are present, they must be empty. |
| 91 | for (const auto& packet : packets) { |
Florian Mayer | 6e15844 | 2019-10-14 15:18:38 +0100 | [diff] [blame] | 92 | ASSERT_EQ(packet.heap_graph().roots_size(), 0); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 93 | 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 Mayer | 710ac8b | 2019-10-28 17:33:04 +0000 | [diff] [blame] | 99 | TEST(HeapprofdJavaCtsTest, DebuggableAppRuntime) { |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 100 | std::string app_name = "android.perfetto.cts.app.debuggable"; |
| 101 | const auto& packets = ProfileRuntime(app_name); |
| 102 | AssertGraphPresent(packets); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Florian Mayer | 710ac8b | 2019-10-28 17:33:04 +0000 | [diff] [blame] | 105 | TEST(HeapprofdJavaCtsTest, ProfileableAppRuntime) { |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 106 | std::string app_name = "android.perfetto.cts.app.profileable"; |
| 107 | const auto& packets = ProfileRuntime(app_name); |
| 108 | AssertGraphPresent(packets); |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Florian Mayer | 710ac8b | 2019-10-28 17:33:04 +0000 | [diff] [blame] | 111 | TEST(HeapprofdJavaCtsTest, ReleaseAppRuntime) { |
Florian Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 112 | 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 Mayer | e8599d4 | 2019-10-14 14:58:15 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | } // namespace perfetto |