blob: 7eead2e8a92fbe0698de04b9ba580a20cf9b69a2 [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"
7
8namespace aos::logger {
9
Austin Schuh66168842021-08-17 19:42:21 -070010// 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.
12struct 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 }
Austin Schuhc1ee1b62022-03-22 17:09:52 -070025 bool operator!=(const BootDuration &m2) const {
26 return boot != m2.boot || duration != m2.duration;
27 }
28
29 static constexpr BootDuration max_time() {
30 return BootDuration{
31 .boot = std::numeric_limits<size_t>::max(),
32 .duration = monotonic_clock::duration(
33 ::std::numeric_limits<monotonic_clock::duration::rep>::max())};
34 }
Austin Schuh66168842021-08-17 19:42:21 -070035};
36
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070037// Simple class representing which boot and what monotonic time in that boot.
38// Boots are assumed to be sequential, and the monotonic clock resets on reboot
39// for all the compare operations.
40struct BootTimestamp {
41 // Boot number for this timestamp.
42 size_t boot = 0u;
43 // Monotonic time in that boot.
44 monotonic_clock::time_point time = monotonic_clock::min_time;
45
Austin Schuh66168842021-08-17 19:42:21 -070046 monotonic_clock::duration time_since_epoch() const {
47 return time.time_since_epoch();
48 }
49
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070050 static constexpr BootTimestamp min_time() {
Austin Schuh66168842021-08-17 19:42:21 -070051 return BootTimestamp{.boot = std::numeric_limits<size_t>::min(),
52 .time = monotonic_clock::min_time};
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070053 }
54 static constexpr BootTimestamp max_time() {
55 return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
56 .time = monotonic_clock::max_time};
57 }
Austin Schuh66168842021-08-17 19:42:21 -070058 static constexpr BootTimestamp epoch() {
59 return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()};
60 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070061
62 // Compare operators. These are implemented such that earlier boots always
63 // compare less than later boots, and the times are only compared in a single
64 // boot.
65 bool operator<(const BootTimestamp &m2) const;
66 bool operator<=(const BootTimestamp &m2) const;
67 bool operator>=(const BootTimestamp &m2) const;
68 bool operator>(const BootTimestamp &m2) const;
69 bool operator==(const BootTimestamp &m2) const;
70 bool operator!=(const BootTimestamp &m2) const;
Austin Schuh66168842021-08-17 19:42:21 -070071
72 BootTimestamp operator+(monotonic_clock::duration d) const {
73 return {boot, time + d};
74 }
75 BootTimestamp operator-(monotonic_clock::duration d) const {
76 return {boot, time - d};
77 }
78 BootTimestamp operator+(BootDuration d) const {
79 return {boot, time + d.duration};
80 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070081};
82
Austin Schuh58646e22021-08-23 23:51:46 -070083// Structure to hold both a boot and queue index. Queue indices reset after
84// reboot, so we need to track them.
85struct BootQueueIndex {
86 // Boot number for this queue index.
87 size_t boot = std::numeric_limits<size_t>::max();
88 // Queue index.
89 uint32_t index = std::numeric_limits<uint32_t>::max();
90
91 // Returns a QueueIndex representing an invalid index. Since
92 // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code
93 // and is reserved as an Invalid value, this will never collide.
94 static BootQueueIndex Invalid() {
95 return {.boot = std::numeric_limits<size_t>::max(),
96 .index = std::numeric_limits<uint32_t>::max()};
97 }
98
99 bool operator==(const BootQueueIndex &b2) const {
100 return index == b2.index && boot == b2.boot;
101 }
102 bool operator!=(const BootQueueIndex &b2) const {
103 return index != b2.index || boot != b2.boot;
104 }
105 bool operator<(const BootQueueIndex &b2) const {
106 if (boot == b2.boot) {
107 return index < b2.index;
108 }
109 return boot < b2.boot;
110 }
111 bool operator>(const BootQueueIndex &b2) const {
112 if (boot == b2.boot) {
113 return index > b2.index;
114 }
115 return boot > b2.boot;
116 }
117};
118
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700119std::ostream &operator<<(std::ostream &os,
120 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -0700121std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh58646e22021-08-23 23:51:46 -0700122std::ostream &operator<<(std::ostream &os,
123 const struct BootQueueIndex &queue_index);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700124
125inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
126 if (boot != m2.boot) {
127 return boot < m2.boot;
128 }
129
130 return time < m2.time;
131}
132inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
133 if (boot != m2.boot) {
134 return boot <= m2.boot;
135 }
136
137 return time <= m2.time;
138}
139inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
140 if (boot != m2.boot) {
141 return boot >= m2.boot;
142 }
143
144 return time >= m2.time;
145}
146inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
147 if (boot != m2.boot) {
148 return boot > m2.boot;
149 }
150
151 return time > m2.time;
152}
153
154inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
155 return boot == m2.boot && time == m2.time;
156}
157inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
158 return boot != m2.boot || time != m2.time;
159}
160
161} // namespace aos::logger
162
163#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_