19 using namespace std::chrono;
20 return steady_clock::now().time_since_epoch().count();
173 #ifndef PR_TURN_OFF_TIMERS 174 inline double TimerGetTimeStampWC()
179 gettimeofday(&tv, &tz);
180 return((
double)tv.tv_sec + 0.000001 * (
double)tv.tv_usec);
187 virtual ~TraceTimer()
189 for(
unsigned int i=0; i<m_children.size(); ++i)
191 delete m_children[i];
197 m_filename = string(
"/dev/null");
199 inline void start(
char* mutex);
200 inline unsigned long long int stop(
char* mutex);
201 inline void report(
bool a_closeAfter=
false);
205 inline void leafStart();
206 inline void leafStop();
208 unsigned long long int time()
const 210 return m_accumulated_WCtime;
217 long long int count()
const 223 bool isPruned()
const {
return m_pruned;}
227 void setTimerFileName(
string a_filename)
230 std::vector<TraceTimer*>* troots = getRootTimerPtr();
231 (*troots)[0]->m_filename = a_filename;
234 inline TraceTimer* getTimer(
const char* name);
235 inline const std::vector<TraceTimer*>& children()
const ;
237 inline void PruneTimersParentChildPercent(
double percent);
238 inline void sampleMemUsage() ;
240 inline void addFlops(
long long int flops) {m_flops+=flops;}
247 long long int m_flops;
248 long long int m_count;
251 static std::vector<TraceTimer*>* getRootTimerPtr()
253 static std::vector<TraceTimer*>* retval =
new std::vector<TraceTimer*>();
254 static bool initialized =
false;
257 std::vector<TraceTimer*>* current = getCurrentTimerPtr();
259 const char* rootName =
"root";
260 TraceTimer* rootTimer =
new TraceTimer(rootName, NULL, 0);
261 rootTimer->m_thread_id = 0;
265 (*retval)[0] = rootTimer;
267 (*current).resize(1);
268 (*current)[0]=rootTimer;
270 (*retval)[0]->tid = 0;
271 rootTimer->start(&mutex);
272 rootTimer->zeroTime = TimerGetTimeStampWC();
280 static std::vector<TraceTimer*>* getCurrentTimerPtr()
282 static std::vector<TraceTimer*>* retval =
new std::vector<TraceTimer*>(1);
288 static void staticReport()
290 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
291 if(vecptr->size() > 0)
293 (*vecptr)[0]->report();
298 static void staticReset()
300 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
301 if(vecptr->size() > 0)
303 (*vecptr)[0]->reset();
308 static void staticPruneTimersParentChildPercent(
double percent)
310 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
311 if(vecptr->size() > 0)
313 (*vecptr)[0]->PruneTimersParentChildPercent(percent);
318 static void staticSetTimerFileName(
string a_filename)
320 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
321 if(vecptr->size() > 0)
323 (*vecptr)[0]->setTimerFileName(a_filename);
331 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
332 if(vecptr->size() > 0)
334 retval = (*vecptr)[0]->tid;
340 static TraceTimer* staticGetTimer(
const char* name)
342 TraceTimer* retval = NULL;
343 std::vector<TraceTimer*>* vecptr = getRootTimerPtr();
344 if(vecptr->size() > 0)
346 retval = (*vecptr)[0]->getTimer(name);
354 TraceTimer(
const char* a_name, TraceTimer* parent,
int thread_id)
363 m_accumulated_WCtime = 0;
364 m_last_WCtime_stamp = 0;
365 m_thread_id = thread_id;
372 TraceTimer* m_parent;
373 std::vector<TraceTimer*> m_children;
375 unsigned long long int m_accumulated_WCtime;
376 unsigned long long int m_last_WCtime_stamp;
379 unsigned long long int zeroTicks = 0;
383 void reportTree(FILE* out,
const TraceTimer& node,
int depth);
384 const TraceTimer* activeChild()
const;
387 inline void macroTest();
388 inline void macroTest2();
390 inline void currentize()
const ;
392 inline void reportFullTree(FILE* out,
const TraceTimer& timer,
393 unsigned long long int totalTime,
int depth,
double secondspertick);
394 inline void reportOneTree(FILE* out,
const TraceTimer& timer,
double secondspertick);
397 inline void reset(TraceTimer& timer);
398 inline void PruneTimersParentChildPercent(
double threshold, TraceTimer* parent);
399 inline void sumFlops(TraceTimer& timer);
406 AutoStartLeaf(TraceTimer* a_timer):m_timer(a_timer) {
if(a_timer)a_timer->leafStart();}
407 ~AutoStartLeaf(){
if(m_timer)m_timer->leafStop();}
416 AutoStart(TraceTimer* a_timer,
char* mutex,
char* childMutex)
417 :m_mutex(mutex), m_childMutex(childMutex),m_timer(a_timer)
418 {
if(a_timer)a_timer->start(mutex);}
419 AutoStart(TraceTimer* a_timer,
char* mutex)
420 :m_mutex(mutex), m_childMutex((
char*)(getOKPtr()) ),m_timer(a_timer)
422 if(a_timer)a_timer->start(mutex);
427 static char* getOKPtr()
429 static bool init =
false;
430 static char* retval =
new char[1024];
433 sprintf(retval,
"0");
441 static const char ok = 0;
446 AutoStart::~AutoStart()
449 if(*m_childMutex == 1)
451 std::cerr<<
" stop called in timer unexpectedly";
455 if(m_timer)m_timer->stop(m_mutex);
459 inline void TraceTimer::leafStart()
466 inline void TraceTimer::leafStop()
469 m_accumulated_WCtime +=
PR_ticks() - m_last_WCtime_stamp;
470 m_last_WCtime_stamp=0;
474 #ifdef PR_TURN_OFF_TIMERS 476 #define PR_TIMER(name, tpointer) 477 #define PR_TIME(name) 478 #define PR_FLOPS(flops) 479 #define PR_TIMELEAF(name) 480 #define PR_TIMERS(name) 481 #define PR_START(tpointer) 482 #define PR_STOP(tpointer) 483 #define PR_TIMER_REPORT() 484 #define PR_TIMER_RESET() 485 #define PR_TIMER_PRUNE(threshold) 486 #define PR_TIMER_SETFILE(filename) 490 #define PR_TIMER(name, tpointer) \ 491 const char* TimerTag_##tpointer = name ; \ 492 ::Proto::TraceTimer* tpointer = NULL ; \ 493 if(::Proto::TraceTimer::getTID()==0) \ 495 tpointer = ::Proto::TraceTimer::staticGetTimer(TimerTag_##tpointer) ; \ 498 #define PR_TIME(name) \ 499 const char* TimerTagA = name ; \ 500 char PR_TimermutexA = 0; \ 501 ::Proto::TraceTimer* PR_tpointer = NULL; \ 502 if(::Proto::TraceTimer::getTID()==0) \ 504 PR_tpointer = ::Proto::TraceTimer::staticGetTimer(TimerTagA); \ 506 ::Proto::AutoStart autostart(PR_tpointer, &PR_TimermutexA) 508 #define PR_FLOPS(flops) \ 509 if(PR_tpointer)PR_tpointer->addFlops(flops); 511 #define PR_TIMELEAF(name) \ 512 const char* TimerTagA = name ; \ 513 ::Proto::TraceTimer* PR_tpointer = NULL; \ 514 if(::Proto::TraceTimer::getTID()==0) \ 516 PR_tpointer = ::Proto::TraceTimer::staticGetTimer(TimerTagA); \ 518 ::Proto::AutoStartLeaf autostart(PR_tpointer) 520 #define PR_TIMERS(name) \ 521 const char* TimerTagA = name ; \ 522 char PR_TimermutexA = 0; \ 523 char PR_Timermutex = 0; \ 524 ::Proto::TraceTimer* PR_tpointer = NULL; \ 525 if(::Proto::TraceTimer::getTID()==0) \ 527 PR_tpointer = ::Proto::TraceTimer::staticGetTimer(TimerTagA); \ 529 ::Proto::AutoStart autostart(PR_tpointer, &PR_TimermutexA, &PR_Timermutex) 532 #define PR_START(tpointer) \ 533 if(::Proto::TraceTimer::getTID()==0) \ 535 tpointer->start(&PR_Timermutex); \ 538 #define PR_STOP(tpointer) \ 539 if(::Proto::TraceTimer::getTID()==0) \ 541 tpointer->stop(&PR_Timermutex); \ 545 #define PR_TIMER_REPORT() ::Proto::TraceTimer::staticReport() 547 #define PR_TIMER_RESET() ::Proto::TraceTimer::staticReset() 549 #define PR_TIMER_PRUNE(threshold) ::Proto::TraceTimer::staticPruneTimersParentChildPercent(threshold) 551 #define PR_TIMER_SETFILE(filename) ::Proto::TraceTimer::staticSetTimerFileName(filename); unsigned long long int PR_ticks()
Definition: Proto_Timer.H:16
Definition: Proto_Array.H:17