John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_TIME_H_ |
| 2 | #define AOS_TIME_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <time.h> |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 6 | #include <sys/time.h> |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <type_traits> |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <thread> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include <ostream> |
| 13 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 14 | #include "aos/type_traits/type_traits.h" |
| 15 | #include "aos/macros.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 18 | |
| 19 | class monotonic_clock { |
| 20 | public: |
| 21 | typedef ::std::chrono::nanoseconds::rep rep; |
| 22 | typedef ::std::chrono::nanoseconds::period period; |
| 23 | typedef ::std::chrono::nanoseconds duration; |
| 24 | typedef ::std::chrono::time_point<monotonic_clock> time_point; |
| 25 | |
| 26 | static monotonic_clock::time_point now() noexcept; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame^] | 27 | // This clock is still subject to rate adjustments based on adjtime, so it is |
| 28 | // not steady. |
| 29 | static constexpr bool is_steady = false; |
| 30 | |
| 31 | // Returns the epoch (0). |
| 32 | static constexpr monotonic_clock::time_point epoch() { |
| 33 | return time_point(zero()); |
| 34 | } |
| 35 | |
| 36 | static constexpr monotonic_clock::duration zero() { return duration(0); } |
| 37 | |
| 38 | static constexpr time_point min_time{ |
| 39 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
| 40 | }; |
| 41 | |
| 42 | class realtime_clock { |
| 43 | public: |
| 44 | typedef ::std::chrono::nanoseconds::rep rep; |
| 45 | typedef ::std::chrono::nanoseconds::period period; |
| 46 | typedef ::std::chrono::nanoseconds duration; |
| 47 | typedef ::std::chrono::time_point<monotonic_clock> time_point; |
| 48 | |
| 49 | #ifdef __linux__ |
| 50 | static monotonic_clock::time_point now() noexcept; |
| 51 | #endif // __linux__ |
| 52 | static constexpr bool is_steady = false; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 53 | |
| 54 | // Returns the epoch (0). |
| 55 | static constexpr monotonic_clock::time_point epoch() { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 56 | return time_point(zero()); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 57 | } |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 58 | |
| 59 | static constexpr monotonic_clock::duration zero() { return duration(0); } |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 60 | |
| 61 | static constexpr time_point min_time{ |
| 62 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 65 | namespace time { |
| 66 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 67 | #ifdef __linux__ |
| 68 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 69 | // Enables returning the mock time value for Now instead of checking the system |
| 70 | // clock. |
| 71 | void EnableMockTime(monotonic_clock::time_point now); |
| 72 | // Calls SetMockTime with the current actual time. |
| 73 | void UpdateMockTime(); |
| 74 | // Sets now when time is being mocked. |
| 75 | void SetMockTime(monotonic_clock::time_point now); |
| 76 | // Convenience function to just increment the mock time by a certain amount in |
| 77 | // a thread safe way. |
| 78 | void IncrementMockTime(monotonic_clock::duration amount); |
| 79 | // Disables mocking time. |
| 80 | void DisableMockTime(); |
| 81 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 82 | // Sets the global offset for all times so monotonic_clock::now() will return |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 83 | // now. |
| 84 | // There is no synchronization here, so this is only safe when only a single |
| 85 | // task is running. |
| 86 | // This is only allowed when the shared memory core infrastructure has been |
| 87 | // initialized in this process. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 88 | void OffsetToNow(const monotonic_clock::time_point now); |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 89 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 90 | // Construct a time representing the period of hertz. |
| 91 | constexpr ::std::chrono::nanoseconds FromRate(int hertz) { |
| 92 | return ::std::chrono::duration_cast<::std::chrono::nanoseconds>( |
| 93 | ::std::chrono::seconds(1)) / |
| 94 | hertz; |
| 95 | } |
| 96 | |
| 97 | // RAII class that freezes monotonic_clock::now() (to avoid making large numbers |
| 98 | // of syscalls to find the real time). |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 99 | class TimeFreezer { |
| 100 | public: |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 101 | TimeFreezer() { EnableMockTime(monotonic_clock::now()); } |
| 102 | ~TimeFreezer() { DisableMockTime(); } |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | DISALLOW_COPY_AND_ASSIGN(TimeFreezer); |
| 106 | }; |
| 107 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 108 | #endif // __linux__ |
| 109 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 110 | // Converts a monotonic_clock::duration into a timespec object. |
| 111 | struct timespec to_timespec(::aos::monotonic_clock::duration duration); |
| 112 | |
| 113 | // Converts a monotonic_clock::time_point into a timespec object as time since |
| 114 | // epoch. |
| 115 | struct timespec to_timespec(::aos::monotonic_clock::time_point time); |
| 116 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 117 | namespace time_internal { |
| 118 | |
| 119 | template <class T> |
| 120 | struct is_duration : std::false_type {}; |
| 121 | template <class Rep, class Period> |
| 122 | struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {}; |
| 123 | |
| 124 | } // namespace time_internal |
| 125 | |
| 126 | // Returns the greatest duration t representable in ToDuration that is less or |
| 127 | // equal to d. |
| 128 | // Implementation copied from |
| 129 | // https://en.cppreference.com/w/cpp/chrono/duration/floor. |
| 130 | // TODO(Brian): Remove once we have C++17 support. |
| 131 | template <class To, class Rep, class Period, |
| 132 | class = std::enable_if_t<time_internal::is_duration<To>{}>> |
| 133 | constexpr To floor(const std::chrono::duration<Rep, Period> &d) { |
| 134 | To t = std::chrono::duration_cast<To>(d); |
| 135 | if (t > d) return t - To{1}; |
| 136 | return t; |
| 137 | } |
| 138 | |
| 139 | // Returns the value t representable in ToDuration that is the closest to d. If |
| 140 | // there are two such values, returns the even value (that is, the value t such |
| 141 | // that t % 2 == 0). |
| 142 | // Implementation copied from |
| 143 | // https://en.cppreference.com/w/cpp/chrono/duration/round. |
| 144 | // TODO(Brian): Remove once we have C++17 support. |
| 145 | template <class To, class Rep, class Period, |
| 146 | class = std::enable_if_t< |
| 147 | time_internal::is_duration<To>{} && |
| 148 | !std::chrono::treat_as_floating_point<typename To::rep>{}>> |
| 149 | constexpr To round(const std::chrono::duration<Rep, Period> &d) { |
| 150 | To t0 = aos::time::floor<To>(d); |
| 151 | To t1 = t0 + To{1}; |
| 152 | auto diff0 = d - t0; |
| 153 | auto diff1 = t1 - d; |
| 154 | if (diff0 == diff1) { |
| 155 | if (t0.count() & 1) return t1; |
| 156 | return t0; |
| 157 | } else if (diff0 < diff1) { |
| 158 | return t0; |
| 159 | } |
| 160 | return t1; |
| 161 | } |
| 162 | |
| 163 | // Returns the nearest time point to tp representable in ToDuration, rounding to |
| 164 | // even in halfway cases, like std::chrono::round in C++17. |
| 165 | // Implementation copied from |
| 166 | // https://en.cppreference.com/w/cpp/chrono/time_point/round. |
| 167 | // TODO(Brian): Remove once we have C++17 support. |
| 168 | template <class To, class Clock, class FromDuration, |
| 169 | class = std::enable_if_t< |
| 170 | time_internal::is_duration<To>{} && |
| 171 | !std::chrono::treat_as_floating_point<typename To::rep>{}>> |
| 172 | constexpr std::chrono::time_point<Clock, To> round( |
| 173 | const std::chrono::time_point<Clock, FromDuration> &tp) { |
| 174 | return std::chrono::time_point<Clock, To>{ |
| 175 | aos::time::round<To>(tp.time_since_epoch())}; |
| 176 | } |
| 177 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 178 | } // namespace time |
| 179 | } // namespace aos |
| 180 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 181 | #ifdef __linux__ |
| 182 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 183 | namespace std { |
| 184 | namespace this_thread { |
| 185 | // Template specialization for monotonic_clock, since we can use clock_nanosleep |
| 186 | // with TIMER_ABSTIME and get very precise absolute time sleeps. |
| 187 | template <> |
| 188 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time); |
| 189 | |
| 190 | } // namespace this_thread |
| 191 | } // namespace std |
| 192 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 193 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 194 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 195 | #endif // AOS_TIME_H_ |