Skip to content
Bitcoin Roots

v31.1-roots.1 · Commit 5: [PATCH 05/17] feat(net): preserve Roots peer controls and health view on v31

src/util/time.h

Before: src/util/time.h · After: src/util/time.h

File metadata
index 59abc4d5a7f64acf6396b6276e4db30cff8fdb15..db74b7deba7b61591744d1147cbf2cb311ae6ba4 100644

Download original patch · Release notes

Diff rows 1–48 of 48. 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,6 +6,7 @@
6
6
  #ifndef BITCOIN_UTIL_TIME_H
7
7
  #define BITCOIN_UTIL_TIME_H
8
8
 
9
+ #include <functional>
9
10
  #include <chrono> // IWYU pragma: export
10
11
  #include <cstdint>
11
12
  #include <optional>
@@ -158,4 +159,41 @@ struct timeval MillisToTimeval(int64_t nTimeout);
158
159
  */
159
160
  struct timeval MillisToTimeval(std::chrono::milliseconds ms);
160
161
 
162
+ /**
163
+ * Retrieve the CPU time (user + system) spent by the current thread.
164
+ */
165
+ std::chrono::nanoseconds ThreadCpuTime();
166
+
167
+ /**
168
+ * Measure CPU time spent by the current thread.
169
+ * A clock is started when a CpuTimer is created. When the object is destroyed
170
+ * the elapsed CPU time is calculated and a callback function is invoked,
171
+ * providing it the elapsed CPU time.
172
+ */
173
+ class CpuTimer
174
+ {
175
+ public:
176
+ using FinishedCB = std::function<void(std::chrono::nanoseconds)>;
177
+
178
+ /**
179
+ * Construct a timer.
180
+ * @param[in] finished_cb A callback to invoke when this object is destroyed.
181
+ */
182
+ CpuTimer(const FinishedCB& finished_cb)
183
+ : m_start{ThreadCpuTime()},
184
+ m_finished_cb{finished_cb}
185
+ {
186
+ }
187
+
188
+ ~CpuTimer()
189
+ {
190
+ const auto end{ThreadCpuTime()};
191
+ if (m_start > 0ns && end >= m_start) m_finished_cb(end - m_start);
192
+ }
193
+
194
+ private:
195
+ const std::chrono::nanoseconds m_start;
196
+ FinishedCB m_finished_cb;
197
+ };
198
+
161
199
  #endif // BITCOIN_UTIL_TIME_H