11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_LIBPLATFORM_TRACING_TRACE_BUFFER_H_
61cb0ef41Sopenharmony_ci#define V8_LIBPLATFORM_TRACING_TRACE_BUFFER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "include/libplatform/v8-tracing.h"
121cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace platform {
161cb0ef41Sopenharmony_cinamespace tracing {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciclass TraceBufferRingBuffer : public TraceBuffer {
191cb0ef41Sopenharmony_ci public:
201cb0ef41Sopenharmony_ci  // Takes ownership of |trace_writer|.
211cb0ef41Sopenharmony_ci  TraceBufferRingBuffer(size_t max_chunks, TraceWriter* trace_writer);
221cb0ef41Sopenharmony_ci  ~TraceBufferRingBuffer() override = default;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  TraceObject* AddTraceEvent(uint64_t* handle) override;
251cb0ef41Sopenharmony_ci  TraceObject* GetEventByHandle(uint64_t handle) override;
261cb0ef41Sopenharmony_ci  bool Flush() override;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci private:
291cb0ef41Sopenharmony_ci  uint64_t MakeHandle(size_t chunk_index, uint32_t chunk_seq,
301cb0ef41Sopenharmony_ci                      size_t event_index) const;
311cb0ef41Sopenharmony_ci  void ExtractHandle(uint64_t handle, size_t* chunk_index, uint32_t* chunk_seq,
321cb0ef41Sopenharmony_ci                     size_t* event_index) const;
331cb0ef41Sopenharmony_ci  size_t Capacity() const { return max_chunks_ * TraceBufferChunk::kChunkSize; }
341cb0ef41Sopenharmony_ci  size_t NextChunkIndex(size_t index) const;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  mutable base::Mutex mutex_;
371cb0ef41Sopenharmony_ci  size_t max_chunks_;
381cb0ef41Sopenharmony_ci  std::unique_ptr<TraceWriter> trace_writer_;
391cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<TraceBufferChunk>> chunks_;
401cb0ef41Sopenharmony_ci  size_t chunk_index_;
411cb0ef41Sopenharmony_ci  bool is_empty_ = true;
421cb0ef41Sopenharmony_ci  uint32_t current_chunk_seq_ = 1;
431cb0ef41Sopenharmony_ci};
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci}  // namespace tracing
461cb0ef41Sopenharmony_ci}  // namespace platform
471cb0ef41Sopenharmony_ci}  // namespace v8
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci#endif  // V8_LIBPLATFORM_TRACING_TRACE_BUFFER_H_
50