180814287SRaphael Isemann //===-- Timer.cpp ---------------------------------------------------------===//
238d0632eSPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
638d0632eSPavel Labath //
738d0632eSPavel Labath //===----------------------------------------------------------------------===//
838d0632eSPavel Labath #include "lldb/Utility/Timer.h"
938d0632eSPavel Labath #include "lldb/Utility/Stream.h"
106d94eeadSJonas Devlieghere #include "llvm/Support/ManagedStatic.h"
116d94eeadSJonas Devlieghere #include "llvm/Support/Signposts.h"
1238d0632eSPavel Labath 
1338d0632eSPavel Labath #include <algorithm>
1438d0632eSPavel Labath #include <map>
1538d0632eSPavel Labath #include <mutex>
16672d2c12SJonas Devlieghere #include <utility>
1738d0632eSPavel Labath #include <vector>
1838d0632eSPavel Labath 
1976e47d48SRaphael Isemann #include <cassert>
2076e47d48SRaphael Isemann #include <cinttypes>
2176e47d48SRaphael Isemann #include <cstdarg>
2276e47d48SRaphael Isemann #include <cstdio>
2338d0632eSPavel Labath 
2438d0632eSPavel Labath using namespace lldb_private;
2538d0632eSPavel Labath 
2638d0632eSPavel Labath #define TIMER_INDENT_AMOUNT 2
2738d0632eSPavel Labath 
2838d0632eSPavel Labath namespace {
2938d0632eSPavel Labath typedef std::vector<Timer *> TimerStack;
3038d0632eSPavel Labath static std::atomic<Timer::Category *> g_categories;
3138d0632eSPavel Labath } // end of anonymous namespace
3238d0632eSPavel Labath 
336d94eeadSJonas Devlieghere /// Allows llvm::Timer to emit signposts when supported.
346d94eeadSJonas Devlieghere static llvm::ManagedStatic<llvm::SignpostEmitter> Signposts;
356d94eeadSJonas Devlieghere 
3638d0632eSPavel Labath std::atomic<bool> Timer::g_quiet(true);
3738d0632eSPavel Labath std::atomic<unsigned> Timer::g_display_depth(0);
GetFileMutex()3838d0632eSPavel Labath static std::mutex &GetFileMutex() {
3938d0632eSPavel Labath   static std::mutex *g_file_mutex_ptr = new std::mutex();
4038d0632eSPavel Labath   return *g_file_mutex_ptr;
4138d0632eSPavel Labath }
4238d0632eSPavel Labath 
GetTimerStackForCurrentThread()4338d0632eSPavel Labath static TimerStack &GetTimerStackForCurrentThread() {
4438d0632eSPavel Labath   static thread_local TimerStack g_stack;
4538d0632eSPavel Labath   return g_stack;
4638d0632eSPavel Labath }
4738d0632eSPavel Labath 
Category(const char * cat)4838d0632eSPavel Labath Timer::Category::Category(const char *cat) : m_name(cat) {
4938d0632eSPavel Labath   m_nanos.store(0, std::memory_order_release);
5078337420SAntonio Afonso   m_nanos_total.store(0, std::memory_order_release);
5178337420SAntonio Afonso   m_count.store(0, std::memory_order_release);
5238d0632eSPavel Labath   Category *expected = g_categories;
5338d0632eSPavel Labath   do {
5438d0632eSPavel Labath     m_next = expected;
5538d0632eSPavel Labath   } while (!g_categories.compare_exchange_weak(expected, this));
5638d0632eSPavel Labath }
5738d0632eSPavel Labath 
SetQuiet(bool value)5838d0632eSPavel Labath void Timer::SetQuiet(bool value) { g_quiet = value; }
5938d0632eSPavel Labath 
Timer(Timer::Category & category,const char * format,...)6038d0632eSPavel Labath Timer::Timer(Timer::Category &category, const char *format, ...)
6138d0632eSPavel Labath     : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
62070315d0SJonas Devlieghere   Signposts->startInterval(this, m_category.GetName());
6338d0632eSPavel Labath   TimerStack &stack = GetTimerStackForCurrentThread();
6438d0632eSPavel Labath 
6538d0632eSPavel Labath   stack.push_back(this);
6658ee14e2SDave Lee   if (!g_quiet && stack.size() <= g_display_depth) {
6738d0632eSPavel Labath     std::lock_guard<std::mutex> lock(GetFileMutex());
6838d0632eSPavel Labath 
6938d0632eSPavel Labath     // Indent
7038d0632eSPavel Labath     ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
7138d0632eSPavel Labath     // Print formatted string
7238d0632eSPavel Labath     va_list args;
7338d0632eSPavel Labath     va_start(args, format);
7438d0632eSPavel Labath     ::vfprintf(stdout, format, args);
7538d0632eSPavel Labath     va_end(args);
7638d0632eSPavel Labath 
7738d0632eSPavel Labath     // Newline
7838d0632eSPavel Labath     ::fprintf(stdout, "\n");
7938d0632eSPavel Labath   }
8038d0632eSPavel Labath }
8138d0632eSPavel Labath 
~Timer()8238d0632eSPavel Labath Timer::~Timer() {
8338d0632eSPavel Labath   using namespace std::chrono;
8438d0632eSPavel Labath 
8538d0632eSPavel Labath   auto stop_time = steady_clock::now();
8638d0632eSPavel Labath   auto total_dur = stop_time - m_total_start;
8738d0632eSPavel Labath   auto timer_dur = total_dur - m_child_duration;
8838d0632eSPavel Labath 
89070315d0SJonas Devlieghere   Signposts->endInterval(this, m_category.GetName());
90070315d0SJonas Devlieghere 
9138d0632eSPavel Labath   TimerStack &stack = GetTimerStackForCurrentThread();
9258ee14e2SDave Lee   if (!g_quiet && stack.size() <= g_display_depth) {
9338d0632eSPavel Labath     std::lock_guard<std::mutex> lock(GetFileMutex());
9438d0632eSPavel Labath     ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
9538d0632eSPavel Labath               int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "",
9638d0632eSPavel Labath               duration<double>(total_dur).count(),
9738d0632eSPavel Labath               duration<double>(timer_dur).count());
9838d0632eSPavel Labath   }
9938d0632eSPavel Labath 
10038d0632eSPavel Labath   assert(stack.back() == this);
10138d0632eSPavel Labath   stack.pop_back();
10238d0632eSPavel Labath   if (!stack.empty())
10338d0632eSPavel Labath     stack.back()->ChildDuration(total_dur);
10438d0632eSPavel Labath 
10538d0632eSPavel Labath   // Keep total results for each category so we can dump results.
10638d0632eSPavel Labath   m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
10778337420SAntonio Afonso   m_category.m_nanos_total += std::chrono::nanoseconds(total_dur).count();
10878337420SAntonio Afonso   m_category.m_count++;
10938d0632eSPavel Labath }
11038d0632eSPavel Labath 
SetDisplayDepth(uint32_t depth)11138d0632eSPavel Labath void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
11238d0632eSPavel Labath 
11338d0632eSPavel Labath /* binary function predicate:
11438d0632eSPavel Labath  * - returns whether a person is less than another person
11538d0632eSPavel Labath  */
11678337420SAntonio Afonso namespace {
11778337420SAntonio Afonso struct Stats {
11878337420SAntonio Afonso   const char *name;
11978337420SAntonio Afonso   uint64_t nanos;
12078337420SAntonio Afonso   uint64_t nanos_total;
12178337420SAntonio Afonso   uint64_t count;
12278337420SAntonio Afonso };
12378337420SAntonio Afonso } // namespace
12438d0632eSPavel Labath 
CategoryMapIteratorSortCriterion(const Stats & lhs,const Stats & rhs)12578337420SAntonio Afonso static bool CategoryMapIteratorSortCriterion(const Stats &lhs,
12678337420SAntonio Afonso                                              const Stats &rhs) {
12778337420SAntonio Afonso   return lhs.nanos > rhs.nanos;
12838d0632eSPavel Labath }
12938d0632eSPavel Labath 
ResetCategoryTimes()13038d0632eSPavel Labath void Timer::ResetCategoryTimes() {
13178337420SAntonio Afonso   for (Category *i = g_categories; i; i = i->m_next) {
13238d0632eSPavel Labath     i->m_nanos.store(0, std::memory_order_release);
13378337420SAntonio Afonso     i->m_nanos_total.store(0, std::memory_order_release);
13478337420SAntonio Afonso     i->m_count.store(0, std::memory_order_release);
13578337420SAntonio Afonso   }
13638d0632eSPavel Labath }
13738d0632eSPavel Labath 
DumpCategoryTimes(Stream * s)13838d0632eSPavel Labath void Timer::DumpCategoryTimes(Stream *s) {
13978337420SAntonio Afonso   std::vector<Stats> sorted;
14038d0632eSPavel Labath   for (Category *i = g_categories; i; i = i->m_next) {
14138d0632eSPavel Labath     uint64_t nanos = i->m_nanos.load(std::memory_order_acquire);
14278337420SAntonio Afonso     if (nanos) {
14378337420SAntonio Afonso       uint64_t nanos_total = i->m_nanos_total.load(std::memory_order_acquire);
14478337420SAntonio Afonso       uint64_t count = i->m_count.load(std::memory_order_acquire);
14578337420SAntonio Afonso       Stats stats{i->m_name, nanos, nanos_total, count};
14678337420SAntonio Afonso       sorted.push_back(stats);
14778337420SAntonio Afonso     }
14838d0632eSPavel Labath   }
14938d0632eSPavel Labath   if (sorted.empty())
15038d0632eSPavel Labath     return; // Later code will break without any elements.
15138d0632eSPavel Labath 
15238d0632eSPavel Labath   // Sort by time
153*cd9a5cfdSDmitri Gribenko   llvm::sort(sorted, CategoryMapIteratorSortCriterion);
15438d0632eSPavel Labath 
15578337420SAntonio Afonso   for (const auto &stats : sorted)
156c8f2efe0SRichard Trieu     s->Printf("%.9f sec (total: %.3fs; child: %.3fs; count: %" PRIu64
157c8f2efe0SRichard Trieu               ") for %s\n",
15878337420SAntonio Afonso               stats.nanos / 1000000000., stats.nanos_total / 1000000000.,
15978337420SAntonio Afonso               (stats.nanos_total - stats.nanos) / 1000000000., stats.count,
16078337420SAntonio Afonso               stats.name);
16138d0632eSPavel Labath }
162