Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGER_H_ |
| 2 | #define AOS_EVENTS_LOGGER_H_ |
| 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> |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 6 | #include <string_view> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 7 | #include <tuple> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 8 | #include <vector> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 9 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/log_namer.h" |
Austin Schuh | f6f9bf3 | 2020-10-11 14:37:43 -0700 | [diff] [blame] | 12 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 13 | #include "aos/events/logging/logfile_utils.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 14 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 15 | #include "aos/events/logging/uuid.h" |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 16 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 17 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 18 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 19 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 20 | #include "aos/network/timestamp_filter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | #include "aos/time/time.h" |
| 22 | #include "flatbuffers/flatbuffers.h" |
| 23 | |
| 24 | namespace aos { |
| 25 | namespace logger { |
| 26 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | // Logs all channels available in the event loop to disk every 100 ms. |
| 28 | // Start by logging one message per channel to capture any state and |
| 29 | // configuration that is sent rately on a channel and would affect execution. |
| 30 | class Logger { |
| 31 | public: |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 32 | // Constructs a logger. |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 33 | // event_loop: The event loop used to read the messages. |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 34 | // configuration: When provided, this is the configuration to log, and the |
| 35 | // configuration to use for the channel list to log. If not provided, |
| 36 | // this becomes the configuration from the event loop. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 37 | // should_log: When provided, a filter for channels to log. If not provided, |
| 38 | // all available channels are logged. |
| 39 | Logger(EventLoop *event_loop) |
| 40 | : Logger(event_loop, event_loop->configuration()) {} |
| 41 | Logger(EventLoop *event_loop, const Configuration *configuration) |
| 42 | : Logger(event_loop, configuration, |
| 43 | [](const Channel *) { return true; }) {} |
| 44 | Logger(EventLoop *event_loop, const Configuration *configuration, |
| 45 | std::function<bool(const Channel *)> should_log); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 46 | ~Logger(); |
| 47 | |
| 48 | // Overrides the name in the log file header. |
| 49 | void set_name(std::string_view name) { name_ = name; } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 50 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 51 | // Sets the callback to run after each period of data is logged. Defaults to |
| 52 | // doing nothing. |
| 53 | // |
| 54 | // This callback may safely do things like call Rotate(). |
| 55 | void set_on_logged_period(std::function<void()> on_logged_period) { |
| 56 | on_logged_period_ = std::move(on_logged_period); |
| 57 | } |
| 58 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 59 | void set_separate_config(bool separate_config) { |
| 60 | separate_config_ = separate_config; |
| 61 | } |
| 62 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 63 | // Sets the period between polling the data. Defaults to 100ms. |
| 64 | // |
| 65 | // Changing this while a set of files is being written may result in |
| 66 | // unreadable files. |
| 67 | void set_polling_period(std::chrono::nanoseconds polling_period) { |
| 68 | polling_period_ = polling_period; |
| 69 | } |
| 70 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 71 | std::string_view log_start_uuid() const { return log_start_uuid_; } |
Brian Silverman | 035e418 | 2020-10-06 17:13:00 -0700 | [diff] [blame] | 72 | UUID logger_instance_uuid() const { return logger_instance_uuid_; } |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 73 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 74 | // The maximum time for a single fetch which returned a message, or 0 if none |
| 75 | // of those have happened. |
| 76 | std::chrono::nanoseconds max_message_fetch_time() const { |
| 77 | return max_message_fetch_time_; |
| 78 | } |
| 79 | // The channel for that longest fetch which returned a message, or -1 if none |
| 80 | // of those have happened. |
| 81 | int max_message_fetch_time_channel() const { |
| 82 | return max_message_fetch_time_channel_; |
| 83 | } |
| 84 | // The size of the message returned by that longest fetch, or -1 if none of |
| 85 | // those have happened. |
| 86 | int max_message_fetch_time_size() const { |
| 87 | return max_message_fetch_time_size_; |
| 88 | } |
| 89 | // The total time spent fetching messages. |
| 90 | std::chrono::nanoseconds total_message_fetch_time() const { |
| 91 | return total_message_fetch_time_; |
| 92 | } |
| 93 | // The total number of fetch calls which returned messages. |
| 94 | int total_message_fetch_count() const { return total_message_fetch_count_; } |
| 95 | // The total number of bytes fetched. |
| 96 | int64_t total_message_fetch_bytes() const { |
| 97 | return total_message_fetch_bytes_; |
| 98 | } |
| 99 | |
| 100 | // The total time spent in fetches which did not return a message. |
| 101 | std::chrono::nanoseconds total_nop_fetch_time() const { |
| 102 | return total_nop_fetch_time_; |
| 103 | } |
| 104 | // The total number of fetches which did not return a message. |
| 105 | int total_nop_fetch_count() const { return total_nop_fetch_count_; } |
| 106 | |
| 107 | // The maximum time for a single copy, or 0 if none of those have happened. |
| 108 | std::chrono::nanoseconds max_copy_time() const { return max_copy_time_; } |
| 109 | // The channel for that longest copy, or -1 if none of those have happened. |
| 110 | int max_copy_time_channel() const { return max_copy_time_channel_; } |
| 111 | // The size of the message for that longest copy, or -1 if none of those have |
| 112 | // happened. |
| 113 | int max_copy_time_size() const { return max_copy_time_size_; } |
| 114 | // The total time spent copying messages. |
| 115 | std::chrono::nanoseconds total_copy_time() const { return total_copy_time_; } |
| 116 | // The total number of messages copied. |
| 117 | int total_copy_count() const { return total_copy_count_; } |
| 118 | // The total number of bytes copied. |
| 119 | int64_t total_copy_bytes() const { return total_copy_bytes_; } |
| 120 | |
| 121 | void ResetStatisics(); |
| 122 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 123 | // Rotates the log file(s), triggering new part files to be written for each |
| 124 | // log file. |
| 125 | void Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 126 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 127 | // Starts logging to files with the given naming scheme. |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 128 | // |
| 129 | // log_start_uuid may be used to tie this log event to other log events across |
| 130 | // multiple nodes. The default (empty string) indicates there isn't one |
| 131 | // available. |
| 132 | void StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 133 | std::string_view log_start_uuid = ""); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 134 | |
| 135 | // Stops logging. Ensures any messages through end_time make it into the log. |
| 136 | // |
| 137 | // If you want to stop ASAP, pass min_time to avoid reading any more messages. |
| 138 | // |
| 139 | // Returns the LogNamer in case the caller wants to do anything else with it |
| 140 | // before destroying it. |
| 141 | std::unique_ptr<LogNamer> StopLogging( |
| 142 | aos::monotonic_clock::time_point end_time); |
| 143 | |
| 144 | // Returns whether a log is currently being written. |
| 145 | bool is_started() const { return static_cast<bool>(log_namer_); } |
| 146 | |
| 147 | // Shortcut to call StartLogging with a LocalLogNamer when event processing |
| 148 | // starts. |
| 149 | void StartLoggingLocalNamerOnRun(std::string base_name) { |
| 150 | event_loop_->OnRun([this, base_name]() { |
| 151 | StartLogging( |
| 152 | std::make_unique<LocalLogNamer>(base_name, event_loop_->node())); |
| 153 | }); |
| 154 | } |
| 155 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 156 | private: |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 157 | // Structure to track both a fetcher, and if the data fetched has been |
| 158 | // written. We may want to delay writing data to disk so that we don't let |
| 159 | // data get too far out of order when written to disk so we can avoid making |
| 160 | // it too hard to sort when reading. |
| 161 | struct FetcherStruct { |
| 162 | std::unique_ptr<RawFetcher> fetcher; |
| 163 | bool written = false; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 164 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 165 | // Channel index to log to. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 166 | int channel_index = -1; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 167 | const Channel *channel = nullptr; |
| 168 | const Node *timestamp_node = nullptr; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 169 | |
| 170 | LogType log_type = LogType::kLogMessage; |
| 171 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 172 | // We fill out the metadata at construction, but the actual writers have to |
| 173 | // be updated each time we start logging. To avoid duplicating the complex |
| 174 | // logic determining whether each writer should be initialized, we just |
| 175 | // stash the answer in separate member variables. |
| 176 | bool wants_writer = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 177 | DetachedBufferWriter *writer = nullptr; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 178 | bool wants_timestamp_writer = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 179 | DetachedBufferWriter *timestamp_writer = nullptr; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 180 | bool wants_contents_writer = false; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 181 | DetachedBufferWriter *contents_writer = nullptr; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 182 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 183 | // Node which this data is from, or -1 if it is unknown. |
| 184 | int data_node_index = -1; |
| 185 | // Node that this timestamp is for, or -1 if it is known. |
| 186 | int timestamp_node_index = -1; |
| 187 | // Node that the contents this contents_writer will log are from. |
| 188 | int contents_node_index = -1; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 191 | // Vector mapping from the channel index from the event loop to the logged |
| 192 | // channel index. |
| 193 | std::vector<int> event_loop_to_logged_channel_index_; |
| 194 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 195 | struct NodeState { |
| 196 | aos::monotonic_clock::time_point monotonic_start_time = |
| 197 | aos::monotonic_clock::min_time; |
| 198 | aos::realtime_clock::time_point realtime_start_time = |
| 199 | aos::realtime_clock::min_time; |
| 200 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 201 | bool has_source_node_boot_uuid = false; |
| 202 | |
| 203 | // This is an initial UUID that is a valid UUID4 and is pretty obvious that |
| 204 | // it isn't valid. |
| 205 | std::string source_node_boot_uuid = "00000000-0000-4000-8000-000000000000"; |
| 206 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 207 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> log_file_header = |
| 208 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty(); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 209 | |
| 210 | // True if a header has been written to the start of a log file. |
| 211 | bool header_written = false; |
| 212 | // True if the current written header represents the contents which will |
| 213 | // follow. This is cleared when boot_uuid is known to not match anymore. |
| 214 | bool header_valid = false; |
| 215 | |
| 216 | // Sets the source_node_boot_uuid, properly updating everything. |
| 217 | void SetBootUUID(std::string_view new_source_node_boot_uuid) { |
| 218 | source_node_boot_uuid = new_source_node_boot_uuid; |
| 219 | header_valid = false; |
| 220 | has_source_node_boot_uuid = true; |
| 221 | |
| 222 | flatbuffers::String *source_node_boot_uuid_string = |
| 223 | log_file_header.mutable_message()->mutable_source_node_boot_uuid(); |
| 224 | CHECK_EQ(source_node_boot_uuid.size(), |
| 225 | source_node_boot_uuid_string->size()); |
| 226 | memcpy(source_node_boot_uuid_string->data(), source_node_boot_uuid.data(), |
| 227 | source_node_boot_uuid.size()); |
| 228 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 229 | }; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 230 | |
| 231 | void WriteHeader(); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 232 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 233 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 234 | const Node *node, std::string_view config_sha256); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 235 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 236 | // Writes the header for the provided node if enough information is valid. |
| 237 | void MaybeWriteHeader(int node_index); |
| 238 | // Overload for when we already know node as well. |
| 239 | void MaybeWriteHeader(int node_index, const Node *node); |
| 240 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 241 | bool MaybeUpdateTimestamp( |
| 242 | const Node *node, int node_index, |
| 243 | aos::monotonic_clock::time_point monotonic_start_time, |
| 244 | aos::realtime_clock::time_point realtime_start_time); |
| 245 | |
| 246 | void DoLogData(const monotonic_clock::time_point end_time); |
| 247 | |
| 248 | void WriteMissingTimestamps(); |
| 249 | |
| 250 | // Fetches from each channel until all the data is logged. |
| 251 | void LogUntil(monotonic_clock::time_point t); |
| 252 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 253 | void RecordFetchResult(aos::monotonic_clock::time_point start, |
| 254 | aos::monotonic_clock::time_point end, bool got_new, |
| 255 | FetcherStruct *fetcher); |
| 256 | |
| 257 | void RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 258 | aos::monotonic_clock::time_point end, |
| 259 | FetcherStruct *fetcher); |
| 260 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 261 | // Sets the start time for a specific node. |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 262 | void SetStartTime( |
| 263 | size_t node_index, aos::monotonic_clock::time_point monotonic_start_time, |
| 264 | aos::realtime_clock::time_point realtime_start_time, |
| 265 | aos::monotonic_clock::time_point logger_monotonic_start_time, |
| 266 | aos::realtime_clock::time_point logger_realtime_start_time); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 267 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 268 | EventLoop *const event_loop_; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 269 | // The configuration to place at the top of the log file. |
| 270 | const Configuration *const configuration_; |
| 271 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 272 | UUID log_event_uuid_ = UUID::Zero(); |
| 273 | const UUID logger_instance_uuid_ = UUID::Random(); |
| 274 | std::unique_ptr<LogNamer> log_namer_; |
| 275 | // Empty indicates there isn't one. |
| 276 | std::string log_start_uuid_; |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 277 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 278 | // Name to save in the log file. Defaults to hostname. |
| 279 | std::string name_; |
| 280 | |
| 281 | std::function<void()> on_logged_period_ = []() {}; |
| 282 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 283 | std::chrono::nanoseconds max_message_fetch_time_ = |
| 284 | std::chrono::nanoseconds::zero(); |
| 285 | int max_message_fetch_time_channel_ = -1; |
| 286 | int max_message_fetch_time_size_ = -1; |
| 287 | std::chrono::nanoseconds total_message_fetch_time_ = |
| 288 | std::chrono::nanoseconds::zero(); |
| 289 | int total_message_fetch_count_ = 0; |
| 290 | int64_t total_message_fetch_bytes_ = 0; |
| 291 | |
| 292 | std::chrono::nanoseconds total_nop_fetch_time_ = |
| 293 | std::chrono::nanoseconds::zero(); |
| 294 | int total_nop_fetch_count_ = 0; |
| 295 | |
| 296 | std::chrono::nanoseconds max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 297 | int max_copy_time_channel_ = -1; |
| 298 | int max_copy_time_size_ = -1; |
| 299 | std::chrono::nanoseconds total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 300 | int total_copy_count_ = 0; |
| 301 | int64_t total_copy_bytes_ = 0; |
| 302 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 303 | std::vector<FetcherStruct> fetchers_; |
| 304 | TimerHandler *timer_handler_; |
| 305 | |
| 306 | // Period to poll the channels. |
| 307 | std::chrono::nanoseconds polling_period_ = std::chrono::milliseconds(100); |
| 308 | |
| 309 | // Last time that data was written for all channels to disk. |
| 310 | monotonic_clock::time_point last_synchronized_time_; |
| 311 | |
| 312 | // Max size that the header has consumed. This much extra data will be |
| 313 | // reserved in the builder to avoid reallocating. |
| 314 | size_t max_header_size_ = 0; |
| 315 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 316 | // If true, write the message header into a separate file. |
| 317 | bool separate_config_ = true; |
| 318 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 319 | // Fetcher for all the statistics from all the nodes. |
| 320 | aos::Fetcher<message_bridge::ServerStatistics> server_statistics_fetcher_; |
| 321 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 322 | std::vector<NodeState> node_state_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 323 | }; |
| 324 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 325 | std::vector<std::vector<std::string>> ToLogReaderVector( |
| 326 | const std::vector<LogFile> &log_files); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 327 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 328 | // We end up with one of the following 3 log file types. |
| 329 | // |
| 330 | // Single node logged as the source node. |
| 331 | // -> Replayed just on the source node. |
| 332 | // |
| 333 | // Forwarding timestamps only logged from the perspective of the destination |
| 334 | // node. |
| 335 | // -> Matched with data on source node and logged. |
| 336 | // |
| 337 | // Forwarding timestamps with data logged as the destination node. |
| 338 | // -> Replayed just as the destination |
| 339 | // -> Replayed as the source (Much harder, ordering is not defined) |
| 340 | // |
| 341 | // Duplicate data logged. -> CHECK that it matches and explode otherwise. |
| 342 | // |
| 343 | // This can be boiled down to a set of constraints and tools. |
| 344 | // |
| 345 | // 1) Forwarding timestamps and data need to be logged separately. |
| 346 | // 2) Any forwarded data logged on the destination node needs to be logged |
| 347 | // separately such that it can be sorted. |
| 348 | // |
| 349 | // 1) Log reader needs to be able to sort a list of log files. |
| 350 | // 2) Log reader needs to be able to merge sorted lists of log files. |
| 351 | // 3) Log reader needs to be able to match timestamps with messages. |
| 352 | // |
| 353 | // We also need to be able to generate multiple views of a log file depending on |
| 354 | // the target. |
| 355 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 356 | // Replays all the channels in the logfile to the event loop. |
| 357 | class LogReader { |
| 358 | public: |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 359 | // If you want to supply a new configuration that will be used for replay |
| 360 | // (e.g., to change message rates, or to populate an updated schema), then |
| 361 | // pass it in here. It must provide all the channels that the original logged |
| 362 | // config did. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 363 | // |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 364 | // The single file constructor calls SortParts internally. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 365 | LogReader(std::string_view filename, |
| 366 | const Configuration *replay_configuration = nullptr); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 367 | LogReader(std::vector<LogFile> log_files, |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 368 | const Configuration *replay_configuration = nullptr); |
James Kuszmaul | 7daef36 | 2019-12-31 18:28:17 -0800 | [diff] [blame] | 369 | ~LogReader(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 370 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 371 | // Registers all the callbacks to send the log file data out on an event loop |
| 372 | // created in event_loop_factory. This also updates time to be at the start |
| 373 | // of the log file by running until the log file starts. |
| 374 | // Note: the configuration used in the factory should be configuration() |
| 375 | // below, but can be anything as long as the locations needed to send |
| 376 | // everything are available. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 377 | void Register(SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 378 | // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(), |
| 379 | // and then calls Register. |
| 380 | void Register(); |
| 381 | // Registers callbacks for all the events after the log file starts. This is |
| 382 | // only useful when replaying live. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 383 | void Register(EventLoop *event_loop); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 384 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 385 | // Unregisters the senders. You only need to call this if you separately |
| 386 | // supplied an event loop or event loop factory and the lifetimes are such |
| 387 | // that they need to be explicitly destroyed before the LogReader destructor |
| 388 | // gets called. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 389 | void Deregister(); |
| 390 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 391 | // Returns the configuration being used for replay from the log file. |
| 392 | // Note that this may be different from the configuration actually used for |
| 393 | // handling events. You should generally only use this to create a |
| 394 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 395 | // everything else. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 396 | const Configuration *logged_configuration() const; |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 397 | // Returns the configuration being used for replay from the log file. |
| 398 | // Note that this may be different from the configuration actually used for |
| 399 | // handling events. You should generally only use this to create a |
| 400 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 401 | // everything else. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 402 | // The pointer is invalidated whenever RemapLoggedChannel is called. |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 403 | const Configuration *configuration() const; |
| 404 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 405 | // 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] | 406 | // pointers to a node in the nodes() list inside logged_configuration(). |
| 407 | std::vector<const Node *> LoggedNodes() const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 408 | |
| 409 | // Returns the starting timestamp for the log file. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 410 | monotonic_clock::time_point monotonic_start_time( |
| 411 | const Node *node = nullptr) const; |
| 412 | realtime_clock::time_point realtime_start_time( |
| 413 | const Node *node = nullptr) const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 414 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 415 | // Causes the logger to publish the provided channel on a different name so |
| 416 | // that replayed applications can publish on the proper channel name without |
| 417 | // interference. This operates on raw channel names, without any node or |
| 418 | // application specific mappings. |
| 419 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 420 | std::string_view add_prefix = "/original", |
| 421 | std::string_view new_type = ""); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 422 | template <typename T> |
| 423 | void RemapLoggedChannel(std::string_view name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 424 | std::string_view add_prefix = "/original", |
| 425 | std::string_view new_type = "") { |
| 426 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix, new_type); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 429 | // Remaps the provided channel, though this respects node mappings, and |
| 430 | // preserves them too. This makes it so if /aos -> /pi1/aos on one node, |
| 431 | // /original/aos -> /original/pi1/aos on the same node after renaming, just |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 432 | // like you would hope. If new_type is not empty, the new channel will use |
| 433 | // the provided type instead. This allows for renaming messages. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 434 | // |
| 435 | // TODO(austin): If you have 2 nodes remapping something to the same channel, |
| 436 | // this doesn't handle that. No use cases exist yet for that, so it isn't |
| 437 | // being done yet. |
| 438 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
| 439 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 440 | std::string_view add_prefix = "/original", |
| 441 | std::string_view new_type = ""); |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 442 | template <typename T> |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 443 | void RemapLoggedChannel(std::string_view name, const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 444 | std::string_view add_prefix = "/original", |
| 445 | std::string_view new_type = "") { |
| 446 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), node, add_prefix, |
| 447 | new_type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | template <typename T> |
| 451 | bool HasChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 452 | return configuration::GetChannel(logged_configuration(), name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 453 | T::GetFullyQualifiedName(), "", node, |
| 454 | true) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 455 | } |
| 456 | |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 457 | // Returns true if the channel exists on the node and was logged. |
| 458 | template <typename T> |
| 459 | bool HasLoggedChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame^] | 460 | const Channel *channel = |
| 461 | configuration::GetChannel(logged_configuration(), name, |
| 462 | T::GetFullyQualifiedName(), "", node, true); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 463 | if (channel == nullptr) return false; |
| 464 | return channel->logger() != LoggerConfig::NOT_LOGGED; |
| 465 | } |
| 466 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 467 | SimulatedEventLoopFactory *event_loop_factory() { |
| 468 | return event_loop_factory_; |
| 469 | } |
| 470 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 471 | std::string_view name() const { return log_files_[0].name; } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 472 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 473 | // Set whether to exit the SimulatedEventLoopFactory when we finish reading |
| 474 | // the logfile. |
| 475 | void set_exit_on_finish(bool exit_on_finish) { |
| 476 | exit_on_finish_ = exit_on_finish; |
| 477 | } |
| 478 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 479 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 480 | const Channel *RemapChannel(const EventLoop *event_loop, |
| 481 | const Channel *channel); |
| 482 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 483 | // Queues at least max_out_of_order_duration_ messages into channels_. |
| 484 | void QueueMessages(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 485 | // Handle constructing a configuration with all the additional remapped |
| 486 | // channels from calls to RemapLoggedChannel. |
| 487 | void MakeRemappedConfig(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 488 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 489 | // Returns the number of nodes. |
| 490 | size_t nodes_count() const { |
| 491 | return !configuration::MultiNode(logged_configuration()) |
| 492 | ? 1u |
| 493 | : logged_configuration()->nodes()->size(); |
| 494 | } |
| 495 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 496 | const std::vector<LogFile> log_files_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 497 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 498 | // Class to manage sending RemoteMessages on the provided node after the |
| 499 | // correct delay. |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame^] | 500 | class RemoteMessageSender { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 501 | public: |
| 502 | RemoteMessageSender(aos::Sender<message_bridge::RemoteMessage> sender, |
| 503 | EventLoop *event_loop); |
| 504 | RemoteMessageSender(RemoteMessageSender const &) = delete; |
| 505 | RemoteMessageSender &operator=(RemoteMessageSender const &) = delete; |
| 506 | |
| 507 | // Sends the provided message. If monotonic_timestamp_time is min_time, |
| 508 | // send it immediately. |
| 509 | void Send( |
| 510 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message, |
| 511 | monotonic_clock::time_point monotonic_timestamp_time); |
| 512 | |
| 513 | private: |
| 514 | // Handles actually sending the timestamp if we were delayed. |
| 515 | void SendTimestamp(); |
| 516 | // Handles scheduling the timer to send at the correct time. |
| 517 | void ScheduleTimestamp(); |
| 518 | |
| 519 | EventLoop *event_loop_; |
| 520 | aos::Sender<message_bridge::RemoteMessage> sender_; |
| 521 | aos::TimerHandler *timer_; |
| 522 | |
| 523 | // Time we are scheduled for, or min_time if we aren't scheduled. |
| 524 | monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time; |
| 525 | |
| 526 | struct Timestamp { |
| 527 | Timestamp(FlatbufferDetachedBuffer<message_bridge::RemoteMessage> |
| 528 | new_remote_message, |
| 529 | monotonic_clock::time_point new_monotonic_timestamp_time) |
| 530 | : remote_message(std::move(new_remote_message)), |
| 531 | monotonic_timestamp_time(new_monotonic_timestamp_time) {} |
| 532 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message; |
| 533 | monotonic_clock::time_point monotonic_timestamp_time; |
| 534 | }; |
| 535 | |
| 536 | // List of messages to send. The timer works through them and then disables |
| 537 | // itself automatically. |
| 538 | std::deque<Timestamp> remote_timestamps_; |
| 539 | }; |
| 540 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 541 | // State per node. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 542 | class State { |
| 543 | public: |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 544 | State(std::unique_ptr<TimestampMapper> timestamp_mapper); |
| 545 | |
| 546 | // Connects up the timestamp mappers. |
| 547 | void AddPeer(State *peer); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 548 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 549 | TimestampMapper *timestamp_mapper() { return timestamp_mapper_.get(); } |
| 550 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 551 | // Returns the next sorted message with all the timestamps extracted and |
| 552 | // matched. |
| 553 | TimestampedMessage PopOldest(); |
Austin Schuh | 188eabe | 2020-12-29 23:41:13 -0800 | [diff] [blame] | 554 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 555 | // Returns the monotonic time of the oldest message. |
| 556 | monotonic_clock::time_point OldestMessageTime() const; |
| 557 | |
| 558 | // Primes the queues inside State. Should be called before calling |
| 559 | // OldestMessageTime. |
| 560 | void SeedSortedMessages(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 561 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 562 | // Returns the starting time for this node. |
| 563 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 564 | return timestamp_mapper_ ? timestamp_mapper_->monotonic_start_time() |
| 565 | : monotonic_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 566 | } |
| 567 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 568 | return timestamp_mapper_ ? timestamp_mapper_->realtime_start_time() |
| 569 | : realtime_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Sets the node event loop factory for replaying into a |
| 573 | // SimulatedEventLoopFactory. Returns the EventLoop to use. |
| 574 | EventLoop *SetNodeEventLoopFactory( |
| 575 | NodeEventLoopFactory *node_event_loop_factory); |
| 576 | |
| 577 | // Sets and gets the event loop to use. |
| 578 | void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; } |
| 579 | EventLoop *event_loop() { return event_loop_; } |
| 580 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 581 | // Sets the current realtime offset from the monotonic clock for this node |
| 582 | // (if we are on a simulated event loop). |
| 583 | void SetRealtimeOffset(monotonic_clock::time_point monotonic_time, |
| 584 | realtime_clock::time_point realtime_time) { |
| 585 | if (node_event_loop_factory_ != nullptr) { |
| 586 | node_event_loop_factory_->SetRealtimeOffset(monotonic_time, |
| 587 | realtime_time); |
| 588 | } |
| 589 | } |
| 590 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 591 | // Returns the MessageHeader sender to log delivery timestamps to for the |
| 592 | // provided remote node. |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 593 | RemoteMessageSender *RemoteTimestampSender(const Node *delivered_node); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 594 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 595 | // Converts a timestamp from the monotonic clock on this node to the |
| 596 | // distributed clock. |
| 597 | distributed_clock::time_point ToDistributedClock( |
| 598 | monotonic_clock::time_point time) { |
| 599 | return node_event_loop_factory_->ToDistributedClock(time); |
| 600 | } |
| 601 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 602 | // Returns the current time on the remote node which sends messages on |
| 603 | // channel_index. |
| 604 | monotonic_clock::time_point monotonic_remote_now(size_t channel_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 605 | return channel_source_state_[channel_index] |
| 606 | ->node_event_loop_factory_->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame^] | 609 | // Returns the start time of the remote for the provided channel. |
| 610 | monotonic_clock::time_point monotonic_remote_start_time( |
| 611 | size_t channel_index) { |
| 612 | return channel_source_state_[channel_index]->monotonic_start_time(); |
| 613 | } |
| 614 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 615 | distributed_clock::time_point RemoteToDistributedClock( |
| 616 | size_t channel_index, monotonic_clock::time_point time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 617 | return channel_source_state_[channel_index] |
| 618 | ->node_event_loop_factory_->ToDistributedClock(time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | const Node *remote_node(size_t channel_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 622 | return channel_source_state_[channel_index] |
| 623 | ->node_event_loop_factory_->node(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | monotonic_clock::time_point monotonic_now() { |
| 627 | return node_event_loop_factory_->monotonic_now(); |
| 628 | } |
| 629 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 630 | // Sets the number of channels. |
| 631 | void SetChannelCount(size_t count); |
| 632 | |
| 633 | // Sets the sender, filter, and target factory for a channel. |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 634 | void SetChannel(size_t logged_channel_index, size_t factory_channel_index, |
| 635 | std::unique_ptr<RawSender> sender, |
| 636 | message_bridge::NoncausalOffsetEstimator *filter, |
| 637 | RemoteMessageSender *remote_timestamp_sender, |
| 638 | State *source_state); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 639 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 640 | // Unregisters everything so we can destory the event loop. |
| 641 | void Deregister(); |
| 642 | |
| 643 | // Sets the current TimerHandle for the replay callback. |
| 644 | void set_timer_handler(TimerHandler *timer_handler) { |
| 645 | timer_handler_ = timer_handler; |
| 646 | } |
| 647 | |
| 648 | // Sets the next wakeup time on the replay callback. |
| 649 | void Setup(monotonic_clock::time_point next_time) { |
| 650 | timer_handler_->Setup(next_time); |
| 651 | } |
| 652 | |
| 653 | // Sends a buffer on the provided channel index. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 654 | bool Send(const TimestampedMessage ×tamped_message); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 655 | |
| 656 | // Returns a debug string for the channel merger. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 657 | std::string DebugString() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 658 | if (!timestamp_mapper_) { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 659 | return ""; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 660 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 661 | return timestamp_mapper_->DebugString(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 662 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 663 | |
| 664 | private: |
| 665 | // Log file. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 666 | std::unique_ptr<TimestampMapper> timestamp_mapper_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 667 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 668 | // Senders. |
| 669 | std::vector<std::unique_ptr<RawSender>> channels_; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 670 | std::vector<RemoteMessageSender *> remote_timestamp_senders_; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 671 | // The mapping from logged channel index to sent channel index. Needed for |
| 672 | // sending out MessageHeaders. |
| 673 | std::vector<int> factory_channel_index_; |
| 674 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 675 | struct ContiguousSentTimestamp { |
| 676 | // Most timestamps make it through the network, so it saves a ton of |
| 677 | // memory and CPU to store the start and end, and search for valid ranges. |
| 678 | // For one of the logs I looked at, we had 2 ranges for 4 days. |
| 679 | // |
| 680 | // Save monotonic times as well to help if a queue index ever wraps. Odds |
| 681 | // are very low, but doesn't hurt. |
| 682 | // |
| 683 | // The starting time and matching queue index. |
| 684 | monotonic_clock::time_point starting_monotonic_event_time = |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 685 | monotonic_clock::min_time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 686 | uint32_t starting_queue_index = 0xffffffff; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 687 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 688 | // Ending time and queue index. |
| 689 | monotonic_clock::time_point ending_monotonic_event_time = |
| 690 | monotonic_clock::max_time; |
| 691 | uint32_t ending_queue_index = 0xffffffff; |
| 692 | |
| 693 | // The queue index that the first message was *actually* sent with. The |
| 694 | // queue indices are assumed to be contiguous through this range. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 695 | uint32_t actual_queue_index = 0xffffffff; |
| 696 | }; |
| 697 | |
| 698 | // Stores all the timestamps that have been sent on this channel. This is |
| 699 | // only done for channels which are forwarded and on the node which |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 700 | // initially sends the message. Compress using ranges and offsets. |
| 701 | std::vector<std::unique_ptr<std::vector<ContiguousSentTimestamp>>> |
| 702 | queue_index_map_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 703 | |
| 704 | // Factory (if we are in sim) that this loop was created on. |
| 705 | NodeEventLoopFactory *node_event_loop_factory_ = nullptr; |
| 706 | std::unique_ptr<EventLoop> event_loop_unique_ptr_; |
| 707 | // Event loop. |
| 708 | EventLoop *event_loop_ = nullptr; |
| 709 | // And timer used to send messages. |
| 710 | TimerHandler *timer_handler_; |
| 711 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 712 | // Filters (or nullptr if it isn't a forwarded channel) for each channel. |
| 713 | // This corresponds to the object which is shared among all the channels |
| 714 | // going between 2 nodes. The second element in the tuple indicates if this |
| 715 | // is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 716 | std::vector<message_bridge::NoncausalOffsetEstimator *> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 717 | |
| 718 | // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded |
| 719 | // channel) which correspond to the originating node. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 720 | std::vector<State *> channel_source_state_; |
| 721 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 722 | std::map<const Node *, std::unique_ptr<RemoteMessageSender>> |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 723 | remote_timestamp_senders_map_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 724 | }; |
| 725 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 726 | // Node index -> State. |
| 727 | std::vector<std::unique_ptr<State>> states_; |
| 728 | |
| 729 | // Creates the requested filter if it doesn't exist, regardless of whether |
| 730 | // these nodes can actually communicate directly. The second return value |
| 731 | // reports if this is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 732 | message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a, |
| 733 | const Node *node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 734 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 735 | // List of filters for a connection. The pointer to the first node will be |
| 736 | // less than the second node. |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 737 | std::unique_ptr<message_bridge::MultiNodeNoncausalOffsetEstimator> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 738 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 739 | std::unique_ptr<FlatbufferDetachedBuffer<Configuration>> |
| 740 | remapped_configuration_buffer_; |
| 741 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 742 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_; |
| 743 | SimulatedEventLoopFactory *event_loop_factory_ = nullptr; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 744 | |
| 745 | // Map of channel indices to new name. The channel index will be an index into |
| 746 | // logged_configuration(), and the string key will be the name of the channel |
| 747 | // to send on instead of the logged channel name. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 748 | struct RemappedChannel { |
| 749 | std::string remapped_name; |
| 750 | std::string new_type; |
| 751 | }; |
| 752 | std::map<size_t, RemappedChannel> remapped_channels_; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 753 | std::vector<MapT> maps_; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 754 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 755 | // Number of nodes which still have data to send. This is used to figure out |
| 756 | // when to exit. |
| 757 | size_t live_nodes_ = 0; |
| 758 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 759 | const Configuration *remapped_configuration_ = nullptr; |
| 760 | const Configuration *replay_configuration_ = nullptr; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 761 | |
| 762 | // If true, the replay timer will ignore any missing data. This is used |
| 763 | // during startup when we are bootstrapping everything and trying to get to |
| 764 | // the start of all the log files. |
| 765 | bool ignore_missing_data_ = false; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 766 | |
| 767 | // Whether to exit the SimulatedEventLoop when we finish reading the logs. |
| 768 | bool exit_on_finish_ = true; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 769 | }; |
| 770 | |
| 771 | } // namespace logger |
| 772 | } // namespace aos |
| 773 | |
| 774 | #endif // AOS_EVENTS_LOGGER_H_ |