blob: d0fde73ee7f4ba5445e2449b608ec85081acedfc [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
Austin Schuhefba96d2022-06-24 13:22:18 -070022 BootDuration operator-() const { return {boot, -duration}; }
23 BootDuration operator-(monotonic_clock::duration d) const {
24 return {boot, duration - d};
25 }
26
Austin Schuh66168842021-08-17 19:42:21 -070027 bool operator==(const BootDuration &m2) const {
28 return boot == m2.boot && duration == m2.duration;
29 }
Austin Schuhc1ee1b62022-03-22 17:09:52 -070030 bool operator!=(const BootDuration &m2) const {
31 return boot != m2.boot || duration != m2.duration;
32 }
33
34 static constexpr BootDuration max_time() {
35 return BootDuration{
36 .boot = std::numeric_limits<size_t>::max(),
37 .duration = monotonic_clock::duration(
38 ::std::numeric_limits<monotonic_clock::duration::rep>::max())};
39 }
Austin Schuh66168842021-08-17 19:42:21 -070040};
41
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070042// Simple class representing which boot and what monotonic time in that boot.
43// Boots are assumed to be sequential, and the monotonic clock resets on reboot
44// for all the compare operations.
45struct BootTimestamp {
46 // Boot number for this timestamp.
47 size_t boot = 0u;
48 // Monotonic time in that boot.
49 monotonic_clock::time_point time = monotonic_clock::min_time;
50
Austin Schuh66168842021-08-17 19:42:21 -070051 monotonic_clock::duration time_since_epoch() const {
52 return time.time_since_epoch();
53 }
54
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070055 static constexpr BootTimestamp min_time() {
Austin Schuh66168842021-08-17 19:42:21 -070056 return BootTimestamp{.boot = std::numeric_limits<size_t>::min(),
57 .time = monotonic_clock::min_time};
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070058 }
59 static constexpr BootTimestamp max_time() {
60 return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
61 .time = monotonic_clock::max_time};
62 }
Austin Schuh66168842021-08-17 19:42:21 -070063 static constexpr BootTimestamp epoch() {
64 return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()};
65 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070066
67 // Compare operators. These are implemented such that earlier boots always
68 // compare less than later boots, and the times are only compared in a single
69 // boot.
70 bool operator<(const BootTimestamp &m2) const;
71 bool operator<=(const BootTimestamp &m2) const;
72 bool operator>=(const BootTimestamp &m2) const;
73 bool operator>(const BootTimestamp &m2) const;
74 bool operator==(const BootTimestamp &m2) const;
75 bool operator!=(const BootTimestamp &m2) const;
Austin Schuh66168842021-08-17 19:42:21 -070076
77 BootTimestamp operator+(monotonic_clock::duration d) const {
78 return {boot, time + d};
79 }
80 BootTimestamp operator-(monotonic_clock::duration d) const {
81 return {boot, time - d};
82 }
83 BootTimestamp operator+(BootDuration d) const {
84 return {boot, time + d.duration};
85 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070086};
87
Austin Schuh58646e22021-08-23 23:51:46 -070088// Structure to hold both a boot and queue index. Queue indices reset after
89// reboot, so we need to track them.
90struct BootQueueIndex {
91 // Boot number for this queue index.
92 size_t boot = std::numeric_limits<size_t>::max();
93 // Queue index.
94 uint32_t index = std::numeric_limits<uint32_t>::max();
95
96 // Returns a QueueIndex representing an invalid index. Since
97 // std::numeric_limits<uint32_t>::max() is never used in the QueueIndex code
98 // and is reserved as an Invalid value, this will never collide.
99 static BootQueueIndex Invalid() {
100 return {.boot = std::numeric_limits<size_t>::max(),
101 .index = std::numeric_limits<uint32_t>::max()};
102 }
103
104 bool operator==(const BootQueueIndex &b2) const {
105 return index == b2.index && boot == b2.boot;
106 }
107 bool operator!=(const BootQueueIndex &b2) const {
108 return index != b2.index || boot != b2.boot;
109 }
110 bool operator<(const BootQueueIndex &b2) const {
111 if (boot == b2.boot) {
112 return index < b2.index;
113 }
114 return boot < b2.boot;
115 }
116 bool operator>(const BootQueueIndex &b2) const {
117 if (boot == b2.boot) {
118 return index > b2.index;
119 }
120 return boot > b2.boot;
121 }
122};
123
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700124std::ostream &operator<<(std::ostream &os,
125 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -0700126std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh58646e22021-08-23 23:51:46 -0700127std::ostream &operator<<(std::ostream &os,
128 const struct BootQueueIndex &queue_index);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700129
130inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
131 if (boot != m2.boot) {
132 return boot < m2.boot;
133 }
134
135 return time < m2.time;
136}
137inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
138 if (boot != m2.boot) {
139 return boot <= m2.boot;
140 }
141
142 return time <= m2.time;
143}
144inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
145 if (boot != m2.boot) {
146 return boot >= m2.boot;
147 }
148
149 return time >= m2.time;
150}
151inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
152 if (boot != m2.boot) {
153 return boot > m2.boot;
154 }
155
156 return time > m2.time;
157}
158
159inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
160 return boot == m2.boot && time == m2.time;
161}
162inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
163 return boot != m2.boot || time != m2.time;
164}
165
166} // namespace aos::logger
167
168#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_