Skip to content

Commit

Permalink
Clocks: use CLOCK_[UPTIME|UPTIME_RAW|BOOTIME] when possible for Clock…
Browse files Browse the repository at this point in the history
…s::monotonic

GitOrigin-RevId: d0aeb9d98d54b298093cba3b2ca97c6ab16eba28
  • Loading branch information
arseny30 committed Aug 14, 2020
1 parent 28596f1 commit 775ef44
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tdutils/td/utils/port/Clocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ namespace td {

double Clocks::monotonic() {
// TODO write system specific functions, because std::chrono::steady_clock is steady only under Windows
#ifdef CLOCK_BOOTTIME
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_BOOTTIME, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_BOOTTIME, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
#ifdef CLOCK_UPTIME_RAW
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_UPTIME_RAW, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_UPTIME_RAW, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
#ifdef CLOCK_UPTIME
{
static bool skip = []() {
struct timespec spec;
return clock_gettime(CLOCK_UPTIME, &spec) != 0;
}();
struct timespec spec;
if (!skip && clock_gettime(CLOCK_UPTIME, &spec) == 0) {
return static_cast<double>(spec.tv_nsec) * 1e-9 + static_cast<double>(spec.tv_sec);
}
}
#endif
auto duration = std::chrono::steady_clock::now().time_since_epoch();
return static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()) * 1e-9;
}
Expand Down

0 comments on commit 775ef44

Please sign in to comment.