1 //===-- Timer.h -------------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Timer_h_ 11 #define liblldb_Timer_h_ 12 #if defined(__cplusplus) 13 14 #include <stdarg.h> 15 #include <stdio.h> 16 #include <string> 17 #include "lldb/lldb-private.h" 18 #include "lldb/Host/TimeValue.h" 19 20 namespace lldb_private { 21 22 //---------------------------------------------------------------------- 23 /// @class Timer Timer.h "lldb/Core/Timer.h" 24 /// @brief A timer class that simplifies common timing metrics. 25 /// 26 /// A scoped timer class that allows a variety of pthread mutex 27 /// objects to have a mutex locked when a Timer::Locker 28 /// object is created, and unlocked when it goes out of scope or 29 /// when the Timer::Locker::Reset(pthread_mutex_t *) 30 /// is called. This provides an exception safe way to lock a mutex 31 /// in a scope. 32 //---------------------------------------------------------------------- 33 34 class Timer 35 { 36 public: 37 static void 38 Initialize (); 39 40 //-------------------------------------------------------------- 41 /// Default constructor. 42 //-------------------------------------------------------------- 43 Timer(const char *category, const char *format, ...) __attribute__ ((format (printf, 3, 4))); 44 45 //-------------------------------------------------------------- 46 /// Desstructor 47 //-------------------------------------------------------------- 48 ~Timer(); 49 50 void 51 Dump (); 52 53 static void 54 SetDisplayDepth (uint32_t depth); 55 56 static void 57 SetQuiet (bool value); 58 59 static void 60 DumpCategoryTimes (Stream *s); 61 62 static void 63 ResetCategoryTimes (); 64 65 protected: 66 67 void 68 ChildStarted (const TimeValue& time); 69 70 void 71 ChildStopped (const TimeValue& time); 72 73 uint64_t 74 GetTotalElapsedNanoSeconds(); 75 76 uint64_t 77 GetTimerElapsedNanoSeconds(); 78 79 //-------------------------------------------------------------- 80 /// Member variables 81 //-------------------------------------------------------------- 82 const char *m_category; 83 TimeValue m_total_start; 84 TimeValue m_timer_start; 85 uint64_t m_total_ticks; // Total running time for this timer including when other timers below this are running 86 uint64_t m_timer_ticks; // Ticks for this timer that do not include when other timers below this one are running 87 static uint32_t g_depth; 88 static uint32_t g_display_depth; 89 static FILE * g_file; 90 private: 91 Timer(); 92 DISALLOW_COPY_AND_ASSIGN (Timer); 93 }; 94 95 class IntervalTimer 96 { 97 public: 98 IntervalTimer() : 99 m_start (TimeValue::Now()) 100 { 101 } 102 103 ~IntervalTimer() 104 { 105 } 106 107 uint64_t 108 GetElapsedNanoSeconds() const 109 { 110 return TimeValue::Now() - m_start; 111 } 112 113 void 114 Reset () 115 { 116 m_start = TimeValue::Now(); 117 } 118 119 int 120 PrintfElapsed (const char *format, ...) __attribute__ ((format (printf, 2, 3))) 121 { 122 TimeValue now (TimeValue::Now()); 123 const uint64_t elapsed_nsec = now - m_start; 124 const char *unit = NULL; 125 float elapsed_value; 126 if (elapsed_nsec < 1000) 127 { 128 unit = "ns"; 129 elapsed_value = (float)elapsed_nsec; 130 } 131 else if (elapsed_nsec < 1000000) 132 { 133 unit = "us"; 134 elapsed_value = (float)elapsed_nsec/1000.0f; 135 } 136 else if (elapsed_nsec < 1000000000) 137 { 138 unit = "ms"; 139 elapsed_value = (float)elapsed_nsec/1000000.0f; 140 } 141 else 142 { 143 unit = "sec"; 144 elapsed_value = (float)elapsed_nsec/1000000000.0f; 145 } 146 int result = printf ("%3.2f %s: ", elapsed_value, unit); 147 va_list args; 148 va_start (args, format); 149 result += vprintf (format, args); 150 va_end (args); 151 return result; 152 } 153 protected: 154 TimeValue m_start; 155 }; 156 157 } // namespace lldb_private 158 159 #endif // #if defined(__cplusplus) 160 #endif // #ifndef liblldb_Timer_h_ 161