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

Unified Diff: components/tracing/core/trace_buffer_reader.h

Issue 2308583003: tracing v2: Introduce TraceBufferReader to read-back the trace buffer (Closed)
Patch Set: DISALLOW_COPY_AND_ASSIGN + cctype for isalnum 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 side-by-side diff with in-line comments
Download patch
Index: components/tracing/core/trace_buffer_reader.h
diff --git a/components/tracing/core/trace_buffer_reader.h b/components/tracing/core/trace_buffer_reader.h
new file mode 100644
index 0000000000000000000000000000000000000000..92b49ac27f5bfa04020f5379eae08b797e3ecc32
--- /dev/null
+++ b/components/tracing/core/trace_buffer_reader.h
@@ -0,0 +1,81 @@
+// 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.
+
+#ifndef COMPONENTS_TRACING_CORE_TRACE_BUFFER_READER_H_
+#define COMPONENTS_TRACING_CORE_TRACE_BUFFER_READER_H_
+
+#include <memory>
+#include <unordered_map>
+
+#include "base/values.h"
+#include "components/tracing/core/trace_ring_buffer.h"
+#include "components/tracing/tracing_export.h"
+
+namespace tracing {
+namespace v2 {
+
+// Reads back chunks from the proto-encoded tracing buffer and:
+// - Extracts a table of interned strings, when kBuildInternedStringsTable is
+// enabled.
+// - Reconstructs a JSON-serializable ListValue which is backwards compatible
+// with Tracing v1, when kBuildLegacyJSONEvents.
+class TRACING_EXPORT TraceBufferReader {
+ public:
+ using InternedStringsMap = std::unordered_map<int32_t, const char*>;
+ static const uint32_t kBuildInternedStringsTable = 1 << 0;
+ static const uint32_t kBuildLegacyJSONEvents = 1 << 1;
+
+ explicit TraceBufferReader(uint32_t options);
+ ~TraceBufferReader();
+
+ // Parses a chunk and the events in it and appends data to the string table
+ // and/or the legacy events values depending on |options|.
+ //.The input buffer contain a valid protobuf EventsChunk message
+ // (see ../proto/events_chunk.proto). Chunks must be passed in order, as
+ // produced by the ring buffer, to ensure that all fragmented events are
+ // consistently processed.
+ void ParseChunk(const uint8_t* start, size_t length);
+
+ const InternedStringsMap& interned_strings() const {
+ return interned_strings_;
+ }
+
+ std::unique_ptr<base::ListValue>& legacy_events() { return legacy_events_; }
+
+ private:
+ // This struct keeps track of the evolution of the parser for each writer
+ // (read: for each thread). Each writer produces a sequence of chunks with
+ // contiguos |seq_id|s. However, some of these chunks might be discarded due
+ // to the wrapping logic of the ring buffer. The only problem here are
+ // events fragmented across different chunks. A fragment can be parsed only
+ // if we contiguously saw all the previous chunks and should be discarded
+ // otherwise.
+ struct WriterState {
+ WriterState();
+ ~WriterState();
+
+ void Reset();
+
+ bool last_fragment_is_valid;
+ uint32_t last_seq_id;
+ std::vector<uint8_t> partial_event_data;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(WriterState);
+ };
+
+ void ParseEvent(const uint8_t* start, const uint8_t* end);
+
+ const uint32_t options_;
+ InternedStringsMap interned_strings_;
+ std::unique_ptr<base::ListValue> legacy_events_;
+ std::unordered_map<uint32_t, WriterState> writer_state_;
+
+ DISALLOW_COPY_AND_ASSIGN(TraceBufferReader);
+};
+
+} // namespace v2
+} // namespace tracing
+
+#endif // COMPONENTS_TRACING_CORE_TRACE_BUFFER_READER_H_

Powered by Google App Engine
This is Rietveld 408576698