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/Core/Timer.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Utility/Stream.h"
13 #include "lldb/lldb-types.h" // for thread_key_t
18 #include <utility> // for pair
21 #include <assert.h> // for assert
22 #include <stdarg.h> // for va_end, va_list, va_start
25 using namespace lldb_private;
27 #define TIMER_INDENT_AMOUNT 2
30 typedef std::vector<Timer *> TimerStack;
31 static std::atomic<Timer::Category *> g_categories;
32 } // end of anonymous namespace
34 std::atomic<bool> Timer::g_quiet(true);
35 std::atomic<unsigned> Timer::g_display_depth(0);
36 static std::mutex &GetFileMutex() {
37 static std::mutex *g_file_mutex_ptr = new std::mutex();
38 return *g_file_mutex_ptr;
41 static TimerStack &GetTimerStackForCurrentThread() {
42 static thread_local TimerStack g_stack;
46 Timer::Category::Category(const char *cat) : m_name(cat) {
47 m_nanos.store(0, std::memory_order_release);
48 Category *expected = g_categories;
51 } while (!g_categories.compare_exchange_weak(expected, this));
54 void Timer::SetQuiet(bool value) { g_quiet = value; }
56 Timer::Timer(Timer::Category &category, const char *format, ...)
57 : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
58 TimerStack &stack = GetTimerStackForCurrentThread();
60 stack.push_back(this);
61 if (g_quiet && stack.size() <= g_display_depth) {
62 std::lock_guard<std::mutex> lock(GetFileMutex());
65 ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
66 // Print formatted string
68 va_start(args, format);
69 ::vfprintf(stdout, format, args);
73 ::fprintf(stdout, "\n");
78 using namespace std::chrono;
80 auto stop_time = steady_clock::now();
81 auto total_dur = stop_time - m_total_start;
82 auto timer_dur = total_dur - m_child_duration;
84 TimerStack &stack = GetTimerStackForCurrentThread();
85 if (g_quiet && stack.size() <= g_display_depth) {
86 std::lock_guard<std::mutex> lock(GetFileMutex());
87 ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
88 int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "",
89 duration<double>(total_dur).count(),
90 duration<double>(timer_dur).count());
93 assert(stack.back() == this);
96 stack.back()->ChildDuration(total_dur);
98 // Keep total results for each category so we can dump results.
99 m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
102 void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
104 /* binary function predicate:
105 * - returns whether a person is less than another person
108 typedef std::pair<const char *, uint64_t> TimerEntry;
110 static bool CategoryMapIteratorSortCriterion(const TimerEntry &lhs,
111 const TimerEntry &rhs) {
112 return lhs.second > rhs.second;
115 void Timer::ResetCategoryTimes() {
116 for (Category *i = g_categories; i; i = i->m_next)
117 i->m_nanos.store(0, std::memory_order_release);
120 void Timer::DumpCategoryTimes(Stream *s) {
121 std::vector<TimerEntry> sorted;
122 for (Category *i = g_categories; i; i = i->m_next) {
123 uint64_t nanos = i->m_nanos.load(std::memory_order_acquire);
125 sorted.push_back(std::make_pair(i->m_name, nanos));
128 return; // Later code will break without any elements.
131 std::sort(sorted.begin(), sorted.end(), CategoryMapIteratorSortCriterion);
133 for (const auto &timer : sorted)
134 s->Printf("%.9f sec for %s\n", timer.second / 1000000000., timer.first);