blob: 542cbe412cb5f8fc549f549f71b1e5925e716a8c [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
23uint32_t Timer::g_depth = 0;
24uint32_t Timer::g_display_depth = 0;
25FILE * Timer::g_file = NULL;
26typedef std::vector<Timer *> TimerStack;
27typedef std::map<const char *, uint64_t> CategoryMap;
28static pthread_key_t g_key;
29
30
31static 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
65Timer::Initialize ()
66{
67 Timer::g_file = stdout;
68 ::pthread_key_create (&g_key, ThreadSpecificCleanup);
69
70}
71
72Timer::Timer (const char *category, const char *format, ...) :
73 m_category (category),
74 m_total_start (),
75 m_timer_start (),
76 m_total_ticks (0),
77 m_timer_ticks (0)
78{
79 if (g_depth++ < g_display_depth)
80 {
81 // Indent
82 ::fprintf (g_file, "%*s", g_depth * TIMER_INDENT_AMOUNT, "");
83 // Print formatted string
84 va_list args;
85 va_start (args, format);
86 ::vfprintf (g_file, format, args);
87 va_end (args);
88
89 // Newline
90 ::fprintf (g_file, "\n");
91 TimeValue start_time(TimeValue::Now());
92 m_total_start = start_time;
93 m_timer_start = start_time;
94 TimerStack *stack = GetTimerStackForCurrentThread ();
95 if (stack)
96 {
97 if (stack->empty() == false)
98 stack->back()->ChildStarted (start_time);
99 stack->push_back(this);
100 }
101 }
102}
103
104
105Timer::~Timer()
106{
107 if (m_total_start.IsValid())
108 {
109 TimeValue stop_time = TimeValue::Now();
110 bool notify = false;
111 if (m_total_start.IsValid())
112 {
113 m_total_ticks += (stop_time - m_total_start);
114 m_total_start.Clear();
115 notify = true;
116 }
117 if (m_timer_start.IsValid())
118 {
119 m_timer_ticks += (stop_time - m_timer_start);
120 m_timer_start.Clear();
121 }
122
123 TimerStack *stack = GetTimerStackForCurrentThread ();
124 if (stack)
125 {
126 assert (stack->back() == this);
127 stack->pop_back();
128 if (stack->empty() == false)
129 stack->back()->ChildStopped(stop_time);
130 }
131
132 const uint64_t total_nsec_uint = GetTotalElapsedNanoSeconds();
133 const uint64_t timer_nsec_uint = GetTimerElapsedNanoSeconds();
134 const double total_nsec = total_nsec_uint;
135 const double timer_nsec = timer_nsec_uint;
136 ::fprintf (g_file,
137 "%*s%.9f sec (%.9f sec)\n",
138 (g_depth - 1) *TIMER_INDENT_AMOUNT, "",
139 total_nsec / 1000000000.0,
140 timer_nsec / 1000000000.0);
141
142 // Keep total results for each category so we can dump results.
143 Mutex::Locker locker (GetCategoryMutex());
144 CategoryMap &category_map = GetCategoryMap();
145 category_map[m_category] += timer_nsec_uint;
146 }
147 if (g_depth > 0)
148 --g_depth;
149}
150
151uint64_t
152Timer::GetTotalElapsedNanoSeconds()
153{
154 uint64_t total_ticks = m_total_ticks;
155
156 // If we are currently running, we need to add the current
157 // elapsed time of the running timer...
158 if (m_total_start.IsValid())
159 total_ticks += (TimeValue::Now() - m_total_start);
160
161 return total_ticks;
162}
163
164uint64_t
165Timer::GetTimerElapsedNanoSeconds()
166{
167 uint64_t timer_ticks = m_timer_ticks;
168
169 // If we are currently running, we need to add the current
170 // elapsed time of the running timer...
171 if (m_timer_start.IsValid())
172 timer_ticks += (TimeValue::Now() - m_timer_start);
173
174 return timer_ticks;
175}
176
177void
178Timer::ChildStarted (const TimeValue& start_time)
179{
180 if (m_timer_start.IsValid())
181 {
182 m_timer_ticks += (start_time - m_timer_start);
183 m_timer_start.Clear();
184 }
185}
186
187void
188Timer::ChildStopped (const TimeValue& stop_time)
189{
190 if (!m_timer_start.IsValid())
191 m_timer_start = stop_time;
192}
193
194void
195Timer::SetDisplayDepth (uint32_t depth)
196{
197 g_display_depth = depth;
198}
199
200
201/* binary function predicate:
202 * - returns whether a person is less than another person
203 */
204static bool
205CategoryMapIteratorSortCriterion (const CategoryMap::const_iterator& lhs, const CategoryMap::const_iterator& rhs)
206{
207 return lhs->second > rhs->second;
208}
209
210
211void
212Timer::ResetCategoryTimes ()
213{
214 Mutex::Locker locker (GetCategoryMutex());
215 CategoryMap &category_map = GetCategoryMap();
216 category_map.clear();
217}
218
219void
220Timer::DumpCategoryTimes (Stream *s)
221{
222 Mutex::Locker locker (GetCategoryMutex());
223 CategoryMap &category_map = GetCategoryMap();
224 std::vector<CategoryMap::const_iterator> sorted_iterators;
225 CategoryMap::const_iterator pos, end = category_map.end();
226 for (pos = category_map.begin(); pos != end; ++pos)
227 {
228 sorted_iterators.push_back (pos);
229 }
230 std::sort (sorted_iterators.begin(), sorted_iterators.end(), CategoryMapIteratorSortCriterion);
231
232 const size_t count = sorted_iterators.size();
233 for (size_t i=0; i<count; ++i)
234 {
235 const double timer_nsec = sorted_iterators[i]->second;
236 s->Printf("%.9f sec for %s\n", timer_nsec / 1000000000.0, sorted_iterators[i]->first);
237 }
Eli Friedman88966972010-06-09 08:50:27 +0000238}