James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logger.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include <fcntl.h> |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 4 | #include <limits.h> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/uio.h> |
| 8 | #include <vector> |
| 9 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 10 | #include "absl/strings/escaping.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 11 | #include "absl/types/span.h" |
| 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" |
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 | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 16 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 17 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 18 | #include "aos/network/remote_message_generated.h" |
| 19 | #include "aos/network/remote_message_schema.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 20 | #include "aos/network/team_number.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | #include "aos/time/time.h" |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 22 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 23 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 24 | #include "openssl/sha.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 26 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 27 | "If true, drop any forwarding entries with missing data. If " |
| 28 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 30 | DECLARE_bool(timestamps_to_csv); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 32 | DEFINE_bool(skip_order_validation, false, |
| 33 | "If true, ignore any out of orderness in replay"); |
| 34 | |
Austin Schuh | f068866 | 2020-12-19 15:37:45 -0800 | [diff] [blame] | 35 | DEFINE_double( |
| 36 | time_estimation_buffer_seconds, 2.0, |
| 37 | "The time to buffer ahead in the log file to accurately reconstruct time."); |
| 38 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 39 | namespace aos { |
| 40 | namespace logger { |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 41 | namespace { |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 42 | std::string Sha256(const absl::Span<const uint8_t> str) { |
| 43 | unsigned char hash[SHA256_DIGEST_LENGTH]; |
| 44 | SHA256_CTX sha256; |
| 45 | SHA256_Init(&sha256); |
| 46 | SHA256_Update(&sha256, str.data(), str.size()); |
| 47 | SHA256_Final(hash, &sha256); |
| 48 | std::stringstream ss; |
| 49 | for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { |
| 50 | ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; |
| 51 | } |
| 52 | return ss.str(); |
| 53 | } |
| 54 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 55 | std::string LogFileVectorToString(std::vector<LogFile> log_files) { |
| 56 | std::stringstream ss; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 57 | for (const auto &f : log_files) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 58 | ss << f << "\n"; |
| 59 | } |
| 60 | return ss.str(); |
| 61 | } |
| 62 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 63 | // Copies the channel, removing the schema as we go. If new_name is provided, |
| 64 | // it is used instead of the name inside the channel. If new_type is provided, |
| 65 | // it is used instead of the type in the channel. |
| 66 | flatbuffers::Offset<Channel> CopyChannel(const Channel *c, |
| 67 | std::string_view new_name, |
| 68 | std::string_view new_type, |
| 69 | flatbuffers::FlatBufferBuilder *fbb) { |
| 70 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 71 | fbb->CreateSharedString(new_name.empty() ? c->name()->string_view() |
| 72 | : new_name); |
| 73 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 74 | fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type); |
| 75 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 76 | c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str()) |
| 77 | : 0; |
| 78 | |
| 79 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 80 | destination_nodes_offset = |
| 81 | aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb); |
| 82 | |
| 83 | flatbuffers::Offset< |
| 84 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 85 | logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb); |
| 86 | |
| 87 | Channel::Builder channel_builder(*fbb); |
| 88 | channel_builder.add_name(name_offset); |
| 89 | channel_builder.add_type(type_offset); |
| 90 | if (c->has_frequency()) { |
| 91 | channel_builder.add_frequency(c->frequency()); |
| 92 | } |
| 93 | if (c->has_max_size()) { |
| 94 | channel_builder.add_max_size(c->max_size()); |
| 95 | } |
| 96 | if (c->has_num_senders()) { |
| 97 | channel_builder.add_num_senders(c->num_senders()); |
| 98 | } |
| 99 | if (c->has_num_watchers()) { |
| 100 | channel_builder.add_num_watchers(c->num_watchers()); |
| 101 | } |
| 102 | if (!source_node_offset.IsNull()) { |
| 103 | channel_builder.add_source_node(source_node_offset); |
| 104 | } |
| 105 | if (!destination_nodes_offset.IsNull()) { |
| 106 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 107 | } |
| 108 | if (c->has_logger()) { |
| 109 | channel_builder.add_logger(c->logger()); |
| 110 | } |
| 111 | if (!logger_nodes_offset.IsNull()) { |
| 112 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 113 | } |
| 114 | if (c->has_read_method()) { |
| 115 | channel_builder.add_read_method(c->read_method()); |
| 116 | } |
| 117 | if (c->has_num_readers()) { |
| 118 | channel_builder.add_num_readers(c->num_readers()); |
| 119 | } |
| 120 | return channel_builder.Finish(); |
| 121 | } |
| 122 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 123 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 124 | using message_bridge::RemoteMessage; |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 125 | } // namespace |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 126 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 127 | Logger::Logger(EventLoop *event_loop, const Configuration *configuration, |
| 128 | std::function<bool(const Channel *)> should_log) |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 129 | : event_loop_(event_loop), |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 130 | configuration_(configuration), |
| 131 | name_(network::GetHostname()), |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 132 | timer_handler_(event_loop_->AddTimer( |
| 133 | [this]() { DoLogData(event_loop_->monotonic_now()); })), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 134 | server_statistics_fetcher_( |
| 135 | configuration::MultiNode(event_loop_->configuration()) |
| 136 | ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>( |
| 137 | "/aos") |
| 138 | : aos::Fetcher<message_bridge::ServerStatistics>()) { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 139 | VLOG(1) << "Creating logger for " << FlatbufferToJson(event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 140 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 141 | // Find all the nodes which are logging timestamps on our node. This may |
| 142 | // over-estimate if should_log is specified. |
| 143 | std::vector<const Node *> timestamp_logger_nodes = |
| 144 | configuration::TimestampNodes(configuration_, event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 145 | |
| 146 | std::map<const Channel *, const Node *> timestamp_logger_channels; |
| 147 | |
| 148 | // Now that we have all the nodes accumulated, make remote timestamp loggers |
| 149 | // for them. |
| 150 | for (const Node *node : timestamp_logger_nodes) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 151 | // Note: since we are doing a find using the event loop channel, we need to |
| 152 | // make sure this channel pointer is part of the event loop configuration, |
| 153 | // not configuration_. This only matters when configuration_ != |
| 154 | // event_loop->configuration(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 155 | const Channel *channel = configuration::GetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 156 | event_loop->configuration(), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 157 | absl::StrCat("/aos/remote_timestamps/", node->name()->string_view()), |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 158 | RemoteMessage::GetFullyQualifiedName(), event_loop_->name(), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 159 | event_loop_->node()); |
| 160 | |
| 161 | CHECK(channel != nullptr) |
| 162 | << ": Remote timestamps are logged on " |
| 163 | << event_loop_->node()->name()->string_view() |
| 164 | << " but can't find channel /aos/remote_timestamps/" |
| 165 | << node->name()->string_view(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 166 | if (!should_log(channel)) { |
| 167 | continue; |
| 168 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 169 | timestamp_logger_channels.insert(std::make_pair(channel, node)); |
| 170 | } |
| 171 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 172 | const size_t our_node_index = |
| 173 | configuration::GetNodeIndex(configuration_, event_loop_->node()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 174 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 175 | for (size_t channel_index = 0; |
| 176 | channel_index < configuration_->channels()->size(); ++channel_index) { |
| 177 | const Channel *const config_channel = |
| 178 | configuration_->channels()->Get(channel_index); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 179 | // The MakeRawFetcher method needs a channel which is in the event loop |
| 180 | // configuration() object, not the configuration_ object. Go look that up |
| 181 | // from the config. |
| 182 | const Channel *channel = aos::configuration::GetChannel( |
| 183 | event_loop_->configuration(), config_channel->name()->string_view(), |
| 184 | config_channel->type()->string_view(), "", event_loop_->node()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 185 | CHECK(channel != nullptr) |
| 186 | << ": Failed to look up channel " |
| 187 | << aos::configuration::CleanedChannelToString(config_channel); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 188 | if (!should_log(channel)) { |
| 189 | continue; |
| 190 | } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 191 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 192 | FetcherStruct fs; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 193 | fs.channel_index = channel_index; |
| 194 | fs.channel = channel; |
| 195 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 196 | const bool is_local = |
| 197 | configuration::ChannelIsSendableOnNode(channel, event_loop_->node()); |
| 198 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 199 | const bool is_readable = |
| 200 | configuration::ChannelIsReadableOnNode(channel, event_loop_->node()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 201 | const bool is_logged = configuration::ChannelMessageIsLoggedOnNode( |
| 202 | channel, event_loop_->node()); |
| 203 | const bool log_message = is_logged && is_readable; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 204 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 205 | bool log_delivery_times = false; |
| 206 | if (event_loop_->node() != nullptr) { |
| 207 | log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 208 | channel, event_loop_->node(), event_loop_->node()); |
| 209 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 210 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 211 | // Now, detect a RemoteMessage timestamp logger where we should just log the |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 212 | // contents to a file directly. |
| 213 | const bool log_contents = timestamp_logger_channels.find(channel) != |
| 214 | timestamp_logger_channels.end(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 215 | |
| 216 | if (log_message || log_delivery_times || log_contents) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 217 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 218 | VLOG(1) << "Logging channel " |
| 219 | << configuration::CleanedChannelToString(channel); |
| 220 | |
| 221 | if (log_delivery_times) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 222 | VLOG(1) << " Delivery times"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 223 | fs.wants_timestamp_writer = true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 224 | fs.timestamp_node_index = our_node_index; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 225 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 226 | if (log_message) { |
| 227 | VLOG(1) << " Data"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 228 | fs.wants_writer = true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 229 | if (!is_local) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 230 | const Node *source_node = configuration::GetNode( |
| 231 | configuration_, channel->source_node()->string_view()); |
| 232 | fs.data_node_index = |
| 233 | configuration::GetNodeIndex(configuration_, source_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 234 | fs.log_type = LogType::kLogRemoteMessage; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 235 | } else { |
| 236 | fs.data_node_index = our_node_index; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 239 | if (log_contents) { |
| 240 | VLOG(1) << "Timestamp logger channel " |
| 241 | << configuration::CleanedChannelToString(channel); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 242 | fs.timestamp_node = timestamp_logger_channels.find(channel)->second; |
| 243 | fs.wants_contents_writer = true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 244 | fs.contents_node_index = |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 245 | configuration::GetNodeIndex(configuration_, fs.timestamp_node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 246 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 247 | fetchers_.emplace_back(std::move(fs)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 248 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 249 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 250 | |
| 251 | // When we are logging remote timestamps, we need to be able to translate from |
| 252 | // the channel index that the event loop uses to the channel index in the |
| 253 | // config in the log file. |
| 254 | event_loop_to_logged_channel_index_.resize( |
| 255 | event_loop->configuration()->channels()->size(), -1); |
| 256 | for (size_t event_loop_channel_index = 0; |
| 257 | event_loop_channel_index < |
| 258 | event_loop->configuration()->channels()->size(); |
| 259 | ++event_loop_channel_index) { |
| 260 | const Channel *event_loop_channel = |
| 261 | event_loop->configuration()->channels()->Get(event_loop_channel_index); |
| 262 | |
| 263 | const Channel *logged_channel = aos::configuration::GetChannel( |
| 264 | configuration_, event_loop_channel->name()->string_view(), |
| 265 | event_loop_channel->type()->string_view(), "", |
| 266 | configuration::GetNode(configuration_, event_loop_->node())); |
| 267 | |
| 268 | if (logged_channel != nullptr) { |
| 269 | event_loop_to_logged_channel_index_[event_loop_channel_index] = |
| 270 | configuration::ChannelIndex(configuration_, logged_channel); |
| 271 | } |
| 272 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | Logger::~Logger() { |
| 276 | if (log_namer_) { |
| 277 | // If we are replaying a log file, or in simulation, we want to force the |
| 278 | // last bit of data to be logged. The easiest way to deal with this is to |
| 279 | // poll everything as we go to destroy the class, ie, shut down the logger, |
| 280 | // and write it to disk. |
| 281 | StopLogging(event_loop_->monotonic_now()); |
| 282 | } |
| 283 | } |
| 284 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 285 | void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 286 | std::string_view log_start_uuid) { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 287 | CHECK(!log_namer_) << ": Already logging"; |
| 288 | log_namer_ = std::move(log_namer); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 289 | |
| 290 | std::string config_sha256; |
| 291 | if (separate_config_) { |
| 292 | flatbuffers::FlatBufferBuilder fbb; |
| 293 | flatbuffers::Offset<aos::Configuration> configuration_offset = |
| 294 | CopyFlatBuffer(configuration_, &fbb); |
| 295 | LogFileHeader::Builder log_file_header_builder(fbb); |
| 296 | log_file_header_builder.add_configuration(configuration_offset); |
| 297 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 298 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header( |
| 299 | fbb.Release()); |
| 300 | config_sha256 = Sha256(config_header.span()); |
| 301 | LOG(INFO) << "Config sha256 of " << config_sha256; |
| 302 | log_namer_->WriteConfiguration(&config_header, config_sha256); |
| 303 | } |
| 304 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 305 | log_event_uuid_ = UUID::Random(); |
| 306 | log_start_uuid_ = log_start_uuid; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 307 | VLOG(1) << "Starting logger for " << FlatbufferToJson(event_loop_->node()); |
| 308 | |
| 309 | // We want to do as much work as possible before the initial Fetch. Time |
| 310 | // between that and actually starting to log opens up the possibility of |
| 311 | // falling off the end of the queue during that time. |
| 312 | |
| 313 | for (FetcherStruct &f : fetchers_) { |
| 314 | if (f.wants_writer) { |
| 315 | f.writer = log_namer_->MakeWriter(f.channel); |
| 316 | } |
| 317 | if (f.wants_timestamp_writer) { |
| 318 | f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel); |
| 319 | } |
| 320 | if (f.wants_contents_writer) { |
| 321 | f.contents_writer = log_namer_->MakeForwardedTimestampWriter( |
| 322 | f.channel, CHECK_NOTNULL(f.timestamp_node)); |
| 323 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 326 | CHECK(node_state_.empty()); |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 327 | node_state_.resize(configuration::MultiNode(configuration_) |
| 328 | ? configuration_->nodes()->size() |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 329 | : 1u); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 330 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 331 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 332 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 333 | |
Austin Schuh | 816e5d6 | 2021-01-05 23:42:20 -0800 | [diff] [blame] | 334 | node_state_[node_index].log_file_header = MakeHeader(node, config_sha256); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 335 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 336 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 337 | // Grab data from each channel right before we declare the log file started |
| 338 | // so we can capture the latest message on each channel. This lets us have |
| 339 | // non periodic messages with configuration that now get logged. |
| 340 | for (FetcherStruct &f : fetchers_) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 341 | const auto start = event_loop_->monotonic_now(); |
| 342 | const bool got_new = f.fetcher->Fetch(); |
| 343 | const auto end = event_loop_->monotonic_now(); |
| 344 | RecordFetchResult(start, end, got_new, &f); |
| 345 | |
| 346 | // If there is a message, we want to write it. |
| 347 | f.written = f.fetcher->context().data == nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Clear out any old timestamps in case we are re-starting logging. |
| 351 | for (size_t i = 0; i < node_state_.size(); ++i) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 352 | SetStartTime(i, monotonic_clock::min_time, realtime_clock::min_time, |
| 353 | monotonic_clock::min_time, realtime_clock::min_time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | WriteHeader(); |
| 357 | |
| 358 | LOG(INFO) << "Logging node as " << FlatbufferToJson(event_loop_->node()) |
| 359 | << " start_time " << last_synchronized_time_; |
| 360 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 361 | // Force logging up until the start of the log file now, so the messages at |
| 362 | // the start are always ordered before the rest of the messages. |
| 363 | // Note: this ship may have already sailed, but we don't have to make it |
| 364 | // worse. |
| 365 | // TODO(austin): Test... |
| 366 | LogUntil(last_synchronized_time_); |
| 367 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 368 | timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_, |
| 369 | polling_period_); |
| 370 | } |
| 371 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 372 | std::unique_ptr<LogNamer> Logger::StopLogging( |
| 373 | aos::monotonic_clock::time_point end_time) { |
| 374 | CHECK(log_namer_) << ": Not logging right now"; |
| 375 | |
| 376 | if (end_time != aos::monotonic_clock::min_time) { |
| 377 | LogUntil(end_time); |
| 378 | } |
| 379 | timer_handler_->Disable(); |
| 380 | |
| 381 | for (FetcherStruct &f : fetchers_) { |
| 382 | f.writer = nullptr; |
| 383 | f.timestamp_writer = nullptr; |
| 384 | f.contents_writer = nullptr; |
| 385 | } |
| 386 | node_state_.clear(); |
| 387 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 388 | log_event_uuid_ = UUID::Zero(); |
| 389 | log_start_uuid_ = std::string(); |
| 390 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 391 | return std::move(log_namer_); |
| 392 | } |
| 393 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 394 | void Logger::WriteHeader() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 395 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 396 | server_statistics_fetcher_.Fetch(); |
| 397 | } |
| 398 | |
| 399 | aos::monotonic_clock::time_point monotonic_start_time = |
| 400 | event_loop_->monotonic_now(); |
| 401 | aos::realtime_clock::time_point realtime_start_time = |
| 402 | event_loop_->realtime_now(); |
| 403 | |
| 404 | // We need to pick a point in time to declare the log file "started". This |
| 405 | // starts here. It needs to be after everything is fetched so that the |
| 406 | // fetchers are all pointed at the most recent message before the start |
| 407 | // time. |
| 408 | last_synchronized_time_ = monotonic_start_time; |
| 409 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 410 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 411 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 412 | MaybeUpdateTimestamp(node, node_index, monotonic_start_time, |
| 413 | realtime_start_time); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 414 | MaybeWriteHeader(node_index, node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 417 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 418 | void Logger::MaybeWriteHeader(int node_index) { |
| 419 | if (configuration::MultiNode(configuration_)) { |
| 420 | return MaybeWriteHeader(node_index, |
| 421 | configuration_->nodes()->Get(node_index)); |
| 422 | } else { |
| 423 | return MaybeWriteHeader(node_index, nullptr); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void Logger::MaybeWriteHeader(int node_index, const Node *node) { |
| 428 | // This function is responsible for writing the header when the header both |
| 429 | // has valid data, and when it needs to be written. |
| 430 | if (node_state_[node_index].header_written && |
| 431 | node_state_[node_index].header_valid) { |
| 432 | // The header has been written and is valid, nothing to do. |
| 433 | return; |
| 434 | } |
| 435 | if (!node_state_[node_index].has_source_node_boot_uuid) { |
| 436 | // Can't write a header if we don't have the boot UUID. |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | // WriteHeader writes the first header in a log file. We want to do this only |
| 441 | // once. |
| 442 | // |
| 443 | // Rotate rewrites the same header with a new part ID, but keeps the same part |
| 444 | // UUID. We don't want that when things reboot, because that implies that |
| 445 | // parts go together across a reboot. |
| 446 | // |
| 447 | // Reboot resets the parts UUID. So, once we've written a header the first |
| 448 | // time, we want to use Reboot to rotate the log and reset the parts UUID. |
| 449 | // |
| 450 | // header_valid is cleared whenever the remote reboots. |
| 451 | if (node_state_[node_index].header_written) { |
| 452 | log_namer_->Reboot(node, &node_state_[node_index].log_file_header); |
| 453 | } else { |
| 454 | log_namer_->WriteHeader(&node_state_[node_index].log_file_header, node); |
| 455 | |
| 456 | node_state_[node_index].header_written = true; |
| 457 | } |
| 458 | node_state_[node_index].header_valid = true; |
| 459 | } |
| 460 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 461 | void Logger::WriteMissingTimestamps() { |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 462 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 463 | server_statistics_fetcher_.Fetch(); |
| 464 | } else { |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | if (server_statistics_fetcher_.get() == nullptr) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 473 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 474 | if (MaybeUpdateTimestamp( |
| 475 | node, node_index, |
| 476 | server_statistics_fetcher_.context().monotonic_event_time, |
| 477 | server_statistics_fetcher_.context().realtime_event_time)) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 478 | CHECK(node_state_[node_index].header_written); |
| 479 | CHECK(node_state_[node_index].header_valid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 480 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 481 | } else { |
| 482 | MaybeWriteHeader(node_index, node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 487 | void Logger::SetStartTime( |
| 488 | size_t node_index, aos::monotonic_clock::time_point monotonic_start_time, |
| 489 | aos::realtime_clock::time_point realtime_start_time, |
| 490 | aos::monotonic_clock::time_point logger_monotonic_start_time, |
| 491 | aos::realtime_clock::time_point logger_realtime_start_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 492 | node_state_[node_index].monotonic_start_time = monotonic_start_time; |
| 493 | node_state_[node_index].realtime_start_time = realtime_start_time; |
| 494 | node_state_[node_index] |
| 495 | .log_file_header.mutable_message() |
| 496 | ->mutate_monotonic_start_time( |
| 497 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 498 | monotonic_start_time.time_since_epoch()) |
| 499 | .count()); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 500 | |
| 501 | // Add logger start times if they are available in the log file header. |
| 502 | if (node_state_[node_index] |
| 503 | .log_file_header.mutable_message() |
| 504 | ->has_logger_monotonic_start_time()) { |
| 505 | node_state_[node_index] |
| 506 | .log_file_header.mutable_message() |
| 507 | ->mutate_logger_monotonic_start_time( |
| 508 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 509 | logger_monotonic_start_time.time_since_epoch()) |
| 510 | .count()); |
| 511 | } |
| 512 | |
| 513 | if (node_state_[node_index] |
| 514 | .log_file_header.mutable_message() |
| 515 | ->has_logger_realtime_start_time()) { |
| 516 | node_state_[node_index] |
| 517 | .log_file_header.mutable_message() |
| 518 | ->mutate_logger_realtime_start_time( |
| 519 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 520 | logger_realtime_start_time.time_since_epoch()) |
| 521 | .count()); |
| 522 | } |
| 523 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 524 | if (node_state_[node_index] |
| 525 | .log_file_header.mutable_message() |
| 526 | ->has_realtime_start_time()) { |
| 527 | node_state_[node_index] |
| 528 | .log_file_header.mutable_message() |
| 529 | ->mutate_realtime_start_time( |
| 530 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 531 | realtime_start_time.time_since_epoch()) |
| 532 | .count()); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | bool Logger::MaybeUpdateTimestamp( |
| 537 | const Node *node, int node_index, |
| 538 | aos::monotonic_clock::time_point monotonic_start_time, |
| 539 | aos::realtime_clock::time_point realtime_start_time) { |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 540 | // Bail early if the start times are already set. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 541 | if (node_state_[node_index].monotonic_start_time != |
| 542 | monotonic_clock::min_time) { |
| 543 | return false; |
| 544 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 545 | if (event_loop_->node() == node || |
| 546 | !configuration::MultiNode(configuration_)) { |
| 547 | // There are no offsets to compute for ourself, so always succeed. |
| 548 | SetStartTime(node_index, monotonic_start_time, realtime_start_time, |
| 549 | monotonic_start_time, realtime_start_time); |
| 550 | node_state_[node_index].SetBootUUID(event_loop_->boot_uuid().string_view()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 551 | return true; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 552 | } else if (server_statistics_fetcher_.get() != nullptr) { |
| 553 | // We must be a remote node now. Look for the connection and see if it is |
| 554 | // connected. |
| 555 | |
| 556 | for (const message_bridge::ServerConnection *connection : |
| 557 | *server_statistics_fetcher_->connections()) { |
| 558 | if (connection->node()->name()->string_view() != |
| 559 | node->name()->string_view()) { |
| 560 | continue; |
| 561 | } |
| 562 | |
| 563 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 564 | VLOG(1) << node->name()->string_view() |
| 565 | << " is not connected, can't start it yet."; |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | // Update the boot UUID as soon as we know we are connected. |
| 570 | if (!connection->has_boot_uuid()) { |
| 571 | VLOG(1) << "Missing boot_uuid for node " << aos::FlatbufferToJson(node); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | if (!node_state_[node_index].has_source_node_boot_uuid || |
| 576 | node_state_[node_index].source_node_boot_uuid != |
| 577 | connection->boot_uuid()->string_view()) { |
| 578 | node_state_[node_index].SetBootUUID( |
| 579 | connection->boot_uuid()->string_view()); |
| 580 | } |
| 581 | |
| 582 | if (!connection->has_monotonic_offset()) { |
| 583 | VLOG(1) << "Missing monotonic offset for setting start time for node " |
| 584 | << aos::FlatbufferToJson(node); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | // Found it and it is connected. Compensate and go. |
| 589 | SetStartTime(node_index, |
| 590 | monotonic_start_time + |
| 591 | std::chrono::nanoseconds(connection->monotonic_offset()), |
| 592 | realtime_start_time, monotonic_start_time, |
| 593 | realtime_start_time); |
| 594 | return true; |
| 595 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 596 | } |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader( |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 601 | const Node *node, std::string_view config_sha256) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 602 | // Now write the header with this timestamp in it. |
| 603 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 604 | fbb.ForceDefaults(true); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 605 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 606 | flatbuffers::Offset<aos::Configuration> configuration_offset; |
| 607 | if (!separate_config_) { |
| 608 | configuration_offset = CopyFlatBuffer(configuration_, &fbb); |
| 609 | } else { |
| 610 | CHECK(!config_sha256.empty()); |
| 611 | } |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 612 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 613 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 614 | fbb.CreateString(name_); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 615 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 616 | CHECK(log_event_uuid_ != UUID::Zero()); |
| 617 | const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset = |
| 618 | fbb.CreateString(log_event_uuid_.string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 619 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 620 | const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset = |
| 621 | fbb.CreateString(logger_instance_uuid_.string_view()); |
| 622 | |
| 623 | flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset; |
| 624 | if (!log_start_uuid_.empty()) { |
| 625 | log_start_uuid_offset = fbb.CreateString(log_start_uuid_); |
| 626 | } |
| 627 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 628 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset; |
| 629 | if (!config_sha256.empty()) { |
| 630 | config_sha256_offset = fbb.CreateString(config_sha256); |
| 631 | } |
| 632 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 633 | const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset = |
| 634 | fbb.CreateString(event_loop_->boot_uuid().string_view()); |
| 635 | |
| 636 | const flatbuffers::Offset<flatbuffers::String> source_node_boot_uuid_offset = |
| 637 | fbb.CreateString(event_loop_->boot_uuid().string_view()); |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 638 | |
| 639 | const flatbuffers::Offset<flatbuffers::String> parts_uuid_offset = |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 640 | fbb.CreateString("00000000-0000-4000-8000-000000000000"); |
| 641 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 642 | flatbuffers::Offset<Node> node_offset; |
Brian Silverman | 80993c2 | 2020-10-01 15:05:19 -0700 | [diff] [blame] | 643 | flatbuffers::Offset<Node> logger_node_offset; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 644 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 645 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 646 | node_offset = RecursiveCopyFlatBuffer(node, &fbb); |
| 647 | logger_node_offset = RecursiveCopyFlatBuffer(event_loop_->node(), &fbb); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 651 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 652 | log_file_header_builder.add_name(name_offset); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 653 | |
| 654 | // Only add the node if we are running in a multinode configuration. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 655 | if (node != nullptr) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 656 | log_file_header_builder.add_node(node_offset); |
Brian Silverman | 80993c2 | 2020-10-01 15:05:19 -0700 | [diff] [blame] | 657 | log_file_header_builder.add_logger_node(logger_node_offset); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 660 | if (!configuration_offset.IsNull()) { |
| 661 | log_file_header_builder.add_configuration(configuration_offset); |
| 662 | } |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 663 | // The worst case theoretical out of order is the polling period times 2. |
| 664 | // One message could get logged right after the boundary, but be for right |
| 665 | // before the next boundary. And the reverse could happen for another |
| 666 | // message. Report back 3x to be extra safe, and because the cost isn't |
| 667 | // huge on the read side. |
| 668 | log_file_header_builder.add_max_out_of_order_duration( |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 669 | std::chrono::nanoseconds(3 * polling_period_).count()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 670 | |
| 671 | log_file_header_builder.add_monotonic_start_time( |
| 672 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 673 | monotonic_clock::min_time.time_since_epoch()) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 674 | .count()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 675 | if (node == event_loop_->node()) { |
| 676 | log_file_header_builder.add_realtime_start_time( |
| 677 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 678 | realtime_clock::min_time.time_since_epoch()) |
| 679 | .count()); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 680 | } else { |
| 681 | log_file_header_builder.add_logger_monotonic_start_time( |
| 682 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 683 | monotonic_clock::min_time.time_since_epoch()) |
| 684 | .count()); |
| 685 | log_file_header_builder.add_logger_realtime_start_time( |
| 686 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 687 | realtime_clock::min_time.time_since_epoch()) |
| 688 | .count()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 691 | log_file_header_builder.add_log_event_uuid(log_event_uuid_offset); |
| 692 | log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset); |
| 693 | if (!log_start_uuid_offset.IsNull()) { |
| 694 | log_file_header_builder.add_log_start_uuid(log_start_uuid_offset); |
| 695 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 696 | log_file_header_builder.add_logger_node_boot_uuid( |
| 697 | logger_node_boot_uuid_offset); |
| 698 | log_file_header_builder.add_source_node_boot_uuid( |
| 699 | source_node_boot_uuid_offset); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 700 | |
| 701 | log_file_header_builder.add_parts_uuid(parts_uuid_offset); |
| 702 | log_file_header_builder.add_parts_index(0); |
| 703 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 704 | log_file_header_builder.add_configuration_sha256(0); |
| 705 | |
| 706 | if (!config_sha256_offset.IsNull()) { |
| 707 | log_file_header_builder.add_configuration_sha256(config_sha256_offset); |
| 708 | } |
| 709 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 710 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 711 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result( |
| 712 | fbb.Release()); |
| 713 | |
| 714 | CHECK(result.Verify()) << ": Built a corrupted header."; |
| 715 | |
| 716 | return result; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 719 | void Logger::ResetStatisics() { |
| 720 | max_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 721 | max_message_fetch_time_channel_ = -1; |
| 722 | max_message_fetch_time_size_ = -1; |
| 723 | total_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 724 | total_message_fetch_count_ = 0; |
| 725 | total_message_fetch_bytes_ = 0; |
| 726 | total_nop_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 727 | total_nop_fetch_count_ = 0; |
| 728 | max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 729 | max_copy_time_channel_ = -1; |
| 730 | max_copy_time_size_ = -1; |
| 731 | total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 732 | total_copy_count_ = 0; |
| 733 | total_copy_bytes_ = 0; |
| 734 | } |
| 735 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 736 | void Logger::Rotate() { |
| 737 | for (const Node *node : log_namer_->nodes()) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 738 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 739 | log_namer_->Rotate(node, &node_state_[node_index].log_file_header); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | void Logger::LogUntil(monotonic_clock::time_point t) { |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 744 | // Grab the latest ServerStatistics message. This will always have the |
| 745 | // oppertunity to be >= to the current time, so it will always represent any |
| 746 | // reboots which may have happened. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 747 | WriteMissingTimestamps(); |
| 748 | |
| 749 | // Write each channel to disk, one at a time. |
| 750 | for (FetcherStruct &f : fetchers_) { |
| 751 | while (true) { |
| 752 | if (f.written) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 753 | const auto start = event_loop_->monotonic_now(); |
| 754 | const bool got_new = f.fetcher->FetchNext(); |
| 755 | const auto end = event_loop_->monotonic_now(); |
| 756 | RecordFetchResult(start, end, got_new, &f); |
| 757 | if (!got_new) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 758 | VLOG(2) << "No new data on " |
| 759 | << configuration::CleanedChannelToString( |
| 760 | f.fetcher->channel()); |
| 761 | break; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 762 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 763 | f.written = false; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 766 | // TODO(james): Write tests to exercise this logic. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 767 | if (f.fetcher->context().monotonic_event_time >= t) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 768 | break; |
| 769 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 770 | if (f.writer != nullptr) { |
| 771 | // Write! |
| 772 | const auto start = event_loop_->monotonic_now(); |
| 773 | flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size + |
| 774 | max_header_size_); |
| 775 | fbb.ForceDefaults(true); |
| 776 | |
| 777 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 778 | f.channel_index, f.log_type)); |
| 779 | const auto end = event_loop_->monotonic_now(); |
| 780 | RecordCreateMessageTime(start, end, &f); |
| 781 | |
| 782 | VLOG(2) << "Writing data as node " |
| 783 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 784 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 785 | << " to " << f.writer->filename() << " data " |
| 786 | << FlatbufferToJson( |
| 787 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 788 | fbb.GetBufferPointer())); |
| 789 | |
| 790 | max_header_size_ = std::max(max_header_size_, |
| 791 | fbb.GetSize() - f.fetcher->context().size); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 792 | CHECK(node_state_[f.data_node_index].header_valid) |
| 793 | << ": Can't write data before the header on channel " |
| 794 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 795 | f.writer->QueueSizedFlatbuffer(&fbb); |
| 796 | } |
| 797 | |
| 798 | if (f.timestamp_writer != nullptr) { |
| 799 | // And now handle timestamps. |
| 800 | const auto start = event_loop_->monotonic_now(); |
| 801 | flatbuffers::FlatBufferBuilder fbb; |
| 802 | fbb.ForceDefaults(true); |
| 803 | |
| 804 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 805 | f.channel_index, |
| 806 | LogType::kLogDeliveryTimeOnly)); |
| 807 | const auto end = event_loop_->monotonic_now(); |
| 808 | RecordCreateMessageTime(start, end, &f); |
| 809 | |
| 810 | VLOG(2) << "Writing timestamps as node " |
| 811 | << FlatbufferToJson(event_loop_->node()) << " for channel " |
| 812 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 813 | << " to " << f.timestamp_writer->filename() << " timestamp " |
| 814 | << FlatbufferToJson( |
| 815 | flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 816 | fbb.GetBufferPointer())); |
| 817 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 818 | CHECK(node_state_[f.timestamp_node_index].header_valid) |
| 819 | << ": Can't write data before the header on channel " |
| 820 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 821 | f.timestamp_writer->QueueSizedFlatbuffer(&fbb); |
| 822 | } |
| 823 | |
| 824 | if (f.contents_writer != nullptr) { |
| 825 | const auto start = event_loop_->monotonic_now(); |
| 826 | // And now handle the special message contents channel. Copy the |
| 827 | // message into a FlatBufferBuilder and save it to disk. |
| 828 | // TODO(austin): We can be more efficient here when we start to |
| 829 | // care... |
| 830 | flatbuffers::FlatBufferBuilder fbb; |
| 831 | fbb.ForceDefaults(true); |
| 832 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 833 | const RemoteMessage *msg = |
| 834 | flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 835 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 836 | CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg); |
| 837 | if (!node_state_[f.contents_node_index].has_source_node_boot_uuid || |
| 838 | node_state_[f.contents_node_index].source_node_boot_uuid != |
| 839 | msg->boot_uuid()->string_view()) { |
| 840 | node_state_[f.contents_node_index].SetBootUUID( |
| 841 | msg->boot_uuid()->string_view()); |
| 842 | |
| 843 | MaybeWriteHeader(f.contents_node_index); |
| 844 | } |
| 845 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 846 | logger::MessageHeader::Builder message_header_builder(fbb); |
| 847 | |
| 848 | // TODO(austin): This needs to check the channel_index and confirm |
| 849 | // that it should be logged before squirreling away the timestamp to |
| 850 | // disk. We don't want to log irrelevant timestamps. |
| 851 | |
| 852 | // Note: this must match the same order as MessageBridgeServer and |
| 853 | // PackMessage. We want identical headers to have identical |
| 854 | // on-the-wire formats to make comparing them easier. |
| 855 | |
| 856 | // Translate from the channel index that the event loop uses to the |
| 857 | // channel index in the log file. |
| 858 | message_header_builder.add_channel_index( |
| 859 | event_loop_to_logged_channel_index_[msg->channel_index()]); |
| 860 | |
| 861 | message_header_builder.add_queue_index(msg->queue_index()); |
| 862 | message_header_builder.add_monotonic_sent_time( |
| 863 | msg->monotonic_sent_time()); |
| 864 | message_header_builder.add_realtime_sent_time( |
| 865 | msg->realtime_sent_time()); |
| 866 | |
| 867 | message_header_builder.add_monotonic_remote_time( |
| 868 | msg->monotonic_remote_time()); |
| 869 | message_header_builder.add_realtime_remote_time( |
| 870 | msg->realtime_remote_time()); |
| 871 | message_header_builder.add_remote_queue_index( |
| 872 | msg->remote_queue_index()); |
| 873 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 874 | message_header_builder.add_monotonic_timestamp_time( |
| 875 | f.fetcher->context() |
| 876 | .monotonic_event_time.time_since_epoch() |
| 877 | .count()); |
| 878 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 879 | fbb.FinishSizePrefixed(message_header_builder.Finish()); |
| 880 | const auto end = event_loop_->monotonic_now(); |
| 881 | RecordCreateMessageTime(start, end, &f); |
| 882 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 883 | CHECK(node_state_[f.contents_node_index].header_valid) |
| 884 | << ": Can't write data before the header on channel " |
| 885 | << configuration::CleanedChannelToString(f.fetcher->channel()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 886 | f.contents_writer->QueueSizedFlatbuffer(&fbb); |
| 887 | } |
| 888 | |
| 889 | f.written = true; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | last_synchronized_time_ = t; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 895 | void Logger::DoLogData(const monotonic_clock::time_point end_time) { |
| 896 | // We want to guarantee that messages aren't out of order by more than |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 897 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 898 | // cycle should be a sync point. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 899 | |
| 900 | do { |
| 901 | // Move the sync point up by at most polling_period. This forces one sync |
| 902 | // per iteration, even if it is small. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 903 | LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time)); |
| 904 | |
| 905 | on_logged_period_(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 906 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 907 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 908 | // caught up. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 909 | } while (last_synchronized_time_ + polling_period_ < end_time); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 912 | void Logger::RecordFetchResult(aos::monotonic_clock::time_point start, |
| 913 | aos::monotonic_clock::time_point end, |
| 914 | bool got_new, FetcherStruct *fetcher) { |
| 915 | const auto duration = end - start; |
| 916 | if (!got_new) { |
| 917 | ++total_nop_fetch_count_; |
| 918 | total_nop_fetch_time_ += duration; |
| 919 | return; |
| 920 | } |
| 921 | ++total_message_fetch_count_; |
| 922 | total_message_fetch_bytes_ += fetcher->fetcher->context().size; |
| 923 | total_message_fetch_time_ += duration; |
| 924 | if (duration > max_message_fetch_time_) { |
| 925 | max_message_fetch_time_ = duration; |
| 926 | max_message_fetch_time_channel_ = fetcher->channel_index; |
| 927 | max_message_fetch_time_size_ = fetcher->fetcher->context().size; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 932 | aos::monotonic_clock::time_point end, |
| 933 | FetcherStruct *fetcher) { |
| 934 | const auto duration = end - start; |
| 935 | total_copy_time_ += duration; |
| 936 | ++total_copy_count_; |
| 937 | total_copy_bytes_ += fetcher->fetcher->context().size; |
| 938 | if (duration > max_copy_time_) { |
| 939 | max_copy_time_ = duration; |
| 940 | max_copy_time_channel_ = fetcher->channel_index; |
| 941 | max_copy_time_size_ = fetcher->fetcher->context().size; |
| 942 | } |
| 943 | } |
| 944 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 945 | std::vector<std::vector<std::string>> ToLogReaderVector( |
| 946 | const std::vector<LogFile> &log_files) { |
| 947 | std::vector<std::vector<std::string>> result; |
| 948 | for (const LogFile &log_file : log_files) { |
| 949 | for (const LogParts &log_parts : log_file.parts) { |
| 950 | std::vector<std::string> parts; |
| 951 | for (const std::string &part : log_parts.parts) { |
| 952 | parts.emplace_back(part); |
| 953 | } |
| 954 | result.emplace_back(std::move(parts)); |
| 955 | } |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 956 | } |
| 957 | return result; |
| 958 | } |
| 959 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 960 | LogReader::LogReader(std::string_view filename, |
| 961 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 962 | : LogReader(SortParts({std::string(filename)}), replay_configuration) {} |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 963 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 964 | LogReader::LogReader(std::vector<LogFile> log_files, |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 965 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 966 | : log_files_(std::move(log_files)), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 967 | replay_configuration_(replay_configuration) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 968 | CHECK_GT(log_files_.size(), 0u); |
| 969 | { |
| 970 | // Validate that we have the same config everwhere. This will be true if |
| 971 | // all the parts were sorted together and the configs match. |
| 972 | const Configuration *config = nullptr; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 973 | for (const LogFile &log_file : log_files_) { |
| 974 | if (log_file.config.get() == nullptr) { |
| 975 | LOG(FATAL) << "Couldn't find a config in " << log_file; |
| 976 | } |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 977 | if (config == nullptr) { |
| 978 | config = log_file.config.get(); |
| 979 | } else { |
| 980 | CHECK_EQ(config, log_file.config.get()); |
| 981 | } |
| 982 | } |
| 983 | } |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 984 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 985 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 986 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 987 | // Remap all existing remote timestamp channels. They will be recreated, and |
| 988 | // the data logged isn't relevant anymore. |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 989 | for (const Node *node : configuration::GetNodes(logged_configuration())) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 990 | std::vector<const Node *> timestamp_logger_nodes = |
| 991 | configuration::TimestampNodes(logged_configuration(), node); |
| 992 | for (const Node *remote_node : timestamp_logger_nodes) { |
| 993 | const std::string channel = absl::StrCat( |
| 994 | "/aos/remote_timestamps/", remote_node->name()->string_view()); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 995 | // See if the log file is an old log with MessageHeader channels in it, or |
| 996 | // a newer log with RemoteMessage. If we find an older log, rename the |
| 997 | // type too along with the name. |
| 998 | if (HasChannel<MessageHeader>(channel, node)) { |
| 999 | CHECK(!HasChannel<RemoteMessage>(channel, node)) |
| 1000 | << ": Can't have both a MessageHeader and RemoteMessage remote " |
| 1001 | "timestamp channel."; |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 1002 | // In theory, we should check NOT_LOGGED like RemoteMessage and be more |
| 1003 | // careful about updating the config, but there are fewer and fewer logs |
| 1004 | // with MessageHeader remote messages, so it isn't worth the effort. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1005 | RemapLoggedChannel<MessageHeader>(channel, node, "/original", |
| 1006 | "aos.message_bridge.RemoteMessage"); |
| 1007 | } else { |
| 1008 | CHECK(HasChannel<RemoteMessage>(channel, node)) |
| 1009 | << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \"" |
| 1010 | << RemoteMessage::GetFullyQualifiedName() << "\"} for node " |
| 1011 | << node->name()->string_view(); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 1012 | // Only bother to remap if there's something on the channel. We can |
| 1013 | // tell if the channel was marked NOT_LOGGED or not. This makes the |
| 1014 | // config not change un-necesarily when we replay a log with NOT_LOGGED |
| 1015 | // messages. |
| 1016 | if (HasLoggedChannel<RemoteMessage>(channel, node)) { |
| 1017 | RemapLoggedChannel<RemoteMessage>(channel, node); |
| 1018 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1019 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1023 | if (replay_configuration) { |
| 1024 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 1025 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1026 | << ": Log file and replay config need to both be multi or single " |
| 1027 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1030 | if (!configuration::MultiNode(configuration())) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1031 | states_.emplace_back(std::make_unique<State>( |
| 1032 | std::make_unique<TimestampMapper>(FilterPartsForNode(log_files_, "")))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1033 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1034 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1035 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1036 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1037 | << ": Log file and replay config need to have matching nodes " |
| 1038 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1039 | for (const Node *node : *logged_configuration()->nodes()) { |
| 1040 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1041 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 1042 | << " in logged config that is not present in the replay " |
| 1043 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1046 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1047 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1048 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1051 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 1052 | if (event_loop_factory_unique_ptr_) { |
| 1053 | Deregister(); |
| 1054 | } else if (event_loop_factory_ != nullptr) { |
| 1055 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 1056 | "is destroyed"; |
| 1057 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1058 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 1059 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 1060 | if (remapped_configuration_buffer_) { |
| 1061 | remapped_configuration_buffer_->Wipe(); |
| 1062 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1063 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1064 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1065 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 1066 | return log_files_[0].config.get(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1069 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1070 | return remapped_configuration_; |
| 1071 | } |
| 1072 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 1073 | std::vector<const Node *> LogReader::LoggedNodes() const { |
| 1074 | return configuration::GetNodes(logged_configuration()); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1075 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1076 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1077 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 1078 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1079 | State *state = |
| 1080 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 1081 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 1082 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1083 | return state->monotonic_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1086 | realtime_clock::time_point LogReader::realtime_start_time( |
| 1087 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1088 | State *state = |
| 1089 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 1090 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 1091 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1092 | return state->realtime_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1095 | void LogReader::Register() { |
| 1096 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1097 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1098 | Register(event_loop_factory_unique_ptr_.get()); |
| 1099 | } |
| 1100 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1101 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1102 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | e5bbd9e | 2020-09-21 17:29:20 -0700 | [diff] [blame] | 1103 | remapped_configuration_ = event_loop_factory_->configuration(); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 1104 | filters_ = |
| 1105 | std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>( |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 1106 | event_loop_factory_->configuration(), logged_configuration(), |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 1107 | FLAGS_skip_order_validation, |
| 1108 | chrono::duration_cast<chrono::nanoseconds>( |
| 1109 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1110 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1111 | std::vector<TimestampMapper *> timestamp_mappers; |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1112 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1113 | const size_t node_index = |
| 1114 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1115 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 1116 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1117 | |
| 1118 | // Confirm that all the parts are from the same boot if there are enough |
| 1119 | // parts to not be from the same boot. |
| 1120 | if (filtered_parts.size() > 1u) { |
| 1121 | for (size_t i = 1; i < filtered_parts.size(); ++i) { |
| 1122 | CHECK_EQ(filtered_parts[i].source_boot_uuid, |
| 1123 | filtered_parts[0].source_boot_uuid) |
| 1124 | << ": Found parts from different boots " |
| 1125 | << LogFileVectorToString(log_files_); |
| 1126 | } |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 1127 | if (!filtered_parts[0].source_boot_uuid.empty()) { |
| 1128 | event_loop_factory_->GetNodeEventLoopFactory(node)->set_boot_uuid( |
| 1129 | filtered_parts[0].source_boot_uuid); |
| 1130 | } |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1133 | states_[node_index] = std::make_unique<State>( |
| 1134 | filtered_parts.size() == 0u |
| 1135 | ? nullptr |
| 1136 | : std::make_unique<TimestampMapper>(std::move(filtered_parts))); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1137 | State *state = states_[node_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1138 | state->set_event_loop(state->SetNodeEventLoopFactory( |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1139 | event_loop_factory_->GetNodeEventLoopFactory(node))); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1140 | |
| 1141 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1142 | timestamp_mappers.emplace_back(state->timestamp_mapper()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1143 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1144 | filters_->SetTimestampMappers(std::move(timestamp_mappers)); |
| 1145 | |
| 1146 | // Note: this needs to be set before any times are pulled, or we won't observe |
| 1147 | // the timestamps. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1148 | event_loop_factory_->SetTimeConverter(filters_.get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1149 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1150 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 1151 | const size_t node_index = |
| 1152 | configuration::GetNodeIndex(configuration(), node); |
| 1153 | State *state = states_[node_index].get(); |
| 1154 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 1155 | const size_t other_node_index = |
| 1156 | configuration::GetNodeIndex(configuration(), other_node); |
| 1157 | State *other_state = states_[other_node_index].get(); |
| 1158 | if (other_state != state) { |
| 1159 | state->AddPeer(other_state); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1164 | // Register after making all the State objects so we can build references |
| 1165 | // between them. |
| 1166 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 1167 | const size_t node_index = |
| 1168 | configuration::GetNodeIndex(configuration(), node); |
| 1169 | State *state = states_[node_index].get(); |
| 1170 | |
| 1171 | Register(state->event_loop()); |
| 1172 | } |
| 1173 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1174 | if (live_nodes_ == 0) { |
| 1175 | LOG(FATAL) |
| 1176 | << "Don't have logs from any of the nodes in the replay config--are " |
| 1177 | "you sure that the replay config matches the original config?"; |
| 1178 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1179 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1180 | filters_->CheckGraph(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1181 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1182 | for (std::unique_ptr<State> &state : states_) { |
| 1183 | state->SeedSortedMessages(); |
| 1184 | } |
| 1185 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1186 | // We want to start the log file at the last start time of the log files |
| 1187 | // from all the nodes. Compute how long each node's simulation needs to run |
| 1188 | // to move time to this point. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1189 | distributed_clock::time_point start_time = distributed_clock::min_time; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1190 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1191 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 1192 | // running until the last node. |
| 1193 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1194 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1195 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1196 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1197 | << state->monotonic_now(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1198 | if (state->monotonic_start_time() == monotonic_clock::min_time) { |
| 1199 | continue; |
| 1200 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1201 | // And start computing the start time on the distributed clock now that |
| 1202 | // that works. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1203 | start_time = std::max( |
| 1204 | start_time, state->ToDistributedClock(state->monotonic_start_time())); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1205 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1206 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1207 | // TODO(austin): If a node doesn't have a start time, we might not queue |
| 1208 | // enough. If this happens, we'll explode with a frozen error eventually. |
| 1209 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1210 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 1211 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 1212 | "everything."; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1213 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1214 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 1215 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1216 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 1217 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1218 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 1219 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 1220 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 1221 | const Node *node = configuration::GetNode( |
| 1222 | configuration(), channel->source_node()->string_view()); |
| 1223 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1224 | State *state = |
| 1225 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1226 | |
| 1227 | const Channel *remapped_channel = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1228 | RemapChannel(state->event_loop(), channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1229 | |
| 1230 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 1231 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1232 | |
| 1233 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 1234 | // from both the real message bridge and simulated message bridge. |
| 1235 | event_loop_factory_->DisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1238 | // While we are starting the system up, we might be relying on matching data |
| 1239 | // to timestamps on log files where the timestamp log file starts before the |
| 1240 | // data. In this case, it is reasonable to expect missing data. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1241 | { |
| 1242 | const bool prior_ignore_missing_data = ignore_missing_data_; |
| 1243 | ignore_missing_data_ = true; |
| 1244 | VLOG(1) << "Running until " << start_time << " in Register"; |
| 1245 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
| 1246 | VLOG(1) << "At start time"; |
| 1247 | // Now that we are running for real, missing data means that the log file is |
| 1248 | // corrupted or went wrong. |
| 1249 | ignore_missing_data_ = prior_ignore_missing_data; |
| 1250 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1251 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1252 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1253 | // Make the RT clock be correct before handing it to the user. |
| 1254 | if (state->realtime_start_time() != realtime_clock::min_time) { |
| 1255 | state->SetRealtimeOffset(state->monotonic_start_time(), |
| 1256 | state->realtime_start_time()); |
| 1257 | } |
| 1258 | VLOG(1) << "Start time is " << state->monotonic_start_time() << " for node " |
| 1259 | << MaybeNodeName(state->event_loop()->node()) << "now " |
| 1260 | << state->monotonic_now(); |
| 1261 | } |
| 1262 | |
| 1263 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 1264 | filters_->Start(event_loop_factory); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1268 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1269 | const Node *node_a, const Node *node_b) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 1270 | if (filters_) { |
| 1271 | return filters_->GetFilter(node_a, node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1272 | } |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 1273 | return nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1276 | void LogReader::Register(EventLoop *event_loop) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1277 | State *state = |
| 1278 | states_[configuration::GetNodeIndex(configuration(), event_loop->node())] |
| 1279 | .get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1280 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1281 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1282 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1283 | // We don't run timing reports when trying to print out logged data, because |
| 1284 | // otherwise we would end up printing out the timing reports themselves... |
| 1285 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1286 | event_loop->SkipTimingReport(); |
| 1287 | event_loop->SkipAosLog(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1288 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1289 | for (size_t logged_channel_index = 0; |
| 1290 | logged_channel_index < logged_configuration()->channels()->size(); |
| 1291 | ++logged_channel_index) { |
| 1292 | const Channel *channel = RemapChannel( |
| 1293 | event_loop, |
| 1294 | logged_configuration()->channels()->Get(logged_channel_index)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1295 | |
Austin Schuh | 532656d | 2021-01-11 10:17:18 -0800 | [diff] [blame] | 1296 | if (channel->logger() == LoggerConfig::NOT_LOGGED) { |
| 1297 | continue; |
| 1298 | } |
| 1299 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1300 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1301 | RemoteMessageSender *remote_timestamp_sender = nullptr; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1302 | |
| 1303 | State *source_state = nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1304 | |
| 1305 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 1306 | configuration::ChannelIsReadableOnNode(channel, event_loop->node())) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1307 | // We've got a message which is being forwarded to this node. |
| 1308 | const Node *source_node = configuration::GetNode( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1309 | event_loop->configuration(), channel->source_node()->string_view()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1310 | filter = GetFilter(event_loop->node(), source_node); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1311 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1312 | // Delivery timestamps are supposed to be logged back on the source node. |
| 1313 | // Configure remote timestamps to be sent. |
| 1314 | const bool delivery_time_is_logged = |
| 1315 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 1316 | channel, event_loop->node(), source_node); |
| 1317 | |
| 1318 | source_state = |
| 1319 | states_[configuration::GetNodeIndex(configuration(), source_node)] |
| 1320 | .get(); |
| 1321 | |
| 1322 | if (delivery_time_is_logged) { |
| 1323 | remote_timestamp_sender = |
| 1324 | source_state->RemoteTimestampSender(event_loop->node()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1327 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1328 | state->SetChannel( |
| 1329 | logged_channel_index, |
| 1330 | configuration::ChannelIndex(event_loop->configuration(), channel), |
| 1331 | event_loop->MakeRawSender(channel), filter, remote_timestamp_sender, |
| 1332 | source_state); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1335 | // If we didn't find any log files with data in them, we won't ever get a |
| 1336 | // callback or be live. So skip the rest of the setup. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1337 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1338 | return; |
| 1339 | } |
| 1340 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1341 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1342 | VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node()) |
| 1343 | << "at " << state->event_loop()->context().monotonic_event_time |
| 1344 | << " now " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1345 | if (state->OldestMessageTime() == monotonic_clock::max_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1346 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1347 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 1348 | if (exit_on_finish_ && live_nodes_ == 0) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1349 | event_loop_factory_->Exit(); |
| 1350 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1351 | return; |
| 1352 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1353 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1354 | TimestampedMessage timestamped_message = state->PopOldest(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1355 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1356 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1357 | state->event_loop()->context().monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1358 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1359 | CHECK(monotonic_now == timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1360 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 1361 | << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1362 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1363 | << state->DebugString(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1364 | } else if (monotonic_now != timestamped_message.monotonic_event_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1365 | LOG(WARNING) << "Check failed: monotonic_now == " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1366 | "timestamped_message.monotonic_event_time) (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1367 | << monotonic_now << " vs. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1368 | << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1369 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 1370 | << " Now " << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1371 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1372 | << state->DebugString(); |
| 1373 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1374 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1375 | if (timestamped_message.monotonic_event_time > |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1376 | state->monotonic_start_time() || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1377 | event_loop_factory_ != nullptr) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1378 | if (timestamped_message.data.span().size() != 0u) { |
| 1379 | if (timestamped_message.monotonic_remote_time != |
| 1380 | monotonic_clock::min_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1381 | // Confirm that the message was sent on the sending node before the |
| 1382 | // destination node (this node). As a proxy, do this by making sure |
| 1383 | // that time on the source node is past when the message was sent. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1384 | // |
| 1385 | // TODO(austin): <= means that the cause message (which we know) could |
| 1386 | // happen after the effect even though we know they are at the same |
| 1387 | // time. I doubt anyone will notice for a bit, but we should really |
| 1388 | // fix that. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1389 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1390 | CHECK_LE( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1391 | timestamped_message.monotonic_remote_time, |
| 1392 | state->monotonic_remote_now(timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1393 | << state->event_loop()->node()->name()->string_view() << " to " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1394 | << state->remote_node(timestamped_message.channel_index) |
| 1395 | ->name() |
| 1396 | ->string_view() |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1397 | << " while trying to send a message on " |
| 1398 | << configuration::CleanedChannelToString( |
| 1399 | logged_configuration()->channels()->Get( |
| 1400 | timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1401 | << " " << state->DebugString(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1402 | } else if (timestamped_message.monotonic_remote_time > |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1403 | state->monotonic_remote_now( |
| 1404 | timestamped_message.channel_index)) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1405 | LOG(WARNING) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1406 | << "Check failed: timestamped_message.monotonic_remote_time < " |
| 1407 | "state->monotonic_remote_now(timestamped_message.channel_" |
| 1408 | "index) (" |
| 1409 | << timestamped_message.monotonic_remote_time << " vs. " |
| 1410 | << state->monotonic_remote_now( |
| 1411 | timestamped_message.channel_index) |
| 1412 | << ") " << state->event_loop()->node()->name()->string_view() |
| 1413 | << " to " |
| 1414 | << state->remote_node(timestamped_message.channel_index) |
| 1415 | ->name() |
| 1416 | ->string_view() |
| 1417 | << " currently " << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1418 | << " (" |
| 1419 | << state->ToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1420 | timestamped_message.monotonic_event_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1421 | << ") remote event time " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1422 | << timestamped_message.monotonic_remote_time << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1423 | << state->RemoteToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1424 | timestamped_message.channel_index, |
| 1425 | timestamped_message.monotonic_remote_time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1426 | << ") " << state->DebugString(); |
| 1427 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1430 | // If we have access to the factory, use it to fix the realtime time. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1431 | state->SetRealtimeOffset(timestamped_message.monotonic_event_time, |
| 1432 | timestamped_message.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1433 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1434 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1435 | << timestamped_message.monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1436 | // TODO(austin): std::move channel_data in and make that efficient in |
| 1437 | // simulation. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1438 | state->Send(std::move(timestamped_message)); |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1439 | } else if (!ignore_missing_data_ && |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 1440 | // When starting up, we can have data which was sent before the |
| 1441 | // log starts, but the timestamp was after the log starts. This |
| 1442 | // is unreasonable to avoid, so ignore the missing data. |
| 1443 | timestamped_message.monotonic_remote_time >= |
| 1444 | state->monotonic_remote_start_time( |
| 1445 | timestamped_message.channel_index) && |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1446 | !FLAGS_skip_missing_forwarding_entries) { |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 1447 | // We've found a timestamp without data that we expect to have data for. |
| 1448 | // This likely means that we are at the end of the log file. Record it |
| 1449 | // and CHECK that in the rest of the log file, we don't find any more |
| 1450 | // data on that channel. Not all channels will end at the same point in |
| 1451 | // time since they can be in different files. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1452 | VLOG(1) << "Found the last message on channel " |
| 1453 | << timestamped_message.channel_index; |
| 1454 | |
| 1455 | // Vector storing if we've seen a nullptr message or not per channel. |
| 1456 | std::vector<bool> last_message; |
| 1457 | last_message.resize(logged_configuration()->channels()->size(), false); |
| 1458 | |
| 1459 | last_message[timestamped_message.channel_index] = true; |
| 1460 | |
| 1461 | // Now that we found the end of one channel, artificially stop the |
| 1462 | // rest. It is confusing when part of your data gets replayed but not |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 1463 | // all. Read the rest of the messages and drop them on the floor while |
| 1464 | // doing some basic validation. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1465 | while (state->OldestMessageTime() != monotonic_clock::max_time) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1466 | TimestampedMessage next = state->PopOldest(); |
| 1467 | // Make sure that once we have seen the last message on a channel, |
| 1468 | // data doesn't start back up again. If the user wants to play |
| 1469 | // through events like this, they can set |
| 1470 | // --skip_missing_forwarding_entries or ignore_missing_data_. |
| 1471 | CHECK_LT(next.channel_index, last_message.size()); |
| 1472 | if (next.data.span().size() == 0u) { |
| 1473 | last_message[next.channel_index] = true; |
| 1474 | } else { |
| 1475 | if (last_message[next.channel_index]) { |
| 1476 | LOG(FATAL) |
| 1477 | << "Found missing data in the middle of the log file on " |
| 1478 | "channel " |
| 1479 | << next.channel_index << " Last " |
| 1480 | << last_message[next.channel_index] << state->DebugString(); |
| 1481 | } |
| 1482 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1483 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1484 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1485 | } else { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1486 | LOG(WARNING) |
| 1487 | << "Not sending data from before the start of the log file. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1488 | << timestamped_message.monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1489 | << " start " << monotonic_start_time().time_since_epoch().count() |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 1490 | << " " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1491 | << FlatbufferToJson(timestamped_message.data, |
Austin Schuh | d85baf8 | 2020-10-19 11:50:12 -0700 | [diff] [blame] | 1492 | {.multi_line = false, .max_vector_size = 100}); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1495 | const monotonic_clock::time_point next_time = state->OldestMessageTime(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1496 | if (next_time != monotonic_clock::max_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1497 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
| 1498 | << "wakeup for " << next_time << "(" |
| 1499 | << state->ToDistributedClock(next_time) |
| 1500 | << " distributed), now is " << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1501 | state->Setup(next_time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 1502 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1503 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 1504 | << "No next message, scheduling shutdown"; |
| 1505 | // Set a timer up immediately after now to die. If we don't do this, |
| 1506 | // then the senders waiting on the message we just read will never get |
| 1507 | // called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1508 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1509 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 1510 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 1511 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1512 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1513 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1514 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 1515 | << state->event_loop()->context().monotonic_event_time << " now " |
| 1516 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1517 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1518 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1519 | ++live_nodes_; |
| 1520 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1521 | if (state->OldestMessageTime() != monotonic_clock::max_time) { |
| 1522 | event_loop->OnRun([state]() { state->Setup(state->OldestMessageTime()); }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1527 | // Make sure that things get destroyed in the correct order, rather than |
| 1528 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1529 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1530 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1531 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 1532 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 1533 | event_loop_factory_unique_ptr_.reset(); |
| 1534 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1537 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1538 | std::string_view add_prefix, |
| 1539 | std::string_view new_type) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1540 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 1541 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 1542 | if (channel->name()->str() == name && |
| 1543 | channel->type()->string_view() == type) { |
| 1544 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 1545 | << "Already remapped channel " |
| 1546 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1547 | RemappedChannel remapped_channel; |
| 1548 | remapped_channel.remapped_name = |
| 1549 | std::string(add_prefix) + std::string(name); |
| 1550 | remapped_channel.new_type = new_type; |
| 1551 | remapped_channels_[ii] = std::move(remapped_channel); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1552 | VLOG(1) << "Remapping channel " |
| 1553 | << configuration::CleanedChannelToString(channel) |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1554 | << " to have name " << remapped_channels_[ii].remapped_name; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 1555 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1556 | return; |
| 1557 | } |
| 1558 | } |
| 1559 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 1560 | << type; |
| 1561 | } |
| 1562 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1563 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 1564 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1565 | std::string_view add_prefix, |
| 1566 | std::string_view new_type) { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1567 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 1568 | const Channel *remapped_channel = |
| 1569 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 1570 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 1571 | << "\", \"type\": \"" << type << "\"}"; |
| 1572 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 1573 | << "\"}"; |
| 1574 | VLOG(1) << "Remapped " |
| 1575 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 1576 | |
| 1577 | // We want to make /spray on node 0 go to /0/spray by snooping the maps. And |
| 1578 | // we want it to degrade if the heuristics fail to just work. |
| 1579 | // |
| 1580 | // The easiest way to do this is going to be incredibly specific and verbose. |
| 1581 | // Look up /spray, to /0/spray. Then, prefix the result with /original to get |
| 1582 | // /original/0/spray. Then, create a map from /original/spray to |
| 1583 | // /original/0/spray for just the type we were asked for. |
| 1584 | if (name != remapped_channel->name()->string_view()) { |
| 1585 | MapT new_map; |
| 1586 | new_map.match = std::make_unique<ChannelT>(); |
| 1587 | new_map.match->name = absl::StrCat(add_prefix, name); |
| 1588 | new_map.match->type = type; |
| 1589 | if (node != nullptr) { |
| 1590 | new_map.match->source_node = node->name()->str(); |
| 1591 | } |
| 1592 | new_map.rename = std::make_unique<ChannelT>(); |
| 1593 | new_map.rename->name = |
| 1594 | absl::StrCat(add_prefix, remapped_channel->name()->string_view()); |
| 1595 | maps_.emplace_back(std::move(new_map)); |
| 1596 | } |
| 1597 | |
| 1598 | const size_t channel_index = |
| 1599 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 1600 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 1601 | << "Already remapped channel " |
| 1602 | << configuration::CleanedChannelToString(remapped_channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1603 | |
| 1604 | RemappedChannel remapped_channel_struct; |
| 1605 | remapped_channel_struct.remapped_name = |
| 1606 | std::string(add_prefix) + |
| 1607 | std::string(remapped_channel->name()->string_view()); |
| 1608 | remapped_channel_struct.new_type = new_type; |
| 1609 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1610 | MakeRemappedConfig(); |
| 1611 | } |
| 1612 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1613 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1614 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1615 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1616 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1617 | << ": Can't change the mapping after the events are scheduled."; |
| 1618 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1619 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1620 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1621 | // If no remapping occurred and we are using the original config, then there |
| 1622 | // is nothing interesting to do here. |
| 1623 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1624 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1625 | return; |
| 1626 | } |
| 1627 | // Config to copy Channel definitions from. Use the specified |
| 1628 | // replay_configuration_ if it has been provided. |
| 1629 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 1630 | ? logged_configuration() |
| 1631 | : replay_configuration_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1632 | |
| 1633 | // Create a config with all the channels, but un-sorted/merged. Collect up |
| 1634 | // the schemas while we do this. Call MergeConfiguration to sort everything, |
| 1635 | // and then merge it all in together. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1636 | |
| 1637 | // This is the builder that we use for the config containing all the new |
| 1638 | // channels. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1639 | flatbuffers::FlatBufferBuilder fbb; |
| 1640 | fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1641 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1642 | |
| 1643 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 1644 | << ": Merging logic needs to be updated when the number of channel " |
| 1645 | "fields changes."; |
| 1646 | |
| 1647 | // List of schemas. |
| 1648 | std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map; |
| 1649 | // Make sure our new RemoteMessage schema is in there for old logs without it. |
| 1650 | schema_map.insert(std::make_pair( |
| 1651 | RemoteMessage::GetFullyQualifiedName(), |
| 1652 | FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>( |
| 1653 | message_bridge::RemoteMessageSchema())))); |
| 1654 | |
| 1655 | // Reconstruct the remapped channels. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1656 | for (auto &pair : remapped_channels_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1657 | const Channel *const c = CHECK_NOTNULL(configuration::GetChannel( |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1658 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 1659 | nullptr)); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1660 | channel_offsets.emplace_back( |
| 1661 | CopyChannel(c, pair.second.remapped_name, "", &fbb)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1662 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1663 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1664 | // Now reconstruct the original channels, translating types as needed |
| 1665 | for (const Channel *c : *base_config->channels()) { |
| 1666 | // Search for a mapping channel. |
| 1667 | std::string_view new_type = ""; |
| 1668 | for (auto &pair : remapped_channels_) { |
| 1669 | const Channel *const remapped_channel = |
| 1670 | logged_configuration()->channels()->Get(pair.first); |
| 1671 | if (remapped_channel->name()->string_view() == c->name()->string_view() && |
| 1672 | remapped_channel->type()->string_view() == c->type()->string_view()) { |
| 1673 | new_type = pair.second.new_type; |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | // Copy everything over. |
| 1679 | channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb)); |
| 1680 | |
| 1681 | // Add the schema if it doesn't exist. |
| 1682 | if (schema_map.find(c->type()->string_view()) == schema_map.end()) { |
| 1683 | CHECK(c->has_schema()); |
| 1684 | schema_map.insert(std::make_pair(c->type()->string_view(), |
| 1685 | RecursiveCopyFlatBuffer(c->schema()))); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | // The MergeConfiguration API takes a vector, not a map. Convert. |
| 1690 | std::vector<FlatbufferVector<reflection::Schema>> schemas; |
| 1691 | while (!schema_map.empty()) { |
| 1692 | schemas.emplace_back(std::move(schema_map.begin()->second)); |
| 1693 | schema_map.erase(schema_map.begin()); |
| 1694 | } |
| 1695 | |
| 1696 | // Create the Configuration containing the new channels that we want to add. |
| 1697 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1698 | channels_offset = |
| 1699 | channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets); |
| 1700 | |
| 1701 | // Copy over the old maps. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1702 | std::vector<flatbuffers::Offset<Map>> map_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1703 | if (base_config->maps()) { |
| 1704 | for (const Map *map : *base_config->maps()) { |
| 1705 | map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb)); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | // Now create the new maps. These are second so they take effect first. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1710 | for (const MapT &map : maps_) { |
| 1711 | const flatbuffers::Offset<flatbuffers::String> match_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1712 | fbb.CreateString(map.match->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1713 | const flatbuffers::Offset<flatbuffers::String> match_type_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1714 | fbb.CreateString(map.match->type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1715 | const flatbuffers::Offset<flatbuffers::String> rename_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1716 | fbb.CreateString(map.rename->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1717 | flatbuffers::Offset<flatbuffers::String> match_source_node_offset; |
| 1718 | if (!map.match->source_node.empty()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1719 | match_source_node_offset = fbb.CreateString(map.match->source_node); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1720 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1721 | Channel::Builder match_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1722 | match_builder.add_name(match_name_offset); |
| 1723 | match_builder.add_type(match_type_offset); |
| 1724 | if (!map.match->source_node.empty()) { |
| 1725 | match_builder.add_source_node(match_source_node_offset); |
| 1726 | } |
| 1727 | const flatbuffers::Offset<Channel> match_offset = match_builder.Finish(); |
| 1728 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1729 | Channel::Builder rename_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1730 | rename_builder.add_name(rename_name_offset); |
| 1731 | const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish(); |
| 1732 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1733 | Map::Builder map_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1734 | map_builder.add_match(match_offset); |
| 1735 | map_builder.add_rename(rename_offset); |
| 1736 | map_offsets.emplace_back(map_builder.Finish()); |
| 1737 | } |
| 1738 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1739 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 1740 | maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1741 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1742 | // And copy everything else over. |
| 1743 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 1744 | nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb); |
| 1745 | |
| 1746 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1747 | applications_offset = |
| 1748 | aos::RecursiveCopyVectorTable(base_config->applications(), &fbb); |
| 1749 | |
| 1750 | // Now insert everything else in unmodified. |
| 1751 | ConfigurationBuilder configuration_builder(fbb); |
| 1752 | if (!channels_offset.IsNull()) { |
| 1753 | configuration_builder.add_channels(channels_offset); |
| 1754 | } |
| 1755 | if (!maps_offsets.IsNull()) { |
| 1756 | configuration_builder.add_maps(maps_offsets); |
| 1757 | } |
| 1758 | if (!nodes_offset.IsNull()) { |
| 1759 | configuration_builder.add_nodes(nodes_offset); |
| 1760 | } |
| 1761 | if (!applications_offset.IsNull()) { |
| 1762 | configuration_builder.add_applications(applications_offset); |
| 1763 | } |
| 1764 | |
| 1765 | if (base_config->has_channel_storage_duration()) { |
| 1766 | configuration_builder.add_channel_storage_duration( |
| 1767 | base_config->channel_storage_duration()); |
| 1768 | } |
| 1769 | |
| 1770 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 1771 | << ": Merging logic needs to be updated when the number of configuration " |
| 1772 | "fields changes."; |
| 1773 | |
| 1774 | fbb.Finish(configuration_builder.Finish()); |
| 1775 | |
| 1776 | // Clean it up and return it! By using MergeConfiguration here, we'll |
| 1777 | // actually get a deduplicated config for free too. |
| 1778 | FlatbufferDetachedBuffer<Configuration> new_merged_config = |
| 1779 | configuration::MergeConfiguration( |
| 1780 | FlatbufferDetachedBuffer<Configuration>(fbb.Release())); |
| 1781 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1782 | remapped_configuration_buffer_ = |
| 1783 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1784 | configuration::MergeConfiguration(new_merged_config, schemas)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1785 | |
| 1786 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1787 | |
| 1788 | // TODO(austin): Lazily re-build to save CPU? |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1791 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
| 1792 | const Channel *channel) { |
| 1793 | std::string_view channel_name = channel->name()->string_view(); |
| 1794 | std::string_view channel_type = channel->type()->string_view(); |
| 1795 | const int channel_index = |
| 1796 | configuration::ChannelIndex(logged_configuration(), channel); |
| 1797 | // If the channel is remapped, find the correct channel name to use. |
| 1798 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1799 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1800 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1801 | channel_name = remapped_channels_[channel_index].remapped_name; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1804 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1805 | const Channel *remapped_channel = configuration::GetChannel( |
| 1806 | event_loop->configuration(), channel_name, channel_type, |
| 1807 | event_loop->name(), event_loop->node()); |
| 1808 | |
| 1809 | CHECK(remapped_channel != nullptr) |
| 1810 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 1811 | << channel_type << "\"} because it is not in the provided configuration."; |
| 1812 | |
| 1813 | return remapped_channel; |
| 1814 | } |
| 1815 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1816 | LogReader::State::State(std::unique_ptr<TimestampMapper> timestamp_mapper) |
| 1817 | : timestamp_mapper_(std::move(timestamp_mapper)) {} |
| 1818 | |
| 1819 | void LogReader::State::AddPeer(State *peer) { |
| 1820 | if (timestamp_mapper_ && peer->timestamp_mapper_) { |
| 1821 | timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get()); |
| 1822 | } |
| 1823 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1824 | |
| 1825 | EventLoop *LogReader::State::SetNodeEventLoopFactory( |
| 1826 | NodeEventLoopFactory *node_event_loop_factory) { |
| 1827 | node_event_loop_factory_ = node_event_loop_factory; |
| 1828 | event_loop_unique_ptr_ = |
| 1829 | node_event_loop_factory_->MakeEventLoop("log_reader"); |
| 1830 | return event_loop_unique_ptr_.get(); |
| 1831 | } |
| 1832 | |
| 1833 | void LogReader::State::SetChannelCount(size_t count) { |
| 1834 | channels_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1835 | remote_timestamp_senders_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1836 | filters_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1837 | channel_source_state_.resize(count); |
| 1838 | factory_channel_index_.resize(count); |
| 1839 | queue_index_map_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | void LogReader::State::SetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1843 | size_t logged_channel_index, size_t factory_channel_index, |
| 1844 | std::unique_ptr<RawSender> sender, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1845 | message_bridge::NoncausalOffsetEstimator *filter, |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1846 | RemoteMessageSender *remote_timestamp_sender, State *source_state) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1847 | channels_[logged_channel_index] = std::move(sender); |
| 1848 | filters_[logged_channel_index] = filter; |
| 1849 | remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender; |
| 1850 | |
| 1851 | if (source_state) { |
| 1852 | channel_source_state_[logged_channel_index] = source_state; |
| 1853 | |
| 1854 | if (remote_timestamp_sender != nullptr) { |
| 1855 | source_state->queue_index_map_[logged_channel_index] = |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1856 | std::make_unique<std::vector<State::ContiguousSentTimestamp>>(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | factory_channel_index_[logged_channel_index] = factory_channel_index; |
| 1861 | } |
| 1862 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1863 | bool LogReader::State::Send(const TimestampedMessage ×tamped_message) { |
| 1864 | aos::RawSender *sender = channels_[timestamped_message.channel_index].get(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1865 | uint32_t remote_queue_index = 0xffffffff; |
| 1866 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1867 | if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1868 | std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1869 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]) |
| 1870 | ->queue_index_map_[timestamped_message.channel_index] |
| 1871 | .get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1872 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1873 | struct SentTimestamp { |
| 1874 | monotonic_clock::time_point monotonic_event_time; |
| 1875 | uint32_t queue_index; |
| 1876 | } search; |
| 1877 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1878 | search.monotonic_event_time = timestamped_message.monotonic_remote_time; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1879 | search.queue_index = timestamped_message.remote_queue_index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1880 | |
| 1881 | // Find the sent time if available. |
| 1882 | auto element = std::lower_bound( |
| 1883 | queue_index_map->begin(), queue_index_map->end(), search, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1884 | [](ContiguousSentTimestamp a, SentTimestamp b) { |
| 1885 | if (a.ending_monotonic_event_time < b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1886 | return true; |
| 1887 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1888 | if (a.starting_monotonic_event_time > b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1889 | return false; |
| 1890 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1891 | |
| 1892 | if (a.ending_queue_index < b.queue_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1893 | return true; |
| 1894 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1895 | if (a.starting_queue_index >= b.queue_index) { |
| 1896 | return false; |
| 1897 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1898 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1899 | // If it isn't clearly below or above, it is below. Since we return |
| 1900 | // the last element <, this will return a match. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1901 | return false; |
| 1902 | }); |
| 1903 | |
| 1904 | // TODO(austin): Be a bit more principled here, but we will want to do that |
| 1905 | // after the logger rewrite. We hit this when one node finishes, but the |
| 1906 | // other node isn't done yet. So there is no send time, but there is a |
| 1907 | // receive time. |
| 1908 | if (element != queue_index_map->end()) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1909 | CHECK_GE(timestamped_message.monotonic_remote_time, |
| 1910 | element->starting_monotonic_event_time); |
| 1911 | CHECK_LE(timestamped_message.monotonic_remote_time, |
| 1912 | element->ending_monotonic_event_time); |
| 1913 | CHECK_GE(timestamped_message.remote_queue_index, |
| 1914 | element->starting_queue_index); |
| 1915 | CHECK_LE(timestamped_message.remote_queue_index, |
| 1916 | element->ending_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1917 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1918 | remote_queue_index = timestamped_message.remote_queue_index + |
| 1919 | element->actual_queue_index - |
| 1920 | element->starting_queue_index; |
| 1921 | } else { |
| 1922 | VLOG(1) << "No timestamp match in the map."; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | // Send! Use the replayed queue index here instead of the logged queue index |
| 1927 | // for the remote queue index. This makes re-logging work. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1928 | const bool sent = sender->Send( |
| 1929 | timestamped_message.data.message().data()->Data(), |
| 1930 | timestamped_message.data.message().data()->size(), |
| 1931 | timestamped_message.monotonic_remote_time, |
| 1932 | timestamped_message.realtime_remote_time, remote_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1933 | if (!sent) return false; |
| 1934 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1935 | if (queue_index_map_[timestamped_message.channel_index]) { |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1936 | if (queue_index_map_[timestamped_message.channel_index]->empty()) { |
| 1937 | // Nothing here, start a range with 0 length. |
| 1938 | ContiguousSentTimestamp timestamp; |
| 1939 | timestamp.starting_monotonic_event_time = |
| 1940 | timestamp.ending_monotonic_event_time = |
| 1941 | timestamped_message.monotonic_event_time; |
| 1942 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
| 1943 | timestamped_message.queue_index; |
| 1944 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1945 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1946 | timestamp); |
| 1947 | } else { |
| 1948 | // We've got something. See if the next timestamp is still contiguous. If |
| 1949 | // so, grow it. |
| 1950 | ContiguousSentTimestamp *back = |
| 1951 | &queue_index_map_[timestamped_message.channel_index]->back(); |
| 1952 | if ((back->starting_queue_index - back->actual_queue_index) == |
| 1953 | (timestamped_message.queue_index - sender->sent_queue_index())) { |
| 1954 | back->ending_queue_index = timestamped_message.queue_index; |
| 1955 | back->ending_monotonic_event_time = |
| 1956 | timestamped_message.monotonic_event_time; |
| 1957 | } else { |
| 1958 | // Otherwise, make a new one. |
| 1959 | ContiguousSentTimestamp timestamp; |
| 1960 | timestamp.starting_monotonic_event_time = |
| 1961 | timestamp.ending_monotonic_event_time = |
| 1962 | timestamped_message.monotonic_event_time; |
| 1963 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
| 1964 | timestamped_message.queue_index; |
| 1965 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1966 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1967 | timestamp); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | // TODO(austin): Should we prune the map? On a many day log, I only saw the |
| 1972 | // queue index diverge a couple of elements, which would be a very small |
| 1973 | // map. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1974 | } else if (remote_timestamp_senders_[timestamped_message.channel_index] != |
| 1975 | nullptr) { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1976 | flatbuffers::FlatBufferBuilder fbb; |
| 1977 | fbb.ForceDefaults(true); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1978 | flatbuffers::Offset<flatbuffers::String> boot_uuid_offset = |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1979 | fbb.CreateString(event_loop_->boot_uuid().string_view()); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1980 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1981 | RemoteMessage::Builder message_header_builder(fbb); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1982 | |
| 1983 | message_header_builder.add_channel_index( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1984 | factory_channel_index_[timestamped_message.channel_index]); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1985 | |
| 1986 | // Swap the remote and sent metrics. They are from the sender's |
| 1987 | // perspective, not the receiver's perspective. |
| 1988 | message_header_builder.add_monotonic_sent_time( |
| 1989 | sender->monotonic_sent_time().time_since_epoch().count()); |
| 1990 | message_header_builder.add_realtime_sent_time( |
| 1991 | sender->realtime_sent_time().time_since_epoch().count()); |
| 1992 | message_header_builder.add_queue_index(sender->sent_queue_index()); |
| 1993 | |
| 1994 | message_header_builder.add_monotonic_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1995 | timestamped_message.monotonic_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1996 | message_header_builder.add_realtime_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1997 | timestamped_message.realtime_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1998 | |
| 1999 | message_header_builder.add_remote_queue_index(remote_queue_index); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 2000 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2001 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2002 | fbb.Finish(message_header_builder.Finish()); |
| 2003 | |
| 2004 | remote_timestamp_senders_[timestamped_message.channel_index]->Send( |
| 2005 | FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()), |
| 2006 | timestamped_message.monotonic_timestamp_time); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2012 | LogReader::RemoteMessageSender::RemoteMessageSender( |
| 2013 | aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop) |
| 2014 | : event_loop_(event_loop), |
| 2015 | sender_(std::move(sender)), |
| 2016 | timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {} |
| 2017 | |
| 2018 | void LogReader::RemoteMessageSender::ScheduleTimestamp() { |
| 2019 | if (remote_timestamps_.empty()) { |
| 2020 | CHECK_NOTNULL(timer_); |
| 2021 | timer_->Disable(); |
| 2022 | scheduled_time_ = monotonic_clock::min_time; |
| 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) { |
| 2027 | CHECK_NOTNULL(timer_); |
Austin Schuh | 816e5d6 | 2021-01-05 23:42:20 -0800 | [diff] [blame] | 2028 | timer_->Setup(remote_timestamps_.front().monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2029 | scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time; |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame^] | 2030 | CHECK_GE(scheduled_time_, event_loop_->monotonic_now()) |
| 2031 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | void LogReader::RemoteMessageSender::Send( |
| 2036 | FlatbufferDetachedBuffer<RemoteMessage> remote_message, |
| 2037 | monotonic_clock::time_point monotonic_timestamp_time) { |
| 2038 | // There are 2 cases. Either we have a monotonic_timestamp_time and need to |
| 2039 | // resend the timestamp at the correct time, or we don't and can send it |
| 2040 | // immediately. |
| 2041 | if (monotonic_timestamp_time == monotonic_clock::min_time) { |
| 2042 | CHECK(remote_timestamps_.empty()) |
| 2043 | << ": Unsupported mix of timestamps and no timestamps."; |
| 2044 | sender_.Send(std::move(remote_message)); |
| 2045 | } else { |
Austin Schuh | b22ae42 | 2021-01-31 17:57:06 -0800 | [diff] [blame] | 2046 | remote_timestamps_.emplace( |
| 2047 | std::upper_bound( |
| 2048 | remote_timestamps_.begin(), remote_timestamps_.end(), |
| 2049 | monotonic_timestamp_time, |
| 2050 | [](const aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 2051 | const Timestamp ×tamp) { |
| 2052 | return monotonic_timestamp_time < |
| 2053 | timestamp.monotonic_timestamp_time; |
| 2054 | }), |
| 2055 | std::move(remote_message), monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2056 | ScheduleTimestamp(); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | void LogReader::RemoteMessageSender::SendTimestamp() { |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame^] | 2061 | CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_) |
| 2062 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2063 | CHECK(!remote_timestamps_.empty()); |
| 2064 | |
| 2065 | // Send out all timestamps at the currently scheduled time. |
| 2066 | while (remote_timestamps_.front().monotonic_timestamp_time == |
| 2067 | scheduled_time_) { |
| 2068 | sender_.Send(std::move(remote_timestamps_.front().remote_message)); |
| 2069 | remote_timestamps_.pop_front(); |
| 2070 | if (remote_timestamps_.empty()) { |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | scheduled_time_ = monotonic_clock::min_time; |
| 2075 | |
| 2076 | ScheduleTimestamp(); |
| 2077 | } |
| 2078 | |
| 2079 | LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2080 | const Node *delivered_node) { |
| 2081 | auto sender = remote_timestamp_senders_map_.find(delivered_node); |
| 2082 | |
| 2083 | if (sender == remote_timestamp_senders_map_.end()) { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2084 | sender = |
| 2085 | remote_timestamp_senders_map_ |
| 2086 | .emplace(delivered_node, |
| 2087 | std::make_unique<RemoteMessageSender>( |
| 2088 | event_loop()->MakeSender<RemoteMessage>(absl::StrCat( |
| 2089 | "/aos/remote_timestamps/", |
| 2090 | delivered_node->name()->string_view())), |
| 2091 | event_loop())) |
| 2092 | .first; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 2095 | return sender->second.get(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 2098 | TimestampedMessage LogReader::State::PopOldest() { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2099 | CHECK(timestamp_mapper_ != nullptr); |
| 2100 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 2101 | CHECK(result_ptr != nullptr); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2102 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2103 | TimestampedMessage result = std::move(*result_ptr); |
| 2104 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2105 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2106 | << result.monotonic_event_time; |
| 2107 | timestamp_mapper_->PopFront(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2108 | SeedSortedMessages(); |
| 2109 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2110 | if (result.monotonic_remote_time != monotonic_clock::min_time) { |
| 2111 | message_bridge::NoncausalOffsetEstimator *filter = |
| 2112 | filters_[result.channel_index]; |
| 2113 | CHECK(filter != nullptr); |
| 2114 | |
| 2115 | // TODO(austin): We probably want to push this down into the timestamp |
| 2116 | // mapper directly. |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame^] | 2117 | filter->Pop(event_loop_->node(), event_loop_->monotonic_now()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 2118 | } |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 2119 | VLOG(1) << "Popped " << result |
| 2120 | << configuration::CleanedChannelToString( |
| 2121 | event_loop_->configuration()->channels()->Get( |
| 2122 | factory_channel_index_[result.channel_index])); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2123 | return result; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | monotonic_clock::time_point LogReader::State::OldestMessageTime() const { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2127 | if (timestamp_mapper_ == nullptr) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2128 | return monotonic_clock::max_time; |
| 2129 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2130 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 2131 | if (result_ptr == nullptr) { |
| 2132 | return monotonic_clock::max_time; |
| 2133 | } |
| 2134 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "oldest message at " |
| 2135 | << result_ptr->monotonic_event_time; |
| 2136 | return result_ptr->monotonic_event_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | void LogReader::State::SeedSortedMessages() { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 2140 | if (!timestamp_mapper_) return; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2141 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 2142 | timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>( |
| 2143 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | void LogReader::State::Deregister() { |
| 2147 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 2148 | channels_[i].reset(); |
| 2149 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 2150 | remote_timestamp_senders_map_.clear(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 2151 | event_loop_unique_ptr_.reset(); |
| 2152 | event_loop_ = nullptr; |
| 2153 | timer_handler_ = nullptr; |
| 2154 | node_event_loop_factory_ = nullptr; |
| 2155 | } |
| 2156 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2157 | } // namespace logger |
| 2158 | } // namespace aos |