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

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

Issue 2196663002: tracing v2: Introduce TraceBufferWriter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proto_refactor
Patch Set: . Created 4 years, 5 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_writer.h
diff --git a/components/tracing/core/trace_buffer_writer.h b/components/tracing/core/trace_buffer_writer.h
new file mode 100644
index 0000000000000000000000000000000000000000..55c026d8d8736a06bc3089e9036d5ebb2db00099
--- /dev/null
+++ b/components/tracing/core/trace_buffer_writer.h
@@ -0,0 +1,104 @@
+// 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_WRITER_H_
+#define COMPONENTS_TRACING_CORE_TRACE_BUFFER_WRITER_H_
+
+#include "base/macros.h"
+#include "components/tracing/core/proto_zero_message.h"
+#include "components/tracing/core/proto_zero_message_handle.h"
+#include "components/tracing/core/scattered_stream_writer.h"
+#include "components/tracing/core/trace_ring_buffer.h"
+#include "components/tracing/tracing_export.h"
+
+namespace tracing {
+
+// TODO(primiano): in next CL the Event class will just come from the C++
+// header generated by the ProtoZero plugin.
+namespace proto {
+class Event : public v2::ProtoZeroMessage {};
+}
+
+namespace v2 {
+
+using TraceEventHandle = ProtoZeroMessageHandle<proto::Event>;
+
+// This class is the entry-point to add events to the TraceRingBuffer.
+// It acts as a glue layer between the protobuf classes (ProtoZeroMessage) and
+// the chunked trace ring buffer (TraceRingBuffer). This class is deliberately
+// NOT thread safe. The expected design is that each thread owns an instance of
+// TraceBufferWriter and that trace events produced on one thread are not passed
+// to other threads.
+class TRACING_EXPORT TraceBufferWriter
+ : public ScatteredStreamWriter::Delegate {
+ public:
+ // |trace_buffer| is the underlying ring buffer for taking and returning
alph 2016/08/04 19:06:52 you're referencing the arg name which is missing i
Primiano Tucci (use gerrit) 2016/08/05 11:23:38 Done.
+ // chunks. |writer_id| is an identifier, unique for each instance, which is
+ // appended to the header of each chunk. Its purpose is to allow the importer
+ // to reconstruct the logical sequence of chunks for a given writer. Think to
+ // this as a thread-id (just, in rare cases, some threads can have more than
+ // one writer).
+ TraceBufferWriter(TraceRingBuffer*, uint32_t writer_id);
+ ~TraceBufferWriter();
alph 2016/08/04 19:06:52 Could you please make the Delegate destructor virt
Primiano Tucci (use gerrit) 2016/08/05 11:23:38 oh yeah sure.
+
+ // Adds a new event and returns a handle to it. The new event is valid (can be
+ // populated) until the next call to AddEvent(). The new event is finalized
+ // and fully committed to the ring buffer on the next call to AddEvent() or
+ // when the returned handle goes out of scope, whichever comes first.
+ TraceEventHandle AddEvent();
+
+ // ScatteredStreamWriter::Delegate implementation.
+ // Called by the ProtoZeroMessage's ScatteredStreamWriter when the caller
+ // tries to append a new field and the write overflows the current chunk.
+ ContiguousMemoryRange GetNewBuffer() override;
+
+ // Finalize the pending event (if any) and returns all chunks.
alph 2016/08/04 19:06:52 doesn't seem to return anything.
Primiano Tucci (use gerrit) 2016/08/05 11:23:38 Reworded the comment. I meant "returns back" the c
+ void Flush();
+
+ const ScatteredStreamWriter& stream_writer() const { return stream_writer_; }
+
+ uint32_t writer_id() const { return writer_id_; }
+
+ private:
+ void FinalizeCurrentEvent();
+ void FinalizeCurrentChunk();
+ ContiguousMemoryRange AcquireNewChunk(bool event_continues_from_prev_chunk);
+ uint8_t* WriteEventPrambleForNewChunk(uint8_t* begin);
+
+ TraceRingBuffer* trace_ring_buffer_;
+
+ // Unique id of this writer (see comment in the ctor).
+ const uint32_t writer_id_;
+
+ // Monotonic counter (within the scope of writer_id_) of chunks.
+ uint32_t chunk_seq_id_;
+
+ // The last chunk acquired from the ring buffer. nullptr before the first call
+ // to AddEvent(). Each instance can own more than one chunk at any given time
+ // (to deal with messages larger than a chunk). This is being tracked through
+ // the singly linked list Chunk::next_in_owner_list().
+ TraceRingBuffer::Chunk* chunk_;
+
+ // Pointer to the byte which represents the Chunk's proto field
+ // |continue_on_next_chunk| (a boolean).
+ uint8_t* continue_on_next_chunk_ptr_;
+
+ // Used to work out how many bytes for the current |event_| lie in the current
+ // |chunk_|.
+ uint8_t* event_data_start_in_current_chunk_;
+
+ ScatteredStreamWriter stream_writer_;
+
+ // This field should be a proto::Event. However, ProtoZeroMessage subclasses
+ // are stateless by design. This avoids to pull the full tree of autogenerated
+ // headers for the stub classes and reduce build time.
+ ProtoZeroMessage event_;
+
+ DISALLOW_COPY_AND_ASSIGN(TraceBufferWriter);
+};
+
+} // namespace v2
+} // namespace tracing
+
+#endif // COMPONENTS_TRACING_CORE_TRACE_BUFFER_WRITER_H_

Powered by Google App Engine
This is Rietveld 408576698