1 /* 2 * Copyright (C) 2017 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 #ifndef SRC_TRACING_CORE_TRACE_WRITER_IMPL_H_ 18 #define SRC_TRACING_CORE_TRACE_WRITER_IMPL_H_ 19 20 #include "perfetto/base/proc_utils.h" 21 #include "perfetto/ext/tracing/core/basic_types.h" 22 #include "perfetto/ext/tracing/core/shared_memory_abi.h" 23 #include "perfetto/ext/tracing/core/shared_memory_arbiter.h" 24 #include "perfetto/ext/tracing/core/trace_writer.h" 25 #include "perfetto/protozero/message_handle.h" 26 #include "perfetto/protozero/proto_utils.h" 27 #include "perfetto/protozero/root_message.h" 28 #include "perfetto/protozero/scattered_stream_writer.h" 29 #include "perfetto/tracing/buffer_exhausted_policy.h" 30 #include "src/tracing/core/patch_list.h" 31 32 namespace perfetto { 33 34 class SharedMemoryArbiterImpl; 35 36 // See //include/perfetto/tracing/core/trace_writer.h for docs. 37 class TraceWriterImpl : public TraceWriter, 38 public protozero::ScatteredStreamWriter::Delegate { 39 public: 40 // TracePacketHandle is defined in trace_writer.h 41 TraceWriterImpl(SharedMemoryArbiterImpl*, 42 WriterID, 43 MaybeUnboundBufferID buffer_id, 44 BufferExhaustedPolicy); 45 ~TraceWriterImpl() override; 46 47 // TraceWriter implementation. See documentation in trace_writer.h. 48 TracePacketHandle NewTracePacket() override; 49 void Flush(std::function<void()> callback = {}) override; 50 WriterID writer_id() const override; written()51 uint64_t written() const override { 52 return protobuf_stream_writer_.written(); 53 } 54 ResetChunkForTesting()55 void ResetChunkForTesting() { cur_chunk_ = SharedMemoryABI::Chunk(); } drop_packets_for_testing()56 bool drop_packets_for_testing() const { return drop_packets_; } 57 58 private: 59 TraceWriterImpl(const TraceWriterImpl&) = delete; 60 TraceWriterImpl& operator=(const TraceWriterImpl&) = delete; 61 62 // ScatteredStreamWriter::Delegate implementation. 63 protozero::ContiguousMemoryRange GetNewBuffer() override; 64 65 // The per-producer arbiter that coordinates access to the shared memory 66 // buffer from several threads. 67 SharedMemoryArbiterImpl* const shmem_arbiter_; 68 69 // ID of the current writer. 70 const WriterID id_; 71 72 // This is copied into the commit request by SharedMemoryArbiter. See comments 73 // in data_source_config.proto for |target_buffer|. If this is a reservation 74 // for a buffer ID in case of a startup trace writer, SharedMemoryArbiterImpl 75 // will also translate the reservation ID to the actual buffer ID. 76 const MaybeUnboundBufferID target_buffer_; 77 78 // Whether GetNewChunk() should stall or return an invalid chunk if the SMB is 79 // exhausted. 80 const BufferExhaustedPolicy buffer_exhausted_policy_; 81 82 // Monotonic (% wrapping) sequence id of the chunk. Together with the WriterID 83 // this allows the Service to reconstruct the linear sequence of packets. 84 ChunkID next_chunk_id_ = 0; 85 86 // The chunk we are holding onto (if any). 87 SharedMemoryABI::Chunk cur_chunk_; 88 89 // Passed to protozero message to write directly into |cur_chunk_|. It 90 // keeps track of the write pointer. It calls us back (GetNewBuffer()) when 91 // |cur_chunk_| is filled. 92 protozero::ScatteredStreamWriter protobuf_stream_writer_; 93 94 // The packet returned via NewTracePacket(). Its owned by this class, 95 // TracePacketHandle has just a pointer to it. 96 std::unique_ptr<protozero::RootMessage<protos::pbzero::TracePacket>> 97 cur_packet_; 98 99 // The start address of |cur_packet_| within |cur_chunk_|. Used to figure out 100 // fragments sizes when a TracePacket write is interrupted by GetNewBuffer(). 101 uint8_t* cur_fragment_start_ = nullptr; 102 103 // true if we received a call to GetNewBuffer() after NewTracePacket(), 104 // false if GetNewBuffer() happened during NewTracePacket() prologue, while 105 // starting the TracePacket header. 106 bool fragmenting_packet_ = false; 107 108 // Set to |true| when the current chunk contains the maximum number of packets 109 // a chunk can contain. When this is |true|, the next packet requires starting 110 // a new chunk. 111 bool reached_max_packets_per_chunk_ = false; 112 113 // If we fail to acquire a new chunk when the arbiter operates in 114 // SharedMemory::BufferExhaustedPolicy::kDrop mode, the trace writer enters a 115 // mode in which data is written to a local garbage chunk and dropped. 116 bool drop_packets_ = false; 117 118 // Whether the trace writer should try to acquire a new chunk from the SMB 119 // when the next TracePacket is started because it filled the garbage chunk at 120 // least once since the last attempt. 121 bool retry_new_chunk_after_packet_ = false; 122 123 // Points to the size field of the last packet we wrote to the current chunk. 124 // If the chunk was already returned, this is reset to |nullptr|. 125 uint8_t* last_packet_size_field_ = nullptr; 126 127 // When a packet is fragmented across different chunks, the |size_field| of 128 // the outstanding nested protobuf messages is redirected onto Patch entries 129 // in this list at the time the Chunk is returned (because at that point we 130 // have to release the ownership of the current Chunk). This list will be 131 // later sent out-of-band to the tracing service, who will patch the required 132 // chunks, if they are still around. 133 PatchList patch_list_; 134 135 // PID of the process that created the trace writer. Used for a DCHECK that 136 // aims to detect unsupported process forks while tracing. 137 const base::PlatformProcessId process_id_; 138 }; 139 140 } // namespace perfetto 141 142 #endif // SRC_TRACING_CORE_TRACE_WRITER_IMPL_H_ 143