Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: components/tracing/core/trace_buffer_writer.cc

Issue 2196663002: tracing v2: Introduce TraceBufferWriter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proto_refactor
Patch Set: Add TRACING_EXPORT also to nested class Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 event_data_start_in_current_chunk_(nullptr),
39 stream_writer_(this) {
40 event_.Reset(&stream_writer_);
41 }
42
43 TraceBufferWriter::~TraceBufferWriter() {}
44
45 void TraceBufferWriter::FinalizeCurrentEvent() {
46 if (UNLIKELY(!chunk_))
47 return;
48
49 // Finalize the last event added. This ensures that it and all its nested
50 // fields are committed to the ring buffer and sealed. No further changes to
51 // the chunks's memory can be made from the |event_| after this point.
52 event_.Finalize();
53
54 // In the unlikely event that the last event did wrap over one or more chunks,
55 // is is now time to return those chunks (all but the active one) back.
56 TraceRingBuffer::Chunk* retained_chunk = chunk_->next_in_owner_list();
57 if (UNLIKELY(retained_chunk)) {
58 while (retained_chunk) {
59 TraceRingBuffer::Chunk* next = retained_chunk->next_in_owner_list();
60 retained_chunk->set_next_in_owner_list(nullptr);
61 trace_ring_buffer_->ReturnChunk(retained_chunk);
62 retained_chunk = next;
63 }
64 chunk_->set_next_in_owner_list(nullptr);
65 }
66 }
67
68 TraceEventHandle TraceBufferWriter::AddEvent() {
69 FinalizeCurrentEvent();
70
71 // In order to start a new event at least kMessageLengthFieldSize + 1 bytes
72 // are required in the chunk to write the preamble and size of the event
73 // itself. We take a bit more room here, it doesn't make a lot of sense
74 // starting a partial event that will fragment immediately after.
75 // TODO(primiano): replace 16 with a more reasonable size, that is, the size
76 // of a simple trace event with no args.
77 if (stream_writer_.bytes_available() < 16)
oystein (OOO til 10th of July) 2016/08/05 23:04:35 nit: make this a named constant at least? Or is th
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 Made a constant and moved the todo up there. It's
78 stream_writer_.Reset(AcquireNewChunk(false /* is_fragmenting_event */));
79
80 event_.Reset(&stream_writer_);
81 WriteEventPreambleForNewChunk(
82 stream_writer_.ReserveBytesUnsafe(kEventPreambleSize));
83 DCHECK_EQ(stream_writer_.write_ptr(), event_data_start_in_current_chunk_);
84 return TraceEventHandle(static_cast<::tracing::proto::Event*>(&event_));
85 }
86
87 // This is invoked by the ProtoZeroMessage write methods when reaching the
88 // end of the current chunk during a write.
89 ContiguousMemoryRange TraceBufferWriter::GetNewBuffer() {
90 return AcquireNewChunk(true /* is_fragmenting_event */);
91 }
92
93 void TraceBufferWriter::FinalizeCurrentChunk(bool is_fragmenting_event) {
94 DCHECK(!is_fragmenting_event || chunk_);
95 if (!chunk_)
96 return;
97 DCHECK_GE(stream_writer_.write_ptr(), chunk_->payload());
98 DCHECK_LE(stream_writer_.write_ptr(), chunk_->end() - 2);
99 size_t used_size = stream_writer_.write_ptr() - chunk_->payload();
oystein (OOO til 10th of July) 2016/08/05 23:04:32 nit: size_t and then casting to uint32 after using
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 Done.
100
101 if (is_fragmenting_event) {
102 uint8_t* write_ptr = stream_writer_.write_ptr();
103 proto::StaticAssertSingleBytePreamble<
104 ChunkProto::kLastEventContinuesOnNextChunkFieldNumber>();
105 *write_ptr++ = static_cast<uint8_t>(proto::MakeTagVarInt(
106 ChunkProto::kLastEventContinuesOnNextChunkFieldNumber));
107 *write_ptr = 1;
oystein (OOO til 10th of July) 2016/08/05 23:04:35 What's the 1?
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 1 == true -> the event does continue on the next c
108 used_size += 2;
oystein (OOO til 10th of July) 2016/08/05 23:04:32 Can you just store an original_write_ptr instead o
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 Done.
109 }
110
111 chunk_->set_used_size(static_cast<uint32_t>(used_size));
112 }
113
114 // There are paths that lead to AcquireNewChunk():
115 // When |is_fragmenting_event| = false:
116 // AddEvent() is called and there isn't enough room in the current chunk to
117 // start a new event (or we don't have a chunk yet).
118 // When |is_fragmenting_event| = true:
119 // The client is writing an event, a ProtoZeroMessage::Append* method hits
120 // the boundary of the chunk and requests a new one via GetNewBuffer().
121 ContiguousMemoryRange TraceBufferWriter::AcquireNewChunk(
122 bool is_fragmenting_event) {
123 FinalizeCurrentChunk(is_fragmenting_event);
124 TraceRingBuffer::Chunk* new_chunk = trace_ring_buffer_->TakeChunk(writer_id_);
125 if (is_fragmenting_event) {
126 // Backfill the size field of the event with the partial size accumulated
127 // so far in the old chunk. WriteEventPreambleForNewChunk() will take care
128 // of resetting the |size_field| of the event to the new chunk.
129 DCHECK_GE(event_data_start_in_current_chunk_, chunk_->payload());
130 DCHECK_LE(event_data_start_in_current_chunk_,
131 chunk_->end() - proto::kMessageLengthFieldSize);
132 const uint32_t event_partial_size = static_cast<uint32_t>(
133 stream_writer_.write_ptr() - event_data_start_in_current_chunk_);
134 proto::WriteRedundantVarIntU32<proto::kMessageLengthFieldSize>(
135 event_partial_size, event_.size_field().begin);
136 event_.inc_size_already_written(event_partial_size);
137
138 // If this is a continuation of an event, this writer needs to retain the
139 // old chunk. The client might still be able to write to it. This is to deal
140 // with the case of a nested message which is started in one chunk and
141 // ends in another one. The finalization needs to write-back the size field
142 // in the old chunk.
143 new_chunk->set_next_in_owner_list(chunk_);
144 } else if (chunk_) {
145 // Otherwise, if this is a new event, the previous chunk can be returned.
146 trace_ring_buffer_->ReturnChunk(chunk_);
147 }
148 chunk_ = new_chunk;
149
150 // Write the protobuf for the chunk header. The generated C++ stub for
151 // events_chunk.proto cannot be used here because that would re-enter this
152 // class and make this code extremely hard to reason about.
153 uint8_t* chunk_proto = new_chunk->payload();
154
155 proto::StaticAssertSingleBytePreamble<ChunkProto::kWriterIdFieldNumber>();
156 *chunk_proto++ = static_cast<uint8_t>(
157 proto::MakeTagVarInt(ChunkProto::kWriterIdFieldNumber));
158 chunk_proto = proto::WriteVarIntU32(writer_id_, chunk_proto);
159
160 proto::StaticAssertSingleBytePreamble<
161 ChunkProto::kSeqIdInStreamFieldNumber>();
162 *chunk_proto++ = static_cast<uint8_t>(
163 proto::MakeTagVarInt(ChunkProto::kSeqIdInStreamFieldNumber));
164 chunk_proto = proto::WriteVarIntU32(chunk_seq_id_, chunk_proto);
165
166 if (is_fragmenting_event) {
167 proto::StaticAssertSingleBytePreamble<
168 ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber>();
169 *chunk_proto++ = static_cast<uint8_t>(proto::MakeTagVarInt(
170 ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber));
171 *chunk_proto++ = 1;
oystein (OOO til 10th of July) 2016/08/05 23:04:32 What does the 1 represent?
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 as above, the value of the bool.
172 }
173
174 ++chunk_seq_id_;
175
176 // If the new chunk was requested while writing an event (the event spans
177 // across chunks) write a new preamble for the partial event in the new chunk.
178 if (is_fragmenting_event)
179 chunk_proto = WriteEventPreambleForNewChunk(chunk_proto);
180
181 // We reserve 2 bytes from the end, so that FinalizeCurrentChunk() can use
182 // them to write the |last_event_continues_on_next_chunk| field.
183 return {chunk_proto, new_chunk->end() - 2};
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
oystein (OOO til 10th of July) 2016/08/05 23:04:33 nit: bookkeeping
Primiano Tucci (use gerrit) 2016/08/08 19:36:23 Done.
189 // past the event preamble, where the event proto should be written.
190 uint8_t* TraceBufferWriter::WriteEventPreambleForNewChunk(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(false /* is_fragmenting_event */);
206 trace_ring_buffer_->ReturnChunk(chunk_);
207 chunk_ = nullptr;
208 }
209
210 } // namespace v2
211 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/core/trace_buffer_writer.h ('k') | components/tracing/core/trace_buffer_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698