Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 3b244db

Browse files
committed
Switch to nanoseconds and use <chrono>
1 parent d65d06c commit 3b244db

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/config.h.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
/* Check whether C++11 atomic is available */
196196
#cmakedefine HAVE_CXX11_ATOMIC ${HAVE_CXX11_ATOMIC}
197197

198+
/* Check whether C++11 chrono is available */
199+
#cmakedefine HAVE_CXX11_CHRONO ${HAVE_CXX11_CHRONO}
200+
198201
/* Check whether C++11 nullptr_t is available */
199202
#cmakedefine HAVE_CXX11_NULLPTR_T ${HAVE_CXX11_NULLPTR_T}
200203

src/logging_unittest.cc

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,18 +1002,17 @@ namespace LogTimes {
10021002
// between total running time of 100ms and the period of 10ms. The period is
10031003
// large enough such that any CPU and OS scheduling variation shouldn't affect
10041004
// the results from the ideal case by more than 5% (500us or 0.5ms)
1005-
GLOG_CONSTEXPR double LOG_PERIOD_SEC = 0.01;
1006-
GLOG_CONSTEXPR int64_t LOG_PERIOD_US = 10000;
1007-
GLOG_CONSTEXPR int64_t LOG_PERIOD_TOL_US = 500;
1005+
GLOG_CONSTEXPR int64_t LOG_PERIOD_NS = 10000000; // 10ms
1006+
GLOG_CONSTEXPR int64_t LOG_PERIOD_TOL_NS = 500000; // 500us
10081007

10091008
// Set an upper limit for the number of times the stream operator can be
10101009
// called. Make sure not to exceed this number of times the stream operator is
10111010
// called, since it is also the array size and will be indexed by the stream
10121011
// operator.
1013-
GLOG_CONSTEXPR size_t MAX_CALLS = 10;
1012+
GLOG_CONSTEXPR size_t MAX_CALLS = 10;
10141013
} // namespace LogStreamTimes
10151014

1016-
#if HAVE_CXX11_CHRONO
1015+
#ifdef HAVE_CXX11_CHRONO
10171016
struct LogTimeRecorder {
10181017
size_t m_streamTimes = 0;
10191018
std::chrono::steady_clock::time_point m_callTimes[LogTimes::MAX_CALLS];
@@ -1025,10 +1024,10 @@ std::ostream& operator<<(std::ostream& stream, LogTimeRecorder& t) {
10251024
t.m_callTimes[t.m_streamTimes++] = std::chrono::steady_clock::now();
10261025
return stream;
10271026
}
1028-
// get elapsed time in microseconds
1029-
int64 elapsedTime_us(const std::chrono::steady_clock::time_point& begin,
1027+
// get elapsed time in nanoseconds
1028+
int64 elapsedTime_ns(const std::chrono::steady_clock::time_point& begin,
10301029
const std::chrono::steady_clock::time_point& end) {
1031-
return std::chrono::duration_cast<std::chrono::microseconds>((end - begin))
1030+
return std::chrono::duration_cast<std::chrono::nanoseconds>((end - begin))
10321031
.count();
10331032
}
10341033
#elif defined(OS_WINDOWS)
@@ -1040,11 +1039,11 @@ std::ostream& operator<<(std::ostream& stream, LogTimeRecorder& t) {
10401039
QueryPerformanceCounter(&t.m_callTimes[t.m_streamTimes++]);
10411040
return stream;
10421041
}
1043-
// get elapsed time in micrseconds
1044-
int64 elapsedTime_us(const LARGE_INTEGER& begin, const LARGE_INTEGER& end) {
1042+
// get elapsed time in nanoseconds
1043+
int64 elapsedTime_ns(const LARGE_INTEGER& begin, const LARGE_INTEGER& end) {
10451044
LARGE_INTEGER freq;
10461045
QueryPerformanceFrequency(&freq);
1047-
return (end.QuadPart - begin.QuadPart) * 1000000 / freq.QuadPart;
1046+
return (end.QuadPart - begin.QuadPart) * 1000000000 / freq.QuadPart;
10481047
}
10491048
#else
10501049
struct LogTimeRecorder {
@@ -1055,11 +1054,10 @@ std::ostream& operator<<(std::ostream& stream, LogTimeRecorder& t) {
10551054
clock_gettime(CLOCK_MONOTONIC, &t.m_callTimes[t.m_streamTimes++]);
10561055
return stream;
10571056
}
1058-
// get elapsed time in microseconds
1059-
int64 elapsedTime_us(const timespec& begin, const timespec& end) {
1060-
const int64 elapsed_ns = ((end.tv_sec - begin.tv_sec) * 1000000000) +
1061-
(end.tv_nsec - begin.tv_nsec);
1062-
return elapsed_ns / 1000;
1057+
// get elapsed time in nanoseconds
1058+
int64 elapsedTime_ns(const timespec& begin, const timespec& end) {
1059+
return (end.tv_sec - begin.tv_sec) * 1000000000 +
1060+
(end.tv_nsec - begin.tv_nsec);
10631061
}
10641062
#endif
10651063

@@ -1068,22 +1066,24 @@ static void TestLogPeriodically() {
10681066

10691067
LogTimeRecorder timeLogger;
10701068

1069+
GLOG_CONSTEXPR double LOG_PERIOD_SEC = LogTimes::LOG_PERIOD_NS / 1000000000.0;
1070+
10711071
while (timeLogger.m_streamTimes < LogTimes::MAX_CALLS) {
1072-
LOG_EVERY_T(INFO, LogTimes::LOG_PERIOD_SEC)
1072+
LOG_EVERY_T(INFO, LOG_PERIOD_SEC)
10731073
<< timeLogger << "Timed Message #" << timeLogger.m_streamTimes;
10741074
}
10751075

1076-
// Calculate time between each call in microseconds for higher resolution to
1076+
// Calculate time between each call in nanoseconds for higher resolution to
10771077
// minimize error.
1078-
int64 usBetweenCalls[LogTimes::MAX_CALLS - 1];
1078+
int64 nsBetweenCalls[LogTimes::MAX_CALLS - 1];
10791079
for (size_t i = 1; i < LogTimes::MAX_CALLS; ++i) {
1080-
usBetweenCalls[i - 1] = elapsedTime_us(
1080+
nsBetweenCalls[i - 1] = elapsedTime_ns(
10811081
timeLogger.m_callTimes[i - 1], timeLogger.m_callTimes[i]);
10821082
}
10831083

10841084
for (size_t idx = 0; idx < LogTimes::MAX_CALLS - 1; ++idx) {
1085-
int64 time = usBetweenCalls[idx];
1086-
EXPECT_NEAR(time, LogTimes::LOG_PERIOD_US, LogTimes::LOG_PERIOD_TOL_US);
1085+
int64 time_ns = nsBetweenCalls[idx];
1086+
EXPECT_NEAR(time_ns, LogTimes::LOG_PERIOD_NS, LogTimes::LOG_PERIOD_TOL_NS);
10871087
}
10881088
}
10891089

0 commit comments

Comments
 (0)