blob: db713790e98f411310cf3190941ed913549b9b92 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/time/time.h"
brians343bc112013-02-10 01:53:46 +00002
Austin Schuh3ce0a922020-07-21 21:08:54 -07003#include <algorithm>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08004#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
6#include <cstring>
James Kuszmaul38735e82019-12-07 16:42:06 -08007#include <ctime>
Austin Schuhe92f7e62019-12-25 13:54:41 -08008#include <iomanip>
Brian Silvermand0575692015-02-21 16:24:02 -05009
Brian Silvermanc03a30c2019-02-16 18:21:56 -080010#ifdef __linux__
11
Austin Schuhd90499f2023-03-24 15:10:51 -070012#include "absl/strings/numbers.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070013#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000014
Brian Silvermanc03a30c2019-02-16 18:21:56 -080015#else // __linux__
16
17#include "motors/core/kinetis.h"
18
19// The systick interrupt increments this every 1ms.
20extern "C" volatile uint32_t systick_millis_count;
21
22#endif // __linux__
23
Austin Schuhf2a50ba2016-12-24 16:16:26 -080024namespace chrono = ::std::chrono;
25
Philipp Schradera7878b92023-10-16 17:33:40 -070026namespace {
27
28void PrintToStream(std::ostream &stream, chrono::nanoseconds duration) {
29 chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(duration);
30 if (duration < chrono::nanoseconds(0)) {
31 stream << "-" << -seconds.count() << "." << std::setfill('0')
32 << std::setw(9)
33 << chrono::duration_cast<chrono::nanoseconds>(seconds - duration)
34 .count()
35 << "sec";
36 } else {
37 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
38 << chrono::duration_cast<chrono::nanoseconds>(duration - seconds)
39 .count()
40 << "sec";
41 }
42}
43
44} // namespace
45
Brian Silvermanc03a30c2019-02-16 18:21:56 -080046#ifdef __linux__
47
Stephan Pleinesf63bde82024-01-13 15:59:33 -080048namespace std::this_thread {
Austin Schuh793d6b92016-05-01 13:28:14 -070049template <>
50void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
51 struct timespec end_time_timespec;
52 ::std::chrono::seconds sec =
53 ::std::chrono::duration_cast<::std::chrono::seconds>(
54 end_time.time_since_epoch());
55 ::std::chrono::nanoseconds nsec =
56 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
57 end_time.time_since_epoch() - sec);
58 end_time_timespec.tv_sec = sec.count();
59 end_time_timespec.tv_nsec = nsec.count();
60 int returnval;
61 do {
62 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
63 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070064 PCHECK(returnval == 0 || returnval == EINTR)
65 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
66 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070067 } while (returnval != 0);
68}
69
Stephan Pleinesf63bde82024-01-13 15:59:33 -080070} // namespace std::this_thread
Austin Schuh793d6b92016-05-01 13:28:14 -070071
Brian Silvermanc03a30c2019-02-16 18:21:56 -080072#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070073
brians343bc112013-02-10 01:53:46 +000074namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080075
James Kuszmaul38735e82019-12-07 16:42:06 -080076std::ostream &operator<<(std::ostream &stream,
77 const aos::monotonic_clock::time_point &now) {
Philipp Schradera7878b92023-10-16 17:33:40 -070078 PrintToStream(stream, now.time_since_epoch());
Austin Schuh40102d22019-12-27 23:30:06 -080079 return stream;
80}
81
Austin Schuhd90499f2023-03-24 15:10:51 -070082#ifdef __linux__
Austin Schuh3ce0a922020-07-21 21:08:54 -070083std::optional<monotonic_clock::time_point> monotonic_clock::FromString(
84 const std::string_view now) {
85 // This should undo the operator << above.
86 if (now.size() < 14) {
87 return std::nullopt;
88 }
89
90 if (now.substr(now.size() - 3, now.size()) != "sec") {
91 return std::nullopt;
92 }
93
Brian J Griglak259048b2022-01-27 12:30:55 -070094 if (now[now.size() - 13] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -070095 return std::nullopt;
96 }
97
Brian J Griglak259048b2022-01-27 12:30:55 -070098 bool negative = now[0] == '-';
Austin Schuh3ce0a922020-07-21 21:08:54 -070099
Austin Schuhd90499f2023-03-24 15:10:51 -0700100 std::string_view sec(
Austin Schuh3ce0a922020-07-21 21:08:54 -0700101 now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13)));
Austin Schuhd90499f2023-03-24 15:10:51 -0700102 std::string_view nsec(now.substr(now.size() - 12, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700103
104 if (!std::all_of(sec.begin(), sec.end(), ::isdigit) ||
105 !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
106 return std::nullopt;
107 }
108
Austin Schuhd90499f2023-03-24 15:10:51 -0700109 std::chrono::seconds::rep seconds_data;
110 if (!absl::SimpleAtoi(sec, &seconds_data)) {
111 return std::nullopt;
112 }
113
114 std::chrono::nanoseconds::rep nanoseconds_data;
115 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
116 return std::nullopt;
117 }
118
Austin Schuh3ce0a922020-07-21 21:08:54 -0700119 return monotonic_clock::time_point(
Austin Schuhd90499f2023-03-24 15:10:51 -0700120 std::chrono::seconds((negative ? -1 : 1) * seconds_data) +
121 std::chrono::nanoseconds((negative ? -1 : 1) * nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700122}
123
124std::optional<realtime_clock::time_point> realtime_clock::FromString(
125 const std::string_view now) {
126 // This should undo the operator << above.
127
128 if (now.size() < 25) {
129 return std::nullopt;
130 }
131
Brian J Griglak259048b2022-01-27 12:30:55 -0700132 if (now[now.size() - 10] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -0700133 return std::nullopt;
134 }
135
Austin Schuhd90499f2023-03-24 15:10:51 -0700136 std::string_view nsec(now.substr(now.size() - 9, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700137
138 if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
139 return std::nullopt;
140 }
141
142 struct tm tm;
143 std::istringstream ss(std::string(now.substr(0, now.size() - 10)));
144 ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S");
Austin Schuh76a313c2021-11-30 19:16:35 -0800145 if (ss.fail()) {
146 return std::nullopt;
147 }
Austin Schuh3ce0a922020-07-21 21:08:54 -0700148 tm.tm_isdst = -1;
149
150 time_t seconds = mktime(&tm);
151
Austin Schuhd90499f2023-03-24 15:10:51 -0700152 std::chrono::nanoseconds::rep nanoseconds_data;
153 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
154 return std::nullopt;
155 }
156
Philipp Schrader790cb542023-07-05 21:06:52 -0700157 return realtime_clock::time_point(std::chrono::seconds(seconds) +
158 std::chrono::nanoseconds(nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700159}
Austin Schuhd90499f2023-03-24 15:10:51 -0700160#endif
Austin Schuh3ce0a922020-07-21 21:08:54 -0700161
Austin Schuh40102d22019-12-27 23:30:06 -0800162std::ostream &operator<<(std::ostream &stream,
163 const aos::realtime_clock::time_point &now) {
164 std::tm tm;
165 std::chrono::seconds seconds =
166 now < realtime_clock::epoch()
Austin Schuhb2ac0562021-09-13 23:23:33 -0700167 ? (std::chrono::duration_cast<std::chrono::seconds>(
168 now.time_since_epoch() + std::chrono::nanoseconds(1)) -
169 std::chrono::seconds(1))
Austin Schuh40102d22019-12-27 23:30:06 -0800170 : std::chrono::duration_cast<std::chrono::seconds>(
Austin Schuhe92f7e62019-12-25 13:54:41 -0800171 now.time_since_epoch());
172
Philipp Schraderd0d264d2023-12-20 11:11:35 -0800173 // We can run into some corner cases where the seconds value is large enough
174 // to cause the conversion to nanoseconds to overflow. That is undefined
175 // behaviour so we prevent it with this check here.
176 if (int64_t result;
177 __builtin_mul_overflow(seconds.count(), 1'000'000'000, &result)) {
178 stream << "(unrepresentable realtime " << now.time_since_epoch().count()
179 << ")";
180 return stream;
181 }
182
Austin Schuh40102d22019-12-27 23:30:06 -0800183 std::time_t seconds_t = seconds.count();
184 stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.")
185 << std::setfill('0') << std::setw(9)
186 << std::chrono::duration_cast<std::chrono::nanoseconds>(
187 now.time_since_epoch() - seconds)
188 .count();
189 return stream;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800190}
191
brians343bc112013-02-10 01:53:46 +0000192namespace time {
193
Austin Schuh40102d22019-12-27 23:30:06 -0800194struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) {
Neil Balch229001a2018-01-07 18:22:52 -0800195 struct timespec time_timespec;
196 ::std::chrono::seconds sec =
197 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
198 ::std::chrono::nanoseconds nsec =
199 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
200 time_timespec.tv_sec = sec.count();
201 time_timespec.tv_nsec = nsec.count();
202 return time_timespec;
203}
204
Austin Schuh40102d22019-12-27 23:30:06 -0800205struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) {
Neil Balch229001a2018-01-07 18:22:52 -0800206 return to_timespec(time.time_since_epoch());
207}
Brian Silverman967e5df2020-02-09 16:43:34 -0800208
209::aos::monotonic_clock::time_point from_timeval(struct timeval t) {
210 return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) +
211 std::chrono::microseconds(t.tv_usec);
212}
213
brians343bc112013-02-10 01:53:46 +0000214} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -0800215
216constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800217constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -0800218constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800219constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -0800220
221monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800222#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800223 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700224 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
225 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
226 << &current_time << ") failed";
227
Austin Schuh858c0212016-11-25 17:23:30 -0800228 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700229 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800230
231#else // __linux__
232
233 __disable_irq();
234 const uint32_t current_counter = SYST_CVR;
235 uint32_t ms_count = systick_millis_count;
236 const uint32_t istatus = SCB_ICSR;
237 __enable_irq();
238 // If the interrupt is pending and the timer has already wrapped from 0 back
239 // up to its max, then add another ms.
240 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
241 ++ms_count;
242 }
243
244 // It counts down, but everything we care about counts up.
245 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
246
247 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
248 // source is always the core clock, FCLK".
249 using systick_duration =
250 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
251
Milo Linfd40acb2021-11-06 14:51:36 -0700252 return time_point(std::chrono::round<std::chrono::nanoseconds>(
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800253 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
254
255#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800256}
257
Brian Silvermana3688802019-02-16 19:31:26 -0800258#ifdef __linux__
259realtime_clock::time_point realtime_clock::now() noexcept {
260 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700261 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
262 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
263 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800264
265 return time_point(::std::chrono::seconds(current_time.tv_sec) +
266 ::std::chrono::nanoseconds(current_time.tv_nsec));
267}
268#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800269
brians343bc112013-02-10 01:53:46 +0000270} // namespace aos