File: trace_buffer_writer.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (206 lines) | stat: -rw-r--r-- 8,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/tracing/core/trace_buffer_writer.h"

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "components/tracing/core/proto_utils.h"
#include "components/tracing/proto/event.pbzero.h"
#include "components/tracing/proto/events_chunk.pbzero.h"

namespace tracing {
namespace v2 {

namespace {

using ChunkProto = pbzero::tracing::proto::EventsChunk;

const size_t kEventPreambleSize = 1 + proto::kMessageLengthFieldSize;

// TODO(primiano): replace 16 with a more reasonable size, that is, the size
// of a simple trace event with no args.
const size_t kMinEventSize = 16;

}  // namespace

TraceBufferWriter::TraceBufferWriter(TraceRingBuffer* trace_ring_buffer,
                                     uint32_t writer_id)
    : trace_ring_buffer_(trace_ring_buffer),
      writer_id_(writer_id),
      chunk_seq_id_(0),
      chunk_(nullptr),
      event_data_start_in_current_chunk_(nullptr),
      stream_writer_(this) {
  event_.Reset(&stream_writer_);
}

TraceBufferWriter::~TraceBufferWriter() {}

void TraceBufferWriter::FinalizeCurrentEvent() {
  if (UNLIKELY(!chunk_))
    return;

  // Finalize the last event added. This ensures that it and all its nested
  // fields are committed to the ring buffer and sealed. No further changes to
  // the chunks's memory can be made from the |event_| after this point.
  event_.Finalize();

  // In the unlikely event that the last event did wrap over one or more chunks,
  // is is now time to return those chunks (all but the active one) back.
  TraceRingBuffer::Chunk* retained_chunk = chunk_->next_in_owner_list();
  if (UNLIKELY(retained_chunk)) {
    while (retained_chunk) {
      TraceRingBuffer::Chunk* next = retained_chunk->next_in_owner_list();
      retained_chunk->set_next_in_owner_list(nullptr);
      trace_ring_buffer_->ReturnChunk(retained_chunk);
      retained_chunk = next;
    }
    chunk_->set_next_in_owner_list(nullptr);
  }
}

TraceEventHandle TraceBufferWriter::AddEvent() {
  FinalizeCurrentEvent();

  // In order to start a new event at least kMessageLengthFieldSize + 1 bytes
  // are required in the chunk to write the preamble and size of the event
  // itself. We take a bit more room here, it doesn't make a lot of sense
  // starting a partial event that will fragment immediately after.
  static_assert(kMinEventSize >= proto::kMessageLengthFieldSize + 1,
                "kMinEventSize too small");
  if (stream_writer_.bytes_available() < kMinEventSize)
    stream_writer_.Reset(AcquireNewChunk(false /* is_fragmenting_event */));

  event_.Reset(&stream_writer_);
  WriteEventPreambleForNewChunk(
      stream_writer_.ReserveBytesUnsafe(kEventPreambleSize));
  DCHECK_EQ(stream_writer_.write_ptr(), event_data_start_in_current_chunk_);
  return TraceEventHandle(static_cast<pbzero::tracing::proto::Event*>(&event_));
}

// This is invoked by the ProtoZeroMessage write methods when reaching the
// end of the current chunk during a write.
ContiguousMemoryRange TraceBufferWriter::GetNewBuffer() {
  return AcquireNewChunk(true /* is_fragmenting_event */);
}

void TraceBufferWriter::FinalizeCurrentChunk(bool is_fragmenting_event) {
  DCHECK(!is_fragmenting_event || chunk_);
  if (!chunk_)
    return;
  uint8_t* write_ptr = stream_writer_.write_ptr();
  DCHECK_GE(write_ptr, chunk_->payload());
  DCHECK_LE(write_ptr, chunk_->end() - 2);

  if (is_fragmenting_event) {
    proto::StaticAssertSingleBytePreamble<
        ChunkProto::kLastEventContinuesOnNextChunkFieldNumber>();
    *write_ptr++ = static_cast<uint8_t>(proto::MakeTagVarInt(
        ChunkProto::kLastEventContinuesOnNextChunkFieldNumber));
    *write_ptr++ = 1;  // = true.
  }

  DCHECK_LT(static_cast<uintptr_t>(write_ptr - chunk_->payload()), kChunkSize);
  chunk_->set_used_size(static_cast<uint32_t>(write_ptr - chunk_->payload()));
}

// There are paths that lead to AcquireNewChunk():
// When |is_fragmenting_event| = false:
//   AddEvent() is called and there isn't enough room in the current chunk to
//   start a new event (or we don't have a chunk yet).
// When |is_fragmenting_event| = true:
//   The client is writing an event, a ProtoZeroMessage::Append* method hits
//   the boundary of the chunk and requests a new one via GetNewBuffer().
ContiguousMemoryRange TraceBufferWriter::AcquireNewChunk(
    bool is_fragmenting_event) {
  FinalizeCurrentChunk(is_fragmenting_event);
  TraceRingBuffer::Chunk* new_chunk = trace_ring_buffer_->TakeChunk(writer_id_);
  if (is_fragmenting_event) {
    // Backfill the size field of the event with the partial size accumulated
    // so far in the old chunk. WriteEventPreambleForNewChunk() will take care
    // of resetting the |size_field| of the event to the new chunk.
    DCHECK_GE(event_data_start_in_current_chunk_, chunk_->payload());
    DCHECK_LE(event_data_start_in_current_chunk_,
              chunk_->end() - proto::kMessageLengthFieldSize);
    const uint32_t event_partial_size = static_cast<uint32_t>(
        stream_writer_.write_ptr() - event_data_start_in_current_chunk_);
    proto::WriteRedundantVarInt(event_partial_size, event_.size_field().begin);
    event_.inc_size_already_written(event_partial_size);

    // If this is a continuation of an event, this writer needs to retain the
    // old chunk. The client might still be able to write to it. This is to deal
    // with the case of a nested message which is started in one chunk and
    // ends in another one. The finalization needs to write-back the size field
    // in the old chunk.
    new_chunk->set_next_in_owner_list(chunk_);
  } else if (chunk_) {
    // Otherwise, if this is a new event, the previous chunk can be returned.
    trace_ring_buffer_->ReturnChunk(chunk_);
  }
  chunk_ = new_chunk;

  // Write the protobuf for the chunk header. The generated C++ stub for
  // events_chunk.proto cannot be used here because that would re-enter this
  // class and make this code extremely hard to reason about.
  uint8_t* chunk_proto = new_chunk->payload();

  proto::StaticAssertSingleBytePreamble<ChunkProto::kWriterIdFieldNumber>();
  *chunk_proto++ = static_cast<uint8_t>(
      proto::MakeTagVarInt(ChunkProto::kWriterIdFieldNumber));
  chunk_proto = proto::WriteVarInt(writer_id_, chunk_proto);

  proto::StaticAssertSingleBytePreamble<ChunkProto::kSeqIdFieldNumber>();
  *chunk_proto++ =
      static_cast<uint8_t>(proto::MakeTagVarInt(ChunkProto::kSeqIdFieldNumber));
  chunk_proto = proto::WriteVarInt(chunk_seq_id_, chunk_proto);

  if (is_fragmenting_event) {
    proto::StaticAssertSingleBytePreamble<
        ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber>();
    *chunk_proto++ = static_cast<uint8_t>(proto::MakeTagVarInt(
        ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber));
    *chunk_proto++ = 1;  // = true.
  }

  ++chunk_seq_id_;

  // If the new chunk was requested while writing an event (the event spans
  // across chunks) write a new preamble for the partial event in the new chunk.
  if (is_fragmenting_event)
    chunk_proto = WriteEventPreambleForNewChunk(chunk_proto);

  // We reserve 2 bytes from the end, so that FinalizeCurrentChunk() can use
  // them to write the |last_event_continues_on_next_chunk| field.
  return {chunk_proto, new_chunk->end() - 2};
}

// Writes the one-byte preamble for the start of either a new or a partial
// event and reserves kMessageLengthFieldSize bytes for its length. Also
// keeps size-field the bookkeeping up to date. Returns the pointer in the chunk
// past the event preamble, where the event proto should be written.
uint8_t* TraceBufferWriter::WriteEventPreambleForNewChunk(uint8_t* begin) {
  // The caller must have ensured to have enough room in the chunk. The event
  // preamble itself cannot be fragmented.
  uint8_t* const end = begin + kEventPreambleSize;
  proto::StaticAssertSingleBytePreamble<ChunkProto::kEventsFieldNumber>();
  *begin++ = static_cast<uint8_t>(
      proto::MakeTagLengthDelimited(ChunkProto::kEventsFieldNumber));
  ContiguousMemoryRange range = {begin, end};
  event_.set_size_field(range);
  event_data_start_in_current_chunk_ = end;
  return end;
}

void TraceBufferWriter::Flush() {
  if (!chunk_)
    return;
  FinalizeCurrentEvent();
  FinalizeCurrentChunk(false /* is_fragmenting_event */);
  trace_ring_buffer_->ReturnChunk(chunk_);
  chunk_ = nullptr;
}

}  // namespace v2
}  // namespace tracing