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