blob: 0e02481efab3ed3b5a16366f25e0cc79c1bd5ae3 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/time/time.h"
brians343bc112013-02-10 01:53:46 +00002
Stephan Pleinescd3701f2024-05-30 10:56:04 -07003#include <ctype.h>
4#include <errno.h>
5#include <sys/time.h>
6
Austin Schuh3ce0a922020-07-21 21:08:54 -07007#include <algorithm>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08008#include <chrono>
Stephan Pleinescd3701f2024-05-30 10:56:04 -07009#include <compare>
10#include <cstdint>
James Kuszmaul38735e82019-12-07 16:42:06 -080011#include <ctime>
Austin Schuhe92f7e62019-12-25 13:54:41 -080012#include <iomanip>
Stephan Pleinescd3701f2024-05-30 10:56:04 -070013#include <ratio>
Eric Schmiedeberg42273f42023-12-14 13:48:45 -070014#include <sstream>
Brian Silvermand0575692015-02-21 16:24:02 -050015
Brian Silvermanc03a30c2019-02-16 18:21:56 -080016#ifdef __linux__
17
Austin Schuhd90499f2023-03-24 15:10:51 -070018#include "absl/strings/numbers.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070019#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000020
Brian Silvermanc03a30c2019-02-16 18:21:56 -080021#else // __linux__
22
23#include "motors/core/kinetis.h"
24
25// The systick interrupt increments this every 1ms.
26extern "C" volatile uint32_t systick_millis_count;
27
28#endif // __linux__
29
Austin Schuhf2a50ba2016-12-24 16:16:26 -080030namespace chrono = ::std::chrono;
31
Philipp Schradera7878b92023-10-16 17:33:40 -070032namespace {
33
34void PrintToStream(std::ostream &stream, chrono::nanoseconds duration) {
35 chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(duration);
36 if (duration < chrono::nanoseconds(0)) {
37 stream << "-" << -seconds.count() << "." << std::setfill('0')
38 << std::setw(9)
39 << chrono::duration_cast<chrono::nanoseconds>(seconds - duration)
40 .count()
41 << "sec";
42 } else {
43 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
44 << chrono::duration_cast<chrono::nanoseconds>(duration - seconds)
45 .count()
46 << "sec";
47 }
48}
49
50} // namespace
51
Brian Silvermanc03a30c2019-02-16 18:21:56 -080052#ifdef __linux__
53
Alexei Strots08b94232024-05-24 16:48:07 -070054namespace aos::this_thread {
55
Austin Schuh793d6b92016-05-01 13:28:14 -070056void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
57 struct timespec end_time_timespec;
58 ::std::chrono::seconds sec =
59 ::std::chrono::duration_cast<::std::chrono::seconds>(
60 end_time.time_since_epoch());
61 ::std::chrono::nanoseconds nsec =
62 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
63 end_time.time_since_epoch() - sec);
64 end_time_timespec.tv_sec = sec.count();
65 end_time_timespec.tv_nsec = nsec.count();
66 int returnval;
67 do {
68 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
69 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070070 PCHECK(returnval == 0 || returnval == EINTR)
71 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
72 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070073 } while (returnval != 0);
74}
75
Alexei Strots08b94232024-05-24 16:48:07 -070076} // namespace aos::this_thread
Austin Schuh793d6b92016-05-01 13:28:14 -070077
Brian Silvermanc03a30c2019-02-16 18:21:56 -080078#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070079
brians343bc112013-02-10 01:53:46 +000080namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080081
James Kuszmaul38735e82019-12-07 16:42:06 -080082std::ostream &operator<<(std::ostream &stream,
83 const aos::monotonic_clock::time_point &now) {
Philipp Schradera7878b92023-10-16 17:33:40 -070084 PrintToStream(stream, now.time_since_epoch());
Austin Schuh40102d22019-12-27 23:30:06 -080085 return stream;
86}
87
Eric Schmiedeberg42273f42023-12-14 13:48:45 -070088std::string ToString(const aos::monotonic_clock::time_point &now) {
89 std::ostringstream stream;
90 stream << now;
91 return stream.str();
92}
93
94std::string ToString(const aos::realtime_clock::time_point &now) {
95 std::ostringstream stream;
96 stream << now;
97 return stream.str();
98}
99
Austin Schuhd90499f2023-03-24 15:10:51 -0700100#ifdef __linux__
Austin Schuh3ce0a922020-07-21 21:08:54 -0700101std::optional<monotonic_clock::time_point> monotonic_clock::FromString(
102 const std::string_view now) {
103 // This should undo the operator << above.
104 if (now.size() < 14) {
105 return std::nullopt;
106 }
107
108 if (now.substr(now.size() - 3, now.size()) != "sec") {
109 return std::nullopt;
110 }
111
Brian J Griglak259048b2022-01-27 12:30:55 -0700112 if (now[now.size() - 13] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -0700113 return std::nullopt;
114 }
115
Brian J Griglak259048b2022-01-27 12:30:55 -0700116 bool negative = now[0] == '-';
Austin Schuh3ce0a922020-07-21 21:08:54 -0700117
Austin Schuhd90499f2023-03-24 15:10:51 -0700118 std::string_view sec(
Austin Schuh3ce0a922020-07-21 21:08:54 -0700119 now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13)));
Austin Schuhd90499f2023-03-24 15:10:51 -0700120 std::string_view nsec(now.substr(now.size() - 12, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700121
122 if (!std::all_of(sec.begin(), sec.end(), ::isdigit) ||
123 !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
124 return std::nullopt;
125 }
126
Austin Schuhd90499f2023-03-24 15:10:51 -0700127 std::chrono::seconds::rep seconds_data;
128 if (!absl::SimpleAtoi(sec, &seconds_data)) {
129 return std::nullopt;
130 }
131
132 std::chrono::nanoseconds::rep nanoseconds_data;
133 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
134 return std::nullopt;
135 }
136
Austin Schuh3ce0a922020-07-21 21:08:54 -0700137 return monotonic_clock::time_point(
Austin Schuhd90499f2023-03-24 15:10:51 -0700138 std::chrono::seconds((negative ? -1 : 1) * seconds_data) +
139 std::chrono::nanoseconds((negative ? -1 : 1) * nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700140}
141
142std::optional<realtime_clock::time_point> realtime_clock::FromString(
143 const std::string_view now) {
144 // This should undo the operator << above.
145
146 if (now.size() < 25) {
147 return std::nullopt;
148 }
149
Brian J Griglak259048b2022-01-27 12:30:55 -0700150 if (now[now.size() - 10] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -0700151 return std::nullopt;
152 }
153
Austin Schuhd90499f2023-03-24 15:10:51 -0700154 std::string_view nsec(now.substr(now.size() - 9, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700155
156 if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
157 return std::nullopt;
158 }
159
160 struct tm tm;
161 std::istringstream ss(std::string(now.substr(0, now.size() - 10)));
162 ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S");
Austin Schuh76a313c2021-11-30 19:16:35 -0800163 if (ss.fail()) {
164 return std::nullopt;
165 }
Austin Schuh3ce0a922020-07-21 21:08:54 -0700166 tm.tm_isdst = -1;
167
168 time_t seconds = mktime(&tm);
169
Austin Schuhd90499f2023-03-24 15:10:51 -0700170 std::chrono::nanoseconds::rep nanoseconds_data;
171 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
172 return std::nullopt;
173 }
174
Philipp Schrader790cb542023-07-05 21:06:52 -0700175 return realtime_clock::time_point(std::chrono::seconds(seconds) +
176 std::chrono::nanoseconds(nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700177}
Austin Schuhd90499f2023-03-24 15:10:51 -0700178#endif
Austin Schuh3ce0a922020-07-21 21:08:54 -0700179
Austin Schuh40102d22019-12-27 23:30:06 -0800180std::ostream &operator<<(std::ostream &stream,
181 const aos::realtime_clock::time_point &now) {
182 std::tm tm;
183 std::chrono::seconds seconds =
184 now < realtime_clock::epoch()
Austin Schuhb2ac0562021-09-13 23:23:33 -0700185 ? (std::chrono::duration_cast<std::chrono::seconds>(
186 now.time_since_epoch() + std::chrono::nanoseconds(1)) -
187 std::chrono::seconds(1))
Austin Schuh40102d22019-12-27 23:30:06 -0800188 : std::chrono::duration_cast<std::chrono::seconds>(
Austin Schuhe92f7e62019-12-25 13:54:41 -0800189 now.time_since_epoch());
190
Philipp Schraderd0d264d2023-12-20 11:11:35 -0800191 // We can run into some corner cases where the seconds value is large enough
192 // to cause the conversion to nanoseconds to overflow. That is undefined
193 // behaviour so we prevent it with this check here.
194 if (int64_t result;
195 __builtin_mul_overflow(seconds.count(), 1'000'000'000, &result)) {
196 stream << "(unrepresentable realtime " << now.time_since_epoch().count()
197 << ")";
198 return stream;
199 }
200
Austin Schuh40102d22019-12-27 23:30:06 -0800201 std::time_t seconds_t = seconds.count();
202 stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.")
203 << std::setfill('0') << std::setw(9)
204 << std::chrono::duration_cast<std::chrono::nanoseconds>(
205 now.time_since_epoch() - seconds)
206 .count();
207 return stream;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800208}
209
brians343bc112013-02-10 01:53:46 +0000210namespace time {
211
Austin Schuh40102d22019-12-27 23:30:06 -0800212struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) {
Neil Balch229001a2018-01-07 18:22:52 -0800213 struct timespec time_timespec;
214 ::std::chrono::seconds sec =
215 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
216 ::std::chrono::nanoseconds nsec =
217 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
218 time_timespec.tv_sec = sec.count();
219 time_timespec.tv_nsec = nsec.count();
220 return time_timespec;
221}
222
Austin Schuh40102d22019-12-27 23:30:06 -0800223struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) {
Neil Balch229001a2018-01-07 18:22:52 -0800224 return to_timespec(time.time_since_epoch());
225}
Brian Silverman967e5df2020-02-09 16:43:34 -0800226
227::aos::monotonic_clock::time_point from_timeval(struct timeval t) {
228 return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) +
229 std::chrono::microseconds(t.tv_usec);
230}
231
brians343bc112013-02-10 01:53:46 +0000232} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -0800233
234constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800235constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -0800236constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800237constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -0800238
239monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800240#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800241 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700242 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
243 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
244 << &current_time << ") failed";
245
Austin Schuh858c0212016-11-25 17:23:30 -0800246 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800248
249#else // __linux__
250
251 __disable_irq();
252 const uint32_t current_counter = SYST_CVR;
253 uint32_t ms_count = systick_millis_count;
254 const uint32_t istatus = SCB_ICSR;
255 __enable_irq();
256 // If the interrupt is pending and the timer has already wrapped from 0 back
257 // up to its max, then add another ms.
258 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
259 ++ms_count;
260 }
261
262 // It counts down, but everything we care about counts up.
263 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
264
265 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
266 // source is always the core clock, FCLK".
267 using systick_duration =
268 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
269
Milo Linfd40acb2021-11-06 14:51:36 -0700270 return time_point(std::chrono::round<std::chrono::nanoseconds>(
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800271 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
272
273#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800274}
275
Brian Silvermana3688802019-02-16 19:31:26 -0800276#ifdef __linux__
277realtime_clock::time_point realtime_clock::now() noexcept {
278 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700279 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
280 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
281 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800282
283 return time_point(::std::chrono::seconds(current_time.tv_sec) +
284 ::std::chrono::nanoseconds(current_time.tv_nsec));
285}
286#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800287
brians343bc112013-02-10 01:53:46 +0000288} // namespace aos