Phosphor
trace_buffer.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2016 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
22 #pragma once
23 
24 #include <array>
25 #include <iterator>
26 #include <memory>
27 #include <thread>
28 #include <type_traits>
29 #include <vector>
30 #include <functional>
31 
32 #include <gsl_p/iterator.h>
33 
34 #include "phosphor/platform/core.h"
35 #include "trace_event.h"
36 
37 namespace phosphor {
38 
39 #ifndef PHOSPHOR_CHUNK_PAGE_COUNT
40 #define PHOSPHOR_CHUNK_PAGE_COUNT 1
41 #endif
42 
49  class PHOSPHOR_API TraceChunk {
50  public:
51  static constexpr auto chunk_page_count = PHOSPHOR_CHUNK_PAGE_COUNT;
52  static constexpr auto page_size = 4096;
53  static constexpr auto array_offset = 64;
54  static constexpr auto chunk_size =
55  (((page_size * chunk_page_count) - array_offset) /
56  sizeof(TraceEvent));
57  using event_array = std::array<TraceEvent, chunk_size>;
58 
59  using const_iterator = event_array::const_iterator;
60 
64  TraceChunk() = default;
65 
72  void reset();
73 
79  TraceEvent& addEvent();
80 
90  const TraceEvent& operator[](const int index) const;
91 
98  bool isFull() const;
99 
105  size_t count() const;
106 
110  const_iterator begin() const;
111 
115  const_iterator end() const;
116 
117  private:
118  unsigned short next_free;
119  event_array chunk;
120  };
121 
122  // Forward decl
123  class StatsCallback;
124 
148  class TraceBuffer {
149  public:
154  virtual ~TraceBuffer() = default;
155 
162  virtual TraceChunk* getChunk() = 0;
163 
173  virtual void returnChunk(TraceChunk& chunk) = 0;
174 
182  virtual bool isFull() const = 0;
183 
205  virtual void getStats(StatsCallback& addStats) const = 0;
206 
218  virtual const TraceChunk& operator[](const int index) const = 0;
219 
225  virtual size_t chunk_count() const = 0;
226 
230  virtual size_t getGeneration() const = 0;
231 
246  class PHOSPHOR_API chunk_iterator
247  : public std::iterator<std::bidirectional_iterator_tag,
248  TraceChunk> {
249  using const_reference = const TraceChunk&;
250  using const_pointer = const TraceChunk*;
251  using _self = chunk_iterator;
252 
253  public:
254  chunk_iterator() = default;
255  chunk_iterator(const TraceBuffer& buffer_);
256  chunk_iterator(const TraceBuffer& buffer_, size_t index_);
257  const_reference operator*() const;
258  const_pointer operator->() const;
259  chunk_iterator& operator++();
260  bool operator==(const chunk_iterator& other) const;
261  bool operator!=(const chunk_iterator& other) const;
262 
263  protected:
264  const TraceBuffer& buffer;
265  size_t index;
266  };
267 
271  virtual chunk_iterator chunk_begin() const = 0;
272 
276  virtual chunk_iterator chunk_end() const = 0;
277 
296 
300  virtual event_iterator begin() const = 0;
301 
305  virtual event_iterator end() const = 0;
306 
315  class PHOSPHOR_API chunk_iterable {
316  public:
320  chunk_iterable(const TraceBuffer& buffer_) : buffer(buffer_) {}
321 
326  return buffer.chunk_begin();
327  }
328 
333  return buffer.chunk_end();
334  }
335 
336  private:
337  const TraceBuffer& buffer;
338  };
339 
350  return chunk_iterable(*this);
351  }
352  };
353 
354  using buffer_ptr = std::unique_ptr<TraceBuffer>;
355 
359  using trace_buffer_factory =
360  std::function<buffer_ptr(size_t generation, size_t buffer_size)>;
361 
362  PHOSPHOR_API
363  buffer_ptr make_fixed_buffer(size_t generation, size_t buffer_size);
364 
365  PHOSPHOR_API
366  buffer_ptr make_ring_buffer(size_t generation, size_t buffer_size);
367 }
chunk_iterable chunks() const
Definition: trace_buffer.h:349
Definition: trace_buffer.h:246
Definition: trace_buffer.h:315
Definition: trace_event.h:40
chunk_iterable(const TraceBuffer &buffer_)
Definition: trace_buffer.h:320
chunk_iterator end()
Definition: trace_buffer.h:332
Definition: trace_buffer.h:148
chunk_iterator begin()
Definition: trace_buffer.h:325
Definition: trace_buffer.h:49
Definition: category_registry.h:32
std::function< buffer_ptr(size_t generation, size_t buffer_size)> trace_buffer_factory
Definition: trace_buffer.h:360
Definition: stats_callback.h:45