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