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" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 20 | #include "aos/uuid.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "glog/logging.h" |
| 22 | |
| 23 | namespace aos { |
| 24 | |
| 25 | // Class for simulated fetchers. |
| 26 | class SimulatedChannel; |
| 27 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 28 | class NodeEventLoopFactory; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 29 | class SimulatedEventLoop; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 30 | namespace message_bridge { |
| 31 | class SimulatedMessageBridge; |
| 32 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 33 | |
| 34 | // There are 2 concepts needed to support multi-node simulations. |
| 35 | // 1) The node. This is implemented with NodeEventLoopFactory. |
| 36 | // 2) The "robot" which runs multiple nodes. This is implemented with |
| 37 | // SimulatedEventLoopFactory. |
| 38 | // |
| 39 | // To make things easier, SimulatedEventLoopFactory takes an optional Node |
| 40 | // argument if you want to make event loops without interacting with the |
| 41 | // NodeEventLoopFactory object. |
| 42 | // |
| 43 | // The basic flow goes something like as follows: |
| 44 | // |
| 45 | // SimulatedEventLoopFactory factory(config); |
| 46 | // const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1"); |
| 47 | // std::unique_ptr<EventLoop> event_loop = factory.MakeEventLoop("ping", pi1); |
| 48 | // |
| 49 | // Or |
| 50 | // |
| 51 | // SimulatedEventLoopFactory factory(config); |
| 52 | // const Node *pi1 = configuration::GetNode(factory.configuration(), "pi1"); |
| 53 | // NodeEventLoopFactory *pi1_factory = factory.GetNodeEventLoopFactory(pi1); |
| 54 | // std::unique_ptr<EventLoop> event_loop = pi1_factory.MakeEventLoop("ping"); |
| 55 | // |
| 56 | // The distributed_clock is used to be the base time. NodeEventLoopFactory has |
| 57 | // all the information needed to adjust both the realtime and monotonic clocks |
| 58 | // relative to the distributed_clock. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | class SimulatedEventLoopFactory { |
| 60 | public: |
| 61 | // Constructs a SimulatedEventLoopFactory with the provided configuration. |
| 62 | // This configuration must remain in scope for the lifetime of the factory and |
| 63 | // all sub-objects. |
| 64 | SimulatedEventLoopFactory(const Configuration *configuration); |
| 65 | ~SimulatedEventLoopFactory(); |
| 66 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 67 | SimulatedEventLoopFactory(const SimulatedEventLoopFactory &) = delete; |
| 68 | SimulatedEventLoopFactory &operator=(const SimulatedEventLoopFactory &) = |
| 69 | delete; |
| 70 | SimulatedEventLoopFactory(SimulatedEventLoopFactory &&) = delete; |
| 71 | SimulatedEventLoopFactory &operator=(SimulatedEventLoopFactory &&) = delete; |
| 72 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 73 | // Creates an event loop. If running in a multi-node environment, node needs |
| 74 | // to point to the node to create this event loop on. |
| 75 | ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name, |
| 76 | const Node *node = nullptr); |
| 77 | |
| 78 | // Returns the NodeEventLoopFactory for the provided node. The returned |
| 79 | // NodeEventLoopFactory is owned by the SimulatedEventLoopFactory and has a |
| 80 | // lifetime identical to the factory. |
| 81 | NodeEventLoopFactory *GetNodeEventLoopFactory(const Node *node); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 82 | NodeEventLoopFactory *GetNodeEventLoopFactory(std::string_view node); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 84 | // Sets the time converter for all nodes. |
| 85 | void SetTimeConverter(TimeConverter *time_converter); |
| 86 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 87 | // Starts executing the event loops unconditionally until Exit is called or |
| 88 | // all the nodes have shut down. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | void Run(); |
| 90 | // Executes the event loops for a duration. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 91 | void RunFor(distributed_clock::duration duration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | |
| 93 | // Stops executing all event loops. Meant to be called from within an event |
| 94 | // loop handler. |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 95 | void Exit(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 97 | const std::vector<const Node *> &nodes() const { return nodes_; } |
| 98 | |
| 99 | // 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] | 100 | void set_send_delay(std::chrono::nanoseconds send_delay); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 101 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 102 | |
| 103 | // Sets the simulated network delay for messages forwarded between nodes. |
Brian Silverman | a7c6205 | 2020-04-28 16:52:27 -0700 | [diff] [blame] | 104 | void set_network_delay(std::chrono::nanoseconds network_delay) { |
| 105 | network_delay_ = network_delay; |
| 106 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 107 | std::chrono::nanoseconds network_delay() const { return network_delay_; } |
| 108 | |
| 109 | // Returns the clock used to synchronize the nodes. |
| 110 | distributed_clock::time_point distributed_now() const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 111 | return scheduler_scheduler_.distributed_now(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Returns the configuration used for everything. |
| 115 | const Configuration *configuration() const { return configuration_; } |
| 116 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 117 | // Disables forwarding for this channel. This should be used very rarely only |
| 118 | // for things like the logger. |
| 119 | void DisableForwarding(const Channel *channel); |
| 120 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 121 | // Disables the messages sent by the simulated message gateway. |
| 122 | void DisableStatistics(); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 123 | // Enables the messages sent by the simulated message gateway. |
| 124 | void EnableStatistics(); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 125 | |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 126 | // Calls SkipTimingReport() on all EventLoops used as part of the |
| 127 | // infrastructure. This may improve the performance of long-simulated-duration |
| 128 | // tests. |
| 129 | void SkipTimingReport(); |
| 130 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 131 | // Re-enables application creation for the duration of fn. This is mostly to |
| 132 | // allow use cases like log reading to create applications after the node |
| 133 | // starts up without stopping execution. |
| 134 | void AllowApplicationCreationDuring(std::function<void()> fn); |
| 135 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 136 | private: |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 137 | friend class NodeEventLoopFactory; |
| 138 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 139 | const Configuration *const configuration_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 140 | EventSchedulerScheduler scheduler_scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 141 | |
| 142 | std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50); |
| 143 | std::chrono::nanoseconds network_delay_ = std::chrono::microseconds(100); |
| 144 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 145 | std::unique_ptr<message_bridge::SimulatedMessageBridge> bridge_; |
| 146 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 147 | std::vector<std::unique_ptr<NodeEventLoopFactory>> node_factories_; |
| 148 | |
| 149 | std::vector<const Node *> nodes_; |
| 150 | }; |
| 151 | |
| 152 | // This class holds all the state required to be a single node. |
| 153 | class NodeEventLoopFactory { |
| 154 | public: |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 155 | ~NodeEventLoopFactory(); |
| 156 | |
| 157 | std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 158 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 159 | // Returns the node that this factory is running as, or nullptr if this is a |
| 160 | // single node setup. |
| 161 | const Node *node() const { return node_; } |
| 162 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 163 | // Sets realtime clock to realtime_now for a given monotonic clock. |
| 164 | void SetRealtimeOffset(monotonic_clock::time_point monotonic_now, |
| 165 | realtime_clock::time_point realtime_now) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 166 | realtime_offset_ = |
| 167 | realtime_now.time_since_epoch() - monotonic_now.time_since_epoch(); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 170 | // Returns the current time on both clocks. |
| 171 | inline monotonic_clock::time_point monotonic_now() const; |
| 172 | inline realtime_clock::time_point realtime_now() const; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 173 | inline distributed_clock::time_point distributed_now() const; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 174 | |
Austin Schuh | faec5e1 | 2020-11-05 17:39:55 -0800 | [diff] [blame] | 175 | const Configuration *configuration() const { |
| 176 | return factory_->configuration(); |
| 177 | } |
| 178 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 179 | // Starts the node up by calling the OnStartup handlers. These get called |
| 180 | // every time a node is started. |
| 181 | |
| 182 | // Called when a node has started. This is typically when a log file starts |
| 183 | // for a node. |
| 184 | void OnStartup(std::function<void()> &&fn); |
| 185 | |
| 186 | // Called when a node shuts down. These get called every time a node is shut |
| 187 | // down. All applications are destroyed right after the last OnShutdown |
| 188 | // callback is called. |
| 189 | void OnShutdown(std::function<void()> &&fn); |
| 190 | |
| 191 | // Starts an application if the configuration says it should be started on |
| 192 | // this node. name is the name of the application. args are the constructor |
| 193 | // args for the Main class. Returns a pointer to the class that was started |
| 194 | // if it was started, or nullptr. |
| 195 | template <class Main, class... Args> |
| 196 | Main *MaybeStart(std::string_view name, Args &&... args); |
| 197 | |
| 198 | // Starts an application regardless of if the config says to or not. name is |
| 199 | // the name of the application, and args are the constructor args for the |
| 200 | // application. Returns a pointer to the class that was started. |
| 201 | template <class Main, class... Args> |
| 202 | Main *AlwaysStart(std::string_view name, Args &&... args); |
| 203 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 204 | // Returns the simulated network delay for messages forwarded between nodes. |
| 205 | std::chrono::nanoseconds network_delay() const { |
| 206 | return factory_->network_delay(); |
| 207 | } |
| 208 | // Returns the simulated send delay for all messages sent within a single |
| 209 | // node. |
| 210 | std::chrono::nanoseconds send_delay() const { return factory_->send_delay(); } |
| 211 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 212 | size_t boot_count() const { return scheduler_.boot_count(); } |
| 213 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 214 | // TODO(austin): Private for the following? |
| 215 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 216 | // Converts a time to the distributed clock for scheduling and cross-node time |
| 217 | // measurement. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 218 | // Note: converting time too far in the future can cause problems when |
| 219 | // replaying logs. Only convert times in the present or near past. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 220 | inline distributed_clock::time_point ToDistributedClock( |
| 221 | monotonic_clock::time_point time) const; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 222 | inline logger::BootTimestamp FromDistributedClock( |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 223 | distributed_clock::time_point time) const; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 224 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 225 | // Sets the class used to convert time. This pointer must out-live the |
| 226 | // SimulatedEventLoopFactory. |
| 227 | void SetTimeConverter(TimeConverter *time_converter) { |
| 228 | scheduler_.SetTimeConverter( |
| 229 | configuration::GetNodeIndex(factory_->configuration(), node_), |
| 230 | time_converter); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 233 | // Returns the boot UUID for this node. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 234 | const UUID &boot_uuid() { |
| 235 | if (boot_uuid_ == UUID::Zero()) { |
| 236 | boot_uuid_ = scheduler_.boot_uuid(); |
| 237 | } |
| 238 | return boot_uuid_; |
| 239 | } |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 240 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 241 | // Stops forwarding messages to the other node, and reports disconnected in |
| 242 | // the ServerStatistics message for this node, and the ClientStatistics for |
| 243 | // the other node. |
| 244 | void Disconnect(const Node *other); |
| 245 | // Resumes forwarding messages. |
| 246 | void Connect(const Node *other); |
| 247 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 248 | // Disables the messages sent by the simulated message gateway. |
| 249 | void DisableStatistics(); |
| 250 | // Enables the messages sent by the simulated message gateway. |
| 251 | void EnableStatistics(); |
| 252 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 253 | private: |
| 254 | friend class SimulatedEventLoopFactory; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 255 | NodeEventLoopFactory(EventSchedulerScheduler *scheduler_scheduler, |
| 256 | SimulatedEventLoopFactory *factory, const Node *node); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 257 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 258 | // Skips timing reports on all event loops on this node. |
| 259 | void SkipTimingReport(); |
| 260 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 261 | // Helpers to restart. |
| 262 | void ScheduleStartup(); |
| 263 | void Startup(); |
| 264 | void Shutdown(); |
| 265 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 266 | EventScheduler scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 267 | SimulatedEventLoopFactory *const factory_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 268 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 269 | UUID boot_uuid_ = UUID::Zero(); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 270 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 271 | const Node *const node_; |
| 272 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 273 | bool skip_timing_report_ = false; |
| 274 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 275 | std::vector<SimulatedEventLoop *> event_loops_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 276 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 277 | std::chrono::nanoseconds realtime_offset_ = std::chrono::seconds(0); |
| 278 | |
| 279 | // Map from name, type to queue. |
| 280 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_; |
| 281 | |
| 282 | // pid so we get unique timing reports. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 283 | pid_t tid_ = 0; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 284 | |
| 285 | // True if we are started. |
| 286 | bool started_ = false; |
| 287 | |
| 288 | std::vector<std::function<void()>> pending_on_startup_; |
| 289 | std::vector<std::function<void()>> on_startup_; |
| 290 | std::vector<std::function<void()>> on_shutdown_; |
| 291 | |
| 292 | // Base class for an application to start. This shouldn't be used directly. |
| 293 | struct Application { |
| 294 | Application(NodeEventLoopFactory *node_factory, std::string_view name) |
| 295 | : event_loop(node_factory->MakeEventLoop(name)) {} |
| 296 | virtual ~Application() {} |
| 297 | |
| 298 | std::unique_ptr<EventLoop> event_loop; |
| 299 | }; |
| 300 | |
| 301 | // Subclass to do type erasure for the base class. Holds an instance of a |
| 302 | // specific class. Use SimulationStarter instead. |
| 303 | template <typename Main> |
| 304 | struct TypedApplication : public Application { |
| 305 | // Constructs an Application by delegating the arguments used to construct |
| 306 | // the event loop to Application and the rest of the args to the actual |
| 307 | // application. |
| 308 | template <class... Args> |
| 309 | TypedApplication(NodeEventLoopFactory *node_factory, std::string_view name, |
| 310 | Args &&... args) |
| 311 | : Application(node_factory, name), |
| 312 | main(event_loop.get(), std::forward<Args>(args)...) { |
| 313 | VLOG(1) << node_factory->scheduler_.distributed_now() << " " |
| 314 | << (node_factory->node() == nullptr |
| 315 | ? "" |
| 316 | : node_factory->node()->name()->str() + " ") |
| 317 | << node_factory->monotonic_now() << " Starting Application \"" |
| 318 | << name << "\""; |
| 319 | } |
| 320 | ~TypedApplication() override {} |
| 321 | |
| 322 | Main main; |
| 323 | }; |
| 324 | |
| 325 | std::vector<std::unique_ptr<Application>> applications_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 328 | template <class Main, class... Args> |
| 329 | Main *NodeEventLoopFactory::MaybeStart(std::string_view name, Args &&... args) { |
| 330 | const aos::Application *application = |
| 331 | configuration::GetApplication(configuration(), node(), name); |
| 332 | |
| 333 | if (application != nullptr) { |
| 334 | return AlwaysStart<Main>(name, std::forward<Args>(args)...); |
| 335 | } |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
| 339 | template <class Main, class... Args> |
| 340 | Main *NodeEventLoopFactory::AlwaysStart(std::string_view name, |
| 341 | Args &&... args) { |
| 342 | std::unique_ptr<TypedApplication<Main>> app = |
| 343 | std::make_unique<TypedApplication<Main>>(this, name, |
| 344 | std::forward<Args>(args)...); |
| 345 | Main *main_ptr = &app->main; |
| 346 | applications_.emplace_back(std::move(app)); |
| 347 | return main_ptr; |
| 348 | } |
| 349 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 350 | inline monotonic_clock::time_point NodeEventLoopFactory::monotonic_now() const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 351 | // TODO(austin): Confirm that time never goes backwards? |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 352 | return scheduler_.monotonic_now(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | inline realtime_clock::time_point NodeEventLoopFactory::realtime_now() const { |
| 356 | return realtime_clock::time_point(monotonic_now().time_since_epoch() + |
| 357 | realtime_offset_); |
| 358 | } |
| 359 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 360 | inline distributed_clock::time_point NodeEventLoopFactory::distributed_now() |
| 361 | const { |
| 362 | return scheduler_.distributed_now(); |
| 363 | } |
| 364 | |
| 365 | inline logger::BootTimestamp NodeEventLoopFactory::FromDistributedClock( |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 366 | distributed_clock::time_point time) const { |
| 367 | return scheduler_.FromDistributedClock(time); |
| 368 | } |
| 369 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 370 | inline distributed_clock::time_point NodeEventLoopFactory::ToDistributedClock( |
| 371 | monotonic_clock::time_point time) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 372 | return scheduler_.ToDistributedClock(time); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 375 | } // namespace aos |
| 376 | |
| 377 | #endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |