14 #include <boost/function.hpp>
15 #include <boost/ptr_container/ptr_circular_buffer.hpp>
16 #include <boost/weak_ptr.hpp>
17 #include <boost/shared_ptr.hpp>
24 template<
typename TraceEntryT>
29 typedef std::map<
const std::string,
34 TraceBuffer(
const std::string& buf_name,
size_t size,
bool trace_enable)
91 std::scoped_lock lock(
mutex_);
118 ReadContextMap::iterator next = it;
130 uint32_t nseqno(
seqno_.fetch_add(1));
140 void TraceRead(
const std::string& context,
const int count,
141 boost::function<
void (TraceEntryT *,
bool)> cb) {
142 std::scoped_lock lock(
mutex_);
149 size_t cnt = count ? count :
trace_buf_.size();
151 size_t *read_index_ptr;
152 typename ContainerType::iterator it;
153 ReadContextMap::iterator context_it =
158 read_index_ptr = context_it->second.get();
164 boost::shared_ptr<size_t> read_context(
new size_t(
read_index_));
165 read_index_ptr = read_context.get();
171 typename ContainerType::iterator next = it;
172 for (i = 0; (it !=
trace_buf_.end()) && (i < cnt); i++, it = next) {
178 size_t offset = *read_index_ptr + i;
186 std::scoped_lock lock(
mutex_);
187 ReadContextMap::iterator context_it =
200 typedef std::map<const std::string, boost::shared_ptr<size_t> >
247 template<
typename TraceEntryT>
263 std::scoped_lock lock(
mutex_);
267 if (it->second.lock() == NULL) {
287 template<
typename TraceEntryT>
319 boost::shared_ptr<TraceBuffer<TraceEntryT> >
TraceBufGet(
const std::string& buf_name) {
320 std::scoped_lock lock(
mutex_);
321 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
323 return it->second.lock();
325 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
331 boost::shared_ptr<TraceBuffer<TraceEntryT> >
TraceBufAdd(
const std::string& buf_name,
size_t size,
335 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
337 std::scoped_lock lock(
mutex_);
338 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
340 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf(
346 return it->second.lock();
351 std::scoped_lock lock(
mutex_);
352 typename TraceBufMap::iterator it;
354 trace_buf_list.push_back(it->first);
360 std::scoped_lock lock(
mutex_);
361 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
363 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
365 return trace_buf->TraceBufCapacityGet();
375 const std::string& buf_name,
size_t size) {
376 std::scoped_lock lock(
mutex_);
377 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
379 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
381 trace_buf->TraceBufCapacityReset(size);
384 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.