blob: 348e7dd4b0b56df4edb3c5017b95e0cd71df58ec [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>
Brian Silverman601b9722020-06-18 14:33:43 -07005#include <functional>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include <map>
7#include <memory>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08008#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#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 Schuhe1dafe42020-01-06 21:12:03 -080016#include "aos/events/simple_channel.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "aos/flatbuffer_merge.h"
18#include "aos/flatbuffers.h"
19#include "aos/ipc_lib/index.h"
Austin Schuh4385b142021-03-14 21:31:13 -070020#include "aos/uuid.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "glog/logging.h"
22
23namespace aos {
24
25// Class for simulated fetchers.
26class SimulatedChannel;
27
Austin Schuhac0771c2020-01-07 18:36:30 -080028class NodeEventLoopFactory;
Austin Schuh898f4972020-01-11 17:21:25 -080029namespace message_bridge {
30class SimulatedMessageBridge;
31}
Austin Schuhac0771c2020-01-07 18:36:30 -080032
33// There are 2 concepts needed to support multi-node simulations.
34// 1) The node. This is implemented with NodeEventLoopFactory.
35// 2) The "robot" which runs multiple nodes. This is implemented with
36// SimulatedEventLoopFactory.
37//
38// To make things easier, SimulatedEventLoopFactory takes an optional Node
39// argument if you want to make event loops without interacting with the
40// NodeEventLoopFactory object.
41//
42// The basic flow goes something like as follows:
43//
44// SimulatedEventLoopFactory factory(config);
45// const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1");
46// std::unique_ptr<EventLoop> event_loop = factory.MakeEventLoop("ping", pi1);
47//
48// Or
49//
50// SimulatedEventLoopFactory factory(config);
51// const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1");
52// NodeEventLoopFactory *pi1_factory = factory.GetNodeEventLoopFactory(pi1);
53// std::unique_ptr<EventLoop> event_loop = pi1_factory.MakeEventLoop("ping");
54//
55// The distributed_clock is used to be the base time. NodeEventLoopFactory has
56// all the information needed to adjust both the realtime and monotonic clocks
57// relative to the distributed_clock.
Alex Perrycb7da4b2019-08-28 19:35:56 -070058class SimulatedEventLoopFactory {
59 public:
60 // Constructs a SimulatedEventLoopFactory with the provided configuration.
61 // This configuration must remain in scope for the lifetime of the factory and
62 // all sub-objects.
63 SimulatedEventLoopFactory(const Configuration *configuration);
64 ~SimulatedEventLoopFactory();
65
Austin Schuhac0771c2020-01-07 18:36:30 -080066 // Creates an event loop. If running in a multi-node environment, node needs
67 // to point to the node to create this event loop on.
68 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name,
69 const Node *node = nullptr);
70
71 // Returns the NodeEventLoopFactory for the provided node. The returned
72 // NodeEventLoopFactory is owned by the SimulatedEventLoopFactory and has a
73 // lifetime identical to the factory.
74 NodeEventLoopFactory *GetNodeEventLoopFactory(const Node *node);
Alex Perrycb7da4b2019-08-28 19:35:56 -070075
Austin Schuh87dd3832021-01-01 23:07:31 -080076 // Sets the time converter for all nodes.
77 void SetTimeConverter(TimeConverter *time_converter);
78
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 // Starts executing the event loops unconditionally.
80 void Run();
81 // Executes the event loops for a duration.
Austin Schuhac0771c2020-01-07 18:36:30 -080082 void RunFor(distributed_clock::duration duration);
Alex Perrycb7da4b2019-08-28 19:35:56 -070083
84 // Stops executing all event loops. Meant to be called from within an event
85 // loop handler.
Austin Schuh8fb315a2020-11-19 22:33:58 -080086 void Exit();
Alex Perrycb7da4b2019-08-28 19:35:56 -070087
Austin Schuhac0771c2020-01-07 18:36:30 -080088 const std::vector<const Node *> &nodes() const { return nodes_; }
89
90 // Sets the simulated send delay for all messages sent within a single node.
Austin Schuh7d87b672019-12-01 20:23:49 -080091 void set_send_delay(std::chrono::nanoseconds send_delay);
Austin Schuhac0771c2020-01-07 18:36:30 -080092 std::chrono::nanoseconds send_delay() const { return send_delay_; }
93
94 // Sets the simulated network delay for messages forwarded between nodes.
Brian Silvermana7c62052020-04-28 16:52:27 -070095 void set_network_delay(std::chrono::nanoseconds network_delay) {
96 network_delay_ = network_delay;
97 }
Austin Schuhac0771c2020-01-07 18:36:30 -080098 std::chrono::nanoseconds network_delay() const { return network_delay_; }
99
100 // Returns the clock used to synchronize the nodes.
101 distributed_clock::time_point distributed_now() const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800102 return scheduler_scheduler_.distributed_now();
Austin Schuhac0771c2020-01-07 18:36:30 -0800103 }
104
105 // Returns the configuration used for everything.
106 const Configuration *configuration() const { return configuration_; }
107
Austin Schuh6f3babe2020-01-26 20:34:50 -0800108 // Disables forwarding for this channel. This should be used very rarely only
109 // for things like the logger.
110 void DisableForwarding(const Channel *channel);
111
Austin Schuh4c3b9702020-08-30 11:34:55 -0700112 // Disables the messages sent by the simulated message gateway.
113 void DisableStatistics();
114
Austin Schuh2928ebe2021-02-07 22:10:27 -0800115 // Calls SkipTimingReport() on all EventLoops used as part of the
116 // infrastructure. This may improve the performance of long-simulated-duration
117 // tests.
118 void SkipTimingReport();
119
Austin Schuhac0771c2020-01-07 18:36:30 -0800120 private:
Austin Schuhc0b0f722020-12-12 18:36:06 -0800121 friend class NodeEventLoopFactory;
122
Austin Schuhac0771c2020-01-07 18:36:30 -0800123 const Configuration *const configuration_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800124 EventSchedulerScheduler scheduler_scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800125 // List of event loops to manage running and not running for.
126 // The function is a callback used to set and clear the running bool on each
127 // event loop.
128 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
129 raw_event_loops_;
130
131 std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50);
132 std::chrono::nanoseconds network_delay_ = std::chrono::microseconds(100);
133
134 std::vector<std::unique_ptr<NodeEventLoopFactory>> node_factories_;
135
136 std::vector<const Node *> nodes_;
Austin Schuh898f4972020-01-11 17:21:25 -0800137
138 std::unique_ptr<message_bridge::SimulatedMessageBridge> bridge_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800139};
140
141// This class holds all the state required to be a single node.
142class NodeEventLoopFactory {
143 public:
144 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name);
Austin Schuh7d87b672019-12-01 20:23:49 -0800145
Austin Schuh217a9782019-12-21 23:02:50 -0800146 // Returns the node that this factory is running as, or nullptr if this is a
147 // single node setup.
148 const Node *node() const { return node_; }
149
Austin Schuh92547522019-12-28 14:33:43 -0800150 // Sets realtime clock to realtime_now for a given monotonic clock.
151 void SetRealtimeOffset(monotonic_clock::time_point monotonic_now,
152 realtime_clock::time_point realtime_now) {
Austin Schuhac0771c2020-01-07 18:36:30 -0800153 realtime_offset_ =
154 realtime_now.time_since_epoch() - monotonic_now.time_since_epoch();
Austin Schuh92547522019-12-28 14:33:43 -0800155 }
156
Austin Schuhac0771c2020-01-07 18:36:30 -0800157 // Returns the current time on both clocks.
158 inline monotonic_clock::time_point monotonic_now() const;
159 inline realtime_clock::time_point realtime_now() const;
Austin Schuh39788ff2019-12-01 18:22:57 -0800160
Austin Schuhfaec5e12020-11-05 17:39:55 -0800161 const Configuration *configuration() const {
162 return factory_->configuration();
163 }
164
Austin Schuh898f4972020-01-11 17:21:25 -0800165 // Returns the simulated network delay for messages forwarded between nodes.
166 std::chrono::nanoseconds network_delay() const {
167 return factory_->network_delay();
168 }
169 // Returns the simulated send delay for all messages sent within a single
170 // node.
171 std::chrono::nanoseconds send_delay() const { return factory_->send_delay(); }
172
Austin Schuh8bd96322020-02-13 21:18:22 -0800173 // TODO(austin): Private for the following?
174
Austin Schuhac0771c2020-01-07 18:36:30 -0800175 // Converts a time to the distributed clock for scheduling and cross-node time
176 // measurement.
Austin Schuh87dd3832021-01-01 23:07:31 -0800177 // Note: converting time too far in the future can cause problems when
178 // replaying logs. Only convert times in the present or near past.
Austin Schuhac0771c2020-01-07 18:36:30 -0800179 inline distributed_clock::time_point ToDistributedClock(
180 monotonic_clock::time_point time) const;
Austin Schuhbe69cf32020-08-27 11:38:33 -0700181 inline monotonic_clock::time_point FromDistributedClock(
182 distributed_clock::time_point time) const;
Austin Schuhac0771c2020-01-07 18:36:30 -0800183
Austin Schuh87dd3832021-01-01 23:07:31 -0800184 // Sets the class used to convert time. This pointer must out-live the
185 // SimulatedEventLoopFactory.
186 void SetTimeConverter(TimeConverter *time_converter) {
187 scheduler_.SetTimeConverter(
188 configuration::GetNodeIndex(factory_->configuration(), node_),
189 time_converter);
Austin Schuhcde938c2020-02-02 17:30:07 -0800190 }
191
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800192 // Sets the boot UUID for this node. This typically should only be used by
193 // the log reader.
194 void set_boot_uuid(std::string_view uuid) {
195 boot_uuid_ = UUID::FromString(uuid);
196 }
Austin Schuh20ac95d2020-12-05 17:24:19 -0800197 // Returns the boot UUID for this node.
198 const UUID &boot_uuid() const { return boot_uuid_; }
199
200 // Reboots the node. This just resets the boot_uuid_, nothing else.
201 // TODO(austin): This is here for a test case or two, not for general
202 // consumption. The interactions with the rest of the system need to be
203 // worked out better. Don't use this for anything real yet.
204 void Reboot() { boot_uuid_ = UUID::Random(); }
205
Austin Schuhc0b0f722020-12-12 18:36:06 -0800206 // Stops forwarding messages to the other node, and reports disconnected in
207 // the ServerStatistics message for this node, and the ClientStatistics for
208 // the other node.
209 void Disconnect(const Node *other);
210 // Resumes forwarding messages.
211 void Connect(const Node *other);
212
Austin Schuhac0771c2020-01-07 18:36:30 -0800213 private:
214 friend class SimulatedEventLoopFactory;
215 NodeEventLoopFactory(
Austin Schuh8bd96322020-02-13 21:18:22 -0800216 EventSchedulerScheduler *scheduler_scheduler,
217 SimulatedEventLoopFactory *factory, const Node *node,
Austin Schuhac0771c2020-01-07 18:36:30 -0800218 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
219 *raw_event_loops);
220
Austin Schuh8bd96322020-02-13 21:18:22 -0800221 EventScheduler scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800222 SimulatedEventLoopFactory *const factory_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800223
Austin Schuh20ac95d2020-12-05 17:24:19 -0800224 UUID boot_uuid_ = UUID::Random();
225
Austin Schuh217a9782019-12-21 23:02:50 -0800226 const Node *const node_;
227
Austin Schuhac0771c2020-01-07 18:36:30 -0800228 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
229 *const raw_event_loops_;
230
Austin Schuhac0771c2020-01-07 18:36:30 -0800231 std::chrono::nanoseconds realtime_offset_ = std::chrono::seconds(0);
232
233 // Map from name, type to queue.
234 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_;
235
236 // pid so we get unique timing reports.
Austin Schuh39788ff2019-12-01 18:22:57 -0800237 pid_t tid_ = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700238};
239
Austin Schuhac0771c2020-01-07 18:36:30 -0800240inline monotonic_clock::time_point NodeEventLoopFactory::monotonic_now() const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800241 // TODO(austin): Confirm that time never goes backwards?
Austin Schuhbe69cf32020-08-27 11:38:33 -0700242 return scheduler_.monotonic_now();
Austin Schuhac0771c2020-01-07 18:36:30 -0800243}
244
245inline realtime_clock::time_point NodeEventLoopFactory::realtime_now() const {
246 return realtime_clock::time_point(monotonic_now().time_since_epoch() +
247 realtime_offset_);
248}
249
Austin Schuhbe69cf32020-08-27 11:38:33 -0700250inline monotonic_clock::time_point NodeEventLoopFactory::FromDistributedClock(
251 distributed_clock::time_point time) const {
252 return scheduler_.FromDistributedClock(time);
253}
254
Austin Schuhac0771c2020-01-07 18:36:30 -0800255inline distributed_clock::time_point NodeEventLoopFactory::ToDistributedClock(
256 monotonic_clock::time_point time) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800257 return scheduler_.ToDistributedClock(time);
Austin Schuhac0771c2020-01-07 18:36:30 -0800258}
259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260} // namespace aos
261
262#endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_