Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOG_READER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOG_READER_H_ |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 3 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 4 | #include <chrono> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include <deque> |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 6 | #include <queue> |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 7 | #include <string_view> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 8 | #include <tuple> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 9 | #include <vector> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 10 | |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 11 | #include "aos/condition.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 12 | #include "aos/events/event_loop.h" |
Austin Schuh | f6f9bf3 | 2020-10-11 14:37:43 -0700 | [diff] [blame] | 13 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 14 | #include "aos/events/logging/logfile_utils.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 15 | #include "aos/events/logging/logger_generated.h" |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 16 | #include "aos/events/logging/replay_timing_generated.h" |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 17 | #include "aos/events/shm_event_loop.h" |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 18 | #include "aos/events/simulated_event_loop.h" |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 19 | #include "aos/mutex/mutex.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 20 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 21 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 22 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 23 | #include "aos/network/timestamp_filter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 24 | #include "aos/time/time.h" |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 25 | #include "aos/util/threaded_queue.h" |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 26 | #include "aos/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | #include "flatbuffers/flatbuffers.h" |
| 28 | |
| 29 | namespace aos { |
| 30 | namespace logger { |
| 31 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 32 | class EventNotifier; |
| 33 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 34 | // We end up with one of the following 3 log file types. |
| 35 | // |
| 36 | // Single node logged as the source node. |
| 37 | // -> Replayed just on the source node. |
| 38 | // |
| 39 | // Forwarding timestamps only logged from the perspective of the destination |
| 40 | // node. |
| 41 | // -> Matched with data on source node and logged. |
| 42 | // |
| 43 | // Forwarding timestamps with data logged as the destination node. |
| 44 | // -> Replayed just as the destination |
| 45 | // -> Replayed as the source (Much harder, ordering is not defined) |
| 46 | // |
| 47 | // Duplicate data logged. -> CHECK that it matches and explode otherwise. |
| 48 | // |
| 49 | // This can be boiled down to a set of constraints and tools. |
| 50 | // |
| 51 | // 1) Forwarding timestamps and data need to be logged separately. |
| 52 | // 2) Any forwarded data logged on the destination node needs to be logged |
| 53 | // separately such that it can be sorted. |
| 54 | // |
| 55 | // 1) Log reader needs to be able to sort a list of log files. |
| 56 | // 2) Log reader needs to be able to merge sorted lists of log files. |
| 57 | // 3) Log reader needs to be able to match timestamps with messages. |
| 58 | // |
| 59 | // We also need to be able to generate multiple views of a log file depending on |
| 60 | // the target. |
| 61 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 62 | // Replays all the channels in the logfile to the event loop. |
| 63 | class LogReader { |
| 64 | public: |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 65 | // If you want to supply a new configuration that will be used for replay |
| 66 | // (e.g., to change message rates, or to populate an updated schema), then |
| 67 | // pass it in here. It must provide all the channels that the original logged |
| 68 | // config did. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 69 | // |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 70 | // The single file constructor calls SortParts internally. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 71 | LogReader(std::string_view filename, |
| 72 | const Configuration *replay_configuration = nullptr); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 73 | LogReader(std::vector<LogFile> log_files, |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 74 | const Configuration *replay_configuration = nullptr); |
James Kuszmaul | 7daef36 | 2019-12-31 18:28:17 -0800 | [diff] [blame] | 75 | ~LogReader(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 76 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 77 | // Registers all the callbacks to send the log file data out on an event loop |
| 78 | // created in event_loop_factory. This also updates time to be at the start |
| 79 | // of the log file by running until the log file starts. |
| 80 | // Note: the configuration used in the factory should be configuration() |
| 81 | // below, but can be anything as long as the locations needed to send |
| 82 | // everything are available. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 83 | void Register(SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 85 | // Registers all the callbacks to send the log file data out to an event loop |
| 86 | // factory. This does not start replaying or change the current distributed |
| 87 | // time of the factory. It does change the monotonic clocks to be right. |
| 88 | void RegisterWithoutStarting(SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 89 | // Runs the log until the last start time. Register above is defined as: |
| 90 | // Register(...) { |
| 91 | // RegisterWithoutStarting |
| 92 | // StartAfterRegister |
| 93 | // } |
| 94 | // This should generally be considered as a stepping stone to convert from |
| 95 | // Register() to RegisterWithoutStarting() incrementally. |
| 96 | void StartAfterRegister(SimulatedEventLoopFactory *event_loop_factory); |
| 97 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 98 | // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(), |
| 99 | // and then calls Register. |
| 100 | void Register(); |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 101 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 102 | // Registers callbacks for all the events after the log file starts. This is |
| 103 | // only useful when replaying live. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 104 | void Register(EventLoop *event_loop); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 105 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 106 | // Sets a sender that should be used for tracking timing statistics. If not |
| 107 | // set, no statistics will be recorded. |
| 108 | void set_timing_accuracy_sender( |
| 109 | const Node *node, aos::Sender<timing::ReplayTiming> timing_sender) { |
| 110 | states_[configuration::GetNodeIndex(configuration(), node)] |
| 111 | ->set_timing_accuracy_sender(std::move(timing_sender)); |
| 112 | } |
| 113 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 114 | // Called whenever a log file starts for a node. |
| 115 | void OnStart(std::function<void()> fn); |
| 116 | void OnStart(const Node *node, std::function<void()> fn); |
| 117 | // Called whenever a log file ends for a node. |
| 118 | void OnEnd(std::function<void()> fn); |
| 119 | void OnEnd(const Node *node, std::function<void()> fn); |
| 120 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 121 | // Unregisters the senders. You only need to call this if you separately |
| 122 | // supplied an event loop or event loop factory and the lifetimes are such |
| 123 | // that they need to be explicitly destroyed before the LogReader destructor |
| 124 | // gets called. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 125 | void Deregister(); |
| 126 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 127 | // Returns the configuration being used for replay from the log file. |
| 128 | // Note that this may be different from the configuration actually used for |
| 129 | // handling events. You should generally only use this to create a |
| 130 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 131 | // everything else. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 132 | const Configuration *logged_configuration() const; |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 133 | // Returns the configuration being used for replay from the log file. |
| 134 | // Note that this may be different from the configuration actually used for |
| 135 | // handling events. You should generally only use this to create a |
| 136 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 137 | // everything else. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 138 | // The pointer is invalidated whenever RemapLoggedChannel is called. |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 139 | const Configuration *configuration() const; |
| 140 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 141 | // Returns the nodes that this log file was created on. This is a list of |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 142 | // pointers to a node in the nodes() list inside logged_configuration(). |
| 143 | std::vector<const Node *> LoggedNodes() const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 144 | |
| 145 | // Returns the starting timestamp for the log file. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 146 | monotonic_clock::time_point monotonic_start_time( |
| 147 | const Node *node = nullptr) const; |
| 148 | realtime_clock::time_point realtime_start_time( |
| 149 | const Node *node = nullptr) const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 150 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 151 | // Sets the start and end times to replay data until for all nodes. This |
| 152 | // overrides the --start_time and --end_time flags. The default is to replay |
| 153 | // all data. |
| 154 | void SetStartTime(std::string start_time); |
| 155 | void SetStartTime(realtime_clock::time_point start_time); |
| 156 | void SetEndTime(std::string end_time); |
| 157 | void SetEndTime(realtime_clock::time_point end_time); |
| 158 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 159 | // Causes the logger to publish the provided channel on a different name so |
| 160 | // that replayed applications can publish on the proper channel name without |
| 161 | // interference. This operates on raw channel names, without any node or |
| 162 | // application specific mappings. |
| 163 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 164 | std::string_view add_prefix = "/original", |
| 165 | std::string_view new_type = ""); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 166 | template <typename T> |
| 167 | void RemapLoggedChannel(std::string_view name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 168 | std::string_view add_prefix = "/original", |
| 169 | std::string_view new_type = "") { |
| 170 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix, new_type); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 173 | // Remaps the provided channel, though this respects node mappings, and |
| 174 | // preserves them too. This makes it so if /aos -> /pi1/aos on one node, |
| 175 | // /original/aos -> /original/pi1/aos on the same node after renaming, just |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 176 | // like you would hope. If new_type is not empty, the new channel will use |
| 177 | // the provided type instead. This allows for renaming messages. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 178 | // |
| 179 | // TODO(austin): If you have 2 nodes remapping something to the same channel, |
| 180 | // this doesn't handle that. No use cases exist yet for that, so it isn't |
| 181 | // being done yet. |
| 182 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
| 183 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 184 | std::string_view add_prefix = "/original", |
| 185 | std::string_view new_type = ""); |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 186 | template <typename T> |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 187 | void RemapLoggedChannel(std::string_view name, const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 188 | std::string_view add_prefix = "/original", |
| 189 | std::string_view new_type = "") { |
| 190 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), node, add_prefix, |
| 191 | new_type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | template <typename T> |
| 195 | bool HasChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 196 | return configuration::GetChannel(logged_configuration(), name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 197 | T::GetFullyQualifiedName(), "", node, |
| 198 | true) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Austin Schuh | 8252906 | 2021-12-08 12:09:52 -0800 | [diff] [blame] | 201 | template <typename T> |
| 202 | void MaybeRemapLoggedChannel(std::string_view name, |
| 203 | const Node *node = nullptr) { |
| 204 | if (HasChannel<T>(name, node)) { |
| 205 | RemapLoggedChannel<T>(name, node); |
| 206 | } |
| 207 | } |
| 208 | |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 209 | // Returns true if the channel exists on the node and was logged. |
| 210 | template <typename T> |
| 211 | bool HasLoggedChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 212 | const Channel *channel = |
| 213 | configuration::GetChannel(logged_configuration(), name, |
| 214 | T::GetFullyQualifiedName(), "", node, true); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 215 | if (channel == nullptr) return false; |
| 216 | return channel->logger() != LoggerConfig::NOT_LOGGED; |
| 217 | } |
| 218 | |
Austin Schuh | 1c22735 | 2021-09-17 12:53:54 -0700 | [diff] [blame] | 219 | // Returns a list of all the original channels from remapping. |
| 220 | std::vector<const Channel *> RemappedChannels() const; |
| 221 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 222 | SimulatedEventLoopFactory *event_loop_factory() { |
| 223 | return event_loop_factory_; |
| 224 | } |
| 225 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 226 | std::string_view name() const { return log_files_[0].name; } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 227 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 228 | // Set whether to exit the SimulatedEventLoopFactory when we finish reading |
| 229 | // the logfile. |
| 230 | void set_exit_on_finish(bool exit_on_finish) { |
| 231 | exit_on_finish_ = exit_on_finish; |
| 232 | } |
| 233 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame] | 234 | // Sets the realtime replay rate. A value of 1.0 will cause the scheduler to |
| 235 | // try to play events in realtime. 0.5 will run at half speed. Use infinity |
| 236 | // (the default) to run as fast as possible. This can be changed during |
| 237 | // run-time. |
| 238 | // Only applies when running against a SimulatedEventLoopFactory. |
| 239 | void SetRealtimeReplayRate(double replay_rate); |
| 240 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 241 | private: |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 242 | void Register(EventLoop *event_loop, const Node *node); |
| 243 | |
| 244 | void RegisterDuringStartup(EventLoop *event_loop, const Node *node); |
| 245 | |
| 246 | const Channel *RemapChannel(const EventLoop *event_loop, const Node *node, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 247 | const Channel *channel); |
| 248 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 249 | // Queues at least max_out_of_order_duration_ messages into channels_. |
| 250 | void QueueMessages(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 251 | // Handle constructing a configuration with all the additional remapped |
| 252 | // channels from calls to RemapLoggedChannel. |
| 253 | void MakeRemappedConfig(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 254 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 255 | // Returns the number of nodes. |
| 256 | size_t nodes_count() const { |
| 257 | return !configuration::MultiNode(logged_configuration()) |
| 258 | ? 1u |
| 259 | : logged_configuration()->nodes()->size(); |
| 260 | } |
| 261 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 262 | const std::vector<LogFile> log_files_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 263 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 264 | // Class to manage sending RemoteMessages on the provided node after the |
| 265 | // correct delay. |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 266 | class RemoteMessageSender { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 267 | public: |
| 268 | RemoteMessageSender(aos::Sender<message_bridge::RemoteMessage> sender, |
| 269 | EventLoop *event_loop); |
| 270 | RemoteMessageSender(RemoteMessageSender const &) = delete; |
| 271 | RemoteMessageSender &operator=(RemoteMessageSender const &) = delete; |
| 272 | |
| 273 | // Sends the provided message. If monotonic_timestamp_time is min_time, |
| 274 | // send it immediately. |
| 275 | void Send( |
| 276 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 277 | BootTimestamp monotonic_timestamp_time, size_t source_boot_count); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 278 | |
| 279 | private: |
| 280 | // Handles actually sending the timestamp if we were delayed. |
| 281 | void SendTimestamp(); |
| 282 | // Handles scheduling the timer to send at the correct time. |
| 283 | void ScheduleTimestamp(); |
| 284 | |
| 285 | EventLoop *event_loop_; |
| 286 | aos::Sender<message_bridge::RemoteMessage> sender_; |
| 287 | aos::TimerHandler *timer_; |
| 288 | |
| 289 | // Time we are scheduled for, or min_time if we aren't scheduled. |
| 290 | monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time; |
| 291 | |
| 292 | struct Timestamp { |
| 293 | Timestamp(FlatbufferDetachedBuffer<message_bridge::RemoteMessage> |
| 294 | new_remote_message, |
| 295 | monotonic_clock::time_point new_monotonic_timestamp_time) |
| 296 | : remote_message(std::move(new_remote_message)), |
| 297 | monotonic_timestamp_time(new_monotonic_timestamp_time) {} |
| 298 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message; |
| 299 | monotonic_clock::time_point monotonic_timestamp_time; |
| 300 | }; |
| 301 | |
| 302 | // List of messages to send. The timer works through them and then disables |
| 303 | // itself automatically. |
| 304 | std::deque<Timestamp> remote_timestamps_; |
| 305 | }; |
| 306 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 307 | // State per node. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 308 | class State { |
| 309 | public: |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 310 | // Whether we should spin up a separate thread for buffering up messages. |
| 311 | // Only allowed in realtime replay--see comments on threading_ member for |
| 312 | // details. |
| 313 | enum class ThreadedBuffering { kYes, kNo }; |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 314 | State(std::unique_ptr<TimestampMapper> timestamp_mapper, |
| 315 | message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters, |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 316 | const Node *node, ThreadedBuffering threading); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 317 | |
| 318 | // Connects up the timestamp mappers. |
| 319 | void AddPeer(State *peer); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 320 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 321 | TimestampMapper *timestamp_mapper() { return timestamp_mapper_.get(); } |
| 322 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 323 | // Returns the next sorted message with all the timestamps extracted and |
| 324 | // matched. |
| 325 | TimestampedMessage PopOldest(); |
Austin Schuh | 188eabe | 2020-12-29 23:41:13 -0800 | [diff] [blame] | 326 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 327 | // Returns the monotonic time of the oldest message. |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 328 | BootTimestamp SingleThreadedOldestMessageTime(); |
| 329 | // Returns the monotonic time of the oldest message, handling querying the |
| 330 | // separate thread of ThreadedBuffering was set. |
| 331 | BootTimestamp MultiThreadedOldestMessageTime(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 332 | |
| 333 | size_t boot_count() const { |
| 334 | // If we are replaying directly into an event loop, we can't reboot. So |
| 335 | // we will stay stuck on the 0th boot. |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 336 | if (!node_event_loop_factory_) { |
| 337 | if (event_loop_ == nullptr) { |
| 338 | // If boot_count is being checked after startup for any of the |
| 339 | // non-primary nodes, then returning 0 may not be accurate (since |
| 340 | // remote nodes *can* reboot even if the EventLoop being played to |
| 341 | // can't). |
| 342 | CHECK(!started_); |
| 343 | CHECK(!stopped_); |
| 344 | } |
| 345 | return 0u; |
| 346 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 347 | return node_event_loop_factory_->boot_count(); |
| 348 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 349 | |
| 350 | // Primes the queues inside State. Should be called before calling |
| 351 | // OldestMessageTime. |
| 352 | void SeedSortedMessages(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 353 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 354 | void SetupStartupTimer() { |
| 355 | const monotonic_clock::time_point start_time = |
| 356 | monotonic_start_time(boot_count()); |
| 357 | if (start_time == monotonic_clock::min_time) { |
| 358 | LOG(ERROR) |
| 359 | << "No start time, skipping, please figure out when this happens"; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 360 | NotifyLogfileStart(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 361 | return; |
| 362 | } |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 363 | if (node_event_loop_factory_) { |
| 364 | CHECK_GE(start_time + clock_offset(), event_loop_->monotonic_now()); |
| 365 | } |
| 366 | startup_timer_->Setup(start_time + clock_offset()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void set_startup_timer(TimerHandler *timer_handler) { |
| 370 | startup_timer_ = timer_handler; |
| 371 | if (startup_timer_) { |
| 372 | if (event_loop_->node() != nullptr) { |
| 373 | startup_timer_->set_name(absl::StrCat( |
| 374 | event_loop_->node()->name()->string_view(), "_startup")); |
| 375 | } else { |
| 376 | startup_timer_->set_name("startup"); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 381 | // Returns the starting time for this node. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 382 | monotonic_clock::time_point monotonic_start_time(size_t boot_count) const { |
| 383 | return timestamp_mapper_ |
| 384 | ? timestamp_mapper_->monotonic_start_time(boot_count) |
| 385 | : monotonic_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 386 | } |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 387 | realtime_clock::time_point realtime_start_time(size_t boot_count) const { |
| 388 | return timestamp_mapper_ |
| 389 | ? timestamp_mapper_->realtime_start_time(boot_count) |
| 390 | : realtime_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Sets the node event loop factory for replaying into a |
| 394 | // SimulatedEventLoopFactory. Returns the EventLoop to use. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 395 | void SetNodeEventLoopFactory(NodeEventLoopFactory *node_event_loop_factory, |
| 396 | SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 397 | |
| 398 | // Sets and gets the event loop to use. |
| 399 | void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; } |
| 400 | EventLoop *event_loop() { return event_loop_; } |
| 401 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 402 | const Node *node() const { return node_; } |
| 403 | |
| 404 | void Register(EventLoop *event_loop); |
| 405 | |
| 406 | void OnStart(std::function<void()> fn); |
| 407 | void OnEnd(std::function<void()> fn); |
| 408 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 409 | // Sets the current realtime offset from the monotonic clock for this node |
| 410 | // (if we are on a simulated event loop). |
| 411 | void SetRealtimeOffset(monotonic_clock::time_point monotonic_time, |
| 412 | realtime_clock::time_point realtime_time) { |
| 413 | if (node_event_loop_factory_ != nullptr) { |
| 414 | node_event_loop_factory_->SetRealtimeOffset(monotonic_time, |
| 415 | realtime_time); |
| 416 | } |
| 417 | } |
| 418 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 419 | // Returns the MessageHeader sender to log delivery timestamps to for the |
| 420 | // provided remote node. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 421 | RemoteMessageSender *RemoteTimestampSender(const Channel *channel, |
| 422 | const Connection *connection); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 423 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 424 | // Converts a timestamp from the monotonic clock on this node to the |
| 425 | // distributed clock. |
| 426 | distributed_clock::time_point ToDistributedClock( |
| 427 | monotonic_clock::time_point time) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 428 | CHECK(node_event_loop_factory_); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 429 | return node_event_loop_factory_->ToDistributedClock(time); |
| 430 | } |
| 431 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 432 | // Returns the current time on the remote node which sends messages on |
| 433 | // channel_index. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 434 | BootTimestamp monotonic_remote_now(size_t channel_index) { |
| 435 | State *s = channel_source_state_[channel_index]; |
| 436 | return BootTimestamp{ |
| 437 | .boot = s->boot_count(), |
| 438 | .time = s->node_event_loop_factory_->monotonic_now()}; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 441 | // Returns the start time of the remote for the provided channel. |
| 442 | monotonic_clock::time_point monotonic_remote_start_time( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 443 | size_t boot_count, size_t channel_index) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 444 | return channel_source_state_[channel_index]->monotonic_start_time( |
| 445 | boot_count); |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 448 | void DestroyEventLoop() { event_loop_unique_ptr_.reset(); } |
| 449 | |
| 450 | EventLoop *MakeEventLoop() { |
| 451 | CHECK(!event_loop_unique_ptr_); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 452 | // TODO(james): Enable exclusive senders on LogReader to allow us to |
| 453 | // ensure we are remapping channels correctly. |
| 454 | event_loop_unique_ptr_ = node_event_loop_factory_->MakeEventLoop( |
| 455 | "log_reader", {NodeEventLoopFactory::CheckSentTooFast::kNo, |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 456 | NodeEventLoopFactory::ExclusiveSenders::kYes, |
| 457 | NonExclusiveChannels()}); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 458 | return event_loop_unique_ptr_.get(); |
| 459 | } |
| 460 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 461 | distributed_clock::time_point RemoteToDistributedClock( |
| 462 | size_t channel_index, monotonic_clock::time_point time) { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 463 | CHECK(node_event_loop_factory_); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 464 | return channel_source_state_[channel_index] |
| 465 | ->node_event_loop_factory_->ToDistributedClock(time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | const Node *remote_node(size_t channel_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 469 | return channel_source_state_[channel_index] |
| 470 | ->node_event_loop_factory_->node(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 473 | monotonic_clock::time_point monotonic_now() const { |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 474 | return event_loop_->monotonic_now(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 477 | // Sets the number of channels. |
| 478 | void SetChannelCount(size_t count); |
| 479 | |
| 480 | // Sets the sender, filter, and target factory for a channel. |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 481 | void SetChannel(size_t logged_channel_index, size_t factory_channel_index, |
| 482 | std::unique_ptr<RawSender> sender, |
| 483 | message_bridge::NoncausalOffsetEstimator *filter, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 484 | bool is_forwarded, State *source_state); |
| 485 | |
| 486 | void SetRemoteTimestampSender(size_t logged_channel_index, |
| 487 | RemoteMessageSender *remote_timestamp_sender); |
| 488 | |
| 489 | void RunOnStart(); |
| 490 | void RunOnEnd(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 491 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 492 | // Handles a logfile start event to potentially call the OnStart callbacks. |
| 493 | void NotifyLogfileStart(); |
| 494 | // Handles a start time flag start event to potentially call the OnStart |
| 495 | // callbacks. |
| 496 | void NotifyFlagStart(); |
| 497 | |
| 498 | // Handles a logfile end event to potentially call the OnEnd callbacks. |
| 499 | void NotifyLogfileEnd(); |
| 500 | // Handles a end time flag start event to potentially call the OnEnd |
| 501 | // callbacks. |
| 502 | void NotifyFlagEnd(); |
| 503 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 504 | // Unregisters everything so we can destory the event loop. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 505 | // TODO(austin): Is this needed? OnShutdown should be able to serve this |
| 506 | // need. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 507 | void Deregister(); |
| 508 | |
| 509 | // Sets the current TimerHandle for the replay callback. |
| 510 | void set_timer_handler(TimerHandler *timer_handler) { |
| 511 | timer_handler_ = timer_handler; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 512 | if (timer_handler_) { |
| 513 | if (event_loop_->node() != nullptr) { |
| 514 | timer_handler_->set_name(absl::StrCat( |
| 515 | event_loop_->node()->name()->string_view(), "_main")); |
| 516 | } else { |
| 517 | timer_handler_->set_name("main"); |
| 518 | } |
| 519 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 522 | // Creates and registers the --start_time and --end_time event callbacks. |
| 523 | void SetStartTimeFlag(realtime_clock::time_point start_time); |
| 524 | void SetEndTimeFlag(realtime_clock::time_point end_time); |
| 525 | |
| 526 | // Notices the next message to update the start/end time callbacks. |
| 527 | void ObserveNextMessage(monotonic_clock::time_point monotonic_event, |
| 528 | realtime_clock::time_point realtime_event); |
| 529 | |
| 530 | // Clears the start and end time flag handlers so we can delete the event |
| 531 | // loop. |
| 532 | void ClearTimeFlags(); |
| 533 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 534 | // Sets the next wakeup time on the replay callback. |
| 535 | void Setup(monotonic_clock::time_point next_time) { |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 536 | timer_handler_->Setup( |
| 537 | std::max(monotonic_now(), next_time + clock_offset())); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Sends a buffer on the provided channel index. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 541 | bool Send(const TimestampedMessage ×tamped_message); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 542 | |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 543 | void MaybeSetClockOffset(); |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 544 | std::chrono::nanoseconds clock_offset() const { return clock_offset_; } |
| 545 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 546 | // Returns a debug string for the channel merger. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 547 | std::string DebugString() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 548 | if (!timestamp_mapper_) { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 549 | return ""; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 550 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 551 | return timestamp_mapper_->DebugString(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 552 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 553 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 554 | void ClearRemoteTimestampSenders() { |
| 555 | channel_timestamp_loggers_.clear(); |
| 556 | timestamp_loggers_.clear(); |
| 557 | } |
| 558 | |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 559 | void SetFoundLastMessage(bool val) { |
| 560 | found_last_message_ = val; |
| 561 | last_message_.resize(factory_channel_index_.size(), false); |
| 562 | } |
| 563 | bool found_last_message() const { return found_last_message_; } |
| 564 | |
| 565 | void set_last_message(size_t channel_index) { |
| 566 | CHECK_LT(channel_index, last_message_.size()); |
| 567 | last_message_[channel_index] = true; |
| 568 | } |
| 569 | |
| 570 | bool last_message(size_t channel_index) { |
| 571 | CHECK_LT(channel_index, last_message_.size()); |
| 572 | return last_message_[channel_index]; |
| 573 | } |
| 574 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 575 | void set_timing_accuracy_sender( |
| 576 | aos::Sender<timing::ReplayTiming> timing_sender) { |
| 577 | timing_statistics_sender_ = std::move(timing_sender); |
| 578 | OnEnd([this]() { SendMessageTimings(); }); |
| 579 | } |
| 580 | |
| 581 | // If running with ThreadedBuffering::kYes, will start the processing thread |
| 582 | // and queue up messages until the specified time. No-op of |
| 583 | // ThreadedBuffering::kNo is set. Should only be called once. |
| 584 | void QueueThreadUntil(BootTimestamp time); |
| 585 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 586 | private: |
James Kuszmaul | c3f34d1 | 2022-08-15 15:57:55 -0700 | [diff] [blame] | 587 | void TrackMessageSendTiming(const RawSender &sender, |
| 588 | monotonic_clock::time_point expected_send_time); |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 589 | void SendMessageTimings(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 590 | // Log file. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 591 | std::unique_ptr<TimestampMapper> timestamp_mapper_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 592 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 593 | // Senders. |
| 594 | std::vector<std::unique_ptr<RawSender>> channels_; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 595 | std::vector<RemoteMessageSender *> remote_timestamp_senders_; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 596 | // The mapping from logged channel index to sent channel index. Needed for |
| 597 | // sending out MessageHeaders. |
| 598 | std::vector<int> factory_channel_index_; |
| 599 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 600 | struct ContiguousSentTimestamp { |
| 601 | // Most timestamps make it through the network, so it saves a ton of |
| 602 | // memory and CPU to store the start and end, and search for valid ranges. |
| 603 | // For one of the logs I looked at, we had 2 ranges for 4 days. |
| 604 | // |
| 605 | // Save monotonic times as well to help if a queue index ever wraps. Odds |
| 606 | // are very low, but doesn't hurt. |
| 607 | // |
| 608 | // The starting time and matching queue index. |
| 609 | monotonic_clock::time_point starting_monotonic_event_time = |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 610 | monotonic_clock::min_time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 611 | uint32_t starting_queue_index = 0xffffffff; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 612 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 613 | // Ending time and queue index. |
| 614 | monotonic_clock::time_point ending_monotonic_event_time = |
| 615 | monotonic_clock::max_time; |
| 616 | uint32_t ending_queue_index = 0xffffffff; |
| 617 | |
| 618 | // The queue index that the first message was *actually* sent with. The |
| 619 | // queue indices are assumed to be contiguous through this range. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 620 | uint32_t actual_queue_index = 0xffffffff; |
| 621 | }; |
| 622 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 623 | // Returns a list of channels which LogReader will send on but which may |
| 624 | // *also* get sent on by other applications in replay. |
| 625 | std::vector< |
| 626 | std::pair<const aos::Channel *, NodeEventLoopFactory::ExclusiveSenders>> |
| 627 | NonExclusiveChannels(); |
| 628 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 629 | // Stores all the timestamps that have been sent on this channel. This is |
| 630 | // only done for channels which are forwarded and on the node which |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 631 | // initially sends the message. Compress using ranges and offsets. |
| 632 | std::vector<std::unique_ptr<std::vector<ContiguousSentTimestamp>>> |
| 633 | queue_index_map_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 634 | |
| 635 | // Factory (if we are in sim) that this loop was created on. |
| 636 | NodeEventLoopFactory *node_event_loop_factory_ = nullptr; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 637 | SimulatedEventLoopFactory *event_loop_factory_ = nullptr; |
| 638 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 639 | std::unique_ptr<EventLoop> event_loop_unique_ptr_; |
| 640 | // Event loop. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 641 | const Node *node_ = nullptr; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 642 | EventLoop *event_loop_ = nullptr; |
| 643 | // And timer used to send messages. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 644 | TimerHandler *timer_handler_ = nullptr; |
| 645 | TimerHandler *startup_timer_ = nullptr; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 646 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 647 | std::unique_ptr<EventNotifier> start_event_notifier_; |
| 648 | std::unique_ptr<EventNotifier> end_event_notifier_; |
| 649 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 650 | // Filters (or nullptr if it isn't a forwarded channel) for each channel. |
| 651 | // This corresponds to the object which is shared among all the channels |
| 652 | // going between 2 nodes. The second element in the tuple indicates if this |
| 653 | // is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 654 | std::vector<message_bridge::NoncausalOffsetEstimator *> filters_; |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 655 | message_bridge::MultiNodeNoncausalOffsetEstimator *multinode_filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 656 | |
| 657 | // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded |
| 658 | // channel) which correspond to the originating node. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 659 | std::vector<State *> channel_source_state_; |
| 660 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 661 | // This is a cache for channel, connection mapping to the corresponding |
| 662 | // sender. |
| 663 | absl::btree_map<std::pair<const Channel *, const Connection *>, |
| 664 | std::shared_ptr<RemoteMessageSender>> |
| 665 | channel_timestamp_loggers_; |
| 666 | |
| 667 | // Mapping from resolved RemoteMessage channel to RemoteMessage sender. This |
| 668 | // is the channel that timestamps are published to. |
| 669 | absl::btree_map<const Channel *, std::shared_ptr<RemoteMessageSender>> |
| 670 | timestamp_loggers_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 671 | |
James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame] | 672 | // Time offset between the log's monotonic clock and the current event |
| 673 | // loop's monotonic clock. Useful when replaying logs with non-simulated |
| 674 | // event loops. |
| 675 | std::chrono::nanoseconds clock_offset_{0}; |
| 676 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 677 | std::vector<std::function<void()>> on_starts_; |
| 678 | std::vector<std::function<void()>> on_ends_; |
| 679 | |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 680 | std::atomic<bool> stopped_ = false; |
| 681 | std::atomic<bool> started_ = false; |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 682 | |
| 683 | bool found_last_message_ = false; |
| 684 | std::vector<bool> last_message_; |
James Kuszmaul | a16a791 | 2022-06-17 10:58:12 -0700 | [diff] [blame] | 685 | |
| 686 | std::vector<timing::MessageTimingT> send_timings_; |
| 687 | aos::Sender<timing::ReplayTiming> timing_statistics_sender_; |
| 688 | |
| 689 | // Protects access to any internal state after Run() is called. Designed |
| 690 | // assuming that only one node is actually executing in replay. |
| 691 | // Threading design: |
| 692 | // * The worker passed to message_queuer_ has full ownership over all |
| 693 | // the log-reading code, timestamp filters, last_queued_message_, etc. |
| 694 | // * The main thread should only have exclusive access to the replay |
| 695 | // event loop and associated features (mainly senders). |
| 696 | // It will pop an item out of the queue (which does maintain a shared_ptr |
| 697 | // reference which may also be being used by the message_queuer_ thread, |
| 698 | // but having shared_ptr's accessing the same memory from |
| 699 | // separate threads is permissible). |
| 700 | // Enabling this in simulation is currently infeasible due to a lack of |
| 701 | // synchronization in the MultiNodeNoncausalOffsetEstimator. Essentially, |
| 702 | // when the message_queuer_ thread attempts to read/pop messages from the |
| 703 | // timestamp_mapper_, it will end up calling callbacks that update the |
| 704 | // internal state of the MultiNodeNoncausalOffsetEstimator. Simultaneously, |
| 705 | // the event scheduler that is running in the main thread to orchestrate the |
| 706 | // simulation will be querying the estimator to know what the clocks on the |
| 707 | // various nodes are at, leading to potential issues. |
| 708 | ThreadedBuffering threading_; |
| 709 | std::optional<BootTimestamp> last_queued_message_; |
| 710 | std::optional<util::ThreadedQueue<TimestampedMessage, BootTimestamp>> |
| 711 | message_queuer_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 712 | }; |
| 713 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 714 | // Node index -> State. |
| 715 | std::vector<std::unique_ptr<State>> states_; |
| 716 | |
| 717 | // Creates the requested filter if it doesn't exist, regardless of whether |
| 718 | // these nodes can actually communicate directly. The second return value |
| 719 | // reports if this is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 720 | message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a, |
| 721 | const Node *node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 722 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 723 | // List of filters for a connection. The pointer to the first node will be |
| 724 | // less than the second node. |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 725 | std::unique_ptr<message_bridge::MultiNodeNoncausalOffsetEstimator> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 726 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 727 | std::unique_ptr<FlatbufferDetachedBuffer<Configuration>> |
| 728 | remapped_configuration_buffer_; |
| 729 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 730 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_; |
| 731 | SimulatedEventLoopFactory *event_loop_factory_ = nullptr; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 732 | |
| 733 | // Map of channel indices to new name. The channel index will be an index into |
| 734 | // logged_configuration(), and the string key will be the name of the channel |
| 735 | // to send on instead of the logged channel name. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 736 | struct RemappedChannel { |
| 737 | std::string remapped_name; |
| 738 | std::string new_type; |
| 739 | }; |
| 740 | std::map<size_t, RemappedChannel> remapped_channels_; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 741 | std::vector<MapT> maps_; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 742 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 743 | // Number of nodes which still have data to send. This is used to figure out |
| 744 | // when to exit. |
| 745 | size_t live_nodes_ = 0; |
| 746 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 747 | const Configuration *remapped_configuration_ = nullptr; |
| 748 | const Configuration *replay_configuration_ = nullptr; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 749 | |
| 750 | // If true, the replay timer will ignore any missing data. This is used |
| 751 | // during startup when we are bootstrapping everything and trying to get to |
| 752 | // the start of all the log files. |
| 753 | bool ignore_missing_data_ = false; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 754 | |
| 755 | // Whether to exit the SimulatedEventLoop when we finish reading the logs. |
| 756 | bool exit_on_finish_ = true; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 757 | |
| 758 | realtime_clock::time_point start_time_ = realtime_clock::min_time; |
| 759 | realtime_clock::time_point end_time_ = realtime_clock::max_time; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 760 | }; |
| 761 | |
| 762 | } // namespace logger |
| 763 | } // namespace aos |
| 764 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 765 | #endif // AOS_EVENTS_LOGGING_LOG_READER_H_ |