blob: e9c7877a8e906876d71332807a9ebdd0d7ca825c [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
Austin Schuh704784e2022-07-13 21:11:41 -07006#include "glog/logging.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07007
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "aos/time/time.h"
9
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070010namespace aos::logger {
11
Austin Schuh66168842021-08-17 19:42:21 -070012// Simple class representing a duration in time and a boot it is from. This
13// gives us something to use for storing the time offset when filtering.
14struct BootDuration {
15 // Boot number for this timestamp.
16 size_t boot = 0u;
17 // Monotonic time in that boot.
18 monotonic_clock::duration duration{0};
19
20 BootDuration operator+(monotonic_clock::duration d) const {
21 return {boot, duration + d};
22 }
23
Austin Schuhefba96d2022-06-24 13:22:18 -070024 BootDuration operator-() const { return {boot, -duration}; }
25 BootDuration operator-(monotonic_clock::duration d) const {
26 return {boot, duration - d};
27 }
28
Austin Schuh704784e2022-07-13 21:11:41 -070029 BootDuration operator-(BootDuration d) const {
30 CHECK_EQ(d.boot, boot);
31 return {boot, duration - d.duration};
32 }
33
34 BootDuration operator+(BootDuration d) const {
35 CHECK_EQ(d.boot, boot);
36 return {boot, duration + d.duration};
37 }
38
39 BootDuration operator/(int x) const { return {boot, duration / x}; }
40
Austin Schuh66168842021-08-17 19:42:21 -070041 bool operator==(const BootDuration &m2) const {
42 return boot == m2.boot && duration == m2.duration;
43 }
Austin Schuhc1ee1b62022-03-22 17:09:52 -070044 bool operator!=(const BootDuration &m2) const {
45 return boot != m2.boot || duration != m2.duration;
46 }
47
48 static constexpr BootDuration max_time() {
49 return BootDuration{
50 .boot = std::numeric_limits<size_t>::max(),
51 .duration = monotonic_clock::duration(
52 ::std::numeric_limits<monotonic_clock::duration::rep>::max())};
53 }
Austin Schuh66168842021-08-17 19:42:21 -070054};
55
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070056// Simple class representing which boot and what monotonic time in that boot.
57// Boots are assumed to be sequential, and the monotonic clock resets on reboot
58// for all the compare operations.
59struct BootTimestamp {
60 // Boot number for this timestamp.
61 size_t boot = 0u;
62 // Monotonic time in that boot.
63 monotonic_clock::time_point time = monotonic_clock::min_time;
64
Austin Schuh66168842021-08-17 19:42:21 -070065 monotonic_clock::duration time_since_epoch() const {
66 return time.time_since_epoch();
67 }
68
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070069 static constexpr BootTimestamp min_time() {
Austin Schuh66168842021-08-17 19:42:21 -070070 return BootTimestamp{.boot = std::numeric_limits<size_t>::min(),
71 .time = monotonic_clock::min_time};
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070072 }
73 static constexpr BootTimestamp max_time() {
74 return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
75 .time = monotonic_clock::max_time};
76 }
Austin Schuh66168842021-08-17 19:42:21 -070077 static constexpr BootTimestamp epoch() {
78 return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()};
79 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070080
81 // Compare operators. These are implemented such that earlier boots always
82 // compare less than later boots, and the times are only compared in a single
83 // boot.
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;
89 bool operator!=(const BootTimestamp &m2) const;
Austin Schuh66168842021-08-17 19:42:21 -070090
91 BootTimestamp operator+(monotonic_clock::duration d) const {
92 return {boot, time + d};
93 }
Austin Schuhf1dace42022-07-13 22:37:12 -070094
95 BootTimestamp operator+=(monotonic_clock::duration d) {
96 time += d;
97 return *this;
98 }
Austin Schuh66168842021-08-17 19:42:21 -070099 BootTimestamp operator-(monotonic_clock::duration d) const {
100 return {boot, time - d};
101 }
102 BootTimestamp operator+(BootDuration d) const {
103 return {boot, time + d.duration};
104 }
Austin Schuh153bc6c2022-07-14 18:17:25 -0700105
106 BootDuration operator-(BootTimestamp t) const {
107 CHECK_EQ(t.boot, boot);
108 return {boot, time - t.time};
109 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700110};
111
Austin Schuh58646e22021-08-23 23:51:46 -0700112// Structure to hold both a boot and queue index. Queue indices reset after
113// reboot, so we need to track them.
114struct BootQueueIndex {
115 // Boot number for this queue index.
116 size_t boot = std::numeric_limits<size_t>::max();
117 // Queue index.
118 uint32_t index = std::numeric_limits<uint32_t>::max();
119
120 // Returns a QueueIndex representing an invalid index. Since
121 // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code
122 // and is reserved as an Invalid value, this will never collide.
123 static BootQueueIndex Invalid() {
124 return {.boot = std::numeric_limits<size_t>::max(),
125 .index = std::numeric_limits<uint32_t>::max()};
126 }
127
128 bool operator==(const BootQueueIndex &b2) const {
129 return index == b2.index && boot == b2.boot;
130 }
131 bool operator!=(const BootQueueIndex &b2) const {
132 return index != b2.index || 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 bool operator>(const BootQueueIndex &b2) const {
141 if (boot == b2.boot) {
142 return index > b2.index;
143 }
144 return boot > b2.boot;
145 }
146};
147
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700148std::ostream &operator<<(std::ostream &os,
149 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -0700150std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh58646e22021-08-23 23:51:46 -0700151std::ostream &operator<<(std::ostream &os,
152 const struct BootQueueIndex &queue_index);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700153
154inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
155 if (boot != m2.boot) {
156 return boot < m2.boot;
157 }
158
159 return time < m2.time;
160}
161inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
162 if (boot != m2.boot) {
163 return boot <= m2.boot;
164 }
165
166 return time <= m2.time;
167}
168inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
169 if (boot != m2.boot) {
170 return boot >= m2.boot;
171 }
172
173 return time >= m2.time;
174}
175inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
176 if (boot != m2.boot) {
177 return boot > m2.boot;
178 }
179
180 return time > m2.time;
181}
182
183inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
184 return boot == m2.boot && time == m2.time;
185}
186inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
187 return boot != m2.boot || time != m2.time;
188}
189
190} // namespace aos::logger
191
192#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_