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