Timer.hpp

00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef stk_util_diag_Timer_hpp
00010 #define stk_util_diag_Timer_hpp
00011 
00012 #include <iosfwd>
00013 #include <vector>
00014 #include <list>
00015 #include <string>
00016 
00017 #include <stk_util/diag/TimerMetricTraits.hpp>
00018 #include <stk_util/parallel/Parallel.hpp>
00019 #include <stk_util/util/FormatTime.hpp>
00020 #include <stk_util/diag/Writer_fwd.hpp>
00021 
00026 
00027 namespace stk {
00028 namespace diag {
00029 
00030 class Timer;
00031 class TimerSet;
00032 class TimerImpl;
00033 
00034 typedef unsigned TimerMask;        
00035 
00042 MetricsMask getEnabledTimerMetricsMask();
00043 
00052 void setEnabledTimerMetricsMask(MetricsMask timer_mask);
00053 
00061 void updateRootTimer(Timer root_timer);
00062 
00076 Timer createRootTimer(const std::string &name, const TimerSet &timer_set);
00077 
00084 void deleteRootTimer(Timer timer);
00085 
00100 std::vector<Timer> &findTimers(Timer root_timer, const std::string &path_tail, std::vector<Timer> &found_timers);
00101 
00107 class TimerSet
00108 {
00109 public:
00110   explicit TimerSet(TimerMask enabled_timer_mask)
00111     : m_enabledTimerMask(enabled_timer_mask)
00112   {}
00113 
00114 private:
00115   TimerSet(const TimerSet &timer_set)
00116     : m_enabledTimerMask(timer_set.m_enabledTimerMask)
00117   {}
00118 
00119   TimerSet &operator=(TimerSet &timer_set) {
00120     m_enabledTimerMask = timer_set.m_enabledTimerMask;
00121 
00122     return *this;
00123   }
00124 
00125 public:
00126   ~TimerSet()
00127   {}
00128 
00135   TimerMask getEnabledTimerMask() const {
00136     return m_enabledTimerMask;
00137   }
00138 
00147   void setEnabledTimerMask(TimerMask timer_mask) {
00148     m_enabledTimerMask = timer_mask;
00149   }
00150 
00159   bool shouldRecord(TimerMask timer_mask) const {
00160     return (timer_mask == 0 || (m_enabledTimerMask & timer_mask));
00161   }
00162 
00163 private:
00164   TimerMask   m_enabledTimerMask; 
00165 };
00166 
00167 
00168 typedef std::list<Timer> TimerList;   
00169 
00175 class Timer
00176 {
00177   friend class TimerImpl;
00178   friend class TimeBlock;
00179   friend class TimeBlockSynchronized;
00180   friend void updateRootTimer(Timer);
00181   friend Timer createRootTimer(const std::string &, const TimerSet &);
00182   friend void deleteRootTimer(Timer);
00183   friend std::vector<Timer> &findTimers(Timer, const std::string &, std::vector<Timer> &);
00184 
00185 public:
00194   template <typename T>
00195   struct Metric
00196   {
00197     Metric()
00198       : m_lapStart(0),
00199   m_lapStop(0),
00200   m_accumulatedLap(0),
00201   m_checkpoint(0)
00202     {}
00203 
00208     void reset() {
00209       m_lapStart = m_lapStop = m_accumulatedLap = m_checkpoint = 0;
00210     }
00211 
00217     typename MetricTraits<T>::Type addLap() {
00218       return m_accumulatedLap += m_lapStop - m_lapStart;
00219     }
00220 
00226     void checkpoint() const {
00227       m_checkpoint = m_accumulatedLap;
00228     }
00229 
00236     typename MetricTraits<T>::Type getLap() const {
00237       return m_lapStop - m_lapStart;
00238     }
00239 
00245     typename MetricTraits<T>::Type getStart() const {
00246       return m_lapStart;
00247     }
00248 
00254     typename MetricTraits<T>::Type getStop() const {
00255       return m_lapStop;
00256     }
00257 
00269     typename MetricTraits<T>::Type getAccumulatedLap(bool arg_checkpoint = false) const {
00270       if (arg_checkpoint)
00271   return m_accumulatedLap - m_checkpoint;
00272       else
00273   return m_accumulatedLap;
00274     }
00275 
00286     Writer &dump(Writer &dout) const;
00287 
00288     typename MetricTraits<T>::Type    m_lapStart;   
00289     typename MetricTraits<T>::Type    m_lapStop;    
00290     typename MetricTraits<T>::Type    m_accumulatedLap; 
00291     mutable typename MetricTraits<T>::Type      m_checkpoint;   
00292   };
00293 
00303   Timer(const std::string &name, const Timer parent);
00304 
00317   Timer(const std::string &name, const Timer parent, const TimerSet &timer_set);
00318 
00330   Timer(const std::string &name, TimerMask timer_mask, const Timer parent);
00331 
00346   Timer(const std::string &name, TimerMask timer_mask, const Timer parent, const TimerSet &timer_set);
00347 
00352   explicit Timer(TimerImpl &timer_impl)
00353     : m_timerImpl(&timer_impl)
00354   {}
00355 
00356   explicit Timer(TimerImpl *timer_impl)
00357     : m_timerImpl(timer_impl)
00358   {}
00359 
00360   Timer(const Timer &timer)
00361     : m_timerImpl(timer.m_timerImpl)
00362   {}
00363 
00364   Timer &operator=(const Timer &timer) {
00365     if (this != &timer)
00366       m_timerImpl = timer.m_timerImpl;
00367 
00368     return *this;
00369   }
00370 
00371   virtual ~Timer()
00372   {}
00373 
00374   const TimerList &getTimerList() const;
00375 
00376   TimerList::iterator begin();
00377   TimerList::const_iterator begin() const;
00378   TimerList::iterator end();
00379   TimerList::const_iterator end() const;
00380 
00387   const std::string &getName() const;
00388 
00389 
00395   const TimerSet &getTimerSet() const;
00396 
00402   TimerMask getTimerMask() const;
00403 
00404   bool shouldRecord() const;
00405 
00412   double getSubtimerLapCount() const;
00413 
00421   template <class T>
00422   const Metric<T> &getMetric() const;
00423 
00430   double accumulateSubtimerLapCounts() const;
00431 
00437   Timer &start();
00438 
00444   Timer &lap();
00445 
00452   Timer &stop();
00453 
00459   void checkpoint() const;
00460 
00469   Writer &dump(Writer& dout) const;
00470 
00471 private:
00472   TimerImpl *   m_timerImpl;      
00473 };
00474 
00475 
00476 
00485 class TimeBlock
00486 {
00487 public:
00501   explicit TimeBlock(Timer &timer, bool start_timer = true)
00502     : m_timer(timer),
00503       m_started(start_timer)
00504   {
00505     if (start_timer)
00506       m_timer.start();
00507   }
00508 
00509 private:
00510   TimeBlock(const TimeBlock &);
00511   TimeBlock &operator=(const TimeBlock &);
00512 
00513 public:
00518   ~TimeBlock() {
00519     try {
00520       if (m_started)
00521         m_timer.stop();
00522     }
00523     catch (...) {
00524     }
00525   }
00526 
00531   void start() {
00532     m_started = true;
00533     m_timer.start();
00534   }
00535 
00541   void lap() {
00542     m_timer.lap();
00543   }
00544 
00549   void stop() {
00550     m_started = false;
00551     m_timer.stop();
00552   }
00553 
00554 private:
00555   Timer &               m_timer;  
00556   bool      m_started;  
00557 };
00558 
00571 class TimeBlockSynchronized
00572 {
00573 public:
00587   TimeBlockSynchronized(Timer &timer, ParallelMachine mpi_comm, bool start_timer = true);
00588 
00594   ~TimeBlockSynchronized();
00595 
00601   void start();
00602 
00607   void stop();
00608 
00609 private:
00610   Timer &     m_timer;  
00611   ParallelMachine   m_mpiComm;  
00612   bool      m_started;  
00613 };
00614 
00615 
00627 template <class T>
00628 inline Writer &operator<<(Writer &dout, const Timer::Metric<T> &timer) {
00629   return timer.dump(dout);
00630 }
00631 
00643 inline Writer &operator<<(Writer &dout, const Timer &timer) {
00644   return timer.dump(dout);
00645 }
00646 
00647 } // namespace diag
00648 } // namespace stk
00649 
00653 
00654 #endif // stk_util_diag_Timer_hpp

Generated on Tue Jul 13 09:27:32 2010 for Sierra Toolkit by  doxygen 1.4.7