brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/time.h" |
| 2 | |
| 3 | #ifdef __VXWORKS__ |
| 4 | #include <taskLib.h> |
| 5 | #endif |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
| 9 | #include "aos/common/logging/logging.h" |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 10 | #include <inttypes.h> |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 11 | #include "aos/common/mutex.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace time { |
| 15 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 16 | // State required to enable and use mock time. |
| 17 | namespace { |
| 18 | // True if mock time is enabled. |
| 19 | // This does not need to be checked with the mutex held because setting time to |
| 20 | // be enabled or disabled is atomic, and all future operations are atomic |
| 21 | // anyways. If there is a race condition setting or clearing whether time is |
| 22 | // enabled or not, it will still be a race condition if current_mock_time is |
| 23 | // also set atomically with enabled. |
| 24 | bool mock_time_enabled = false; |
| 25 | // Mutex to make time reads and writes thread safe. |
| 26 | Mutex time_mutex; |
| 27 | // Current time when time is mocked. |
| 28 | Time current_mock_time(0, 0); |
| 29 | |
| 30 | // TODO(aschuh): This doesn't include SleepFor and SleepUntil. |
| 31 | // TODO(aschuh): Create a clock source object and change the default? |
| 32 | // That would let me create a MockTime clock source. |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 33 | |
| 34 | Time NowImpl(clockid_t clock) { |
| 35 | timespec temp; |
| 36 | if (clock_gettime(clock, &temp) != 0) { |
| 37 | LOG(FATAL, "clock_gettime(%jd, %p) failed with %d: %s\n", |
| 38 | static_cast<uintmax_t>(clock), &temp, errno, strerror(errno)); |
| 39 | } |
| 40 | return Time(temp); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 42 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 43 | } // namespace |
| 44 | |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 45 | void Time::EnableMockTime(const Time &now) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 46 | MutexLocker time_mutex_locker(&time_mutex); |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 47 | mock_time_enabled = true; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 48 | current_mock_time = now; |
| 49 | } |
| 50 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 51 | void Time::UpdateMockTime() { |
| 52 | SetMockTime(NowImpl(kDefaultClock)); |
| 53 | } |
| 54 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 55 | void Time::DisableMockTime() { |
| 56 | MutexLocker time_mutex_locker(&time_mutex); |
| 57 | mock_time_enabled = false; |
| 58 | } |
| 59 | |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 60 | void Time::SetMockTime(const Time &now) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 61 | MutexLocker time_mutex_locker(&time_mutex); |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 62 | if (__builtin_expect(!mock_time_enabled, 0)) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 63 | LOG(FATAL, "Tried to set mock time and mock time is not enabled\n"); |
| 64 | } |
| 65 | current_mock_time = now; |
| 66 | } |
| 67 | |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 68 | void Time::IncrementMockTime(const Time &amount) { |
| 69 | static ::aos::Mutex mutex; |
| 70 | ::aos::MutexLocker sync(&mutex); |
| 71 | SetMockTime(Now() + amount); |
| 72 | } |
| 73 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 74 | Time Time::Now(clockid_t clock) { |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 75 | { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 76 | MutexLocker time_mutex_locker(&time_mutex); |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 77 | if (mock_time_enabled) { |
| 78 | return current_mock_time; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 79 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 80 | } |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame^] | 81 | return NowImpl(clock); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 84 | void Time::CheckImpl(int32_t nsec) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | static_assert(aos::shm_ok<Time>::value, |
| 86 | "it should be able to go through shared memory"); |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 87 | if (nsec >= kNSecInSec || nsec < 0) { |
| 88 | LOG(FATAL, "0 <= nsec(%" PRId32 ") < %" PRId32 " isn't true.\n", |
| 89 | nsec, kNSecInSec); |
| 90 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Time &Time::operator+=(const Time &rhs) { |
| 94 | sec_ += rhs.sec_; |
| 95 | nsec_ += rhs.nsec_; |
| 96 | if (nsec_ >= kNSecInSec) { |
| 97 | nsec_ -= kNSecInSec; |
| 98 | sec_ += 1; |
| 99 | } |
| 100 | return *this; |
| 101 | } |
| 102 | const Time Time::operator+(const Time &rhs) const { |
| 103 | return Time(*this) += rhs; |
| 104 | } |
| 105 | Time &Time::operator-=(const Time &rhs) { |
| 106 | sec_ -= rhs.sec_; |
| 107 | nsec_ -= rhs.nsec_; |
| 108 | if (nsec_ < 0) { |
| 109 | nsec_ += kNSecInSec; |
| 110 | sec_ -= 1; |
| 111 | } |
| 112 | return *this; |
| 113 | } |
| 114 | const Time Time::operator-(const Time &rhs) const { |
| 115 | return Time(*this) -= rhs; |
| 116 | } |
| 117 | Time &Time::operator*=(int32_t rhs) { |
| 118 | const int64_t temp = static_cast<int64_t>(nsec_) * |
| 119 | static_cast<int64_t>(rhs); |
| 120 | sec_ *= rhs; // better not overflow, or the result is just too big |
| 121 | nsec_ = temp % kNSecInSec; |
| 122 | sec_ += (temp - nsec_) / kNSecInSec; |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 123 | if (nsec_ < 0) { |
| 124 | nsec_ += kNSecInSec; |
| 125 | sec_ -= 1; |
| 126 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 127 | return *this; |
| 128 | } |
| 129 | const Time Time::operator*(int32_t rhs) const { |
| 130 | return Time(*this) *= rhs; |
| 131 | } |
| 132 | Time &Time::operator/=(int32_t rhs) { |
| 133 | nsec_ = (sec_ % rhs) * (kNSecInSec / rhs) + nsec_ / rhs; |
| 134 | sec_ /= rhs; |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 135 | if (nsec_ < 0) { |
| 136 | nsec_ += kNSecInSec; |
| 137 | sec_ -= 1; |
| 138 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 139 | return *this; |
| 140 | } |
| 141 | const Time Time::operator/(int32_t rhs) const { |
| 142 | return Time(*this) /= rhs; |
| 143 | } |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 144 | double Time::operator/(const Time &rhs) const { |
| 145 | return ToSeconds() / rhs.ToSeconds(); |
| 146 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | Time &Time::operator%=(int32_t rhs) { |
| 148 | nsec_ = ToNSec() % rhs; |
| 149 | const int wraps = nsec_ / kNSecInSec; |
| 150 | sec_ = wraps; |
| 151 | nsec_ -= kNSecInSec * wraps; |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 152 | if (nsec_ < 0) { |
| 153 | nsec_ += kNSecInSec; |
| 154 | sec_ -= 1; |
| 155 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 156 | return *this; |
| 157 | } |
| 158 | const Time Time::operator%(int32_t rhs) const { |
| 159 | return Time(*this) %= rhs; |
| 160 | } |
| 161 | |
Brian Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 162 | const Time Time::operator-() const { |
| 163 | return Time(-sec_ - 1, kNSecInSec - nsec_); |
| 164 | } |
| 165 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 166 | bool Time::operator==(const Time &rhs) const { |
| 167 | return sec_ == rhs.sec_ && nsec_ == rhs.nsec_; |
| 168 | } |
| 169 | bool Time::operator!=(const Time &rhs) const { |
| 170 | return !(*this == rhs); |
| 171 | } |
| 172 | bool Time::operator<(const Time &rhs) const { |
| 173 | return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ < rhs.nsec_); |
| 174 | } |
| 175 | bool Time::operator>(const Time &rhs) const { |
| 176 | return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ > rhs.nsec_); |
| 177 | } |
| 178 | bool Time::operator<=(const Time &rhs) const { |
| 179 | return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ <= rhs.nsec_); |
| 180 | } |
| 181 | bool Time::operator>=(const Time &rhs) const { |
| 182 | return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ >= rhs.nsec_); |
| 183 | } |
| 184 | |
| 185 | bool Time::IsWithin(const Time &other, int64_t amount) const { |
| 186 | const int64_t temp = ToNSec() - other.ToNSec(); |
| 187 | return temp <= amount && temp >= -amount; |
| 188 | } |
| 189 | |
| 190 | std::ostream &operator<<(std::ostream &os, const Time& time) { |
| 191 | return os << "Time{" << time.sec_ << "s, " << time.nsec_ << "ns}"; |
| 192 | } |
| 193 | |
| 194 | void SleepFor(const Time &time, clockid_t clock) { |
| 195 | #ifdef __VXWORKS__ |
| 196 | SleepUntil(Time::Now(clock) + time, clock); |
| 197 | #else |
| 198 | timespec converted(time.ToTimespec()), remaining; |
| 199 | int failure = EINTR; |
| 200 | do { |
| 201 | // This checks whether the last time through the loop actually failed or got |
| 202 | // interrupted. |
| 203 | if (failure != EINTR) { |
| 204 | LOG(FATAL, "clock_nanosleep(%jd, 0, %p, %p) returned %d: %s\n", |
| 205 | static_cast<intmax_t>(clock), &converted, &remaining, |
| 206 | failure, strerror(failure)); |
| 207 | } |
| 208 | failure = clock_nanosleep(clock, 0, &converted, &remaining); |
| 209 | memcpy(&converted, &remaining, sizeof(converted)); |
| 210 | } while (failure != 0); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | void SleepUntil(const Time &time, clockid_t clock) { |
| 215 | #ifdef __VXWORKS__ |
| 216 | if (clock != CLOCK_REALTIME) { |
| 217 | LOG(FATAL, "vxworks only supports CLOCK_REALTIME\n"); |
| 218 | } |
| 219 | // Vxworks nanosleep is definitely broken (fails horribly at doing remaining |
| 220 | // right), and I don't really want to know how else it's broken, so I'm using |
| 221 | // taskDelay instead because that's simpler. |
| 222 | // The +1 is because sleep functions are supposed to sleep for at least the |
| 223 | // requested amount, so we have to round up to the next clock tick. |
| 224 | while (taskDelay((time - Time::Now(clock)).ToTicks() + 1) != 0) { |
| 225 | if (errno != EINTR) { |
| 226 | LOG(FATAL, "taskDelay(some ticks) failed with %d: %s\n", |
| 227 | errno, strerror(errno)); |
| 228 | } |
| 229 | } |
| 230 | #else |
| 231 | timespec converted(time.ToTimespec()); |
| 232 | int failure; |
| 233 | while ((failure = clock_nanosleep(clock, TIMER_ABSTIME, |
| 234 | &converted, NULL)) != 0) { |
| 235 | if (failure != EINTR) { |
| 236 | LOG(FATAL, "clock_nanosleep(%jd, TIMER_ABSTIME, %p, NULL)" |
| 237 | " returned %d: %s\n", static_cast<intmax_t>(clock), &converted, |
| 238 | failure, strerror(failure)); |
| 239 | } |
| 240 | } |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | } // namespace time |
| 245 | } // namespace aos |