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 | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | namespace logger { |
| 17 | namespace { |
| 18 | using message_bridge::RemoteMessage; |
Austin Schuh | bd06ae4 | 2021-03-31 22:48:21 -0700 | [diff] [blame] | 19 | namespace chrono = std::chrono; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 20 | } // namespace |
| 21 | |
| 22 | Logger::Logger(EventLoop *event_loop, const Configuration *configuration, |
| 23 | std::function<bool(const Channel *)> should_log) |
| 24 | : event_loop_(event_loop), |
| 25 | configuration_(configuration), |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 26 | node_(configuration::GetNode(configuration_, event_loop->node())), |
| 27 | node_index_(configuration::GetNodeIndex(configuration_, node_)), |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 28 | name_(network::GetHostname()), |
| 29 | timer_handler_(event_loop_->AddTimer( |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 30 | [this]() { DoLogData(event_loop_->monotonic_now(), true); })), |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 31 | server_statistics_fetcher_( |
| 32 | configuration::MultiNode(event_loop_->configuration()) |
| 33 | ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>( |
| 34 | "/aos") |
| 35 | : aos::Fetcher<message_bridge::ServerStatistics>()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 36 | timer_handler_->set_name("channel_poll"); |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 37 | VLOG(1) << "Creating logger for " << FlatbufferToJson(node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 38 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 39 | // When we are logging remote timestamps, we need to be able to translate from |
| 40 | // the channel index that the event loop uses to the channel index in the |
| 41 | // config in the log file. |
| 42 | event_loop_to_logged_channel_index_.resize( |
| 43 | event_loop->configuration()->channels()->size(), -1); |
| 44 | for (size_t event_loop_channel_index = 0; |
| 45 | event_loop_channel_index < |
| 46 | event_loop->configuration()->channels()->size(); |
| 47 | ++event_loop_channel_index) { |
| 48 | const Channel *event_loop_channel = |
| 49 | event_loop->configuration()->channels()->Get(event_loop_channel_index); |
| 50 | |
| 51 | const Channel *logged_channel = aos::configuration::GetChannel( |
| 52 | configuration_, event_loop_channel->name()->string_view(), |
| 53 | event_loop_channel->type()->string_view(), "", node_); |
| 54 | |
| 55 | if (logged_channel != nullptr) { |
| 56 | event_loop_to_logged_channel_index_[event_loop_channel_index] = |
| 57 | configuration::ChannelIndex(configuration_, logged_channel); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Map to match source channels with the timestamp logger, if the contents |
| 62 | // should be reliable, and a list of all channels logged on it to be treated |
| 63 | // as reliable. |
| 64 | std::map<const Channel *, std::tuple<const Node *, bool, std::vector<bool>>> |
| 65 | timestamp_logger_channels; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 67 | message_bridge::ChannelTimestampFinder finder(event_loop_); |
| 68 | for (const Channel *channel : *event_loop_->configuration()->channels()) { |
| 69 | if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node())) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 70 | continue; |
| 71 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 72 | if (!channel->has_destination_nodes()) { |
| 73 | continue; |
| 74 | } |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 75 | const size_t channel_index = |
| 76 | configuration::ChannelIndex(event_loop_->configuration(), channel); |
| 77 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 78 | for (const Connection *connection : *channel->destination_nodes()) { |
| 79 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 80 | connection, event_loop_->node())) { |
| 81 | const Node *other_node = configuration::GetNode( |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 82 | configuration_, connection->name()->string_view()); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 83 | |
| 84 | VLOG(1) << "Timestamps are logged from " |
| 85 | << FlatbufferToJson(other_node); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 86 | // True if each channel's remote timestamps are split into a separate |
| 87 | // RemoteMessage channel. |
| 88 | const bool is_split = |
| 89 | finder.SplitChannelForChannel(channel, connection) != nullptr; |
| 90 | |
| 91 | const Channel *const timestamp_logger_channel = |
| 92 | finder.ForChannel(channel, connection); |
| 93 | |
| 94 | auto it = timestamp_logger_channels.find(timestamp_logger_channel); |
| 95 | if (it != timestamp_logger_channels.end()) { |
| 96 | CHECK(!is_split); |
| 97 | CHECK_LT(channel_index, std::get<2>(it->second).size()); |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 98 | std::get<2>(it->second)[channel_index] = |
| 99 | (connection->time_to_live() == 0); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 100 | } else { |
| 101 | if (is_split) { |
| 102 | timestamp_logger_channels.insert(std::make_pair( |
| 103 | timestamp_logger_channel, |
| 104 | std::make_tuple(other_node, (connection->time_to_live() == 0), |
| 105 | std::vector<bool>()))); |
| 106 | } else { |
| 107 | std::vector<bool> channel_reliable_contents( |
| 108 | event_loop->configuration()->channels()->size(), false); |
| 109 | channel_reliable_contents[channel_index] = |
| 110 | (connection->time_to_live() == 0); |
| 111 | |
| 112 | timestamp_logger_channels.insert(std::make_pair( |
| 113 | timestamp_logger_channel, |
| 114 | std::make_tuple(other_node, false, |
| 115 | std::move(channel_reliable_contents)))); |
| 116 | } |
| 117 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 122 | for (size_t channel_index = 0; |
| 123 | channel_index < configuration_->channels()->size(); ++channel_index) { |
| 124 | const Channel *const config_channel = |
| 125 | configuration_->channels()->Get(channel_index); |
| 126 | // The MakeRawFetcher method needs a channel which is in the event loop |
| 127 | // configuration() object, not the configuration_ object. Go look that up |
| 128 | // from the config. |
| 129 | const Channel *channel = aos::configuration::GetChannel( |
| 130 | event_loop_->configuration(), config_channel->name()->string_view(), |
| 131 | config_channel->type()->string_view(), "", event_loop_->node()); |
| 132 | CHECK(channel != nullptr) |
| 133 | << ": Failed to look up channel " |
| 134 | << aos::configuration::CleanedChannelToString(config_channel); |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 135 | if (!should_log(config_channel)) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 136 | continue; |
| 137 | } |
| 138 | |
| 139 | FetcherStruct fs; |
| 140 | fs.channel_index = channel_index; |
| 141 | fs.channel = channel; |
| 142 | |
| 143 | const bool is_local = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 144 | configuration::ChannelIsSendableOnNode(config_channel, node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 145 | |
| 146 | const bool is_readable = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 147 | configuration::ChannelIsReadableOnNode(config_channel, node_); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 148 | const bool is_logged = |
| 149 | configuration::ChannelMessageIsLoggedOnNode(config_channel, node_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 150 | const bool log_message = is_logged && is_readable; |
| 151 | |
| 152 | bool log_delivery_times = false; |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 153 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 154 | const aos::Connection *connection = |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 155 | configuration::ConnectionToNode(config_channel, node_); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 156 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 157 | log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 158 | connection, event_loop_->node()); |
| 159 | |
| 160 | CHECK_EQ(log_delivery_times, |
| 161 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 162 | config_channel, node_, node_)); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 163 | |
| 164 | if (connection) { |
| 165 | fs.reliable_forwarding = (connection->time_to_live() == 0); |
| 166 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 169 | // Now, detect a RemoteMessage timestamp logger where we should just log |
| 170 | // the contents to a file directly. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 171 | const bool log_contents = timestamp_logger_channels.find(channel) != |
| 172 | timestamp_logger_channels.end(); |
| 173 | |
| 174 | if (log_message || log_delivery_times || log_contents) { |
| 175 | fs.fetcher = event_loop->MakeRawFetcher(channel); |
| 176 | VLOG(1) << "Logging channel " |
| 177 | << configuration::CleanedChannelToString(channel); |
| 178 | |
| 179 | if (log_delivery_times) { |
| 180 | VLOG(1) << " Delivery times"; |
| 181 | fs.wants_timestamp_writer = true; |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 182 | fs.timestamp_node_index = static_cast<int>(node_index_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 183 | } |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 184 | // Both the timestamp and data writers want data_node_index so it knows |
| 185 | // what the source node is. |
| 186 | if (log_message || log_delivery_times) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 187 | if (!is_local) { |
| 188 | const Node *source_node = configuration::GetNode( |
| 189 | configuration_, channel->source_node()->string_view()); |
| 190 | fs.data_node_index = |
| 191 | configuration::GetNodeIndex(configuration_, source_node); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | if (log_message) { |
| 195 | VLOG(1) << " Data"; |
| 196 | fs.wants_writer = true; |
| 197 | if (!is_local) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 198 | fs.log_type = LogType::kLogRemoteMessage; |
| 199 | } else { |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 200 | fs.data_node_index = static_cast<int>(node_index_); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | if (log_contents) { |
| 204 | VLOG(1) << "Timestamp logger channel " |
| 205 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 206 | auto timestamp_logger_channel_info = |
| 207 | timestamp_logger_channels.find(channel); |
| 208 | CHECK(timestamp_logger_channel_info != timestamp_logger_channels.end()); |
| 209 | fs.timestamp_node = std::get<0>(timestamp_logger_channel_info->second); |
| 210 | fs.reliable_contents = |
| 211 | std::get<1>(timestamp_logger_channel_info->second); |
| 212 | fs.channel_reliable_contents = |
| 213 | std::get<2>(timestamp_logger_channel_info->second); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 214 | fs.wants_contents_writer = true; |
| 215 | fs.contents_node_index = |
| 216 | configuration::GetNodeIndex(configuration_, fs.timestamp_node); |
| 217 | } |
| 218 | fetchers_.emplace_back(std::move(fs)); |
| 219 | } |
| 220 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | Logger::~Logger() { |
| 224 | if (log_namer_) { |
| 225 | // If we are replaying a log file, or in simulation, we want to force the |
| 226 | // 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] | 227 | // poll everything as we go to destroy the class, ie, shut down the |
| 228 | // logger, and write it to disk. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 229 | StopLogging(event_loop_->monotonic_now()); |
| 230 | } |
| 231 | } |
| 232 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 233 | bool Logger::RenameLogBase(std::string new_base_name) { |
| 234 | if (new_base_name == log_namer_->base_name()) { |
| 235 | return true; |
| 236 | } |
| 237 | std::string current_directory = std::string(log_namer_->base_name()); |
| 238 | std::string new_directory = new_base_name; |
| 239 | |
| 240 | auto current_path_split = current_directory.rfind("/"); |
| 241 | auto new_path_split = new_directory.rfind("/"); |
| 242 | |
| 243 | CHECK(new_base_name.substr(new_path_split) == |
| 244 | current_directory.substr(current_path_split)) |
| 245 | << "Rename of file base from " << current_directory << " to " |
| 246 | << new_directory << " is not supported."; |
| 247 | |
| 248 | current_directory.resize(current_path_split); |
| 249 | new_directory.resize(new_path_split); |
| 250 | DIR *dir = opendir(current_directory.c_str()); |
| 251 | if (dir) { |
| 252 | closedir(dir); |
| 253 | const int result = rename(current_directory.c_str(), new_directory.c_str()); |
| 254 | if (result != 0) { |
| 255 | PLOG(ERROR) << "Unable to rename " << current_directory << " to " |
| 256 | << new_directory; |
| 257 | return false; |
| 258 | } |
| 259 | } else { |
| 260 | // Handle if directory was already renamed. |
| 261 | dir = opendir(new_directory.c_str()); |
| 262 | if (!dir) { |
| 263 | LOG(ERROR) << "Old directory " << current_directory |
| 264 | << " missing and new directory " << new_directory |
| 265 | << " not present."; |
| 266 | return false; |
| 267 | } |
| 268 | closedir(dir); |
| 269 | } |
| 270 | |
| 271 | log_namer_->set_base_name(new_base_name); |
| 272 | Rotate(); |
| 273 | return true; |
| 274 | } |
| 275 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 276 | std::string Logger::WriteConfiguration(LogNamer *log_namer) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 277 | std::string config_sha256; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 278 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 279 | if (separate_config_) { |
| 280 | flatbuffers::FlatBufferBuilder fbb; |
| 281 | flatbuffers::Offset<aos::Configuration> configuration_offset = |
| 282 | CopyFlatBuffer(configuration_, &fbb); |
| 283 | LogFileHeader::Builder log_file_header_builder(fbb); |
| 284 | log_file_header_builder.add_configuration(configuration_offset); |
| 285 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 286 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header( |
| 287 | fbb.Release()); |
| 288 | config_sha256 = Sha256(config_header.span()); |
| 289 | LOG(INFO) << "Config sha256 of " << config_sha256; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 290 | log_namer->WriteConfiguration(&config_header, config_sha256); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 293 | return config_sha256; |
| 294 | } |
| 295 | |
| 296 | void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 297 | std::optional<UUID> log_start_uuid) { |
| 298 | CHECK(!log_namer_) << ": Already logging"; |
| 299 | |
| 300 | VLOG(1) << "Starting logger for " << FlatbufferToJson(node_); |
| 301 | |
| 302 | auto config_sha256 = WriteConfiguration(log_namer.get()); |
| 303 | |
| 304 | log_namer_ = std::move(log_namer); |
| 305 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 306 | log_event_uuid_ = UUID::Random(); |
| 307 | log_start_uuid_ = log_start_uuid; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 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 | } |
| 324 | } |
| 325 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 326 | log_namer_->SetHeaderTemplate(MakeHeader(config_sha256)); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 327 | |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 328 | const aos::monotonic_clock::time_point beginning_time = |
| 329 | event_loop_->monotonic_now(); |
| 330 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 331 | // Grab data from each channel right before we declare the log file started |
| 332 | // so we can capture the latest message on each channel. This lets us have |
| 333 | // non periodic messages with configuration that now get logged. |
| 334 | for (FetcherStruct &f : fetchers_) { |
| 335 | const auto start = event_loop_->monotonic_now(); |
| 336 | const bool got_new = f.fetcher->Fetch(); |
| 337 | const auto end = event_loop_->monotonic_now(); |
| 338 | RecordFetchResult(start, end, got_new, &f); |
| 339 | |
| 340 | // If there is a message, we want to write it. |
| 341 | f.written = f.fetcher->context().data == nullptr; |
| 342 | } |
| 343 | |
| 344 | // Clear out any old timestamps in case we are re-starting logging. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 345 | for (size_t i = 0; i < configuration::NodesCount(configuration_); ++i) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 346 | log_namer_->ClearStartTimes(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 349 | const aos::monotonic_clock::time_point fetch_time = |
| 350 | event_loop_->monotonic_now(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 351 | WriteHeader(); |
Austin Schuh | a42ee96 | 2021-03-31 22:49:30 -0700 | [diff] [blame] | 352 | const aos::monotonic_clock::time_point header_time = |
| 353 | event_loop_->monotonic_now(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 354 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 355 | VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " start_time " |
| 356 | << last_synchronized_time_ << ", took " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 357 | << chrono::duration<double>(fetch_time - beginning_time).count() |
| 358 | << " to fetch, " |
| 359 | << chrono::duration<double>(header_time - fetch_time).count() |
| 360 | << " to write headers, boot uuid " << event_loop_->boot_uuid(); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 361 | |
| 362 | // Force logging up until the start of the log file now, so the messages at |
| 363 | // the start are always ordered before the rest of the messages. |
| 364 | // Note: this ship may have already sailed, but we don't have to make it |
| 365 | // worse. |
| 366 | // TODO(austin): Test... |
Austin Schuh | 855f893 | 2021-03-19 22:01:32 -0700 | [diff] [blame] | 367 | // |
| 368 | // This is safe to call here since we have set last_synchronized_time_ as the |
| 369 | // same time as in the header, and all the data before it should be logged |
| 370 | // without ordering concerns. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 371 | LogUntil(last_synchronized_time_); |
| 372 | |
| 373 | timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_, |
| 374 | polling_period_); |
| 375 | } |
| 376 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 377 | std::unique_ptr<LogNamer> Logger::RestartLogging( |
| 378 | std::unique_ptr<LogNamer> log_namer, std::optional<UUID> log_start_uuid) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 379 | CHECK(log_namer_) << ": Unexpected restart while not logging"; |
| 380 | |
| 381 | VLOG(1) << "Restarting logger for " << FlatbufferToJson(node_); |
| 382 | |
| 383 | // Force out every currently pending message, pointing all fetchers at the |
| 384 | // last (currently available) records. Note that LogUntil() updates |
| 385 | // last_synchronized_time_ to the time value that it receives. |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 386 | while (LogUntil(last_synchronized_time_ + polling_period_)) |
| 387 | ; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 388 | |
| 389 | std::unique_ptr<LogNamer> old_log_namer = std::move(log_namer_); |
| 390 | log_namer_ = std::move(log_namer); |
| 391 | |
| 392 | const aos::monotonic_clock::time_point beginning_time = |
| 393 | event_loop_->monotonic_now(); |
| 394 | |
| 395 | auto config_sha256 = WriteConfiguration(log_namer_.get()); |
| 396 | |
| 397 | log_event_uuid_ = UUID::Random(); |
| 398 | log_start_uuid_ = log_start_uuid; |
| 399 | |
| 400 | log_namer_->SetHeaderTemplate(MakeHeader(config_sha256)); |
| 401 | |
| 402 | // Note that WriteHeader updates last_synchronized_time_ to be the |
| 403 | // current time when it is called, which is then the "start time" |
| 404 | // of the new (restarted) log. This timestamp will be after |
| 405 | // the timestamp of the last message fetched on each channel. |
| 406 | WriteHeader(); |
| 407 | |
| 408 | const aos::monotonic_clock::time_point header_time = |
| 409 | event_loop_->monotonic_now(); |
| 410 | |
| 411 | // Write the transition record(s) for each channel ... |
| 412 | for (FetcherStruct &f : fetchers_) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 413 | // Create writers from the new namer |
| 414 | NewDataWriter *next_writer = nullptr; |
| 415 | NewDataWriter *next_timestamp_writer = nullptr; |
| 416 | NewDataWriter *next_contents_writer = nullptr; |
| 417 | |
| 418 | if (f.wants_writer) { |
| 419 | next_writer = log_namer_->MakeWriter(f.channel); |
| 420 | } |
| 421 | if (f.wants_timestamp_writer) { |
| 422 | next_timestamp_writer = log_namer_->MakeTimestampWriter(f.channel); |
| 423 | } |
| 424 | if (f.wants_contents_writer) { |
| 425 | next_contents_writer = log_namer_->MakeForwardedTimestampWriter( |
| 426 | f.channel, CHECK_NOTNULL(f.timestamp_node)); |
| 427 | } |
| 428 | |
| 429 | if (f.fetcher->context().data != nullptr) { |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 430 | // Write the last message fetched as the first of the new log of this |
| 431 | // type. The timestamps on these will all be before the new start time. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 432 | WriteData(next_writer, f); |
| 433 | WriteTimestamps(next_timestamp_writer, f); |
| 434 | WriteContent(next_contents_writer, f); |
| 435 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 436 | // It is possible that a few more snuck in. Write them all out also, |
| 437 | // including any that should also be in the old log. |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 438 | while (true) { |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 439 | // Get the next message ... |
| 440 | const auto start = event_loop_->monotonic_now(); |
| 441 | const bool got_new = f.fetcher->FetchNext(); |
| 442 | const auto end = event_loop_->monotonic_now(); |
| 443 | RecordFetchResult(start, end, got_new, &f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 444 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 445 | if (got_new) { |
| 446 | if (f.fetcher->context().monotonic_event_time <= |
| 447 | last_synchronized_time_) { |
| 448 | WriteFetchedRecord(f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 449 | WriteData(next_writer, f); |
| 450 | WriteTimestamps(next_timestamp_writer, f); |
| 451 | WriteContent(next_contents_writer, f); |
| 452 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 453 | } else { |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 454 | f.written = false; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 455 | break; |
| 456 | } |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 457 | |
| 458 | } else { |
| 459 | f.written = true; |
| 460 | break; |
| 461 | } |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | // Switch fully over to the new writers. |
| 466 | f.writer = next_writer; |
| 467 | f.timestamp_writer = next_timestamp_writer; |
| 468 | f.contents_writer = next_contents_writer; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | const aos::monotonic_clock::time_point channel_time = |
| 472 | event_loop_->monotonic_now(); |
| 473 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 474 | VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " restart_time " |
| 475 | << last_synchronized_time_ << ", took " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 476 | << chrono::duration<double>(header_time - beginning_time).count() |
| 477 | << " to prepare and write header, " |
| 478 | << chrono::duration<double>(channel_time - header_time).count() |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 479 | << " to write initial channel messages, boot uuid " |
| 480 | << event_loop_->boot_uuid(); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 481 | |
| 482 | return old_log_namer; |
| 483 | } |
| 484 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 485 | std::unique_ptr<LogNamer> Logger::StopLogging( |
| 486 | aos::monotonic_clock::time_point end_time) { |
| 487 | CHECK(log_namer_) << ": Not logging right now"; |
| 488 | |
| 489 | if (end_time != aos::monotonic_clock::min_time) { |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 490 | // Folks like to use the on_logged_period_ callback to trigger stop and |
| 491 | // start events. We can't have those then recurse and try to stop again. |
| 492 | // Rather than making everything reentrant, let's just instead block the |
| 493 | // callback here. |
| 494 | DoLogData(end_time, false); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 495 | } |
| 496 | timer_handler_->Disable(); |
| 497 | |
| 498 | for (FetcherStruct &f : fetchers_) { |
| 499 | f.writer = nullptr; |
| 500 | f.timestamp_writer = nullptr; |
| 501 | f.contents_writer = nullptr; |
| 502 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 503 | |
| 504 | log_event_uuid_ = UUID::Zero(); |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 505 | log_start_uuid_ = std::nullopt; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 506 | |
| 507 | return std::move(log_namer_); |
| 508 | } |
| 509 | |
| 510 | void Logger::WriteHeader() { |
| 511 | if (configuration::MultiNode(configuration_)) { |
| 512 | server_statistics_fetcher_.Fetch(); |
| 513 | } |
| 514 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 515 | const aos::monotonic_clock::time_point monotonic_start_time = |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 516 | event_loop_->monotonic_now(); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 517 | const aos::realtime_clock::time_point realtime_start_time = |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 518 | event_loop_->realtime_now(); |
| 519 | |
| 520 | // We need to pick a point in time to declare the log file "started". This |
| 521 | // starts here. It needs to be after everything is fetched so that the |
| 522 | // fetchers are all pointed at the most recent message before the start |
| 523 | // time. |
| 524 | last_synchronized_time_ = monotonic_start_time; |
| 525 | |
| 526 | for (const Node *node : log_namer_->nodes()) { |
| 527 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
| 528 | MaybeUpdateTimestamp(node, node_index, monotonic_start_time, |
| 529 | realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 533 | void Logger::WriteMissingTimestamps() { |
| 534 | if (configuration::MultiNode(configuration_)) { |
| 535 | server_statistics_fetcher_.Fetch(); |
| 536 | } else { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | if (server_statistics_fetcher_.get() == nullptr) { |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | for (const Node *node : log_namer_->nodes()) { |
| 545 | const int node_index = configuration::GetNodeIndex(configuration_, node); |
| 546 | if (MaybeUpdateTimestamp( |
| 547 | node, node_index, |
| 548 | server_statistics_fetcher_.context().monotonic_event_time, |
| 549 | server_statistics_fetcher_.context().realtime_event_time)) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 550 | VLOG(1) << "Timestamps changed on " << aos::FlatbufferToJson(node); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 555 | bool Logger::MaybeUpdateTimestamp( |
| 556 | const Node *node, int node_index, |
| 557 | aos::monotonic_clock::time_point monotonic_start_time, |
| 558 | aos::realtime_clock::time_point realtime_start_time) { |
| 559 | // Bail early if the start times are already set. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 560 | if (node_ == node || !configuration::MultiNode(configuration_)) { |
| 561 | if (log_namer_->monotonic_start_time(node_index, |
| 562 | event_loop_->boot_uuid()) != |
| 563 | monotonic_clock::min_time) { |
| 564 | return false; |
| 565 | } |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 566 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 567 | // There are no offsets to compute for ourself, so always succeed. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 568 | log_namer_->SetStartTimes(node_index, event_loop_->boot_uuid(), |
| 569 | monotonic_start_time, realtime_start_time, |
| 570 | monotonic_start_time, realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 571 | return true; |
| 572 | } else if (server_statistics_fetcher_.get() != nullptr) { |
| 573 | // We must be a remote node now. Look for the connection and see if it is |
| 574 | // connected. |
James Kuszmaul | 17607fb | 2021-10-15 20:00:32 -0700 | [diff] [blame] | 575 | CHECK(server_statistics_fetcher_->has_connections()); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 576 | |
| 577 | for (const message_bridge::ServerConnection *connection : |
| 578 | *server_statistics_fetcher_->connections()) { |
| 579 | if (connection->node()->name()->string_view() != |
| 580 | node->name()->string_view()) { |
| 581 | continue; |
| 582 | } |
| 583 | |
| 584 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 585 | VLOG(1) << node->name()->string_view() |
| 586 | << " is not connected, can't start it yet."; |
| 587 | break; |
| 588 | } |
| 589 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 590 | if (!connection->has_monotonic_offset()) { |
| 591 | VLOG(1) << "Missing monotonic offset for setting start time for node " |
| 592 | << aos::FlatbufferToJson(node); |
| 593 | break; |
| 594 | } |
| 595 | |
James Kuszmaul | 17607fb | 2021-10-15 20:00:32 -0700 | [diff] [blame] | 596 | CHECK(connection->has_boot_uuid()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 597 | const UUID boot_uuid = |
| 598 | UUID::FromString(connection->boot_uuid()->string_view()); |
| 599 | |
| 600 | if (log_namer_->monotonic_start_time(node_index, boot_uuid) != |
| 601 | monotonic_clock::min_time) { |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | VLOG(1) << "Updating start time for " |
| 606 | << aos::FlatbufferToJson(connection); |
| 607 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 608 | // Found it and it is connected. Compensate and go. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 609 | log_namer_->SetStartTimes( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 610 | node_index, boot_uuid, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 611 | monotonic_start_time + |
| 612 | std::chrono::nanoseconds(connection->monotonic_offset()), |
| 613 | realtime_start_time, monotonic_start_time, realtime_start_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | } |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader( |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 621 | std::string_view config_sha256) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 622 | flatbuffers::FlatBufferBuilder fbb; |
| 623 | fbb.ForceDefaults(true); |
| 624 | |
| 625 | flatbuffers::Offset<aos::Configuration> configuration_offset; |
| 626 | if (!separate_config_) { |
| 627 | configuration_offset = CopyFlatBuffer(configuration_, &fbb); |
| 628 | } else { |
| 629 | CHECK(!config_sha256.empty()); |
| 630 | } |
| 631 | |
| 632 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
| 633 | fbb.CreateString(name_); |
| 634 | |
| 635 | CHECK(log_event_uuid_ != UUID::Zero()); |
| 636 | const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 637 | log_event_uuid_.PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 638 | |
| 639 | const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 640 | logger_instance_uuid_.PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 641 | |
| 642 | flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset; |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 643 | if (log_start_uuid_) { |
| 644 | log_start_uuid_offset = fbb.CreateString(log_start_uuid_->ToString()); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset; |
| 648 | if (!config_sha256.empty()) { |
| 649 | config_sha256_offset = fbb.CreateString(config_sha256); |
| 650 | } |
| 651 | |
| 652 | const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 653 | event_loop_->boot_uuid().PackString(&fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 654 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 655 | flatbuffers::Offset<Node> logger_node_offset; |
| 656 | |
| 657 | if (configuration::MultiNode(configuration_)) { |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 658 | logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 662 | |
| 663 | log_file_header_builder.add_name(name_offset); |
| 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(); |
| 733 | flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size + |
| 734 | max_header_size_); |
| 735 | fbb.ForceDefaults(true); |
| 736 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 737 | fbb.FinishSizePrefixed( |
| 738 | PackMessage(&fbb, f.fetcher->context(), f.channel_index, f.log_type)); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 739 | const auto end = event_loop_->monotonic_now(); |
| 740 | RecordCreateMessageTime(start, end, f); |
| 741 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 742 | max_header_size_ = |
| 743 | std::max(max_header_size_, fbb.GetSize() - f.fetcher->context().size); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 744 | writer->QueueMessage(&fbb, source_node_boot_uuid, end); |
| 745 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 746 | VLOG(2) << "Wrote data as node " << FlatbufferToJson(node_) |
| 747 | << " for channel " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 748 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 749 | << " to " << writer->filename() << " data " |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 750 | << FlatbufferToJson(flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 751 | fbb.GetBufferPointer())); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 755 | void Logger::WriteTimestamps(NewDataWriter *timestamp_writer, |
| 756 | const FetcherStruct &f) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 757 | if (timestamp_writer != nullptr) { |
| 758 | // And now handle timestamps. |
| 759 | const auto start = event_loop_->monotonic_now(); |
| 760 | flatbuffers::FlatBufferBuilder fbb; |
| 761 | fbb.ForceDefaults(true); |
| 762 | |
| 763 | fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(), |
| 764 | f.channel_index, |
| 765 | LogType::kLogDeliveryTimeOnly)); |
| 766 | const auto end = event_loop_->monotonic_now(); |
| 767 | RecordCreateMessageTime(start, end, f); |
| 768 | |
| 769 | // Tell our writer that we know something about the remote boot. |
| 770 | timestamp_writer->UpdateRemote( |
| 771 | f.data_node_index, f.fetcher->context().source_boot_uuid, |
| 772 | f.fetcher->context().monotonic_remote_time, |
| 773 | f.fetcher->context().monotonic_event_time, f.reliable_forwarding); |
| 774 | timestamp_writer->QueueMessage(&fbb, event_loop_->boot_uuid(), end); |
| 775 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 776 | VLOG(2) << "Wrote timestamps as node " << FlatbufferToJson(node_) |
| 777 | << " for channel " |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 778 | << configuration::CleanedChannelToString(f.fetcher->channel()) |
| 779 | << " to " << timestamp_writer->filename() << " timestamp " |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 780 | << FlatbufferToJson(flatbuffers::GetSizePrefixedRoot<MessageHeader>( |
| 781 | fbb.GetBufferPointer())); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 785 | void Logger::WriteContent(NewDataWriter *contents_writer, |
| 786 | const FetcherStruct &f) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 787 | if (contents_writer != nullptr) { |
| 788 | const auto start = event_loop_->monotonic_now(); |
| 789 | // And now handle the special message contents channel. Copy the |
| 790 | // message into a FlatBufferBuilder and save it to disk. |
| 791 | // TODO(austin): We can be more efficient here when we start to |
| 792 | // care... |
| 793 | flatbuffers::FlatBufferBuilder fbb; |
| 794 | fbb.ForceDefaults(true); |
| 795 | |
| 796 | const RemoteMessage *msg = |
| 797 | flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data); |
| 798 | |
| 799 | CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg); |
| 800 | |
| 801 | logger::MessageHeader::Builder message_header_builder(fbb); |
| 802 | |
| 803 | // TODO(austin): This needs to check the channel_index and confirm |
| 804 | // that it should be logged before squirreling away the timestamp to |
| 805 | // disk. We don't want to log irrelevant timestamps. |
| 806 | |
| 807 | // Note: this must match the same order as MessageBridgeServer and |
| 808 | // PackMessage. We want identical headers to have identical |
| 809 | // on-the-wire formats to make comparing them easier. |
| 810 | |
| 811 | // Translate from the channel index that the event loop uses to the |
| 812 | // channel index in the log file. |
| 813 | message_header_builder.add_channel_index( |
| 814 | event_loop_to_logged_channel_index_[msg->channel_index()]); |
| 815 | |
| 816 | message_header_builder.add_queue_index(msg->queue_index()); |
| 817 | message_header_builder.add_monotonic_sent_time(msg->monotonic_sent_time()); |
| 818 | message_header_builder.add_realtime_sent_time(msg->realtime_sent_time()); |
| 819 | |
| 820 | message_header_builder.add_monotonic_remote_time( |
| 821 | msg->monotonic_remote_time()); |
| 822 | message_header_builder.add_realtime_remote_time( |
| 823 | msg->realtime_remote_time()); |
| 824 | message_header_builder.add_remote_queue_index(msg->remote_queue_index()); |
| 825 | |
| 826 | const aos::monotonic_clock::time_point monotonic_timestamp_time = |
| 827 | f.fetcher->context().monotonic_event_time; |
| 828 | message_header_builder.add_monotonic_timestamp_time( |
| 829 | monotonic_timestamp_time.time_since_epoch().count()); |
| 830 | |
| 831 | fbb.FinishSizePrefixed(message_header_builder.Finish()); |
| 832 | const auto end = event_loop_->monotonic_now(); |
| 833 | RecordCreateMessageTime(start, end, f); |
| 834 | |
| 835 | // Timestamps tell us information about what happened too! |
| 836 | // Capture any reboots so UpdateRemote is properly recorded. |
| 837 | contents_writer->UpdateBoot(UUID::FromVector(msg->boot_uuid())); |
| 838 | |
| 839 | // Start with recording info about the data flowing from our node to the |
| 840 | // remote. |
| 841 | const bool reliable = |
| 842 | f.channel_reliable_contents.size() != 0u |
| 843 | ? f.channel_reliable_contents[msg->channel_index()] |
| 844 | : f.reliable_contents; |
| 845 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 846 | contents_writer->UpdateRemote( |
| 847 | node_index_, event_loop_->boot_uuid(), |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 848 | monotonic_clock::time_point( |
| 849 | chrono::nanoseconds(msg->monotonic_remote_time())), |
| 850 | monotonic_clock::time_point( |
| 851 | chrono::nanoseconds(msg->monotonic_sent_time())), |
| 852 | reliable, monotonic_timestamp_time); |
| 853 | |
Brian Smartt | 796cca0 | 2022-04-12 15:07:21 -0700 | [diff] [blame] | 854 | contents_writer->QueueMessage(&fbb, UUID::FromVector(msg->boot_uuid()), |
| 855 | end); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | void Logger::WriteFetchedRecord(FetcherStruct &f) { |
| 860 | WriteData(f.writer, f); |
| 861 | WriteTimestamps(f.timestamp_writer, f); |
| 862 | WriteContent(f.contents_writer, f); |
| 863 | } |
| 864 | |
| 865 | bool Logger::LogUntil(monotonic_clock::time_point t) { |
| 866 | bool has_pending_messages = false; |
| 867 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 868 | // Grab the latest ServerStatistics message. This will always have the |
| 869 | // oppertunity to be >= to the current time, so it will always represent any |
| 870 | // reboots which may have happened. |
| 871 | WriteMissingTimestamps(); |
| 872 | |
| 873 | // Write each channel to disk, one at a time. |
| 874 | for (FetcherStruct &f : fetchers_) { |
| 875 | while (true) { |
| 876 | if (f.written) { |
| 877 | const auto start = event_loop_->monotonic_now(); |
| 878 | const bool got_new = f.fetcher->FetchNext(); |
| 879 | const auto end = event_loop_->monotonic_now(); |
| 880 | RecordFetchResult(start, end, got_new, &f); |
| 881 | if (!got_new) { |
| 882 | VLOG(2) << "No new data on " |
| 883 | << configuration::CleanedChannelToString( |
| 884 | f.fetcher->channel()); |
| 885 | break; |
| 886 | } |
| 887 | f.written = false; |
| 888 | } |
| 889 | |
| 890 | // TODO(james): Write tests to exercise this logic. |
| 891 | if (f.fetcher->context().monotonic_event_time >= t) { |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 892 | has_pending_messages = true; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 893 | break; |
| 894 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 895 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 896 | WriteFetchedRecord(f); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 897 | |
| 898 | f.written = true; |
| 899 | } |
| 900 | } |
| 901 | last_synchronized_time_ = t; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 902 | |
| 903 | return has_pending_messages; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 906 | void Logger::DoLogData(const monotonic_clock::time_point end_time, |
| 907 | bool run_on_logged) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 908 | // We want to guarantee that messages aren't out of order by more than |
| 909 | // max_out_of_order_duration. To do this, we need sync points. Every write |
| 910 | // cycle should be a sync point. |
| 911 | |
| 912 | do { |
| 913 | // Move the sync point up by at most polling_period. This forces one sync |
| 914 | // per iteration, even if it is small. |
| 915 | LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time)); |
| 916 | |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 917 | if (run_on_logged) { |
| 918 | on_logged_period_(); |
| 919 | } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 920 | |
| 921 | // If we missed cycles, we could be pretty far behind. Spin until we are |
| 922 | // caught up. |
| 923 | } while (last_synchronized_time_ + polling_period_ < end_time); |
| 924 | } |
| 925 | |
| 926 | void Logger::RecordFetchResult(aos::monotonic_clock::time_point start, |
| 927 | aos::monotonic_clock::time_point end, |
| 928 | bool got_new, FetcherStruct *fetcher) { |
| 929 | const auto duration = end - start; |
| 930 | if (!got_new) { |
| 931 | ++total_nop_fetch_count_; |
| 932 | total_nop_fetch_time_ += duration; |
| 933 | return; |
| 934 | } |
| 935 | ++total_message_fetch_count_; |
| 936 | total_message_fetch_bytes_ += fetcher->fetcher->context().size; |
| 937 | total_message_fetch_time_ += duration; |
| 938 | if (duration > max_message_fetch_time_) { |
| 939 | max_message_fetch_time_ = duration; |
| 940 | max_message_fetch_time_channel_ = fetcher->channel_index; |
| 941 | max_message_fetch_time_size_ = fetcher->fetcher->context().size; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 946 | aos::monotonic_clock::time_point end, |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 947 | const FetcherStruct &fetcher) { |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 948 | const auto duration = end - start; |
| 949 | total_copy_time_ += duration; |
| 950 | ++total_copy_count_; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 951 | total_copy_bytes_ += fetcher.fetcher->context().size; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 952 | if (duration > max_copy_time_) { |
| 953 | max_copy_time_ = duration; |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 954 | max_copy_time_channel_ = fetcher.channel_index; |
| 955 | max_copy_time_size_ = fetcher.fetcher->context().size; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | } // namespace logger |
| 960 | } // namespace aos |