Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/log_writer.h" |
| 2 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 3 | #include <dirent.h> |
| 4 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 5 | #include <functional> |
| 6 | #include <map> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "aos/configuration.h" |
| 10 | #include "aos/events/event_loop.h" |
| 11 | #include "aos/network/message_bridge_server_generated.h" |
| 12 | #include "aos/network/team_number.h" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 13 | #include "aos/network/timestamp_channel.h" |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 14 | #include "aos/sha256.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 15 | |
| 16 | namespace aos { |
| 17 | namespace logger { |
| 18 | namespace { |
| 19 | using message_bridge::RemoteMessage; |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 20 | namespace chrono = std::chrono; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 21 | } // namespace |
| 22 | |
| 23 | Logger::Logger(EventLoop *event_loop, const Configuration *configuration, |
| 24 | std::function<bool(const Channel *)> should_log) |
| 25 | : event_loop_(event_loop), |
| 26 | configuration_(configuration), |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 27 | node_(configuration::GetNode(configuration_, event_loop->node())), |
| 28 | node_index_(configuration::GetNodeIndex(configuration_, node_)), |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 29 | name_(network::GetHostname()), |
| 30 | timer_handler_(event_loop_->AddTimer( |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 31 | [this]() { DoLogData(event_loop_->monotonic_now(), true); })), |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 32 | server_statistics_fetcher_( |
| 33 | configuration::MultiNode(event_loop_->configuration()) |
| 34 | ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>( |
| 35 | "/aos") |
| 36 | : aos::Fetcher<message_bridge::ServerStatistics>()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 37 | timer_handler_->set_name("channel_poll"); |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 38 | VLOG(1) << "Creating logger for " << FlatbufferToJson(node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 39 | |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 40 | // When we are logging remote timestamps, we need to be able to translate |
| 41 | // from the channel index that the event loop uses to the channel index in |
| 42 | // the config in the log file. |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 43 | event_loop_to_logged_channel_index_.resize( |
| 44 | event_loop->configuration()->channels()->size(), -1); |
| 45 | for (size_t event_loop_channel_index = 0; |
| 46 | event_loop_channel_index < |
| 47 | event_loop->configuration()->channels()->size(); |
| 48 | ++event_loop_channel_index) { |
| 49 | const Channel *event_loop_channel = |
| 50 | event_loop->configuration()->channels()->Get(event_loop_channel_index); |
| 51 | |
| 52 | const Channel *logged_channel = aos::configuration::GetChannel( |
| 53 | configuration_, event_loop_channel->name()->string_view(), |
| 54 | event_loop_channel->type()->string_view(), "", node_); |
| 55 | |
| 56 | if (logged_channel != nullptr) { |
| 57 | event_loop_to_logged_channel_index_[event_loop_channel_index] = |
| 58 | configuration::ChannelIndex(configuration_, logged_channel); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Map to match source channels with the timestamp logger, if the contents |
| 63 | // should be reliable, and a list of all channels logged on it to be treated |
| 64 | // as reliable. |
| 65 | std::map<const Channel *, std::tuple<const Node *, bool, std::vector<bool>>> |
| 66 | timestamp_logger_channels; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 68 | message_bridge::ChannelTimestampFinder finder(event_loop_); |
| 69 | for (const Channel *channel : *event_loop_->configuration()->channels()) { |
| 70 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node())) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 71 | continue; |
| 72 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 73 | if (!channel->has_destination_nodes()) { |
| 74 | continue; |
| 75 | } |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 76 | const size_t channel_index = |
| 77 | configuration::ChannelIndex(event_loop_->configuration(), channel); |
| 78 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 79 | for (const Connection *connection : *channel->destination_nodes()) { |
| 80 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 81 | connection, event_loop_->node())) { |
| 82 | const Node *other_node = configuration::GetNode( |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 83 | configuration_, connection->name()->string_view()); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 84 | |
| 85 | VLOG(1) << "Timestamps are logged from " |
| 86 | << FlatbufferToJson(other_node); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 87 | // True if each channel's remote timestamps are split into a separate |
| 88 | // RemoteMessage channel. |
| 89 | const bool is_split = |
| 90 | finder.SplitChannelForChannel(channel, connection) != nullptr; |
| 91 | |
| 92 | const Channel *const timestamp_logger_channel = |
| 93 | finder.ForChannel(channel, connection); |
| 94 | |
| 95 | auto it = timestamp_logger_channels.find(timestamp_logger_channel); |
| 96 | if (it != timestamp_logger_channels.end()) { |
| 97 | CHECK(!is_split); |
| 98 | CHECK_LT(channel_index, std::get<2>(it->second).size()); |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 99 | std::get<2>(it->second)[channel_index] = |
| 100 | (connection->time_to_live() == 0); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 101 | } else { |
| 102 | if (is_split) { |
| 103 | timestamp_logger_channels.insert(std::make_pair( |
| 104 | timestamp_logger_channel, |
| 105 | std::make_tuple(other_node, (connection->time_to_live() == 0), |
| 106 | std::vector<bool>()))); |
| 107 | } else { |
| 108 | std::vector<bool> channel_reliable_contents( |
| 109 | event_loop->configuration()->channels()->size(), false); |
| 110 | channel_reliable_contents[channel_index] = |
| 111 | (connection->time_to_live() == 0); |
| 112 | |
| 113 | timestamp_logger_channels.insert(std::make_pair( |
| 114 | timestamp_logger_channel, |
| 115 | std::make_tuple(other_node, false, |
| 116 | std::move(channel_reliable_contents)))); |
| 117 | } |
| 118 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 123 | for (size_t channel_index = 0; |
| 124 | channel_index < configuration_->channels()->size(); ++channel_index) { |
| 125 | const Channel *const config_channel = |
| 126 | configuration_->channels()->Get(channel_index); |
| 127 | // The MakeRawFetcher method needs a channel which is in the event loop |
| 128 | // configuration() object, not the configuration_ object. Go look that up |
| 129 | // from the config. |
| 130 | const Channel *channel = aos::configuration::GetChannel( |
| 131 | event_loop_->configuration(), config_channel->name()->string_view(), |
| 132 | config_channel->type()->string_view(), "", event_loop_->node()); |
| 133 | CHECK(channel != nullptr) |
| 134 | << ": Failed to look up channel " |
| 135 | << aos::configuration::CleanedChannelToString(config_channel); |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 136 | if (!should_log(config_channel)) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | |
| 140 | FetcherStruct fs; |
| 141 | fs.channel_index = channel_index; |
| 142 | fs.channel = channel; |
| 143 | |
| 144 | const bool is_local = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 145 | configuration::ChannelIsSendableOnNode(config_channel, node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 146 | |
| 147 | const bool is_readable = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 148 | configuration::ChannelIsReadableOnNode(config_channel, node_); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 149 | const bool is_logged = |
| 150 | configuration::ChannelMessageIsLoggedOnNode(config_channel, node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 151 | const bool log_message = is_logged && is_readable; |
| 152 | |
| 153 | bool log_delivery_times = false; |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 154 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 155 | const aos::Connection *connection = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 156 | configuration::ConnectionToNode(config_channel, node_); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 157 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 158 | log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 159 | connection, event_loop_->node()); |
| 160 | |
| 161 | CHECK_EQ(log_delivery_times, |
| 162 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 163 | config_channel, node_, node_)); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 164 | |
| 165 | if (connection) { |
| 166 | fs.reliable_forwarding = (connection->time_to_live() == 0); |
| 167 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 170 | // Now, detect a RemoteMessage timestamp logger where we should just log |
| 171 | // the contents to a file directly. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 172 | const bool log_contents = timestamp_logger_channels.find(channel) != |
| 173 | timestamp_logger_channels.end(); |
| 174 | |
| 175 | if (log_message || log_delivery_times || log_contents) { |
| 176 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 177 | VLOG(1) << "Logging channel " |
| 178 | << configuration::CleanedChannelToString(channel); |
| 179 | |
| 180 | if (log_delivery_times) { |
| 181 | VLOG(1) << " Delivery times"; |
| 182 | fs.wants_timestamp_writer = true; |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 183 | fs.timestamp_node_index = static_cast<int>(node_index_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 184 | } |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 185 | // Both the timestamp and data writers want data_node_index so it knows |
| 186 | // what the source node is. |
| 187 | if (log_message || log_delivery_times) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 188 | if (!is_local) { |
| 189 | const Node *source_node = configuration::GetNode( |
| 190 | configuration_, channel->source_node()->string_view()); |
| 191 | fs.data_node_index = |
| 192 | configuration::GetNodeIndex(configuration_, source_node); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | if (log_message) { |
| 196 | VLOG(1) << " Data"; |
| 197 | fs.wants_writer = true; |
| 198 | if (!is_local) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 199 | fs.log_type = LogType::kLogRemoteMessage; |
| 200 | } else { |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 201 | fs.data_node_index = static_cast<int>(node_index_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | if (log_contents) { |
| 205 | VLOG(1) << "Timestamp logger channel " |
| 206 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 207 | auto timestamp_logger_channel_info = |
| 208 | timestamp_logger_channels.find(channel); |
| 209 | CHECK(timestamp_logger_channel_info != timestamp_logger_channels.end()); |
| 210 | fs.timestamp_node = std::get<0>(timestamp_logger_channel_info->second); |
| 211 | fs.reliable_contents = |
| 212 | std::get<1>(timestamp_logger_channel_info->second); |
| 213 | fs.channel_reliable_contents = |
| 214 | std::get<2>(timestamp_logger_channel_info->second); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 215 | fs.wants_contents_writer = true; |
| 216 | fs.contents_node_index = |
| 217 | configuration::GetNodeIndex(configuration_, fs.timestamp_node); |
| 218 | } |
| 219 | fetchers_.emplace_back(std::move(fs)); |
| 220 | } |
| 221 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | Logger::~Logger() { |
| 225 | if (log_namer_) { |
| 226 | // If we are replaying a log file, or in simulation, we want to force the |
| 227 | // last bit of data to be logged. The easiest way to deal with this is to |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 228 | // poll everything as we go to destroy the class, ie, shut down the |
| 229 | // logger, and write it to disk. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 230 | StopLogging(event_loop_->monotonic_now()); |
| 231 | } |
| 232 | } |
| 233 | |
Austin Schuh | 3b2b5b5 | 2023-07-05 11:36:46 -0700 | [diff] [blame^] | 234 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> PackConfiguration( |
| 235 | const Configuration *const configuration) { |
| 236 | flatbuffers::FlatBufferBuilder fbb; |
| 237 | flatbuffers::Offset<aos::Configuration> configuration_offset = |
| 238 | CopyFlatBuffer(configuration, &fbb); |
| 239 | LogFileHeader::Builder log_file_header_builder(fbb); |
| 240 | log_file_header_builder.add_configuration(configuration_offset); |
| 241 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 242 | return fbb.Release(); |
| 243 | } |
| 244 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 245 | std::string Logger::WriteConfiguration(LogNamer *log_namer) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 246 | std::string config_sha256; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 247 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 248 | if (separate_config_) { |
Austin Schuh | 3b2b5b5 | 2023-07-05 11:36:46 -0700 | [diff] [blame^] | 249 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header = |
| 250 | PackConfiguration(configuration_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 251 | config_sha256 = Sha256(config_header.span()); |
| 252 | LOG(INFO) << "Config sha256 of " << config_sha256; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 253 | log_namer->WriteConfiguration(&config_header, config_sha256); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 256 | return config_sha256; |
| 257 | } |
| 258 | |
| 259 | void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 260 | std::optional<UUID> log_start_uuid) { |
| 261 | CHECK(!log_namer_) << ": Already logging"; |
| 262 | |
| 263 | VLOG(1) << "Starting logger for " << FlatbufferToJson(node_); |
| 264 | |
| 265 | auto config_sha256 = WriteConfiguration(log_namer.get()); |
| 266 | |
| 267 | log_namer_ = std::move(log_namer); |
| 268 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 269 | log_event_uuid_ = UUID::Random(); |
| 270 | log_start_uuid_ = log_start_uuid; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 271 | |
| 272 | // We want to do as much work as possible before the initial Fetch. Time |
| 273 | // between that and actually starting to log opens up the possibility of |
| 274 | // falling off the end of the queue during that time. |
| 275 | |
| 276 | for (FetcherStruct &f : fetchers_) { |
| 277 | if (f.wants_writer) { |
| 278 | f.writer = log_namer_->MakeWriter(f.channel); |
| 279 | } |
| 280 | if (f.wants_timestamp_writer) { |
| 281 | f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel); |
| 282 | } |
| 283 | if (f.wants_contents_writer) { |
| 284 | f.contents_writer = log_namer_->MakeForwardedTimestampWriter( |
| 285 | f.channel, CHECK_NOTNULL(f.timestamp_node)); |
| 286 | } |
| 287 | } |
| 288 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 289 | log_namer_->SetHeaderTemplate(MakeHeader(config_sha256)); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 290 | |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 291 | const aos::monotonic_clock::time_point beginning_time = |
| 292 | event_loop_->monotonic_now(); |
| 293 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 294 | // Grab data from each channel right before we declare the log file started |
| 295 | // so we can capture the latest message on each channel. This lets us have |
| 296 | // non periodic messages with configuration that now get logged. |
| 297 | for (FetcherStruct &f : fetchers_) { |
| 298 | const auto start = event_loop_->monotonic_now(); |
| 299 | const bool got_new = f.fetcher->Fetch(); |
| 300 | const auto end = event_loop_->monotonic_now(); |
| 301 | RecordFetchResult(start, end, got_new, &f); |
| 302 | |
| 303 | // If there is a message, we want to write it. |
| 304 | f.written = f.fetcher->context().data == nullptr; |
| 305 | } |
| 306 | |
| 307 | // Clear out any old timestamps in case we are re-starting logging. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 308 | for (size_t i = 0; i < configuration::NodesCount(configuration_); ++i) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 309 | log_namer_->ClearStartTimes(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 312 | const aos::monotonic_clock::time_point fetch_time = |
| 313 | event_loop_->monotonic_now(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 314 | WriteHeader(); |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 315 | const aos::monotonic_clock::time_point header_time = |
| 316 | event_loop_->monotonic_now(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 317 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 318 | VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " start_time " |
| 319 | << last_synchronized_time_ << ", took " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 320 | << chrono::duration<double>(fetch_time - beginning_time).count() |
| 321 | << " to fetch, " |
| 322 | << chrono::duration<double>(header_time - fetch_time).count() |
| 323 | << " to write headers, boot uuid " << event_loop_->boot_uuid(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 324 | |
| 325 | // Force logging up until the start of the log file now, so the messages at |
| 326 | // the start are always ordered before the rest of the messages. |
| 327 | // Note: this ship may have already sailed, but we don't have to make it |
| 328 | // worse. |
| 329 | // TODO(austin): Test... |
Austin Schuh | 855f893 | 2021-03-19 22:01:32 -0700 | [diff] [blame] | 330 | // |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 331 | // This is safe to call here since we have set last_synchronized_time_ as |
| 332 | // the same time as in the header, and all the data before it should be |
| 333 | // logged without ordering concerns. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 334 | LogUntil(last_synchronized_time_); |
| 335 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 336 | timer_handler_->Schedule(event_loop_->monotonic_now() + polling_period_, |
| 337 | polling_period_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 340 | std::unique_ptr<LogNamer> Logger::RestartLogging( |
| 341 | std::unique_ptr<LogNamer> log_namer, std::optional<UUID> log_start_uuid) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 342 | CHECK(log_namer_) << ": Unexpected restart while not logging"; |
| 343 | |
| 344 | VLOG(1) << "Restarting logger for " << FlatbufferToJson(node_); |
| 345 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 346 | // Make sure not to write past now so we don't risk out of order problems. We |
| 347 | // don't want to get into a situation where we write out up to now + 0.1 sec, |
| 348 | // and that operation takes ~0.1 seconds, so we end up writing a different |
| 349 | // amount of the early and late channels. That would then result in the next |
| 350 | // go around finding more than 0.1 sec of data on the early channels. |
| 351 | // |
| 352 | // Make sure we read up until "now" and log it. This sets us up so that we |
| 353 | // are unlikely to fetch a message far in the future and have a ton of data |
| 354 | // before the offical start time. |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 355 | monotonic_clock::time_point newest_record = monotonic_clock::min_time; |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 356 | while (true) { |
| 357 | aos::monotonic_clock::time_point next_time = |
| 358 | last_synchronized_time_ + polling_period_; |
| 359 | const aos::monotonic_clock::time_point monotonic_now = |
| 360 | event_loop_->monotonic_now(); |
| 361 | if (next_time > monotonic_now) { |
| 362 | next_time = monotonic_now; |
| 363 | } |
| 364 | |
| 365 | bool wrote_messages = false; |
| 366 | std::tie(wrote_messages, newest_record) = LogUntil(next_time); |
| 367 | |
| 368 | if (next_time == monotonic_now && |
| 369 | (!wrote_messages || newest_record < monotonic_now + polling_period_)) { |
| 370 | // If we stopped writing messages, then we probably have stopped making |
| 371 | // progress. If the newest record (unwritten or written) on a channel is |
| 372 | // very close to the current time, then there won't be much data |
| 373 | // officially after the end of the last log but before the start of the |
| 374 | // current one. We need to pick the start of the current log to be after |
| 375 | // the last message on record so we don't have holes in the log. |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // We are now synchronized up to last_synchronized_time_. Our start time can |
| 381 | // safely be "newest_record". But, we need to guarentee that the start time |
| 382 | // is after the newest message we have a record of, and that we don't skip any |
| 383 | // messages as we rotate. This means we can't call Fetch anywhere. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 384 | |
| 385 | std::unique_ptr<LogNamer> old_log_namer = std::move(log_namer_); |
| 386 | log_namer_ = std::move(log_namer); |
| 387 | |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 388 | // Now grab a representative time on both the RT and monotonic clock. |
| 389 | // Average a monotonic clock before and after to reduce the error. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 390 | const aos::monotonic_clock::time_point beginning_time = |
| 391 | event_loop_->monotonic_now(); |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 392 | const aos::realtime_clock::time_point beginning_time_rt = |
| 393 | event_loop_->realtime_now(); |
| 394 | const aos::monotonic_clock::time_point beginning_time2 = |
| 395 | event_loop_->monotonic_now(); |
| 396 | |
| 397 | if (beginning_time > last_synchronized_time_) { |
| 398 | LOG(WARNING) << "Took over " << polling_period_.count() |
| 399 | << "ns to swap log_namer"; |
| 400 | } |
| 401 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 402 | // Our start time is now the newest message we have a record of. We will |
| 403 | // declare the old log "done", and start in on the new one, double-logging |
| 404 | // anything we have a record of so we have all the messages from before the |
| 405 | // start. |
| 406 | const aos::monotonic_clock::time_point monotonic_start_time = newest_record; |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 407 | const aos::realtime_clock::time_point realtime_start_time = |
| 408 | (beginning_time_rt + (monotonic_start_time.time_since_epoch() - |
| 409 | ((beginning_time.time_since_epoch() + |
| 410 | beginning_time2.time_since_epoch()) / |
| 411 | 2))); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 412 | |
| 413 | auto config_sha256 = WriteConfiguration(log_namer_.get()); |
| 414 | |
| 415 | log_event_uuid_ = UUID::Random(); |
| 416 | log_start_uuid_ = log_start_uuid; |
| 417 | |
| 418 | log_namer_->SetHeaderTemplate(MakeHeader(config_sha256)); |
| 419 | |
| 420 | // Note that WriteHeader updates last_synchronized_time_ to be the |
| 421 | // current time when it is called, which is then the "start time" |
| 422 | // of the new (restarted) log. This timestamp will be after |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 423 | // the timestamp of the last message fetched on each channel, but is |
| 424 | // carefully picked per the comment above to not violate |
| 425 | // max_out_of_order_duration. |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 426 | WriteHeader(monotonic_start_time, realtime_start_time); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 427 | |
| 428 | const aos::monotonic_clock::time_point header_time = |
| 429 | event_loop_->monotonic_now(); |
| 430 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 431 | // Close out the old writers to free up memory to be used by the new writers. |
| 432 | old_log_namer->Close(); |
| 433 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 434 | for (FetcherStruct &f : fetchers_) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 435 | // Create writers from the new namer |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 436 | |
| 437 | if (f.wants_writer) { |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 438 | f.writer = log_namer_->MakeWriter(f.channel); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 439 | } |
| 440 | if (f.wants_timestamp_writer) { |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 441 | f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 442 | } |
| 443 | if (f.wants_contents_writer) { |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 444 | f.contents_writer = log_namer_->MakeForwardedTimestampWriter( |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 445 | f.channel, CHECK_NOTNULL(f.timestamp_node)); |
| 446 | } |
| 447 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 448 | // Mark each channel with data as not written. That triggers each channel |
| 449 | // to be re-logged. |
| 450 | f.written = f.fetcher->context().data == nullptr; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 453 | // And now make sure to log everything up to the start time in 1 big go so we |
| 454 | // make sure we have it before we let the world start logging normally again. |
| 455 | LogUntil(monotonic_start_time); |
| 456 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 457 | const aos::monotonic_clock::time_point channel_time = |
| 458 | event_loop_->monotonic_now(); |
| 459 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 460 | VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " restart_time " |
| 461 | << last_synchronized_time_ << ", took " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 462 | << chrono::duration<double>(header_time - beginning_time).count() |
| 463 | << " to prepare and write header, " |
| 464 | << chrono::duration<double>(channel_time - header_time).count() |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 465 | << " to write initial channel messages, boot uuid " |
| 466 | << event_loop_->boot_uuid(); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 467 | |
| 468 | return old_log_namer; |
| 469 | } |
| 470 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 471 | std::unique_ptr<LogNamer> Logger::StopLogging( |
| 472 | aos::monotonic_clock::time_point end_time) { |
| 473 | CHECK(log_namer_) << ": Not logging right now"; |
| 474 | |
| 475 | if (end_time != aos::monotonic_clock::min_time) { |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 476 | // Folks like to use the on_logged_period_ callback to trigger stop and |
| 477 | // start events. We can't have those then recurse and try to stop again. |
| 478 | // Rather than making everything reentrant, let's just instead block the |
| 479 | // callback here. |
| 480 | DoLogData(end_time, false); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 481 | } |
| 482 | timer_handler_->Disable(); |
| 483 | |
| 484 | for (FetcherStruct &f : fetchers_) { |
| 485 | f.writer = nullptr; |
| 486 | f.timestamp_writer = nullptr; |
| 487 | f.contents_writer = nullptr; |
| 488 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 489 | |
| 490 | log_event_uuid_ = UUID::Zero(); |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 491 | log_start_uuid_ = std::nullopt; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 492 | |
Alexei Strots | bc776d5 | 2023-04-15 23:46:45 -0700 | [diff] [blame] | 493 | log_namer_->Close(); |
| 494 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 495 | return std::move(log_namer_); |
| 496 | } |
| 497 | |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 498 | void Logger::WriteHeader(aos::monotonic_clock::time_point monotonic_start_time, |
| 499 | aos::realtime_clock::time_point realtime_start_time) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 500 | if (configuration::MultiNode(configuration_)) { |
| 501 | server_statistics_fetcher_.Fetch(); |
| 502 | } |
| 503 | |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 504 | if (monotonic_start_time == aos::monotonic_clock::min_time) { |
| 505 | monotonic_start_time = event_loop_->monotonic_now(); |
| 506 | realtime_start_time = event_loop_->realtime_now(); |
| 507 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 508 | |
| 509 | // We need to pick a point in time to declare the log file "started". This |
| 510 | // starts here. It needs to be after everything is fetched so that the |
| 511 | // fetchers are all pointed at the most recent message before the start |
| 512 | // time. |
| 513 | last_synchronized_time_ = monotonic_start_time; |
| 514 | |
| 515 | for (const Node *node : log_namer_->nodes()) { |
| 516 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
| 517 | MaybeUpdateTimestamp(node, node_index, monotonic_start_time, |
| 518 | realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 522 | void Logger::WriteMissingTimestamps() { |
| 523 | if (configuration::MultiNode(configuration_)) { |
| 524 | server_statistics_fetcher_.Fetch(); |
| 525 | } else { |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if (server_statistics_fetcher_.get() == nullptr) { |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | for (const Node *node : log_namer_->nodes()) { |
| 534 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
| 535 | if (MaybeUpdateTimestamp( |
| 536 | node, node_index, |
| 537 | server_statistics_fetcher_.context().monotonic_event_time, |
| 538 | server_statistics_fetcher_.context().realtime_event_time)) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 539 | VLOG(1) << "Timestamps changed on " << aos::FlatbufferToJson(node); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 544 | bool Logger::MaybeUpdateTimestamp( |
| 545 | const Node *node, int node_index, |
| 546 | aos::monotonic_clock::time_point monotonic_start_time, |
| 547 | aos::realtime_clock::time_point realtime_start_time) { |
| 548 | // Bail early if the start times are already set. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 549 | if (node_ == node || !configuration::MultiNode(configuration_)) { |
| 550 | if (log_namer_->monotonic_start_time(node_index, |
| 551 | event_loop_->boot_uuid()) != |
| 552 | monotonic_clock::min_time) { |
| 553 | return false; |
| 554 | } |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 555 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 556 | // There are no offsets to compute for ourself, so always succeed. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 557 | log_namer_->SetStartTimes(node_index, event_loop_->boot_uuid(), |
| 558 | monotonic_start_time, realtime_start_time, |
| 559 | monotonic_start_time, realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 560 | return true; |
| 561 | } else if (server_statistics_fetcher_.get() != nullptr) { |
| 562 | // We must be a remote node now. Look for the connection and see if it is |
| 563 | // connected. |
James Kuszmaul | 17607fb | 2021-10-15 20:00:32 -0700 | [diff] [blame] | 564 | CHECK(server_statistics_fetcher_->has_connections()); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 565 | |
| 566 | for (const message_bridge::ServerConnection *connection : |
| 567 | *server_statistics_fetcher_->connections()) { |
| 568 | if (connection->node()->name()->string_view() != |
| 569 | node->name()->string_view()) { |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 574 | VLOG(1) << node->name()->string_view() |
| 575 | << " is not connected, can't start it yet."; |
| 576 | break; |
| 577 | } |
| 578 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 579 | if (!connection->has_monotonic_offset()) { |
| 580 | VLOG(1) << "Missing monotonic offset for setting start time for node " |
| 581 | << aos::FlatbufferToJson(node); |
| 582 | break; |
| 583 | } |
| 584 | |
James Kuszmaul | 17607fb | 2021-10-15 20:00:32 -0700 | [diff] [blame] | 585 | CHECK(connection->has_boot_uuid()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 586 | const UUID boot_uuid = |
| 587 | UUID::FromString(connection->boot_uuid()->string_view()); |
| 588 | |
| 589 | if (log_namer_->monotonic_start_time(node_index, boot_uuid) != |
| 590 | monotonic_clock::min_time) { |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | VLOG(1) << "Updating start time for " |
| 595 | << aos::FlatbufferToJson(connection); |
| 596 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 597 | // Found it and it is connected. Compensate and go. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 598 | log_namer_->SetStartTimes( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 599 | node_index, boot_uuid, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 600 | monotonic_start_time + |
| 601 | std::chrono::nanoseconds(connection->monotonic_offset()), |
| 602 | realtime_start_time, monotonic_start_time, realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 603 | return true; |
| 604 | } |
| 605 | } |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader( |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 610 | std::string_view config_sha256) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 611 | flatbuffers::FlatBufferBuilder fbb; |
| 612 | fbb.ForceDefaults(true); |
| 613 | |
| 614 | flatbuffers::Offset<aos::Configuration> configuration_offset; |
| 615 | if (!separate_config_) { |
| 616 | configuration_offset = CopyFlatBuffer(configuration_, &fbb); |
| 617 | } else { |
| 618 | CHECK(!config_sha256.empty()); |
| 619 | } |
| 620 | |
| 621 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
| 622 | fbb.CreateString(name_); |
| 623 | |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 624 | const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset = |
| 625 | logger_sha1_.empty() ? 0 : fbb.CreateString(logger_sha1_); |
| 626 | const flatbuffers::Offset<flatbuffers::String> logger_version_offset = |
| 627 | logger_version_.empty() ? 0 : fbb.CreateString(logger_version_); |
| 628 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 629 | CHECK(log_event_uuid_ != UUID::Zero()); |
| 630 | const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 631 | log_event_uuid_.PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 632 | |
| 633 | const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 634 | logger_instance_uuid_.PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 635 | |
| 636 | flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset; |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 637 | if (log_start_uuid_) { |
| 638 | log_start_uuid_offset = fbb.CreateString(log_start_uuid_->ToString()); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset; |
| 642 | if (!config_sha256.empty()) { |
| 643 | config_sha256_offset = fbb.CreateString(config_sha256); |
| 644 | } |
| 645 | |
| 646 | const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 647 | event_loop_->boot_uuid().PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 648 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 649 | flatbuffers::Offset<Node> logger_node_offset; |
| 650 | |
| 651 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 652 | logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 656 | |
| 657 | log_file_header_builder.add_name(name_offset); |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 658 | if (!logger_sha1_offset.IsNull()) { |
| 659 | log_file_header_builder.add_logger_sha1(logger_sha1_offset); |
| 660 | } |
| 661 | if (!logger_version_offset.IsNull()) { |
| 662 | log_file_header_builder.add_logger_version(logger_version_offset); |
| 663 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 664 | |
| 665 | // Only add the node if we are running in a multinode configuration. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 666 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 667 | log_file_header_builder.add_logger_node(logger_node_offset); |
| 668 | } |
| 669 | |
| 670 | if (!configuration_offset.IsNull()) { |
| 671 | log_file_header_builder.add_configuration(configuration_offset); |
| 672 | } |
| 673 | // The worst case theoretical out of order is the polling period times 2. |
| 674 | // One message could get logged right after the boundary, but be for right |
| 675 | // before the next boundary. And the reverse could happen for another |
| 676 | // message. Report back 3x to be extra safe, and because the cost isn't |
| 677 | // huge on the read side. |
| 678 | log_file_header_builder.add_max_out_of_order_duration( |
| 679 | std::chrono::nanoseconds(3 * polling_period_).count()); |
| 680 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 681 | log_file_header_builder.add_log_event_uuid(log_event_uuid_offset); |
| 682 | log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset); |
| 683 | if (!log_start_uuid_offset.IsNull()) { |
| 684 | log_file_header_builder.add_log_start_uuid(log_start_uuid_offset); |
| 685 | } |
| 686 | log_file_header_builder.add_logger_node_boot_uuid( |
| 687 | logger_node_boot_uuid_offset); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 688 | |
| 689 | if (!config_sha256_offset.IsNull()) { |
| 690 | log_file_header_builder.add_configuration_sha256(config_sha256_offset); |
| 691 | } |
| 692 | |
| 693 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 694 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result( |
| 695 | fbb.Release()); |
| 696 | |
| 697 | CHECK(result.Verify()) << ": Built a corrupted header."; |
| 698 | |
| 699 | return result; |
| 700 | } |
| 701 | |
| 702 | void Logger::ResetStatisics() { |
| 703 | max_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 704 | max_message_fetch_time_channel_ = -1; |
| 705 | max_message_fetch_time_size_ = -1; |
| 706 | total_message_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 707 | total_message_fetch_count_ = 0; |
| 708 | total_message_fetch_bytes_ = 0; |
| 709 | total_nop_fetch_time_ = std::chrono::nanoseconds::zero(); |
| 710 | total_nop_fetch_count_ = 0; |
| 711 | max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 712 | max_copy_time_channel_ = -1; |
| 713 | max_copy_time_size_ = -1; |
| 714 | total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 715 | total_copy_count_ = 0; |
| 716 | total_copy_bytes_ = 0; |
| 717 | } |
| 718 | |
| 719 | void Logger::Rotate() { |
| 720 | for (const Node *node : log_namer_->nodes()) { |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 721 | log_namer_->Rotate(node); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 725 | void Logger::WriteData(NewDataWriter *writer, const FetcherStruct &f) { |
| 726 | if (writer != nullptr) { |
| 727 | const UUID source_node_boot_uuid = |
| 728 | static_cast<int>(node_index_) != f.data_node_index |
| 729 | ? f.fetcher->context().source_boot_uuid |
| 730 | : event_loop_->boot_uuid(); |
| 731 | // Write! |
| 732 | const auto start = event_loop_->monotonic_now(); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 733 | |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 734 | ContextDataCopier coppier(f.fetcher->context(), f.channel_index, f.log_type, |
| 735 | event_loop_); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 736 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 737 | writer->CopyMessage(&coppier, source_node_boot_uuid, start); |
| 738 | RecordCreateMessageTime(start, coppier.end_time(), f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 739 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 740 | VLOG(2) << "Wrote data as node " << FlatbufferToJson(node_) |
| 741 | << " for channel " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 742 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 743 | << " to " << writer->name(); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 747 | void Logger::WriteTimestamps(NewDataWriter *timestamp_writer, |
| 748 | const FetcherStruct &f) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 749 | if (timestamp_writer != nullptr) { |
| 750 | // And now handle timestamps. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 751 | |
| 752 | // Tell our writer that we know something about the remote boot. |
| 753 | timestamp_writer->UpdateRemote( |
| 754 | f.data_node_index, f.fetcher->context().source_boot_uuid, |
| 755 | f.fetcher->context().monotonic_remote_time, |
| 756 | f.fetcher->context().monotonic_event_time, f.reliable_forwarding); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 757 | |
| 758 | const auto start = event_loop_->monotonic_now(); |
| 759 | ContextDataCopier coppier(f.fetcher->context(), f.channel_index, |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 760 | LogType::kLogDeliveryTimeOnly, event_loop_); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 761 | |
| 762 | timestamp_writer->CopyMessage(&coppier, event_loop_->boot_uuid(), start); |
| 763 | RecordCreateMessageTime(start, coppier.end_time(), f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 764 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 765 | VLOG(2) << "Wrote timestamps as node " << FlatbufferToJson(node_) |
| 766 | << " for channel " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 767 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 768 | << " to " << timestamp_writer->name() << " timestamp"; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 772 | void Logger::WriteContent(NewDataWriter *contents_writer, |
| 773 | const FetcherStruct &f) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 774 | if (contents_writer != nullptr) { |
| 775 | const auto start = event_loop_->monotonic_now(); |
| 776 | // And now handle the special message contents channel. Copy the |
| 777 | // message into a FlatBufferBuilder and save it to disk. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 778 | const RemoteMessage *msg = |
| 779 | flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data); |
| 780 | |
| 781 | CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 782 | // Translate from the channel index that the event loop uses to the |
| 783 | // channel index in the log file. |
Austin Schuh | f2d0e68 | 2022-10-16 14:20:58 -0700 | [diff] [blame] | 784 | const int channel_index = |
| 785 | event_loop_to_logged_channel_index_[msg->channel_index()]; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 786 | |
| 787 | const aos::monotonic_clock::time_point monotonic_timestamp_time = |
| 788 | f.fetcher->context().monotonic_event_time; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 789 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 790 | // Timestamps tell us information about what happened too! |
| 791 | // Capture any reboots so UpdateRemote is properly recorded. |
| 792 | contents_writer->UpdateBoot(UUID::FromVector(msg->boot_uuid())); |
| 793 | |
| 794 | // Start with recording info about the data flowing from our node to the |
| 795 | // remote. |
| 796 | const bool reliable = |
| 797 | f.channel_reliable_contents.size() != 0u |
| 798 | ? f.channel_reliable_contents[msg->channel_index()] |
| 799 | : f.reliable_contents; |
| 800 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 801 | contents_writer->UpdateRemote( |
| 802 | node_index_, event_loop_->boot_uuid(), |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 803 | monotonic_clock::time_point( |
| 804 | chrono::nanoseconds(msg->monotonic_remote_time())), |
| 805 | monotonic_clock::time_point( |
| 806 | chrono::nanoseconds(msg->monotonic_sent_time())), |
| 807 | reliable, monotonic_timestamp_time); |
| 808 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 809 | RemoteMessageCopier coppier(msg, channel_index, monotonic_timestamp_time, |
Naman Gupta | 41d70c2 | 2022-11-21 15:29:52 -0800 | [diff] [blame] | 810 | event_loop_); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 811 | |
| 812 | contents_writer->CopyMessage(&coppier, UUID::FromVector(msg->boot_uuid()), |
| 813 | start); |
| 814 | |
| 815 | RecordCreateMessageTime(start, coppier.end_time(), f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | void Logger::WriteFetchedRecord(FetcherStruct &f) { |
| 820 | WriteData(f.writer, f); |
| 821 | WriteTimestamps(f.timestamp_writer, f); |
| 822 | WriteContent(f.contents_writer, f); |
| 823 | } |
| 824 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 825 | std::pair<bool, monotonic_clock::time_point> Logger::LogUntil( |
| 826 | monotonic_clock::time_point t) { |
| 827 | bool wrote_messages = false; |
| 828 | monotonic_clock::time_point newest_record = monotonic_clock::min_time; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 829 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 830 | // Grab the latest ServerStatistics message. This will always have the |
| 831 | // oppertunity to be >= to the current time, so it will always represent any |
| 832 | // reboots which may have happened. |
| 833 | WriteMissingTimestamps(); |
| 834 | |
| 835 | // Write each channel to disk, one at a time. |
| 836 | for (FetcherStruct &f : fetchers_) { |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 837 | if (f.fetcher->context().data != nullptr) { |
| 838 | newest_record = |
| 839 | std::max(newest_record, f.fetcher->context().monotonic_event_time); |
| 840 | } |
| 841 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 842 | while (true) { |
| 843 | if (f.written) { |
| 844 | const auto start = event_loop_->monotonic_now(); |
| 845 | const bool got_new = f.fetcher->FetchNext(); |
| 846 | const auto end = event_loop_->monotonic_now(); |
| 847 | RecordFetchResult(start, end, got_new, &f); |
| 848 | if (!got_new) { |
| 849 | VLOG(2) << "No new data on " |
| 850 | << configuration::CleanedChannelToString( |
| 851 | f.fetcher->channel()); |
| 852 | break; |
| 853 | } |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 854 | newest_record = |
| 855 | std::max(newest_record, f.fetcher->context().monotonic_event_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 856 | f.written = false; |
| 857 | } |
| 858 | |
| 859 | // TODO(james): Write tests to exercise this logic. |
| 860 | if (f.fetcher->context().monotonic_event_time >= t) { |
| 861 | break; |
| 862 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 863 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 864 | WriteFetchedRecord(f); |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 865 | wrote_messages = true; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 866 | |
| 867 | f.written = true; |
| 868 | } |
| 869 | } |
| 870 | last_synchronized_time_ = t; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 871 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 872 | return std::make_pair(wrote_messages, newest_record); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 875 | void Logger::DoLogData(const monotonic_clock::time_point end_time, |
| 876 | bool run_on_logged) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 877 | // We want to guarantee that messages aren't out of order by more than |
| 878 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 879 | // cycle should be a sync point. |
| 880 | |
| 881 | do { |
| 882 | // Move the sync point up by at most polling_period. This forces one sync |
| 883 | // per iteration, even if it is small. |
| 884 | LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time)); |
| 885 | |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 886 | if (run_on_logged) { |
| 887 | on_logged_period_(); |
| 888 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 889 | |
| 890 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 891 | // caught up. |
| 892 | } while (last_synchronized_time_ + polling_period_ < end_time); |
| 893 | } |
| 894 | |
| 895 | void Logger::RecordFetchResult(aos::monotonic_clock::time_point start, |
| 896 | aos::monotonic_clock::time_point end, |
| 897 | bool got_new, FetcherStruct *fetcher) { |
| 898 | const auto duration = end - start; |
| 899 | if (!got_new) { |
| 900 | ++total_nop_fetch_count_; |
| 901 | total_nop_fetch_time_ += duration; |
| 902 | return; |
| 903 | } |
| 904 | ++total_message_fetch_count_; |
| 905 | total_message_fetch_bytes_ += fetcher->fetcher->context().size; |
| 906 | total_message_fetch_time_ += duration; |
| 907 | if (duration > max_message_fetch_time_) { |
| 908 | max_message_fetch_time_ = duration; |
| 909 | max_message_fetch_time_channel_ = fetcher->channel_index; |
| 910 | max_message_fetch_time_size_ = fetcher->fetcher->context().size; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 915 | aos::monotonic_clock::time_point end, |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 916 | const FetcherStruct &fetcher) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 917 | const auto duration = end - start; |
| 918 | total_copy_time_ += duration; |
| 919 | ++total_copy_count_; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 920 | total_copy_bytes_ += fetcher.fetcher->context().size; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 921 | if (duration > max_copy_time_) { |
| 922 | max_copy_time_ = duration; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 923 | max_copy_time_channel_ = fetcher.channel_index; |
| 924 | max_copy_time_size_ = fetcher.fetcher->context().size; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | } // namespace logger |
| 929 | } // namespace aos |