15ffd83dbSDimitry Andric //===-- Timer.cpp ---------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
90b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
10e8d8bef9SDimitry Andric #include "llvm/Support/ManagedStatic.h"
11e8d8bef9SDimitry Andric #include "llvm/Support/Signposts.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include <algorithm>
140b57cec5SDimitry Andric #include <map>
150b57cec5SDimitry Andric #include <mutex>
160b57cec5SDimitry Andric #include <utility>
170b57cec5SDimitry Andric #include <vector>
180b57cec5SDimitry Andric
19fe6060f1SDimitry Andric #include <cassert>
20fe6060f1SDimitry Andric #include <cinttypes>
21fe6060f1SDimitry Andric #include <cstdarg>
22fe6060f1SDimitry Andric #include <cstdio>
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric using namespace lldb_private;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric #define TIMER_INDENT_AMOUNT 2
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric namespace {
290b57cec5SDimitry Andric typedef std::vector<Timer *> TimerStack;
300b57cec5SDimitry Andric static std::atomic<Timer::Category *> g_categories;
310b57cec5SDimitry Andric } // end of anonymous namespace
320b57cec5SDimitry Andric
33e8d8bef9SDimitry Andric /// Allows llvm::Timer to emit signposts when supported.
34e8d8bef9SDimitry Andric static llvm::ManagedStatic<llvm::SignpostEmitter> Signposts;
35e8d8bef9SDimitry Andric
360b57cec5SDimitry Andric std::atomic<bool> Timer::g_quiet(true);
370b57cec5SDimitry Andric std::atomic<unsigned> Timer::g_display_depth(0);
GetFileMutex()380b57cec5SDimitry Andric static std::mutex &GetFileMutex() {
390b57cec5SDimitry Andric static std::mutex *g_file_mutex_ptr = new std::mutex();
400b57cec5SDimitry Andric return *g_file_mutex_ptr;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
GetTimerStackForCurrentThread()430b57cec5SDimitry Andric static TimerStack &GetTimerStackForCurrentThread() {
440b57cec5SDimitry Andric static thread_local TimerStack g_stack;
450b57cec5SDimitry Andric return g_stack;
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
Category(const char * cat)480b57cec5SDimitry Andric Timer::Category::Category(const char *cat) : m_name(cat) {
490b57cec5SDimitry Andric m_nanos.store(0, std::memory_order_release);
500b57cec5SDimitry Andric m_nanos_total.store(0, std::memory_order_release);
510b57cec5SDimitry Andric m_count.store(0, std::memory_order_release);
520b57cec5SDimitry Andric Category *expected = g_categories;
530b57cec5SDimitry Andric do {
540b57cec5SDimitry Andric m_next = expected;
550b57cec5SDimitry Andric } while (!g_categories.compare_exchange_weak(expected, this));
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
SetQuiet(bool value)580b57cec5SDimitry Andric void Timer::SetQuiet(bool value) { g_quiet = value; }
590b57cec5SDimitry Andric
Timer(Timer::Category & category,const char * format,...)600b57cec5SDimitry Andric Timer::Timer(Timer::Category &category, const char *format, ...)
610b57cec5SDimitry Andric : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
62349cc55cSDimitry Andric Signposts->startInterval(this, m_category.GetName());
630b57cec5SDimitry Andric TimerStack &stack = GetTimerStackForCurrentThread();
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric stack.push_back(this);
6604eeddc0SDimitry Andric if (!g_quiet && stack.size() <= g_display_depth) {
670b57cec5SDimitry Andric std::lock_guard<std::mutex> lock(GetFileMutex());
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Indent
700b57cec5SDimitry Andric ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
710b57cec5SDimitry Andric // Print formatted string
720b57cec5SDimitry Andric va_list args;
730b57cec5SDimitry Andric va_start(args, format);
740b57cec5SDimitry Andric ::vfprintf(stdout, format, args);
750b57cec5SDimitry Andric va_end(args);
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // Newline
780b57cec5SDimitry Andric ::fprintf(stdout, "\n");
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric
~Timer()820b57cec5SDimitry Andric Timer::~Timer() {
830b57cec5SDimitry Andric using namespace std::chrono;
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric auto stop_time = steady_clock::now();
860b57cec5SDimitry Andric auto total_dur = stop_time - m_total_start;
870b57cec5SDimitry Andric auto timer_dur = total_dur - m_child_duration;
880b57cec5SDimitry Andric
89349cc55cSDimitry Andric Signposts->endInterval(this, m_category.GetName());
90349cc55cSDimitry Andric
910b57cec5SDimitry Andric TimerStack &stack = GetTimerStackForCurrentThread();
9204eeddc0SDimitry Andric if (!g_quiet && stack.size() <= g_display_depth) {
930b57cec5SDimitry Andric std::lock_guard<std::mutex> lock(GetFileMutex());
940b57cec5SDimitry Andric ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
950b57cec5SDimitry Andric int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "",
960b57cec5SDimitry Andric duration<double>(total_dur).count(),
970b57cec5SDimitry Andric duration<double>(timer_dur).count());
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric assert(stack.back() == this);
1010b57cec5SDimitry Andric stack.pop_back();
1020b57cec5SDimitry Andric if (!stack.empty())
1030b57cec5SDimitry Andric stack.back()->ChildDuration(total_dur);
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric // Keep total results for each category so we can dump results.
1060b57cec5SDimitry Andric m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
1070b57cec5SDimitry Andric m_category.m_nanos_total += std::chrono::nanoseconds(total_dur).count();
1080b57cec5SDimitry Andric m_category.m_count++;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric
SetDisplayDepth(uint32_t depth)1110b57cec5SDimitry Andric void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric /* binary function predicate:
1140b57cec5SDimitry Andric * - returns whether a person is less than another person
1150b57cec5SDimitry Andric */
1160b57cec5SDimitry Andric namespace {
1170b57cec5SDimitry Andric struct Stats {
1180b57cec5SDimitry Andric const char *name;
1190b57cec5SDimitry Andric uint64_t nanos;
1200b57cec5SDimitry Andric uint64_t nanos_total;
1210b57cec5SDimitry Andric uint64_t count;
1220b57cec5SDimitry Andric };
1230b57cec5SDimitry Andric } // namespace
1240b57cec5SDimitry Andric
CategoryMapIteratorSortCriterion(const Stats & lhs,const Stats & rhs)1250b57cec5SDimitry Andric static bool CategoryMapIteratorSortCriterion(const Stats &lhs,
1260b57cec5SDimitry Andric const Stats &rhs) {
1270b57cec5SDimitry Andric return lhs.nanos > rhs.nanos;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric
ResetCategoryTimes()1300b57cec5SDimitry Andric void Timer::ResetCategoryTimes() {
1310b57cec5SDimitry Andric for (Category *i = g_categories; i; i = i->m_next) {
1320b57cec5SDimitry Andric i->m_nanos.store(0, std::memory_order_release);
1330b57cec5SDimitry Andric i->m_nanos_total.store(0, std::memory_order_release);
1340b57cec5SDimitry Andric i->m_count.store(0, std::memory_order_release);
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric
DumpCategoryTimes(Stream & s)138*fe013be4SDimitry Andric void Timer::DumpCategoryTimes(Stream &s) {
1390b57cec5SDimitry Andric std::vector<Stats> sorted;
1400b57cec5SDimitry Andric for (Category *i = g_categories; i; i = i->m_next) {
1410b57cec5SDimitry Andric uint64_t nanos = i->m_nanos.load(std::memory_order_acquire);
1420b57cec5SDimitry Andric if (nanos) {
1430b57cec5SDimitry Andric uint64_t nanos_total = i->m_nanos_total.load(std::memory_order_acquire);
1440b57cec5SDimitry Andric uint64_t count = i->m_count.load(std::memory_order_acquire);
1450b57cec5SDimitry Andric Stats stats{i->m_name, nanos, nanos_total, count};
1460b57cec5SDimitry Andric sorted.push_back(stats);
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric if (sorted.empty())
1500b57cec5SDimitry Andric return; // Later code will break without any elements.
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric // Sort by time
153fcaf7f86SDimitry Andric llvm::sort(sorted, CategoryMapIteratorSortCriterion);
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric for (const auto &stats : sorted)
156*fe013be4SDimitry Andric s.Printf("%.9f sec (total: %.3fs; child: %.3fs; count: %" PRIu64
1570b57cec5SDimitry Andric ") for %s\n",
1580b57cec5SDimitry Andric stats.nanos / 1000000000., stats.nanos_total / 1000000000.,
1590b57cec5SDimitry Andric (stats.nanos_total - stats.nanos) / 1000000000., stats.count,
1600b57cec5SDimitry Andric stats.name);
1610b57cec5SDimitry Andric }
162