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