11#include "llvm/Support/ManagedStatic.h"
12#include "llvm/Support/Signposts.h"
27#define TIMER_INDENT_AMOUNT 2
30typedef std::vector<Timer *> TimerStack;
35static llvm::ManagedStatic<llvm::SignpostEmitter>
Signposts;
40 static std::mutex *g_file_mutex_ptr =
new std::mutex();
41 return *g_file_mutex_ptr;
45 static thread_local TimerStack g_stack;
50 m_nanos.store(0, std::memory_order_release);
52 m_count.store(0, std::memory_order_release);
56 }
while (!
g_categories.compare_exchange_weak(expected,
this));
61void Timer::Start(llvm::function_ref<std::string()> start_message_func) {
65 stack.push_back(
this);
72 const std::string start_message = start_message_func();
73 ::fprintf(stdout,
"%s", start_message.c_str());
75 ::fprintf(stdout,
"\n");
82 va_start(args, format);
83 Start([&]() -> std::string {
94 Start([&]() -> std::string {
return obj.str(); });
98 using namespace std::chrono;
100 auto stop_time = steady_clock::now();
109 ::fprintf(stdout,
"%*s%.9f sec (%.9f sec)\n",
111 duration<double>(total_dur).count(),
112 duration<double>(timer_dur).count());
115 assert(stack.back() ==
this);
118 stack.back()->ChildDuration(total_dur);
121 m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
122 m_category.m_nanos_total += std::chrono::nanoseconds(total_dur).count();
135 uint64_t nanos_total;
142 return lhs.nanos > rhs.nanos;
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);
154 std::vector<Stats> sorted;
156 uint64_t nanos = i->m_nanos.load(std::memory_order_acquire);
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);
170 for (
const auto &stats : sorted)
171 s.
Printf(
"%.9f sec (total: %.3fs; child: %.3fs; count: %" PRIu64
173 stats.nanos / 1000000000., stats.nanos_total / 1000000000.,
174 (stats.nanos_total - stats.nanos) / 1000000000., stats.count,
static llvm::ManagedStatic< llvm::SignpostEmitter > Signposts
Allows llvm::Timer to emit signposts when supported.
static TimerStack & GetTimerStackForCurrentThread()
#define TIMER_INDENT_AMOUNT
static std::mutex & GetFileMutex()
static bool CategoryMapIteratorSortCriterion(const Stats &lhs, const Stats &rhs)
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t size_t PrintfVarArg(const char *format, va_list args)
std::atomic< Category * > m_next
std::atomic< uint64_t > m_nanos
std::atomic< uint64_t > m_nanos_total
Category(const char *category_name)
std::atomic< uint64_t > m_count
static void ResetCategoryTimes()
static std::atomic< bool > g_quiet
TimePoint::duration m_child_duration
static std::atomic< unsigned > g_display_depth
void Start(llvm::function_ref< std::string()> start_message_func)
static void SetQuiet(bool value)
static void SetDisplayDepth(uint32_t depth)
Timer(Category &category, const char *format,...) __attribute__((format(printf
Default constructor.
static void DumpCategoryTimes(Stream &s)
static constexpr Log::Category g_categories[]
A class that represents a running process on the host machine.