From fca21e83b0f39264ba4d2b6e4051289082b5efd5 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 2 Mar 2020 12:16:15 -0600 Subject: [PATCH] aux/util: Provide a conversion function from CLOCK_MONOTONIC nanoseconds to adjusted time. --- src/xrt/auxiliary/util/u_time.cpp | 35 ++++++++++++++++++++++++++++++- src/xrt/auxiliary/util/u_time.h | 22 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/xrt/auxiliary/util/u_time.cpp b/src/xrt/auxiliary/util/u_time.cpp index 89499f3b2..b5ed2eea9 100644 --- a/src/xrt/auxiliary/util/u_time.cpp +++ b/src/xrt/auxiliary/util/u_time.cpp @@ -1,4 +1,4 @@ -// Copyright 2019, Collabora, Ltd. +// Copyright 2019-2020, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -8,20 +8,37 @@ */ #include "u_time.h" +#include "xrt/xrt_config.h" +#include "xrt/xrt_compiler.h" #include #include #include #include +#include + +#ifdef XRT_OS_LINUX +#include +#endif using namespace std::chrono; struct MatchingTimePoints { + MatchingTimePoints() + { +#ifdef XRT_OS_LINUX + clock_gettime(CLOCK_MONOTONIC, &clock_monotonic); +#endif + } system_clock::time_point sys = system_clock::now(); steady_clock::time_point steady = steady_clock::now(); // high_resolution_clock::time_point highRes; +#ifdef XRT_OS_LINUX + struct timespec clock_monotonic; +#endif + timepoint_ns getTimestamp(time_state const &prevState); }; @@ -134,3 +151,19 @@ time_state_from_timespec(struct time_state const *state, return state->lastTime + duration_cast(sinceLastUpdate).count(); } + + +timepoint_ns +time_state_from_monotonic_ns(struct time_state const *state, + uint64_t monotonic_ns) +{ + assert(state != NULL); + auto sinceLastUpdate = + seconds{state->lastTimePoints.clock_monotonic.tv_sec} + + nanoseconds{state->lastTimePoints.clock_monotonic.tv_nsec} - + nanoseconds{monotonic_ns}; + + // Offset the last timestamp by that duration. + return state->lastTime + + duration_cast(sinceLastUpdate).count(); +} diff --git a/src/xrt/auxiliary/util/u_time.h b/src/xrt/auxiliary/util/u_time.h index 2dda2041e..24628ae9b 100644 --- a/src/xrt/auxiliary/util/u_time.h +++ b/src/xrt/auxiliary/util/u_time.h @@ -1,9 +1,14 @@ -// Copyright 2019, Collabora, Ltd. +// Copyright 2019-2020, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file * @brief Time-keeping: a clock that is steady, convertible to system time, and * ideally high-resolution. + * + * Designed to suit the needs of OpenXR: you can and should use something + * simpler (like @ref aux_os_time) for most purposes that aren't in OpenXR + * interface code. + * * @author Ryan Pavlik * @ingroup aux_util * @@ -146,6 +151,21 @@ timepoint_ns time_state_from_timespec(struct time_state const *state, const struct timespec *timespecTime); +/*! + * Convert a monotonic system time (such as from @ref aux_os_time) to an + * adjusted integer timestamp. + * + * Adjustments may need to be applied to achieve the other guarantees that e.g. + * CLOCK_MONOTONIC does not provide: this function performs those adjustments. + * + * Should not be called simultaneously with time_state_get_now_and_update. + * + * @public @memberof time_state + * @ingroup aux_util + */ +timepoint_ns +time_state_from_monotonic_ns(struct time_state const *state, + uint64_t monotonic_ns); #ifdef __cplusplus } #endif