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
StopBlocking()162 void TracingSession::StopBlocking() {
163 PerfettoTracingSessionStopBlocking(session_);
164 }
165
ReadBlocking()166 std::vector<uint8_t> TracingSession::ReadBlocking() {
167 std::vector<uint8_t> data;
168 PerfettoTracingSessionReadTraceBlocking(
169 session_,
170 [](struct PerfettoTracingSessionImpl*, const void* trace_data,
171 size_t size, bool, void* user_arg) {
172 auto& dst = *static_cast<std::vector<uint8_t>*>(user_arg);
173 auto* src = static_cast<const uint8_t*>(trace_data);
174 dst.insert(dst.end(), src, src + size);
175 },
176 &data);
177 return data;
178 }
179
180 } // namespace test_utils
181 } // namespace shlib
182 } // namespace perfetto
183
PrintTo(const PerfettoPbDecoderField & field,std::ostream * pos)184 void PrintTo(const PerfettoPbDecoderField& field, std::ostream* pos) {
185 std::ostream& os = *pos;
186 PerfettoPbDecoderStatus status =
187 static_cast<PerfettoPbDecoderStatus>(field.status);
188 switch (status) {
189 case PERFETTO_PB_DECODER_ERROR:
190 os << "MALFORMED PROTOBUF";
191 break;
192 case PERFETTO_PB_DECODER_DONE:
193 os << "DECODER DONE";
194 break;
195 case PERFETTO_PB_DECODER_OK:
196 switch (field.wire_type) {
197 case PERFETTO_PB_WIRE_TYPE_DELIMITED:
198 os << "\"";
199 for (size_t i = 0; i < field.value.delimited.len; i++) {
200 os << perfetto::shlib::test_utils::ToHexChars(
201 field.value.delimited.start[i])
202 << " ";
203 }
204 os << "\"";
205 break;
206 case PERFETTO_PB_WIRE_TYPE_VARINT:
207 os << "varint: " << field.value.integer64;
208 break;
209 case PERFETTO_PB_WIRE_TYPE_FIXED32:
210 os << "fixed32: " << field.value.integer32;
211 break;
212 case PERFETTO_PB_WIRE_TYPE_FIXED64:
213 os << "fixed64: " << field.value.integer64;
214 break;
215 }
216 break;
217 }
218 }
219