12 #include <boost/function.hpp>
13 #include <boost/ptr_container/ptr_circular_buffer.hpp>
14 #include <boost/weak_ptr.hpp>
15 #include <boost/shared_ptr.hpp>
22 template<
typename TraceEntryT>
27 typedef std::map<
const std::string,
32 TraceBuffer(
const std::string& buf_name,
size_t size,
bool trace_enable)
89 tbb::mutex::scoped_lock lock(
mutex_);
116 ReadContextMap::iterator next = it;
128 uint32_t nseqno(
seqno_.fetch_and_increment());
138 void TraceRead(
const std::string& context,
const int count,
139 boost::function<
void (TraceEntryT *,
bool)> cb) {
140 tbb::mutex::scoped_lock lock(
mutex_);
147 size_t cnt = count ? count :
trace_buf_.size();
149 size_t *read_index_ptr;
150 typename ContainerType::iterator it;
151 ReadContextMap::iterator context_it =
156 read_index_ptr = context_it->second.get();
162 boost::shared_ptr<size_t> read_context(
new size_t(
read_index_));
163 read_index_ptr = read_context.get();
169 typename ContainerType::iterator next = it;
170 for (i = 0; (it !=
trace_buf_.end()) && (i < cnt); i++, it = next) {
176 size_t offset = *read_index_ptr + i;
184 tbb::mutex::scoped_lock lock(
mutex_);
185 ReadContextMap::iterator context_it =
198 typedef std::map<const std::string, boost::shared_ptr<size_t> >
245 template<
typename TraceEntryT>
261 tbb::mutex::scoped_lock lock(
mutex_);
265 if (it->second.lock() == NULL) {
285 template<
typename TraceEntryT>
317 boost::shared_ptr<TraceBuffer<TraceEntryT> >
TraceBufGet(
const std::string& buf_name) {
318 tbb::mutex::scoped_lock lock(
mutex_);
319 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
321 return it->second.lock();
323 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
329 boost::shared_ptr<TraceBuffer<TraceEntryT> >
TraceBufAdd(
const std::string& buf_name,
size_t size,
333 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
335 tbb::mutex::scoped_lock lock(
mutex_);
336 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
338 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf(
344 return it->second.lock();
349 tbb::mutex::scoped_lock lock(
mutex_);
350 typename TraceBufMap::iterator it;
352 trace_buf_list.push_back(it->first);
358 tbb::mutex::scoped_lock lock(
mutex_);
359 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
361 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
363 return trace_buf->TraceBufCapacityGet();
373 const std::string& buf_name,
size_t size) {
374 tbb::mutex::scoped_lock lock(
mutex_);
375 typename TraceBufMap::iterator it =
trace_buf_map_.find(buf_name);
377 boost::shared_ptr<TraceBuffer<TraceEntryT> > trace_buf =
379 trace_buf->TraceBufCapacityReset(size);
382 return boost::shared_ptr<TraceBuffer<TraceEntryT> >();
The class is responsible for the destruction of a trace buffer.
TraceBufferDeleter(TraceBufMap &trace_buf_map, tbb::mutex &mutex)
tbb::mutex & mutex_
A reference to the mutex object.
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()
tbb::atomic< uint32_t > seqno_
Stores the current sequence number.
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.
tbb::atomic< bool > trace_enable_
bool IsTraceOn()
Determines whether the trace buffer is enabled or not.
static const uint32_t kMaxSeqno
Reserves max(uint32_t)
ReadContextMap read_context_map_
Stores the read context.
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)
tbb::atomic< bool > trace_enable_
Determines if the tracing is enabled for the table.
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)
tbb::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.