brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_TIME_H_ |
| 2 | #define AOS_COMMON_TIME_H_ |
| 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> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include <ostream> |
| 11 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include "aos/common/type_traits.h" |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 13 | #include "aos/common/macros.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | namespace 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 22 | // Negative times are supported so that all of the normal arithmetic identities |
| 23 | // work. nsec_ is still always positive. |
| 24 | // |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | // 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 27 | // left because it works fine. Multiplication of Times by Times is |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 28 | // not implemented because I can't think of any uses for them and there are |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 29 | // 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 Silverman | 0534df6 | 2014-05-26 21:19:15 -0700 | [diff] [blame] | 31 | // implemented as interpreting the argument as nanoseconds. Modulus takes the |
| 32 | // sign from the first operand. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | struct Time { |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 34 | #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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 37 | // member functions const is not valid, so they all have to be explicitly marked |
| 38 | // const. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 39 | #define constexpr |
| 40 | #endif // SWIG |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | 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 Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 47 | |
| 48 | static const Time kZero; |
| 49 | |
Brian Silverman | 92c3f1e | 2015-12-08 20:21:31 -0500 | [diff] [blame^] | 50 | explicit constexpr Time(int32_t sec = 0, int32_t nsec = 0) |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 51 | : sec_(sec), nsec_(CheckConstexpr(nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 52 | } |
| 53 | #ifndef SWIG |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 54 | explicit constexpr Time(const struct timespec &value) |
| 55 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | } |
| 57 | struct timespec ToTimespec() const { |
| 58 | struct timespec ans; |
| 59 | ans.tv_sec = sec_; |
| 60 | ans.tv_nsec = nsec_; |
| 61 | return ans; |
| 62 | } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 63 | explicit constexpr Time(const struct timeval &value) |
| 64 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) { |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 65 | } |
| 66 | struct timeval ToTimeval() const { |
| 67 | struct timeval ans; |
| 68 | ans.tv_sec = sec_; |
| 69 | ans.tv_usec = nsec_ / kNSecInUSec; |
| 70 | return ans; |
| 71 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 72 | #endif // SWIG |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 73 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 74 | // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 75 | // 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 92 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 101 | static constexpr Time InNS(int64_t nseconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 102 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 110 | static constexpr Time InUS(int useconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 111 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Constructs a time representing mseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 120 | static constexpr Time InMS(int mseconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 121 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | } |
| 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 138 | int64_t constexpr ToNSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 139 | 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 Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 148 | // Constructs a Time representing ticks. |
| 149 | static Time InTicks(int ticks) { |
Brian Silverman | ffe3d71 | 2013-03-15 21:35:59 -0700 | [diff] [blame] | 150 | return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet()); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 151 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | // Returns the time represented in milliseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 155 | int64_t constexpr ToMSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 156 | 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 Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 160 | // Returns the time represent in microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 161 | int64_t constexpr ToUSec() const { |
Brian Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 162 | return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) + |
| 163 | (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec)); |
| 164 | } |
| 165 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 166 | // Returns the time represented in fractional seconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 167 | double constexpr ToSeconds() const { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 168 | return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec; |
| 169 | } |
| 170 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 171 | #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 Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 177 | 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; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 183 | #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 Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 188 | double operator/(const Time &rhs) const; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 189 | const Time operator%(int32_t rhs) const; |
| 190 | |
Brian Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 191 | const Time operator-() const; |
| 192 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 193 | 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 205 | int32_t constexpr sec() const { return sec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 206 | void set_sec(int32_t sec) { sec_ = sec; } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 207 | int32_t constexpr nsec() const { return nsec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 208 | void set_nsec(int32_t nsec) { |
| 209 | nsec_ = nsec; |
| 210 | Check(); |
| 211 | } |
| 212 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 213 | // Absolute value. |
| 214 | Time abs() const { |
| 215 | if (*this > Time(0, 0)) return *this; |
Brian Silverman | f3cbebd | 2013-03-19 16:53:18 -0700 | [diff] [blame] | 216 | if (nsec_ == 0) return Time(-sec_, 0); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 217 | return Time(-sec_ - 1, kNSecInSec - nsec_); |
| 218 | } |
| 219 | |
| 220 | // Enables returning the mock time value for Now instead of checking the |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 221 | // system clock. |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 222 | static void EnableMockTime(const Time &now = Now()); |
| 223 | // Calls SetMockTime with the current actual time. |
| 224 | static void UpdateMockTime(); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 225 | // Sets now when time is being mocked. |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 226 | 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 Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 230 | // Disables mocking time. |
| 231 | static void DisableMockTime(); |
| 232 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 233 | private: |
| 234 | int32_t sec_, nsec_; |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 235 | |
| 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 240 | // 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 243 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | // Sleeps for the amount of time represented by time counted by clock. |
| 253 | void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 254 | // Sleeps until clock is at the time represented by time. |
| 255 | void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 256 | |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 257 | // 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. |
| 263 | void OffsetToNow(const Time &now); |
| 264 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 265 | // RAII class that freezes Time::Now() (to avoid making large numbers of |
| 266 | // syscalls to find the real time). |
| 267 | class 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 280 | } // namespace time |
| 281 | } // namespace aos |
| 282 | |
| 283 | #endif // AOS_COMMON_TIME_H_ |