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>
25 template<
typename TraceEntryT>
30 typedef std::map<
const std::string,
35 TraceBuffer(
const std::string& buf_name,
size_t size,
bool trace_enable)
92 std::scoped_lock lock(
mutex_);
119 ReadContextMap::iterator next = it;
131 uint32_t nseqno(
seqno_.fetch_add(1));
141 void TraceRead(
const std::string& context,
const int count,
142 boost::function<
void (TraceEntryT *,
bool)> cb) {
143 std::scoped_lock lock(
mutex_);
150 size_t cnt = count ? count :
trace_buf_.size();
152 size_t *read_index_ptr;
153 typename ContainerType::iterator it;
154 ReadContextMap::iterator context_it =
159 read_index_ptr = context_it->second.get();
165 boost::shared_ptr<size_t> read_context(
new size_t(
read_index_));
166 read_index_ptr = read_context.get();
172 typename ContainerType::iterator next = it;
173 for (i = 0; (it !=
trace_buf_.end()) && (i < cnt); i++, it = next) {
179 size_t offset = *read_index_ptr + i;
187 std::scoped_lock lock(
mutex_);
188 ReadContextMap::iterator context_it =
201 typedef std::map<const std::string, boost::shared_ptr<size_t> >
248 template<
typename TraceEntryT>
264 std::scoped_lock lock(
mutex_);
268 if (it->second.lock() == NULL) {
288 template<
typename TraceEntryT>
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);
324 return it->second.lock();
326 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
332 boost::shared_ptr<TraceBuffer<TraceEntryT> >
TraceBufAdd(
const std::string& buf_name,
size_t size,
336 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
338 std::scoped_lock lock(
mutex_);
339 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
341 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf(
347 return it->second.lock();
352 std::scoped_lock lock(
mutex_);
353 typename TraceBufMap::iterator it;
355 trace_buf_list.push_back(it->first);
361 std::scoped_lock lock(
mutex_);
362 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
364 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
366 return trace_buf->TraceBufCapacityGet();
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);
380 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
382 trace_buf->TraceBufCapacityReset(size);
385 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
The class is responsible for the destruction of a trace buffer.
std::mutex & mutex_
A reference to the mutex object.
TraceBufferDeleter(TraceBufMap &trace_buf_map, std::mutex &mutex)
TraceBufMap & trace_buf_map_
A reference to the trace buffers table.
void operator()(TraceBuffer< TraceEntryT > *trace_buffer) const
Performs the deletion of the trace buffer from the given map.
typename TraceBuffer< TraceEntryT >::TraceBufMap TraceBufMap
A link to the trace buffers table type.
size_t TraceBufCapacityGet()
std::map< const std::string, boost::weak_ptr< TraceBuffer< TraceEntryT > > > TraceBufMap
The type defines how a map (a table) of trace buffers is stored.
TraceBuffer(const std::string &buf_name, size_t size, bool trace_enable)
uint32_t GetNextSeqNum()
Returns the next sequence number.
void TraceOff()
Disables the trace buffer.
boost::ptr_circular_buffer< TraceEntryT > ContainerType
Specifies the data type for storing records of the trace buffer.
void TraceWrite(TraceEntryT *trace_entry)
Writes the provided data into the circular buffer.
bool wrap_
Indicates if the trace buffer is wrapped.
static const uint32_t kMinSeqno
Reserves 0.
ContainerType trace_buf_
Stores the records of the trace buffer.
void TraceReadDone(const std::string &context)
void TraceBufCapacityReset(size_t size)
Resets the size of the circular buffer.
std::string trace_buf_name_
Stores the name of the trace buffer.
void TraceOn()
Enables the trace buffer.
void TraceRead(const std::string &context, const int count, boost::function< void(TraceEntryT *, bool)> cb)
size_t trace_buf_size_
Stores the size of the trace buffer.
std::string Name()
Returns the name of the trace buffer.
~TraceBuffer()
Destroys a trace buffer.
DISALLOW_COPY_AND_ASSIGN(TraceBuffer)
std::map< const std::string, boost::shared_ptr< size_t > > ReadContextMap
Specifies the read context for the trace buffer.
bool IsTraceOn()
Determines whether the trace buffer is enabled or not.
static const uint32_t kMaxSeqno
Reserves max(uint32_t)
std::atomic< bool > trace_enable_
ReadContextMap read_context_map_
Stores the read context.
std::atomic< uint32_t > seqno_
Stores the current sequence number.
TraceBufMap trace_buf_map_
Stores the table of trace buffers.
~Trace()
Destroys the table.
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufAdd(const std::string &buf_name, size_t size, bool trace_enable)
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufGet(const std::string &buf_name)
typename TraceBuffer< TraceEntryT >::TraceBufMap TraceBufMap
A link to the trace buffers table type.
void TraceBufListGet(std::vector< std::string > &trace_buf_list)
Requests the list of trace buffers names from the table.
boost::shared_ptr< TraceBuffer< TraceEntryT > > TraceBufCapacityReset(const std::string &buf_name, size_t size)
std::mutex mutex_
A mutex to protect the table from data races.
void TraceOff()
Disables tracing for the table.
bool IsTraceOn()
Determines whether tracing is enabled for the table.
Trace()
Forbids the default ctor.
size_t TraceBufCapacityGet(const std::string &buf_name)
Returns the capacity of the trace buffer with the given name.
static Trace * GetInstance()
Returns a pointer to the trace buffers table instance.
DISALLOW_COPY_AND_ASSIGN(Trace)
void TraceOn()
Enables tracing for the table.
static Trace * trace_
A pointer to the table (singleton) used in this program.
std::atomic< bool > trace_enable_
Determines if the tracing is enabled for the table.