File: trace_ring_buffer.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (104 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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_RING_BUFFER_H_
#define COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_

#include <memory>

#include "base/atomicops.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "components/tracing/tracing_export.h"

namespace tracing {
namespace v2 {

static const uint32_t kNoChunkOwner = 0;
static const size_t kChunkSize = 32 * 1024;

class TRACING_EXPORT TraceRingBuffer {
 public:
  class Chunk {
   public:
    using Header = base::subtle::Atomic32;

    Chunk();
    ~Chunk();

    void Initialize(uint8_t* begin);
    void Clear();

    uint8_t* begin() const { return begin_; }
    Header* header() const { return reinterpret_cast<Header*>(begin_); }
    uint8_t* payload() const { return begin_ + sizeof(Header); }
    uint8_t* end() const { return begin_ + kChunkSize; }

    void set_used_size(uint32_t size) {
      base::subtle::NoBarrier_Store(header(), size);
    }
    uint32_t used_size() const {
      return base::subtle::NoBarrier_Load(header());
    }

    void set_next_in_owner_list(Chunk* next) { next_in_owner_list_ = next; }
    Chunk* next_in_owner_list() const { return next_in_owner_list_; }

    // Owner is a flag matching the id of the TraceBufferWriter, 0 if not owned.
    // Accesses to |owner_| must happen under the buffer |lock_|.
    bool is_owned() const { return owner_ != kNoChunkOwner; }
    uint32_t owner() const { return owner_; }
    void clear_owner() { owner_ = kNoChunkOwner; }
    void set_owner(uint32_t owner) {
      DCHECK_NE(kNoChunkOwner, owner);
      owner_ = owner;
    }

   private:
    uint8_t* begin_;
    uint32_t owner_;

    // When a chunk is owned, this is the next pointer to keep track of all
    // owned chunks in a singly linked list.
    Chunk* next_in_owner_list_;

    DISALLOW_COPY_AND_ASSIGN(Chunk);
  };

  TraceRingBuffer(uint8_t* begin, size_t size);
  ~TraceRingBuffer();

  Chunk* TakeChunk(uint32_t writer_id);
  void ReturnChunk(Chunk* chunk);

  size_t num_chunks() const { return num_chunks_; }

  // Returns the number of chunks taken and not returned, without counting any
  // bankrupcy chunk obtained when the ring buffer was full.
  size_t GetNumChunksTaken() const;

  const Chunk* chunks_for_testing() const { return chunks_.get(); }
  bool IsBankrupcyChunkForTesting(const Chunk*) const;

 private:
  mutable base::Lock lock_;
  std::unique_ptr<Chunk[]> chunks_;
  const size_t num_chunks_;
  size_t num_chunks_taken_;
  size_t current_chunk_idx_;

  // An emergency chunk used in the rare case in which all chunks are in flight.
  // This chunk is not part of the ring buffer and its contents are always
  // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning
  // nullptr) in the case of a thread storm.
  Chunk bankrupcy_chunk_;
  std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_;

  DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer);
};

}  // namespace v2
}  // namespace tracing

#endif  // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_