blob: 7718cfbe7dff8c3d310ff632ce6a95729e1c22ae [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_SIMULATED_EVENT_LOOP_H_
2#define AOS_EVENTS_SIMULATED_EVENT_LOOP_H_
3
4#include <algorithm>
5#include <map>
6#include <memory>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08007#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include <unordered_set>
9#include <utility>
10#include <vector>
11
12#include "absl/container/btree_map.h"
13#include "aos/events/event_loop.h"
14#include "aos/events/event_scheduler.h"
Austin Schuhe1dafe42020-01-06 21:12:03 -080015#include "aos/events/simple_channel.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/flatbuffer_merge.h"
17#include "aos/flatbuffers.h"
18#include "aos/ipc_lib/index.h"
19#include "glog/logging.h"
20
21namespace aos {
22
23// Class for simulated fetchers.
24class SimulatedChannel;
25
Austin Schuhac0771c2020-01-07 18:36:30 -080026class NodeEventLoopFactory;
Austin Schuh898f4972020-01-11 17:21:25 -080027namespace message_bridge {
28class SimulatedMessageBridge;
29}
Austin Schuhac0771c2020-01-07 18:36:30 -080030
31// There are 2 concepts needed to support multi-node simulations.
32// 1) The node. This is implemented with NodeEventLoopFactory.
33// 2) The "robot" which runs multiple nodes. This is implemented with
34// SimulatedEventLoopFactory.
35//
36// To make things easier, SimulatedEventLoopFactory takes an optional Node
37// argument if you want to make event loops without interacting with the
38// NodeEventLoopFactory object.
39//
40// The basic flow goes something like as follows:
41//
42// SimulatedEventLoopFactory factory(config);
43// const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1");
44// std::unique_ptr<EventLoop> event_loop = factory.MakeEventLoop("ping", pi1);
45//
46// Or
47//
48// SimulatedEventLoopFactory factory(config);
49// const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1");
50// NodeEventLoopFactory *pi1_factory = factory.GetNodeEventLoopFactory(pi1);
51// std::unique_ptr<EventLoop> event_loop = pi1_factory.MakeEventLoop("ping");
52//
53// The distributed_clock is used to be the base time. NodeEventLoopFactory has
54// all the information needed to adjust both the realtime and monotonic clocks
55// relative to the distributed_clock.
Alex Perrycb7da4b2019-08-28 19:35:56 -070056class SimulatedEventLoopFactory {
57 public:
58 // Constructs a SimulatedEventLoopFactory with the provided configuration.
59 // This configuration must remain in scope for the lifetime of the factory and
60 // all sub-objects.
61 SimulatedEventLoopFactory(const Configuration *configuration);
62 ~SimulatedEventLoopFactory();
63
Austin Schuhac0771c2020-01-07 18:36:30 -080064 // Creates an event loop. If running in a multi-node environment, node needs
65 // to point to the node to create this event loop on.
66 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name,
67 const Node *node = nullptr);
68
69 // Returns the NodeEventLoopFactory for the provided node. The returned
70 // NodeEventLoopFactory is owned by the SimulatedEventLoopFactory and has a
71 // lifetime identical to the factory.
72 NodeEventLoopFactory *GetNodeEventLoopFactory(const Node *node);
Alex Perrycb7da4b2019-08-28 19:35:56 -070073
74 // Starts executing the event loops unconditionally.
75 void Run();
76 // Executes the event loops for a duration.
Austin Schuhac0771c2020-01-07 18:36:30 -080077 void RunFor(distributed_clock::duration duration);
Alex Perrycb7da4b2019-08-28 19:35:56 -070078
79 // Stops executing all event loops. Meant to be called from within an event
80 // loop handler.
Austin Schuh8bd96322020-02-13 21:18:22 -080081 void Exit() { scheduler_scheduler_.Exit(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -070082
Austin Schuhac0771c2020-01-07 18:36:30 -080083 const std::vector<const Node *> &nodes() const { return nodes_; }
84
85 // Sets the simulated send delay for all messages sent within a single node.
Austin Schuh7d87b672019-12-01 20:23:49 -080086 void set_send_delay(std::chrono::nanoseconds send_delay);
Austin Schuhac0771c2020-01-07 18:36:30 -080087 std::chrono::nanoseconds send_delay() const { return send_delay_; }
88
89 // Sets the simulated network delay for messages forwarded between nodes.
Brian Silvermana7c62052020-04-28 16:52:27 -070090 void set_network_delay(std::chrono::nanoseconds network_delay) {
91 network_delay_ = network_delay;
92 }
Austin Schuhac0771c2020-01-07 18:36:30 -080093 std::chrono::nanoseconds network_delay() const { return network_delay_; }
94
95 // Returns the clock used to synchronize the nodes.
96 distributed_clock::time_point distributed_now() const {
Austin Schuh8bd96322020-02-13 21:18:22 -080097 return scheduler_scheduler_.distributed_now();
Austin Schuhac0771c2020-01-07 18:36:30 -080098 }
99
100 // Returns the configuration used for everything.
101 const Configuration *configuration() const { return configuration_; }
102
Austin Schuh6f3babe2020-01-26 20:34:50 -0800103 // Disables forwarding for this channel. This should be used very rarely only
104 // for things like the logger.
105 void DisableForwarding(const Channel *channel);
106
Austin Schuhac0771c2020-01-07 18:36:30 -0800107 private:
108 const Configuration *const configuration_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800109 EventSchedulerScheduler scheduler_scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800110 // List of event loops to manage running and not running for.
111 // The function is a callback used to set and clear the running bool on each
112 // event loop.
113 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
114 raw_event_loops_;
115
116 std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50);
117 std::chrono::nanoseconds network_delay_ = std::chrono::microseconds(100);
118
119 std::vector<std::unique_ptr<NodeEventLoopFactory>> node_factories_;
120
121 std::vector<const Node *> nodes_;
Austin Schuh898f4972020-01-11 17:21:25 -0800122
123 std::unique_ptr<message_bridge::SimulatedMessageBridge> bridge_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800124};
125
126// This class holds all the state required to be a single node.
127class NodeEventLoopFactory {
128 public:
129 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name);
Austin Schuh7d87b672019-12-01 20:23:49 -0800130
Austin Schuh217a9782019-12-21 23:02:50 -0800131 // Returns the node that this factory is running as, or nullptr if this is a
132 // single node setup.
133 const Node *node() const { return node_; }
134
Austin Schuh92547522019-12-28 14:33:43 -0800135 // Sets realtime clock to realtime_now for a given monotonic clock.
136 void SetRealtimeOffset(monotonic_clock::time_point monotonic_now,
137 realtime_clock::time_point realtime_now) {
Austin Schuhac0771c2020-01-07 18:36:30 -0800138 realtime_offset_ =
139 realtime_now.time_since_epoch() - monotonic_now.time_since_epoch();
Austin Schuh92547522019-12-28 14:33:43 -0800140 }
141
Austin Schuhac0771c2020-01-07 18:36:30 -0800142 // Returns the current time on both clocks.
143 inline monotonic_clock::time_point monotonic_now() const;
144 inline realtime_clock::time_point realtime_now() const;
Austin Schuh39788ff2019-12-01 18:22:57 -0800145
Austin Schuh898f4972020-01-11 17:21:25 -0800146 // Returns the simulated network delay for messages forwarded between nodes.
147 std::chrono::nanoseconds network_delay() const {
148 return factory_->network_delay();
149 }
150 // Returns the simulated send delay for all messages sent within a single
151 // node.
152 std::chrono::nanoseconds send_delay() const { return factory_->send_delay(); }
153
Austin Schuh8bd96322020-02-13 21:18:22 -0800154 // TODO(austin): Private for the following?
155
Austin Schuhac0771c2020-01-07 18:36:30 -0800156 // Converts a time to the distributed clock for scheduling and cross-node time
157 // measurement.
158 inline distributed_clock::time_point ToDistributedClock(
159 monotonic_clock::time_point time) const;
160
Austin Schuh8bd96322020-02-13 21:18:22 -0800161 // Sets the offset between the monotonic clock and the central distributed
162 // clock. distributed_clock = monotonic_clock + offset.
163 void SetDistributedOffset(std::chrono::nanoseconds monotonic_offset) {
164 scheduler_.SetDistributedOffset(monotonic_offset);
Austin Schuhcde938c2020-02-02 17:30:07 -0800165 }
166
Austin Schuhac0771c2020-01-07 18:36:30 -0800167 private:
168 friend class SimulatedEventLoopFactory;
169 NodeEventLoopFactory(
Austin Schuh8bd96322020-02-13 21:18:22 -0800170 EventSchedulerScheduler *scheduler_scheduler,
171 SimulatedEventLoopFactory *factory, const Node *node,
Austin Schuhac0771c2020-01-07 18:36:30 -0800172 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
173 *raw_event_loops);
174
Austin Schuh8bd96322020-02-13 21:18:22 -0800175 EventScheduler scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800176 SimulatedEventLoopFactory *const factory_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800177
Austin Schuh217a9782019-12-21 23:02:50 -0800178 const Node *const node_;
179
Austin Schuhac0771c2020-01-07 18:36:30 -0800180 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
181 *const raw_event_loops_;
182
Austin Schuhac0771c2020-01-07 18:36:30 -0800183 std::chrono::nanoseconds realtime_offset_ = std::chrono::seconds(0);
184
185 // Map from name, type to queue.
186 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_;
187
188 // pid so we get unique timing reports.
Austin Schuh39788ff2019-12-01 18:22:57 -0800189 pid_t tid_ = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190};
191
Austin Schuhac0771c2020-01-07 18:36:30 -0800192inline monotonic_clock::time_point NodeEventLoopFactory::monotonic_now() const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800193 // TODO(austin): Confirm that time never goes backwards?
194 return scheduler_.FromDistributedClock(factory_->distributed_now());
Austin Schuhac0771c2020-01-07 18:36:30 -0800195}
196
197inline realtime_clock::time_point NodeEventLoopFactory::realtime_now() const {
198 return realtime_clock::time_point(monotonic_now().time_since_epoch() +
199 realtime_offset_);
200}
201
202inline distributed_clock::time_point NodeEventLoopFactory::ToDistributedClock(
203 monotonic_clock::time_point time) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800204 return scheduler_.ToDistributedClock(time);
Austin Schuhac0771c2020-01-07 18:36:30 -0800205}
206
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207} // namespace aos
208
209#endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_