aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/Timer.h
blob: ffaeba6fce9b71d8c1be4740bb8f4b6723be1c1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//===-- Timer.h -------------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_Timer_h_
#define liblldb_Timer_h_

// C Includes
#include <stdarg.h>
#include <stdio.h>

// C++ Includes
#include <atomic>
#include <mutex>

// Other libraries and framework includes
// Project includes
#include "lldb/lldb-private.h"
#include "lldb/Host/TimeValue.h"

namespace lldb_private {

//----------------------------------------------------------------------
/// @class Timer Timer.h "lldb/Core/Timer.h"
/// @brief A timer class that simplifies common timing metrics.
///
/// A scoped timer class that allows a variety of pthread mutex
/// objects to have a mutex locked when a Timer::Locker
/// object is created, and unlocked when it goes out of scope or
/// when the Timer::Locker::Reset(pthread_mutex_t *)
/// is called. This provides an exception safe way to lock a mutex
/// in a scope.
//----------------------------------------------------------------------

class Timer
{
public:
    //--------------------------------------------------------------
    /// Default constructor.
    //--------------------------------------------------------------
    Timer(const char *category, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));

    //--------------------------------------------------------------
    /// Destructor
    //--------------------------------------------------------------
    ~Timer();

    static void
    Initialize ();

    void
    Dump ();

    static void
    SetDisplayDepth (uint32_t depth);
    
    static void
    SetQuiet (bool value);

    static void
    DumpCategoryTimes (Stream *s);

    static void
    ResetCategoryTimes ();

protected:
    void
    ChildStarted (const TimeValue& time);

    void
    ChildStopped (const TimeValue& time);

    uint64_t
    GetTotalElapsedNanoSeconds();

    uint64_t
    GetTimerElapsedNanoSeconds();

    const char *m_category;
    TimeValue m_total_start;
    TimeValue m_timer_start;
    uint64_t m_total_ticks; // Total running time for this timer including when other timers below this are running
    uint64_t m_timer_ticks; // Ticks for this timer that do not include when other timers below this one are running

    static std::atomic<bool> g_quiet;
    static std::atomic<unsigned> g_display_depth;
    static std::mutex g_file_mutex;
    static FILE* g_file;

private:
    Timer();
    DISALLOW_COPY_AND_ASSIGN (Timer);
};
    
class IntervalTimer
{
public:
    IntervalTimer() :
        m_start (TimeValue::Now())
    {
    }

    ~IntervalTimer() = default;

    uint64_t
    GetElapsedNanoSeconds() const
    {
        return TimeValue::Now() - m_start;
    }
    
    void
    Reset ()
    {
        m_start = TimeValue::Now();
    }
    
    int
    PrintfElapsed (const char *format, ...)  __attribute__ ((format (printf, 2, 3)))
    {
        TimeValue now (TimeValue::Now());
        const uint64_t elapsed_nsec = now - m_start;
        const char *unit = nullptr;
        float elapsed_value;
        if (elapsed_nsec < 1000)
        {
            unit = "ns";
            elapsed_value = (float)elapsed_nsec;
        }
        else if (elapsed_nsec < 1000000)
        {
            unit = "us";
            elapsed_value = (float)elapsed_nsec/1000.0f;
        }
        else if (elapsed_nsec < 1000000000)
        {
            unit = "ms";
            elapsed_value = (float)elapsed_nsec/1000000.0f;
        }
        else
        {
            unit = "sec";
            elapsed_value = (float)elapsed_nsec/1000000000.0f;
        }
        int result = printf ("%3.2f %s: ", elapsed_value, unit);
        va_list args;
        va_start (args, format);
        result += vprintf (format, args);
        va_end (args);
        return result;
    }

protected:
    TimeValue m_start;
};

} // namespace lldb_private

#endif // liblldb_Timer_h_