LLDB mainline
Timer.h
Go to the documentation of this file.
1//===-- Timer.h -------------------------------------------------*- C++ -*-===//
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//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_TIMER_H
10#define LLDB_UTILITY_TIMER_H
11
12#include "lldb/lldb-defines.h"
13#include "llvm/Support/Chrono.h"
14#include "llvm/Support/FormatVariadic.h"
15#include <atomic>
16#include <cstdint>
17
18namespace lldb_private {
19class Stream;
20
21/// \class Timer Timer.h "lldb/Utility/Timer.h"
22/// A timer class that simplifies common timing metrics.
23
24class Timer {
25public:
26 class Category {
27 public:
28 explicit Category(const char *category_name);
29 llvm::StringRef GetName() { return m_name; }
30
31 private:
32 friend class Timer;
33 const char *m_name;
34 std::atomic<uint64_t> m_nanos;
35 std::atomic<uint64_t> m_nanos_total;
36 std::atomic<uint64_t> m_count;
37 std::atomic<Category *> m_next;
38
39 Category(const Category &) = delete;
40 const Category &operator=(const Category &) = delete;
41 };
42
43 /// Default constructor.
44 Timer(Category &category, const char *format, ...)
45#if !defined(_MSC_VER)
46 // MSVC appears to have trouble recognizing the this argument in the constructor.
47 __attribute__((format(printf, 3, 4)))
48#endif
49 ;
50
51 Timer(Category &category, const llvm::formatv_object_base &obj);
52
53 /// Destructor
54 ~Timer();
55
56 void Dump();
57
58 static void SetDisplayDepth(uint32_t depth);
59
60 static void SetQuiet(bool value);
61
62 static void DumpCategoryTimes(Stream &s);
63
64 static void ResetCategoryTimes();
65
66protected:
67 using TimePoint = std::chrono::steady_clock::time_point;
68 void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
69
70 void Start(llvm::function_ref<std::string()> start_message_func);
71
74 TimePoint::duration m_child_duration{0};
75
76 static std::atomic<bool> g_quiet;
77 static std::atomic<unsigned> g_display_depth;
78
79private:
80 Timer(const Timer &) = delete;
81 const Timer &operator=(const Timer &) = delete;
82};
83
84} // namespace lldb_private
85
86// Use a format string because LLVM_PRETTY_FUNCTION might not be a string
87// literal.
88#define LLDB_SCOPED_TIMER() \
89 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
90 ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
91#define LLDB_SCOPED_TIMERF(...) \
92 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
93 ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
94#define LLDB_SCOPED_TIMERV(...) \
95 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
96 ::lldb_private::Timer _scoped_timer(_cat, llvm::formatv(__VA_ARGS__))
97
98#endif // LLDB_UTILITY_TIMER_H
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Category(const Category &)=delete
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
const Category & operator=(const Category &)=delete
llvm::StringRef GetName()
Definition Timer.h:29
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
const Timer & operator=(const Timer &)=delete
Timer(const Timer &)=delete
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
std::chrono::steady_clock::time_point TimePoint
Definition Timer.h:67
void ChildDuration(TimePoint::duration dur)
Definition Timer.h:68
Timer(Category &category, const char *format,...) __attribute__((format(printf
Default constructor.
Definition Timer.cpp:79
static void DumpCategoryTimes(Stream &s)
Definition Timer.cpp:153
A class that represents a running process on the host machine.