]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/Timer.cpp
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / Timer.cpp
1 //===-- 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 <algorithm>
12 #include <map>
13 #include <mutex>
14 #include <vector>
15
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Host/Host.h"
18
19 #include <stdio.h>
20
21 using namespace lldb_private;
22
23 #define TIMER_INDENT_AMOUNT 2
24
25 namespace {
26 typedef std::map<const char *, std::chrono::nanoseconds> TimerCategoryMap;
27 typedef std::vector<Timer *> TimerStack;
28 } // end of anonymous namespace
29
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;
35 }
36
37 static std::mutex &GetCategoryMutex() {
38   static std::mutex g_category_mutex;
39   return g_category_mutex;
40 }
41
42 static TimerCategoryMap &GetCategoryMap() {
43   static TimerCategoryMap g_category_map;
44   return g_category_map;
45 }
46
47 static void ThreadSpecificCleanup(void *p) {
48   delete static_cast<TimerStack *>(p);
49 }
50
51 static TimerStack *GetTimerStackForCurrentThread() {
52   static lldb::thread_key_t g_key =
53       Host::ThreadLocalStorageCreate(ThreadSpecificCleanup);
54
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);
59   }
60   return (TimerStack *)timer_stack;
61 }
62
63 void Timer::SetQuiet(bool value) { g_quiet = value; }
64
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();
68   if (!stack)
69     return;
70
71   stack->push_back(this);
72   if (g_quiet && stack->size() <= g_display_depth) {
73     std::lock_guard<std::mutex> lock(GetFileMutex());
74
75     // Indent
76     ::fprintf(stdout, "%*s", int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "");
77     // Print formatted string
78     va_list args;
79     va_start(args, format);
80     ::vfprintf(stdout, format, args);
81     va_end(args);
82
83     // Newline
84     ::fprintf(stdout, "\n");
85   }
86 }
87
88 Timer::~Timer() {
89   using namespace std::chrono;
90
91   TimerStack *stack = GetTimerStackForCurrentThread();
92   if (!stack)
93     return;
94
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;
98
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());
105   }
106
107   assert(stack->back() == this);
108   stack->pop_back();
109   if (!stack->empty())
110     stack->back()->ChildDuration(total_dur);
111
112   // Keep total results for each category so we can dump results.
113   {
114     std::lock_guard<std::mutex> guard(GetCategoryMutex());
115     TimerCategoryMap &category_map = GetCategoryMap();
116     category_map[m_category] += timer_dur;
117   }
118 }
119
120 void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
121
122 /* binary function predicate:
123  * - returns whether a person is less than another person
124  */
125 static bool
126 CategoryMapIteratorSortCriterion(const TimerCategoryMap::const_iterator &lhs,
127                                  const TimerCategoryMap::const_iterator &rhs) {
128   return lhs->second > rhs->second;
129 }
130
131 void Timer::ResetCategoryTimes() {
132   std::lock_guard<std::mutex> guard(GetCategoryMutex());
133   TimerCategoryMap &category_map = GetCategoryMap();
134   category_map.clear();
135 }
136
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);
144   }
145   std::sort(sorted_iterators.begin(), sorted_iterators.end(),
146             CategoryMapIteratorSortCriterion);
147
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);
153   }
154 }