blob: a176ba056284cc4b42245f69bb2535570481e547 [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>
brians343bc112013-02-10 01:53:46 +000010#include <ostream>
11
brians343bc112013-02-10 01:53:46 +000012#include "aos/common/type_traits.h"
Brian Silvermanb407c672014-04-09 11:58:37 -070013#include "aos/common/macros.h"
brians343bc112013-02-10 01:53:46 +000014
15namespace aos {
16namespace time {
17
18// A nice structure for representing times.
19// 0 <= nsec_ < kNSecInSec should always be true. All functions here will make
20// sure that that is true if it was on all inputs (including *this).
21//
Brian Silverman6659bc32013-10-16 10:31:32 -070022// Negative times are supported so that all of the normal arithmetic identities
23// work. nsec_ is still always positive.
24//
brians343bc112013-02-10 01:53:46 +000025// The arithmetic and comparison operators are overloaded because they make
26// complete sense and are very useful. The default copy and assignment stuff is
Brian Silverman6659bc32013-10-16 10:31:32 -070027// left because it works fine. Multiplication of Times by Times is
brians343bc112013-02-10 01:53:46 +000028// not implemented because I can't think of any uses for them and there are
Brian Silverman6659bc32013-10-16 10:31:32 -070029// multiple ways to do it. Division of Times by Times is implemented as the
30// ratio of them. Multiplication, division, and modulus of Times by integers are
Brian Silverman0534df62014-05-26 21:19:15 -070031// implemented as interpreting the argument as nanoseconds. Modulus takes the
32// sign from the first operand.
brians343bc112013-02-10 01:53:46 +000033struct Time {
Brian Silverman52aeeac2013-08-28 16:20:53 -070034#ifdef SWIG
35// All of the uses of constexpr here can safely be simply removed.
36// NOTE: This means that relying on the fact that constexpr implicitly makes
Brian Silverman6659bc32013-10-16 10:31:32 -070037// member functions const is not valid, so they all have to be explicitly marked
38// const.
Brian Silverman52aeeac2013-08-28 16:20:53 -070039#define constexpr
40#endif // SWIG
brians343bc112013-02-10 01:53:46 +000041 public:
42 static const int32_t kNSecInSec = 1000000000;
43 static const int32_t kNSecInMSec = 1000000;
44 static const int32_t kNSecInUSec = 1000;
45 static const int32_t kMSecInSec = 1000;
46 static const int32_t kUSecInSec = 1000000;
Brian Silvermandcaa3f72015-11-29 05:32:08 +000047
48 static const Time kZero;
49
Brian Silverman92c3f1e2015-12-08 20:21:31 -050050 explicit constexpr Time(int32_t sec = 0, int32_t nsec = 0)
Brian Silverman52aeeac2013-08-28 16:20:53 -070051 : sec_(sec), nsec_(CheckConstexpr(nsec)) {
brians343bc112013-02-10 01:53:46 +000052 }
53 #ifndef SWIG
Brian Silverman52aeeac2013-08-28 16:20:53 -070054 explicit constexpr Time(const struct timespec &value)
55 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) {
brians343bc112013-02-10 01:53:46 +000056 }
57 struct timespec ToTimespec() const {
58 struct timespec ans;
59 ans.tv_sec = sec_;
60 ans.tv_nsec = nsec_;
61 return ans;
62 }
Brian Silverman52aeeac2013-08-28 16:20:53 -070063 explicit constexpr Time(const struct timeval &value)
64 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) {
brians57dd5822013-02-27 21:44:15 +000065 }
66 struct timeval ToTimeval() const {
67 struct timeval ans;
68 ans.tv_sec = sec_;
69 ans.tv_usec = nsec_ / kNSecInUSec;
70 return ans;
71 }
brians343bc112013-02-10 01:53:46 +000072 #endif // SWIG
Brian Silverman3204dd82013-03-12 18:42:01 -070073
Brian Silverman14fd0fb2014-01-14 21:42:01 -080074 // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the
brians343bc112013-02-10 01:53:46 +000075 // cRIO doesn't have any others.
76 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
77 // change when somebody changes the wall clock (like the ntp deamon or
78 // whatever). See clock_gettime(2) for details.
79 //
80 // This is the clock that code that just wants to sleep for a certain amount
81 // of time or measure how long something takes should use.
82 #ifndef __VXWORKS__
83 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
84 #else
85 static const clockid_t kDefaultClock = CLOCK_REALTIME;
86 #endif
87 // Creates a Time representing the current value of the specified clock or
88 // dies.
89 static Time Now(clockid_t clock = kDefaultClock);
90
91 // Constructs a Time representing seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -070092 static constexpr Time InSeconds(double seconds) {
93 return (seconds < 0.0) ?
94 Time(static_cast<int32_t>(seconds) - 1,
95 (seconds - static_cast<int32_t>(seconds) + 1.0) * kNSecInSec) :
96 Time(static_cast<int32_t>(seconds),
97 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
brians343bc112013-02-10 01:53:46 +000098 }
99
100 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700101 static constexpr Time InNS(int64_t nseconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000102 return (nseconds < 0)
103 ? Time((nseconds - 1) / static_cast<int64_t>(kNSecInSec) - 1,
104 (((nseconds - 1) % kNSecInSec) + 1) + kNSecInSec)
105 : Time(nseconds / static_cast<int64_t>(kNSecInSec),
106 nseconds % kNSecInSec);
brians343bc112013-02-10 01:53:46 +0000107 }
108
109 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700110 static constexpr Time InUS(int useconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000111 return (useconds < 0)
112 ? Time((useconds + 1) / kUSecInSec - 1,
113 (((useconds + 1) % kUSecInSec) - 1) * kNSecInUSec +
114 kNSecInSec)
115 : Time(useconds / kUSecInSec,
116 (useconds % kUSecInSec) * kNSecInUSec);
brians343bc112013-02-10 01:53:46 +0000117 }
118
119 // Constructs a time representing mseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700120 static constexpr Time InMS(int mseconds) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000121 return (mseconds < 0)
122 ? Time((mseconds + 1) / kMSecInSec - 1,
123 (((mseconds + 1) % kMSecInSec) - 1) * kNSecInMSec +
124 kNSecInSec)
125 : Time(mseconds / kMSecInSec,
126 (mseconds % kMSecInSec) * kNSecInMSec);
127 }
128
129 // Construct a time representing the period of hertz.
130 static constexpr Time FromRate(int hertz) {
131 return Time(0, kNSecInSec / hertz);
brians343bc112013-02-10 01:53:46 +0000132 }
133
134 // Checks whether or not this time is within amount nanoseconds of other.
135 bool IsWithin(const Time &other, int64_t amount) const;
136
137 // Returns the time represented all in nanoseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700138 int64_t constexpr ToNSec() const {
brians343bc112013-02-10 01:53:46 +0000139 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
140 static_cast<int64_t>(nsec_);
141 }
142#ifdef __VXWORKS__
143 // Returns the time represented all in system clock ticks. The system clock
144 // rate is retrieved using sysClkRateGet().
145 int ToTicks() const {
146 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
147 }
Brian Silverman3204dd82013-03-12 18:42:01 -0700148 // Constructs a Time representing ticks.
149 static Time InTicks(int ticks) {
Brian Silvermanffe3d712013-03-15 21:35:59 -0700150 return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet());
Brian Silverman3204dd82013-03-12 18:42:01 -0700151 }
brians343bc112013-02-10 01:53:46 +0000152#endif
153
154 // Returns the time represented in milliseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700155 int64_t constexpr ToMSec() const {
brians343bc112013-02-10 01:53:46 +0000156 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
157 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
158 }
159
Brian Silverman7645d2f2013-03-30 18:53:56 -0700160 // Returns the time represent in microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700161 int64_t constexpr ToUSec() const {
Brian Silverman7645d2f2013-03-30 18:53:56 -0700162 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) +
163 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec));
164 }
165
briansf0165ca2013-03-02 06:17:47 +0000166 // Returns the time represented in fractional seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700167 double constexpr ToSeconds() const {
briansf0165ca2013-03-02 06:17:47 +0000168 return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec;
169 }
170
brians343bc112013-02-10 01:53:46 +0000171 #ifndef SWIG
172 Time &operator+=(const Time &rhs);
173 Time &operator-=(const Time &rhs);
174 Time &operator*=(int32_t rhs);
175 Time &operator/=(int32_t rhs);
176 Time &operator%=(int32_t rhs);
Brian Silverman0079a9d2013-10-24 15:57:35 -0700177 Time &operator%=(double rhs) = delete;
178 Time &operator*=(double rhs) = delete;
179 Time &operator/=(double rhs) = delete;
180 const Time operator*(double rhs) const = delete;
181 const Time operator/(double rhs) const = delete;
182 const Time operator%(double rhs) const = delete;
brians343bc112013-02-10 01:53:46 +0000183 #endif
184 const Time operator+(const Time &rhs) const;
185 const Time operator-(const Time &rhs) const;
186 const Time operator*(int32_t rhs) const;
187 const Time operator/(int32_t rhs) const;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700188 double operator/(const Time &rhs) const;
brians343bc112013-02-10 01:53:46 +0000189 const Time operator%(int32_t rhs) const;
190
Brian Silverman0079a9d2013-10-24 15:57:35 -0700191 const Time operator-() const;
192
brians343bc112013-02-10 01:53:46 +0000193 bool operator==(const Time &rhs) const;
194 bool operator!=(const Time &rhs) const;
195 bool operator<(const Time &rhs) const;
196 bool operator>(const Time &rhs) const;
197 bool operator<=(const Time &rhs) const;
198 bool operator>=(const Time &rhs) const;
199
200 #ifndef SWIG
201 // For gtest etc.
202 friend std::ostream &operator<<(std::ostream &os, const Time &time);
203 #endif // SWIG
204
Brian Silverman52aeeac2013-08-28 16:20:53 -0700205 int32_t constexpr sec() const { return sec_; }
brians343bc112013-02-10 01:53:46 +0000206 void set_sec(int32_t sec) { sec_ = sec; }
Brian Silverman52aeeac2013-08-28 16:20:53 -0700207 int32_t constexpr nsec() const { return nsec_; }
brians343bc112013-02-10 01:53:46 +0000208 void set_nsec(int32_t nsec) {
209 nsec_ = nsec;
210 Check();
211 }
212
Brian Silverman3204dd82013-03-12 18:42:01 -0700213 // Absolute value.
214 Time abs() const {
215 if (*this > Time(0, 0)) return *this;
Brian Silvermanf3cbebd2013-03-19 16:53:18 -0700216 if (nsec_ == 0) return Time(-sec_, 0);
Brian Silverman3204dd82013-03-12 18:42:01 -0700217 return Time(-sec_ - 1, kNSecInSec - nsec_);
218 }
219
220 // Enables returning the mock time value for Now instead of checking the
Brian Silverman6da04272014-05-18 18:47:48 -0700221 // system clock.
Brian Silvermanb407c672014-04-09 11:58:37 -0700222 static void EnableMockTime(const Time &now = Now());
223 // Calls SetMockTime with the current actual time.
224 static void UpdateMockTime();
Brian Silverman3204dd82013-03-12 18:42:01 -0700225 // Sets now when time is being mocked.
Brian Silverman6659bc32013-10-16 10:31:32 -0700226 static void SetMockTime(const Time &now);
227 // Convenience function to just increment the mock time by a certain amount in
228 // a thread safe way.
229 static void IncrementMockTime(const Time &amount);
Brian Silverman3204dd82013-03-12 18:42:01 -0700230 // Disables mocking time.
231 static void DisableMockTime();
232
brians343bc112013-02-10 01:53:46 +0000233 private:
234 int32_t sec_, nsec_;
Brian Silverman52aeeac2013-08-28 16:20:53 -0700235
236 // LOG(FATAL)s if nsec is >= kNSecInSec or negative.
237 static void CheckImpl(int32_t nsec);
238 void Check() { CheckImpl(nsec_); }
239 // A constexpr version of CheckImpl that returns the given value when it
Brian Silverman6659bc32013-10-16 10:31:32 -0700240 // succeeds or evaluates to non-constexpr and returns 0 when it fails.
241 // This will result in the usual LOG(FATAL) if this is used where it isn't
242 // required to be constexpr or a compile error if it is.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700243 static constexpr int32_t CheckConstexpr(int32_t nsec) {
244 return (nsec >= kNSecInSec || nsec < 0) ? CheckImpl(nsec), 0 : nsec;
245 }
246
247#ifdef SWIG
248#undef constexpr
249#endif // SWIG
brians343bc112013-02-10 01:53:46 +0000250};
251
252// Sleeps for the amount of time represented by time counted by clock.
253void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
254// Sleeps until clock is at the time represented by time.
255void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
256
Brian Silvermand0575692015-02-21 16:24:02 -0500257// Sets the global offset for all times so ::aos::time::Time::Now() will return
258// now.
259// There is no synchronization here, so this is only safe when only a single
260// task is running.
261// This is only allowed when the shared memory core infrastructure has been
262// initialized in this process.
263void OffsetToNow(const Time &now);
264
Brian Silvermanb407c672014-04-09 11:58:37 -0700265// RAII class that freezes Time::Now() (to avoid making large numbers of
266// syscalls to find the real time).
267class TimeFreezer {
268 public:
269 TimeFreezer() {
270 Time::EnableMockTime();
271 }
272 ~TimeFreezer() {
273 Time::DisableMockTime();
274 }
275
276 private:
277 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
278};
279
brians343bc112013-02-10 01:53:46 +0000280} // namespace time
281} // namespace aos
282
283#endif // AOS_COMMON_TIME_H_