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 <atomic>
15 #include <stdint.h>
16 
17 namespace lldb_private {
18 class Stream;
19 
20 /// \class Timer Timer.h "lldb/Utility/Timer.h"
21 /// A timer class that simplifies common timing metrics.
22 
23 class Timer {
24 public:
25  class Category {
26  public:
27  explicit Category(const char *category_name);
28  llvm::StringRef GetName() { return m_name; }
29 
30  private:
31  friend class Timer;
32  const char *m_name;
33  std::atomic<uint64_t> m_nanos;
34  std::atomic<uint64_t> m_nanos_total;
35  std::atomic<uint64_t> m_count;
36  std::atomic<Category *> m_next;
37 
38  Category(const Category &) = delete;
39  const Category &operator=(const Category &) = delete;
40  };
41 
42  /// Default constructor.
43  Timer(Category &category, const char *format, ...)
44  __attribute__((format(printf, 3, 4)));
45 
46  /// Destructor
47  ~Timer();
48 
49  void Dump();
50 
51  static void SetDisplayDepth(uint32_t depth);
52 
53  static void SetQuiet(bool value);
54 
55  static void DumpCategoryTimes(Stream *s);
56 
57  static void ResetCategoryTimes();
58 
59 protected:
60  using TimePoint = std::chrono::steady_clock::time_point;
61  void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
62 
65  TimePoint::duration m_child_duration{0};
66 
67  static std::atomic<bool> g_quiet;
68  static std::atomic<unsigned> g_display_depth;
69 
70 private:
71  Timer(const Timer &) = delete;
72  const Timer &operator=(const Timer &) = delete;
73 };
74 
75 } // namespace lldb_private
76 
77 #define LLDB_SCOPED_TIMER() \
78  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
79  ::lldb_private::Timer _scoped_timer(_cat, LLVM_PRETTY_FUNCTION)
80 #define LLDB_SCOPED_TIMERF(...) \
81  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
82  ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
83 
84 #endif // LLDB_UTILITY_TIMER_H
lldb_private::Timer::Category
Definition: Timer.h:25
lldb_private::Timer::Category::m_nanos_total
std::atomic< uint64_t > m_nanos_total
Definition: Timer.h:34
lldb_private::Timer::SetDisplayDepth
static void SetDisplayDepth(uint32_t depth)
Definition: Timer.cpp:111
lldb_private::Timer::Category::operator=
const Category & operator=(const Category &)=delete
lldb_private::Timer::ChildDuration
void ChildDuration(TimePoint::duration dur)
Definition: Timer.h:61
lldb-defines.h
lldb_private::Timer::Category::m_nanos
std::atomic< uint64_t > m_nanos
Definition: Timer.h:33
lldb_private::Timer::g_quiet
static std::atomic< bool > g_quiet
Definition: Timer.h:67
lldb_private::Timer::m_category
Category & m_category
Definition: Timer.h:63
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:60
lldb_private::Timer::Category::m_count
std::atomic< uint64_t > m_count
Definition: Timer.h:35
lldb_private::Timer::DumpCategoryTimes
static void DumpCategoryTimes(Stream *s)
Definition: Timer.cpp:138
lldb_private::Timer::Dump
void Dump()
lldb_private::Timer::m_total_start
TimePoint m_total_start
Definition: Timer.h:64
lldb_private::Timer::SetQuiet
static void SetQuiet(bool value)
Definition: Timer.cpp:58
lldb_private::Timer::g_display_depth
static std::atomic< unsigned > g_display_depth
Definition: Timer.h:68
lldb_private::Timer::Category::m_name
const char * m_name
Definition: Timer.h:32
uint32_t
lldb_private::Timer::TimePoint
std::chrono::steady_clock::time_point TimePoint
Definition: Timer.h:60
lldb_private::Timer::ResetCategoryTimes
static void ResetCategoryTimes()
Definition: Timer.cpp:130
lldb_private::Timer
Definition: Timer.h:23
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:65
lldb_private::Timer::Category::m_next
std::atomic< Category * > m_next
Definition: Timer.h:36
lldb_private::Timer::Category::GetName
llvm::StringRef GetName()
Definition: Timer.h:28
lldb_private::Timer::Category::Category
Category(const char *category_name)
Definition: Timer.cpp:48