Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 1 | #include "aos/network/testing_time_converter.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <deque> |
| 5 | #include <optional> |
| 6 | #include <tuple> |
| 7 | |
| 8 | #include "aos/events/event_scheduler.h" |
| 9 | #include "aos/network/multinode_timestamp_filter.h" |
| 10 | #include "aos/time/time.h" |
| 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace aos::message_bridge { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
| 16 | TestingTimeConverter ::TestingTimeConverter(size_t node_count) |
| 17 | : InterpolatedTimeConverter(node_count), |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 18 | last_monotonic_(node_count, logger::BootTimestamp::epoch()) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 19 | CHECK_GE(node_count, 1u); |
| 20 | } |
| 21 | |
| 22 | TestingTimeConverter::~TestingTimeConverter() { |
| 23 | if (at_end_) { |
James Kuszmaul | beaa3c8 | 2023-09-07 11:11:27 -0700 | [diff] [blame] | 24 | auto next_timestamp = NextTimestamp(); |
| 25 | CHECK(next_timestamp.has_value()) << ": Unexpected error"; |
| 26 | CHECK(!next_timestamp.value().has_value()) |
| 27 | << ": At the end but there is more data."; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | void TestingTimeConverter::StartEqual() { |
| 32 | CHECK(first_); |
| 33 | first_ = false; |
| 34 | ts_.emplace_back(std::make_tuple(last_distributed_, last_monotonic_)); |
| 35 | } |
| 36 | |
| 37 | chrono::nanoseconds TestingTimeConverter::AddMonotonic( |
| 38 | std::vector<monotonic_clock::duration> times) { |
| 39 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 40 | for (size_t i = 0; i < times.size(); ++i) { |
| 41 | CHECK_GT(times[i].count(), 0); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 42 | last_monotonic_[i].time += times[i]; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 43 | } |
| 44 | chrono::nanoseconds dt(0); |
| 45 | if (!first_) { |
| 46 | dt = *std::max_element(times.begin(), times.end()); |
| 47 | last_distributed_ += dt; |
| 48 | } else { |
| 49 | first_ = false; |
| 50 | } |
| 51 | ts_.emplace_back(std::make_tuple(last_distributed_, last_monotonic_)); |
| 52 | return dt; |
| 53 | } |
| 54 | |
| 55 | chrono::nanoseconds TestingTimeConverter::AddMonotonic( |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 56 | std::vector<logger::BootTimestamp> times) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 57 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 58 | chrono::nanoseconds dt(0); |
| 59 | if (!first_) { |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 60 | CHECK_EQ(times[0].boot, last_monotonic_[0].boot); |
| 61 | dt = times[0].time - last_monotonic_[0].time; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 62 | for (size_t i = 0; i < times.size(); ++i) { |
| 63 | CHECK_GT(times[i], last_monotonic_[i]); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 64 | dt = std::max(dt, times[i].time - times[0].time); |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 65 | } |
| 66 | last_distributed_ += dt; |
| 67 | last_monotonic_ = times; |
| 68 | } else { |
| 69 | first_ = false; |
| 70 | last_monotonic_ = times; |
| 71 | } |
| 72 | ts_.emplace_back(std::make_tuple(last_distributed_, std::move(times))); |
| 73 | return dt; |
| 74 | } |
| 75 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 76 | void TestingTimeConverter::RebootAt(size_t node_index, |
| 77 | distributed_clock::time_point t) { |
| 78 | CHECK(!first_); |
| 79 | const chrono::nanoseconds dt = t - last_distributed_; |
| 80 | |
| 81 | for (size_t i = 0; i < last_monotonic_.size(); ++i) { |
| 82 | last_monotonic_[i].time += dt; |
| 83 | } |
| 84 | |
| 85 | ++last_monotonic_[node_index].boot; |
| 86 | last_monotonic_[node_index].time = monotonic_clock::epoch(); |
| 87 | |
| 88 | last_distributed_ = t; |
| 89 | ts_.emplace_back(std::make_tuple(last_distributed_, last_monotonic_)); |
| 90 | } |
| 91 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 92 | void TestingTimeConverter::AddNextTimestamp( |
| 93 | distributed_clock::time_point time, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 94 | std::vector<logger::BootTimestamp> times) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 95 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 96 | if (!first_) { |
| 97 | CHECK_GT(time, last_distributed_); |
| 98 | for (size_t i = 0; i < times.size(); ++i) { |
| 99 | CHECK_GT(times[i], last_monotonic_[i]); |
| 100 | } |
| 101 | } else { |
| 102 | first_ = false; |
| 103 | } |
| 104 | last_distributed_ = time; |
| 105 | last_monotonic_ = times; |
| 106 | |
| 107 | ts_.emplace_back(std::make_tuple(time, std::move(times))); |
| 108 | } |
| 109 | |
James Kuszmaul | beaa3c8 | 2023-09-07 11:11:27 -0700 | [diff] [blame] | 110 | std::optional<std::optional<std::tuple<distributed_clock::time_point, |
| 111 | std::vector<logger::BootTimestamp>>>> |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 112 | TestingTimeConverter::NextTimestamp() { |
| 113 | CHECK(!first_) << ": Tried to pull a timestamp before one was added. This " |
| 114 | "is unlikely to be what you want."; |
| 115 | if (ts_.empty()) { |
James Kuszmaul | beaa3c8 | 2023-09-07 11:11:27 -0700 | [diff] [blame] | 116 | std::optional<std::optional<std::tuple<distributed_clock::time_point, |
| 117 | std::vector<logger::BootTimestamp>>>> |
| 118 | result; |
| 119 | result.emplace(std::nullopt); |
| 120 | return result; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 121 | } |
| 122 | auto result = ts_.front(); |
| 123 | ts_.pop_front(); |
| 124 | return result; |
| 125 | } |
| 126 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 127 | } // namespace aos::message_bridge |