Sierra Toolkit Version of the Day
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/stk_config.h>
00018 #if defined( STK_HAS_MPI )
00019 #include <mpi.h>
00020 #endif
00021 
00022 #include <stk_util/diag/TimerMetricTraits.hpp>
00023 #include <stk_util/parallel/Parallel.hpp>
00024 #include <stk_util/environment/FormatTime.hpp>
00025 #include <stk_util/diag/Writer_fwd.hpp>
00026 
00027 #include <stk_util/diag/String.hpp>
00028 #include <stk_util/diag/WriterParser.hpp>
00029 #include <stk_util/diag/Option.hpp>
00030 
00031 
00036 
00037 namespace stk {
00038 namespace diag {
00039 
00040 class Timer;
00041 class TimerSet;
00042 class TimerImpl;
00043 
00044 typedef unsigned TimerMask;        
00045 
00052 MetricsMask getEnabledTimerMetricsMask();
00053 
00062 void setEnabledTimerMetricsMask(MetricsMask timer_mask);
00063 
00071 void updateRootTimer(Timer root_timer);
00072 
00086 Timer createRootTimer(const std::string &name, const TimerSet &timer_set);
00087 
00094 void deleteRootTimer(Timer timer);
00095 
00110 std::vector<Timer> &findTimers(Timer root_timer, const std::string &path_tail, std::vector<Timer> &found_timers);
00111 
00117 class TimerSet
00118 {
00119 public:
00120   explicit TimerSet(TimerMask enabled_timer_mask)
00121     : m_enabledTimerMask(enabled_timer_mask)
00122   {}
00123 
00124 private:
00125   TimerSet(const TimerSet &timer_set)
00126     : m_enabledTimerMask(timer_set.m_enabledTimerMask)
00127   {}
00128 
00129   TimerSet &operator=(TimerSet &timer_set) {
00130     m_enabledTimerMask = timer_set.m_enabledTimerMask;
00131 
00132     return *this;
00133   }
00134 
00135 public:
00136   ~TimerSet()
00137   {}
00138 
00145   TimerMask getEnabledTimerMask() const {
00146     return m_enabledTimerMask;
00147   }
00148 
00157   void setEnabledTimerMask(TimerMask timer_mask) {
00158     m_enabledTimerMask = timer_mask;
00159   }
00160 
00169   bool shouldRecord(TimerMask timer_mask) const {
00170     return (timer_mask == 0 || (m_enabledTimerMask & timer_mask));
00171   }
00172 
00173 private:
00174   TimerMask    m_enabledTimerMask;  
00175 };
00176 
00177 
00178 typedef std::list<Timer> TimerList;    
00179 
00185 class Timer
00186 {
00187   friend class TimerImpl;
00188   friend class TimeBlock;
00189   friend class TimeBlockSynchronized;
00190   friend void updateRootTimer(Timer);
00191   friend Timer createRootTimer(const std::string &, const TimerSet &);
00192   friend void deleteRootTimer(Timer);
00193   friend std::vector<Timer> &findTimers(Timer, const std::string &, std::vector<Timer> &);
00194 
00195 public:
00204   template <typename T>
00205   struct Metric
00206   {
00207     Metric()
00208       : m_lapStart(0),
00209         m_lapStop(0),
00210         m_accumulatedLap(0),
00211         m_checkpoint(0)
00212     {}
00213 
00218     void reset() {
00219       m_lapStart = m_lapStop = m_accumulatedLap = m_checkpoint = 0;
00220     }
00221 
00227     typename MetricTraits<T>::Type addLap() {
00228       return m_accumulatedLap += m_lapStop - m_lapStart;
00229     }
00230 
00236     void checkpoint() const {
00237       m_checkpoint = m_accumulatedLap;
00238     }
00239 
00246     typename MetricTraits<T>::Type getLap() const {
00247       return m_lapStop - m_lapStart;
00248     }
00249 
00255     typename MetricTraits<T>::Type getStart() const {
00256       return m_lapStart;
00257     }
00258 
00264     typename MetricTraits<T>::Type getStop() const {
00265       return m_lapStop;
00266     }
00267 
00279     typename MetricTraits<T>::Type getAccumulatedLap(bool arg_checkpoint = false) const {
00280       if (arg_checkpoint)
00281         return m_accumulatedLap - m_checkpoint;
00282       else
00283         return m_accumulatedLap;
00284     }
00285 
00296     Writer &dump(Writer &dout) const;
00297 
00298     typename MetricTraits<T>::Type    m_lapStart;    
00299     typename MetricTraits<T>::Type    m_lapStop;    
00300     typename MetricTraits<T>::Type    m_accumulatedLap;  
00301     mutable typename MetricTraits<T>::Type      m_checkpoint;    
00302   };
00303 
00313   Timer(const std::string &name, const Timer parent);
00314 
00327   Timer(const std::string &name, const Timer parent, const TimerSet &timer_set);
00328 
00340   Timer(const std::string &name, TimerMask timer_mask, const Timer parent);
00341 
00356   Timer(const std::string &name, TimerMask timer_mask, const Timer parent, const TimerSet &timer_set);
00357 
00362   explicit Timer(TimerImpl &timer_impl)
00363     : m_timerImpl(&timer_impl)
00364   {}
00365 
00366   explicit Timer(TimerImpl *timer_impl)
00367     : m_timerImpl(timer_impl)
00368   {}
00369 
00370   Timer(const Timer &timer)
00371     : m_timerImpl(timer.m_timerImpl)
00372   {}
00373 
00374   Timer &operator=(const Timer &timer) {
00375     if (this != &timer)
00376       m_timerImpl = timer.m_timerImpl;
00377 
00378     return *this;
00379   }
00380 
00381   virtual ~Timer()
00382   {}
00383 
00384   const TimerList &getTimerList() const;
00385 
00386   TimerList::iterator begin();
00387   TimerList::const_iterator begin() const;
00388   TimerList::iterator end();
00389   TimerList::const_iterator end() const;
00390 
00397   const std::string &getName() const;
00398 
00399 
00405   const TimerSet &getTimerSet() const;
00406 
00412   TimerMask getTimerMask() const;
00413 
00414   bool shouldRecord() const;
00415 
00422   double getSubtimerLapCount() const;
00423 
00431   template <class T>
00432   const Metric<T> &getMetric() const;
00433 
00440   double accumulateSubtimerLapCounts() const;
00441 
00447   Timer &start();
00448 
00454   Timer &lap();
00455 
00462   Timer &stop();
00463 
00469   void checkpoint() const;
00470 
00479   Writer &dump(Writer& dout) const;
00480 
00481 private:
00482   TimerImpl *    m_timerImpl;      
00483 };
00484 
00485 
00486 
00495 class TimeBlock
00496 {
00497 public:
00511   explicit TimeBlock(Timer &timer, bool start_timer = true)
00512     : m_timer(timer),
00513       m_started(start_timer)
00514   {
00515     if (start_timer)
00516       m_timer.start();
00517   }
00518 
00519 private:
00520   TimeBlock(const TimeBlock &);
00521   TimeBlock &operator=(const TimeBlock &);
00522 
00523 public:
00528   ~TimeBlock() {
00529     try {
00530       if (m_started)
00531         m_timer.stop();
00532     }
00533     catch (...) {
00534     }
00535   }
00536 
00541   void start() {
00542     m_started = true;
00543     m_timer.start();
00544   }
00545 
00551   void lap() {
00552     m_timer.lap();
00553   }
00554 
00559   void stop() {
00560     m_started = false;
00561     m_timer.stop();
00562   }
00563 
00564 private:
00565   Timer &               m_timer;  
00566   bool      m_started;  
00567 };
00568 
00581 class TimeBlockSynchronized
00582 {
00583 public:
00597   TimeBlockSynchronized(Timer &timer, ParallelMachine mpi_comm, bool start_timer = true);
00598 
00604   ~TimeBlockSynchronized();
00605 
00611   void start();
00612 
00617   void stop();
00618 
00619 private:
00620   Timer &      m_timer;  
00621   ParallelMachine    m_mpiComm;  
00622   bool      m_started;  
00623 };
00624 
00625 
00637 template <class T>
00638 inline Writer &operator<<(Writer &dout, const Timer::Metric<T> &timer) {
00639   return timer.dump(dout);
00640 }
00641 
00653 inline Writer &operator<<(Writer &dout, const Timer &timer) {
00654   return timer.dump(dout);
00655 }
00656 
00657 } // namespace diag
00658 } // namespace stk
00659 
00660 
00661 namespace sierra {
00662 namespace Diag {
00663 
00664 typedef stk::diag::Timer Timer;
00665 typedef stk::diag::TimerSet TimerSet;
00666 typedef stk::TimeFormat TimeFormat;
00667 typedef stk::diag::TimeBlock TimeBlock;
00668 typedef stk::diag::TimeBlockSynchronized TimeBlockSynchronized;
00669 
00675 enum TimerSetMask{
00676   TIMER_DOMAIN    = 0x00000001,   
00677   TIMER_REGION    = 0x00000002,   
00678   TIMER_PROCEDURE = 0x00000004,   
00679   TIMER_MECHANICS = 0x00000008,   
00680   TIMER_ALGORITHM = 0x00000010,   
00681   TIMER_SOLVER    = 0x00000020,   
00682   TIMER_CONTACT   = 0x00000040,   
00683   TIMER_MATERIAL  = 0x00000080,   
00684   TIMER_SEARCH    = 0x00000100,   
00685   TIMER_TRANSFER  = 0x00000200,   
00686   TIMER_ADAPTIVITY  = 0x00000400,   
00687   TIMER_RECOVERY  = 0x00000800,   
00688   TIMER_PROFILE_1 = 0x00001000,   
00689   TIMER_PROFILE_2 = 0x00002000,   
00690   TIMER_PROFILE_3 = 0x00004000,   
00691   TIMER_PROFILE_4 = 0x00008000,   
00692   TIMER_APP_1   = 0x00010000,   
00693   TIMER_APP_2   = 0x00020000,   
00694   TIMER_APP_3   = 0x00040000,   
00695   TIMER_APP_4   = 0x00080000,   
00696   TIMER_ALL   = 0x000FFFFF,   
00697   TIMER_NONE    = 0x00000000,   
00698 
00699   TIMER_FORCE   = 0x00000000    
00700 };
00701 
00702 
00703 TimerSet &sierraTimerSet();
00704 
00705 Timer &sierraTimer();
00706 
00707 void sierraTimerDestroy();
00708 
00709 class TimerParser;
00710 
00716 typedef sierra::OptionMask TimerMask;     
00717 
00723 enum {
00724   DEFAULT_TIMER_NAME_MAX_WIDTH = 40     
00725 };
00726 
00733 TimerParser &theTimerParser();
00734 
00743 void setEnabledTimerMask(TimerMask timer_mask);
00744 
00751 TimerMask getEnabledTimerMask();
00752 
00753 void setTimeFormat(int time_format);
00754 
00755 void setTimeFormatMillis();
00756 
00757 int getTimeFormat();
00758 
00767 void setTimerNameMaxWidth(size_t width);
00768 
00776 size_t getTimerNameMaxWidth();
00777 
00778 stk::diag::MetricTraits<stk::diag::CPUTime>::Type getCPULapTime(Timer timer);
00779 
00780 stk::diag::MetricTraits<stk::diag::CPUTime>::Type getCPUAccumulatedLapTime(Timer timer);
00781 
00782 stk::diag::MetricTraits<stk::diag::CPUTime>::Type getSierraCPUTime();
00783 stk::diag::MetricTraits<stk::diag::CPUTime>::Type getSierraWallTime();
00784 
00785 
00790 class TimerParser : public OptionMaskParser
00791 {
00792 public:
00797   TimerParser();
00798 
00808   Mask parse(const char *mask_string) const;
00809 
00819   virtual void parseArg(const std::string &name, const std::string &arg) const;  
00820 
00821   mutable stk::diag::MetricsMask        m_metricsSetMask;
00822   mutable stk::diag::MetricsMask        m_metricsMask;
00823 };
00824 
00825 
00826 class SierraRootTimer 
00827 {
00828   public:
00829     SierraRootTimer();
00830     virtual ~SierraRootTimer();
00831     stk::diag::Timer & sierraTimer();
00832 
00833   private:
00834     stk::diag::Timer m_sierraTimer; 
00835 };
00836 
00837 } // namespace Diag
00838 } // namespace sierra
00839 
00843 
00844 #endif // STK_UTIL_DIAG_Timer_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines