Skip to content
Bitcoin Roots

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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/util/time.h CHANGED
@@ -6,8 +6,10 @@
6
  #ifndef BITCOIN_UTIL_TIME_H
7
  #define BITCOIN_UTIL_TIME_H
8
 
 
9
  #include <chrono> // IWYU pragma: export
10
  #include <cstdint>
 
11
  #include <optional>
12
  #include <string>
13
  #include <string_view>
@@ -73,6 +75,12 @@ constexpr auto Ticks(Dur2 d)
73
  {
74
  return std::chrono::duration_cast<Dur1>(d).count();
75
  }
 
 
 
 
 
 
76
  template <typename Duration, typename Timepoint>
77
  constexpr auto TicksSinceEpoch(Timepoint t)
78
  {
@@ -132,6 +140,7 @@ T GetTime()
132
  */
133
  std::string FormatISO8601DateTime(int64_t nTime);
134
  std::string FormatISO8601Date(int64_t nTime);
 
135
  std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
136
 
137
  /**
@@ -144,4 +153,46 @@ struct timeval MillisToTimeval(int64_t nTimeout);
144
  */
145
  struct timeval MillisToTimeval(std::chrono::milliseconds ms);
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  #endif // BITCOIN_UTIL_TIME_H
 
6
  #ifndef BITCOIN_UTIL_TIME_H
7
  #define BITCOIN_UTIL_TIME_H
8
 
9
+ #include <atomic>
10
  #include <chrono> // IWYU pragma: export
11
  #include <cstdint>
12
+ #include <functional>
13
  #include <optional>
14
  #include <string>
15
  #include <string_view>
 
75
  {
76
  return std::chrono::duration_cast<Dur1>(d).count();
77
  }
78
+
79
+ template <typename Duration>
80
+ constexpr int64_t TicksSeconds(Duration d)
81
+ {
82
+ return int64_t{Ticks<std::chrono::seconds>(d)};
83
+ }
84
  template <typename Duration, typename Timepoint>
85
  constexpr auto TicksSinceEpoch(Timepoint t)
86
  {
 
140
  */
141
  std::string FormatISO8601DateTime(int64_t nTime);
142
  std::string FormatISO8601Date(int64_t nTime);
143
+ std::string FormatISO8601Time(int64_t nTime);
144
  std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
145
 
146
  /**
 
153
  */
154
  struct timeval MillisToTimeval(std::chrono::milliseconds ms);
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
+
198
  #endif // BITCOIN_UTIL_TIME_H