blob: 136629c4a36ab1a4a66555b90431e73667f56bef [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_TIME_H_
2#define AOS_TIME_H_
brians343bc112013-02-10 01:53:46 +00003
4#include <stdint.h>
5#include <time.h>
brians57dd5822013-02-27 21:44:15 +00006#include <sys/time.h>
Brian4a424a22014-04-02 11:52:45 -07007#include <stdint.h>
8
9#include <type_traits>
Austin Schuh793d6b92016-05-01 13:28:14 -070010#include <chrono>
11#include <thread>
brians343bc112013-02-10 01:53:46 +000012#include <ostream>
13
John Park33858a32018-09-28 23:05:48 -070014#include "aos/type_traits/type_traits.h"
15#include "aos/macros.h"
brians343bc112013-02-10 01:53:46 +000016
17namespace aos {
Austin Schuh793d6b92016-05-01 13:28:14 -070018
19class 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 Silvermana3688802019-02-16 19:31:26 -080027 // 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
42class 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 Schuh793d6b92016-05-01 13:28:14 -070053
54 // Returns the epoch (0).
55 static constexpr monotonic_clock::time_point epoch() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -070056 return time_point(zero());
Austin Schuh793d6b92016-05-01 13:28:14 -070057 }
Austin Schuh8aec1ed2016-05-01 13:29:20 -070058
59 static constexpr monotonic_clock::duration zero() { return duration(0); }
Austin Schuh858c0212016-11-25 17:23:30 -080060
61 static constexpr time_point min_time{
62 time_point(duration(::std::numeric_limits<duration::rep>::min()))};
Austin Schuh793d6b92016-05-01 13:28:14 -070063};
64
brians343bc112013-02-10 01:53:46 +000065namespace time {
66
Brian Silvermanc03a30c2019-02-16 18:21:56 -080067#ifdef __linux__
68
Austin Schuh6a6f90c2016-11-25 21:36:42 -080069// Enables returning the mock time value for Now instead of checking the system
70// clock.
71void EnableMockTime(monotonic_clock::time_point now);
72// Calls SetMockTime with the current actual time.
73void UpdateMockTime();
74// Sets now when time is being mocked.
75void 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.
78void IncrementMockTime(monotonic_clock::duration amount);
79// Disables mocking time.
80void DisableMockTime();
81
Austin Schuhf2a50ba2016-12-24 16:16:26 -080082// Sets the global offset for all times so monotonic_clock::now() will return
Brian Silvermand0575692015-02-21 16:24:02 -050083// 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 Schuhf2a50ba2016-12-24 16:16:26 -080088void OffsetToNow(const monotonic_clock::time_point now);
Brian Silvermand0575692015-02-21 16:24:02 -050089
Austin Schuhf2a50ba2016-12-24 16:16:26 -080090// Construct a time representing the period of hertz.
91constexpr ::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 Silvermanb407c672014-04-09 11:58:37 -070099class TimeFreezer {
100 public:
Austin Schuh6a6f90c2016-11-25 21:36:42 -0800101 TimeFreezer() { EnableMockTime(monotonic_clock::now()); }
102 ~TimeFreezer() { DisableMockTime(); }
Brian Silvermanb407c672014-04-09 11:58:37 -0700103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
106};
107
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800108#endif // __linux__
109
Neil Balch229001a2018-01-07 18:22:52 -0800110// Converts a monotonic_clock::duration into a timespec object.
111struct 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.
115struct timespec to_timespec(::aos::monotonic_clock::time_point time);
116
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800117namespace time_internal {
118
119template <class T>
120struct is_duration : std::false_type {};
121template <class Rep, class Period>
122struct 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.
131template <class To, class Rep, class Period,
132 class = std::enable_if_t<time_internal::is_duration<To>{}>>
133constexpr 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.
145template <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>{}>>
149constexpr 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.
168template <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>{}>>
172constexpr 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
brians343bc112013-02-10 01:53:46 +0000178} // namespace time
179} // namespace aos
180
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800181#ifdef __linux__
182
Austin Schuh793d6b92016-05-01 13:28:14 -0700183namespace std {
184namespace 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.
187template <>
188void sleep_until(const ::aos::monotonic_clock::time_point &end_time);
189
190} // namespace this_thread
191} // namespace std
192
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800193#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -0700194
John Park33858a32018-09-28 23:05:48 -0700195#endif // AOS_TIME_H_