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> |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <thread> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include <ostream> |
| 13 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | #include "aos/common/type_traits.h" |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 15 | #include "aos/common/macros.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 18 | |
| 19 | class 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 35 | namespace 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 41 | // Negative times are supported so that all of the normal arithmetic identities |
| 42 | // work. nsec_ is still always positive. |
| 43 | // |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 44 | // 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 46 | // left because it works fine. Multiplication of Times by Times is |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 47 | // 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] | 48 | // 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 Silverman | 0534df6 | 2014-05-26 21:19:15 -0700 | [diff] [blame] | 50 | // implemented as interpreting the argument as nanoseconds. Modulus takes the |
| 51 | // sign from the first operand. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 52 | struct Time { |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 53 | #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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 56 | // member functions const is not valid, so they all have to be explicitly marked |
| 57 | // const. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 58 | #define constexpr |
| 59 | #endif // SWIG |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 60 | 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 Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 66 | |
| 67 | static const Time kZero; |
| 68 | |
Brian Silverman | 92c3f1e | 2015-12-08 20:21:31 -0500 | [diff] [blame] | 69 | explicit constexpr Time(int32_t sec = 0, int32_t nsec = 0) |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 70 | : sec_(sec), nsec_(CheckConstexpr(nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 71 | } |
| 72 | #ifndef SWIG |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 73 | explicit constexpr Time(const struct timespec &value) |
| 74 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 75 | } |
| 76 | struct timespec ToTimespec() const { |
| 77 | struct timespec ans; |
| 78 | ans.tv_sec = sec_; |
| 79 | ans.tv_nsec = nsec_; |
| 80 | return ans; |
| 81 | } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 82 | explicit constexpr Time(const struct timeval &value) |
| 83 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) { |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 84 | } |
| 85 | struct timeval ToTimeval() const { |
| 86 | struct timeval ans; |
| 87 | ans.tv_sec = sec_; |
| 88 | ans.tv_usec = nsec_ / kNSecInUSec; |
| 89 | return ans; |
| 90 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | #endif // SWIG |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 92 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 93 | // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 94 | // 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 111 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 120 | static constexpr Time InNS(int64_t nseconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 121 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 129 | static constexpr Time InUS(int useconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 130 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Constructs a time representing mseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 139 | static constexpr Time InMS(int mseconds) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 140 | 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 151 | } |
| 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 157 | int64_t constexpr ToNSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 158 | 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 Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 167 | // Constructs a Time representing ticks. |
| 168 | static Time InTicks(int ticks) { |
Brian Silverman | ffe3d71 | 2013-03-15 21:35:59 -0700 | [diff] [blame] | 169 | return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet()); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 170 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
| 173 | // Returns the time represented in milliseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 174 | int64_t constexpr ToMSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 175 | 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 Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 179 | // Returns the time represent in microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 180 | int64_t constexpr ToUSec() const { |
Brian Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 181 | return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) + |
| 182 | (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec)); |
| 183 | } |
| 184 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 185 | // Returns the time represented in fractional seconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 186 | double constexpr ToSeconds() const { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 187 | return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec; |
| 188 | } |
| 189 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 190 | #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 Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 196 | 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; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 202 | #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 Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 207 | double operator/(const Time &rhs) const; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 208 | const Time operator%(int32_t rhs) const; |
| 209 | |
Brian Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 210 | const Time operator-() const; |
| 211 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 212 | 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 224 | int32_t constexpr sec() const { return sec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 225 | void set_sec(int32_t sec) { sec_ = sec; } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 226 | int32_t constexpr nsec() const { return nsec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 227 | void set_nsec(int32_t nsec) { |
| 228 | nsec_ = nsec; |
| 229 | Check(); |
| 230 | } |
| 231 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 232 | // Absolute value. |
| 233 | Time abs() const { |
| 234 | if (*this > Time(0, 0)) return *this; |
Brian Silverman | f3cbebd | 2013-03-19 16:53:18 -0700 | [diff] [blame] | 235 | if (nsec_ == 0) return Time(-sec_, 0); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 236 | return Time(-sec_ - 1, kNSecInSec - nsec_); |
| 237 | } |
| 238 | |
| 239 | // Enables returning the mock time value for Now instead of checking the |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 240 | // system clock. |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 241 | static void EnableMockTime(const Time &now = Now()); |
| 242 | // Calls SetMockTime with the current actual time. |
| 243 | static void UpdateMockTime(); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 244 | // Sets now when time is being mocked. |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 245 | 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 Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 249 | // Disables mocking time. |
| 250 | static void DisableMockTime(); |
| 251 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 252 | private: |
| 253 | int32_t sec_, nsec_; |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 254 | |
| 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 Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 259 | // 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 Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 262 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | // Sleeps for the amount of time represented by time counted by clock. |
| 272 | void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 273 | // Sleeps until clock is at the time represented by time. |
| 274 | void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 275 | |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 276 | // 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. |
| 282 | void OffsetToNow(const Time &now); |
| 283 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 284 | // RAII class that freezes Time::Now() (to avoid making large numbers of |
| 285 | // syscalls to find the real time). |
| 286 | class 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 299 | } // namespace time |
| 300 | } // namespace aos |
| 301 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 302 | namespace std { |
| 303 | namespace 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. |
| 306 | template <> |
| 307 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time); |
| 308 | |
| 309 | } // namespace this_thread |
| 310 | } // namespace std |
| 311 | |
| 312 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 313 | #endif // AOS_COMMON_TIME_H_ |