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