brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/time.h" |
| 2 | |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 3 | #include <inttypes.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 4 | #include <string.h> |
| 5 | |
| 6 | #include <atomic> |
| 7 | #include <chrono> |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 8 | |
| 9 | // We only use global_core from here, which is weak, so we don't really have a |
| 10 | // dependency on it. |
| 11 | #include "aos/linux_code/ipc_lib/shared_mem.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
| 13 | #include "aos/common/logging/logging.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 14 | #include "aos/common/mutex.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 16 | namespace chrono = ::std::chrono; |
| 17 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 18 | namespace std { |
| 19 | namespace this_thread { |
| 20 | template <> |
| 21 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time) { |
| 22 | struct timespec end_time_timespec; |
| 23 | ::std::chrono::seconds sec = |
| 24 | ::std::chrono::duration_cast<::std::chrono::seconds>( |
| 25 | end_time.time_since_epoch()); |
| 26 | ::std::chrono::nanoseconds nsec = |
| 27 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>( |
| 28 | end_time.time_since_epoch() - sec); |
| 29 | end_time_timespec.tv_sec = sec.count(); |
| 30 | end_time_timespec.tv_nsec = nsec.count(); |
| 31 | int returnval; |
| 32 | do { |
| 33 | returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, |
| 34 | &end_time_timespec, nullptr); |
| 35 | if (returnval != EINTR && returnval != 0) { |
| 36 | PLOG(FATAL, "clock_nanosleep(%jd, TIMER_ABSTIME, %p, nullptr) failed", |
| 37 | static_cast<uintmax_t>(CLOCK_MONOTONIC), &end_time_timespec); |
| 38 | } |
| 39 | } while (returnval != 0); |
| 40 | } |
| 41 | |
| 42 | } // namespace this_thread |
| 43 | } // namespace std |
| 44 | |
| 45 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 46 | namespace aos { |
| 47 | namespace time { |
| 48 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 49 | // State required to enable and use mock time. |
| 50 | namespace { |
| 51 | // True if mock time is enabled. |
| 52 | // This does not need to be checked with the mutex held because setting time to |
| 53 | // be enabled or disabled is atomic, and all future operations are atomic |
| 54 | // anyways. If there is a race condition setting or clearing whether time is |
| 55 | // enabled or not, it will still be a race condition if current_mock_time is |
| 56 | // also set atomically with enabled. |
Brian Silverman | 0308f16 | 2016-01-02 13:32:49 -0800 | [diff] [blame] | 57 | ::std::atomic<bool> mock_time_enabled{false}; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 58 | // Mutex to make time reads and writes thread safe. |
| 59 | Mutex time_mutex; |
| 60 | // Current time when time is mocked. |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 61 | monotonic_clock::time_point current_mock_time = monotonic_clock::epoch(); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 62 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 63 | } // namespace |
| 64 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 65 | void EnableMockTime(monotonic_clock::time_point 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 | mock_time_enabled = true; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 68 | current_mock_time = now; |
| 69 | } |
| 70 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 71 | void UpdateMockTime() { SetMockTime(monotonic_clock::now()); } |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 72 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 73 | void DisableMockTime() { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 74 | MutexLocker time_mutex_locker(&time_mutex); |
| 75 | mock_time_enabled = false; |
| 76 | } |
| 77 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 78 | void SetMockTime(monotonic_clock::time_point now) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 79 | MutexLocker time_mutex_locker(&time_mutex); |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 80 | if (__builtin_expect(!mock_time_enabled, 0)) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 81 | LOG(FATAL, "Tried to set mock time and mock time is not enabled\n"); |
| 82 | } |
| 83 | current_mock_time = now; |
| 84 | } |
| 85 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 86 | void IncrementMockTime(monotonic_clock::duration amount) { |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 87 | static ::aos::Mutex mutex; |
| 88 | ::aos::MutexLocker sync(&mutex); |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 89 | SetMockTime(monotonic_clock::now() + amount); |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 92 | void OffsetToNow(monotonic_clock::time_point now) { |
Brian Silverman | 0c715e6 | 2015-10-24 16:49:46 -0400 | [diff] [blame] | 93 | CHECK_NOTNULL(&global_core); |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 94 | CHECK_NOTNULL(global_core); |
Brian Silverman | 0c715e6 | 2015-10-24 16:49:46 -0400 | [diff] [blame] | 95 | CHECK_NOTNULL(global_core->mem_struct); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 96 | const auto offset = now - monotonic_clock::now(); |
| 97 | global_core->mem_struct->time_offset = |
| 98 | chrono::duration_cast<chrono::nanoseconds>(offset).count(); |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 101 | struct timespec to_timespec( |
| 102 | const ::aos::monotonic_clock::duration duration) { |
| 103 | struct timespec time_timespec; |
| 104 | ::std::chrono::seconds sec = |
| 105 | ::std::chrono::duration_cast<::std::chrono::seconds>(duration); |
| 106 | ::std::chrono::nanoseconds nsec = |
| 107 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec); |
| 108 | time_timespec.tv_sec = sec.count(); |
| 109 | time_timespec.tv_nsec = nsec.count(); |
| 110 | return time_timespec; |
| 111 | } |
| 112 | |
| 113 | struct timespec to_timespec( |
| 114 | const ::aos::monotonic_clock::time_point time) { |
| 115 | return to_timespec(time.time_since_epoch()); |
| 116 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 118 | |
| 119 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
| 120 | |
| 121 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
| 122 | { |
| 123 | if (time::mock_time_enabled.load(::std::memory_order_relaxed)) { |
| 124 | MutexLocker time_mutex_locker(&time::time_mutex); |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 125 | return time::current_mock_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | struct timespec current_time; |
| 130 | if (clock_gettime(CLOCK_MONOTONIC, ¤t_time) != 0) { |
| 131 | PLOG(FATAL, "clock_gettime(%jd, %p) failed", |
| 132 | static_cast<uintmax_t>(CLOCK_MONOTONIC), ¤t_time); |
| 133 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 134 | const chrono::nanoseconds offset = |
| 135 | (&global_core == nullptr || global_core == nullptr || |
| 136 | global_core->mem_struct == nullptr) |
| 137 | ? chrono::nanoseconds(0) |
| 138 | : chrono::nanoseconds(global_core->mem_struct->time_offset); |
| 139 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 140 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 141 | ::std::chrono::nanoseconds(current_time.tv_nsec)) + offset; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 145 | } // namespace aos |