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