16 #include <stk_util/diag/Trace.hpp>
17 #include <stk_util/diag/Writer.hpp>
18 #include <stk_util/environment/FormatTime.hpp>
19 #include <stk_util/diag/Platform.hpp>
21 namespace stk_classic {
31 Trace::s_traceListExists =
false;
34 Traceback::s_top = s_stack;
37 Traceback::s_storedTop = s_storedStack;
43 Traceback::s_storedStack;
46 Traceback::s_tracebackState = Traceback::RUNNING;
49 Traceback::s_tracebackPreserve = 0;
52 Traceback::s_tracebackDisplay = 0;
55 Traceback::s_coverageEnabled =
false;
58 Traceback::s_coverage;
67 for (; *prefix != 0 && *prefix == *name; ++prefix, ++name)
75 const Trace::TraceList & trace_list,
78 for (Trace::TraceList::const_iterator it = trace_list.begin(); it != trace_list.end(); ++it)
79 if (prefix_compare((*it), s))
91 std::string::const_iterator
93 std::string::const_iterator p,
94 std::string::const_iterator end,
97 while (p != end && *p != c)
102 std::string::const_iterator
104 std::string::const_iterator begin,
105 std::string::const_iterator p,
108 while (p != begin && *p != c)
113 std::string::const_iterator
114 find_prev_double_colon(
115 std::string::const_iterator begin,
116 std::string::const_iterator p)
118 std::string::const_iterator it = p - 1;
120 while ((it = find_prev_char(begin, it,
':')) != begin) {
121 if (*(it - 1) ==
':')
130 inline std::string::const_iterator find_next_open_paren(std::string::const_iterator p, std::string::const_iterator end) {
131 return find_next_char(p, end,
'(');
156 get_function_spec_parts(
157 const std::string & spec,
158 std::string & namespace_name,
159 std::string & class_name,
160 std::string & function_name,
161 std::vector<std::string> & arglist)
163 namespace_name.erase(namespace_name.begin(), namespace_name.end());
164 class_name.erase(class_name.begin(), class_name.end());
165 function_name.erase(function_name.begin(), function_name.end());
166 arglist.erase(arglist.begin(), arglist.end());
168 std::string::const_iterator it_paren = find_next_open_paren(spec.begin(), spec.end());
169 std::string::const_iterator it_func_name = find_prev_double_colon(spec.begin(), it_paren);
170 function_name = std::string(it_func_name, it_paren);
171 if (it_func_name != spec.begin()) {
173 std::string::const_iterator it_class_name = find_prev_double_colon(spec.begin(), it_func_name);
174 class_name = std::string(it_class_name, it_func_name);
175 if (it_class_name != spec.begin()) {
177 namespace_name = std::string(spec.begin(), it_class_name);
186 static const char *suffix[] = {
" B",
" KB",
" MB",
" GB"};
188 char sign = size < 0 ?
'-' :
'+';
190 size = size > 0 ? size : -size;
195 for (i = 0; i <
sizeof(suffix); i++) {
202 std::stringstream strout;
204 strout << sign << size << suffix[i];
215 std::string namespace_name;
216 std::string class_name;
217 std::string function_name;
218 std::vector<std::string> arglist;
220 get_function_spec_parts(
m_functionSpec, namespace_name, class_name, function_name, arglist);
222 return namespace_name;
229 std::string namespace_name;
230 std::string class_name;
231 std::string function_name;
232 std::vector<std::string> arglist;
234 get_function_spec_parts(
m_functionSpec, namespace_name, class_name, function_name, arglist);
236 return namespace_name +
"::" + class_name;
243 std::string namespace_name;
244 std::string class_name;
245 std::string function_name;
246 std::vector<std::string> arglist;
248 get_function_spec_parts(
m_functionSpec, namespace_name, class_name, function_name, arglist);
257 std::string namespace_name;
258 std::string class_name;
259 std::string function_name;
260 std::vector<std::string> arglist;
262 get_function_spec_parts(
m_functionSpec, namespace_name, class_name, function_name, arglist);
264 return namespace_name +
"::" + class_name +
"::" + function_name;
271 std::string namespace_name;
272 std::string class_name;
273 std::string function_name;
274 std::vector<std::string> arglist;
276 get_function_spec_parts(
m_functionSpec, namespace_name, class_name, function_name, arglist);
278 return function_name;
282 struct CoverageValueSort
284 int operator()(
const std::pair<const char *, int> &s1,
const std::pair<const char *, int> &s2) {
285 return std::strcmp(s1.first, s2.first) < 0;
291 const char *function_name,
298 m_lineMask(line_mask),
299 m_do_trace(do_trace),
300 m_flags((dout.isTracing()
301 || (dout.shouldTrace(m_lineMask)
302 && (s_traceListExists && (s_traceList.empty() || prefix_find(s_traceList, m_functionSpec))))) ? IN_TRACE_LIST : 0)
308 << (std::uncaught_exception() ?
" (throw unwinding) " :
"")
313 m_startMemAlloc = get_heap_used();
324 m_startMemAlloc = get_heap_used() - m_startMemAlloc;
328 m_diagWriter.
m(m_lineMask) <<
"[" << stk_classic::formatTime(m_startCpuTime)
329 <<
"s, " << format_memory(m_startMemAlloc) <<
"]" <<
dendl;
332 m_diagWriter.
m(m_lineMask) << (std::uncaught_exception() ?
" (throw unwinding) " :
"")
350 Traceback::snapshot()
352 TracebackStack traceback_stack;
353 traceback_stack.reserve(s_top - s_stack);
357 traceback_stack.push_back(*it);
360 traceback_stack.push_back(*it);
362 return traceback_stack;
368 const TracebackStack & traceback_stack)
370 std::ostringstream s;
371 if (traceback_stack.empty())
372 s <<
" traceback not available" << std::endl;
374 for (TracebackStack::const_iterator it = traceback_stack.begin(); it != traceback_stack.end(); ++it)
375 s <<
" from " << (*it) << std::endl;
385 std::vector<std::pair<const char *, int> > sorted_list;
386 sorted_list.reserve(s_coverage.size());
388 for (Coverage::const_iterator it = s_coverage.begin(); it != s_coverage.end(); ++it)
389 sorted_list.push_back(std::pair<const char *, int>((*it).first, (*it).second));
391 std::sort(sorted_list.begin(), sorted_list.end(), CoverageValueSort());
393 for (std::vector<std::pair<const char *, int> >::const_iterator it = sorted_list.begin(); it != sorted_list.end(); ++it)
394 os <<
"<FUNCTION specification=\"" << (*it).first <<
"\" count=\"" << (*it).second <<
"\"/>" << std::endl;
std::string getFunctionNamespace() const
Member function getFunctionName returns the function's name.
double cpu_now()
Member function cpu_now returns the accumlated cpu time for the process as a double precision value i...
int incTraceDepth()
Member function incTraceDepth increments the tracing count.
Writer &(* ExtraFuncPtr)(Writer &)
Typedef ExtraFuncPtr declares the extra function pointer signature.
static TracebackState getTracebackState()
Member function getTracebackState returns the value of the traceback state.
Tracing enabled by this function.
Trace(Writer &dout, const char *function_name, int print_mask=LOG_TRACE, bool do_trace=true)
Creates a new Trace instance, resulting in the printing of the member function name and pushing the d...
static std::string printTraceback(const TracebackStack &traceback_stack)
Member function printTraceback writes the traceback stack function specifications to the output strea...
std::string getFunctionName() const
Member function getFunctionName returns the function's name.
static PrintCoverage printCoverage()
Member function printCoverage creates a PrintCoverage type holder class which enables operator<< to p...
const char * m_functionSpec
The member function specification.
size_t get_heap_usage()
Function get_heap_usage returns the number of bytes used by the heap.
std::map< const char *, int > Coverage
Typedef Coverage declares the function usage coverage data type.
Class Traceback is a stack of char constant pointers to function specifications which have been encou...
Writer & verbose_print(Writer &dout) const
Member function dump writes the trace to the specified Writer.
bool shouldPrint()
Member function shouldPrint returns true if the line should print.
Writer & dendl(Writer &dout)
Writer function dendl calls the Writer::dendl manipulator.
std::string getFunctionClass() const
Member function getFunctionName returns the function's name.
std::string getFunctionShortClass() const
Member function getFunctionName returns the function's name.
const char * Stack[STACK_SIZE]
Stack type.
Class Writer implements a runtime selectable diagnostic output writer to aid in the development and d...
const char *const * const_stack_iterator
const iterator thru stack
Writer & m(PrintMask line_mask)
Member function m sets the line mask of this line.
int decTraceDepth()
Member function decTraceDepth decrements the tracing count.
TracebackState
Enumeration TracebackState lists the traceback execution states.
std::string getFunctionShortName() const
Member function getFunctionName returns the function's name.
~Trace()
Destroys a Trace instance, resulting in the printing of the member function name and popping the diag...
const char ** stack_iterator
iterator thru stack