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 <cstdint>
16
17namespace lldb_private {
18class Stream;
19
20/// \class Timer Timer.h "lldb/Utility/Timer.h"
21/// A timer class that simplifies common timing metrics.
22
23class Timer {
24public:
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#if !defined(_MSC_VER)
45 // MSVC appears to have trouble recognizing the this argument in the constructor.
46 __attribute__((format(printf, 3, 4)))
47#endif
48 ;
49
50 /// Destructor
51 ~Timer();
52
53 void Dump();
54
55 static void SetDisplayDepth(uint32_t depth);
56
57 static void SetQuiet(bool value);
58
59 static void DumpCategoryTimes(Stream &s);
60
61 static void ResetCategoryTimes();
62
63protected:
64 using TimePoint = std::chrono::steady_clock::time_point;
65 void ChildDuration(TimePoint::duration dur) { m_child_duration += dur; }
66
69 TimePoint::duration m_child_duration{0};
70
71 static std::atomic<bool> g_quiet;
72 static std::atomic<unsigned> g_display_depth;
73
74private:
75 Timer(const Timer &) = delete;
76 const Timer &operator=(const Timer &) = delete;
77};
78
79} // namespace lldb_private
80
81// Use a format string because LLVM_PRETTY_FUNCTION might not be a string
82// literal.
83#define LLDB_SCOPED_TIMER() \
84 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
85 ::lldb_private::Timer _scoped_timer(_cat, "%s", LLVM_PRETTY_FUNCTION)
86#define LLDB_SCOPED_TIMERF(...) \
87 static ::lldb_private::Timer::Category _cat(LLVM_PRETTY_FUNCTION); \
88 ::lldb_private::Timer _scoped_timer(_cat, __VA_ARGS__)
89
90#endif // LLDB_UTILITY_TIMER_H
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Category(const Category &)=delete
std::atomic< Category * > m_next
Definition: Timer.h:36
std::atomic< uint64_t > m_nanos
Definition: Timer.h:33
std::atomic< uint64_t > m_nanos_total
Definition: Timer.h:34
const Category & operator=(const Category &)=delete
llvm::StringRef GetName()
Definition: Timer.h:28
std::atomic< uint64_t > m_count
Definition: Timer.h:35
A timer class that simplifies common timing metrics.
Definition: Timer.h:23
Category & m_category
Definition: Timer.h:67
~Timer()
Destructor.
Definition: Timer.cpp:82
static void ResetCategoryTimes()
Definition: Timer.cpp:130
const Timer & operator=(const Timer &)=delete
Timer(const Timer &)=delete
static std::atomic< bool > g_quiet
Definition: Timer.h:71
TimePoint::duration m_child_duration
Definition: Timer.h:69
static std::atomic< unsigned > g_display_depth
Definition: Timer.h:72
static void SetQuiet(bool value)
Definition: Timer.cpp:58
static void SetDisplayDepth(uint32_t depth)
Definition: Timer.cpp:111
TimePoint m_total_start
Definition: Timer.h:68
std::chrono::steady_clock::time_point TimePoint
Definition: Timer.h:64
void ChildDuration(TimePoint::duration dur)
Definition: Timer.h:65
static void DumpCategoryTimes(Stream &s)
Definition: Timer.cpp:138
A class that represents a running process on the host machine.