Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |
| 2 | #define AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
| 6 | #include "aos/time/time.h" |
| 7 | |
| 8 | namespace aos::logger { |
| 9 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 10 | // Simple class representing a duration in time and a boot it is from. This |
| 11 | // gives us something to use for storing the time offset when filtering. |
| 12 | struct BootDuration { |
| 13 | // Boot number for this timestamp. |
| 14 | size_t boot = 0u; |
| 15 | // Monotonic time in that boot. |
| 16 | monotonic_clock::duration duration{0}; |
| 17 | |
| 18 | BootDuration operator+(monotonic_clock::duration d) const { |
| 19 | return {boot, duration + d}; |
| 20 | } |
| 21 | |
| 22 | bool operator==(const BootDuration &m2) const { |
| 23 | return boot == m2.boot && duration == m2.duration; |
| 24 | } |
Austin Schuh | c1ee1b6 | 2022-03-22 17:09:52 -0700 | [diff] [blame] | 25 | bool operator!=(const BootDuration &m2) const { |
| 26 | return boot != m2.boot || duration != m2.duration; |
| 27 | } |
| 28 | |
| 29 | static constexpr BootDuration max_time() { |
| 30 | return BootDuration{ |
| 31 | .boot = std::numeric_limits<size_t>::max(), |
| 32 | .duration = monotonic_clock::duration( |
| 33 | ::std::numeric_limits<monotonic_clock::duration::rep>::max())}; |
| 34 | } |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 37 | // Simple class representing which boot and what monotonic time in that boot. |
| 38 | // Boots are assumed to be sequential, and the monotonic clock resets on reboot |
| 39 | // for all the compare operations. |
| 40 | struct BootTimestamp { |
| 41 | // Boot number for this timestamp. |
| 42 | size_t boot = 0u; |
| 43 | // Monotonic time in that boot. |
| 44 | monotonic_clock::time_point time = monotonic_clock::min_time; |
| 45 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 46 | monotonic_clock::duration time_since_epoch() const { |
| 47 | return time.time_since_epoch(); |
| 48 | } |
| 49 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 50 | static constexpr BootTimestamp min_time() { |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 51 | return BootTimestamp{.boot = std::numeric_limits<size_t>::min(), |
| 52 | .time = monotonic_clock::min_time}; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 53 | } |
| 54 | static constexpr BootTimestamp max_time() { |
| 55 | return BootTimestamp{.boot = std::numeric_limits<size_t>::max(), |
| 56 | .time = monotonic_clock::max_time}; |
| 57 | } |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 58 | static constexpr BootTimestamp epoch() { |
| 59 | return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()}; |
| 60 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 61 | |
| 62 | // Compare operators. These are implemented such that earlier boots always |
| 63 | // compare less than later boots, and the times are only compared in a single |
| 64 | // boot. |
| 65 | bool operator<(const BootTimestamp &m2) const; |
| 66 | bool operator<=(const BootTimestamp &m2) const; |
| 67 | bool operator>=(const BootTimestamp &m2) const; |
| 68 | bool operator>(const BootTimestamp &m2) const; |
| 69 | bool operator==(const BootTimestamp &m2) const; |
| 70 | bool operator!=(const BootTimestamp &m2) const; |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 71 | |
| 72 | BootTimestamp operator+(monotonic_clock::duration d) const { |
| 73 | return {boot, time + d}; |
| 74 | } |
| 75 | BootTimestamp operator-(monotonic_clock::duration d) const { |
| 76 | return {boot, time - d}; |
| 77 | } |
| 78 | BootTimestamp operator+(BootDuration d) const { |
| 79 | return {boot, time + d.duration}; |
| 80 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 83 | // Structure to hold both a boot and queue index. Queue indices reset after |
| 84 | // reboot, so we need to track them. |
| 85 | struct BootQueueIndex { |
| 86 | // Boot number for this queue index. |
| 87 | size_t boot = std::numeric_limits<size_t>::max(); |
| 88 | // Queue index. |
| 89 | uint32_t index = std::numeric_limits<uint32_t>::max(); |
| 90 | |
| 91 | // Returns a QueueIndex representing an invalid index. Since |
| 92 | // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code |
| 93 | // and is reserved as an Invalid value, this will never collide. |
| 94 | static BootQueueIndex Invalid() { |
| 95 | return {.boot = std::numeric_limits<size_t>::max(), |
| 96 | .index = std::numeric_limits<uint32_t>::max()}; |
| 97 | } |
| 98 | |
| 99 | bool operator==(const BootQueueIndex &b2) const { |
| 100 | return index == b2.index && boot == b2.boot; |
| 101 | } |
| 102 | bool operator!=(const BootQueueIndex &b2) const { |
| 103 | return index != b2.index || boot != b2.boot; |
| 104 | } |
| 105 | bool operator<(const BootQueueIndex &b2) const { |
| 106 | if (boot == b2.boot) { |
| 107 | return index < b2.index; |
| 108 | } |
| 109 | return boot < b2.boot; |
| 110 | } |
| 111 | bool operator>(const BootQueueIndex &b2) const { |
| 112 | if (boot == b2.boot) { |
| 113 | return index > b2.index; |
| 114 | } |
| 115 | return boot > b2.boot; |
| 116 | } |
| 117 | }; |
| 118 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 119 | std::ostream &operator<<(std::ostream &os, |
| 120 | const struct BootTimestamp ×tamp); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 121 | std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 122 | std::ostream &operator<<(std::ostream &os, |
| 123 | const struct BootQueueIndex &queue_index); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 124 | |
| 125 | inline bool BootTimestamp::operator<(const BootTimestamp &m2) const { |
| 126 | if (boot != m2.boot) { |
| 127 | return boot < m2.boot; |
| 128 | } |
| 129 | |
| 130 | return time < m2.time; |
| 131 | } |
| 132 | inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const { |
| 133 | if (boot != m2.boot) { |
| 134 | return boot <= m2.boot; |
| 135 | } |
| 136 | |
| 137 | return time <= m2.time; |
| 138 | } |
| 139 | inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const { |
| 140 | if (boot != m2.boot) { |
| 141 | return boot >= m2.boot; |
| 142 | } |
| 143 | |
| 144 | return time >= m2.time; |
| 145 | } |
| 146 | inline bool BootTimestamp::operator>(const BootTimestamp &m2) const { |
| 147 | if (boot != m2.boot) { |
| 148 | return boot > m2.boot; |
| 149 | } |
| 150 | |
| 151 | return time > m2.time; |
| 152 | } |
| 153 | |
| 154 | inline bool BootTimestamp::operator==(const BootTimestamp &m2) const { |
| 155 | return boot == m2.boot && time == m2.time; |
| 156 | } |
| 157 | inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const { |
| 158 | return boot != m2.boot || time != m2.time; |
| 159 | } |
| 160 | |
| 161 | } // namespace aos::logger |
| 162 | |
| 163 | #endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |