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