Timer.h revision f892c42725ed36c97e8ce10e758170cf6f1aff83
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 <stdio.h>
15#include <memory>
16#include <string>
17#include "lldb/lldb-private.h"
18#include "lldb/Host/TimeValue.h"
19
20namespace 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
34class Timer
35{
36public:
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
65protected:
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;
90private:
91    Timer();
92    DISALLOW_COPY_AND_ASSIGN (Timer);
93};
94
95    class ScopedTimer
96    {
97    public:
98        ScopedTimer() :
99            m_start (TimeValue::Now())
100        {
101        }
102
103        uint64_t
104        GetElapsedNanoSeconds() const
105        {
106            return TimeValue::Now() - m_start;
107        }
108
109    protected:
110        TimeValue m_start;
111    };
112
113    class ScopedTimerAggregator
114    {
115    public:
116        ScopedTimerAggregator(const char *desc) :
117            m_description (desc),
118            m_total_nsec()
119        {
120        }
121
122        ~ScopedTimerAggregator()
123        {
124            printf ("Total nsec spent in %s is %llu\n", m_description.c_str(), m_total_nsec);
125        }
126        void
127        Aggregate (const ScopedTimer &scoped_timer)
128        {
129            m_total_nsec += scoped_timer.GetElapsedNanoSeconds();
130        }
131
132    protected:
133        std::string m_description;
134        uint64_t m_total_nsec;
135    };
136
137
138} // namespace lldb_private
139
140#endif  // #if defined(__cplusplus)
141#endif // #ifndef liblldb_Timer_h_
142