| Index: base/trace_event/v2/ring_buffer.h
|
| diff --git a/base/trace_event/v2/ring_buffer.h b/base/trace_event/v2/ring_buffer.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cf3f7fff3003e7d0aaee8ecaed14825f530fde86
|
| --- /dev/null
|
| +++ b/base/trace_event/v2/ring_buffer.h
|
| @@ -0,0 +1,112 @@
|
| +// 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 BASE_TRACE_EVENT_V2_RING_BUFFER_H_
|
| +#define BASE_TRACE_EVENT_V2_RING_BUFFER_H_
|
| +
|
| +#include <inttypes.h>
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/atomicops.h"
|
| +#include "base/base_export.h"
|
| +#include "base/macros.h"
|
| +#include "base/synchronization/lock.h"
|
| +#include "base/threading/thread.h"
|
| +#include "base/trace_event/v2/scattered_buffer.h"
|
| +#include "base/trace_event/v2/trace_event.h"
|
| +
|
| +namespace tracing {
|
| + class EventsChunk;
|
| +}
|
| +
|
| +namespace base {
|
| +namespace trace_event {
|
| +namespace v2 {
|
| +
|
| +// This class deals only with raw storage, doesn't know anything about categories and such.
|
| +
|
| +class BASE_EXPORT RingBuffer {
|
| + public:
|
| +
|
| + class Chunk {
|
| + public:
|
| + using Header = subtle::Atomic32;
|
| + Chunk();
|
| + ~Chunk();
|
| +
|
| + uint8_t* begin() const { return begin_; }
|
| + uint8_t* proto_begin() const { return begin_ + sizeof(Header); }
|
| + uint8_t* end() const { return end_; }
|
| + void set_proto_used_size(uint32_t size) { subtle::NoBarrier_Store(header(), size); }
|
| + uint32_t proto_used_size() const { return subtle::NoBarrier_Load(header()); }
|
| +
|
| + private:
|
| + friend class RingBuffer;
|
| +
|
| + Header* header() const { return reinterpret_cast<Header*>(begin_); }
|
| + bool is_returned() const { return owner_ == kInvalidThreadId; }
|
| + void set_returned() { owner_ = kInvalidThreadId; }
|
| + void set_owner(PlatformThreadId tid) { owner_ = tid; }
|
| +
|
| + uint8_t* begin_;
|
| + uint8_t* end_;
|
| +
|
| + // Accesses to owner_ must happen under the buffer |lock_|.
|
| + PlatformThreadId owner_; // kInvalidThreadId means free/returned;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(Chunk);
|
| + };
|
| +
|
| + RingBuffer(uint8_t* begin, size_t size);
|
| + ~RingBuffer();
|
| +
|
| + Chunk* TakeChunk();
|
| + void ReturnChunk(Chunk* chunk, uint32_t used_size);
|
| +
|
| + private:
|
| + uint8_t* const begin_;
|
| + uint8_t* const end_;
|
| +
|
| + Lock lock_;
|
| + std::unique_ptr<Chunk[]> chunks_;
|
| + const size_t num_chunks_;
|
| + size_t current_chunk_idx_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(RingBuffer);
|
| +};
|
| +
|
| +// Typically there will be one instance of this per thread. This is going to
|
| +// be owned by some thread-local tracing thing.
|
| +class BASE_EXPORT ZeroCopyTraceBufferWriter : public ScatteredBuffer::Delegate {
|
| + public:
|
| + ZeroCopyTraceBufferWriter(RingBuffer* trace_buffer, uint32_t stream_id);
|
| + ~ZeroCopyTraceBufferWriter();
|
| +
|
| + TraceEventHandle AddEvent();
|
| +
|
| + // ScatteredBuffer::Delegate implementation.
|
| + ScatteredBuffer::ContiguousMemoryRange GetNewContiguousMemoryBuffer() override;
|
| +
|
| + private:
|
| + ScatteredBuffer::ContiguousMemoryRange AcquireNewChunk(bool event_continues_from_prev_chunk);
|
| +
|
| + RingBuffer* ring_buffer_;
|
| + RingBuffer::Chunk* chunk_;
|
| + uint32_t stream_id_;
|
| + uint32_t chunk_seq_id_;
|
| + uint8_t* continue_on_next_chunk_ptr_;
|
| + uint8_t* event_start_addr_;
|
| + TraceEvent* event_;
|
| + uint8_t event_storage_[sizeof(TraceEvent)];
|
| + ScatteredBuffer scattered_buffer_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ZeroCopyTraceBufferWriter);
|
| +};
|
| +
|
| +} // namespace v2
|
| +} // namespace trace_event
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TRACE_EVENT_V2_RING_BUFFER_H_
|
|
|