Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/tracing/core/trace_buffer_writer.h" | |
| 6 | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "components/tracing/core/proto_utils.h" | |
| 10 | |
| 11 namespace tracing { | |
| 12 namespace v2 { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // TODO(primiano) remove this in next CLs. This should just be taken from the | |
| 17 // C++ class autogenerated from events_chunk.proto (crbug.com/608721). | |
| 18 struct ChunkProto { | |
| 19 enum : uint32_t { | |
| 20 kWriterIdFieldNumber = 1, | |
| 21 kSeqIdInStreamFieldNumber = 2, | |
| 22 kEventsFieldNumber = 3, | |
| 23 kFirstEventContinuesFromPrevChunkFieldNumber = 4, | |
| 24 kLastEventContinuesOnNextChunkFieldNumber = 5 | |
| 25 }; | |
| 26 }; | |
| 27 | |
| 28 const size_t kEventPreambleSize = 1 + proto::kMessageLengthFieldSize; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 TraceBufferWriter::TraceBufferWriter(TraceRingBuffer* trace_ring_buffer, | |
| 33 uint32_t writer_id) | |
| 34 : trace_ring_buffer_(trace_ring_buffer), | |
| 35 writer_id_(writer_id), | |
| 36 chunk_seq_id_(0), | |
| 37 chunk_(nullptr), | |
| 38 continue_on_next_chunk_ptr_(nullptr), | |
| 39 event_data_start_in_current_chunk_(nullptr), | |
| 40 stream_writer_(this) { | |
| 41 event_.Reset(&stream_writer_); | |
| 42 } | |
| 43 | |
| 44 TraceBufferWriter::~TraceBufferWriter() {} | |
| 45 | |
| 46 void TraceBufferWriter::FinalizeCurrentEvent() { | |
| 47 if (UNLIKELY(!chunk_)) | |
| 48 return; | |
| 49 | |
| 50 // Finalize the last event added. This ensures that it and all its nested | |
| 51 // fields are committed to the ring buffer and sealed. No further changes to | |
| 52 // the chunks's memory can be made from the |event_| after this point. | |
| 53 event_.Finalize(); | |
| 54 | |
| 55 // In the unlikely event that the last event did wrap over one or more chunks, | |
| 56 // is is now time to return those chunks (all but the active one) back. | |
| 57 TraceRingBuffer::Chunk* retained_chunk = chunk_->next_in_owner_list(); | |
| 58 if (UNLIKELY(retained_chunk)) { | |
| 59 while (retained_chunk) { | |
| 60 TraceRingBuffer::Chunk* next = retained_chunk->next_in_owner_list(); | |
| 61 retained_chunk->set_next_in_owner_list(nullptr); | |
| 62 trace_ring_buffer_->ReturnChunk(retained_chunk); | |
| 63 retained_chunk = next; | |
| 64 } | |
| 65 chunk_->set_next_in_owner_list(nullptr); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 TraceEventHandle TraceBufferWriter::AddEvent() { | |
| 70 FinalizeCurrentEvent(); | |
| 71 | |
| 72 // In order to start a new event at least kMessageLengthFieldSize + 1 bytes | |
| 73 // are required in the chunk to write the preamble and size of the event | |
| 74 // itself. We take a bit more room here, it doesn't make a lot of sense | |
| 75 // starting a partial event that will fragment immediately after. | |
| 76 // TODO(primiano): replace 16 with a more reasonable size, that is, the size | |
| 77 // of a simple trace event with no args. | |
| 78 if (stream_writer_.bytes_available() < 16) | |
| 79 stream_writer_.Reset(AcquireNewChunk(false /* is_fragmenting_event */)); | |
| 80 | |
| 81 event_.Reset(&stream_writer_); | |
| 82 WriteEventPrambleForNewChunk( | |
|
alph
2016/08/04 19:06:52
Preamble
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
Oops.
| |
| 83 stream_writer_.ReserveBytesUnsafe<kEventPreambleSize>()); | |
| 84 DCHECK_EQ(stream_writer_.write_ptr(), event_data_start_in_current_chunk_); | |
| 85 return TraceEventHandle(static_cast<::tracing::proto::Event*>(&event_)); | |
| 86 } | |
| 87 | |
| 88 // This is invoked by the ProtoZeroMessage write methods when reaching the | |
| 89 // end of the current chunk during a write. | |
| 90 ContiguousMemoryRange TraceBufferWriter::GetNewBuffer() { | |
| 91 return AcquireNewChunk(true /* is_fragmenting_event */); | |
| 92 } | |
| 93 | |
| 94 void TraceBufferWriter::FinalizeCurrentChunk() { | |
| 95 if (!chunk_) | |
| 96 return; | |
| 97 DCHECK_GE(stream_writer_.write_ptr(), chunk_->payload()); | |
| 98 DCHECK_LE(stream_writer_.write_ptr(), chunk_->end()); | |
| 99 const size_t used_size = stream_writer_.write_ptr() - chunk_->payload(); | |
| 100 chunk_->set_used_size(static_cast<uint32_t>(used_size)); | |
| 101 } | |
| 102 | |
| 103 // There are paths that lead to AcquireNewChunk(): | |
| 104 // When |is_fragmenting_event| = false: | |
| 105 // AddEvent() is called and there isn't enough room in the current chunk to | |
| 106 // start a new event (or we don't have a chunk yet). | |
| 107 // When |is_fragmenting_event| = true: | |
| 108 // The client is writing an event, a ProtoZeroMessage::Append* method hits | |
| 109 // the boundary of the chunk and requests a new one via GetNewBuffer(). | |
| 110 ContiguousMemoryRange TraceBufferWriter::AcquireNewChunk( | |
| 111 bool is_fragmenting_event) { | |
| 112 FinalizeCurrentChunk(); | |
| 113 TraceRingBuffer::Chunk* new_chunk = trace_ring_buffer_->TakeChunk(writer_id_); | |
| 114 if (is_fragmenting_event) { | |
| 115 // continue_on_next_chunk_ptr_ points to the proto field of the previous | |
| 116 // chunk and indicates that its last event continues in |new_chunk|. | |
| 117 *continue_on_next_chunk_ptr_ = 1; | |
| 118 | |
| 119 // Backfill the size field of the event with the partial size acccumulated | |
|
alph
2016/08/04 19:06:52
s/ccc/cc/
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
But it can acccccccumulate a lot :P
| |
| 120 // so far in the old chunk. WriteEventPrambleForNewChunk() will take care | |
|
alph
2016/08/04 19:06:52
Preamble
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
yeah by virtue of autocompletion, I've been very c
| |
| 121 // of resetting the |size_field| of the event to the new chunk. | |
| 122 DCHECK_GE(event_data_start_in_current_chunk_, chunk_->payload()); | |
| 123 DCHECK_LE(event_data_start_in_current_chunk_, | |
| 124 chunk_->end() - proto::kMessageLengthFieldSize); | |
| 125 const uint32_t event_partial_size = static_cast<uint32_t>( | |
| 126 stream_writer_.write_ptr() - event_data_start_in_current_chunk_); | |
| 127 proto::WriteRedundantVarIntU32<proto::kMessageLengthFieldSize>( | |
| 128 event_partial_size, event_.size_field().begin); | |
| 129 event_.inc_size_already_written(event_partial_size); | |
| 130 | |
| 131 // If this is a continuation of an event, this writer needs to retain the | |
| 132 // old chunk. The client might still be able to write to it. This is to deal | |
| 133 // with the case of a nested message which is started in one chunk and | |
| 134 // ends in another one. The finalization needs to write-back the size field | |
| 135 // in the old chunk. | |
| 136 new_chunk->set_next_in_owner_list(chunk_); | |
| 137 } else if (chunk_) { | |
| 138 // Otherwise, if this is a new event, the previous chunk can be returned. | |
| 139 trace_ring_buffer_->ReturnChunk(chunk_); | |
| 140 } | |
| 141 chunk_ = new_chunk; | |
| 142 | |
| 143 // Write the protobuf for the chunk header. The generated C++ stub for | |
| 144 // events_chunk.proto cannot be used here because that would re-enter this | |
|
alph
2016/08/04 19:06:52
Why it would reenter. We're at the very beginning
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
SO the situation is the following:
- somebody is
| |
| 145 // class and make this code extremely hard to reason about. | |
| 146 uint8_t* chunk_proto = new_chunk->payload(); | |
| 147 | |
| 148 proto::StaticAssertSingleBytePreamble<ChunkProto::kWriterIdFieldNumber>(); | |
| 149 *chunk_proto++ = static_cast<uint8_t>( | |
| 150 proto::MakeTagVarInt(ChunkProto::kWriterIdFieldNumber)); | |
| 151 chunk_proto = proto::WriteVarIntU32(writer_id_, chunk_proto); | |
| 152 | |
| 153 proto::StaticAssertSingleBytePreamble< | |
| 154 ChunkProto::kSeqIdInStreamFieldNumber>(); | |
| 155 *chunk_proto++ = static_cast<uint8_t>( | |
| 156 proto::MakeTagVarInt(ChunkProto::kSeqIdInStreamFieldNumber)); | |
| 157 chunk_proto = proto::WriteVarIntU32(chunk_seq_id_, chunk_proto); | |
| 158 | |
| 159 proto::StaticAssertSingleBytePreamble< | |
| 160 ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber>(); | |
| 161 *chunk_proto++ = static_cast<uint8_t>(proto::MakeTagVarInt( | |
| 162 ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber)); | |
|
alph
2016/08/04 19:06:52
This is an optional field. We could just skip it i
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
ah good point. yes. done.
| |
| 163 *chunk_proto++ = is_fragmenting_event ? 1u : 0u; | |
| 164 | |
| 165 // At this point we don't know yet whether the last event in the chunk will | |
| 166 // fragment and continue in the next chunk. For the moment we put a zero as a | |
|
alph
2016/08/04 19:06:52
Could we optionally write this field when we're re
Primiano Tucci (use gerrit)
2016/08/05 11:23:38
Ah I think I see the suggestion here.
Instead of r
| |
| 167 // placeholder and remember its position in the chunk. The actual value will | |
| 168 // be written the next time we will take a new chunk (above in this function). | |
| 169 proto::StaticAssertSingleBytePreamble< | |
| 170 ChunkProto::kLastEventContinuesOnNextChunkFieldNumber>(); | |
| 171 *chunk_proto++ = static_cast<uint8_t>(proto::MakeTagVarInt( | |
| 172 ChunkProto::kLastEventContinuesOnNextChunkFieldNumber)); | |
| 173 continue_on_next_chunk_ptr_ = chunk_proto; | |
| 174 *chunk_proto++ = 0; | |
| 175 | |
| 176 ++chunk_seq_id_; | |
| 177 | |
| 178 // If the new chunk was requested while writing an event (the event spans | |
| 179 // across chunks) write a new preamble for the partial event in the new chunk. | |
| 180 if (is_fragmenting_event) | |
| 181 chunk_proto = WriteEventPrambleForNewChunk(chunk_proto); | |
| 182 | |
| 183 return {chunk_proto, new_chunk->end()}; | |
| 184 } | |
| 185 | |
| 186 // Writes the one-byte preamble for the start of either a new or a partial | |
| 187 // event and reserves kMessageLengthFieldSize bytes for its length. Also | |
| 188 // keeps size-field the bookeeping up to date. Returns the pointer in the chunk | |
| 189 // past the event preamble, where the event proto should be written. | |
| 190 uint8_t* TraceBufferWriter::WriteEventPrambleForNewChunk(uint8_t* begin) { | |
| 191 // The caller must have ensured to have enough room in the chunk. The event | |
| 192 // preamble itself cannot be fragmented. | |
| 193 uint8_t* const end = begin + kEventPreambleSize; | |
| 194 proto::StaticAssertSingleBytePreamble<ChunkProto::kEventsFieldNumber>(); | |
| 195 *begin++ = static_cast<uint8_t>( | |
| 196 proto::MakeTagLengthDelimited(ChunkProto::kEventsFieldNumber)); | |
| 197 ContiguousMemoryRange range = {begin, end}; | |
| 198 event_.set_size_field(range); | |
| 199 event_data_start_in_current_chunk_ = end; | |
| 200 return end; | |
| 201 } | |
| 202 | |
| 203 void TraceBufferWriter::Flush() { | |
| 204 FinalizeCurrentEvent(); | |
| 205 FinalizeCurrentChunk(); | |
| 206 trace_ring_buffer_->ReturnChunk(chunk_); | |
| 207 chunk_ = nullptr; | |
| 208 } | |
| 209 | |
| 210 } // namespace v2 | |
| 211 } // namespace tracing | |
| OLD | NEW |