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/tracing_session.h"
25
26 namespace perfetto {
27 namespace shlib {
28 namespace test_utils {
29 namespace {
30
ToHexChars(uint8_t val)31 std::string ToHexChars(uint8_t val) {
32 std::string ret;
33 uint8_t high_nibble = (val & 0xF0) >> 4;
34 uint8_t low_nibble = (val & 0xF);
35 static const char hex_chars[] = "0123456789ABCDEF";
36 ret.push_back(hex_chars[high_nibble]);
37 ret.push_back(hex_chars[low_nibble]);
38 return ret;
39 }
40
41 } // namespace
42
Build()43 TracingSession TracingSession::Builder::Build() {
44 struct PerfettoPbMsgWriter writer;
45 struct PerfettoHeapBuffer* hb = PerfettoHeapBufferCreate(&writer.writer);
46
47 struct perfetto_protos_TraceConfig cfg;
48 PerfettoPbMsgInit(&cfg.msg, &writer);
49
50 {
51 struct perfetto_protos_TraceConfig_BufferConfig buffers;
52 perfetto_protos_TraceConfig_begin_buffers(&cfg, &buffers);
53
54 perfetto_protos_TraceConfig_BufferConfig_set_size_kb(&buffers, 1024);
55
56 perfetto_protos_TraceConfig_end_buffers(&cfg, &buffers);
57 }
58
59 {
60 struct perfetto_protos_TraceConfig_DataSource data_sources;
61 perfetto_protos_TraceConfig_begin_data_sources(&cfg, &data_sources);
62
63 {
64 struct perfetto_protos_DataSourceConfig ds_cfg;
65 perfetto_protos_TraceConfig_DataSource_begin_config(&data_sources,
66 &ds_cfg);
67
68 perfetto_protos_DataSourceConfig_set_cstr_name(&ds_cfg,
69 data_source_name_.c_str());
70
71 perfetto_protos_TraceConfig_DataSource_end_config(&data_sources, &ds_cfg);
72 }
73
74 perfetto_protos_TraceConfig_end_data_sources(&cfg, &data_sources);
75 }
76 size_t cfg_size = PerfettoStreamWriterGetWrittenSize(&writer.writer);
77 std::unique_ptr<uint8_t[]> ser(new uint8_t[cfg_size]);
78 PerfettoHeapBufferCopyInto(hb, &writer.writer, ser.get(), cfg_size);
79 PerfettoHeapBufferDestroy(hb, &writer.writer);
80
81 struct PerfettoTracingSessionImpl* ts =
82 PerfettoTracingSessionCreate(PERFETTO_BACKEND_IN_PROCESS);
83
84 PerfettoTracingSessionSetup(ts, ser.get(), cfg_size);
85
86 PerfettoTracingSessionStartBlocking(ts);
87
88 TracingSession ret;
89 ret.session_ = ts;
90 return ret;
91 }
92
TracingSession(TracingSession && other)93 TracingSession::TracingSession(TracingSession&& other) noexcept {
94 session_ = other.session_;
95 other.session_ = nullptr;
96 }
97
~TracingSession()98 TracingSession::~TracingSession() {
99 if (!session_) {
100 return;
101 }
102 if (!stopped_) {
103 PerfettoTracingSessionStopBlocking(session_);
104 }
105 PerfettoTracingSessionDestroy(session_);
106 }
107
StopBlocking()108 void TracingSession::StopBlocking() {
109 stopped_ = true;
110 PerfettoTracingSessionStopBlocking(session_);
111 }
112
ReadBlocking()113 std::vector<uint8_t> TracingSession::ReadBlocking() {
114 std::vector<uint8_t> data;
115 PerfettoTracingSessionReadTraceBlocking(
116 session_,
117 [](struct PerfettoTracingSessionImpl*, const void* trace_data,
118 size_t size, bool, void* user_arg) {
119 auto& dst = *static_cast<std::vector<uint8_t>*>(user_arg);
120 auto* src = static_cast<const uint8_t*>(trace_data);
121 dst.insert(dst.end(), src, src + size);
122 },
123 &data);
124 return data;
125 }
126
127 } // namespace test_utils
128 } // namespace shlib
129 } // namespace perfetto
130
PrintTo(const PerfettoPbDecoderField & field,std::ostream * pos)131 void PrintTo(const PerfettoPbDecoderField& field, std::ostream* pos) {
132 std::ostream& os = *pos;
133 PerfettoPbDecoderStatus status =
134 static_cast<PerfettoPbDecoderStatus>(field.status);
135 switch (status) {
136 case PERFETTO_PB_DECODER_ERROR:
137 os << "MALFORMED PROTOBUF";
138 break;
139 case PERFETTO_PB_DECODER_DONE:
140 os << "DECODER DONE";
141 break;
142 case PERFETTO_PB_DECODER_OK:
143 switch (field.wire_type) {
144 case PERFETTO_PB_WIRE_TYPE_DELIMITED:
145 os << "\"";
146 for (size_t i = 0; i < field.value.delimited.len; i++) {
147 os << perfetto::shlib::test_utils::ToHexChars(
148 field.value.delimited.start[i])
149 << " ";
150 }
151 os << "\"";
152 break;
153 case PERFETTO_PB_WIRE_TYPE_VARINT:
154 os << "varint: " << field.value.integer64;
155 break;
156 case PERFETTO_PB_WIRE_TYPE_FIXED32:
157 os << "fixed32: " << field.value.integer32;
158 break;
159 case PERFETTO_PB_WIRE_TYPE_FIXED64:
160 os << "fixed64: " << field.value.integer64;
161 break;
162 }
163 break;
164 }
165 }
166