/bitcoin/src/logging/timer.h
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #ifndef BITCOIN_LOGGING_TIMER_H |
7 | | #define BITCOIN_LOGGING_TIMER_H |
8 | | |
9 | | #include <logging.h> |
10 | | #include <util/macros.h> |
11 | | #include <util/time.h> |
12 | | #include <util/types.h> |
13 | | |
14 | | #include <chrono> |
15 | | #include <optional> |
16 | | #include <string> |
17 | | |
18 | | |
19 | | namespace BCLog { |
20 | | |
21 | | //! RAII-style object that outputs timing information to logs. |
22 | | template <typename TimeType> |
23 | | class Timer |
24 | | { |
25 | | public: |
26 | | //! If log_category is left as the default, end_msg will log unconditionally |
27 | | //! (instead of being filtered by category). |
28 | | Timer( |
29 | | std::string prefix, |
30 | | std::string end_msg, |
31 | | BCLog::LogFlags log_category = BCLog::LogFlags::ALL, |
32 | | bool msg_on_completion = true) |
33 | 98.7k | : m_prefix(std::move(prefix)), |
34 | 98.7k | m_title(std::move(end_msg)), |
35 | 98.7k | m_log_category(log_category), |
36 | 98.7k | m_message_on_completion(msg_on_completion) |
37 | 98.7k | { |
38 | 98.7k | this->Log(strprintf("%s started", m_title)); |
39 | 98.7k | m_start_t = std::chrono::steady_clock::now(); |
40 | 98.7k | } BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1l> > >::Timer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, BCLog::LogFlags, bool) Line | Count | Source | 33 | 11.0k | : m_prefix(std::move(prefix)), | 34 | 11.0k | m_title(std::move(end_msg)), | 35 | 11.0k | m_log_category(log_category), | 36 | 11.0k | m_message_on_completion(msg_on_completion) | 37 | 11.0k | { | 38 | 11.0k | this->Log(strprintf("%s started", m_title)); | 39 | 11.0k | m_start_t = std::chrono::steady_clock::now(); | 40 | 11.0k | } |
BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1000l> > >::Timer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, BCLog::LogFlags, bool) Line | Count | Source | 33 | 87.6k | : m_prefix(std::move(prefix)), | 34 | 87.6k | m_title(std::move(end_msg)), | 35 | 87.6k | m_log_category(log_category), | 36 | 87.6k | m_message_on_completion(msg_on_completion) | 37 | 87.6k | { | 38 | 87.6k | this->Log(strprintf("%s started", m_title)); | 39 | 87.6k | m_start_t = std::chrono::steady_clock::now(); | 40 | 87.6k | } |
|
41 | | |
42 | | ~Timer() |
43 | 98.7k | { |
44 | 98.7k | if (m_message_on_completion) { Branch (44:13): [True: 11.0k, False: 0]
Branch (44:13): [True: 87.6k, False: 0]
|
45 | 98.7k | this->Log(strprintf("%s completed", m_title)); |
46 | 98.7k | } else { |
47 | 0 | this->Log("completed"); |
48 | 0 | } |
49 | 98.7k | } BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1l> > >::~Timer() Line | Count | Source | 43 | 11.0k | { | 44 | 11.0k | if (m_message_on_completion) { Branch (44:13): [True: 11.0k, False: 0]
| 45 | 11.0k | this->Log(strprintf("%s completed", m_title)); | 46 | 11.0k | } else { | 47 | 0 | this->Log("completed"); | 48 | 0 | } | 49 | 11.0k | } |
BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1000l> > >::~Timer() Line | Count | Source | 43 | 87.6k | { | 44 | 87.6k | if (m_message_on_completion) { Branch (44:13): [True: 87.6k, False: 0]
| 45 | 87.6k | this->Log(strprintf("%s completed", m_title)); | 46 | 87.6k | } else { | 47 | 0 | this->Log("completed"); | 48 | 0 | } | 49 | 87.6k | } |
|
50 | | |
51 | | void Log(const std::string& msg) |
52 | 197k | { |
53 | 197k | const std::string full_msg = this->LogMsg(msg); |
54 | | |
55 | 197k | if (m_log_category == BCLog::LogFlags::ALL) { Branch (55:13): [True: 22.1k, False: 0]
Branch (55:13): [True: 0, False: 175k]
|
56 | 22.1k | LogPrintf("%s\n", full_msg); |
57 | 175k | } else { |
58 | 175k | LogDebug(m_log_category, "%s\n", full_msg); |
59 | 175k | } |
60 | 197k | } BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1l> > >::Log(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) Line | Count | Source | 52 | 22.1k | { | 53 | 22.1k | const std::string full_msg = this->LogMsg(msg); | 54 | | | 55 | 22.1k | if (m_log_category == BCLog::LogFlags::ALL) { Branch (55:13): [True: 22.1k, False: 0]
| 56 | 22.1k | LogPrintf("%s\n", full_msg); | 57 | 22.1k | } else { | 58 | 0 | LogDebug(m_log_category, "%s\n", full_msg); | 59 | 0 | } | 60 | 22.1k | } |
BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1000l> > >::Log(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) Line | Count | Source | 52 | 175k | { | 53 | 175k | const std::string full_msg = this->LogMsg(msg); | 54 | | | 55 | 175k | if (m_log_category == BCLog::LogFlags::ALL) { Branch (55:13): [True: 0, False: 175k]
| 56 | 0 | LogPrintf("%s\n", full_msg); | 57 | 175k | } else { | 58 | 175k | LogDebug(m_log_category, "%s\n", full_msg); | 59 | 175k | } | 60 | 175k | } |
|
61 | | |
62 | | std::string LogMsg(const std::string& msg) |
63 | 197k | { |
64 | 197k | const auto end_time{std::chrono::steady_clock::now()}; |
65 | 197k | if (!m_start_t) { Branch (65:13): [True: 11.0k, False: 11.0k]
Branch (65:13): [True: 87.6k, False: 87.6k]
|
66 | 98.7k | return strprintf("%s: %s", m_prefix, msg); |
67 | 98.7k | } |
68 | 98.7k | const auto duration{end_time - *m_start_t}; |
69 | | |
70 | | if constexpr (std::is_same_v<TimeType, std::chrono::microseconds>) { |
71 | | return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks<std::chrono::microseconds>(duration)); |
72 | 87.6k | } else if constexpr (std::is_same_v<TimeType, std::chrono::milliseconds>) { |
73 | 87.6k | return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration)); |
74 | 87.6k | } else if constexpr (std::is_same_v<TimeType, std::chrono::seconds>) { |
75 | 11.0k | return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration)); |
76 | | } else { |
77 | | static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type"); |
78 | | } |
79 | 98.7k | } BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1l> > >::LogMsg(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) Line | Count | Source | 63 | 22.1k | { | 64 | 22.1k | const auto end_time{std::chrono::steady_clock::now()}; | 65 | 22.1k | if (!m_start_t) { Branch (65:13): [True: 11.0k, False: 11.0k]
| 66 | 11.0k | return strprintf("%s: %s", m_prefix, msg); | 67 | 11.0k | } | 68 | 11.0k | const auto duration{end_time - *m_start_t}; | 69 | | | 70 | | if constexpr (std::is_same_v<TimeType, std::chrono::microseconds>) { | 71 | | return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks<std::chrono::microseconds>(duration)); | 72 | | } else if constexpr (std::is_same_v<TimeType, std::chrono::milliseconds>) { | 73 | | return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration)); | 74 | 11.0k | } else if constexpr (std::is_same_v<TimeType, std::chrono::seconds>) { | 75 | 11.0k | return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration)); | 76 | | } else { | 77 | | static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type"); | 78 | | } | 79 | 11.0k | } |
BCLog::Timer<std::chrono::duration<long, std::ratio<1l, 1000l> > >::LogMsg(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) Line | Count | Source | 63 | 175k | { | 64 | 175k | const auto end_time{std::chrono::steady_clock::now()}; | 65 | 175k | if (!m_start_t) { Branch (65:13): [True: 87.6k, False: 87.6k]
| 66 | 87.6k | return strprintf("%s: %s", m_prefix, msg); | 67 | 87.6k | } | 68 | 87.6k | const auto duration{end_time - *m_start_t}; | 69 | | | 70 | | if constexpr (std::is_same_v<TimeType, std::chrono::microseconds>) { | 71 | | return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks<std::chrono::microseconds>(duration)); | 72 | 87.6k | } else if constexpr (std::is_same_v<TimeType, std::chrono::milliseconds>) { | 73 | 87.6k | return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration)); | 74 | | } else if constexpr (std::is_same_v<TimeType, std::chrono::seconds>) { | 75 | | return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration)); | 76 | | } else { | 77 | | static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type"); | 78 | | } | 79 | 87.6k | } |
|
80 | | |
81 | | private: |
82 | | std::optional<std::chrono::steady_clock::time_point> m_start_t{}; |
83 | | |
84 | | //! Log prefix; usually the name of the function this was created in. |
85 | | const std::string m_prefix; |
86 | | |
87 | | //! A descriptive message of what is being timed. |
88 | | const std::string m_title; |
89 | | |
90 | | //! Forwarded on to LogDebug if specified - has the effect of only |
91 | | //! outputting the timing log when a particular debug= category is specified. |
92 | | const BCLog::LogFlags m_log_category; |
93 | | |
94 | | //! Whether to output the message again on completion. |
95 | | const bool m_message_on_completion; |
96 | | }; |
97 | | |
98 | | } // namespace BCLog |
99 | | |
100 | | |
101 | | #define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \ |
102 | | BCLog::Timer<std::chrono::microseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category) |
103 | | #define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \ |
104 | 87.6k | BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category) |
105 | | #define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \ |
106 | 0 | BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category, /* msg_on_completion=*/false) |
107 | | #define LOG_TIME_SECONDS(end_msg) \ |
108 | 11.0k | BCLog::Timer<std::chrono::seconds> UNIQUE_NAME(logging_timer)(__func__, end_msg) |
109 | | |
110 | | |
111 | | #endif // BITCOIN_LOGGING_TIMER_H |