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