Add a BootTimestamp class and move some things over
This makes it so we can compare timestamps again across boots. Move
most things over to using it instead so we can more naturally track what
happens in what order.
Note: this is not complete and has no tests. Anywhere where I'm not
comfortable that the code will handle it today should now CHECK that the
count is 0 so we explode. Tests and tolerance of reboots will come in
future patches, starting with TimestampMapper and then with the
time recover code and then LogReader.
Change-Id: I0fdbbe1860de4fe5e48d1a6cd516e7118e5942dc
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/logging/boot_timestamp.h b/aos/events/logging/boot_timestamp.h
new file mode 100644
index 0000000..ea66c8e
--- /dev/null
+++ b/aos/events/logging/boot_timestamp.h
@@ -0,0 +1,79 @@
+#ifndef AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_
+#define AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_
+
+#include <iostream>
+
+#include "aos/time/time.h"
+
+namespace aos::logger {
+
+// Simple class representing which boot and what monotonic time in that boot.
+// Boots are assumed to be sequential, and the monotonic clock resets on reboot
+// for all the compare operations.
+struct BootTimestamp {
+ // Boot number for this timestamp.
+ size_t boot = 0u;
+ // Monotonic time in that boot.
+ monotonic_clock::time_point time = monotonic_clock::min_time;
+
+ static constexpr BootTimestamp min_time() {
+ return BootTimestamp{.boot = 0u, .time = monotonic_clock::min_time};
+ }
+ static constexpr BootTimestamp max_time() {
+ return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
+ .time = monotonic_clock::max_time};
+ }
+
+ // Compare operators. These are implemented such that earlier boots always
+ // compare less than later boots, and the times are only compared in a single
+ // boot.
+ bool operator<(const BootTimestamp &m2) const;
+ bool operator<=(const BootTimestamp &m2) const;
+ bool operator>=(const BootTimestamp &m2) const;
+ bool operator>(const BootTimestamp &m2) const;
+ bool operator==(const BootTimestamp &m2) const;
+ bool operator!=(const BootTimestamp &m2) const;
+};
+
+std::ostream &operator<<(std::ostream &os,
+ const struct BootTimestamp ×tamp);
+
+inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
+ if (boot != m2.boot) {
+ return boot < m2.boot;
+ }
+
+ return time < m2.time;
+}
+inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
+ if (boot != m2.boot) {
+ return boot <= m2.boot;
+ }
+
+ return time <= m2.time;
+}
+inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
+ if (boot != m2.boot) {
+ return boot >= m2.boot;
+ }
+
+ return time >= m2.time;
+}
+inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
+ if (boot != m2.boot) {
+ return boot > m2.boot;
+ }
+
+ return time > m2.time;
+}
+
+inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
+ return boot == m2.boot && time == m2.time;
+}
+inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
+ return boot != m2.boot || time != m2.time;
+}
+
+} // namespace aos::logger
+
+#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_