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