v31.1-roots.1 · Commit 5: [PATCH 05/17] feat(net): preserve Roots peer controls and health view on v31
src/util/time.cpp
Before: src/util/time.cpp · After: src/util/time.cpp
File metadata
index 9e0715e52509dd9226ce1e7200d2eedfebef4cf7..df9f5ca759133cab245eee94e0d29c3a214a18c6 100644
Download original patch · Release notes
Diff rows 1–56 of 56. 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.cpp
CHANGED
|
@@ -18,6 +18,13 @@
|
|
|
18
18
|
#include <string_view>
|
|
19
19
|
#include <thread>
|
|
20
20
|
|
|
21
|
+
#ifdef WIN32
|
|
22
|
+
#include <windows.h>
|
|
23
|
+
#include <processthreadsapi.h>
|
|
24
|
+
#else
|
|
25
|
+
#include <ctime>
|
|
26
|
+
#endif
|
|
27
|
+
|
|
21
28
|
static constexpr std::array<std::string_view, 7> weekdays{"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; // 1970-01-01 was a Thursday.
|
|
22
29
|
static constexpr std::array<std::string_view, 12> months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
23
30
|
|
|
@@ -151,3 +158,43 @@ struct timeval MillisToTimeval(std::chrono::milliseconds ms)
|
|
|
151
158
|
{
|
|
152
159
|
return MillisToTimeval(count_milliseconds(ms));
|
|
153
160
|
}
|
|
161
|
+
|
|
162
|
+
std::chrono::nanoseconds ThreadCpuTime()
|
|
163
|
+
{
|
|
164
|
+
#ifdef CLOCK_THREAD_CPUTIME_ID
|
|
165
|
+
timespec t;
|
|
166
|
+
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) == -1) {
|
|
167
|
+
return std::chrono::nanoseconds{0};
|
|
168
|
+
}
|
|
169
|
+
return std::chrono::seconds{t.tv_sec} + std::chrono::nanoseconds{t.tv_nsec};
|
|
170
|
+
#elif defined(WIN32)
|
|
171
|
+
FILETIME creation;
|
|
172
|
+
FILETIME exit;
|
|
173
|
+
FILETIME kernel;
|
|
174
|
+
FILETIME user;
|
|
175
|
+
// GetThreadTimes():
|
|
176
|
+
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
|
|
177
|
+
if (GetThreadTimes(GetCurrentThread(), &creation, &exit, &kernel, &user) == 0) {
|
|
178
|
+
return std::chrono::nanoseconds{0};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
|
|
182
|
+
// "... you should copy the low- and high-order parts of the file time to a
|
|
183
|
+
// ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart
|
|
184
|
+
// member ..."
|
|
185
|
+
|
|
186
|
+
ULARGE_INTEGER kernel_;
|
|
187
|
+
kernel_.LowPart = kernel.dwLowDateTime;
|
|
188
|
+
kernel_.HighPart = kernel.dwHighDateTime;
|
|
189
|
+
|
|
190
|
+
ULARGE_INTEGER user_;
|
|
191
|
+
user_.LowPart = user.dwLowDateTime;
|
|
192
|
+
user_.HighPart = user.dwHighDateTime;
|
|
193
|
+
|
|
194
|
+
// The units of the returned values from GetThreadTimes() are "100-nanosecond periods".
|
|
195
|
+
// So, we multiply by 100 to get nanoseconds.
|
|
196
|
+
return std::chrono::nanoseconds{(kernel_.QuadPart + user_.QuadPart) * 100};
|
|
197
|
+
#else
|
|
198
|
+
return std::chrono::nanoseconds{0};
|
|
199
|
+
#endif
|
|
200
|
+
}
|