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 | } |
| 25 | }; |
| 26 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 27 | // Simple class representing which boot and what monotonic time in that boot. |
| 28 | // Boots are assumed to be sequential, and the monotonic clock resets on reboot |
| 29 | // for all the compare operations. |
| 30 | struct BootTimestamp { |
| 31 | // Boot number for this timestamp. |
| 32 | size_t boot = 0u; |
| 33 | // Monotonic time in that boot. |
| 34 | monotonic_clock::time_point time = monotonic_clock::min_time; |
| 35 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 36 | monotonic_clock::duration time_since_epoch() const { |
| 37 | return time.time_since_epoch(); |
| 38 | } |
| 39 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 40 | static constexpr BootTimestamp min_time() { |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 41 | return BootTimestamp{.boot = std::numeric_limits<size_t>::min(), |
| 42 | .time = monotonic_clock::min_time}; |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 43 | } |
| 44 | static constexpr BootTimestamp max_time() { |
| 45 | return BootTimestamp{.boot = std::numeric_limits<size_t>::max(), |
| 46 | .time = monotonic_clock::max_time}; |
| 47 | } |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 48 | static constexpr BootTimestamp epoch() { |
| 49 | return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()}; |
| 50 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 51 | |
| 52 | // Compare operators. These are implemented such that earlier boots always |
| 53 | // compare less than later boots, and the times are only compared in a single |
| 54 | // boot. |
| 55 | bool operator<(const BootTimestamp &m2) const; |
| 56 | bool operator<=(const BootTimestamp &m2) const; |
| 57 | bool operator>=(const BootTimestamp &m2) const; |
| 58 | bool operator>(const BootTimestamp &m2) const; |
| 59 | bool operator==(const BootTimestamp &m2) const; |
| 60 | bool operator!=(const BootTimestamp &m2) const; |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 61 | |
| 62 | BootTimestamp operator+(monotonic_clock::duration d) const { |
| 63 | return {boot, time + d}; |
| 64 | } |
| 65 | BootTimestamp operator-(monotonic_clock::duration d) const { |
| 66 | return {boot, time - d}; |
| 67 | } |
| 68 | BootTimestamp operator+(BootDuration d) const { |
| 69 | return {boot, time + d.duration}; |
| 70 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | std::ostream &operator<<(std::ostream &os, |
| 74 | const struct BootTimestamp ×tamp); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 75 | std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 76 | |
| 77 | inline bool BootTimestamp::operator<(const BootTimestamp &m2) const { |
| 78 | if (boot != m2.boot) { |
| 79 | return boot < m2.boot; |
| 80 | } |
| 81 | |
| 82 | return time < m2.time; |
| 83 | } |
| 84 | inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const { |
| 85 | if (boot != m2.boot) { |
| 86 | return boot <= m2.boot; |
| 87 | } |
| 88 | |
| 89 | return time <= m2.time; |
| 90 | } |
| 91 | inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const { |
| 92 | if (boot != m2.boot) { |
| 93 | return boot >= m2.boot; |
| 94 | } |
| 95 | |
| 96 | return time >= m2.time; |
| 97 | } |
| 98 | inline bool BootTimestamp::operator>(const BootTimestamp &m2) const { |
| 99 | if (boot != m2.boot) { |
| 100 | return boot > m2.boot; |
| 101 | } |
| 102 | |
| 103 | return time > m2.time; |
| 104 | } |
| 105 | |
| 106 | inline bool BootTimestamp::operator==(const BootTimestamp &m2) const { |
| 107 | return boot == m2.boot && time == m2.time; |
| 108 | } |
| 109 | inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const { |
| 110 | return boot != m2.boot || time != m2.time; |
| 111 | } |
| 112 | |
| 113 | } // namespace aos::logger |
| 114 | |
| 115 | #endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |