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 | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 10 | #include "absl/types/span.h" |
| 11 | #include "aos/events/event_loop.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 13 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 14 | #include "aos/network/team_number.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 15 | #include "aos/time/time.h" |
| 16 | #include "flatbuffers/flatbuffers.h" |
| 17 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 18 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 19 | "If true, drop any forwarding entries with missing data. If " |
| 20 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | |
| 22 | namespace aos { |
| 23 | namespace logger { |
| 24 | |
| 25 | namespace chrono = std::chrono; |
| 26 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | Logger::Logger(DetachedBufferWriter *writer, EventLoop *event_loop, |
| 28 | std::chrono::milliseconds polling_period) |
| 29 | : event_loop_(event_loop), |
| 30 | writer_(writer), |
| 31 | timer_handler_(event_loop_->AddTimer([this]() { DoLogData(); })), |
| 32 | polling_period_(polling_period) { |
| 33 | for (const Channel *channel : *event_loop_->configuration()->channels()) { |
| 34 | FetcherStruct fs; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 35 | const bool is_readable = |
| 36 | configuration::ChannelIsReadableOnNode(channel, event_loop_->node()); |
| 37 | const bool log_message = configuration::ChannelMessageIsLoggedOnNode( |
| 38 | channel, event_loop_->node()) && |
| 39 | is_readable; |
| 40 | |
| 41 | const bool log_delivery_times = |
| 42 | (event_loop_->node() == nullptr) |
| 43 | ? false |
| 44 | : configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 45 | channel, event_loop_->node(), event_loop_->node()); |
| 46 | |
| 47 | if (log_message || log_delivery_times) { |
| 48 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 49 | VLOG(1) << "Logging channel " |
| 50 | << configuration::CleanedChannelToString(channel); |
| 51 | |
| 52 | if (log_delivery_times) { |
| 53 | if (log_message) { |
| 54 | VLOG(1) << " Logging message and delivery times"; |
| 55 | fs.log_type = LogType::kLogMessageAndDeliveryTime; |
| 56 | } else { |
| 57 | VLOG(1) << " Logging delivery times only"; |
| 58 | fs.log_type = LogType::kLogDeliveryTimeOnly; |
| 59 | } |
| 60 | } else { |
| 61 | // We don't have a particularly great use case right now for logging a |
| 62 | // forwarded message, but either not logging the delivery times, or |
| 63 | // logging them on another node. Fail rather than produce bad results. |
| 64 | CHECK(configuration::ChannelIsSendableOnNode(channel, |
| 65 | event_loop_->node())) |
| 66 | << ": Logger only knows how to log remote messages with " |
| 67 | "forwarding timestamps."; |
| 68 | VLOG(1) << " Logging message only"; |
| 69 | fs.log_type = LogType::kLogMessage; |
| 70 | } |
| 71 | } |
| 72 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 73 | fs.written = false; |
| 74 | fetchers_.emplace_back(std::move(fs)); |
| 75 | } |
| 76 | |
| 77 | // When things start, we want to log the header, then the most recent messages |
| 78 | // available on each fetcher to capture the previous state, then start |
| 79 | // polling. |
| 80 | event_loop_->OnRun([this, polling_period]() { |
| 81 | // Grab data from each channel right before we declare the log file started |
| 82 | // so we can capture the latest message on each channel. This lets us have |
| 83 | // non periodic messages with configuration that now get logged. |
| 84 | for (FetcherStruct &f : fetchers_) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 85 | if (f.fetcher.get() != nullptr) { |
| 86 | f.written = !f.fetcher->Fetch(); |
| 87 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // We need to pick a point in time to declare the log file "started". This |
| 91 | // starts here. It needs to be after everything is fetched so that the |
| 92 | // fetchers are all pointed at the most recent message before the start |
| 93 | // time. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 94 | monotonic_start_time_ = event_loop_->monotonic_now(); |
| 95 | realtime_start_time_ = event_loop_->realtime_now(); |
| 96 | last_synchronized_time_ = monotonic_start_time_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 97 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 98 | LOG(INFO) << "Logging node as " << FlatbufferToJson(event_loop_->node()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 99 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 100 | WriteHeader(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 101 | |
| 102 | timer_handler_->Setup(event_loop_->monotonic_now() + polling_period, |
| 103 | polling_period); |
| 104 | }); |
| 105 | } |
| 106 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 107 | void Logger::WriteHeader() { |
| 108 | // Now write the header with this timestamp in it. |
| 109 | flatbuffers::FlatBufferBuilder fbb; |
| 110 | fbb.ForceDefaults(1); |
| 111 | |
| 112 | flatbuffers::Offset<aos::Configuration> configuration_offset = |
| 113 | CopyFlatBuffer(event_loop_->configuration(), &fbb); |
| 114 | |
| 115 | flatbuffers::Offset<flatbuffers::String> string_offset = |
| 116 | fbb.CreateString(network::GetHostname()); |
| 117 | |
| 118 | flatbuffers::Offset<Node> node_offset; |
| 119 | if (event_loop_->node() != nullptr) { |
| 120 | node_offset = CopyFlatBuffer(event_loop_->node(), &fbb); |
| 121 | } |
| 122 | |
| 123 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 124 | |
| 125 | log_file_header_builder.add_name(string_offset); |
| 126 | |
| 127 | // Only add the node if we are running in a multinode configuration. |
| 128 | if (event_loop_->node() != nullptr) { |
| 129 | log_file_header_builder.add_node(node_offset); |
| 130 | } |
| 131 | |
| 132 | log_file_header_builder.add_configuration(configuration_offset); |
| 133 | // The worst case theoretical out of order is the polling period times 2. |
| 134 | // One message could get logged right after the boundary, but be for right |
| 135 | // before the next boundary. And the reverse could happen for another |
| 136 | // message. Report back 3x to be extra safe, and because the cost isn't |
| 137 | // huge on the read side. |
| 138 | log_file_header_builder.add_max_out_of_order_duration( |
| 139 | std::chrono::duration_cast<std::chrono::nanoseconds>(3 * polling_period_) |
| 140 | .count()); |
| 141 | |
| 142 | log_file_header_builder.add_monotonic_start_time( |
| 143 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 144 | monotonic_start_time_.time_since_epoch()) |
| 145 | .count()); |
| 146 | log_file_header_builder.add_realtime_start_time( |
| 147 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 148 | realtime_start_time_.time_since_epoch()) |
| 149 | .count()); |
| 150 | |
| 151 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 152 | writer_->QueueSizedFlatbuffer(&fbb); |
| 153 | } |
| 154 | |
| 155 | void Logger::Rotate(DetachedBufferWriter *writer) { |
| 156 | // Force data up until now to be written. |
| 157 | DoLogData(); |
| 158 | |
| 159 | // Swap the writer out, and re-write the header. |
| 160 | writer_ = writer; |
| 161 | WriteHeader(); |
| 162 | } |
| 163 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 164 | void Logger::DoLogData() { |
| 165 | // We want to guarentee that messages aren't out of order by more than |
| 166 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 167 | // cycle should be a sync point. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 168 | const monotonic_clock::time_point monotonic_now = |
| 169 | event_loop_->monotonic_now(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 170 | |
| 171 | do { |
| 172 | // Move the sync point up by at most polling_period. This forces one sync |
| 173 | // per iteration, even if it is small. |
| 174 | last_synchronized_time_ = |
| 175 | std::min(last_synchronized_time_ + polling_period_, monotonic_now); |
| 176 | size_t channel_index = 0; |
| 177 | // Write each channel to disk, one at a time. |
| 178 | for (FetcherStruct &f : fetchers_) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 179 | // Skip any channels which we aren't supposed to log. |
| 180 | if (f.fetcher.get() != nullptr) { |
| 181 | while (true) { |
| 182 | if (f.written) { |
| 183 | if (!f.fetcher->FetchNext()) { |
| 184 | VLOG(2) << "No new data on " |
| 185 | << configuration::CleanedChannelToString( |
| 186 | f.fetcher->channel()); |
| 187 | break; |
| 188 | } else { |
| 189 | f.written = false; |
| 190 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 191 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 192 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 193 | CHECK(!f.written); |
| 194 | |
| 195 | // TODO(james): Write tests to exercise this logic. |
| 196 | if (f.fetcher->context().monotonic_event_time < |
| 197 | last_synchronized_time_) { |
| 198 | // Write! |
| 199 | flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size + |
| 200 | max_header_size_); |
| 201 | fbb.ForceDefaults(1); |
| 202 | |
| 203 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 204 | channel_index, f.log_type)); |
| 205 | |
| 206 | VLOG(2) << "Writing data for channel " |
| 207 | << configuration::CleanedChannelToString( |
| 208 | f.fetcher->channel()); |
| 209 | |
| 210 | max_header_size_ = std::max( |
| 211 | max_header_size_, fbb.GetSize() - f.fetcher->context().size); |
| 212 | writer_->QueueSizedFlatbuffer(&fbb); |
| 213 | |
| 214 | f.written = true; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 215 | } else { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 216 | break; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | ++channel_index; |
| 222 | } |
| 223 | |
| 224 | CHECK_EQ(channel_index, fetchers_.size()); |
| 225 | |
| 226 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 227 | // caught up. |
| 228 | } while (last_synchronized_time_ + polling_period_ < monotonic_now); |
| 229 | |
| 230 | writer_->Flush(); |
| 231 | } |
| 232 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 233 | LogReader::LogReader(std::string_view filename, |
| 234 | const Configuration *replay_configuration) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 235 | : LogReader(std::vector<std::string>{std::string(filename)}, |
| 236 | replay_configuration) {} |
| 237 | |
| 238 | LogReader::LogReader(const std::vector<std::string> &filenames, |
| 239 | const Configuration *replay_configuration) |
| 240 | : sorted_message_reader_(filenames), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 241 | replay_configuration_(replay_configuration) { |
| 242 | channels_.resize(logged_configuration()->channels()->size()); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 243 | MakeRemappedConfig(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 246 | LogReader::~LogReader() { Deregister(); } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 247 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 248 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 249 | return sorted_message_reader_.configuration(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 250 | } |
| 251 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 252 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 253 | return remapped_configuration_; |
| 254 | } |
| 255 | |
| 256 | const Node *LogReader::node() const { |
| 257 | // Because the Node pointer will only be valid if it actually points to memory |
| 258 | // owned by remapped_configuration_, we need to wait for the |
| 259 | // remapped_configuration_ to be populated before accessing it. |
| 260 | CHECK(remapped_configuration_ != nullptr) |
| 261 | << ": Need to call Register before the node() pointer will be valid."; |
| 262 | if (sorted_message_reader_.node() == nullptr) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | return configuration::GetNode( |
| 266 | configuration(), sorted_message_reader_.node()->name()->string_view()); |
| 267 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 268 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 269 | monotonic_clock::time_point LogReader::monotonic_start_time() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 270 | return sorted_message_reader_.monotonic_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | realtime_clock::time_point LogReader::realtime_start_time() { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 274 | return sorted_message_reader_.realtime_start_time(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 275 | } |
| 276 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 277 | void LogReader::Register() { |
| 278 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 279 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 280 | Register(event_loop_factory_unique_ptr_.get()); |
| 281 | } |
| 282 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 283 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 284 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 285 | node_event_loop_factory_ = |
| 286 | event_loop_factory_->GetNodeEventLoopFactory(node()); |
| 287 | event_loop_unique_ptr_ = |
| 288 | event_loop_factory->MakeEventLoop("log_reader", node()); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 289 | // We don't run timing reports when trying to print out logged data, because |
| 290 | // otherwise we would end up printing out the timing reports themselves... |
| 291 | // This is only really relevant when we are replaying into a simulation. |
| 292 | event_loop_unique_ptr_->SkipTimingReport(); |
| 293 | |
| 294 | Register(event_loop_unique_ptr_.get()); |
| 295 | event_loop_factory_->RunFor(monotonic_start_time() - |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 296 | event_loop_->monotonic_now()); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 299 | void LogReader::Register(EventLoop *event_loop) { |
| 300 | event_loop_ = event_loop; |
| 301 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 302 | // We don't run timing reports when trying to print out logged data, because |
| 303 | // otherwise we would end up printing out the timing reports themselves... |
| 304 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 305 | // Otherwise we replay the timing report and try to resend it... |
| 306 | event_loop_->SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame^] | 307 | event_loop_->SkipAosLog(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 308 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 309 | for (size_t i = 0; i < channels_.size(); ++i) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 310 | const Channel *const original_channel = |
| 311 | logged_configuration()->channels()->Get(i); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 312 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 313 | std::string_view channel_name = original_channel->name()->string_view(); |
| 314 | std::string_view channel_type = original_channel->type()->string_view(); |
| 315 | // If the channel is remapped, find the correct channel name to use. |
| 316 | if (remapped_channels_.count(i) > 0) { |
| 317 | VLOG(2) << "Got remapped channel on " |
| 318 | << configuration::CleanedChannelToString(original_channel); |
| 319 | channel_name = remapped_channels_[i]; |
| 320 | } |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 321 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 322 | VLOG(1) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 323 | const Channel *channel = configuration::GetChannel( |
| 324 | event_loop_->configuration(), channel_name, channel_type, |
| 325 | event_loop_->name(), event_loop_->node()); |
| 326 | |
| 327 | CHECK(channel != nullptr) |
| 328 | << ": Unable to send {\"name\": \"" << channel_name |
| 329 | << "\", \"type\": \"" << channel_type |
| 330 | << "\"} because it is not in the provided configuration."; |
| 331 | |
| 332 | channels_[i] = event_loop_->MakeRawSender(channel); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | timer_handler_ = event_loop_->AddTimer([this]() { |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 336 | if (sorted_message_reader_.active_channel_count() == 0u) { |
| 337 | event_loop_factory_->Exit(); |
| 338 | return; |
| 339 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 340 | monotonic_clock::time_point channel_timestamp; |
| 341 | int channel_index; |
| 342 | FlatbufferVector<MessageHeader> channel_data = |
| 343 | FlatbufferVector<MessageHeader>::Empty(); |
| 344 | |
| 345 | std::tie(channel_timestamp, channel_index, channel_data) = |
| 346 | sorted_message_reader_.PopOldestChannel(); |
| 347 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 348 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 349 | event_loop_->context().monotonic_event_time; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 350 | CHECK(monotonic_now == channel_timestamp) |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 351 | << ": Now " << monotonic_now.time_since_epoch().count() |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 352 | << " trying to send " << channel_timestamp.time_since_epoch().count(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 353 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 354 | if (channel_timestamp > monotonic_start_time() || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 355 | event_loop_factory_ != nullptr) { |
| 356 | if (!FLAGS_skip_missing_forwarding_entries || |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 357 | channel_data.message().data() != nullptr) { |
| 358 | CHECK(channel_data.message().data() != nullptr) |
| 359 | << ": Got a message without data. Forwarding entry which was " |
| 360 | "not " |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 361 | "matched? Use --skip_missing_forwarding_entries to ignore " |
| 362 | "this."; |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 363 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 364 | // If we have access to the factory, use it to fix the realtime time. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 365 | if (node_event_loop_factory_ != nullptr) { |
| 366 | node_event_loop_factory_->SetRealtimeOffset( |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 367 | monotonic_clock::time_point(chrono::nanoseconds( |
| 368 | channel_data.message().monotonic_sent_time())), |
| 369 | realtime_clock::time_point(chrono::nanoseconds( |
| 370 | channel_data.message().realtime_sent_time()))); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 373 | channels_[channel_index]->Send( |
| 374 | channel_data.message().data()->Data(), |
| 375 | channel_data.message().data()->size(), |
| 376 | monotonic_clock::time_point(chrono::nanoseconds( |
| 377 | channel_data.message().monotonic_remote_time())), |
| 378 | realtime_clock::time_point(chrono::nanoseconds( |
| 379 | channel_data.message().realtime_remote_time())), |
| 380 | channel_data.message().remote_queue_index()); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 381 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 382 | } else { |
| 383 | LOG(WARNING) << "Not sending data from before the start of the log file. " |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 384 | << channel_timestamp.time_since_epoch().count() << " start " |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 385 | << monotonic_start_time().time_since_epoch().count() << " " |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 386 | << FlatbufferToJson(channel_data); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 389 | if (sorted_message_reader_.active_channel_count() > 0u) { |
| 390 | timer_handler_->Setup(sorted_message_reader_.oldest_message().first); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 391 | } else { |
| 392 | // Set a timer up immediately after now to die. If we don't do this, then |
| 393 | // the senders waiting on the message we just read will never get called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 394 | if (event_loop_factory_ != nullptr) { |
| 395 | timer_handler_->Setup(monotonic_now + |
| 396 | event_loop_factory_->send_delay() + |
| 397 | std::chrono::nanoseconds(1)); |
| 398 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 399 | } |
| 400 | }); |
| 401 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 402 | if (sorted_message_reader_.active_channel_count() > 0u) { |
| 403 | event_loop_->OnRun([this]() { |
| 404 | timer_handler_->Setup(sorted_message_reader_.oldest_message().first); |
| 405 | }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 410 | // Make sure that things get destroyed in the correct order, rather than |
| 411 | // relying on getting the order correct in the class definition. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 412 | for (size_t i = 0; i < channels_.size(); ++i) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 413 | channels_[i].reset(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 414 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 415 | |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 416 | event_loop_unique_ptr_.reset(); |
| 417 | event_loop_ = nullptr; |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 418 | event_loop_factory_unique_ptr_.reset(); |
| 419 | event_loop_factory_ = nullptr; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 420 | node_event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 421 | } |
| 422 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 423 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 424 | std::string_view add_prefix) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 425 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 426 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 427 | if (channel->name()->str() == name && |
| 428 | channel->type()->string_view() == type) { |
| 429 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 430 | << "Already remapped channel " |
| 431 | << configuration::CleanedChannelToString(channel); |
| 432 | remapped_channels_[ii] = std::string(add_prefix) + std::string(name); |
| 433 | VLOG(1) << "Remapping channel " |
| 434 | << configuration::CleanedChannelToString(channel) |
| 435 | << " to have name " << remapped_channels_[ii]; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 436 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | } |
| 440 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 441 | << type; |
| 442 | } |
| 443 | |
| 444 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 445 | CHECK(!event_loop_) |
| 446 | << ": Can't change the mapping after the events are scheduled."; |
| 447 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 448 | // If no remapping occurred and we are using the original config, then there |
| 449 | // is nothing interesting to do here. |
| 450 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
| 451 | remapped_configuration_ = sorted_message_reader_.configuration(); |
| 452 | return; |
| 453 | } |
| 454 | // Config to copy Channel definitions from. Use the specified |
| 455 | // replay_configuration_ if it has been provided. |
| 456 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 457 | ? logged_configuration() |
| 458 | : replay_configuration_; |
| 459 | // The remapped config will be identical to the base_config, except that it |
| 460 | // will have a bunch of extra channels in the channel list, which are exact |
| 461 | // copies of the remapped channels, but with different names. |
| 462 | // Because the flatbuffers API is a pain to work with, this requires a bit of |
| 463 | // a song-and-dance to get copied over. |
| 464 | // The order of operations is to: |
| 465 | // 1) Make a flatbuffer builder for a config that will just contain a list of |
| 466 | // the new channels that we want to add. |
| 467 | // 2) For each channel that we are remapping: |
| 468 | // a) Make a buffer/builder and construct into it a Channel table that only |
| 469 | // contains the new name for the channel. |
| 470 | // b) Merge the new channel with just the name into the channel that we are |
| 471 | // trying to copy, built in the flatbuffer builder made in 1. This gives |
| 472 | // us the new channel definition that we need. |
| 473 | // 3) Using this list of offsets, build the Configuration of just new |
| 474 | // Channels. |
| 475 | // 4) Merge the Configuration with the new Channels into the base_config. |
| 476 | // 5) Call MergeConfiguration() on that result to give MergeConfiguration a |
| 477 | // chance to sanitize the config. |
| 478 | |
| 479 | // This is the builder that we use for the config containing all the new |
| 480 | // channels. |
| 481 | flatbuffers::FlatBufferBuilder new_config_fbb; |
| 482 | new_config_fbb.ForceDefaults(1); |
| 483 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 484 | for (auto &pair : remapped_channels_) { |
| 485 | // This is the builder that we use for creating the Channel with just the |
| 486 | // new name. |
| 487 | flatbuffers::FlatBufferBuilder new_name_fbb; |
| 488 | new_name_fbb.ForceDefaults(1); |
| 489 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
| 490 | new_name_fbb.CreateString(pair.second); |
| 491 | ChannelBuilder new_name_builder(new_name_fbb); |
| 492 | new_name_builder.add_name(name_offset); |
| 493 | new_name_fbb.Finish(new_name_builder.Finish()); |
| 494 | const FlatbufferDetachedBuffer<Channel> new_name = new_name_fbb.Release(); |
| 495 | // Retrieve the channel that we want to copy, confirming that it is actually |
| 496 | // present in base_config. |
| 497 | const Channel *const base_channel = CHECK_NOTNULL(configuration::GetChannel( |
| 498 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 499 | nullptr)); |
| 500 | // Actually create the new channel and put it into the vector of Offsets |
| 501 | // that we will use to create the new Configuration. |
| 502 | channel_offsets.emplace_back(MergeFlatBuffers<Channel>( |
| 503 | reinterpret_cast<const flatbuffers::Table *>(base_channel), |
| 504 | reinterpret_cast<const flatbuffers::Table *>(&new_name.message()), |
| 505 | &new_config_fbb)); |
| 506 | } |
| 507 | // Create the Configuration containing the new channels that we want to add. |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 508 | const auto new_name_vector_offsets = |
| 509 | new_config_fbb.CreateVector(channel_offsets); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 510 | ConfigurationBuilder new_config_builder(new_config_fbb); |
| 511 | new_config_builder.add_channels(new_name_vector_offsets); |
| 512 | new_config_fbb.Finish(new_config_builder.Finish()); |
| 513 | const FlatbufferDetachedBuffer<Configuration> new_name_config = |
| 514 | new_config_fbb.Release(); |
| 515 | // Merge the new channels configuration into the base_config, giving us the |
| 516 | // remapped configuration. |
| 517 | remapped_configuration_buffer_ = |
| 518 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
| 519 | MergeFlatBuffers<Configuration>(base_config, |
| 520 | &new_name_config.message())); |
| 521 | // Call MergeConfiguration to deal with sanitizing the config. |
| 522 | remapped_configuration_buffer_ = |
| 523 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
| 524 | configuration::MergeConfiguration(*remapped_configuration_buffer_)); |
| 525 | |
| 526 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
| 527 | } |
| 528 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 529 | } // namespace logger |
| 530 | } // namespace aos |