1 //===-- Timer.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/Timer.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Host/Host.h"
21 using namespace lldb_private;
23 #define TIMER_INDENT_AMOUNT 2
26 typedef std::map<const char *, std::chrono::nanoseconds> TimerCategoryMap;
27 typedef std::vector<Timer *> TimerStack;
28 } // end of anonymous namespace
30 std::atomic<bool> Timer::g_quiet(true);
31 std::atomic<unsigned> Timer::g_display_depth(0);
32 static std::mutex &GetFileMutex() {
33 static std::mutex *g_file_mutex_ptr = new std::mutex();
34 return *g_file_mutex_ptr;
37 static std::mutex &GetCategoryMutex() {
38 static std::mutex g_category_mutex;
39 return g_category_mutex;
42 static TimerCategoryMap &GetCategoryMap() {
43 static TimerCategoryMap g_category_map;
44 return g_category_map;
47 static void ThreadSpecificCleanup(void *p) {
48 delete static_cast<TimerStack *>(p);
51 static TimerStack *GetTimerStackForCurrentThread() {
52 static lldb::thread_key_t g_key =
53 Host::ThreadLocalStorageCreate(ThreadSpecificCleanup);
55 void *timer_stack = Host::ThreadLocalStorageGet(g_key);
56 if (timer_stack == NULL) {
57 Host::ThreadLocalStorageSet(g_key, new TimerStack);
58 timer_stack = Host::ThreadLocalStorageGet(g_key);
60 return (TimerStack *)timer_stack;
63 void Timer::SetQuiet(bool value) { g_quiet = value; }
65 Timer::Timer(const char *category, const char *format, ...)
66 : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
67 TimerStack *stack = GetTimerStackForCurrentThread();
71 stack->push_back(this);
72 if (g_quiet && stack->size() <= g_display_depth) {
73 std::lock_guard<std::mutex> lock(GetFileMutex());
76 ::fprintf(stdout, "%*s", int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "");
77 // Print formatted string
79 va_start(args, format);
80 ::vfprintf(stdout, format, args);
84 ::fprintf(stdout, "\n");
89 using namespace std::chrono;
91 TimerStack *stack = GetTimerStackForCurrentThread();
95 auto stop_time = steady_clock::now();
96 auto total_dur = stop_time - m_total_start;
97 auto timer_dur = total_dur - m_child_duration;
99 if (g_quiet && stack->size() <= g_display_depth) {
100 std::lock_guard<std::mutex> lock(GetFileMutex());
101 ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
102 int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "",
103 duration<double>(total_dur).count(),
104 duration<double>(timer_dur).count());
107 assert(stack->back() == this);
110 stack->back()->ChildDuration(total_dur);
112 // Keep total results for each category so we can dump results.
114 std::lock_guard<std::mutex> guard(GetCategoryMutex());
115 TimerCategoryMap &category_map = GetCategoryMap();
116 category_map[m_category] += timer_dur;
120 void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
122 /* binary function predicate:
123 * - returns whether a person is less than another person
126 CategoryMapIteratorSortCriterion(const TimerCategoryMap::const_iterator &lhs,
127 const TimerCategoryMap::const_iterator &rhs) {
128 return lhs->second > rhs->second;
131 void Timer::ResetCategoryTimes() {
132 std::lock_guard<std::mutex> guard(GetCategoryMutex());
133 TimerCategoryMap &category_map = GetCategoryMap();
134 category_map.clear();
137 void Timer::DumpCategoryTimes(Stream *s) {
138 std::lock_guard<std::mutex> guard(GetCategoryMutex());
139 TimerCategoryMap &category_map = GetCategoryMap();
140 std::vector<TimerCategoryMap::const_iterator> sorted_iterators;
141 TimerCategoryMap::const_iterator pos, end = category_map.end();
142 for (pos = category_map.begin(); pos != end; ++pos) {
143 sorted_iterators.push_back(pos);
145 std::sort(sorted_iterators.begin(), sorted_iterators.end(),
146 CategoryMapIteratorSortCriterion);
148 const size_t count = sorted_iterators.size();
149 for (size_t i = 0; i < count; ++i) {
150 const auto timer = sorted_iterators[i]->second;
151 s->Printf("%.9f sec for %s\n", std::chrono::duration<double>(timer).count(),
152 sorted_iterators[i]->first);