blob: 7a6e5de75cf597760f3e14d1c113248cc770654c [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"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000017#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Eli Friedman88966972010-06-09 08:50:27 +000019#include <stdio.h>
20
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021using namespace lldb_private;
22
23#define TIMER_INDENT_AMOUNT 2
Tamas Berghammerd779da92015-10-23 10:34:29 +000024
25namespace
26{
27 typedef std::map<const char*, uint64_t> TimerCategoryMap;
28
29 struct TimerStack
30 {
31 TimerStack() :
32 m_depth(0)
33 {}
34
35 uint32_t m_depth;
36 std::vector<Timer*> m_stack;
37 };
38} // end of anonymous namespace
39
Tamas Berghammer5ee6b7f2015-10-23 10:53:31 +000040std::atomic<bool> Timer::g_quiet(true);
41std::atomic<unsigned> Timer::g_display_depth(0);
Tamas Berghammerd779da92015-10-23 10:34:29 +000042std::mutex Timer::g_file_mutex;
Tamas Berghammerd779da92015-10-23 10:34:29 +000043
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045static Mutex &
46GetCategoryMutex()
47{
48 static Mutex g_category_mutex(Mutex::eMutexTypeNormal);
49 return g_category_mutex;
50}
51
Enrico Granata5548cb52013-01-28 23:47:25 +000052static TimerCategoryMap &
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053GetCategoryMap()
54{
Enrico Granata5548cb52013-01-28 23:47:25 +000055 static TimerCategoryMap g_category_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 return g_category_map;
57}
58
Pavel Labath061140c2016-02-01 13:29:41 +000059static void
60ThreadSpecificCleanup(void *p)
61{
62 delete static_cast<TimerStack *>(p);
63}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65static TimerStack *
66GetTimerStackForCurrentThread ()
67{
Pavel Labath061140c2016-02-01 13:29:41 +000068 static lldb::thread_key_t g_key = Host::ThreadLocalStorageCreate(ThreadSpecificCleanup);
69
Virgile Bellob2f1fb22013-08-23 12:44:05 +000070 void *timer_stack = Host::ThreadLocalStorageGet(g_key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 if (timer_stack == NULL)
72 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +000073 Host::ThreadLocalStorageSet(g_key, new TimerStack);
74 timer_stack = Host::ThreadLocalStorageGet(g_key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 }
76 return (TimerStack *)timer_stack;
77}
78
79void
Jim Inghamf7f4f502010-11-04 23:19:21 +000080Timer::SetQuiet (bool value)
81{
82 g_quiet = value;
83}
84
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085Timer::Timer (const char *category, const char *format, ...) :
86 m_category (category),
87 m_total_start (),
88 m_timer_start (),
89 m_total_ticks (0),
90 m_timer_ticks (0)
91{
Tamas Berghammerd779da92015-10-23 10:34:29 +000092 TimerStack *stack = GetTimerStackForCurrentThread ();
93 if (!stack)
94 return;
95
96 if (stack->m_depth++ < g_display_depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 {
Greg Clayton09960032010-09-10 18:31:35 +000098 if (g_quiet == false)
99 {
Tamas Berghammerd779da92015-10-23 10:34:29 +0000100 std::lock_guard<std::mutex> lock(g_file_mutex);
101
Greg Clayton09960032010-09-10 18:31:35 +0000102 // Indent
Pavel Labath061140c2016-02-01 13:29:41 +0000103 ::fprintf(stdout, "%*s", stack->m_depth * TIMER_INDENT_AMOUNT, "");
Greg Clayton09960032010-09-10 18:31:35 +0000104 // Print formatted string
105 va_list args;
106 va_start (args, format);
Pavel Labath061140c2016-02-01 13:29:41 +0000107 ::vfprintf(stdout, format, args);
Greg Clayton09960032010-09-10 18:31:35 +0000108 va_end (args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
Greg Clayton09960032010-09-10 18:31:35 +0000110 // Newline
Pavel Labath061140c2016-02-01 13:29:41 +0000111 ::fprintf(stdout, "\n");
Greg Clayton09960032010-09-10 18:31:35 +0000112 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 TimeValue start_time(TimeValue::Now());
114 m_total_start = start_time;
115 m_timer_start = start_time;
Tamas Berghammerd779da92015-10-23 10:34:29 +0000116
117 if (!stack->m_stack.empty())
118 stack->m_stack.back()->ChildStarted (start_time);
119 stack->m_stack.push_back(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 }
121}
122
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123Timer::~Timer()
124{
Tamas Berghammerd779da92015-10-23 10:34:29 +0000125 TimerStack *stack = GetTimerStackForCurrentThread ();
126 if (!stack)
127 return;
128
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 if (m_total_start.IsValid())
130 {
131 TimeValue stop_time = TimeValue::Now();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 if (m_total_start.IsValid())
133 {
134 m_total_ticks += (stop_time - m_total_start);
135 m_total_start.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 }
137 if (m_timer_start.IsValid())
138 {
139 m_timer_ticks += (stop_time - m_timer_start);
140 m_timer_start.Clear();
141 }
142
Tamas Berghammerd779da92015-10-23 10:34:29 +0000143 assert (stack->m_stack.back() == this);
144 stack->m_stack.pop_back();
145 if (stack->m_stack.empty() == false)
146 stack->m_stack.back()->ChildStopped(stop_time);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
148 const uint64_t total_nsec_uint = GetTotalElapsedNanoSeconds();
149 const uint64_t timer_nsec_uint = GetTimerElapsedNanoSeconds();
150 const double total_nsec = total_nsec_uint;
151 const double timer_nsec = timer_nsec_uint;
Greg Clayton09960032010-09-10 18:31:35 +0000152
153 if (g_quiet == false)
154 {
Tamas Berghammerd779da92015-10-23 10:34:29 +0000155 std::lock_guard<std::mutex> lock(g_file_mutex);
Pavel Labath061140c2016-02-01 13:29:41 +0000156 ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", (stack->m_depth - 1) * TIMER_INDENT_AMOUNT, "",
157 total_nsec / 1000000000.0, timer_nsec / 1000000000.0);
Greg Clayton09960032010-09-10 18:31:35 +0000158 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159
160 // Keep total results for each category so we can dump results.
161 Mutex::Locker locker (GetCategoryMutex());
Enrico Granata5548cb52013-01-28 23:47:25 +0000162 TimerCategoryMap &category_map = GetCategoryMap();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 category_map[m_category] += timer_nsec_uint;
164 }
Tamas Berghammerd779da92015-10-23 10:34:29 +0000165 if (stack->m_depth > 0)
166 --stack->m_depth;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169uint64_t
170Timer::GetTotalElapsedNanoSeconds()
171{
172 uint64_t total_ticks = m_total_ticks;
173
174 // If we are currently running, we need to add the current
175 // elapsed time of the running timer...
176 if (m_total_start.IsValid())
177 total_ticks += (TimeValue::Now() - m_total_start);
178
179 return total_ticks;
180}
181
182uint64_t
183Timer::GetTimerElapsedNanoSeconds()
184{
185 uint64_t timer_ticks = m_timer_ticks;
186
187 // If we are currently running, we need to add the current
188 // elapsed time of the running timer...
189 if (m_timer_start.IsValid())
190 timer_ticks += (TimeValue::Now() - m_timer_start);
191
192 return timer_ticks;
193}
194
195void
196Timer::ChildStarted (const TimeValue& start_time)
197{
198 if (m_timer_start.IsValid())
199 {
200 m_timer_ticks += (start_time - m_timer_start);
201 m_timer_start.Clear();
202 }
203}
204
205void
206Timer::ChildStopped (const TimeValue& stop_time)
207{
208 if (!m_timer_start.IsValid())
209 m_timer_start = stop_time;
210}
211
212void
213Timer::SetDisplayDepth (uint32_t depth)
214{
215 g_display_depth = depth;
216}
217
218
219/* binary function predicate:
220 * - returns whether a person is less than another person
221 */
222static bool
Enrico Granata5548cb52013-01-28 23:47:25 +0000223CategoryMapIteratorSortCriterion (const TimerCategoryMap::const_iterator& lhs, const TimerCategoryMap::const_iterator& rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224{
225 return lhs->second > rhs->second;
226}
227
228
229void
230Timer::ResetCategoryTimes ()
231{
232 Mutex::Locker locker (GetCategoryMutex());
Enrico Granata5548cb52013-01-28 23:47:25 +0000233 TimerCategoryMap &category_map = GetCategoryMap();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 category_map.clear();
235}
236
237void
238Timer::DumpCategoryTimes (Stream *s)
239{
240 Mutex::Locker locker (GetCategoryMutex());
Enrico Granata5548cb52013-01-28 23:47:25 +0000241 TimerCategoryMap &category_map = GetCategoryMap();
242 std::vector<TimerCategoryMap::const_iterator> sorted_iterators;
243 TimerCategoryMap::const_iterator pos, end = category_map.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 for (pos = category_map.begin(); pos != end; ++pos)
245 {
246 sorted_iterators.push_back (pos);
247 }
248 std::sort (sorted_iterators.begin(), sorted_iterators.end(), CategoryMapIteratorSortCriterion);
249
250 const size_t count = sorted_iterators.size();
251 for (size_t i=0; i<count; ++i)
252 {
253 const double timer_nsec = sorted_iterators[i]->second;
254 s->Printf("%.9f sec for %s\n", timer_nsec / 1000000000.0, sorted_iterators[i]->first);
255 }
Eli Friedman88966972010-06-09 08:50:27 +0000256}