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 liblldb_Timer_h_
10 #define liblldb_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 
29   private:
30     friend class Timer;
31     const char *m_name;
32     std::atomic<uint64_t> m_nanos;
33     std::atomic<uint64_t> m_nanos_total;
34     std::atomic<uint64_t> m_count;
35     std::atomic<Category *> m_next;
36 
37     DISALLOW_COPY_AND_ASSIGN(Category);
38   };
39 
40   /// Default constructor.
41   Timer(Category &category, const char *format, ...)
42       __attribute__((format(printf, 3, 4)));
43 
44   /// Destructor
45   ~Timer();
46 
47   void Dump();
48 
49   static void SetDisplayDepth(uint32_t depth);
50 
51   static void SetQuiet(bool value);
52 
53   static void DumpCategoryTimes(Stream *s);
54 
55   static void ResetCategoryTimes();
56 
57 protected:
58   using TimePoint = std::chrono::steady_clock::time_point;
59   void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
60 
61   Category &m_category;
62   TimePoint m_total_start;
63   TimePoint::duration m_child_duration{0};
64 
65   static std::atomic<bool> g_quiet;
66   static std::atomic<unsigned> g_display_depth;
67 
68 private:
69   DISALLOW_COPY_AND_ASSIGN(Timer);
70 };
71 
72 } // namespace lldb_private
73 
74 #endif // liblldb_Timer_h_
75