1 /*
2  * Copyright 2024 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 "tracing_perfetto.h"
18 
19 #include <thread>
20 
21 #include <android_os.h>
22 #include <flag_macros.h>
23 
24 #include "gtest/gtest.h"
25 #include "perfetto/public/abi/data_source_abi.h"
26 #include "perfetto/public/abi/heap_buffer.h"
27 #include "perfetto/public/abi/pb_decoder_abi.h"
28 #include "perfetto/public/abi/tracing_session_abi.h"
29 #include "perfetto/public/abi/track_event_abi.h"
30 #include "perfetto/public/data_source.h"
31 #include "perfetto/public/pb_decoder.h"
32 #include "perfetto/public/producer.h"
33 #include "perfetto/public/protos/config/trace_config.pzc.h"
34 #include "perfetto/public/protos/trace/interned_data/interned_data.pzc.h"
35 #include "perfetto/public/protos/trace/test_event.pzc.h"
36 #include "perfetto/public/protos/trace/trace.pzc.h"
37 #include "perfetto/public/protos/trace/trace_packet.pzc.h"
38 #include "perfetto/public/protos/trace/track_event/debug_annotation.pzc.h"
39 #include "perfetto/public/protos/trace/track_event/track_descriptor.pzc.h"
40 #include "perfetto/public/protos/trace/track_event/track_event.pzc.h"
41 #include "perfetto/public/protos/trace/trigger.pzc.h"
42 #include "perfetto/public/te_category_macros.h"
43 #include "perfetto/public/te_macros.h"
44 #include "perfetto/public/track_event.h"
45 #include "trace_categories.h"
46 #include "utils.h"
47 
48 namespace tracing_perfetto {
49 
50 using ::perfetto::shlib::test_utils::AllFieldsWithId;
51 using ::perfetto::shlib::test_utils::FieldView;
52 using ::perfetto::shlib::test_utils::IdFieldView;
53 using ::perfetto::shlib::test_utils::MsgField;
54 using ::perfetto::shlib::test_utils::PbField;
55 using ::perfetto::shlib::test_utils::StringField;
56 using ::perfetto::shlib::test_utils::TracingSession;
57 using ::perfetto::shlib::test_utils::VarIntField;
58 using ::testing::_;
59 using ::testing::ElementsAre;
60 using ::testing::UnorderedElementsAre;
61 
62 const auto PERFETTO_SDK_TRACING = ACONFIG_FLAG(android::os, perfetto_sdk_tracing);
63 
64 class TracingPerfettoTest : public testing::Test {
65  protected:
SetUp()66   void SetUp() override {
67     tracing_perfetto::registerWithPerfetto(true /* test */);
68   }
69 };
70 
71 // TODO(b/303199244): Add tests for all the library functions.
72 
TEST_F_WITH_FLAGS(TracingPerfettoTest,traceInstant,REQUIRES_FLAGS_ENABLED (PERFETTO_SDK_TRACING))73 TEST_F_WITH_FLAGS(TracingPerfettoTest, traceInstant,
74                   REQUIRES_FLAGS_ENABLED(PERFETTO_SDK_TRACING)) {
75   TracingSession tracing_session =
76       TracingSession::Builder().set_data_source_name("track_event").Build();
77   tracing_perfetto::traceInstant(TRACE_CATEGORY_INPUT, "");
78 
79   tracing_session.StopBlocking();
80   std::vector<uint8_t> data = tracing_session.ReadBlocking();
81   bool found = false;
82   for (struct PerfettoPbDecoderField trace_field : FieldView(data)) {
83     ASSERT_THAT(trace_field, PbField(perfetto_protos_Trace_packet_field_number,
84                                      MsgField(_)));
85     IdFieldView track_event(
86         trace_field, perfetto_protos_TracePacket_track_event_field_number);
87     if (track_event.size() == 0) {
88       continue;
89     }
90     found = true;
91     IdFieldView cat_iid_fields(
92         track_event.front(),
93         perfetto_protos_TrackEvent_category_iids_field_number);
94     ASSERT_THAT(cat_iid_fields, ElementsAre(VarIntField(_)));
95     uint64_t cat_iid = cat_iid_fields.front().value.integer64;
96     EXPECT_THAT(
97         trace_field,
98         AllFieldsWithId(
99             perfetto_protos_TracePacket_interned_data_field_number,
100             ElementsAre(AllFieldsWithId(
101                 perfetto_protos_InternedData_event_categories_field_number,
102                 ElementsAre(MsgField(UnorderedElementsAre(
103                     PbField(perfetto_protos_EventCategory_iid_field_number,
104                             VarIntField(cat_iid)),
105                     PbField(perfetto_protos_EventCategory_name_field_number,
106                             StringField("input")))))))));
107   }
108   EXPECT_TRUE(found);
109 }
110 
111 }  // namespace tracing_perfetto