blob: dd37d79e5fd381988249e8a30ee74130d2b970b1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Timer.cpp -----------------------------------------------*- 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#include "lldb/Core/Timer.h"
10
11#include <map>
12#include <vector>
Eli Friedman88966972010-06-09 08:50:27 +000013#include <algorithm>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include "lldb/Core/Stream.h"
16#include "lldb/Host/Mutex.h"
17
Eli Friedman88966972010-06-09 08:50:27 +000018#include <stdio.h>
19
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020using namespace lldb_private;
21
22#define TIMER_INDENT_AMOUNT 2
Greg Clayton09960032010-09-10 18:31:35 +000023static bool g_quiet = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024uint32_t Timer::g_depth = 0;
25uint32_t Timer::g_display_depth = 0;
26FILE * Timer::g_file = NULL;
27typedef std::vector<Timer *> TimerStack;
28typedef std::map<const char *, uint64_t> CategoryMap;
29static pthread_key_t g_key;
30
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031static Mutex &
32GetCategoryMutex()
33{
34 static Mutex g_category_mutex(Mutex::eMutexTypeNormal);
35 return g_category_mutex;
36}
37
38static CategoryMap &
39GetCategoryMap()
40{
41 static CategoryMap g_category_map;
42 return g_category_map;
43}
44
45
46static TimerStack *
47GetTimerStackForCurrentThread ()
48{
49 void *timer_stack = ::pthread_getspecific (g_key);
50 if (timer_stack == NULL)
51 {
52 ::pthread_setspecific (g_key, new TimerStack);
53 timer_stack = ::pthread_getspecific (g_key);
54 }
55 return (TimerStack *)timer_stack;
56}
57
58void
59ThreadSpecificCleanup (void *p)
60{
61 delete (TimerStack *)p;
62}
63
64void
Jim Inghamf7f4f502010-11-04 23:19:21 +000065Timer::SetQuiet (bool value)
66{
67 g_quiet = value;
68}
69
70void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071Timer::Initialize ()
72{
73 Timer::g_file = stdout;
74 ::pthread_key_create (&g_key, ThreadSpecificCleanup);
75
76}
77
78Timer::Timer (const char *category, const char *format, ...) :
79 m_category (category),
80 m_total_start (),
81 m_timer_start (),
82 m_total_ticks (0),
83 m_timer_ticks (0)
84{
85 if (g_depth++ < g_display_depth)
86 {
Greg Clayton09960032010-09-10 18:31:35 +000087 if (g_quiet == false)
88 {
89 // Indent
90 ::fprintf (g_file, "%*s", g_depth * TIMER_INDENT_AMOUNT, "");
91 // Print formatted string
92 va_list args;
93 va_start (args, format);
94 ::vfprintf (g_file, format, args);
95 va_end (args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096
Greg Clayton09960032010-09-10 18:31:35 +000097 // Newline
98 ::fprintf (g_file, "\n");
99 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 TimeValue start_time(TimeValue::Now());
101 m_total_start = start_time;
102 m_timer_start = start_time;
103 TimerStack *stack = GetTimerStackForCurrentThread ();
104 if (stack)
105 {
106 if (stack->empty() == false)
107 stack->back()->ChildStarted (start_time);
108 stack->push_back(this);
109 }
110 }
111}
112
113
114Timer::~Timer()
115{
116 if (m_total_start.IsValid())
117 {
118 TimeValue stop_time = TimeValue::Now();
119 bool notify = false;
120 if (m_total_start.IsValid())
121 {
122 m_total_ticks += (stop_time - m_total_start);
123 m_total_start.Clear();
124 notify = true;
125 }
126 if (m_timer_start.IsValid())
127 {
128 m_timer_ticks += (stop_time - m_timer_start);
129 m_timer_start.Clear();
130 }
131
132 TimerStack *stack = GetTimerStackForCurrentThread ();
133 if (stack)
134 {
135 assert (stack->back() == this);
136 stack->pop_back();
137 if (stack->empty() == false)
138 stack->back()->ChildStopped(stop_time);
139 }
140
141 const uint64_t total_nsec_uint = GetTotalElapsedNanoSeconds();
142 const uint64_t timer_nsec_uint = GetTimerElapsedNanoSeconds();
143 const double total_nsec = total_nsec_uint;
144 const double timer_nsec = timer_nsec_uint;
Greg Clayton09960032010-09-10 18:31:35 +0000145
146 if (g_quiet == false)
147 {
148
149 ::fprintf (g_file,
150 "%*s%.9f sec (%.9f sec)\n",
151 (g_depth - 1) *TIMER_INDENT_AMOUNT, "",
152 total_nsec / 1000000000.0,
153 timer_nsec / 1000000000.0);
154 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
156 // Keep total results for each category so we can dump results.
157 Mutex::Locker locker (GetCategoryMutex());
158 CategoryMap &category_map = GetCategoryMap();
159 category_map[m_category] += timer_nsec_uint;
160 }
161 if (g_depth > 0)
162 --g_depth;
163}
164
165uint64_t
166Timer::GetTotalElapsedNanoSeconds()
167{
168 uint64_t total_ticks = m_total_ticks;
169
170 // If we are currently running, we need to add the current
171 // elapsed time of the running timer...
172 if (m_total_start.IsValid())
173 total_ticks += (TimeValue::Now() - m_total_start);
174
175 return total_ticks;
176}
177
178uint64_t
179Timer::GetTimerElapsedNanoSeconds()
180{
181 uint64_t timer_ticks = m_timer_ticks;
182
183 // If we are currently running, we need to add the current
184 // elapsed time of the running timer...
185 if (m_timer_start.IsValid())
186 timer_ticks += (TimeValue::Now() - m_timer_start);
187
188 return timer_ticks;
189}
190
191void
192Timer::ChildStarted (const TimeValue& start_time)
193{
194 if (m_timer_start.IsValid())
195 {
196 m_timer_ticks += (start_time - m_timer_start);
197 m_timer_start.Clear();
198 }
199}
200
201void
202Timer::ChildStopped (const TimeValue& stop_time)
203{
204 if (!m_timer_start.IsValid())
205 m_timer_start = stop_time;
206}
207
208void
209Timer::SetDisplayDepth (uint32_t depth)
210{
211 g_display_depth = depth;
212}
213
214
215/* binary function predicate:
216 * - returns whether a person is less than another person
217 */
218static bool
219CategoryMapIteratorSortCriterion (const CategoryMap::const_iterator& lhs, const CategoryMap::const_iterator& rhs)
220{
221 return lhs->second > rhs->second;
222}
223
224
225void
226Timer::ResetCategoryTimes ()
227{
228 Mutex::Locker locker (GetCategoryMutex());
229 CategoryMap &category_map = GetCategoryMap();
230 category_map.clear();
231}
232
233void
234Timer::DumpCategoryTimes (Stream *s)
235{
236 Mutex::Locker locker (GetCategoryMutex());
237 CategoryMap &category_map = GetCategoryMap();
238 std::vector<CategoryMap::const_iterator> sorted_iterators;
239 CategoryMap::const_iterator pos, end = category_map.end();
240 for (pos = category_map.begin(); pos != end; ++pos)
241 {
242 sorted_iterators.push_back (pos);
243 }
244 std::sort (sorted_iterators.begin(), sorted_iterators.end(), CategoryMapIteratorSortCriterion);
245
246 const size_t count = sorted_iterators.size();
247 for (size_t i=0; i<count; ++i)
248 {
249 const double timer_nsec = sorted_iterators[i]->second;
250 s->Printf("%.9f sec for %s\n", timer_nsec / 1000000000.0, sorted_iterators[i]->first);
251 }
Eli Friedman88966972010-06-09 08:50:27 +0000252}