blob: 0440d91a45ea125c5b4abf3a67840e417cce7fa8 [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
31// implemented as interpreting the argument as nanoseconds.
brians343bc112013-02-10 01:53:46 +000032struct Time {
Brian Silverman52aeeac2013-08-28 16:20:53 -070033#ifdef SWIG
34// All of the uses of constexpr here can safely be simply removed.
35// NOTE: This means that relying on the fact that constexpr implicitly makes
Brian Silverman6659bc32013-10-16 10:31:32 -070036// member functions const is not valid, so they all have to be explicitly marked
37// const.
Brian Silverman52aeeac2013-08-28 16:20:53 -070038#define constexpr
39#endif // SWIG
brians343bc112013-02-10 01:53:46 +000040 public:
41 static const int32_t kNSecInSec = 1000000000;
42 static const int32_t kNSecInMSec = 1000000;
43 static const int32_t kNSecInUSec = 1000;
44 static const int32_t kMSecInSec = 1000;
45 static const int32_t kUSecInSec = 1000000;
Brian Silverman96e6d5a2014-03-24 15:55:40 -070046 constexpr Time(int32_t sec = 0, int32_t nsec = 0)
Brian Silverman52aeeac2013-08-28 16:20:53 -070047 : sec_(sec), nsec_(CheckConstexpr(nsec)) {
brians343bc112013-02-10 01:53:46 +000048 }
49 #ifndef SWIG
Brian Silverman52aeeac2013-08-28 16:20:53 -070050 explicit constexpr Time(const struct timespec &value)
51 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) {
brians343bc112013-02-10 01:53:46 +000052 }
53 struct timespec ToTimespec() const {
54 struct timespec ans;
55 ans.tv_sec = sec_;
56 ans.tv_nsec = nsec_;
57 return ans;
58 }
Brian Silverman52aeeac2013-08-28 16:20:53 -070059 explicit constexpr Time(const struct timeval &value)
60 : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) {
brians57dd5822013-02-27 21:44:15 +000061 }
62 struct timeval ToTimeval() const {
63 struct timeval ans;
64 ans.tv_sec = sec_;
65 ans.tv_usec = nsec_ / kNSecInUSec;
66 return ans;
67 }
brians343bc112013-02-10 01:53:46 +000068 #endif // SWIG
Brian Silverman3204dd82013-03-12 18:42:01 -070069
Brian Silverman14fd0fb2014-01-14 21:42:01 -080070 // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the
brians343bc112013-02-10 01:53:46 +000071 // cRIO doesn't have any others.
72 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
73 // change when somebody changes the wall clock (like the ntp deamon or
74 // whatever). See clock_gettime(2) for details.
75 //
76 // This is the clock that code that just wants to sleep for a certain amount
77 // of time or measure how long something takes should use.
78 #ifndef __VXWORKS__
79 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
80 #else
81 static const clockid_t kDefaultClock = CLOCK_REALTIME;
82 #endif
83 // Creates a Time representing the current value of the specified clock or
84 // dies.
85 static Time Now(clockid_t clock = kDefaultClock);
86
87 // Constructs a Time representing seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -070088 static constexpr Time InSeconds(double seconds) {
89 return (seconds < 0.0) ?
90 Time(static_cast<int32_t>(seconds) - 1,
91 (seconds - static_cast<int32_t>(seconds) + 1.0) * kNSecInSec) :
92 Time(static_cast<int32_t>(seconds),
93 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
brians343bc112013-02-10 01:53:46 +000094 }
95
96 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -070097 static constexpr Time InNS(int64_t nseconds) {
Brian Silverman6659bc32013-10-16 10:31:32 -070098 return (nseconds < 0) ?
99 Time(nseconds / static_cast<int64_t>(kNSecInSec) - 1,
100 (nseconds % kNSecInSec) + kNSecInSec) :
101 Time(nseconds / static_cast<int64_t>(kNSecInSec),
102 nseconds % kNSecInSec);
brians343bc112013-02-10 01:53:46 +0000103 }
104
105 // Constructs a time representing microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700106 static constexpr Time InUS(int useconds) {
107 return (useconds < 0) ?
108 Time(useconds / kUSecInSec - 1,
109 (useconds % kUSecInSec) * kNSecInUSec + kNSecInSec) :
110 Time(useconds / kUSecInSec, (useconds % kUSecInSec) * kNSecInUSec);
brians343bc112013-02-10 01:53:46 +0000111 }
112
113 // Constructs a time representing mseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700114 static constexpr Time InMS(int mseconds) {
Brian Silverman6659bc32013-10-16 10:31:32 -0700115 return (mseconds < 0) ?
116 Time(mseconds / kMSecInSec - 1,
117 (mseconds % kMSecInSec) * kNSecInMSec + kNSecInSec) :
118 Time(mseconds / kMSecInSec, (mseconds % kMSecInSec) * kNSecInMSec);
brians343bc112013-02-10 01:53:46 +0000119 }
120
121 // Checks whether or not this time is within amount nanoseconds of other.
122 bool IsWithin(const Time &other, int64_t amount) const;
123
124 // Returns the time represented all in nanoseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700125 int64_t constexpr ToNSec() const {
brians343bc112013-02-10 01:53:46 +0000126 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
127 static_cast<int64_t>(nsec_);
128 }
129#ifdef __VXWORKS__
130 // Returns the time represented all in system clock ticks. The system clock
131 // rate is retrieved using sysClkRateGet().
132 int ToTicks() const {
133 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
134 }
Brian Silverman3204dd82013-03-12 18:42:01 -0700135 // Constructs a Time representing ticks.
136 static Time InTicks(int ticks) {
Brian Silvermanffe3d712013-03-15 21:35:59 -0700137 return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet());
Brian Silverman3204dd82013-03-12 18:42:01 -0700138 }
brians343bc112013-02-10 01:53:46 +0000139#endif
140
141 // Returns the time represented in milliseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700142 int64_t constexpr ToMSec() const {
brians343bc112013-02-10 01:53:46 +0000143 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
144 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
145 }
146
Brian Silverman7645d2f2013-03-30 18:53:56 -0700147 // Returns the time represent in microseconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700148 int64_t constexpr ToUSec() const {
Brian Silverman7645d2f2013-03-30 18:53:56 -0700149 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) +
150 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec));
151 }
152
briansf0165ca2013-03-02 06:17:47 +0000153 // Returns the time represented in fractional seconds.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700154 double constexpr ToSeconds() const {
briansf0165ca2013-03-02 06:17:47 +0000155 return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec;
156 }
157
brians343bc112013-02-10 01:53:46 +0000158 #ifndef SWIG
159 Time &operator+=(const Time &rhs);
160 Time &operator-=(const Time &rhs);
161 Time &operator*=(int32_t rhs);
162 Time &operator/=(int32_t rhs);
163 Time &operator%=(int32_t rhs);
Brian Silverman0079a9d2013-10-24 15:57:35 -0700164 Time &operator%=(double rhs) = delete;
165 Time &operator*=(double rhs) = delete;
166 Time &operator/=(double rhs) = delete;
167 const Time operator*(double rhs) const = delete;
168 const Time operator/(double rhs) const = delete;
169 const Time operator%(double rhs) const = delete;
brians343bc112013-02-10 01:53:46 +0000170 #endif
171 const Time operator+(const Time &rhs) const;
172 const Time operator-(const Time &rhs) const;
173 const Time operator*(int32_t rhs) const;
174 const Time operator/(int32_t rhs) const;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700175 double operator/(const Time &rhs) const;
brians343bc112013-02-10 01:53:46 +0000176 const Time operator%(int32_t rhs) const;
177
Brian Silverman0079a9d2013-10-24 15:57:35 -0700178 const Time operator-() const;
179
brians343bc112013-02-10 01:53:46 +0000180 bool operator==(const Time &rhs) const;
181 bool operator!=(const Time &rhs) const;
182 bool operator<(const Time &rhs) const;
183 bool operator>(const Time &rhs) const;
184 bool operator<=(const Time &rhs) const;
185 bool operator>=(const Time &rhs) const;
186
187 #ifndef SWIG
188 // For gtest etc.
189 friend std::ostream &operator<<(std::ostream &os, const Time &time);
190 #endif // SWIG
191
Brian Silverman52aeeac2013-08-28 16:20:53 -0700192 int32_t constexpr sec() const { return sec_; }
brians343bc112013-02-10 01:53:46 +0000193 void set_sec(int32_t sec) { sec_ = sec; }
Brian Silverman52aeeac2013-08-28 16:20:53 -0700194 int32_t constexpr nsec() const { return nsec_; }
brians343bc112013-02-10 01:53:46 +0000195 void set_nsec(int32_t nsec) {
196 nsec_ = nsec;
197 Check();
198 }
199
Brian Silverman3204dd82013-03-12 18:42:01 -0700200 // Absolute value.
201 Time abs() const {
202 if (*this > Time(0, 0)) return *this;
Brian Silvermanf3cbebd2013-03-19 16:53:18 -0700203 if (nsec_ == 0) return Time(-sec_, 0);
Brian Silverman3204dd82013-03-12 18:42:01 -0700204 return Time(-sec_ - 1, kNSecInSec - nsec_);
205 }
206
207 // Enables returning the mock time value for Now instead of checking the
Brian Silverman6da04272014-05-18 18:47:48 -0700208 // system clock.
Brian Silvermanb407c672014-04-09 11:58:37 -0700209 static void EnableMockTime(const Time &now = Now());
210 // Calls SetMockTime with the current actual time.
211 static void UpdateMockTime();
Brian Silverman3204dd82013-03-12 18:42:01 -0700212 // Sets now when time is being mocked.
Brian Silverman6659bc32013-10-16 10:31:32 -0700213 static void SetMockTime(const Time &now);
214 // Convenience function to just increment the mock time by a certain amount in
215 // a thread safe way.
216 static void IncrementMockTime(const Time &amount);
Brian Silverman3204dd82013-03-12 18:42:01 -0700217 // Disables mocking time.
218 static void DisableMockTime();
219
brians343bc112013-02-10 01:53:46 +0000220 private:
221 int32_t sec_, nsec_;
Brian Silverman52aeeac2013-08-28 16:20:53 -0700222
223 // LOG(FATAL)s if nsec is >= kNSecInSec or negative.
224 static void CheckImpl(int32_t nsec);
225 void Check() { CheckImpl(nsec_); }
226 // A constexpr version of CheckImpl that returns the given value when it
Brian Silverman6659bc32013-10-16 10:31:32 -0700227 // succeeds or evaluates to non-constexpr and returns 0 when it fails.
228 // This will result in the usual LOG(FATAL) if this is used where it isn't
229 // required to be constexpr or a compile error if it is.
Brian Silverman52aeeac2013-08-28 16:20:53 -0700230 static constexpr int32_t CheckConstexpr(int32_t nsec) {
231 return (nsec >= kNSecInSec || nsec < 0) ? CheckImpl(nsec), 0 : nsec;
232 }
233
234#ifdef SWIG
235#undef constexpr
236#endif // SWIG
brians343bc112013-02-10 01:53:46 +0000237};
238
239// Sleeps for the amount of time represented by time counted by clock.
240void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
241// Sleeps until clock is at the time represented by time.
242void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
243
Brian Silvermanb407c672014-04-09 11:58:37 -0700244// RAII class that freezes Time::Now() (to avoid making large numbers of
245// syscalls to find the real time).
246class TimeFreezer {
247 public:
248 TimeFreezer() {
249 Time::EnableMockTime();
250 }
251 ~TimeFreezer() {
252 Time::DisableMockTime();
253 }
254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
257};
258
brians343bc112013-02-10 01:53:46 +0000259} // namespace time
260} // namespace aos
261
262#endif // AOS_COMMON_TIME_H_