John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/time/time.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
Stephan Pleines | cd3701f | 2024-05-30 10:56:04 -0700 | [diff] [blame] | 3 | #include <ctype.h> |
| 4 | #include <errno.h> |
| 5 | #include <sys/time.h> |
| 6 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 8 | #include <chrono> |
Stephan Pleines | cd3701f | 2024-05-30 10:56:04 -0700 | [diff] [blame] | 9 | #include <compare> |
| 10 | #include <cstdint> |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 11 | #include <ctime> |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 12 | #include <iomanip> |
Stephan Pleines | cd3701f | 2024-05-30 10:56:04 -0700 | [diff] [blame] | 13 | #include <ratio> |
Eric Schmiedeberg | 42273f4 | 2023-12-14 13:48:45 -0700 | [diff] [blame] | 14 | #include <sstream> |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 15 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 16 | #ifdef __linux__ |
| 17 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 18 | #include "absl/log/check.h" |
| 19 | #include "absl/log/log.h" |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 20 | #include "absl/strings/numbers.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 22 | #else // __linux__ |
| 23 | |
| 24 | #include "motors/core/kinetis.h" |
| 25 | |
| 26 | // The systick interrupt increments this every 1ms. |
| 27 | extern "C" volatile uint32_t systick_millis_count; |
| 28 | |
| 29 | #endif // __linux__ |
| 30 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 31 | namespace chrono = ::std::chrono; |
| 32 | |
Philipp Schrader | a7878b9 | 2023-10-16 17:33:40 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | void PrintToStream(std::ostream &stream, chrono::nanoseconds duration) { |
| 36 | chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(duration); |
| 37 | if (duration < chrono::nanoseconds(0)) { |
| 38 | stream << "-" << -seconds.count() << "." << std::setfill('0') |
| 39 | << std::setw(9) |
| 40 | << chrono::duration_cast<chrono::nanoseconds>(seconds - duration) |
| 41 | .count() |
| 42 | << "sec"; |
| 43 | } else { |
| 44 | stream << seconds.count() << "." << std::setfill('0') << std::setw(9) |
| 45 | << chrono::duration_cast<chrono::nanoseconds>(duration - seconds) |
| 46 | .count() |
| 47 | << "sec"; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 53 | #ifdef __linux__ |
| 54 | |
Alexei Strots | 08b9423 | 2024-05-24 16:48:07 -0700 | [diff] [blame] | 55 | namespace aos::this_thread { |
| 56 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 57 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time) { |
| 58 | struct timespec end_time_timespec; |
| 59 | ::std::chrono::seconds sec = |
| 60 | ::std::chrono::duration_cast<::std::chrono::seconds>( |
| 61 | end_time.time_since_epoch()); |
| 62 | ::std::chrono::nanoseconds nsec = |
| 63 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>( |
| 64 | end_time.time_since_epoch() - sec); |
| 65 | end_time_timespec.tv_sec = sec.count(); |
| 66 | end_time_timespec.tv_nsec = nsec.count(); |
| 67 | int returnval; |
| 68 | do { |
| 69 | returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, |
| 70 | &end_time_timespec, nullptr); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 71 | PCHECK(returnval == 0 || returnval == EINTR) |
| 72 | << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) |
| 73 | << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed"; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 74 | } while (returnval != 0); |
| 75 | } |
| 76 | |
Alexei Strots | 08b9423 | 2024-05-24 16:48:07 -0700 | [diff] [blame] | 77 | } // namespace aos::this_thread |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 78 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 79 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 80 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 81 | namespace aos { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 82 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 83 | std::ostream &operator<<(std::ostream &stream, |
| 84 | const aos::monotonic_clock::time_point &now) { |
Philipp Schrader | a7878b9 | 2023-10-16 17:33:40 -0700 | [diff] [blame] | 85 | PrintToStream(stream, now.time_since_epoch()); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 86 | return stream; |
| 87 | } |
| 88 | |
Eric Schmiedeberg | 42273f4 | 2023-12-14 13:48:45 -0700 | [diff] [blame] | 89 | std::string ToString(const aos::monotonic_clock::time_point &now) { |
| 90 | std::ostringstream stream; |
| 91 | stream << now; |
| 92 | return stream.str(); |
| 93 | } |
| 94 | |
| 95 | std::string ToString(const aos::realtime_clock::time_point &now) { |
| 96 | std::ostringstream stream; |
| 97 | stream << now; |
| 98 | return stream.str(); |
| 99 | } |
| 100 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 101 | #ifdef __linux__ |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 102 | std::optional<monotonic_clock::time_point> monotonic_clock::FromString( |
| 103 | const std::string_view now) { |
| 104 | // This should undo the operator << above. |
| 105 | if (now.size() < 14) { |
| 106 | return std::nullopt; |
| 107 | } |
| 108 | |
| 109 | if (now.substr(now.size() - 3, now.size()) != "sec") { |
| 110 | return std::nullopt; |
| 111 | } |
| 112 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 113 | if (now[now.size() - 13] != '.') { |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 114 | return std::nullopt; |
| 115 | } |
| 116 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 117 | bool negative = now[0] == '-'; |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 118 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 119 | std::string_view sec( |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 120 | now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13))); |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 121 | std::string_view nsec(now.substr(now.size() - 12, 9)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 122 | |
| 123 | if (!std::all_of(sec.begin(), sec.end(), ::isdigit) || |
| 124 | !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) { |
| 125 | return std::nullopt; |
| 126 | } |
| 127 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 128 | std::chrono::seconds::rep seconds_data; |
| 129 | if (!absl::SimpleAtoi(sec, &seconds_data)) { |
| 130 | return std::nullopt; |
| 131 | } |
| 132 | |
| 133 | std::chrono::nanoseconds::rep nanoseconds_data; |
| 134 | if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) { |
| 135 | return std::nullopt; |
| 136 | } |
| 137 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 138 | return monotonic_clock::time_point( |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 139 | std::chrono::seconds((negative ? -1 : 1) * seconds_data) + |
| 140 | std::chrono::nanoseconds((negative ? -1 : 1) * nanoseconds_data)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | std::optional<realtime_clock::time_point> realtime_clock::FromString( |
| 144 | const std::string_view now) { |
| 145 | // This should undo the operator << above. |
| 146 | |
| 147 | if (now.size() < 25) { |
| 148 | return std::nullopt; |
| 149 | } |
| 150 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 151 | if (now[now.size() - 10] != '.') { |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 152 | return std::nullopt; |
| 153 | } |
| 154 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 155 | std::string_view nsec(now.substr(now.size() - 9, 9)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 156 | |
| 157 | if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) { |
| 158 | return std::nullopt; |
| 159 | } |
| 160 | |
| 161 | struct tm tm; |
| 162 | std::istringstream ss(std::string(now.substr(0, now.size() - 10))); |
| 163 | ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S"); |
Austin Schuh | 76a313c | 2021-11-30 19:16:35 -0800 | [diff] [blame] | 164 | if (ss.fail()) { |
| 165 | return std::nullopt; |
| 166 | } |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 167 | tm.tm_isdst = -1; |
| 168 | |
| 169 | time_t seconds = mktime(&tm); |
| 170 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 171 | std::chrono::nanoseconds::rep nanoseconds_data; |
| 172 | if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) { |
| 173 | return std::nullopt; |
| 174 | } |
| 175 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 176 | return realtime_clock::time_point(std::chrono::seconds(seconds) + |
| 177 | std::chrono::nanoseconds(nanoseconds_data)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 178 | } |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 179 | #endif |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 180 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 181 | std::ostream &operator<<(std::ostream &stream, |
| 182 | const aos::realtime_clock::time_point &now) { |
| 183 | std::tm tm; |
| 184 | std::chrono::seconds seconds = |
| 185 | now < realtime_clock::epoch() |
Austin Schuh | b2ac056 | 2021-09-13 23:23:33 -0700 | [diff] [blame] | 186 | ? (std::chrono::duration_cast<std::chrono::seconds>( |
| 187 | now.time_since_epoch() + std::chrono::nanoseconds(1)) - |
| 188 | std::chrono::seconds(1)) |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 189 | : std::chrono::duration_cast<std::chrono::seconds>( |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 190 | now.time_since_epoch()); |
| 191 | |
Philipp Schrader | d0d264d | 2023-12-20 11:11:35 -0800 | [diff] [blame] | 192 | // We can run into some corner cases where the seconds value is large enough |
| 193 | // to cause the conversion to nanoseconds to overflow. That is undefined |
| 194 | // behaviour so we prevent it with this check here. |
| 195 | if (int64_t result; |
| 196 | __builtin_mul_overflow(seconds.count(), 1'000'000'000, &result)) { |
| 197 | stream << "(unrepresentable realtime " << now.time_since_epoch().count() |
| 198 | << ")"; |
| 199 | return stream; |
| 200 | } |
| 201 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 202 | std::time_t seconds_t = seconds.count(); |
| 203 | stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.") |
| 204 | << std::setfill('0') << std::setw(9) |
| 205 | << std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 206 | now.time_since_epoch() - seconds) |
| 207 | .count(); |
| 208 | return stream; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 209 | } |
| 210 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 211 | namespace time { |
| 212 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 213 | struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 214 | struct timespec time_timespec; |
| 215 | ::std::chrono::seconds sec = |
| 216 | ::std::chrono::duration_cast<::std::chrono::seconds>(duration); |
| 217 | ::std::chrono::nanoseconds nsec = |
| 218 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec); |
| 219 | time_timespec.tv_sec = sec.count(); |
| 220 | time_timespec.tv_nsec = nsec.count(); |
| 221 | return time_timespec; |
| 222 | } |
| 223 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 224 | struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 225 | return to_timespec(time.time_since_epoch()); |
| 226 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 227 | |
| 228 | ::aos::monotonic_clock::time_point from_timeval(struct timeval t) { |
| 229 | return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) + |
| 230 | std::chrono::microseconds(t.tv_usec); |
| 231 | } |
| 232 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 233 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 234 | |
| 235 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 236 | constexpr monotonic_clock::time_point monotonic_clock::max_time; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 237 | constexpr realtime_clock::time_point realtime_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 238 | constexpr realtime_clock::time_point realtime_clock::max_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 239 | |
| 240 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 241 | #ifdef __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 242 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 243 | PCHECK(clock_gettime(CLOCK_MONOTONIC, ¤t_time) == 0) |
| 244 | << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", " |
| 245 | << ¤t_time << ") failed"; |
| 246 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 247 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 248 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 249 | |
| 250 | #else // __linux__ |
| 251 | |
| 252 | __disable_irq(); |
| 253 | const uint32_t current_counter = SYST_CVR; |
| 254 | uint32_t ms_count = systick_millis_count; |
| 255 | const uint32_t istatus = SCB_ICSR; |
| 256 | __enable_irq(); |
| 257 | // If the interrupt is pending and the timer has already wrapped from 0 back |
| 258 | // up to its max, then add another ms. |
| 259 | if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) { |
| 260 | ++ms_count; |
| 261 | } |
| 262 | |
| 263 | // It counts down, but everything we care about counts up. |
| 264 | const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter; |
| 265 | |
| 266 | // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock |
| 267 | // source is always the core clock, FCLK". |
| 268 | using systick_duration = |
| 269 | std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>; |
| 270 | |
Milo Lin | fd40acb | 2021-11-06 14:51:36 -0700 | [diff] [blame] | 271 | return time_point(std::chrono::round<std::chrono::nanoseconds>( |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 272 | std::chrono::milliseconds(ms_count) + systick_duration(counter_up))); |
| 273 | |
| 274 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 277 | #ifdef __linux__ |
| 278 | realtime_clock::time_point realtime_clock::now() noexcept { |
| 279 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 280 | PCHECK(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0) |
| 281 | << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", " |
| 282 | << ¤t_time << ") failed"; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 283 | |
| 284 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
| 285 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
| 286 | } |
| 287 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 288 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 289 | } // namespace aos |