1204431Sraj//===-- Timer.h -------------------------------------------------*- C++ -*-===//
2204431Sraj//
3204431Sraj// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4204431Sraj// See https://llvm.org/LICENSE.txt for license information.
5204431Sraj// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6266130Sian//
7204431Sraj//===----------------------------------------------------------------------===//
8204431Sraj
9204431Sraj#ifndef LLDB_UTILITY_TIMER_H
10204431Sraj#define LLDB_UTILITY_TIMER_H
11204431Sraj
12204431Sraj#include "lldb/lldb-defines.h"
13204431Sraj#include "llvm/Support/Chrono.h"
14204431Sraj#include <atomic>
15204431Sraj#include <cstdint>
16204431Sraj
17204431Srajnamespace lldb_private {
18204431Srajclass Stream;
19204431Sraj
20204431Sraj/// \class Timer Timer.h "lldb/Utility/Timer.h"
21204431Sraj/// A timer class that simplifies common timing metrics.
22204431Sraj
23204433Srajclass Timer {
24204433Srajpublic:
25238742Simp  class Category {
26204431Sraj  public:
27204433Sraj    explicit Category(const char *category_name);
28204431Sraj    llvm::StringRef GetName() { return m_name; }
29204431Sraj
30204431Sraj  private:
31204431Sraj    friend class Timer;
32204431Sraj    const char *m_name;
33204431Sraj    std::atomic<uint64_t> m_nanos;
34204431Sraj    std::atomic<uint64_t> m_nanos_total;
35204431Sraj    std::atomic<uint64_t> m_count;
36204433Sraj    std::atomic<Category *> m_next;
37204431Sraj
38204431Sraj    Category(const Category &) = delete;
39204431Sraj    const Category &operator=(const Category &) = delete;
40204431Sraj  };
41204431Sraj
42204431Sraj  /// Default constructor.
43204431Sraj  Timer(Category &category, const char *format, ...)
44204431Sraj#if !defined(_MSC_VER)
45204433Sraj  // MSVC appears to have trouble recognizing the this argument in the constructor.
46204431Sraj      __attribute__((format(printf, 3, 4)))
47204431Sraj#endif
48266130Sian    ;
49204431Sraj
50266130Sian  /// Destructor
51266130Sian  ~Timer();
52266130Sian
53204431Sraj  void Dump();
54204431Sraj
55204431Sraj  static void SetDisplayDepth(uint32_t depth);
56204431Sraj
57204431Sraj  static void SetQuiet(bool value);
58204431Sraj
59204431Sraj  static void DumpCategoryTimes(Stream &s);
60204431Sraj
61204431Sraj  static void ResetCategoryTimes();
62204431Sraj
63204431Srajprotected:
64204431Sraj  using TimePoint = std::chrono::steady_clock::time_point;
65204431Sraj  void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
66204431Sraj
67204431Sraj  Category &m_category;
68204431Sraj  TimePoint m_total_start;
69204431Sraj  TimePoint::duration m_child_duration{0};
70204431Sraj
71204431Sraj  static std::atomic<bool> g_quiet;
72204431Sraj  static std::atomic<unsigned> g_display_depth;
73204431Sraj
74204431Srajprivate:
75204431Sraj  Timer(const Timer &) = delete;
76204431Sraj  const Timer &operator=(const Timer &) = delete;
77204431Sraj};
78204431Sraj
79204431Sraj} // namespace lldb_private
80204431Sraj
81204431Sraj// Use a format string because LLVM_PRETTY_FUNCTION might not be a string
82204431Sraj// literal.
83204431Sraj#define LLDB_SCOPED_TIMER()                                                    \
84204431Sraj  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
85204431Sraj  ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
86204431Sraj#define LLDB_SCOPED_TIMERF(...)                                                \
87204431Sraj  static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION);           \
88204431Sraj  ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
89204431Sraj
90204431Sraj#endif // LLDB_UTILITY_TIMER_H
91204431Sraj