1 //===-- Timer.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/Timer.h"
10 #include "lldb/Utility/Stream.h"
15 #include <utility> // for pair
18 #include <assert.h> // for assert
19 #include <stdarg.h> // for va_end, va_list, va_start
22 using namespace lldb_private;
24 #define TIMER_INDENT_AMOUNT 2
27 typedef std::vector<Timer *> TimerStack;
28 static std::atomic<Timer::Category *> g_categories;
29 } // end of anonymous namespace
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;
38 static TimerStack &GetTimerStackForCurrentThread() {
39 static thread_local TimerStack g_stack;
43 Timer::Category::Category(const char *cat) : m_name(cat) {
44 m_nanos.store(0, std::memory_order_release);
45 Category *expected = g_categories;
48 } while (!g_categories.compare_exchange_weak(expected, this));
51 void Timer::SetQuiet(bool value) { g_quiet = value; }
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();
57 stack.push_back(this);
58 if (g_quiet && stack.size() <= g_display_depth) {
59 std::lock_guard<std::mutex> lock(GetFileMutex());
62 ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
63 // Print formatted string
65 va_start(args, format);
66 ::vfprintf(stdout, format, args);
70 ::fprintf(stdout, "\n");
75 using namespace std::chrono;
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;
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());
90 assert(stack.back() == this);
93 stack.back()->ChildDuration(total_dur);
95 // Keep total results for each category so we can dump results.
96 m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
99 void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
101 /* binary function predicate:
102 * - returns whether a person is less than another person
105 typedef std::pair<const char *, uint64_t> TimerEntry;
107 static bool CategoryMapIteratorSortCriterion(const TimerEntry &lhs,
108 const TimerEntry &rhs) {
109 return lhs.second > rhs.second;
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);
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);
122 sorted.push_back(std::make_pair(i->m_name, nanos));
125 return; // Later code will break without any elements.
128 std::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion);
130 for (const auto &timer : sorted)
131 s->Printf("%.9f sec for %s\n", timer.second / 1000000000., timer.first);