Commit aa842f61 authored by Edwin Carlinet's avatar Edwin Carlinet
Browse files

Use thread local stack for trace.

	*  mln/core/trace.hpp: thread local stack.
parent 8f84d165
......@@ -7,7 +7,11 @@
# include <iostream>
# include <stack>
# include <string>
# include <mutex>
//# include <mutex>
# include <thread>
# include <tbb/compat/thread>
# include <tbb/combinable.h>
# include <tbb/tick_count.h>
# define mln_entering(NAME) \
mln::trace::entering(NAME);
......@@ -24,26 +28,29 @@ namespace mln
explicit trace_t(const std::string& fun)
: fname (fun)
{
clock = std::clock();
clock = tbb::tick_count::now();
}
std::string fname;
std::clock_t clock;
std::string fname;
tbb::tick_count clock;
};
static std::stack<trace_t> callstack;
static tbb::combinable< std::stack<trace_t> > callstacks;
//static std::stack<trace_t> callstack;
static bool verbose = (std::getenv("TRACE") != NULL);
static std::mutex stack_mutex;
//static std::mutex stack_mutex;
static inline
void entering(const std::string& fname)
{
if (verbose) {
std::lock_guard<std::mutex> lock(stack_mutex);
//std::lock_guard<std::mutex> lock(stack_mutex);
std::stack<trace_t>& callstack = callstacks.local();
callstack.emplace(fname);
std::clog << std::string(callstack.size(), ' ') << fname << std::endl;
std::clog << std::string(callstack.size(), ' ')
<< "#" << std::this_thread::get_id() << " - "
<< fname << std::endl;
}
};
......@@ -52,11 +59,13 @@ namespace mln
void exiting()
{
if (verbose) {
std::lock_guard<std::mutex> lock(stack_mutex);
//std::lock_guard<std::mutex> lock(stack_mutex);
std::stack<trace_t>& callstack = callstacks.local();
trace_t tr = callstack.top();
std::clog << std::string(callstack.size(), ' ') << tr.fname
<< " in " << ((float)(std::clock() - tr.clock) / CLOCKS_PER_SEC) << std::endl;
std::clog << std::string(callstack.size(), ' ')
<< "#" << std::this_thread::get_id() << " - "
<< tr.fname
<< " in " << (tbb::tick_count::now() - tr.clock).seconds() << std::endl;
callstack.pop();
}
......@@ -66,7 +75,9 @@ namespace mln
void warn(const std::string& msg)
{
if (verbose)
std::clog << std::string(callstack.size(), ' ') << msg << std::endl;
std::clog << std::string(callstacks.local().size(), ' ')
<< "#" << std::this_thread::get_id() << " - "
<< msg << std::endl;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment