blob: 40b72b0edd17691b894f318dcc6f80fb777feb70 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_TIME_H_
2#define AOS_COMMON_TIME_H_
3
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
brians343bc112013-02-10 01:53:46 +000014#include "aos/common/type_traits.h"
Brian Silvermanb407c672014-04-09 11:58:37 -070015#include "aos/common/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;
27 static constexpr bool is_steady = true;
28
29 // Returns the epoch (0).
30 static constexpr monotonic_clock::time_point epoch() {
31 return time_point(duration(0));
32 }
33};
34
brians343bc112013-02-10 01:53:46 +000035namespace time {
36
37// A nice structure for representing times.
38// 0 <= nsec_ < kNSecInSec should always be true. All functions here will make
39// sure that that is true if it was on all inputs (including *this).
40//
Brian Silverman6659bc32013-10-16 10:31:32 -070041// Negative times are supported so that all of the normal arithmetic identities
42// work. nsec_ is still always positive.
43//
brians343bc112013-02-10 01:53:46 +000044// The arithmetic and comparison operators are overloaded because they make
45// complete sense and are very useful. The default copy and assignment stuff is
Brian Silverman6659bc32013-10-16 10:31:32 -070046// left because it works fine. Multiplication of Times by Times is
brians343bc112013-02-10 01:53:46 +000047// not implemented because I can't think of any uses for them and there are
Brian Silverman6659bc32013-10-16 10:31:32 -070048// multiple ways to do it. Division of Times by Times is implemented as the
49// ratio of them. Multiplication, division, and modulus of Times by integers are
Brian Silverman0534df62014-05-26 21:19:15 -070050// implemented as interpreting the argument as nanoseconds. Modulus takes the
51// sign from the first operand.
brians343bc112013-02-10 01:53:46 +000052struct Time {
Brian Silverman52aeeac2013-08-28 16:20:53 -070053#ifdef SWIG
54// All of the uses of constexpr here can safely be simply removed.
55// NOTE: This means that relying on the fact that constexpr implicitly makes
Brian Silverman6659bc32013-10-16 10:31:32 -070056// member functions const is not valid, so they all have to be explicitly marked
57// const.
Brian Silverman52aeeac2013-08-28 16:20:53 -070058#define constexpr
59#endif // SWIG
brians343bc112013-02-10 01:53:46 +000060 public:
61 static const int32_t kNSecInSec = 1000000000;
62 static const int32_t kNSecInMSec = 1000000;
63 static const int32_t kNSecInUSec = 1000;
64 static const int32_t kMSecInSec = 1000;
65 static const int32_t kUSecInSec = 1000000;
Brian Silvermandcaa3f72015-11-29 05:32:08 +000066
67 static const Time kZero;
68
Brian Silverman92c3f1e2015-12-08 20:21:31 -050069 explicit constexpr Time(int32_t sec = 0, int32_t nsec = 0)
Brian Silverman52aeeac2013-08-28 16:20:53 -070070 : sec_(sec), nsec_(CheckConstexpr(nsec)) {
brians343bc112013-02-10 01:53:46 +000071 }
72 #ifndef SWIG
Brian Silverman52aeeac2013-08-28 16:20:53 -070073 explicit constexpr Time(const struct timespec &value)
74 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) {
brians343bc112013-02-10 01:53:46 +000075 }
76 struct timespec ToTimespec() const {
77 struct timespec ans;
78 ans.tv_sec = sec_;
79 ans.tv_nsec = nsec_;
80 return ans;
81 }
Brian Silverman52aeeac2013-08-28 16:20:53 -070082 explicit constexpr Time(const struct timeval &value)
83 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) {
brians57dd5822013-02-27 21:44:15 +000084 }
85 struct timeval ToTimeval() const {
86 struct timeval ans;
87 ans.tv_sec = sec_;
88 ans.tv_usec = nsec_ / kNSecInUSec;
89 return ans;
90 }
brians343bc112013-02-10 01:53:46 +000091 #endif // SWIG
Brian Silverman3204dd82013-03-12 18:42:01 -070092
Brian Silverman14fd0fb2014-01-14 21:42:01 -080093 // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the
brians343bc112013-02-10 01:53:46 +000094 // cRIO doesn't have any others.
95 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
96 // change when somebody changes the wall clock (like the ntp deamon or
97 // whatever). See clock_gettime(2) for details.
98 //
99 // This is the clock that code that just wants to sleep for a certain amount
100 // of time or measure how long something takes should use.
101 #ifndef __VXWORKS__
102 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
103 #else
104 static const clockid_t kDefaultClock = CLOCK_REALTIME;
105 #endif
106 // Creates a Time representing the current value of the specified clock or
107 // dies.
108 static Time Now(clockid_t clock = kDefaultClock);
109
110 // Constructs a Time representing seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700111 static constexpr Time InSeconds(double seconds) {
112 return (seconds < 0.0) ?
113 Time(static_cast<int32_t>(seconds) - 1,
114 (seconds - static_cast<int32_t>(seconds) + 1.0) * kNSecInSec) :
115 Time(static_cast<int32_t>(seconds),
116 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
brians343bc112013-02-10 01:53:46 +0000117 }
118
119 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700120 static constexpr Time InNS(int64_t nseconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000121 return (nseconds < 0)
122 ? Time((nseconds - 1) / static_cast<int64_t>(kNSecInSec) - 1,
123 (((nseconds - 1) % kNSecInSec) + 1) + kNSecInSec)
124 : Time(nseconds / static_cast<int64_t>(kNSecInSec),
125 nseconds % kNSecInSec);
brians343bc112013-02-10 01:53:46 +0000126 }
127
128 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700129 static constexpr Time InUS(int useconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000130 return (useconds < 0)
131 ? Time((useconds + 1) / kUSecInSec - 1,
132 (((useconds + 1) % kUSecInSec) - 1) * kNSecInUSec +
133 kNSecInSec)
134 : Time(useconds / kUSecInSec,
135 (useconds % kUSecInSec) * kNSecInUSec);
brians343bc112013-02-10 01:53:46 +0000136 }
137
138 // Constructs a time representing mseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700139 static constexpr Time InMS(int mseconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000140 return (mseconds < 0)
141 ? Time((mseconds + 1) / kMSecInSec - 1,
142 (((mseconds + 1) % kMSecInSec) - 1) * kNSecInMSec +
143 kNSecInSec)
144 : Time(mseconds / kMSecInSec,
145 (mseconds % kMSecInSec) * kNSecInMSec);
146 }
147
148 // Construct a time representing the period of hertz.
149 static constexpr Time FromRate(int hertz) {
150 return Time(0, kNSecInSec / hertz);
brians343bc112013-02-10 01:53:46 +0000151 }
152
153 // Checks whether or not this time is within amount nanoseconds of other.
154 bool IsWithin(const Time &other, int64_t amount) const;
155
156 // Returns the time represented all in nanoseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700157 int64_t constexpr ToNSec() const {
brians343bc112013-02-10 01:53:46 +0000158 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
159 static_cast<int64_t>(nsec_);
160 }
161#ifdef __VXWORKS__
162 // Returns the time represented all in system clock ticks. The system clock
163 // rate is retrieved using sysClkRateGet().
164 int ToTicks() const {
165 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
166 }
Brian Silverman3204dd82013-03-12 18:42:01 -0700167 // Constructs a Time representing ticks.
168 static Time InTicks(int ticks) {
Brian Silvermanffe3d712013-03-15 21:35:59 -0700169 return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet());
Brian Silverman3204dd82013-03-12 18:42:01 -0700170 }
brians343bc112013-02-10 01:53:46 +0000171#endif
172
173 // Returns the time represented in milliseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700174 int64_t constexpr ToMSec() const {
brians343bc112013-02-10 01:53:46 +0000175 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
176 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
177 }
178
Brian Silverman7645d2f2013-03-30 18:53:56 -0700179 // Returns the time represent in microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700180 int64_t constexpr ToUSec() const {
Brian Silverman7645d2f2013-03-30 18:53:56 -0700181 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) +
182 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec));
183 }
184
briansf0165ca2013-03-02 06:17:47 +0000185 // Returns the time represented in fractional seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700186 double constexpr ToSeconds() const {
briansf0165ca2013-03-02 06:17:47 +0000187 return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec;
188 }
189
brians343bc112013-02-10 01:53:46 +0000190 #ifndef SWIG
191 Time &operator+=(const Time &rhs);
192 Time &operator-=(const Time &rhs);
193 Time &operator*=(int32_t rhs);
194 Time &operator/=(int32_t rhs);
195 Time &operator%=(int32_t rhs);
Brian Silverman0079a9d2013-10-24 15:57:35 -0700196 Time &operator%=(double rhs) = delete;
197 Time &operator*=(double rhs) = delete;
198 Time &operator/=(double rhs) = delete;
199 const Time operator*(double rhs) const = delete;
200 const Time operator/(double rhs) const = delete;
201 const Time operator%(double rhs) const = delete;
brians343bc112013-02-10 01:53:46 +0000202 #endif
203 const Time operator+(const Time &rhs) const;
204 const Time operator-(const Time &rhs) const;
205 const Time operator*(int32_t rhs) const;
206 const Time operator/(int32_t rhs) const;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700207 double operator/(const Time &rhs) const;
brians343bc112013-02-10 01:53:46 +0000208 const Time operator%(int32_t rhs) const;
209
Brian Silverman0079a9d2013-10-24 15:57:35 -0700210 const Time operator-() const;
211
brians343bc112013-02-10 01:53:46 +0000212 bool operator==(const Time &rhs) const;
213 bool operator!=(const Time &rhs) const;
214 bool operator<(const Time &rhs) const;
215 bool operator>(const Time &rhs) const;
216 bool operator<=(const Time &rhs) const;
217 bool operator>=(const Time &rhs) const;
218
219 #ifndef SWIG
220 // For gtest etc.
221 friend std::ostream &operator<<(std::ostream &os, const Time &time);
222 #endif // SWIG
223
Brian Silverman52aeeac2013-08-28 16:20:53 -0700224 int32_t constexpr sec() const { return sec_; }
brians343bc112013-02-10 01:53:46 +0000225 void set_sec(int32_t sec) { sec_ = sec; }
Brian Silverman52aeeac2013-08-28 16:20:53 -0700226 int32_t constexpr nsec() const { return nsec_; }
brians343bc112013-02-10 01:53:46 +0000227 void set_nsec(int32_t nsec) {
228 nsec_ = nsec;
229 Check();
230 }
231
Brian Silverman3204dd82013-03-12 18:42:01 -0700232 // Absolute value.
233 Time abs() const {
234 if (*this > Time(0, 0)) return *this;
Brian Silvermanf3cbebd2013-03-19 16:53:18 -0700235 if (nsec_ == 0) return Time(-sec_, 0);
Brian Silverman3204dd82013-03-12 18:42:01 -0700236 return Time(-sec_ - 1, kNSecInSec - nsec_);
237 }
238
239 // Enables returning the mock time value for Now instead of checking the
Brian Silverman6da04272014-05-18 18:47:48 -0700240 // system clock.
Brian Silvermanb407c672014-04-09 11:58:37 -0700241 static void EnableMockTime(const Time &now = Now());
242 // Calls SetMockTime with the current actual time.
243 static void UpdateMockTime();
Brian Silverman3204dd82013-03-12 18:42:01 -0700244 // Sets now when time is being mocked.
Brian Silverman6659bc32013-10-16 10:31:32 -0700245 static void SetMockTime(const Time &now);
246 // Convenience function to just increment the mock time by a certain amount in
247 // a thread safe way.
248 static void IncrementMockTime(const Time &amount);
Brian Silverman3204dd82013-03-12 18:42:01 -0700249 // Disables mocking time.
250 static void DisableMockTime();
251
brians343bc112013-02-10 01:53:46 +0000252 private:
253 int32_t sec_, nsec_;
Brian Silverman52aeeac2013-08-28 16:20:53 -0700254
255 // LOG(FATAL)s if nsec is >= kNSecInSec or negative.
256 static void CheckImpl(int32_t nsec);
257 void Check() { CheckImpl(nsec_); }
258 // A constexpr version of CheckImpl that returns the given value when it
Brian Silverman6659bc32013-10-16 10:31:32 -0700259 // succeeds or evaluates to non-constexpr and returns 0 when it fails.
260 // This will result in the usual LOG(FATAL) if this is used where it isn't
261 // required to be constexpr or a compile error if it is.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700262 static constexpr int32_t CheckConstexpr(int32_t nsec) {
263 return (nsec >= kNSecInSec || nsec < 0) ? CheckImpl(nsec), 0 : nsec;
264 }
265
266#ifdef SWIG
267#undef constexpr
268#endif // SWIG
brians343bc112013-02-10 01:53:46 +0000269};
270
271// Sleeps for the amount of time represented by time counted by clock.
272void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
273// Sleeps until clock is at the time represented by time.
274void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
275
Brian Silvermand0575692015-02-21 16:24:02 -0500276// Sets the global offset for all times so ::aos::time::Time::Now() will return
277// now.
278// There is no synchronization here, so this is only safe when only a single
279// task is running.
280// This is only allowed when the shared memory core infrastructure has been
281// initialized in this process.
282void OffsetToNow(const Time &now);
283
Brian Silvermanb407c672014-04-09 11:58:37 -0700284// RAII class that freezes Time::Now() (to avoid making large numbers of
285// syscalls to find the real time).
286class TimeFreezer {
287 public:
288 TimeFreezer() {
289 Time::EnableMockTime();
290 }
291 ~TimeFreezer() {
292 Time::DisableMockTime();
293 }
294
295 private:
296 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
297};
298
brians343bc112013-02-10 01:53:46 +0000299} // namespace time
300} // namespace aos
301
Austin Schuh793d6b92016-05-01 13:28:14 -0700302namespace std {
303namespace this_thread {
304// Template specialization for monotonic_clock, since we can use clock_nanosleep
305// with TIMER_ABSTIME and get very precise absolute time sleeps.
306template <>
307void sleep_until(const ::aos::monotonic_clock::time_point &end_time);
308
309} // namespace this_thread
310} // namespace std
311
312
brians343bc112013-02-10 01:53:46 +0000313#endif // AOS_COMMON_TIME_H_