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
BuildProtoConfig()44 std::vector<uint8_t> TracingSession::Builder::BuildProtoConfig() {
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::vector<uint8_t> cfg_buf(cfg_size);
94 PerfettoHeapBufferCopyInto(hb, &writer.writer, cfg_buf.data(), cfg_size);
95 PerfettoHeapBufferDestroy(hb, &writer.writer);
96 return cfg_buf;
97 }
98
Build()99 TracingSession TracingSession::Builder::Build() {
100 std::vector<uint8_t> config = BuildProtoConfig();
101
102 struct PerfettoTracingSessionImpl* ts =
103 PerfettoTracingSessionCreate(PERFETTO_BACKEND_IN_PROCESS);
104
105 PerfettoTracingSessionSetup(ts, config.data(), config.size());
106
107 PerfettoTracingSessionStartBlocking(ts);
108
109 return TracingSession::Adopt(ts);
110 }
111
Adopt(struct PerfettoTracingSessionImpl * session)112 TracingSession TracingSession::Adopt(
113 struct PerfettoTracingSessionImpl* session) {
114 TracingSession ret;
115 ret.session_ = session;
116 ret.stopped_ = std::make_unique<WaitableEvent>();
117 PerfettoTracingSessionSetStopCb(
118 ret.session_,
119 [](struct PerfettoTracingSessionImpl*, void* arg) {
120 static_cast<WaitableEvent*>(arg)->Notify();
121 },
122 ret.stopped_.get());
123 return ret;
124 }
125
TracingSession(TracingSession && other)126 TracingSession::TracingSession(TracingSession&& other) noexcept {
127 session_ = other.session_;
128 other.session_ = nullptr;
129 stopped_ = std::move(other.stopped_);
130 other.stopped_ = nullptr;
131 }
132
~TracingSession()133 TracingSession::~TracingSession() {
134 if (!session_) {
135 return;
136 }
137 if (!stopped_->IsNotified()) {
138 PerfettoTracingSessionStopBlocking(session_);
139 stopped_->WaitForNotification();
140 }
141 PerfettoTracingSessionDestroy(session_);
142 }
143
FlushBlocking(uint32_t timeout_ms)144 bool TracingSession::FlushBlocking(uint32_t timeout_ms) {
145 WaitableEvent notification;
146 bool result;
147 auto* cb = new std::function<void(bool)>([&](bool success) {
148 result = success;
149 notification.Notify();
150 });
151 PerfettoTracingSessionFlushAsync(
152 session_, timeout_ms,
153 [](PerfettoTracingSessionImpl*, bool success, void* user_arg) {
154 auto* f = reinterpret_cast<std::function<void(bool)>*>(user_arg);
155 (*f)(success);
156 delete f;
157 },
158 cb);
159 notification.WaitForNotification();
160 return result;
161 }
162
WaitForStopped()163 void TracingSession::WaitForStopped() {
164 stopped_->WaitForNotification();
165 }
166
StopAsync()167 void TracingSession::StopAsync() {
168 PerfettoTracingSessionStopAsync(session_);
169 }
170
StopBlocking()171 void TracingSession::StopBlocking() {
172 PerfettoTracingSessionStopBlocking(session_);
173 }
174
ReadBlocking()175 std::vector<uint8_t> TracingSession::ReadBlocking() {
176 std::vector<uint8_t> data;
177 PerfettoTracingSessionReadTraceBlocking(
178 session_,
179 [](struct PerfettoTracingSessionImpl*, const void* trace_data,
180 size_t size, bool, void* user_arg) {
181 auto& dst = *static_cast<std::vector<uint8_t>*>(user_arg);
182 auto* src = static_cast<const uint8_t*>(trace_data);
183 dst.insert(dst.end(), src, src + size);
184 },
185 &data);
186 return data;
187 }
188
189 } // namespace test_utils
190 } // namespace shlib
191 } // namespace perfetto
192
PrintTo(const PerfettoPbDecoderField & field,std::ostream * pos)193 void PrintTo(const PerfettoPbDecoderField& field, std::ostream* pos) {
194 std::ostream& os = *pos;
195 PerfettoPbDecoderStatus status =
196 static_cast<PerfettoPbDecoderStatus>(field.status);
197 switch (status) {
198 case PERFETTO_PB_DECODER_ERROR:
199 os << "MALFORMED PROTOBUF";
200 break;
201 case PERFETTO_PB_DECODER_DONE:
202 os << "DECODER DONE";
203 break;
204 case PERFETTO_PB_DECODER_OK:
205 switch (field.wire_type) {
206 case PERFETTO_PB_WIRE_TYPE_DELIMITED:
207 os << "\"";
208 for (size_t i = 0; i < field.value.delimited.len; i++) {
209 os << perfetto::shlib::test_utils::ToHexChars(
210 field.value.delimited.start[i])
211 << " ";
212 }
213 os << "\"";
214 break;
215 case PERFETTO_PB_WIRE_TYPE_VARINT:
216 os << "varint: " << field.value.integer64;
217 break;
218 case PERFETTO_PB_WIRE_TYPE_FIXED32:
219 os << "fixed32: " << field.value.integer32;
220 break;
221 case PERFETTO_PB_WIRE_TYPE_FIXED64:
222 os << "fixed64: " << field.value.integer64;
223 break;
224 }
225 break;
226 }
227 }
228