Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- Timer.cpp ---------------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 8 | #include "lldb/Utility/Timer.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/Stream.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 10 | |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 11 | #include <algorithm> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 12 | #include <map> |
| 13 | #include <mutex> |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 14 | #include <utility> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 15 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 17 | #include <assert.h> |
Reid Kleckner | 34312ed | 2020-02-26 10:34:12 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 19 | #include <stdarg.h> |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | using namespace lldb_private; |
| 23 | |
| 24 | #define TIMER_INDENT_AMOUNT 2 |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | namespace { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 27 | typedef std::vector<Timer *> TimerStack; |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 28 | static std::atomic<Timer::Category *> g_categories; |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 29 | } // end of anonymous namespace |
| 30 | |
Tamas Berghammer | 5ee6b7f | 2015-10-23 10:53:31 +0000 | [diff] [blame] | 31 | std::atomic<bool> Timer::g_quiet(true); |
| 32 | std::atomic<unsigned> Timer::g_display_depth(0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | static std::mutex &GetFileMutex() { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 34 | static std::mutex *g_file_mutex_ptr = new std::mutex(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | return *g_file_mutex_ptr; |
Greg Clayton | 8e1232a | 2016-03-24 21:46:47 +0000 | [diff] [blame] | 36 | } |
Tamas Berghammer | d779da9 | 2015-10-23 10:34:29 +0000 | [diff] [blame] | 37 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 38 | static TimerStack &GetTimerStackForCurrentThread() { |
| 39 | static thread_local TimerStack g_stack; |
| 40 | return g_stack; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 43 | Timer::Category::Category(const char *cat) : m_name(cat) { |
| 44 | m_nanos.store(0, std::memory_order_release); |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 45 | m_nanos_total.store(0, std::memory_order_release); |
| 46 | m_count.store(0, std::memory_order_release); |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 47 | Category *expected = g_categories; |
| 48 | do { |
| 49 | m_next = expected; |
| 50 | } while (!g_categories.compare_exchange_weak(expected, this)); |
| 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | void Timer::SetQuiet(bool value) { g_quiet = value; } |
| 54 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 55 | Timer::Timer(Timer::Category &category, const char *format, ...) |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 56 | : m_category(category), m_total_start(std::chrono::steady_clock::now()) { |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 57 | TimerStack &stack = GetTimerStackForCurrentThread(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 59 | stack.push_back(this); |
| 60 | if (g_quiet && stack.size() <= g_display_depth) { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 61 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 63 | // Indent |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 64 | ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, ""); |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 65 | // Print formatted string |
| 66 | va_list args; |
| 67 | va_start(args, format); |
| 68 | ::vfprintf(stdout, format, args); |
| 69 | va_end(args); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 71 | // Newline |
| 72 | ::fprintf(stdout, "\n"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | Timer::~Timer() { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 77 | using namespace std::chrono; |
| 78 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 79 | auto stop_time = steady_clock::now(); |
| 80 | auto total_dur = stop_time - m_total_start; |
| 81 | auto timer_dur = total_dur - m_child_duration; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 83 | TimerStack &stack = GetTimerStackForCurrentThread(); |
| 84 | if (g_quiet && stack.size() <= g_display_depth) { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 85 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
| 86 | ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 87 | int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "", |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 88 | duration<double>(total_dur).count(), |
| 89 | duration<double>(timer_dur).count()); |
| 90 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 92 | assert(stack.back() == this); |
| 93 | stack.pop_back(); |
| 94 | if (!stack.empty()) |
| 95 | stack.back()->ChildDuration(total_dur); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 97 | // Keep total results for each category so we can dump results. |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 98 | m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count(); |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 99 | m_category.m_nanos_total += std::chrono::nanoseconds(total_dur).count(); |
| 100 | m_category.m_count++; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
| 105 | /* binary function predicate: |
| 106 | * - returns whether a person is less than another person |
| 107 | */ |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 108 | namespace { |
| 109 | struct Stats { |
| 110 | const char *name; |
| 111 | uint64_t nanos; |
| 112 | uint64_t nanos_total; |
| 113 | uint64_t count; |
| 114 | }; |
| 115 | } // namespace |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 116 | |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 117 | static bool CategoryMapIteratorSortCriterion(const Stats &lhs, |
| 118 | const Stats &rhs) { |
| 119 | return lhs.nanos > rhs.nanos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | void Timer::ResetCategoryTimes() { |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 123 | for (Category *i = g_categories; i; i = i->m_next) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 124 | i->m_nanos.store(0, std::memory_order_release); |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 125 | i->m_nanos_total.store(0, std::memory_order_release); |
| 126 | i->m_count.store(0, std::memory_order_release); |
| 127 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | void Timer::DumpCategoryTimes(Stream *s) { |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 131 | std::vector<Stats> sorted; |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 132 | for (Category *i = g_categories; i; i = i->m_next) { |
| 133 | uint64_t nanos = i->m_nanos.load(std::memory_order_acquire); |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 134 | if (nanos) { |
| 135 | uint64_t nanos_total = i->m_nanos_total.load(std::memory_order_acquire); |
| 136 | uint64_t count = i->m_count.load(std::memory_order_acquire); |
| 137 | Stats stats{i->m_name, nanos, nanos_total, count}; |
| 138 | sorted.push_back(stats); |
| 139 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | } |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 141 | if (sorted.empty()) |
| 142 | return; // Later code will break without any elements. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 144 | // Sort by time |
Jonas Devlieghere | 9bbba27 | 2019-01-08 23:25:06 +0000 | [diff] [blame] | 145 | llvm::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion); |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 146 | |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 147 | for (const auto &stats : sorted) |
Richard Trieu | c8f2efe | 2019-05-29 21:25:15 +0000 | [diff] [blame] | 148 | s->Printf("%.9f sec (total: %.3fs; child: %.3fs; count: %" PRIu64 |
| 149 | ") for %s\n", |
Antonio Afonso | 7833742 | 2019-05-29 16:31:32 +0000 | [diff] [blame] | 150 | stats.nanos / 1000000000., stats.nanos_total / 1000000000., |
| 151 | (stats.nanos_total - stats.nanos) / 1000000000., stats.count, |
| 152 | stats.name); |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 153 | } |