blob: 0d68045111976369a5921b89bd8ce90fb7044136 [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 }
Austin Schuhf1dace42022-07-13 22:37:12 -070093
94 BootTimestamp operator+=(monotonic_clock::duration d) {
95 time += d;
96 return *this;
97 }
Austin Schuh66168842021-08-17 19:42:21 -070098 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 Schuh2dc8c7d2021-07-01 17:41:28 -0700104};
105
Austin Schuh58646e22021-08-23 23:51:46 -0700106// Structure to hold both a boot and queue index. Queue indices reset after
107// reboot, so we need to track them.
108struct BootQueueIndex {
109 // Boot number for this queue index.
110 size_t boot = std::numeric_limits<size_t>::max();
111 // Queue index.
112 uint32_t index = std::numeric_limits<uint32_t>::max();
113
114 // Returns a QueueIndex representing an invalid index. Since
115 // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code
116 // and is reserved as an Invalid value, this will never collide.
117 static BootQueueIndex Invalid() {
118 return {.boot = std::numeric_limits<size_t>::max(),
119 .index = std::numeric_limits<uint32_t>::max()};
120 }
121
122 bool operator==(const BootQueueIndex &b2) const {
123 return index == b2.index && boot == b2.boot;
124 }
125 bool operator!=(const BootQueueIndex &b2) const {
126 return index != b2.index || boot != b2.boot;
127 }
128 bool operator<(const BootQueueIndex &b2) const {
129 if (boot == b2.boot) {
130 return index < b2.index;
131 }
132 return boot < b2.boot;
133 }
134 bool operator>(const BootQueueIndex &b2) const {
135 if (boot == b2.boot) {
136 return index > b2.index;
137 }
138 return boot > b2.boot;
139 }
140};
141
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700142std::ostream &operator<<(std::ostream &os,
143 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -0700144std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh58646e22021-08-23 23:51:46 -0700145std::ostream &operator<<(std::ostream &os,
146 const struct BootQueueIndex &queue_index);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700147
148inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
149 if (boot != m2.boot) {
150 return boot < m2.boot;
151 }
152
153 return time < m2.time;
154}
155inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
156 if (boot != m2.boot) {
157 return boot <= m2.boot;
158 }
159
160 return time <= m2.time;
161}
162inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
163 if (boot != m2.boot) {
164 return boot >= m2.boot;
165 }
166
167 return time >= m2.time;
168}
169inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
170 if (boot != m2.boot) {
171 return boot > m2.boot;
172 }
173
174 return time > m2.time;
175}
176
177inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
178 return boot == m2.boot && time == m2.time;
179}
180inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
181 return boot != m2.boot || time != m2.time;
182}
183
184} // namespace aos::logger
185
186#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_