Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_TESTING_TIME_CONVERTER_H_ |
| 2 | #define AOS_NETWORK_TESTING_TIME_CONVERTER_H_ |
| 3 | |
| 4 | #include <deque> |
| 5 | #include <optional> |
| 6 | #include <tuple> |
| 7 | |
| 8 | #include "aos/events/event_scheduler.h" |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 9 | #include "aos/events/logging/boot_timestamp.h" |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 10 | #include "aos/network/multinode_timestamp_filter.h" |
| 11 | #include "aos/time/time.h" |
| 12 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 13 | namespace aos::message_bridge { |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 14 | |
| 15 | // Simple class to which uses InterpolatedTimeConverter to produce an |
| 16 | // interpolated timeline. Should only be used for testing. |
| 17 | class TestingTimeConverter final : public InterpolatedTimeConverter { |
| 18 | public: |
| 19 | TestingTimeConverter(size_t node_count); |
| 20 | |
| 21 | virtual ~TestingTimeConverter(); |
| 22 | |
| 23 | // Starts all nodes equal to the distributed clock. |
| 24 | void StartEqual(); |
| 25 | |
| 26 | // Elapses each node's clock by the provided duration, and returns the |
| 27 | // duration that the distributed clock elapsed by. |
| 28 | std::chrono::nanoseconds AddMonotonic( |
| 29 | std::vector<monotonic_clock::duration> times); |
| 30 | |
| 31 | // Sets time on each node's clock to the provided times, and returns the |
| 32 | // duration that the distributed clock elapsed by. Note: time must always go |
| 33 | // forwards. |
| 34 | std::chrono::nanoseconds AddMonotonic( |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 35 | std::vector<logger::BootTimestamp> times); |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 37 | void RebootAt(size_t node_index, distributed_clock::time_point t); |
| 38 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 39 | // Adds a distributed to monotonic clock mapping to the queue. |
| 40 | void AddNextTimestamp(distributed_clock::time_point time, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 41 | std::vector<logger::BootTimestamp> times); |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 42 | |
James Kuszmaul | beaa3c8 | 2023-09-07 11:11:27 -0700 | [diff] [blame] | 43 | std::optional<std::optional<std::tuple<distributed_clock::time_point, |
| 44 | std::vector<logger::BootTimestamp>>>> |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 45 | NextTimestamp() override; |
| 46 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 47 | void set_boot_uuid(size_t node_index, size_t boot_count, UUID uuid) { |
| 48 | CHECK(boot_uuids_ |
| 49 | .emplace(std::make_pair(node_index, boot_count), std ::move(uuid)) |
| 50 | .second) |
| 51 | << ": Duplicate boot"; |
| 52 | } |
| 53 | |
| 54 | UUID boot_uuid(size_t node_index, size_t boot_count) override { |
| 55 | auto it = boot_uuids_.find(std::make_pair(node_index, boot_count)); |
| 56 | if (it != boot_uuids_.end()) return it->second; |
| 57 | |
| 58 | auto new_it = boot_uuids_.emplace(std::make_pair(node_index, boot_count), |
| 59 | UUID::Random()); |
| 60 | CHECK(new_it.second); |
| 61 | return new_it.first->second; |
| 62 | } |
| 63 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 64 | private: |
| 65 | // List of timestamps. |
| 66 | std::deque<std::tuple<distributed_clock::time_point, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 67 | std::vector<logger::BootTimestamp>>> |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 68 | ts_; |
| 69 | |
| 70 | // True if there is no time queued. |
| 71 | bool first_ = true; |
| 72 | // The last times returned on all clocks. |
| 73 | distributed_clock::time_point last_distributed_ = distributed_clock::epoch(); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 74 | std::vector<logger::BootTimestamp> last_monotonic_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 75 | |
| 76 | std::map<std::pair<size_t, size_t>, UUID> boot_uuids_; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 79 | } // namespace aos::message_bridge |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 80 | |
| 81 | #endif // AOS_NETWORK_TESTING_TIME_CONVERTER_H_ |