blob: 64c824a0dc2168b20c8690bdb734d8c5ee86c714 [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 }
25};
26
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070027// Simple class representing which boot and what monotonic time in that boot.
28// Boots are assumed to be sequential, and the monotonic clock resets on reboot
29// for all the compare operations.
30struct BootTimestamp {
31 // Boot number for this timestamp.
32 size_t boot = 0u;
33 // Monotonic time in that boot.
34 monotonic_clock::time_point time = monotonic_clock::min_time;
35
Austin Schuh66168842021-08-17 19:42:21 -070036 monotonic_clock::duration time_since_epoch() const {
37 return time.time_since_epoch();
38 }
39
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070040 static constexpr BootTimestamp min_time() {
Austin Schuh66168842021-08-17 19:42:21 -070041 return BootTimestamp{.boot = std::numeric_limits<size_t>::min(),
42 .time = monotonic_clock::min_time};
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070043 }
44 static constexpr BootTimestamp max_time() {
45 return BootTimestamp{.boot = std::numeric_limits<size_t>::max(),
46 .time = monotonic_clock::max_time};
47 }
Austin Schuh66168842021-08-17 19:42:21 -070048 static constexpr BootTimestamp epoch() {
49 return BootTimestamp{.boot = 0, .time = monotonic_clock::epoch()};
50 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070051
52 // Compare operators. These are implemented such that earlier boots always
53 // compare less than later boots, and the times are only compared in a single
54 // boot.
55 bool operator<(const BootTimestamp &m2) const;
56 bool operator<=(const BootTimestamp &m2) const;
57 bool operator>=(const BootTimestamp &m2) const;
58 bool operator>(const BootTimestamp &m2) const;
59 bool operator==(const BootTimestamp &m2) const;
60 bool operator!=(const BootTimestamp &m2) const;
Austin Schuh66168842021-08-17 19:42:21 -070061
62 BootTimestamp operator+(monotonic_clock::duration d) const {
63 return {boot, time + d};
64 }
65 BootTimestamp operator-(monotonic_clock::duration d) const {
66 return {boot, time - d};
67 }
68 BootTimestamp operator+(BootDuration d) const {
69 return {boot, time + d.duration};
70 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070071};
72
73std::ostream &operator<<(std::ostream &os,
74 const struct BootTimestamp &timestamp);
Austin Schuh66168842021-08-17 19:42:21 -070075std::ostream &operator<<(std::ostream &os, const struct BootDuration &duration);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070076
77inline bool BootTimestamp::operator<(const BootTimestamp &m2) const {
78 if (boot != m2.boot) {
79 return boot < m2.boot;
80 }
81
82 return time < m2.time;
83}
84inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const {
85 if (boot != m2.boot) {
86 return boot <= m2.boot;
87 }
88
89 return time <= m2.time;
90}
91inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const {
92 if (boot != m2.boot) {
93 return boot >= m2.boot;
94 }
95
96 return time >= m2.time;
97}
98inline bool BootTimestamp::operator>(const BootTimestamp &m2) const {
99 if (boot != m2.boot) {
100 return boot > m2.boot;
101 }
102
103 return time > m2.time;
104}
105
106inline bool BootTimestamp::operator==(const BootTimestamp &m2) const {
107 return boot == m2.boot && time == m2.time;
108}
109inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const {
110 return boot != m2.boot || time != m2.time;
111}
112
113} // namespace aos::logger
114
115#endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_