1 //===-- Timer.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Timer_h_
11 #define liblldb_Timer_h_
12 #if defined(__cplusplus)
17 #include "lldb/lldb-private.h"
18 #include "lldb/Host/TimeValue.h"
20 namespace lldb_private {
22 //----------------------------------------------------------------------
23 /// @class Timer Timer.h "lldb/Core/Timer.h"
24 /// @brief A timer class that simplifies common timing metrics.
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
32 //----------------------------------------------------------------------
40 //--------------------------------------------------------------
41 /// Default constructor.
42 //--------------------------------------------------------------
43 Timer(const char *category, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
45 //--------------------------------------------------------------
47 //--------------------------------------------------------------
54 SetDisplayDepth (uint32_t depth);
57 SetQuiet (bool value);
60 DumpCategoryTimes (Stream *s);
63 ResetCategoryTimes ();
68 ChildStarted (const TimeValue& time);
71 ChildStopped (const TimeValue& time);
74 GetTotalElapsedNanoSeconds();
77 GetTimerElapsedNanoSeconds();
79 //--------------------------------------------------------------
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;
92 DISALLOW_COPY_AND_ASSIGN (Timer);
99 m_start (TimeValue::Now())
108 GetElapsedNanoSeconds() const
110 return TimeValue::Now() - m_start;
116 m_start = TimeValue::Now();
120 PrintfElapsed (const char *format, ...) __attribute__ ((format (printf, 2, 3)))
122 TimeValue now (TimeValue::Now());
123 const uint64_t elapsed_nsec = now - m_start;
124 const char *unit = NULL;
126 if (elapsed_nsec < 1000)
129 elapsed_value = (float)elapsed_nsec;
131 else if (elapsed_nsec < 1000000)
134 elapsed_value = (float)elapsed_nsec/1000.0f;
136 else if (elapsed_nsec < 1000000000)
139 elapsed_value = (float)elapsed_nsec/1000000.0f;
144 elapsed_value = (float)elapsed_nsec/1000000000.0f;
146 int result = printf ("%3.2f %s: ", elapsed_value, unit);
148 va_start (args, format);
149 result += vprintf (format, args);
157 } // namespace lldb_private
159 #endif // #if defined(__cplusplus)
160 #endif // #ifndef liblldb_Timer_h_