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 "llvm/ADT/ScopeExit.h"
13 #include "llvm/Support/Chrono.h"
14 #include "llvm/Support/Signposts.h"
15 #include <atomic>
16 #include <cstdint>
17 
18 namespace llvm {
19  class SignpostEmitter;
20 }
21 
22 namespace lldb_private {
23 class Stream;
24 
25 /// \class Timer Timer.h "lldb/Utility/Timer.h"
26 /// A timer class that simplifies common timing metrics.
27 
28 class Timer {
29 public:
30  class Category {
31  public:
32  explicit Category(const char *category_name);
33  llvm::StringRef GetName() { return m_name; }
34 
35  private:
36  friend class Timer;
37  const char *m_name;
38  std::atomic<uint64_t> m_nanos;
39  std::atomic<uint64_t> m_nanos_total;
40  std::atomic<uint64_t> m_count;
41  std::atomic<Category *> m_next;
42 
43  Category(const Category &) = delete;
44  const Category &operator=(const Category &) = delete;
45  };
46 
47  /// Default constructor.
48  Timer(Category &category, const char *format, ...)
49 #if !defined(_MSC_VER)
50  // MSVC appears to have trouble recognizing the this argument in the constructor.
51  __attribute__((format(printf, 3, 4)))
52 #endif
53  ;
54 
55  /// Destructor
56  ~Timer();
57 
58  void Dump();
59 
60  static void SetDisplayDepth(uint32_t depth);
61 
62  static void SetQuiet(bool value);
63 
64  static void DumpCategoryTimes(Stream *s);
65 
66  static void ResetCategoryTimes();
67 
68 protected:
69  using TimePoint = std::chrono::steady_clock::time_point;
70  void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
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 
79 private:
80  Timer(const Timer &) = delete;
81  const Timer &operator=(const Timer &) = delete;
82 };
83 
84 llvm::SignpostEmitter &GetSignposts();
85 
86 } // namespace lldb_private
87 
88 // Use a format string because LLVM_PRETTY_FUNCTION might not be a string
89 // literal.
90 #define LLDB_SCOPED_TIMER() \
91  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
92  ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION); \
93  SIGNPOST_EMITTER_START_INTERVAL(::lldb_private::GetSignposts(), \
94  &_scoped_timer, "%s", LLVM_PRETTY_FUNCTION); \
95  auto _scoped_signpost = llvm::make_scope_exit([&_scoped_timer]() { \
96  ::lldb_private::GetSignposts().endInterval(&_scoped_timer); \
97  })
98 
99 #define LLDB_SCOPED_TIMERF(FMT, ...) \
100  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
101  ::lldb_private::Timer _scoped_timer(_cat, FMT, __VA_ARGS__); \
102  SIGNPOST_EMITTER_START_INTERVAL(::lldb_private::GetSignposts(), \
103  &_scoped_timer, FMT, __VA_ARGS__); \
104  auto _scoped_signpost = llvm::make_scope_exit([&_scoped_timer]() { \
105  ::lldb_private::GetSignposts().endInterval(&_scoped_timer); \
106  })
107 
108 #endif // LLDB_UTILITY_TIMER_H
lldb_private::Timer::Category
Definition: Timer.h:30
lldb_private::Timer::Category::m_nanos_total
std::atomic< uint64_t > m_nanos_total
Definition: Timer.h:39
llvm
Definition: Debugger.h:49
lldb_private::Timer::SetDisplayDepth
static void SetDisplayDepth(uint32_t depth)
Definition: Timer.cpp:110
lldb_private::Timer::Category::operator=
const Category & operator=(const Category &)=delete
lldb_private::Timer::ChildDuration
void ChildDuration(TimePoint::duration dur)
Definition: Timer.h:70
lldb_private::Timer::Category::m_nanos
std::atomic< uint64_t > m_nanos
Definition: Timer.h:38
lldb_private::Timer::g_quiet
static std::atomic< bool > g_quiet
Definition: Timer.h:76
lldb_private::Timer::m_category
Category & m_category
Definition: Timer.h:72
lldb_private::Timer::operator=
const Timer & operator=(const Timer &)=delete
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Timer::Timer
Timer(Category &category, const char *format,...) __attribute__((format(printf
Default constructor.
Definition: Timer.cpp:62
lldb_private::Timer::Category::m_count
std::atomic< uint64_t > m_count
Definition: Timer.h:40
lldb_private::Timer::DumpCategoryTimes
static void DumpCategoryTimes(Stream *s)
Definition: Timer.cpp:137
lldb_private::Timer::Dump
void Dump()
lldb_private::Timer::m_total_start
TimePoint m_total_start
Definition: Timer.h:73
lldb_private::Timer::SetQuiet
static void SetQuiet(bool value)
Definition: Timer.cpp:60
lldb_private::Timer::~Timer
~Timer()
Destructor.
Definition: Timer.cpp:83
lldb_private::Timer::g_display_depth
static std::atomic< unsigned > g_display_depth
Definition: Timer.h:77
lldb_private::Timer::Category::m_name
const char * m_name
Definition: Timer.h:37
uint32_t
lldb_private::Timer::TimePoint
std::chrono::steady_clock::time_point TimePoint
Definition: Timer.h:69
lldb_private::Timer::ResetCategoryTimes
static void ResetCategoryTimes()
Definition: Timer.cpp:129
lldb_private::Timer
Definition: Timer.h:28
lldb_private::GetSignposts
llvm::SignpostEmitter & GetSignposts()
Definition: Timer.cpp:36
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Timer::m_child_duration
TimePoint::duration m_child_duration
Definition: Timer.h:74
lldb_private::Timer::Category::m_next
std::atomic< Category * > m_next
Definition: Timer.h:41
lldb_private::Timer::Category::GetName
llvm::StringRef GetName()
Definition: Timer.h:33
lldb_private::Timer::Category::Category
Category(const char *category_name)
Definition: Timer.cpp:50