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 | |
| 12 | namespace aos { |
| 13 | namespace message_bridge { |
| 14 | |
| 15 | namespace chrono = std::chrono; |
| 16 | |
| 17 | TestingTimeConverter ::TestingTimeConverter(size_t node_count) |
| 18 | : InterpolatedTimeConverter(node_count), |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 19 | last_monotonic_(node_count, logger::BootTimestamp::epoch()) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 20 | CHECK_GE(node_count, 1u); |
| 21 | } |
| 22 | |
| 23 | TestingTimeConverter::~TestingTimeConverter() { |
| 24 | if (at_end_) { |
| 25 | CHECK(!NextTimestamp()) << ": At the end but there is more data."; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void TestingTimeConverter::StartEqual() { |
| 30 | CHECK(first_); |
| 31 | first_ = false; |
| 32 | ts_.emplace_back(std::make_tuple(last_distributed_, last_monotonic_)); |
| 33 | } |
| 34 | |
| 35 | chrono::nanoseconds TestingTimeConverter::AddMonotonic( |
| 36 | std::vector<monotonic_clock::duration> times) { |
| 37 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 38 | for (size_t i = 0; i < times.size(); ++i) { |
| 39 | CHECK_GT(times[i].count(), 0); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 40 | last_monotonic_[i].time += times[i]; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 41 | } |
| 42 | chrono::nanoseconds dt(0); |
| 43 | if (!first_) { |
| 44 | dt = *std::max_element(times.begin(), times.end()); |
| 45 | last_distributed_ += dt; |
| 46 | } else { |
| 47 | first_ = false; |
| 48 | } |
| 49 | ts_.emplace_back(std::make_tuple(last_distributed_, last_monotonic_)); |
| 50 | return dt; |
| 51 | } |
| 52 | |
| 53 | chrono::nanoseconds TestingTimeConverter::AddMonotonic( |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 54 | std::vector<logger::BootTimestamp> times) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 55 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 56 | chrono::nanoseconds dt(0); |
| 57 | if (!first_) { |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 58 | CHECK_EQ(times[0].boot, last_monotonic_[0].boot); |
| 59 | dt = times[0].time - last_monotonic_[0].time; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 60 | for (size_t i = 0; i < times.size(); ++i) { |
| 61 | CHECK_GT(times[i], last_monotonic_[i]); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 62 | dt = std::max(dt, times[i].time - times[0].time); |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 63 | } |
| 64 | last_distributed_ += dt; |
| 65 | last_monotonic_ = times; |
| 66 | } else { |
| 67 | first_ = false; |
| 68 | last_monotonic_ = times; |
| 69 | } |
| 70 | ts_.emplace_back(std::make_tuple(last_distributed_, std::move(times))); |
| 71 | return dt; |
| 72 | } |
| 73 | |
| 74 | void TestingTimeConverter::AddNextTimestamp( |
| 75 | distributed_clock::time_point time, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 76 | std::vector<logger::BootTimestamp> times) { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 77 | CHECK_EQ(times.size(), last_monotonic_.size()); |
| 78 | if (!first_) { |
| 79 | CHECK_GT(time, last_distributed_); |
| 80 | for (size_t i = 0; i < times.size(); ++i) { |
| 81 | CHECK_GT(times[i], last_monotonic_[i]); |
| 82 | } |
| 83 | } else { |
| 84 | first_ = false; |
| 85 | } |
| 86 | last_distributed_ = time; |
| 87 | last_monotonic_ = times; |
| 88 | |
| 89 | ts_.emplace_back(std::make_tuple(time, std::move(times))); |
| 90 | } |
| 91 | |
| 92 | std::optional<std::tuple<distributed_clock::time_point, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame^] | 93 | std::vector<logger::BootTimestamp>>> |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 94 | TestingTimeConverter::NextTimestamp() { |
| 95 | CHECK(!first_) << ": Tried to pull a timestamp before one was added. This " |
| 96 | "is unlikely to be what you want."; |
| 97 | if (ts_.empty()) { |
| 98 | return std::nullopt; |
| 99 | } |
| 100 | auto result = ts_.front(); |
| 101 | ts_.pop_front(); |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | } // namespace message_bridge |
| 106 | } // namespace aos |