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