Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 2: [PATCH 02/68] feat(runtime): preserve Roots operator controls on Core 30.3

src/util/time.h

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

File metadata
index 655db4475a585f12f3de7c81be889924fb6761f8..72dea009d0328bacbac7790d4c367044c422d5fc 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>
@@ -74,6 +76,12 @@ constexpr auto Ticks(Dur2 d)
74
76
  {
75
77
  return std::chrono::duration_cast<Dur1>(d).count();
76
78
  }
79
+
80
+ template <typename Duration>
81
+ constexpr int64_t TicksSeconds(Duration d)
82
+ {
83
+ return int64_t{Ticks<std::chrono::seconds>(d)};
84
+ }
77
85
  template <typename Duration, typename Timepoint>
78
86
  constexpr auto TicksSinceEpoch(Timepoint t)
79
87
  {
@@ -134,6 +142,7 @@ T GetTime()
134
142
  */
135
143
  std::string FormatISO8601DateTime(int64_t nTime);
136
144
  std::string FormatISO8601Date(int64_t nTime);
145
+ std::string FormatISO8601Time(int64_t nTime);
137
146
  std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
138
147
 
139
148
  /**
@@ -146,4 +155,46 @@ struct timeval MillisToTimeval(int64_t nTimeout);
146
155
  */
147
156
  struct timeval MillisToTimeval(std::chrono::milliseconds ms);
148
157
 
158
+ /**
159
+ * Retrieve the CPU time (user + system) spent by the current thread.
160
+ */
161
+ std::chrono::nanoseconds ThreadCpuTime();
162
+
163
+ /**
164
+ * Measure CPU time spent by the current thread.
165
+ * A clock is started when a CpuTimer is created. When the object is destroyed
166
+ * the elapsed CPU time is calculated and a callback function is invoked,
167
+ * providing it the elapsed CPU time.
168
+ */
169
+ class CpuTimer
170
+ {
171
+ public:
172
+ using FinishedCB = std::function<void(std::chrono::nanoseconds)>;
173
+
174
+ /**
175
+ * Construct a timer.
176
+ * @param[in] finished_cb A callback to invoke when this object is destroyed.
177
+ */
178
+ CpuTimer(const FinishedCB& finished_cb)
179
+ : m_start{ThreadCpuTime()},
180
+ m_finished_cb{finished_cb}
181
+ {
182
+ }
183
+
184
+ ~CpuTimer()
185
+ {
186
+ m_finished_cb(ThreadCpuTime() - m_start);
187
+ }
188
+
189
+ private:
190
+ const std::chrono::nanoseconds m_start;
191
+ FinishedCB m_finished_cb;
192
+ };
193
+
194
+ /**
195
+ * Add `b` nanoseconds to a nanoseconds atomic.
196
+ * @return The value of `a` immediately after the operation.
197
+ */
198
+ std::chrono::nanoseconds operator+=(std::atomic<std::chrono::nanoseconds>& a, std::chrono::nanoseconds b);
199
+
149
200
  #endif // BITCOIN_UTIL_TIME_H