xref: /aosp_15_r20/external/perfetto/src/shared_lib/test/utils.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 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 "src/shared_lib/test/utils.h"
18 
19 #include "perfetto/public/abi/heap_buffer.h"
20 #include "perfetto/public/pb_msg.h"
21 #include "perfetto/public/pb_utils.h"
22 #include "perfetto/public/protos/config/data_source_config.pzc.h"
23 #include "perfetto/public/protos/config/trace_config.pzc.h"
24 #include "perfetto/public/protos/config/track_event/track_event_config.pzc.h"
25 #include "perfetto/public/tracing_session.h"
26 
27 namespace perfetto {
28 namespace shlib {
29 namespace test_utils {
30 namespace {
31 
ToHexChars(uint8_t val)32 std::string ToHexChars(uint8_t val) {
33   std::string ret;
34   uint8_t high_nibble = (val & 0xF0) >> 4;
35   uint8_t low_nibble = (val & 0xF);
36   static const char hex_chars[] = "0123456789ABCDEF";
37   ret.push_back(hex_chars[high_nibble]);
38   ret.push_back(hex_chars[low_nibble]);
39   return ret;
40 }
41 
42 }  // namespace
43 
Build()44 TracingSession TracingSession::Builder::Build() {
45   struct PerfettoPbMsgWriter writer;
46   struct PerfettoHeapBuffer* hb = PerfettoHeapBufferCreate(&writer.writer);
47 
48   struct perfetto_protos_TraceConfig cfg;
49   PerfettoPbMsgInit(&cfg.msg, &writer);
50 
51   {
52     struct perfetto_protos_TraceConfig_BufferConfig buffers;
53     perfetto_protos_TraceConfig_begin_buffers(&cfg, &buffers);
54 
55     perfetto_protos_TraceConfig_BufferConfig_set_size_kb(&buffers, 1024);
56 
57     perfetto_protos_TraceConfig_end_buffers(&cfg, &buffers);
58   }
59 
60   {
61     struct perfetto_protos_TraceConfig_DataSource data_sources;
62     perfetto_protos_TraceConfig_begin_data_sources(&cfg, &data_sources);
63 
64     {
65       struct perfetto_protos_DataSourceConfig ds_cfg;
66       perfetto_protos_TraceConfig_DataSource_begin_config(&data_sources,
67                                                           &ds_cfg);
68 
69       perfetto_protos_DataSourceConfig_set_cstr_name(&ds_cfg,
70                                                      data_source_name_.c_str());
71       if (!enabled_categories_.empty() || !disabled_categories_.empty()) {
72         perfetto_protos_TrackEventConfig te_cfg;
73         perfetto_protos_DataSourceConfig_begin_track_event_config(&ds_cfg,
74                                                                   &te_cfg);
75         for (const std::string& cat : enabled_categories_) {
76           perfetto_protos_TrackEventConfig_set_enabled_categories(
77               &te_cfg, cat.data(), cat.size());
78         }
79         for (const std::string& cat : disabled_categories_) {
80           perfetto_protos_TrackEventConfig_set_disabled_categories(
81               &te_cfg, cat.data(), cat.size());
82         }
83         perfetto_protos_DataSourceConfig_end_track_event_config(&ds_cfg,
84                                                                 &te_cfg);
85       }
86 
87       perfetto_protos_TraceConfig_DataSource_end_config(&data_sources, &ds_cfg);
88     }
89 
90     perfetto_protos_TraceConfig_end_data_sources(&cfg, &data_sources);
91   }
92   size_t cfg_size = PerfettoStreamWriterGetWrittenSize(&writer.writer);
93   std::unique_ptr<uint8_t[]> ser(new uint8_t[cfg_size]);
94   PerfettoHeapBufferCopyInto(hb, &writer.writer, ser.get(), cfg_size);
95   PerfettoHeapBufferDestroy(hb, &writer.writer);
96 
97   struct PerfettoTracingSessionImpl* ts =
98       PerfettoTracingSessionCreate(PERFETTO_BACKEND_IN_PROCESS);
99 
100   PerfettoTracingSessionSetup(ts, ser.get(), cfg_size);
101 
102   PerfettoTracingSessionStartBlocking(ts);
103 
104   return TracingSession::Adopt(ts);
105 }
106 
Adopt(struct PerfettoTracingSessionImpl * session)107 TracingSession TracingSession::Adopt(
108     struct PerfettoTracingSessionImpl* session) {
109   TracingSession ret;
110   ret.session_ = session;
111   ret.stopped_ = std::make_unique<WaitableEvent>();
112   PerfettoTracingSessionSetStopCb(
113       ret.session_,
114       [](struct PerfettoTracingSessionImpl*, void* arg) {
115         static_cast<WaitableEvent*>(arg)->Notify();
116       },
117       ret.stopped_.get());
118   return ret;
119 }
120 
TracingSession(TracingSession && other)121 TracingSession::TracingSession(TracingSession&& other) noexcept {
122   session_ = other.session_;
123   other.session_ = nullptr;
124   stopped_ = std::move(other.stopped_);
125   other.stopped_ = nullptr;
126 }
127 
~TracingSession()128 TracingSession::~TracingSession() {
129   if (!session_) {
130     return;
131   }
132   if (!stopped_->IsNotified()) {
133     PerfettoTracingSessionStopBlocking(session_);
134     stopped_->WaitForNotification();
135   }
136   PerfettoTracingSessionDestroy(session_);
137 }
138 
FlushBlocking(uint32_t timeout_ms)139 bool TracingSession::FlushBlocking(uint32_t timeout_ms) {
140   WaitableEvent notification;
141   bool result;
142   auto* cb = new std::function<void(bool)>([&](bool success) {
143     result = success;
144     notification.Notify();
145   });
146   PerfettoTracingSessionFlushAsync(
147       session_, timeout_ms,
148       [](PerfettoTracingSessionImpl*, bool success, void* user_arg) {
149         auto* f = reinterpret_cast<std::function<void(bool)>*>(user_arg);
150         (*f)(success);
151         delete f;
152       },
153       cb);
154   notification.WaitForNotification();
155   return result;
156 }
157 
WaitForStopped()158 void TracingSession::WaitForStopped() {
159   stopped_->WaitForNotification();
160 }
161 
StopAsync()162 void TracingSession::StopAsync() {
163   PerfettoTracingSessionStopAsync(session_);
164 }
165 
StopBlocking()166 void TracingSession::StopBlocking() {
167   PerfettoTracingSessionStopBlocking(session_);
168 }
169 
ReadBlocking()170 std::vector<uint8_t> TracingSession::ReadBlocking() {
171   std::vector<uint8_t> data;
172   PerfettoTracingSessionReadTraceBlocking(
173       session_,
174       [](struct PerfettoTracingSessionImpl*, const void* trace_data,
175          size_t size, bool, void* user_arg) {
176         auto& dst = *static_cast<std::vector<uint8_t>*>(user_arg);
177         auto* src = static_cast<const uint8_t*>(trace_data);
178         dst.insert(dst.end(), src, src + size);
179       },
180       &data);
181   return data;
182 }
183 
184 }  // namespace test_utils
185 }  // namespace shlib
186 }  // namespace perfetto
187 
PrintTo(const PerfettoPbDecoderField & field,std::ostream * pos)188 void PrintTo(const PerfettoPbDecoderField& field, std::ostream* pos) {
189   std::ostream& os = *pos;
190   PerfettoPbDecoderStatus status =
191       static_cast<PerfettoPbDecoderStatus>(field.status);
192   switch (status) {
193     case PERFETTO_PB_DECODER_ERROR:
194       os << "MALFORMED PROTOBUF";
195       break;
196     case PERFETTO_PB_DECODER_DONE:
197       os << "DECODER DONE";
198       break;
199     case PERFETTO_PB_DECODER_OK:
200       switch (field.wire_type) {
201         case PERFETTO_PB_WIRE_TYPE_DELIMITED:
202           os << "\"";
203           for (size_t i = 0; i < field.value.delimited.len; i++) {
204             os << perfetto::shlib::test_utils::ToHexChars(
205                       field.value.delimited.start[i])
206                << " ";
207           }
208           os << "\"";
209           break;
210         case PERFETTO_PB_WIRE_TYPE_VARINT:
211           os << "varint: " << field.value.integer64;
212           break;
213         case PERFETTO_PB_WIRE_TYPE_FIXED32:
214           os << "fixed32: " << field.value.integer32;
215           break;
216         case PERFETTO_PB_WIRE_TYPE_FIXED64:
217           os << "fixed64: " << field.value.integer64;
218           break;
219       }
220       break;
221   }
222 }
223