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

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

Issue 2308583003: tracing v2: Introduce TraceBufferReader to read-back the trace buffer (Closed)
Patch Set: some fixes + moar tests Created 4 years, 3 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_reader.h"
6
7 #include <ctype.h>
8
9 #include <deque>
10 #include <utility>
11
12 #include "base/memory/ptr_util.h"
13 #include "base/strings/string_piece.h"
14 #include "components/tracing/core/proto_utils.h"
15 #include "components/tracing/core/string_interning.h"
16 #include "components/tracing/proto/event.pbzero.h"
17 #include "components/tracing/proto/events_chunk.pbzero.h"
18
19 namespace tracing {
20 namespace v2 {
21
22 TraceBufferReader::TraceBufferReader(uint32_t options) : options_(options) {
23 DCHECK((options & kBuildInternedStringsTable) ||
24 (options & kBuildLegacyJSONEvents));
25 if (options & kBuildLegacyJSONEvents)
26 legacy_events_ = base::MakeUnique<base::ListValue>();
27 }
28
29 TraceBufferReader::~TraceBufferReader() {}
30
31 void TraceBufferReader::ParseChunk(const TraceRingBuffer::Chunk* chunk) {
32 // At the moment we support only chunks that have been returned.
33 DCHECK(!chunk->is_owned());
34 ParseChunk(chunk->payload(), chunk->used_size());
35 }
36
37 void TraceBufferReader::ParseChunk(const uint8_t* const start, size_t length) {
38 using ChunkProto = pbzero::tracing::proto::EventsChunk;
39 uint32_t writer_id = 0;
40 bool has_writer_id = false;
41 uint32_t seq_id = 0;
42 bool has_seq_id = false;
43 bool first_event_continues_from_prev_chunk = false;
44 bool last_event_continues_on_next_chunk = false;
45 using Range = std::pair<const uint8_t*, const uint8_t*>; // (begin, end)
46 std::deque<Range> events;
47
48 const uint8_t* const end = start + length;
49 if (end == start)
50 return; // Empty chunk.
51
52 const uint8_t* pos = start;
53 while (pos < end) {
54 uint32_t field_id;
55 proto::FieldType field_type;
56 uint64_t field_intvalue;
57 const uint8_t* field_payload = nullptr;
58 pos = proto::ParseField(pos, end, &field_id, &field_type, &field_intvalue);
59 if (field_type == proto::kFieldTypeLengthDelimited)
60 field_payload = pos - field_intvalue;
oystein (OOO til 10th of July) 2016/09/09 00:27:35 Can we do a DCHECK_LT(field_intvalue, limits::nume
Primiano Tucci (use gerrit) 2016/09/13 14:40:25 The reason I didn't put it is because ParseField h
61 DCHECK(pos);
62 switch (field_id) {
63 case ChunkProto::kWriterIdFieldNumber:
64 DCHECK_EQ(proto::kFieldTypeVarInt, field_type);
65 writer_id = static_cast<uint32_t>(field_intvalue);
66 has_writer_id = true;
67 break;
68 case ChunkProto::kSeqIdFieldNumber:
69 DCHECK_EQ(proto::kFieldTypeVarInt, field_type);
70 has_seq_id = true;
71 seq_id = static_cast<uint32_t>(field_intvalue);
72 break;
73 case ChunkProto::kFirstEventContinuesFromPrevChunkFieldNumber:
74 DCHECK_EQ(proto::kFieldTypeVarInt, field_type);
75 first_event_continues_from_prev_chunk = field_intvalue ? true : false;
76 break;
77 case ChunkProto::kLastEventContinuesOnNextChunkFieldNumber:
78 DCHECK_EQ(proto::kFieldTypeVarInt, field_type);
79 last_event_continues_on_next_chunk = field_intvalue ? true : false;
80 break;
81 case ChunkProto::kEventsFieldNumber:
82 DCHECK_EQ(proto::kFieldTypeLengthDelimited, field_type);
83 events.push_back(std::make_pair(field_payload, pos));
84 break;
85 default:
86 NOTREACHED();
87 }
88 }
89
90 DCHECK(has_seq_id);
91 DCHECK(has_writer_id);
92 WriterState* writer_state = &writer_state_[writer_id];
oystein (OOO til 10th of July) 2016/09/09 00:27:35 I'm finding it hard to reason about the usage of t
Primiano Tucci (use gerrit) 2016/09/13 14:40:25 Allright, I added a comment to the WriterState str
93
94 // When |first_event_continues_from_prev_chunk| is true, the first event is
95 // a fragment. The fragment will be parsed only if we saw all the previous
96 // fragments AND there are no more fragments upcoming.
97 if (first_event_continues_from_prev_chunk) {
98 DCHECK(!events.empty());
99 if (writer_state->last_fragment_is_valid &&
100 seq_id == writer_state->last_seq_id + 1) {
101 writer_state->partial_event_data.insert(
102 writer_state->partial_event_data.end(), events[0].first,
103 events[0].second);
104 writer_state->last_seq_id = seq_id;
105 if (events.size() > 1 || !last_event_continues_on_next_chunk) {
106 ParseEvent(&*writer_state->partial_event_data.begin(),
107 &*writer_state->partial_event_data.end());
108 writer_state->Reset();
109 }
110 } else {
111 // We missed the previous chunk, dropping the first partial event. This
112 // can happen if the trace ring buffer wraps over and the previous chunk
113 // is recycled.
114 writer_state->Reset();
115 }
116 events.pop_front();
117 }
118
119 size_t event_count = 1;
120 for (const Range& event : events) {
121 if (event_count++ == events.size() && last_event_continues_on_next_chunk) {
122 writer_state->last_fragment_is_valid = true;
123 writer_state->last_seq_id = seq_id;
124 writer_state->partial_event_data.insert(
kraynov 2016/09/08 16:10:01 move "insert" to new line. Hard to read.
Primiano Tucci (use gerrit) 2016/09/13 14:40:25 Done.
125 writer_state->partial_event_data.end(), event.first, event.second);
126 break;
127 }
128 ParseEvent(event.first, event.second);
129 }
130 }
131
132 void TraceBufferReader::ParseEvent(const uint8_t* const start,
133 const uint8_t* const end) {
134 using EventProto = pbzero::tracing::proto::Event;
135 std::unique_ptr<base::DictionaryValue> event_value;
136 if (options_ & kBuildLegacyJSONEvents)
137 event_value = base::MakeUnique<base::DictionaryValue>();
138
139 DCHECK_GT((void*)end, (void*)start);
140 const uint8_t* pos = start;
141 while (pos < end) {
142 uint32_t field_id;
143 proto::FieldType field_type;
144 uint64_t field_intvalue;
145 pos = proto::ParseField(pos, end, &field_id, &field_type, &field_intvalue);
146 base::StringPiece field_strvalue;
147 if (field_type == proto::kFieldTypeLengthDelimited) {
148 const size_t len = static_cast<size_t>(field_intvalue);
149 field_strvalue =
150 base::StringPiece(reinterpret_cast<const char*>(pos - len), len);
oystein (OOO til 10th of July) 2016/09/09 00:27:35 DCHECK_LT(pos, len)?
Primiano Tucci (use gerrit) 2016/09/13 14:40:24 Done (more strictly)
151 }
152 const char* interned_string = nullptr;
153 if (field_id == EventProto::kNameIdFieldNumber ||
154 field_id == EventProto::kCategoryIdFieldNumber) {
155 const int64_t str_id = static_cast<int64_t>(field_intvalue);
156 interned_string = GetInternedStringValue(str_id);
157 if (options_ & kBuildInternedStringsTable)
158 interned_strings_[str_id] = interned_string;
159 }
160
161 if (options_ & kBuildLegacyJSONEvents) {
162 switch (field_id) {
oystein (OOO til 10th of July) 2016/09/09 00:27:35 Should there be a default NOTREACHED() here?
Primiano Tucci (use gerrit) 2016/09/13 14:40:25 Hmm good question. So the case to cover here is so
163 case EventProto::kTypeFieldNumber: {
164 char event_phase[2] = {static_cast<char>(field_intvalue), '\0'};
oystein (OOO til 10th of July) 2016/09/09 00:27:34 nit: Can't you leave out the '2' here?
Primiano Tucci (use gerrit) 2016/09/13 14:40:25 Done.
165 DCHECK(std::isalnum(event_phase[0]));
166 event_value->SetString("ph", base::StringPiece(event_phase, 1));
167 break;
168 }
169 case EventProto::kNameStrFieldNumber:
170 // For the rare cases of TRACE_STR_COPY.
171 event_value->SetString("name", field_strvalue);
172 break;
173 case EventProto::kNameIdFieldNumber:
174 // For the most common case of event name being a const char* string.
175 event_value->SetString("name", interned_string);
176 break;
177 case EventProto::kCategoryIdFieldNumber:
178 event_value->SetString("cat", interned_string);
179 break;
180 case EventProto::kTimestampFieldNumber:
181 event_value->SetInteger("ts", static_cast<int>(field_intvalue));
182 break;
183 case EventProto::kThreadTimestampFieldNumber:
184 event_value->SetInteger("tts", static_cast<int>(field_intvalue));
185 break;
186 }
187 }
188 }
189 DCHECK_EQ(end, pos);
190 if (options_ & kBuildLegacyJSONEvents)
191 legacy_events_->Append(std::move(event_value));
192 }
193
194 TraceBufferReader::WriterState::WriterState() {
195 Reset();
196 }
197
198 TraceBufferReader::WriterState::~WriterState() {}
199
200 void TraceBufferReader::WriterState::Reset() {
201 last_seq_id = 0;
202 last_fragment_is_valid = false;
203 partial_event_data.clear();
204 }
205
206 } // namespace v2
207 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698