LCOV - code coverage report
Current view: top level - src/libplatform/tracing - trace-buffer.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 45 53 84.9 %
Date: 2019-04-17 Functions: 5 11 45.5 %

          Line data    Source code
       1             : // Copyright 2016 the V8 project 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 "src/libplatform/tracing/trace-buffer.h"
       6             : 
       7             : namespace v8 {
       8             : namespace platform {
       9             : namespace tracing {
      10             : 
      11          41 : TraceBufferRingBuffer::TraceBufferRingBuffer(size_t max_chunks,
      12             :                                              TraceWriter* trace_writer)
      13          82 :     : max_chunks_(max_chunks) {
      14             :   trace_writer_.reset(trace_writer);
      15          41 :   chunks_.resize(max_chunks);
      16          41 : }
      17             : 
      18        2071 : TraceObject* TraceBufferRingBuffer::AddTraceEvent(uint64_t* handle) {
      19        2071 :   base::MutexGuard guard(&mutex_);
      20        4116 :   if (is_empty_ || chunks_[chunk_index_]->IsFull()) {
      21         110 :     chunk_index_ = is_empty_ ? 0 : NextChunkIndex(chunk_index_);
      22          55 :     is_empty_ = false;
      23             :     auto& chunk = chunks_[chunk_index_];
      24          55 :     if (chunk) {
      25          24 :       chunk->Reset(current_chunk_seq_++);
      26             :     } else {
      27          31 :       chunk.reset(new TraceBufferChunk(current_chunk_seq_++));
      28             :     }
      29             :   }
      30        2071 :   auto& chunk = chunks_[chunk_index_];
      31             :   size_t event_index;
      32             :   TraceObject* trace_object = chunk->AddTraceEvent(&event_index);
      33        4142 :   *handle = MakeHandle(chunk_index_, chunk->seq(), event_index);
      34        2071 :   return trace_object;
      35             : }
      36             : 
      37        1709 : TraceObject* TraceBufferRingBuffer::GetEventByHandle(uint64_t handle) {
      38        1709 :   base::MutexGuard guard(&mutex_);
      39             :   size_t chunk_index, event_index;
      40             :   uint32_t chunk_seq;
      41             :   ExtractHandle(handle, &chunk_index, &chunk_seq, &event_index);
      42        1709 :   if (chunk_index >= chunks_.size()) return nullptr;
      43             :   auto& chunk = chunks_[chunk_index];
      44        1709 :   if (!chunk || chunk->seq() != chunk_seq) return nullptr;
      45        1375 :   return chunk->GetEventAt(event_index);
      46             : }
      47             : 
      48          46 : bool TraceBufferRingBuffer::Flush() {
      49          46 :   base::MutexGuard guard(&mutex_);
      50             :   // This flushes all the traces stored in the buffer.
      51          46 :   if (!is_empty_) {
      52        1054 :     for (size_t i = NextChunkIndex(chunk_index_);; i = NextChunkIndex(i)) {
      53        1054 :       if (auto& chunk = chunks_[i]) {
      54        1101 :         for (size_t j = 0; j < chunk->size(); ++j) {
      55        1070 :           trace_writer_->AppendTraceEvent(chunk->GetEventAt(j));
      56             :         }
      57             :       }
      58        1054 :       if (i == chunk_index_) break;
      59             :     }
      60             :   }
      61          46 :   trace_writer_->Flush();
      62             :   // This resets the trace buffer.
      63          46 :   is_empty_ = true;
      64          46 :   return true;
      65             : }
      66             : 
      67           0 : uint64_t TraceBufferRingBuffer::MakeHandle(size_t chunk_index,
      68             :                                            uint32_t chunk_seq,
      69             :                                            size_t event_index) const {
      70        6213 :   return static_cast<uint64_t>(chunk_seq) * Capacity() +
      71        4142 :          chunk_index * TraceBufferChunk::kChunkSize + event_index;
      72             : }
      73             : 
      74           0 : void TraceBufferRingBuffer::ExtractHandle(uint64_t handle, size_t* chunk_index,
      75             :                                           uint32_t* chunk_seq,
      76             :                                           size_t* event_index) const {
      77        1709 :   *chunk_seq = static_cast<uint32_t>(handle / Capacity());
      78        1709 :   size_t indices = handle % Capacity();
      79        1709 :   *chunk_index = indices / TraceBufferChunk::kChunkSize;
      80        1709 :   *event_index = indices % TraceBufferChunk::kChunkSize;
      81           0 : }
      82             : 
      83           0 : size_t TraceBufferRingBuffer::NextChunkIndex(size_t index) const {
      84        1083 :   if (++index >= max_chunks_) index = 0;
      85           0 :   return index;
      86             : }
      87             : 
      88        2015 : TraceBufferChunk::TraceBufferChunk(uint32_t seq) : seq_(seq) {}
      89             : 
      90           0 : void TraceBufferChunk::Reset(uint32_t new_seq) {
      91          24 :   next_free_ = 0;
      92          24 :   seq_ = new_seq;
      93           0 : }
      94             : 
      95           0 : TraceObject* TraceBufferChunk::AddTraceEvent(size_t* event_index) {
      96        2071 :   *event_index = next_free_++;
      97        2071 :   return &chunk_[*event_index];
      98             : }
      99             : 
     100          41 : TraceBuffer* TraceBuffer::CreateTraceBufferRingBuffer(
     101             :     size_t max_chunks, TraceWriter* trace_writer) {
     102          41 :   return new TraceBufferRingBuffer(max_chunks, trace_writer);
     103             : }
     104             : 
     105             : }  // namespace tracing
     106             : }  // namespace platform
     107             : }  // namespace v8

Generated by: LCOV version 1.10