xref: /openbsd-src/gnu/llvm/lldb/include/lldb/Utility/Timer.h (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrick //===-- Timer.h -------------------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9dda28197Spatrick #ifndef LLDB_UTILITY_TIMER_H
10dda28197Spatrick #define LLDB_UTILITY_TIMER_H
11061da546Spatrick 
12*f6aab3d8Srobert #include "lldb/lldb-defines.h"
13061da546Spatrick #include "llvm/Support/Chrono.h"
14061da546Spatrick #include <atomic>
15be691f3bSpatrick #include <cstdint>
16be691f3bSpatrick 
17061da546Spatrick namespace lldb_private {
18061da546Spatrick class Stream;
19061da546Spatrick 
20061da546Spatrick /// \class Timer Timer.h "lldb/Utility/Timer.h"
21061da546Spatrick /// A timer class that simplifies common timing metrics.
22061da546Spatrick 
23061da546Spatrick class Timer {
24061da546Spatrick public:
25061da546Spatrick   class Category {
26061da546Spatrick   public:
27061da546Spatrick     explicit Category(const char *category_name);
GetName()28be691f3bSpatrick     llvm::StringRef GetName() { return m_name; }
29061da546Spatrick 
30061da546Spatrick   private:
31061da546Spatrick     friend class Timer;
32061da546Spatrick     const char *m_name;
33061da546Spatrick     std::atomic<uint64_t> m_nanos;
34061da546Spatrick     std::atomic<uint64_t> m_nanos_total;
35061da546Spatrick     std::atomic<uint64_t> m_count;
36061da546Spatrick     std::atomic<Category *> m_next;
37061da546Spatrick 
38dda28197Spatrick     Category(const Category &) = delete;
39dda28197Spatrick     const Category &operator=(const Category &) = delete;
40061da546Spatrick   };
41061da546Spatrick 
42061da546Spatrick   /// Default constructor.
43061da546Spatrick   Timer(Category &category, const char *format, ...)
44be691f3bSpatrick #if !defined(_MSC_VER)
45be691f3bSpatrick   // MSVC appears to have trouble recognizing the this argument in the constructor.
46be691f3bSpatrick       __attribute__((format(printf, 3, 4)))
47be691f3bSpatrick #endif
48be691f3bSpatrick     ;
49061da546Spatrick 
50061da546Spatrick   /// Destructor
51061da546Spatrick   ~Timer();
52061da546Spatrick 
53061da546Spatrick   void Dump();
54061da546Spatrick 
55061da546Spatrick   static void SetDisplayDepth(uint32_t depth);
56061da546Spatrick 
57061da546Spatrick   static void SetQuiet(bool value);
58061da546Spatrick 
59061da546Spatrick   static void DumpCategoryTimes(Stream *s);
60061da546Spatrick 
61061da546Spatrick   static void ResetCategoryTimes();
62061da546Spatrick 
63061da546Spatrick protected:
64061da546Spatrick   using TimePoint = std::chrono::steady_clock::time_point;
ChildDuration(TimePoint::duration dur)65061da546Spatrick   void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
66061da546Spatrick 
67061da546Spatrick   Category &m_category;
68061da546Spatrick   TimePoint m_total_start;
69061da546Spatrick   TimePoint::duration m_child_duration{0};
70061da546Spatrick 
71061da546Spatrick   static std::atomic<bool> g_quiet;
72061da546Spatrick   static std::atomic<unsigned> g_display_depth;
73061da546Spatrick 
74061da546Spatrick private:
75dda28197Spatrick   Timer(const Timer &) = delete;
76dda28197Spatrick   const Timer &operator=(const Timer &) = delete;
77061da546Spatrick };
78061da546Spatrick 
79061da546Spatrick } // namespace lldb_private
80061da546Spatrick 
81be691f3bSpatrick // Use a format string because LLVM_PRETTY_FUNCTION might not be a string
82be691f3bSpatrick // literal.
83be691f3bSpatrick #define LLDB_SCOPED_TIMER()                                                    \
84be691f3bSpatrick   static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
85*f6aab3d8Srobert   ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
86*f6aab3d8Srobert #define LLDB_SCOPED_TIMERF(...)                                                \
87be691f3bSpatrick   static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
88*f6aab3d8Srobert   ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
89be691f3bSpatrick 
90dda28197Spatrick #endif // LLDB_UTILITY_TIMER_H
91