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