Index: components/tracing/core/trace_buffer_reader.cc |
diff --git a/components/tracing/core/trace_buffer_reader.cc b/components/tracing/core/trace_buffer_reader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8607fc26cf86532a023bb83c37989117d1642502 |
--- /dev/null |
+++ b/components/tracing/core/trace_buffer_reader.cc |
@@ -0,0 +1,223 @@ |
+// 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_reader.h" |
+ |
+#include <cctype> |
+#include <deque> |
+#include <utility> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/strings/string_piece.h" |
+#include "components/tracing/core/proto_utils.h" |
+#include "components/tracing/core/string_interning.h" |
+#include "components/tracing/proto/event.pbzero.h" |
+#include "components/tracing/proto/events_chunk.pbzero.h" |
+ |
+namespace tracing { |
+namespace v2 { |
+ |
+TraceBufferReader::TraceBufferReader(uint32_t options) : options_(options) { |
+ DCHECK((options & kBuildInternedStringsTable) || |
+ (options & kBuildLegacyJSONEvents)); |
+ if (options & kBuildLegacyJSONEvents) |
+ legacy_events_ = base::MakeUnique<base::ListValue>(); |
+} |
+ |
+TraceBufferReader::~TraceBufferReader() {} |
+ |
+void TraceBufferReader::ParseChunk(const uint8_t* const start, size_t length) { |
+ using ChunkProto = pbzero::tracing::proto::EventsChunk; |
+ uint32_t writer_id = 0; |
+ bool has_writer_id = false; |
+ uint32_t seq_id = 0; |
+ bool has_seq_id = false; |
+ bool first_event_continues_from_prev_chunk = false; |
+ bool last_event_continues_on_next_chunk = false; |
+ using Range = std::pair<const uint8_t*, const uint8_t*>; // (begin, end) |
+ std::deque<Range> events; |
+ |
+ const uint8_t* const end = start + length; |
+ if (end == start) |
+ return; // Empty chunk. |
+ |
+ // Iterate over all the proto fields in the chunk. Each chunk proto message |
+ // contains some global properties for the chunk and one submessage for each |
+ // trace event. |
+ const uint8_t *cur, *next; |
+ for (cur = start, next = nullptr; cur < end; cur = next) { |
+ uint32_t field_id; |
+ proto::FieldType field_type; |
+ uint64_t field_intvalue; |
+ const uint8_t* field_payload = nullptr; |
+ next = proto::ParseField(cur, end, &field_id, &field_type, &field_intvalue); |
+ DCHECK(next && next <= end); |
+ if (field_type == proto::kFieldTypeLengthDelimited) { |
+ // In the case of a length-delimited field, |field_intvalue| contains |
+ // the size of the field. |field_payload| points to the start of the |
+ // payload (either a string or a submessage). |
+ field_payload = next - field_intvalue; |
+ DCHECK(cur < field_payload && field_payload <= next); |
+ } |
+ switch (field_id) { |
+ case ChunkProto::kWriterIdFieldNumber: |
+ DCHECK_EQ(proto::kFieldTypeVarInt, field_type); |
+ writer_id = static_cast<uint32_t>(field_intvalue); |
+ has_writer_id = true; |
+ break; |
+ case ChunkProto::kSeqIdFieldNumber: |
+ DCHECK_EQ(proto::kFieldTypeVarInt, field_type); |
+ has_seq_id = true; |
+ seq_id = static_cast<uint32_t>(field_intvalue); |
+ break; |
+ case ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber: |
+ DCHECK_EQ(proto::kFieldTypeVarInt, field_type); |
+ first_event_continues_from_prev_chunk = field_intvalue ? true : false; |
+ break; |
+ case ChunkProto::kLastEventContinuesOnNextChunkFieldNumber: |
+ DCHECK_EQ(proto::kFieldTypeVarInt, field_type); |
+ last_event_continues_on_next_chunk = field_intvalue ? true : false; |
+ break; |
+ case ChunkProto::kEventsFieldNumber: |
+ DCHECK_EQ(proto::kFieldTypeLengthDelimited, field_type); |
+ events.push_back(std::make_pair(field_payload, next)); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+ } |
+ DCHECK_EQ(end, cur); |
+ DCHECK(has_seq_id); |
+ DCHECK(has_writer_id); |
+ |
+ WriterState* writer_state = &writer_state_[writer_id]; |
+ |
+ // When |first_event_continues_from_prev_chunk| is true, the first event is |
+ // a fragment. The fragment will be parsed only if we saw all the previous |
+ // fragments AND there are no more fragments upcoming. |
+ if (first_event_continues_from_prev_chunk) { |
+ DCHECK(!events.empty()); |
+ if (writer_state->last_fragment_is_valid && |
+ seq_id == writer_state->last_seq_id + 1) { |
+ // The chunk sequence is valid (no drops). |
+ writer_state->partial_event_data.insert( |
+ writer_state->partial_event_data.end(), events[0].first, |
+ events[0].second); |
+ writer_state->last_seq_id = seq_id; |
+ |
+ // If: |
+ // - The current chunk contains > 1 event, in which case the current |
+ // fragment was the last one. |
+ // OR |
+ // - The current chunks contains 1 event, but is not flagged to to |
+ // continue in the next chunk. |
+ // All the sequence of fragments that have been merged in |
+ // |partial_event_data| is valid and can be processed now. |
+ if (events.size() > 1 || !last_event_continues_on_next_chunk) { |
+ ParseEvent(&*writer_state->partial_event_data.begin(), |
+ &*writer_state->partial_event_data.end()); |
+ writer_state->Reset(); |
+ } |
+ } else { |
+ // The previous chunk has been missed, dropping a previous fragment. This |
+ // can happen if the trace ring buffer wraps over and the previous chunk |
+ // is recycled. |
+ writer_state->Reset(); |
+ } |
+ events.pop_front(); |
+ } |
+ |
+ size_t event_count = 1; |
+ for (const Range& event : events) { |
+ if (event_count++ == events.size() && last_event_continues_on_next_chunk) { |
+ writer_state->last_fragment_is_valid = true; |
+ writer_state->last_seq_id = seq_id; |
+ writer_state->partial_event_data.insert( |
+ writer_state->partial_event_data.end(), event.first, event.second); |
+ break; |
+ } |
+ ParseEvent(event.first, event.second); |
+ } |
+} |
+ |
+void TraceBufferReader::ParseEvent(const uint8_t* const start, |
+ const uint8_t* const end) { |
+ using EventProto = pbzero::tracing::proto::Event; |
+ std::unique_ptr<base::DictionaryValue> event_value; |
+ if (options_ & kBuildLegacyJSONEvents) |
+ event_value = base::MakeUnique<base::DictionaryValue>(); |
+ |
+ DCHECK_GT(end, start); |
+ const uint8_t *cur, *next; |
+ for (cur = start, next = nullptr; cur < end; cur = next) { |
+ uint32_t field_id; |
+ proto::FieldType field_type; |
+ uint64_t field_intvalue; |
+ next = proto::ParseField(cur, end, &field_id, &field_type, &field_intvalue); |
+ base::StringPiece field_strvalue; |
+ if (field_type == proto::kFieldTypeLengthDelimited) { |
+ const size_t str_len = static_cast<size_t>(field_intvalue); |
+ const uint8_t* const str_start = next - str_len; |
+ DCHECK(cur < str_start && str_start <= next); |
+ field_strvalue = |
+ base::StringPiece(reinterpret_cast<const char*>(str_start), str_len); |
+ } |
+ const char* interned_string = nullptr; |
+ if (field_id == EventProto::kNameIdFieldNumber || |
+ field_id == EventProto::kCategoryIdFieldNumber) { |
+ const int64_t str_id = static_cast<int64_t>(field_intvalue); |
+ interned_string = GetInternedStringValue(str_id); |
+ if (options_ & kBuildInternedStringsTable) |
+ interned_strings_[str_id] = interned_string; |
DmitrySkiba
2016/11/29 18:09:56
Hmm, str_id is int64_t here, but interned_strings_
|
+ } |
+ |
+ if (options_ & kBuildLegacyJSONEvents) { |
+ switch (field_id) { |
+ case EventProto::kTypeFieldNumber: { |
+ char event_phase[] = {static_cast<char>(field_intvalue), '\0'}; |
+ DCHECK(std::isalnum(event_phase[0])); |
+ event_value->SetString("ph", base::StringPiece(event_phase, 1)); |
+ break; |
+ } |
+ case EventProto::kNameStrFieldNumber: |
+ // For the rare cases of TRACE_STR_COPY. |
+ event_value->SetString("name", field_strvalue); |
+ break; |
+ case EventProto::kNameIdFieldNumber: |
+ // For the most common case of event name being a const char* string. |
+ event_value->SetString("name", interned_string); |
+ break; |
+ case EventProto::kCategoryIdFieldNumber: |
+ event_value->SetString("cat", interned_string); |
+ break; |
+ case EventProto::kTimestampFieldNumber: |
+ event_value->SetInteger("ts", static_cast<int>(field_intvalue)); |
+ break; |
+ case EventProto::kThreadTimestampFieldNumber: |
+ event_value->SetInteger("tts", static_cast<int>(field_intvalue)); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+ } |
+ } |
+ DCHECK_EQ(end, cur); |
+ if (options_ & kBuildLegacyJSONEvents) |
+ legacy_events_->Append(std::move(event_value)); |
+} |
+ |
+TraceBufferReader::WriterState::WriterState() { |
+ Reset(); |
+} |
+ |
+TraceBufferReader::WriterState::~WriterState() {} |
+ |
+void TraceBufferReader::WriterState::Reset() { |
+ last_seq_id = 0; |
+ last_fragment_is_valid = false; |
+ partial_event_data.clear(); |
+} |
+ |
+} // namespace v2 |
+} // namespace tracing |