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