1 /* 2 * Copyright (C) 2018 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 #ifndef SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_ 17 #define SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_ 18 19 #include <vector> 20 21 #include "perfetto/ext/tracing/core/trace_writer.h" 22 #include "perfetto/protozero/message_handle.h" 23 #include "perfetto/protozero/root_message.h" 24 #include "perfetto/protozero/scattered_heap_buffer.h" 25 #include "protos/perfetto/trace/trace_packet.gen.h" 26 27 namespace perfetto { 28 29 // A specialization of TraceWriter for testing which writes into memory 30 // allocated by the ScatteredHeapBuffer. 31 // See //include/perfetto/ext/tracing/core/trace_writer.h for docs. 32 class TraceWriterForTesting : public TraceWriter { 33 public: 34 TraceWriterForTesting(); 35 ~TraceWriterForTesting() override; 36 37 // TraceWriter implementation. See documentation in trace_writer.h. 38 // TracePacketHandle is defined in trace_writer.h 39 TracePacketHandle NewTracePacket() override; 40 void FinishTracePacket() override; 41 void Flush(std::function<void()> callback = {}) override; 42 43 std::vector<protos::gen::TracePacket> GetAllTracePackets(); 44 protos::gen::TracePacket GetOnlyTracePacket(); 45 46 WriterID writer_id() const override; 47 uint64_t written() const override; 48 49 private: 50 TraceWriterForTesting(const TraceWriterForTesting&) = delete; 51 TraceWriterForTesting& operator=(const TraceWriterForTesting&) = delete; 52 53 protozero::ScatteredHeapBuffer delegate_; 54 protozero::ScatteredStreamWriter stream_; 55 56 // The packet returned via NewTracePacket(). Its owned by this class, 57 // TracePacketHandle has just a pointer to it. 58 // 59 // The caller of NewTracePacket can use TakeStreamWriter() and use the stream 60 // writer directly: in that case: 61 // * cur_packet_->size() is not up to date. Only the stream writer has the 62 // correct information. 63 // * cur_packet_->nested_message() is always nullptr. 64 // * cur_packet_->size_field() is still used to track the start of the current 65 // packet. 66 std::unique_ptr<protozero::RootMessage<protos::pbzero::TracePacket>> 67 cur_packet_; 68 69 size_t cur_packet_written_start_ = 0; 70 }; 71 72 } // namespace perfetto 73 74 #endif // SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_ 75