OpenSDN source code
trace.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef __TRACE_H__
7 #define __TRACE_H__
8 
9 #include <atomic>
10 #include <mutex>
11 #include <map>
12 #include <vector>
13 #include <stdexcept>
14 
15 #include <boost/function.hpp>
16 #include <boost/ptr_container/ptr_circular_buffer.hpp>
17 #include <boost/weak_ptr.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include "base/util.h"
20 
25 template<typename TraceEntryT>
26 class TraceBuffer {
27 public:
28 
30  typedef std::map<const std::string,
31  boost::weak_ptr<TraceBuffer<TraceEntryT> > > TraceBufMap;
32 
35  TraceBuffer(const std::string& buf_name, size_t size, bool trace_enable)
36  : trace_buf_name_(buf_name),
37  trace_buf_size_(size),
39  write_index_(0),
40  read_index_(0),
41  wrap_(false) {
42  seqno_ = 0;
43  trace_enable_ = trace_enable;
44  }
45 
48  read_context_map_.clear();
49  trace_buf_.clear();
50  }
51 
53  std::string Name() {
54  return trace_buf_name_;
55  }
56 
58  void TraceOn() {
59  trace_enable_ = true;
60  }
61 
63  void TraceOff() {
64  trace_enable_ = false;
65  }
66 
68  bool IsTraceOn() {
69  return trace_enable_;
70  }
71 
74  size_t TraceBufSizeGet() {
75  return trace_buf_size_;
76  }
77 
81  return trace_buf_.capacity();
82  }
83 
85  void TraceBufCapacityReset(size_t size) {
86  trace_buf_.rset_capacity(size);
87  trace_buf_size_ = size;
88  }
89 
91  void TraceWrite(TraceEntryT *trace_entry) {
92  std::scoped_lock lock(mutex_);
93 
94  // Add the trace
95  trace_buf_.push_back(trace_entry);
96 
97  // Once the trace buffer is wrapped, increment the read index
98  if (wrap_) {
99  if (++read_index_ == trace_buf_size_) {
100  read_index_ = 0;
101  }
102  }
103 
104  // Increment the write_index_ and reset upon reaching trace_buf_size_
105  if (++write_index_ == trace_buf_size_) {
106  write_index_ = 0;
107  wrap_ = true;
108  }
109 
110  // Trace messages could be read in batches instead of reading
111  // the entire trace buffer in one shot. Therefore, trace messages
112  // could be added between subsequent read requests. If the
113  // read_index_ [points to the oldest message in the trace buffer]
114  // becomes same as the read index [points to the position in the
115  // trace buffer from where the next trace message should be read]
116  // stored in the read context, then there is no need to remember the
117  // read context.
118  ReadContextMap::iterator it = read_context_map_.begin();
119  ReadContextMap::iterator next = it;
120  for (size_t i = 0, cnt = read_context_map_.size(); i < cnt;
121  i++, it = next) {
122  ++next;
123  if (*it->second.get() == read_index_) {
124  read_context_map_.erase(it);
125  }
126  }
127  }
128 
130  uint32_t GetNextSeqNum() {
131  uint32_t nseqno(seqno_.fetch_add(1));
132  // Reset seqno_ if it reaches max value
133  if (nseqno+1 >= kMaxSeqno) {
134  seqno_ = kMinSeqno;
135  }
136  return nseqno;
137  }
138 
141  void TraceRead(const std::string& context, const int count,
142  boost::function<void (TraceEntryT *, bool)> cb) {
143  std::scoped_lock lock(mutex_);
144  if (trace_buf_.empty()) {
145  // No message in the trace buffer
146  return;
147  }
148 
149  // if count = 0, then set the cnt equal to the size of trace_buf_
150  size_t cnt = count ? count : trace_buf_.size();
151 
152  size_t *read_index_ptr;
153  typename ContainerType::iterator it;
154  ReadContextMap::iterator context_it =
155  read_context_map_.find(context);
156  if (context_it != read_context_map_.end()) {
157  // If the read context is present, manipulate the position
158  // from where we wanna start
159  read_index_ptr = context_it->second.get();
160  size_t offset = *read_index_ptr - read_index_;
161  offset = offset > 0 ? offset : trace_buf_size_ + offset;
162  it = trace_buf_.begin() + offset;
163  } else {
164  // Create read context
165  boost::shared_ptr<size_t> read_context(new size_t(read_index_));
166  read_index_ptr = read_context.get();
167  read_context_map_.insert(std::make_pair(context, read_context));
168  it = trace_buf_.begin();
169  }
170 
171  size_t i;
172  typename ContainerType::iterator next = it;
173  for (i = 0; (it != trace_buf_.end()) && (i < cnt); i++, it = next) {
174  ++next;
175  cb(&(*it), next != trace_buf_.end());
176  }
177 
178  // Update the read index in the read context
179  size_t offset = *read_index_ptr + i;
180  *read_index_ptr = offset >= trace_buf_size_ ?
181  offset - trace_buf_size_ : offset;
182  }
183 
186  void TraceReadDone(const std::string& context) {
187  std::scoped_lock lock(mutex_);
188  ReadContextMap::iterator context_it =
189  read_context_map_.find(context);
190  if (context_it != read_context_map_.end()) {
191  read_context_map_.erase(context_it);
192  }
193  }
194 
195 private:
196 
198  typedef boost::ptr_circular_buffer<TraceEntryT> ContainerType;
199 
201  typedef std::map<const std::string, boost::shared_ptr<size_t> >
203 
205  std::string trace_buf_name_;
206 
209 
212 
215  std::atomic<bool> trace_enable_;
216 
219  size_t write_index_;
220 
223  size_t read_index_;
224 
226  bool wrap_;
227 
230 
232  std::atomic<uint32_t> seqno_;
233 
236  std::mutex mutex_;
237 
239  static const uint32_t kMaxSeqno = ((2 ^ 32) - 1) - 1;
240 
242  static const uint32_t kMinSeqno = 1;
243 
245 };
246 
248 template<typename TraceEntryT>
250 public:
251 
254 
257  explicit TraceBufferDeleter(TraceBufMap &trace_buf_map, std::mutex &mutex) :
258  trace_buf_map_(trace_buf_map),
259  mutex_(mutex) {
260  }
261 
263  void operator()(TraceBuffer<TraceEntryT> *trace_buffer) const {
264  std::scoped_lock lock(mutex_);
265  for (typename TraceBufMap::iterator it = trace_buf_map_.begin();
266  it != trace_buf_map_.end();
267  it++) {
268  if (it->second.lock() == NULL) {
269  trace_buf_map_.erase(it->first);
270  delete trace_buffer;
271  break;
272  }
273  }
274  }
275 
276 private:
277 
280 
282  std::mutex &mutex_;
283 };
284 
288 template<typename TraceEntryT>
289 class Trace {
290 public:
291 
294 
296  static Trace* GetInstance() {
297  if (!trace_) {
298  trace_ = new Trace;
299  }
300  return trace_;
301  }
302 
304  void TraceOn() {
305  trace_enable_ = true;
306  }
307 
309  void TraceOff() {
310  trace_enable_ = false;
311  }
312 
314  bool IsTraceOn() {
315  return trace_enable_;
316  }
317 
320  boost::shared_ptr<TraceBuffer<TraceEntryT> > TraceBufGet(const std::string& buf_name) {
321  std::scoped_lock lock(mutex_);
322  typename TraceBufMap::iterator it = trace_buf_map_.find(buf_name);
323  if (it != trace_buf_map_.end()) {
324  return it->second.lock();
325  }
326  return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
327  }
328 
332  boost::shared_ptr<TraceBuffer<TraceEntryT> > TraceBufAdd(const std::string& buf_name, size_t size,
333  bool trace_enable) {
334  // should we have a default size for the buffer?
335  if (!size) {
336  return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
337  }
338  std::scoped_lock lock(mutex_);
339  typename TraceBufMap::iterator it = trace_buf_map_.find(buf_name);
340  if (it == trace_buf_map_.end()) {
341  boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf(
342  new TraceBuffer<TraceEntryT>(buf_name, size, trace_enable),
344  trace_buf_map_.insert(std::make_pair(buf_name, trace_buf));
345  return trace_buf;
346  }
347  return it->second.lock();
348  }
349 
351  void TraceBufListGet(std::vector<std::string>& trace_buf_list) {
352  std::scoped_lock lock(mutex_);
353  typename TraceBufMap::iterator it;
354  for (it = trace_buf_map_.begin(); it != trace_buf_map_.end(); ++it) {
355  trace_buf_list.push_back(it->first);
356  }
357  }
358 
360  size_t TraceBufCapacityGet(const std::string& buf_name) {
361  std::scoped_lock lock(mutex_);
362  typename TraceBufMap::iterator it = trace_buf_map_.find(buf_name);
363  if (it != trace_buf_map_.end()) {
364  boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
365  it->second.lock();
366  return trace_buf->TraceBufCapacityGet();
367  } else {
368  return 0;
369  }
370  }
371 
375  boost::shared_ptr<TraceBuffer<TraceEntryT> > TraceBufCapacityReset(
376  const std::string& buf_name, size_t size) {
377  std::scoped_lock lock(mutex_);
378  typename TraceBufMap::iterator it = trace_buf_map_.find(buf_name);
379  if (it != trace_buf_map_.end()) {
380  boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
381  it->second.lock();
382  trace_buf->TraceBufCapacityReset(size);
383  return trace_buf;
384  }
385  return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
386  }
387 
388 private:
389 
391  Trace() {
392  trace_enable_ = true;
393  }
394 
396  ~Trace() {
397 
398  delete trace_;
399  }
400 
402  static Trace *trace_;
403 
405  std::atomic<bool> trace_enable_;
406 
409 
411  std::mutex mutex_;
412 
414 };
415 
416 #endif // __TRACE_H__
The class is responsible for the destruction of a trace buffer.
Definition: trace.h:249
std::mutex & mutex_
A reference to the mutex object.
Definition: trace.h:282
TraceBufferDeleter(TraceBufMap &trace_buf_map, std::mutex &mutex)
Definition: trace.h:257
TraceBufMap & trace_buf_map_
A reference to the trace buffers table.
Definition: trace.h:279
void operator()(TraceBuffer< TraceEntryT > *trace_buffer) const
Performs the deletion of the trace buffer from the given map.
Definition: trace.h:263
typename TraceBuffer< TraceEntryT >::TraceBufMap TraceBufMap
A link to the trace buffers table type.
Definition: trace.h:253
size_t TraceBufCapacityGet()
Definition: trace.h:80
size_t TraceBufSizeGet()
Definition: trace.h:74
std::map< const std::string, boost::weak_ptr< TraceBuffer< TraceEntryT > > > TraceBufMap
The type defines how a map (a table) of trace buffers is stored.
Definition: trace.h:31
TraceBuffer(const std::string &buf_name, size_t size, bool trace_enable)
Definition: trace.h:35
size_t write_index_
Definition: trace.h:219
uint32_t GetNextSeqNum()
Returns the next sequence number.
Definition: trace.h:130
void TraceOff()
Disables the trace buffer.
Definition: trace.h:63
boost::ptr_circular_buffer< TraceEntryT > ContainerType
Specifies the data type for storing records of the trace buffer.
Definition: trace.h:198
void TraceWrite(TraceEntryT *trace_entry)
Writes the provided data into the circular buffer.
Definition: trace.h:91
std::mutex mutex_
Definition: trace.h:236
size_t read_index_
Definition: trace.h:223
bool wrap_
Indicates if the trace buffer is wrapped.
Definition: trace.h:226
static const uint32_t kMinSeqno
Reserves 0.
Definition: trace.h:242
ContainerType trace_buf_
Stores the records of the trace buffer.
Definition: trace.h:211
void TraceReadDone(const std::string &context)
Definition: trace.h:186
void TraceBufCapacityReset(size_t size)
Resets the size of the circular buffer.
Definition: trace.h:85
std::string trace_buf_name_
Stores the name of the trace buffer.
Definition: trace.h:205
void TraceOn()
Enables the trace buffer.
Definition: trace.h:58
void TraceRead(const std::string &context, const int count, boost::function< void(TraceEntryT *, bool)> cb)
Definition: trace.h:141
size_t trace_buf_size_
Stores the size of the trace buffer.
Definition: trace.h:208
std::string Name()
Returns the name of the trace buffer.
Definition: trace.h:53
~TraceBuffer()
Destroys a trace buffer.
Definition: trace.h:47
DISALLOW_COPY_AND_ASSIGN(TraceBuffer)
std::map< const std::string, boost::shared_ptr< size_t > > ReadContextMap
Specifies the read context for the trace buffer.
Definition: trace.h:202
bool IsTraceOn()
Determines whether the trace buffer is enabled or not.
Definition: trace.h:68
static const uint32_t kMaxSeqno
Reserves max(uint32_t)
Definition: trace.h:239
std::atomic< bool > trace_enable_
Definition: trace.h:215
ReadContextMap read_context_map_
Stores the read context.
Definition: trace.h:229
std::atomic< uint32_t > seqno_
Stores the current sequence number.
Definition: trace.h:232
Definition: trace.h:289
TraceBufMap trace_buf_map_
Stores the table of trace buffers.
Definition: trace.h:408
~Trace()
Destroys the table.
Definition: trace.h:396
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufAdd(const std::string &buf_name, size_t size, bool trace_enable)
Definition: trace.h:332
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufGet(const std::string &buf_name)
Definition: trace.h:320
typename TraceBuffer< TraceEntryT >::TraceBufMap TraceBufMap
A link to the trace buffers table type.
Definition: trace.h:293
void TraceBufListGet(std::vector< std::string > &trace_buf_list)
Requests the list of trace buffers names from the table.
Definition: trace.h:351
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufCapacityReset(const std::string &buf_name, size_t size)
Definition: trace.h:375
std::mutex mutex_
A mutex to protect the table from data races.
Definition: trace.h:411
void TraceOff()
Disables tracing for the table.
Definition: trace.h:309
bool IsTraceOn()
Determines whether tracing is enabled for the table.
Definition: trace.h:314
Trace()
Forbids the default ctor.
Definition: trace.h:391
size_t TraceBufCapacityGet(const std::string &buf_name)
Returns the capacity of the trace buffer with the given name.
Definition: trace.h:360
static Trace * GetInstance()
Returns a pointer to the trace buffers table instance.
Definition: trace.h:296
DISALLOW_COPY_AND_ASSIGN(Trace)
void TraceOn()
Enables tracing for the table.
Definition: trace.h:304
static Trace * trace_
A pointer to the table (singleton) used in this program.
Definition: trace.h:402
std::atomic< bool > trace_enable_
Determines if the tracing is enabled for the table.
Definition: trace.h:405