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 | //===----------------------------------------------------------------------===// |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/Timer.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 10 | #include "lldb/Utility/Stream.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 11 | |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 13 | #include <map> |
| 14 | #include <mutex> |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 15 | #include <utility> // for pair |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 16 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 18 | #include <assert.h> // for assert |
| 19 | #include <stdarg.h> // for va_end, va_list, va_start |
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); |
| 45 | Category *expected = g_categories; |
| 46 | do { |
| 47 | m_next = expected; |
| 48 | } while (!g_categories.compare_exchange_weak(expected, this)); |
| 49 | } |
| 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | void Timer::SetQuiet(bool value) { g_quiet = value; } |
| 52 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 53 | Timer::Timer(Timer::Category &category, const char *format, ...) |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 54 | : m_category(category), m_total_start(std::chrono::steady_clock::now()) { |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 55 | TimerStack &stack = GetTimerStackForCurrentThread(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 57 | stack.push_back(this); |
| 58 | if (g_quiet && stack.size() <= g_display_depth) { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 59 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 61 | // Indent |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 62 | ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, ""); |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 63 | // Print formatted string |
| 64 | va_list args; |
| 65 | va_start(args, format); |
| 66 | ::vfprintf(stdout, format, args); |
| 67 | va_end(args); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 69 | // Newline |
| 70 | ::fprintf(stdout, "\n"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | Timer::~Timer() { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 75 | using namespace std::chrono; |
| 76 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 77 | auto stop_time = steady_clock::now(); |
| 78 | auto total_dur = stop_time - m_total_start; |
| 79 | auto timer_dur = total_dur - m_child_duration; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 81 | TimerStack &stack = GetTimerStackForCurrentThread(); |
| 82 | if (g_quiet && stack.size() <= g_display_depth) { |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 83 | std::lock_guard<std::mutex> lock(GetFileMutex()); |
| 84 | ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 85 | int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "", |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 86 | duration<double>(total_dur).count(), |
| 87 | duration<double>(timer_dur).count()); |
| 88 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | |
Pavel Labath | f891812 | 2017-06-20 08:11:43 +0000 | [diff] [blame] | 90 | assert(stack.back() == this); |
| 91 | stack.pop_back(); |
| 92 | if (!stack.empty()) |
| 93 | stack.back()->ChildDuration(total_dur); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
Pavel Labath | 96a3c91 | 2016-11-03 09:14:09 +0000 | [diff] [blame] | 95 | // Keep total results for each category so we can dump results. |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 96 | m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
| 101 | /* binary function predicate: |
| 102 | * - returns whether a person is less than another person |
| 103 | */ |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 104 | |
| 105 | typedef std::pair<const char *, uint64_t> TimerEntry; |
| 106 | |
| 107 | static bool CategoryMapIteratorSortCriterion(const TimerEntry &lhs, |
| 108 | const TimerEntry &rhs) { |
| 109 | return lhs.second > rhs.second; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | void Timer::ResetCategoryTimes() { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 113 | for (Category *i = g_categories; i; i = i->m_next) |
| 114 | i->m_nanos.store(0, std::memory_order_release); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | void Timer::DumpCategoryTimes(Stream *s) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 118 | std::vector<TimerEntry> sorted; |
| 119 | for (Category *i = g_categories; i; i = i->m_next) { |
| 120 | uint64_t nanos = i->m_nanos.load(std::memory_order_acquire); |
| 121 | if (nanos) |
| 122 | sorted.push_back(std::make_pair(i->m_name, nanos)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | } |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 124 | if (sorted.empty()) |
| 125 | return; // Later code will break without any elements. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 127 | // Sort by time |
| 128 | std::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion); |
| 129 | |
| 130 | for (const auto &timer : sorted) |
| 131 | s->Printf("%.9f sec for %s\n", timer.second / 1000000000., timer.first); |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 132 | } |