1 /*
2 * Copyright (C) 2022 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 <sys/system_properties.h>
18 #include <random>
19 #include "test/gtest_and_gmock.h"
20
21 #include "perfetto/ext/base/android_utils.h"
22 #include "perfetto/ext/base/uuid.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/test_helper.h"
27
28 #include "protos/perfetto/config/test_config.gen.h"
29 #include "protos/perfetto/trace/test_event.gen.h"
30 #include "protos/perfetto/trace/trace.gen.h"
31 #include "protos/perfetto/trace/trace_packet.gen.h"
32
33 namespace perfetto {
34 namespace {
35
TEST(PerfettoReporterTest,TestEndToEndReport)36 TEST(PerfettoReporterTest, TestEndToEndReport) {
37 base::TestTaskRunner task_runner;
38 TestHelper helper(&task_runner);
39 helper.ConnectFakeProducer();
40
41 TraceConfig trace_config;
42 trace_config.add_buffers()->set_size_kb(1024);
43 trace_config.set_duration_ms(200);
44 trace_config.set_allow_user_build_tracing(true);
45 trace_config.set_unique_session_name("TestEndToEndReport");
46
47 // Make the trace as small as possible (see b/282508742).
48 auto* builtin = trace_config.mutable_builtin_data_sources();
49 builtin->set_disable_clock_snapshotting(true);
50 builtin->set_disable_system_info(true);
51 builtin->set_disable_service_events(true);
52 builtin->set_disable_chunk_usage_histograms(true);
53
54 auto* ds_config = trace_config.add_data_sources()->mutable_config();
55 ds_config->set_name("android.perfetto.FakeProducer");
56 ds_config->set_target_buffer(0);
57
58 base::Uuid uuid = base::Uuidv4();
59 trace_config.set_trace_uuid_lsb(uuid.lsb());
60 trace_config.set_trace_uuid_msb(uuid.msb());
61
62 static constexpr uint32_t kRandomSeed = 42;
63 static constexpr uint32_t kEventCount = 1;
64 static constexpr uint32_t kMessageSizeBytes = 2;
65 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
66 ds_config->mutable_for_testing()->set_message_count(kEventCount);
67 ds_config->mutable_for_testing()->set_message_size(kMessageSizeBytes);
68 ds_config->mutable_for_testing()->set_send_batch_on_register(true);
69
70 auto* report_config = trace_config.mutable_android_report_config();
71 report_config->set_reporter_service_package("android.perfetto.cts.reporter");
72 report_config->set_reporter_service_class(
73 "android.perfetto.cts.reporter.PerfettoReportService");
74 report_config->set_use_pipe_in_framework_for_testing(true);
75
76 // We have to construct all the processes we want to fork before we start the
77 // service with |StartServiceIfRequired()|. this is because it is unsafe
78 // (could deadlock) to fork after we've spawned some threads which might
79 // printf (and thus hold locks).
80 auto perfetto_proc = Exec("perfetto",
81 {
82 "--upload",
83 "--no-guardrails",
84 "-c",
85 "-",
86 },
87 trace_config.SerializeAsString());
88
89 std::string stderr_str;
90 EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)) << stderr_str;
91
92 static constexpr char kPath[] =
93 "/sdcard/Android/data/android.perfetto.cts.reporter/files/";
94 std::string path = kPath + uuid.ToPrettyString();
95 static constexpr uint32_t kIterationSleepMs = 500;
96 static constexpr uint32_t kIterationCount =
97 kDefaultTestTimeoutMs / kIterationSleepMs;
98 for (size_t i = 0; i < kIterationCount; ++i) {
99 if (!base::FileExists(path)) {
100 base::SleepMicroseconds(kIterationSleepMs * 1000);
101 continue;
102 }
103
104 std::string trace_str;
105 ASSERT_TRUE(base::ReadFile(path, &trace_str));
106
107 protos::gen::Trace trace;
108 ASSERT_TRUE(trace.ParseFromString(trace_str));
109 int for_testing = 0;
110 for (const auto& packet : trace.packet()) {
111 for_testing += packet.has_for_testing();
112 }
113 ASSERT_EQ(for_testing, kEventCount);
114 return;
115 }
116 FAIL() << "Timed out waiting for trace file";
117 }
118
119 } // namespace
120 } // namespace perfetto
121