LLDB mainline
Timer.cpp
Go to the documentation of this file.
1//===-- Timer.cpp ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
11#include "llvm/Support/ManagedStatic.h"
12#include "llvm/Support/Signposts.h"
13
14#include <algorithm>
15#include <map>
16#include <mutex>
17#include <utility>
18#include <vector>
19
20#include <cassert>
21#include <cinttypes>
22#include <cstdarg>
23#include <cstdio>
24
25using namespace lldb_private;
26
27#define TIMER_INDENT_AMOUNT 2
28
29namespace {
30typedef std::vector<Timer *> TimerStack;
31static std::atomic<Timer::Category *> g_categories;
32} // end of anonymous namespace
33
34/// Allows llvm::Timer to emit signposts when supported.
35static llvm::ManagedStatic<llvm::SignpostEmitter> Signposts;
36
37std::atomic<bool> Timer::g_quiet(true);
38std::atomic<unsigned> Timer::g_display_depth(0);
39static std::mutex &GetFileMutex() {
40 static std::mutex *g_file_mutex_ptr = new std::mutex();
41 return *g_file_mutex_ptr;
42}
43
44static TimerStack &GetTimerStackForCurrentThread() {
45 static thread_local TimerStack g_stack;
46 return g_stack;
47}
48
49Timer::Category::Category(const char *cat) : m_name(cat) {
50 m_nanos.store(0, std::memory_order_release);
51 m_nanos_total.store(0, std::memory_order_release);
52 m_count.store(0, std::memory_order_release);
53 Category *expected = g_categories;
54 do {
55 m_next = expected;
56 } while (!g_categories.compare_exchange_weak(expected, this));
57}
58
59void Timer::SetQuiet(bool value) { g_quiet = value; }
60
61void Timer::Start(llvm::function_ref<std::string()> start_message_func) {
62 Signposts->startInterval(this, m_category.GetName());
63 TimerStack &stack = GetTimerStackForCurrentThread();
64
65 stack.push_back(this);
66 if (!g_quiet && stack.size() <= g_display_depth) {
67 std::lock_guard<std::mutex> lock(GetFileMutex());
68
69 // Indent
70 ::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
71 // Print formatted string
72 const std::string start_message = start_message_func();
73 ::fprintf(stdout, "%s", start_message.c_str());
74 // Newline
75 ::fprintf(stdout, "\n");
76 }
77}
78
79Timer::Timer(Timer::Category &category, const char *format, ...)
80 : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
81 va_list args;
82 va_start(args, format);
83 Start([&]() -> std::string {
84 StreamString sstr;
85 sstr.PrintfVarArg(format, args);
86
87 return sstr.GetString().str();
88 });
89 va_end(args);
90}
91
92Timer::Timer(Timer::Category &category, const llvm::formatv_object_base &obj)
93 : m_category(category), m_total_start(std::chrono::steady_clock::now()) {
94 Start([&]() -> std::string { return obj.str(); });
95}
96
98 using namespace std::chrono;
99
100 auto stop_time = steady_clock::now();
101 auto total_dur = stop_time - m_total_start;
102 auto timer_dur = total_dur - m_child_duration;
103
104 Signposts->endInterval(this, m_category.GetName());
105
106 TimerStack &stack = GetTimerStackForCurrentThread();
107 if (!g_quiet && stack.size() <= g_display_depth) {
108 std::lock_guard<std::mutex> lock(GetFileMutex());
109 ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
110 int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "",
111 duration<double>(total_dur).count(),
112 duration<double>(timer_dur).count());
113 }
114
115 assert(stack.back() == this);
116 stack.pop_back();
117 if (!stack.empty())
118 stack.back()->ChildDuration(total_dur);
119
120 // Keep total results for each category so we can dump results.
121 m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
122 m_category.m_nanos_total += std::chrono::nanoseconds(total_dur).count();
123 m_category.m_count++;
124}
125
126void Timer::SetDisplayDepth(uint32_t depth) { g_display_depth = depth; }
127
128/* binary function predicate:
129 * - returns whether a person is less than another person
130 */
131namespace {
132struct Stats {
133 const char *name;
134 uint64_t nanos;
135 uint64_t nanos_total;
136 uint64_t count;
137};
138} // namespace
139
140static bool CategoryMapIteratorSortCriterion(const Stats &lhs,
141 const Stats &rhs) {
142 return lhs.nanos > rhs.nanos;
143}
144
146 for (Category *i = g_categories; i; i = i->m_next) {
147 i->m_nanos.store(0, std::memory_order_release);
148 i->m_nanos_total.store(0, std::memory_order_release);
149 i->m_count.store(0, std::memory_order_release);
150 }
151}
152
154 std::vector<Stats> sorted;
155 for (Category *i = g_categories; i; i = i->m_next) {
156 uint64_t nanos = i->m_nanos.load(std::memory_order_acquire);
157 if (nanos) {
158 uint64_t nanos_total = i->m_nanos_total.load(std::memory_order_acquire);
159 uint64_t count = i->m_count.load(std::memory_order_acquire);
160 Stats stats{i->m_name, nanos, nanos_total, count};
161 sorted.push_back(stats);
162 }
163 }
164 if (sorted.empty())
165 return; // Later code will break without any elements.
166
167 // Sort by time
168 llvm::sort(sorted, CategoryMapIteratorSortCriterion);
169
170 for (const auto &stats : sorted)
171 s.Printf("%.9f sec (total: %.3fs; child: %.3fs; count: %" PRIu64
172 ") for %s\n",
173 stats.nanos / 1000000000., stats.nanos_total / 1000000000.,
174 (stats.nanos_total - stats.nanos) / 1000000000., stats.count,
175 stats.name);
176}
static llvm::ManagedStatic< llvm::SignpostEmitter > Signposts
Allows llvm::Timer to emit signposts when supported.
Definition Timer.cpp:35
static TimerStack & GetTimerStackForCurrentThread()
Definition Timer.cpp:44
#define TIMER_INDENT_AMOUNT
Definition Timer.cpp:27
static std::mutex & GetFileMutex()
Definition Timer.cpp:39
static bool CategoryMapIteratorSortCriterion(const Stats &lhs, const Stats &rhs)
Definition Timer.cpp:140
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition Stream.cpp:143
std::atomic< Category * > m_next
Definition Timer.h:37
std::atomic< uint64_t > m_nanos
Definition Timer.h:34
std::atomic< uint64_t > m_nanos_total
Definition Timer.h:35
Category(const char *category_name)
Definition Timer.cpp:49
std::atomic< uint64_t > m_count
Definition Timer.h:36
Category & m_category
Definition Timer.h:72
~Timer()
Destructor.
Definition Timer.cpp:97
static void ResetCategoryTimes()
Definition Timer.cpp:145
static std::atomic< bool > g_quiet
Definition Timer.h:76
TimePoint::duration m_child_duration
Definition Timer.h:74
static std::atomic< unsigned > g_display_depth
Definition Timer.h:77
void Start(llvm::function_ref< std::string()> start_message_func)
Definition Timer.cpp:61
static void SetQuiet(bool value)
Definition Timer.cpp:59
static void SetDisplayDepth(uint32_t depth)
Definition Timer.cpp:126
TimePoint m_total_start
Definition Timer.h:73
Timer(Category &category, const char *format,...) __attribute__((format(printf
Default constructor.
Definition Timer.cpp:79
static void DumpCategoryTimes(Stream &s)
Definition Timer.cpp:153
static constexpr Log::Category g_categories[]
A class that represents a running process on the host machine.