v29.4-roots.4 · Commit 2: [PATCH 02/37] runtime: preserve shared operator controls
src/util/time.h
Before: src/util/time.h · After: src/util/time.h
File metadata
index c43b306ff2438bb9f7dcb1dc7494968b937d310c..340ad2b9b2de7effa8e0ef2436446e847bffc265 100644
Download original patch · Release notes
Diff rows 1–75 of 75. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.
src/util/time.h
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
#ifndef BITCOIN_UTIL_TIME_H
|
|
7
7
|
#define BITCOIN_UTIL_TIME_H
|
|
8
8
|
|
|
9
|
+
#include <atomic>
|
|
9
10
|
#include <chrono> // IWYU pragma: export
|
|
10
11
|
#include <cstdint>
|
|
12
|
+
#include <functional>
|
|
11
13
|
#include <optional>
|
|
12
14
|
#include <string>
|
|
13
15
|
#include <string_view>
|
|
@@ -73,6 +75,12 @@ constexpr auto Ticks(Dur2 d)
|
|
|
73
75
|
{
|
|
74
76
|
return std::chrono::duration_cast<Dur1>(d).count();
|
|
75
77
|
}
|
|
78
|
+
|
|
79
|
+
template <typename Duration>
|
|
80
|
+
constexpr int64_t TicksSeconds(Duration d)
|
|
81
|
+
{
|
|
82
|
+
return int64_t{Ticks<std::chrono::seconds>(d)};
|
|
83
|
+
}
|
|
76
84
|
template <typename Duration, typename Timepoint>
|
|
77
85
|
constexpr auto TicksSinceEpoch(Timepoint t)
|
|
78
86
|
{
|
|
@@ -132,6 +140,7 @@ T GetTime()
|
|
|
132
140
|
*/
|
|
133
141
|
std::string FormatISO8601DateTime(int64_t nTime);
|
|
134
142
|
std::string FormatISO8601Date(int64_t nTime);
|
|
143
|
+
std::string FormatISO8601Time(int64_t nTime);
|
|
135
144
|
std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
|
|
136
145
|
|
|
137
146
|
/**
|
|
@@ -144,4 +153,46 @@ struct timeval MillisToTimeval(int64_t nTimeout);
|
|
|
144
153
|
*/
|
|
145
154
|
struct timeval MillisToTimeval(std::chrono::milliseconds ms);
|
|
146
155
|
|
|
156
|
+
/**
|
|
157
|
+
* Retrieve the CPU time (user + system) spent by the current thread.
|
|
158
|
+
*/
|
|
159
|
+
std::chrono::nanoseconds ThreadCpuTime();
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Measure CPU time spent by the current thread.
|
|
163
|
+
* A clock is started when a CpuTimer is created. When the object is destroyed
|
|
164
|
+
* the elapsed CPU time is calculated and a callback function is invoked,
|
|
165
|
+
* providing it the elapsed CPU time.
|
|
166
|
+
*/
|
|
167
|
+
class CpuTimer
|
|
168
|
+
{
|
|
169
|
+
public:
|
|
170
|
+
using FinishedCB = std::function<void(std::chrono::nanoseconds)>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Construct a timer.
|
|
174
|
+
* @param[in] finished_cb A callback to invoke when this object is destroyed.
|
|
175
|
+
*/
|
|
176
|
+
CpuTimer(const FinishedCB& finished_cb)
|
|
177
|
+
: m_start{ThreadCpuTime()},
|
|
178
|
+
m_finished_cb{finished_cb}
|
|
179
|
+
{
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
~CpuTimer()
|
|
183
|
+
{
|
|
184
|
+
m_finished_cb(ThreadCpuTime() - m_start);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private:
|
|
188
|
+
const std::chrono::nanoseconds m_start;
|
|
189
|
+
FinishedCB m_finished_cb;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Add `b` nanoseconds to a nanoseconds atomic.
|
|
194
|
+
* @return The value of `a` immediately after the operation.
|
|
195
|
+
*/
|
|
196
|
+
std::chrono::nanoseconds operator+=(std::atomic<std::chrono::nanoseconds>& a, std::chrono::nanoseconds b);
|
|
197
|
+
|
|
147
198
|
#endif // BITCOIN_UTIL_TIME_H
|