Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Host.h" |
| 12 | #include "lldb/Utility/Stream.h" |
| 13 | |
| 14 | #include "llvm/Support/ThreadLocal.h" |
| 15 | |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <mutex> |
| 19 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | using namespace lldb_private; |
| 24 | |
| 25 | #define TIMER_INDENT_AMOUNT 2 |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | namespace { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 28 | typedef std::map<const char *, std::chrono::nanoseconds> TimerCategoryMap; |
| 29 | typedef std::vector<Timer *> TimerStack; |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 30 | } // end of anonymous namespace |
| 31 | |
Tamas Berghammer | 5ee6b7f | 2015-10-23 10:53:31 +0000 | [diff] [blame] | 32 | std::atomic<bool> Timer::g_quiet(true); |
| 33 | std::atomic<unsigned> Timer::g_display_depth(0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | static std::mutex &GetFileMutex() { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 35 | static std::mutex *g_file_mutex_ptr = new std::mutex(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | return *g_file_mutex_ptr; |
Greg Clayton | 8e1232a | 2016-03-24 21:46:47 +0000 | [diff] [blame] | 37 | } |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | static std::mutex &GetCategoryMutex() { |
| 40 | static std::mutex g_category_mutex; |
| 41 | return g_category_mutex; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | static TimerCategoryMap &GetCategoryMap() { |
| 45 | static TimerCategoryMap g_category_map; |
| 46 | return g_category_map; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | static void ThreadSpecificCleanup(void *p) { |
| 50 | delete static_cast<TimerStack *>(p); |
Pavel Labath | 061140c | 2016-02-01 13:29:41 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | static TimerStack *GetTimerStackForCurrentThread() { |
| 54 | static lldb::thread_key_t g_key = |
| 55 | Host::ThreadLocalStorageCreate(ThreadSpecificCleanup); |
Pavel Labath | 061140c | 2016-02-01 13:29:41 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | void *timer_stack = Host::ThreadLocalStorageGet(g_key); |
| 58 | if (timer_stack == NULL) { |
| 59 | Host::ThreadLocalStorageSet(g_key, new TimerStack); |
| 60 | timer_stack = Host::ThreadLocalStorageGet(g_key); |
| 61 | } |
| 62 | return (TimerStack *)timer_stack; |
| 63 | } |
| 64 | |
| 65 | void Timer::SetQuiet(bool value) { g_quiet = value; } |
| 66 | |
| 67 | Timer::Timer(const char *category, const char *format, ...) |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 68 | : m_category(category), m_total_start(std::chrono::steady_clock::now()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | TimerStack *stack = GetTimerStackForCurrentThread(); |
| 70 | if (!stack) |
| 71 | return; |
| 72 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 73 | stack->push_back(this); |
| 74 | if (g_quiet && stack->size() <= g_display_depth) { |
| 75 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 77 | // Indent |
| 78 | ::fprintf(stdout, "%*s", int(stack->size() - 1) * TIMER_INDENT_AMOUNT, ""); |
| 79 | // Print formatted string |
| 80 | va_list args; |
| 81 | va_start(args, format); |
| 82 | ::vfprintf(stdout, format, args); |
| 83 | va_end(args); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 85 | // Newline |
| 86 | ::fprintf(stdout, "\n"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | Timer::~Timer() { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 91 | using namespace std::chrono; |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | TimerStack *stack = GetTimerStackForCurrentThread(); |
| 94 | if (!stack) |
| 95 | return; |
Jim Ingham | f7f4f50 | 2010-11-04 23:19:21 +0000 | [diff] [blame] | 96 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 97 | auto stop_time = steady_clock::now(); |
| 98 | auto total_dur = stop_time - m_total_start; |
| 99 | auto timer_dur = total_dur - m_child_duration; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 101 | if (g_quiet && stack->size() <= g_display_depth) { |
| 102 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
| 103 | ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", |
| 104 | int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "", |
| 105 | duration<double>(total_dur).count(), |
| 106 | duration<double>(timer_dur).count()); |
| 107 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 109 | assert(stack->back() == this); |
| 110 | stack->pop_back(); |
| 111 | if (!stack->empty()) |
| 112 | stack->back()->ChildDuration(total_dur); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 114 | // Keep total results for each category so we can dump results. |
| 115 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | std::lock_guard<std::mutex> guard(GetCategoryMutex()); |
| 117 | TimerCategoryMap &category_map = GetCategoryMap(); |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 118 | category_map[m_category] += timer_dur; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | |
| 124 | /* binary function predicate: |
| 125 | * - returns whether a person is less than another person |
| 126 | */ |
| 127 | static bool |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | CategoryMapIteratorSortCriterion(const TimerCategoryMap::const_iterator &lhs, |
| 129 | const TimerCategoryMap::const_iterator &rhs) { |
| 130 | return lhs->second > rhs->second; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | void Timer::ResetCategoryTimes() { |
| 134 | std::lock_guard<std::mutex> guard(GetCategoryMutex()); |
| 135 | TimerCategoryMap &category_map = GetCategoryMap(); |
| 136 | category_map.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | void Timer::DumpCategoryTimes(Stream *s) { |
| 140 | std::lock_guard<std::mutex> guard(GetCategoryMutex()); |
| 141 | TimerCategoryMap &category_map = GetCategoryMap(); |
| 142 | std::vector<TimerCategoryMap::const_iterator> sorted_iterators; |
| 143 | TimerCategoryMap::const_iterator pos, end = category_map.end(); |
| 144 | for (pos = category_map.begin(); pos != end; ++pos) { |
| 145 | sorted_iterators.push_back(pos); |
| 146 | } |
| 147 | std::sort(sorted_iterators.begin(), sorted_iterators.end(), |
| 148 | CategoryMapIteratorSortCriterion); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | const size_t count = sorted_iterators.size(); |
| 151 | for (size_t i = 0; i < count; ++i) { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 152 | const auto timer = sorted_iterators[i]->second; |
| 153 | s->Printf("%.9f sec for %s\n", std::chrono::duration<double>(timer).count(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | sorted_iterators[i]->first); |
| 155 | } |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 156 | } |