Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |
| 3 | |
| 4 | #include <algorithm> |
Brian Silverman | 601b972 | 2020-06-18 14:33:43 -0700 | [diff] [blame^] | 5 | #include <functional> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include <map> |
| 7 | #include <memory> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 8 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include <unordered_set> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "absl/container/btree_map.h" |
| 14 | #include "aos/events/event_loop.h" |
| 15 | #include "aos/events/event_scheduler.h" |
Austin Schuh | e1dafe4 | 2020-01-06 21:12:03 -0800 | [diff] [blame] | 16 | #include "aos/events/simple_channel.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "aos/flatbuffer_merge.h" |
| 18 | #include "aos/flatbuffers.h" |
| 19 | #include "aos/ipc_lib/index.h" |
| 20 | #include "glog/logging.h" |
| 21 | |
| 22 | namespace aos { |
| 23 | |
| 24 | // Class for simulated fetchers. |
| 25 | class SimulatedChannel; |
| 26 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 27 | class NodeEventLoopFactory; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 28 | namespace message_bridge { |
| 29 | class SimulatedMessageBridge; |
| 30 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 31 | |
| 32 | // There are 2 concepts needed to support multi-node simulations. |
| 33 | // 1) The node. This is implemented with NodeEventLoopFactory. |
| 34 | // 2) The "robot" which runs multiple nodes. This is implemented with |
| 35 | // SimulatedEventLoopFactory. |
| 36 | // |
| 37 | // To make things easier, SimulatedEventLoopFactory takes an optional Node |
| 38 | // argument if you want to make event loops without interacting with the |
| 39 | // NodeEventLoopFactory object. |
| 40 | // |
| 41 | // The basic flow goes something like as follows: |
| 42 | // |
| 43 | // SimulatedEventLoopFactory factory(config); |
| 44 | // const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1"); |
| 45 | // std::unique_ptr<EventLoop> event_loop = factory.MakeEventLoop("ping", pi1); |
| 46 | // |
| 47 | // Or |
| 48 | // |
| 49 | // SimulatedEventLoopFactory factory(config); |
| 50 | // const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1"); |
| 51 | // NodeEventLoopFactory *pi1_factory = factory.GetNodeEventLoopFactory(pi1); |
| 52 | // std::unique_ptr<EventLoop> event_loop = pi1_factory.MakeEventLoop("ping"); |
| 53 | // |
| 54 | // The distributed_clock is used to be the base time. NodeEventLoopFactory has |
| 55 | // all the information needed to adjust both the realtime and monotonic clocks |
| 56 | // relative to the distributed_clock. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 57 | class SimulatedEventLoopFactory { |
| 58 | public: |
| 59 | // Constructs a SimulatedEventLoopFactory with the provided configuration. |
| 60 | // This configuration must remain in scope for the lifetime of the factory and |
| 61 | // all sub-objects. |
| 62 | SimulatedEventLoopFactory(const Configuration *configuration); |
| 63 | ~SimulatedEventLoopFactory(); |
| 64 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 65 | // Creates an event loop. If running in a multi-node environment, node needs |
| 66 | // to point to the node to create this event loop on. |
| 67 | ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name, |
| 68 | const Node *node = nullptr); |
| 69 | |
| 70 | // Returns the NodeEventLoopFactory for the provided node. The returned |
| 71 | // NodeEventLoopFactory is owned by the SimulatedEventLoopFactory and has a |
| 72 | // lifetime identical to the factory. |
| 73 | NodeEventLoopFactory *GetNodeEventLoopFactory(const Node *node); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | |
| 75 | // Starts executing the event loops unconditionally. |
| 76 | void Run(); |
| 77 | // Executes the event loops for a duration. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 78 | void RunFor(distributed_clock::duration duration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | |
| 80 | // Stops executing all event loops. Meant to be called from within an event |
| 81 | // loop handler. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 82 | void Exit() { scheduler_scheduler_.Exit(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 84 | const std::vector<const Node *> &nodes() const { return nodes_; } |
| 85 | |
| 86 | // Sets the simulated send delay for all messages sent within a single node. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 87 | void set_send_delay(std::chrono::nanoseconds send_delay); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 88 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 89 | |
| 90 | // Sets the simulated network delay for messages forwarded between nodes. |
Brian Silverman | a7c6205 | 2020-04-28 16:52:27 -0700 | [diff] [blame] | 91 | void set_network_delay(std::chrono::nanoseconds network_delay) { |
| 92 | network_delay_ = network_delay; |
| 93 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 94 | std::chrono::nanoseconds network_delay() const { return network_delay_; } |
| 95 | |
| 96 | // Returns the clock used to synchronize the nodes. |
| 97 | distributed_clock::time_point distributed_now() const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 98 | return scheduler_scheduler_.distributed_now(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Returns the configuration used for everything. |
| 102 | const Configuration *configuration() const { return configuration_; } |
| 103 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 104 | // Disables forwarding for this channel. This should be used very rarely only |
| 105 | // for things like the logger. |
| 106 | void DisableForwarding(const Channel *channel); |
| 107 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 108 | private: |
| 109 | const Configuration *const configuration_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 110 | EventSchedulerScheduler scheduler_scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 111 | // List of event loops to manage running and not running for. |
| 112 | // The function is a callback used to set and clear the running bool on each |
| 113 | // event loop. |
| 114 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 115 | raw_event_loops_; |
| 116 | |
| 117 | std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50); |
| 118 | std::chrono::nanoseconds network_delay_ = std::chrono::microseconds(100); |
| 119 | |
| 120 | std::vector<std::unique_ptr<NodeEventLoopFactory>> node_factories_; |
| 121 | |
| 122 | std::vector<const Node *> nodes_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 123 | |
| 124 | std::unique_ptr<message_bridge::SimulatedMessageBridge> bridge_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | // This class holds all the state required to be a single node. |
| 128 | class NodeEventLoopFactory { |
| 129 | public: |
| 130 | ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 131 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 132 | // Returns the node that this factory is running as, or nullptr if this is a |
| 133 | // single node setup. |
| 134 | const Node *node() const { return node_; } |
| 135 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 136 | // Sets realtime clock to realtime_now for a given monotonic clock. |
| 137 | void SetRealtimeOffset(monotonic_clock::time_point monotonic_now, |
| 138 | realtime_clock::time_point realtime_now) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 139 | realtime_offset_ = |
| 140 | realtime_now.time_since_epoch() - monotonic_now.time_since_epoch(); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 143 | // Returns the current time on both clocks. |
| 144 | inline monotonic_clock::time_point monotonic_now() const; |
| 145 | inline realtime_clock::time_point realtime_now() const; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 146 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 147 | // Returns the simulated network delay for messages forwarded between nodes. |
| 148 | std::chrono::nanoseconds network_delay() const { |
| 149 | return factory_->network_delay(); |
| 150 | } |
| 151 | // Returns the simulated send delay for all messages sent within a single |
| 152 | // node. |
| 153 | std::chrono::nanoseconds send_delay() const { return factory_->send_delay(); } |
| 154 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 155 | // TODO(austin): Private for the following? |
| 156 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 157 | // Converts a time to the distributed clock for scheduling and cross-node time |
| 158 | // measurement. |
| 159 | inline distributed_clock::time_point ToDistributedClock( |
| 160 | monotonic_clock::time_point time) const; |
| 161 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 162 | // Sets the offset between the monotonic clock and the central distributed |
| 163 | // clock. distributed_clock = monotonic_clock + offset. |
| 164 | void SetDistributedOffset(std::chrono::nanoseconds monotonic_offset) { |
| 165 | scheduler_.SetDistributedOffset(monotonic_offset); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 168 | private: |
| 169 | friend class SimulatedEventLoopFactory; |
| 170 | NodeEventLoopFactory( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 171 | EventSchedulerScheduler *scheduler_scheduler, |
| 172 | SimulatedEventLoopFactory *factory, const Node *node, |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 173 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 174 | *raw_event_loops); |
| 175 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 176 | EventScheduler scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 177 | SimulatedEventLoopFactory *const factory_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 178 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 179 | const Node *const node_; |
| 180 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 181 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 182 | *const raw_event_loops_; |
| 183 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 184 | std::chrono::nanoseconds realtime_offset_ = std::chrono::seconds(0); |
| 185 | |
| 186 | // Map from name, type to queue. |
| 187 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_; |
| 188 | |
| 189 | // pid so we get unique timing reports. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 190 | pid_t tid_ = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 193 | inline monotonic_clock::time_point NodeEventLoopFactory::monotonic_now() const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 194 | // TODO(austin): Confirm that time never goes backwards? |
| 195 | return scheduler_.FromDistributedClock(factory_->distributed_now()); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | inline realtime_clock::time_point NodeEventLoopFactory::realtime_now() const { |
| 199 | return realtime_clock::time_point(monotonic_now().time_since_epoch() + |
| 200 | realtime_offset_); |
| 201 | } |
| 202 | |
| 203 | inline distributed_clock::time_point NodeEventLoopFactory::ToDistributedClock( |
| 204 | monotonic_clock::time_point time) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 205 | return scheduler_.ToDistributedClock(time); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | } // namespace aos |
| 209 | |
| 210 | #endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |