blob: e818fb9c1e851f04be1f0a28f4f64582b1b68a48 [file] [log] [blame]
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001#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 Schuh704784e2022-07-13 21:11:41 -07007#include "glog/logging.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07008
9namespace aos::logger {
10
Austin Schuh66168842021-08-17 19:42:21 -070011// 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.
13struct 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 Schuhefba96d2022-06-24 13:22:18 -070023 BootDuration operator-() const { return {boot, -duration}; }
24 BootDuration operator-(monotonic_clock::duration d) const {
25 return {boot, duration - d};
26 }
27
Austin Schuh704784e2022-07-13 21:11:41 -070028 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 Schuh66168842021-08-17 19:42:21 -070040 bool operator==(const BootDuration &m2) const {
41 return boot == m2.boot && duration == m2.duration;
42 }
Austin Schuhc1ee1b62022-03-22 17:09:52 -070043 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 Schuh66168842021-08-17 19:42:21 -070053};
54
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070055// 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.
58struct 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 Schuh66168842021-08-17 19:42:21 -070064 monotonic_clock::duration time_since_epoch() const {
65 return time.time_since_epoch();
66 }
67
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070068 static constexpr BootTimestamp min_time() {
Austin Schuh66168842021-08-17 19:42:21 -070069 return BootTimestamp{.boot = std::numeric_limits<size_t>::min(),
70 .time = monotonic_clock::min_time};
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070071 }
72 static constexpr BootTimestamp max_time() {
73 return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
74 .time = monotonic_clock::max_time};
75 }
Austin Schuh66168842021-08-17 19:42:21 -070076 static constexpr BootTimestamp epoch() {
77 return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()};
78 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070079
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 Schuh66168842021-08-17 19:42:21 -070089
90 BootTimestamp operator+(monotonic_clock::duration d) const {
91 return {boot, time + d};
92 }
93 BootTimestamp operator-(monotonic_clock::duration d) const {
94 return {boot, time - d};
95 }
96 BootTimestamp operator+(BootDuration d) const {
97 return {boot, time + d.duration};
98 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070099};
100
Austin Schuh58646e22021-08-23 23:51:46 -0700101// Structure to hold both a boot and queue index. Queue indices reset after
102// reboot, so we need to track them.
103struct BootQueueIndex {
104 // Boot number for this queue index.
105 size_t boot = std::numeric_limits<size_t>::max();
106 // Queue index.
107 uint32_t index = std::numeric_limits<uint32_t>::max();
108
109 // Returns a QueueIndex representing an invalid index. Since
110 // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code
111 // and is reserved as an Invalid value, this will never collide.
112 static BootQueueIndex Invalid() {
113 return {.boot = std::numeric_limits<size_t>::max(),
114 .index = std::numeric_limits<uint32_t>::max()};
115 }
116
117 bool operator==(const BootQueueIndex &b2) const {
118 return index == b2.index && boot == b2.boot;
119 }
120 bool operator!=(const BootQueueIndex &b2) const {
121 return index != b2.index || boot != b2.boot;
122 }
123 bool operator<(const BootQueueIndex &b2) const {
124 if (boot == b2.boot) {
125 return index < b2.index;
126 }
127 return boot < b2.boot;
128 }
129 bool operator>(const BootQueueIndex &b2) const {
130 if (boot == b2.boot) {
131 return index > b2.index;
132 }
133 return boot > b2.boot;
134 }
135};
136
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700137std::ostream &operator<<(std::ostream &os,
138 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -0700139std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh58646e22021-08-23 23:51:46 -0700140std::ostream &operator<<(std::ostream &os,
141 const struct BootQueueIndex &queue_index);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700142
143inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
144 if (boot != m2.boot) {
145 return boot < m2.boot;
146 }
147
148 return time < m2.time;
149}
150inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
151 if (boot != m2.boot) {
152 return boot <= m2.boot;
153 }
154
155 return time <= m2.time;
156}
157inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
158 if (boot != m2.boot) {
159 return boot >= m2.boot;
160 }
161
162 return time >= m2.time;
163}
164inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
165 if (boot != m2.boot) {
166 return boot > m2.boot;
167 }
168
169 return time > m2.time;
170}
171
172inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
173 return boot == m2.boot && time == m2.time;
174}
175inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
176 return boot != m2.boot || time != m2.time;
177}
178
179} // namespace aos::logger
180
181#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_