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/Utility/Timer.h" 10 #include "lldb/Utility/Stream.h" 11 12 #include <algorithm> 13 #include <map> 14 #include <mutex> 15 #include <utility> // for pair 16 #include <vector> 17 18 #include <assert.h> // for assert 19 #include <stdarg.h> // for va_end, va_list, va_start 20 #include <stdio.h> 21 22 using namespace lldb_private; 23 24 #define TIMER_INDENT_AMOUNT 2 25 26 namespace { 27 typedef std::vector<Timer *> TimerStack; 28 static std::atomic<Timer::Category *> g_categories; 29 } // end of anonymous namespace 30 31 std::atomic<bool> Timer::g_quiet(true); 32 std::atomic<unsigned> Timer::g_display_depth(0); 33 static std::mutex &GetFileMutex() { 34 static std::mutex *g_file_mutex_ptr = new std::mutex(); 35 return *g_file_mutex_ptr; 36 } 37 38 static TimerStack &GetTimerStackForCurrentThread() { 39 static thread_local TimerStack g_stack; 40 return g_stack; 41 } 42 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 51 void Timer::SetQuiet(bool value) { g_quiet = value; } 52 53 Timer::Timer(Timer::Category &category, const char *format, ...) 54 : m_category(category), m_total_start(std::chrono::steady_clock::now()) { 55 TimerStack &stack = GetTimerStackForCurrentThread(); 56 57 stack.push_back(this); 58 if (g_quiet && stack.size() <= g_display_depth) { 59 std::lock_guard<std::mutex> lock(GetFileMutex()); 60 61 // Indent 62 ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, ""); 63 // Print formatted string 64 va_list args; 65 va_start(args, format); 66 ::vfprintf(stdout, format, args); 67 va_end(args); 68 69 // Newline 70 ::fprintf(stdout, "\n"); 71 } 72 } 73 74 Timer::~Timer() { 75 using namespace std::chrono; 76 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; 80 81 TimerStack &stack = GetTimerStackForCurrentThread(); 82 if (g_quiet && stack.size() <= g_display_depth) { 83 std::lock_guard<std::mutex> lock(GetFileMutex()); 84 ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", 85 int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "", 86 duration<double>(total_dur).count(), 87 duration<double>(timer_dur).count()); 88 } 89 90 assert(stack.back() == this); 91 stack.pop_back(); 92 if (!stack.empty()) 93 stack.back()->ChildDuration(total_dur); 94 95 // Keep total results for each category so we can dump results. 96 m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count(); 97 } 98 99 void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; } 100 101 /* binary function predicate: 102 * - returns whether a person is less than another person 103 */ 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; 110 } 111 112 void Timer::ResetCategoryTimes() { 113 for (Category *i = g_categories; i; i = i->m_next) 114 i->m_nanos.store(0, std::memory_order_release); 115 } 116 117 void Timer::DumpCategoryTimes(Stream *s) { 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)); 123 } 124 if (sorted.empty()) 125 return; // Later code will break without any elements. 126 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); 132 } 133