blob: 56d0c4f8f4384693181b124d2e22550b26a202ca [file] [log] [blame]
Austin Schuhb06f03b2021-02-17 22:00:37 -08001#include "aos/events/logging/log_writer.h"
2
Austin Schuh6bb8a822021-03-31 23:04:39 -07003#include <dirent.h>
4
Austin Schuhb06f03b2021-02-17 22:00:37 -08005#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 Schuh61e973f2021-02-21 21:43:56 -080013#include "aos/network/timestamp_channel.h"
Austin Schuhb0e439d2023-05-15 10:55:40 -070014#include "aos/sha256.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -080015
16namespace aos {
17namespace logger {
18namespace {
19using message_bridge::RemoteMessage;
Austin Schuhbd06ae42021-03-31 22:48:21 -070020namespace chrono = std::chrono;
Austin Schuhb06f03b2021-02-17 22:00:37 -080021} // namespace
22
23Logger::Logger(EventLoop *event_loop, const Configuration *configuration,
24 std::function<bool(const Channel *)> should_log)
25 : event_loop_(event_loop),
26 configuration_(configuration),
Austin Schuh5b728b72021-06-16 14:57:15 -070027 node_(configuration::GetNode(configuration_, event_loop->node())),
28 node_index_(configuration::GetNodeIndex(configuration_, node_)),
Austin Schuhb06f03b2021-02-17 22:00:37 -080029 name_(network::GetHostname()),
Austin Schuh2d612c82023-07-17 13:37:48 -070030 timer_handler_(event_loop_->AddTimer([this]() {
31 DoLogData(event_loop_->monotonic_now() - logging_delay_, true);
32 })),
Austin Schuhb06f03b2021-02-17 22:00:37 -080033 server_statistics_fetcher_(
34 configuration::MultiNode(event_loop_->configuration())
35 ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>(
36 "/aos")
37 : aos::Fetcher<message_bridge::ServerStatistics>()) {
Austin Schuh58646e22021-08-23 23:51:46 -070038 timer_handler_->set_name("channel_poll");
Austin Schuh5b728b72021-06-16 14:57:15 -070039 VLOG(1) << "Creating logger for " << FlatbufferToJson(node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -080040
Naman Gupta41d70c22022-11-21 15:29:52 -080041 // When we are logging remote timestamps, we need to be able to translate
42 // from the channel index that the event loop uses to the channel index in
43 // the config in the log file.
Austin Schuh01f3b392022-01-25 20:03:09 -080044 event_loop_to_logged_channel_index_.resize(
45 event_loop->configuration()->channels()->size(), -1);
46 for (size_t event_loop_channel_index = 0;
47 event_loop_channel_index <
48 event_loop->configuration()->channels()->size();
49 ++event_loop_channel_index) {
50 const Channel *event_loop_channel =
51 event_loop->configuration()->channels()->Get(event_loop_channel_index);
52
53 const Channel *logged_channel = aos::configuration::GetChannel(
54 configuration_, event_loop_channel->name()->string_view(),
55 event_loop_channel->type()->string_view(), "", node_);
56
57 if (logged_channel != nullptr) {
58 event_loop_to_logged_channel_index_[event_loop_channel_index] =
59 configuration::ChannelIndex(configuration_, logged_channel);
60 }
61 }
62
63 // Map to match source channels with the timestamp logger, if the contents
64 // should be reliable, and a list of all channels logged on it to be treated
65 // as reliable.
66 std::map<const Channel *, std::tuple<const Node *, bool, std::vector<bool>>>
67 timestamp_logger_channels;
Austin Schuhb06f03b2021-02-17 22:00:37 -080068
Austin Schuh61e973f2021-02-21 21:43:56 -080069 message_bridge::ChannelTimestampFinder finder(event_loop_);
70 for (const Channel *channel : *event_loop_->configuration()->channels()) {
71 if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node())) {
Austin Schuhb06f03b2021-02-17 22:00:37 -080072 continue;
73 }
Austin Schuh61e973f2021-02-21 21:43:56 -080074 if (!channel->has_destination_nodes()) {
75 continue;
76 }
Austin Schuh01f3b392022-01-25 20:03:09 -080077 const size_t channel_index =
78 configuration::ChannelIndex(event_loop_->configuration(), channel);
79
Austin Schuh61e973f2021-02-21 21:43:56 -080080 for (const Connection *connection : *channel->destination_nodes()) {
81 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(
82 connection, event_loop_->node())) {
83 const Node *other_node = configuration::GetNode(
Austin Schuh5b728b72021-06-16 14:57:15 -070084 configuration_, connection->name()->string_view());
Austin Schuh61e973f2021-02-21 21:43:56 -080085
86 VLOG(1) << "Timestamps are logged from "
87 << FlatbufferToJson(other_node);
Austin Schuh01f3b392022-01-25 20:03:09 -080088 // True if each channel's remote timestamps are split into a separate
89 // RemoteMessage channel.
90 const bool is_split =
91 finder.SplitChannelForChannel(channel, connection) != nullptr;
92
93 const Channel *const timestamp_logger_channel =
94 finder.ForChannel(channel, connection);
95
96 auto it = timestamp_logger_channels.find(timestamp_logger_channel);
97 if (it != timestamp_logger_channels.end()) {
98 CHECK(!is_split);
99 CHECK_LT(channel_index, std::get<2>(it->second).size());
Brian Smartt796cca02022-04-12 15:07:21 -0700100 std::get<2>(it->second)[channel_index] =
101 (connection->time_to_live() == 0);
Austin Schuh01f3b392022-01-25 20:03:09 -0800102 } else {
103 if (is_split) {
104 timestamp_logger_channels.insert(std::make_pair(
105 timestamp_logger_channel,
106 std::make_tuple(other_node, (connection->time_to_live() == 0),
107 std::vector<bool>())));
108 } else {
109 std::vector<bool> channel_reliable_contents(
110 event_loop->configuration()->channels()->size(), false);
111 channel_reliable_contents[channel_index] =
112 (connection->time_to_live() == 0);
113
114 timestamp_logger_channels.insert(std::make_pair(
115 timestamp_logger_channel,
116 std::make_tuple(other_node, false,
117 std::move(channel_reliable_contents))));
118 }
119 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800120 }
121 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800122 }
123
Austin Schuhb06f03b2021-02-17 22:00:37 -0800124 for (size_t channel_index = 0;
125 channel_index < configuration_->channels()->size(); ++channel_index) {
126 const Channel *const config_channel =
127 configuration_->channels()->Get(channel_index);
128 // The MakeRawFetcher method needs a channel which is in the event loop
129 // configuration() object, not the configuration_ object. Go look that up
130 // from the config.
131 const Channel *channel = aos::configuration::GetChannel(
132 event_loop_->configuration(), config_channel->name()->string_view(),
133 config_channel->type()->string_view(), "", event_loop_->node());
134 CHECK(channel != nullptr)
135 << ": Failed to look up channel "
136 << aos::configuration::CleanedChannelToString(config_channel);
Austin Schuh5b728b72021-06-16 14:57:15 -0700137 if (!should_log(config_channel)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800138 continue;
139 }
140
141 FetcherStruct fs;
142 fs.channel_index = channel_index;
143 fs.channel = channel;
144
145 const bool is_local =
Austin Schuh5b728b72021-06-16 14:57:15 -0700146 configuration::ChannelIsSendableOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800147
148 const bool is_readable =
Austin Schuh5b728b72021-06-16 14:57:15 -0700149 configuration::ChannelIsReadableOnNode(config_channel, node_);
Austin Schuh01f3b392022-01-25 20:03:09 -0800150 const bool is_logged =
151 configuration::ChannelMessageIsLoggedOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800152 const bool log_message = is_logged && is_readable;
153
154 bool log_delivery_times = false;
Austin Schuh5b728b72021-06-16 14:57:15 -0700155 if (configuration::MultiNode(configuration_)) {
Austin Schuh72211ae2021-08-05 14:02:30 -0700156 const aos::Connection *connection =
Austin Schuh5b728b72021-06-16 14:57:15 -0700157 configuration::ConnectionToNode(config_channel, node_);
Austin Schuh72211ae2021-08-05 14:02:30 -0700158
Austin Schuhb06f03b2021-02-17 22:00:37 -0800159 log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh72211ae2021-08-05 14:02:30 -0700160 connection, event_loop_->node());
161
162 CHECK_EQ(log_delivery_times,
163 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh5b728b72021-06-16 14:57:15 -0700164 config_channel, node_, node_));
Austin Schuh72211ae2021-08-05 14:02:30 -0700165
166 if (connection) {
167 fs.reliable_forwarding = (connection->time_to_live() == 0);
168 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800169 }
170
Austin Schuh01f3b392022-01-25 20:03:09 -0800171 // Now, detect a RemoteMessage timestamp logger where we should just log
172 // the contents to a file directly.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800173 const bool log_contents = timestamp_logger_channels.find(channel) !=
174 timestamp_logger_channels.end();
175
176 if (log_message || log_delivery_times || log_contents) {
177 fs.fetcher = event_loop->MakeRawFetcher(channel);
178 VLOG(1) << "Logging channel "
179 << configuration::CleanedChannelToString(channel);
180
181 if (log_delivery_times) {
182 VLOG(1) << " Delivery times";
183 fs.wants_timestamp_writer = true;
Austin Schuh5b728b72021-06-16 14:57:15 -0700184 fs.timestamp_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800185 }
Austin Schuhe46492f2021-07-31 19:49:41 -0700186 // Both the timestamp and data writers want data_node_index so it knows
187 // what the source node is.
188 if (log_message || log_delivery_times) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800189 if (!is_local) {
190 const Node *source_node = configuration::GetNode(
191 configuration_, channel->source_node()->string_view());
192 fs.data_node_index =
193 configuration::GetNodeIndex(configuration_, source_node);
Austin Schuhe46492f2021-07-31 19:49:41 -0700194 }
195 }
196 if (log_message) {
197 VLOG(1) << " Data";
198 fs.wants_writer = true;
199 if (!is_local) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800200 fs.log_type = LogType::kLogRemoteMessage;
201 } else {
Austin Schuh5b728b72021-06-16 14:57:15 -0700202 fs.data_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800203 }
204 }
205 if (log_contents) {
206 VLOG(1) << "Timestamp logger channel "
207 << configuration::CleanedChannelToString(channel);
Austin Schuh01f3b392022-01-25 20:03:09 -0800208 auto timestamp_logger_channel_info =
209 timestamp_logger_channels.find(channel);
210 CHECK(timestamp_logger_channel_info != timestamp_logger_channels.end());
211 fs.timestamp_node = std::get<0>(timestamp_logger_channel_info->second);
212 fs.reliable_contents =
213 std::get<1>(timestamp_logger_channel_info->second);
214 fs.channel_reliable_contents =
215 std::get<2>(timestamp_logger_channel_info->second);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800216 fs.wants_contents_writer = true;
217 fs.contents_node_index =
218 configuration::GetNodeIndex(configuration_, fs.timestamp_node);
219 }
220 fetchers_.emplace_back(std::move(fs));
221 }
222 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800223}
224
225Logger::~Logger() {
226 if (log_namer_) {
227 // If we are replaying a log file, or in simulation, we want to force the
228 // last bit of data to be logged. The easiest way to deal with this is to
Austin Schuh01f3b392022-01-25 20:03:09 -0800229 // poll everything as we go to destroy the class, ie, shut down the
230 // logger, and write it to disk.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800231 StopLogging(event_loop_->monotonic_now());
232 }
233}
234
Austin Schuh3b2b5b52023-07-05 11:36:46 -0700235aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> PackConfiguration(
236 const Configuration *const configuration) {
237 flatbuffers::FlatBufferBuilder fbb;
238 flatbuffers::Offset<aos::Configuration> configuration_offset =
239 CopyFlatBuffer(configuration, &fbb);
240 LogFileHeader::Builder log_file_header_builder(fbb);
241 log_file_header_builder.add_configuration(configuration_offset);
242 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
243 return fbb.Release();
244}
245
Brian Smartt796cca02022-04-12 15:07:21 -0700246std::string Logger::WriteConfiguration(LogNamer *log_namer) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800247 std::string config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800248
Austin Schuhb06f03b2021-02-17 22:00:37 -0800249 if (separate_config_) {
Austin Schuh3b2b5b52023-07-05 11:36:46 -0700250 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header =
251 PackConfiguration(configuration_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800252 config_sha256 = Sha256(config_header.span());
Alexei Strots6bd1e642023-07-20 19:36:27 -0700253 VLOG(1) << "Config sha256 of " << config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800254 log_namer->WriteConfiguration(&config_header, config_sha256);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800255 }
256
Brian Smartt03c00da2022-02-24 10:25:00 -0800257 return config_sha256;
258}
259
260void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer,
261 std::optional<UUID> log_start_uuid) {
262 CHECK(!log_namer_) << ": Already logging";
263
264 VLOG(1) << "Starting logger for " << FlatbufferToJson(node_);
265
266 auto config_sha256 = WriteConfiguration(log_namer.get());
267
268 log_namer_ = std::move(log_namer);
269
Austin Schuhb06f03b2021-02-17 22:00:37 -0800270 log_event_uuid_ = UUID::Random();
271 log_start_uuid_ = log_start_uuid;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800272
273 // We want to do as much work as possible before the initial Fetch. Time
274 // between that and actually starting to log opens up the possibility of
275 // falling off the end of the queue during that time.
276
277 for (FetcherStruct &f : fetchers_) {
278 if (f.wants_writer) {
279 f.writer = log_namer_->MakeWriter(f.channel);
280 }
281 if (f.wants_timestamp_writer) {
282 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
283 }
284 if (f.wants_contents_writer) {
285 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
286 f.channel, CHECK_NOTNULL(f.timestamp_node));
287 }
288 }
289
Austin Schuh73340842021-07-30 22:32:06 -0700290 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
Austin Schuhb06f03b2021-02-17 22:00:37 -0800291
Austin Schuha42ee962021-03-31 22:49:30 -0700292 const aos::monotonic_clock::time_point beginning_time =
293 event_loop_->monotonic_now();
294
Austin Schuh2d612c82023-07-17 13:37:48 -0700295 log_until_time_ = beginning_time;
296
Austin Schuhb06f03b2021-02-17 22:00:37 -0800297 // Grab data from each channel right before we declare the log file started
298 // so we can capture the latest message on each channel. This lets us have
299 // non periodic messages with configuration that now get logged.
300 for (FetcherStruct &f : fetchers_) {
301 const auto start = event_loop_->monotonic_now();
302 const bool got_new = f.fetcher->Fetch();
303 const auto end = event_loop_->monotonic_now();
304 RecordFetchResult(start, end, got_new, &f);
305
306 // If there is a message, we want to write it.
307 f.written = f.fetcher->context().data == nullptr;
308 }
309
310 // Clear out any old timestamps in case we are re-starting logging.
Austin Schuh572924a2021-07-30 22:32:12 -0700311 for (size_t i = 0; i < configuration::NodesCount(configuration_); ++i) {
Austin Schuh58646e22021-08-23 23:51:46 -0700312 log_namer_->ClearStartTimes();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800313 }
314
Austin Schuha42ee962021-03-31 22:49:30 -0700315 const aos::monotonic_clock::time_point fetch_time =
316 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800317 WriteHeader();
Austin Schuha42ee962021-03-31 22:49:30 -0700318 const aos::monotonic_clock::time_point header_time =
319 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800320
Brian Smartt796cca02022-04-12 15:07:21 -0700321 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " start_time "
322 << last_synchronized_time_ << ", took "
Brian Smartt03c00da2022-02-24 10:25:00 -0800323 << chrono::duration<double>(fetch_time - beginning_time).count()
324 << " to fetch, "
325 << chrono::duration<double>(header_time - fetch_time).count()
326 << " to write headers, boot uuid " << event_loop_->boot_uuid();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800327
328 // Force logging up until the start of the log file now, so the messages at
329 // the start are always ordered before the rest of the messages.
330 // Note: this ship may have already sailed, but we don't have to make it
331 // worse.
332 // TODO(austin): Test...
Austin Schuh855f8932021-03-19 22:01:32 -0700333 //
Naman Gupta41d70c22022-11-21 15:29:52 -0800334 // This is safe to call here since we have set last_synchronized_time_ as
335 // the same time as in the header, and all the data before it should be
336 // logged without ordering concerns.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800337 LogUntil(last_synchronized_time_);
338
Philipp Schradera6712522023-07-05 20:25:11 -0700339 timer_handler_->Schedule(event_loop_->monotonic_now() + polling_period_,
340 polling_period_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800341}
342
Brian Smartt796cca02022-04-12 15:07:21 -0700343std::unique_ptr<LogNamer> Logger::RestartLogging(
Austin Schuh2d612c82023-07-17 13:37:48 -0700344 std::unique_ptr<LogNamer> log_namer, std::optional<UUID> log_start_uuid,
345 std::optional<monotonic_clock::time_point> end_time) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800346 CHECK(log_namer_) << ": Unexpected restart while not logging";
347
348 VLOG(1) << "Restarting logger for " << FlatbufferToJson(node_);
349
Austin Schuh2d612c82023-07-17 13:37:48 -0700350 // Grab a representative time on both the RT and monotonic clock.
351 // Average a monotonic clock before and after to reduce the error.
352 const aos::monotonic_clock::time_point monotonic_now1 =
353 event_loop_->monotonic_now();
354 const aos::realtime_clock::time_point realtime_now =
355 event_loop_->realtime_now();
356 const aos::monotonic_clock::time_point monotonic_now2 =
357 event_loop_->monotonic_now();
Austin Schuh08dba8f2023-05-01 08:29:30 -0700358
Austin Schuh2d612c82023-07-17 13:37:48 -0700359 // Log until the provided end time.
360 if (end_time) {
361 CHECK_LE(*end_time, monotonic_now1) << ": Can't log into the future.";
362 // DoLogData is a bit fragile.
363 if (*end_time > last_synchronized_time_) {
364 DoLogData(*end_time, false);
Austin Schuh08dba8f2023-05-01 08:29:30 -0700365 }
366 }
367
Austin Schuh2d612c82023-07-17 13:37:48 -0700368 // We are now synchronized up to last_synchronized_time_. We only have record
369 // of messages from before last_synchronized_time_, so it is a safe start
370 // time.
Brian Smartt03c00da2022-02-24 10:25:00 -0800371
372 std::unique_ptr<LogNamer> old_log_namer = std::move(log_namer_);
373 log_namer_ = std::move(log_namer);
374
Austin Schuh2d612c82023-07-17 13:37:48 -0700375 // Our start time is now how far we logged until before.
376 const aos::monotonic_clock::time_point monotonic_start_time =
377 last_synchronized_time_;
Austin Schuh41f8df92022-04-15 11:45:52 -0700378 const aos::realtime_clock::time_point realtime_start_time =
Austin Schuh2d612c82023-07-17 13:37:48 -0700379 (realtime_now + (last_synchronized_time_.time_since_epoch() -
380 ((monotonic_now1.time_since_epoch() +
381 monotonic_now2.time_since_epoch()) /
382 2)));
Brian Smartt03c00da2022-02-24 10:25:00 -0800383
384 auto config_sha256 = WriteConfiguration(log_namer_.get());
385
386 log_event_uuid_ = UUID::Random();
387 log_start_uuid_ = log_start_uuid;
388
389 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
390
391 // Note that WriteHeader updates last_synchronized_time_ to be the
392 // current time when it is called, which is then the "start time"
393 // of the new (restarted) log. This timestamp will be after
Naman Gupta41d70c22022-11-21 15:29:52 -0800394 // the timestamp of the last message fetched on each channel, but is
395 // carefully picked per the comment above to not violate
396 // max_out_of_order_duration.
Austin Schuh41f8df92022-04-15 11:45:52 -0700397 WriteHeader(monotonic_start_time, realtime_start_time);
Brian Smartt03c00da2022-02-24 10:25:00 -0800398
399 const aos::monotonic_clock::time_point header_time =
400 event_loop_->monotonic_now();
401
Austin Schuh08dba8f2023-05-01 08:29:30 -0700402 // Close out the old writers to free up memory to be used by the new writers.
403 old_log_namer->Close();
404
Brian Smartt03c00da2022-02-24 10:25:00 -0800405 for (FetcherStruct &f : fetchers_) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800406 // Create writers from the new namer
Brian Smartt03c00da2022-02-24 10:25:00 -0800407
408 if (f.wants_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700409 f.writer = log_namer_->MakeWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800410 }
411 if (f.wants_timestamp_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700412 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800413 }
414 if (f.wants_contents_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700415 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
Brian Smartt03c00da2022-02-24 10:25:00 -0800416 f.channel, CHECK_NOTNULL(f.timestamp_node));
417 }
418
Austin Schuh08dba8f2023-05-01 08:29:30 -0700419 // Mark each channel with data as not written. That triggers each channel
420 // to be re-logged.
421 f.written = f.fetcher->context().data == nullptr;
Brian Smartt03c00da2022-02-24 10:25:00 -0800422 }
423
Austin Schuh08dba8f2023-05-01 08:29:30 -0700424 // And now make sure to log everything up to the start time in 1 big go so we
425 // make sure we have it before we let the world start logging normally again.
426 LogUntil(monotonic_start_time);
427
Brian Smartt03c00da2022-02-24 10:25:00 -0800428 const aos::monotonic_clock::time_point channel_time =
429 event_loop_->monotonic_now();
430
Brian Smartt796cca02022-04-12 15:07:21 -0700431 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " restart_time "
432 << last_synchronized_time_ << ", took "
Austin Schuh2d612c82023-07-17 13:37:48 -0700433 << chrono::duration<double>(header_time - monotonic_now1).count()
Brian Smartt03c00da2022-02-24 10:25:00 -0800434 << " to prepare and write header, "
435 << chrono::duration<double>(channel_time - header_time).count()
Brian Smartt796cca02022-04-12 15:07:21 -0700436 << " to write initial channel messages, boot uuid "
437 << event_loop_->boot_uuid();
Brian Smartt03c00da2022-02-24 10:25:00 -0800438
439 return old_log_namer;
440}
441
Austin Schuhb06f03b2021-02-17 22:00:37 -0800442std::unique_ptr<LogNamer> Logger::StopLogging(
443 aos::monotonic_clock::time_point end_time) {
444 CHECK(log_namer_) << ": Not logging right now";
445
446 if (end_time != aos::monotonic_clock::min_time) {
Austin Schuh30586902021-03-30 22:54:08 -0700447 // Folks like to use the on_logged_period_ callback to trigger stop and
448 // start events. We can't have those then recurse and try to stop again.
449 // Rather than making everything reentrant, let's just instead block the
450 // callback here.
451 DoLogData(end_time, false);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800452 }
453 timer_handler_->Disable();
454
455 for (FetcherStruct &f : fetchers_) {
456 f.writer = nullptr;
457 f.timestamp_writer = nullptr;
458 f.contents_writer = nullptr;
459 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800460
461 log_event_uuid_ = UUID::Zero();
Austin Schuh34f9e482021-03-31 22:54:18 -0700462 log_start_uuid_ = std::nullopt;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800463
Alexei Strotsbc776d52023-04-15 23:46:45 -0700464 log_namer_->Close();
465
Austin Schuhb06f03b2021-02-17 22:00:37 -0800466 return std::move(log_namer_);
467}
468
Austin Schuh41f8df92022-04-15 11:45:52 -0700469void Logger::WriteHeader(aos::monotonic_clock::time_point monotonic_start_time,
470 aos::realtime_clock::time_point realtime_start_time) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800471 if (configuration::MultiNode(configuration_)) {
472 server_statistics_fetcher_.Fetch();
473 }
474
Austin Schuh41f8df92022-04-15 11:45:52 -0700475 if (monotonic_start_time == aos::monotonic_clock::min_time) {
476 monotonic_start_time = event_loop_->monotonic_now();
477 realtime_start_time = event_loop_->realtime_now();
478 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800479
480 // We need to pick a point in time to declare the log file "started". This
481 // starts here. It needs to be after everything is fetched so that the
482 // fetchers are all pointed at the most recent message before the start
483 // time.
484 last_synchronized_time_ = monotonic_start_time;
485
486 for (const Node *node : log_namer_->nodes()) {
487 const int node_index = configuration::GetNodeIndex(configuration_, node);
488 MaybeUpdateTimestamp(node, node_index, monotonic_start_time,
489 realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800490 }
491}
492
Austin Schuhb06f03b2021-02-17 22:00:37 -0800493void Logger::WriteMissingTimestamps() {
494 if (configuration::MultiNode(configuration_)) {
495 server_statistics_fetcher_.Fetch();
496 } else {
497 return;
498 }
499
500 if (server_statistics_fetcher_.get() == nullptr) {
501 return;
502 }
503
504 for (const Node *node : log_namer_->nodes()) {
505 const int node_index = configuration::GetNodeIndex(configuration_, node);
506 if (MaybeUpdateTimestamp(
507 node, node_index,
508 server_statistics_fetcher_.context().monotonic_event_time,
509 server_statistics_fetcher_.context().realtime_event_time)) {
Austin Schuh58646e22021-08-23 23:51:46 -0700510 VLOG(1) << "Timestamps changed on " << aos::FlatbufferToJson(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800511 }
512 }
513}
514
Austin Schuhb06f03b2021-02-17 22:00:37 -0800515bool Logger::MaybeUpdateTimestamp(
516 const Node *node, int node_index,
517 aos::monotonic_clock::time_point monotonic_start_time,
518 aos::realtime_clock::time_point realtime_start_time) {
519 // Bail early if the start times are already set.
Austin Schuh58646e22021-08-23 23:51:46 -0700520 if (node_ == node || !configuration::MultiNode(configuration_)) {
521 if (log_namer_->monotonic_start_time(node_index,
522 event_loop_->boot_uuid()) !=
523 monotonic_clock::min_time) {
524 return false;
525 }
Brian Smartt03c00da2022-02-24 10:25:00 -0800526
Austin Schuhb06f03b2021-02-17 22:00:37 -0800527 // There are no offsets to compute for ourself, so always succeed.
Austin Schuh58646e22021-08-23 23:51:46 -0700528 log_namer_->SetStartTimes(node_index, event_loop_->boot_uuid(),
529 monotonic_start_time, realtime_start_time,
530 monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800531 return true;
532 } else if (server_statistics_fetcher_.get() != nullptr) {
533 // We must be a remote node now. Look for the connection and see if it is
534 // connected.
James Kuszmaul17607fb2021-10-15 20:00:32 -0700535 CHECK(server_statistics_fetcher_->has_connections());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800536
537 for (const message_bridge::ServerConnection *connection :
538 *server_statistics_fetcher_->connections()) {
539 if (connection->node()->name()->string_view() !=
540 node->name()->string_view()) {
541 continue;
542 }
543
544 if (connection->state() != message_bridge::State::CONNECTED) {
545 VLOG(1) << node->name()->string_view()
546 << " is not connected, can't start it yet.";
547 break;
548 }
549
Austin Schuhb06f03b2021-02-17 22:00:37 -0800550 if (!connection->has_monotonic_offset()) {
551 VLOG(1) << "Missing monotonic offset for setting start time for node "
552 << aos::FlatbufferToJson(node);
553 break;
554 }
555
James Kuszmaul17607fb2021-10-15 20:00:32 -0700556 CHECK(connection->has_boot_uuid());
Austin Schuh58646e22021-08-23 23:51:46 -0700557 const UUID boot_uuid =
558 UUID::FromString(connection->boot_uuid()->string_view());
559
560 if (log_namer_->monotonic_start_time(node_index, boot_uuid) !=
561 monotonic_clock::min_time) {
562 break;
563 }
564
565 VLOG(1) << "Updating start time for "
566 << aos::FlatbufferToJson(connection);
567
Austin Schuhb06f03b2021-02-17 22:00:37 -0800568 // Found it and it is connected. Compensate and go.
Austin Schuh73340842021-07-30 22:32:06 -0700569 log_namer_->SetStartTimes(
Austin Schuh58646e22021-08-23 23:51:46 -0700570 node_index, boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700571 monotonic_start_time +
572 std::chrono::nanoseconds(connection->monotonic_offset()),
573 realtime_start_time, monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800574 return true;
575 }
576 }
577 return false;
578}
579
580aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader(
Austin Schuh73340842021-07-30 22:32:06 -0700581 std::string_view config_sha256) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800582 flatbuffers::FlatBufferBuilder fbb;
583 fbb.ForceDefaults(true);
584
585 flatbuffers::Offset<aos::Configuration> configuration_offset;
586 if (!separate_config_) {
587 configuration_offset = CopyFlatBuffer(configuration_, &fbb);
588 } else {
589 CHECK(!config_sha256.empty());
590 }
591
592 const flatbuffers::Offset<flatbuffers::String> name_offset =
593 fbb.CreateString(name_);
594
Austin Schuhfa712682022-05-11 16:43:42 -0700595 const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset =
596 logger_sha1_.empty() ? 0 : fbb.CreateString(logger_sha1_);
597 const flatbuffers::Offset<flatbuffers::String> logger_version_offset =
598 logger_version_.empty() ? 0 : fbb.CreateString(logger_version_);
599
Austin Schuhb06f03b2021-02-17 22:00:37 -0800600 CHECK(log_event_uuid_ != UUID::Zero());
601 const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800602 log_event_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800603
604 const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800605 logger_instance_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800606
607 flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset;
Austin Schuh34f9e482021-03-31 22:54:18 -0700608 if (log_start_uuid_) {
609 log_start_uuid_offset = fbb.CreateString(log_start_uuid_->ToString());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800610 }
611
612 flatbuffers::Offset<flatbuffers::String> config_sha256_offset;
613 if (!config_sha256.empty()) {
614 config_sha256_offset = fbb.CreateString(config_sha256);
615 }
616
617 const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800618 event_loop_->boot_uuid().PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800619
Austin Schuhb06f03b2021-02-17 22:00:37 -0800620 flatbuffers::Offset<Node> logger_node_offset;
621
622 if (configuration::MultiNode(configuration_)) {
Austin Schuh5b728b72021-06-16 14:57:15 -0700623 logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800624 }
625
626 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
627
628 log_file_header_builder.add_name(name_offset);
Austin Schuhfa712682022-05-11 16:43:42 -0700629 if (!logger_sha1_offset.IsNull()) {
630 log_file_header_builder.add_logger_sha1(logger_sha1_offset);
631 }
632 if (!logger_version_offset.IsNull()) {
633 log_file_header_builder.add_logger_version(logger_version_offset);
634 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800635
636 // Only add the node if we are running in a multinode configuration.
Austin Schuh73340842021-07-30 22:32:06 -0700637 if (configuration::MultiNode(configuration_)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800638 log_file_header_builder.add_logger_node(logger_node_offset);
639 }
640
641 if (!configuration_offset.IsNull()) {
642 log_file_header_builder.add_configuration(configuration_offset);
643 }
644 // The worst case theoretical out of order is the polling period times 2.
645 // One message could get logged right after the boundary, but be for right
646 // before the next boundary. And the reverse could happen for another
647 // message. Report back 3x to be extra safe, and because the cost isn't
648 // huge on the read side.
649 log_file_header_builder.add_max_out_of_order_duration(
650 std::chrono::nanoseconds(3 * polling_period_).count());
651
Austin Schuhb06f03b2021-02-17 22:00:37 -0800652 log_file_header_builder.add_log_event_uuid(log_event_uuid_offset);
653 log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset);
654 if (!log_start_uuid_offset.IsNull()) {
655 log_file_header_builder.add_log_start_uuid(log_start_uuid_offset);
656 }
657 log_file_header_builder.add_logger_node_boot_uuid(
658 logger_node_boot_uuid_offset);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800659
660 if (!config_sha256_offset.IsNull()) {
661 log_file_header_builder.add_configuration_sha256(config_sha256_offset);
662 }
663
664 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
665 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result(
666 fbb.Release());
667
668 CHECK(result.Verify()) << ": Built a corrupted header.";
669
670 return result;
671}
672
673void Logger::ResetStatisics() {
674 max_message_fetch_time_ = std::chrono::nanoseconds::zero();
675 max_message_fetch_time_channel_ = -1;
676 max_message_fetch_time_size_ = -1;
677 total_message_fetch_time_ = std::chrono::nanoseconds::zero();
678 total_message_fetch_count_ = 0;
679 total_message_fetch_bytes_ = 0;
680 total_nop_fetch_time_ = std::chrono::nanoseconds::zero();
681 total_nop_fetch_count_ = 0;
682 max_copy_time_ = std::chrono::nanoseconds::zero();
683 max_copy_time_channel_ = -1;
684 max_copy_time_size_ = -1;
685 total_copy_time_ = std::chrono::nanoseconds::zero();
686 total_copy_count_ = 0;
687 total_copy_bytes_ = 0;
688}
689
690void Logger::Rotate() {
691 for (const Node *node : log_namer_->nodes()) {
Austin Schuh73340842021-07-30 22:32:06 -0700692 log_namer_->Rotate(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800693 }
694}
695
Brian Smartt03c00da2022-02-24 10:25:00 -0800696void Logger::WriteData(NewDataWriter *writer, const FetcherStruct &f) {
697 if (writer != nullptr) {
698 const UUID source_node_boot_uuid =
699 static_cast<int>(node_index_) != f.data_node_index
700 ? f.fetcher->context().source_boot_uuid
701 : event_loop_->boot_uuid();
702 // Write!
703 const auto start = event_loop_->monotonic_now();
Brian Smartt03c00da2022-02-24 10:25:00 -0800704
Naman Gupta41d70c22022-11-21 15:29:52 -0800705 ContextDataCopier coppier(f.fetcher->context(), f.channel_index, f.log_type,
706 event_loop_);
Brian Smartt03c00da2022-02-24 10:25:00 -0800707
Austin Schuh48d10d62022-10-16 22:19:23 -0700708 writer->CopyMessage(&coppier, source_node_boot_uuid, start);
709 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800710
Brian Smartt796cca02022-04-12 15:07:21 -0700711 VLOG(2) << "Wrote data as node " << FlatbufferToJson(node_)
712 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800713 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700714 << " to " << writer->name();
Brian Smartt03c00da2022-02-24 10:25:00 -0800715 }
716}
717
Brian Smartt796cca02022-04-12 15:07:21 -0700718void Logger::WriteTimestamps(NewDataWriter *timestamp_writer,
719 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800720 if (timestamp_writer != nullptr) {
721 // And now handle timestamps.
Brian Smartt03c00da2022-02-24 10:25:00 -0800722
723 // Tell our writer that we know something about the remote boot.
724 timestamp_writer->UpdateRemote(
725 f.data_node_index, f.fetcher->context().source_boot_uuid,
726 f.fetcher->context().monotonic_remote_time,
727 f.fetcher->context().monotonic_event_time, f.reliable_forwarding);
Austin Schuh48d10d62022-10-16 22:19:23 -0700728
729 const auto start = event_loop_->monotonic_now();
730 ContextDataCopier coppier(f.fetcher->context(), f.channel_index,
Naman Gupta41d70c22022-11-21 15:29:52 -0800731 LogType::kLogDeliveryTimeOnly, event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700732
733 timestamp_writer->CopyMessage(&coppier, event_loop_->boot_uuid(), start);
734 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800735
Brian Smartt796cca02022-04-12 15:07:21 -0700736 VLOG(2) << "Wrote timestamps as node " << FlatbufferToJson(node_)
737 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800738 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700739 << " to " << timestamp_writer->name() << " timestamp";
Brian Smartt03c00da2022-02-24 10:25:00 -0800740 }
741}
742
Brian Smartt796cca02022-04-12 15:07:21 -0700743void Logger::WriteContent(NewDataWriter *contents_writer,
744 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800745 if (contents_writer != nullptr) {
746 const auto start = event_loop_->monotonic_now();
747 // And now handle the special message contents channel. Copy the
748 // message into a FlatBufferBuilder and save it to disk.
Brian Smartt03c00da2022-02-24 10:25:00 -0800749 const RemoteMessage *msg =
750 flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data);
751
752 CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg);
Brian Smartt03c00da2022-02-24 10:25:00 -0800753 // Translate from the channel index that the event loop uses to the
754 // channel index in the log file.
Austin Schuhf2d0e682022-10-16 14:20:58 -0700755 const int channel_index =
756 event_loop_to_logged_channel_index_[msg->channel_index()];
Brian Smartt03c00da2022-02-24 10:25:00 -0800757
758 const aos::monotonic_clock::time_point monotonic_timestamp_time =
759 f.fetcher->context().monotonic_event_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800760
Brian Smartt03c00da2022-02-24 10:25:00 -0800761 // Timestamps tell us information about what happened too!
762 // Capture any reboots so UpdateRemote is properly recorded.
763 contents_writer->UpdateBoot(UUID::FromVector(msg->boot_uuid()));
764
765 // Start with recording info about the data flowing from our node to the
766 // remote.
767 const bool reliable =
768 f.channel_reliable_contents.size() != 0u
769 ? f.channel_reliable_contents[msg->channel_index()]
770 : f.reliable_contents;
771
Brian Smartt796cca02022-04-12 15:07:21 -0700772 contents_writer->UpdateRemote(
773 node_index_, event_loop_->boot_uuid(),
Brian Smartt03c00da2022-02-24 10:25:00 -0800774 monotonic_clock::time_point(
775 chrono::nanoseconds(msg->monotonic_remote_time())),
776 monotonic_clock::time_point(
777 chrono::nanoseconds(msg->monotonic_sent_time())),
778 reliable, monotonic_timestamp_time);
779
Austin Schuh48d10d62022-10-16 22:19:23 -0700780 RemoteMessageCopier coppier(msg, channel_index, monotonic_timestamp_time,
Naman Gupta41d70c22022-11-21 15:29:52 -0800781 event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700782
783 contents_writer->CopyMessage(&coppier, UUID::FromVector(msg->boot_uuid()),
784 start);
785
786 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800787 }
788}
789
790void Logger::WriteFetchedRecord(FetcherStruct &f) {
791 WriteData(f.writer, f);
792 WriteTimestamps(f.timestamp_writer, f);
793 WriteContent(f.contents_writer, f);
794}
795
Austin Schuh08dba8f2023-05-01 08:29:30 -0700796std::pair<bool, monotonic_clock::time_point> Logger::LogUntil(
797 monotonic_clock::time_point t) {
798 bool wrote_messages = false;
799 monotonic_clock::time_point newest_record = monotonic_clock::min_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800800
Austin Schuh2d612c82023-07-17 13:37:48 -0700801 log_until_time_ = t;
802
Austin Schuhb06f03b2021-02-17 22:00:37 -0800803 // Grab the latest ServerStatistics message. This will always have the
804 // oppertunity to be >= to the current time, so it will always represent any
805 // reboots which may have happened.
806 WriteMissingTimestamps();
807
808 // Write each channel to disk, one at a time.
809 for (FetcherStruct &f : fetchers_) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700810 if (f.fetcher->context().data != nullptr) {
811 newest_record =
812 std::max(newest_record, f.fetcher->context().monotonic_event_time);
813 }
814
Austin Schuhb06f03b2021-02-17 22:00:37 -0800815 while (true) {
816 if (f.written) {
817 const auto start = event_loop_->monotonic_now();
Austin Schuh2d612c82023-07-17 13:37:48 -0700818 const bool got_new =
819 f.fetcher->FetchNextIf(std::ref(fetch_next_if_fn_));
Austin Schuhb06f03b2021-02-17 22:00:37 -0800820 const auto end = event_loop_->monotonic_now();
821 RecordFetchResult(start, end, got_new, &f);
822 if (!got_new) {
823 VLOG(2) << "No new data on "
824 << configuration::CleanedChannelToString(
825 f.fetcher->channel());
826 break;
827 }
Austin Schuh08dba8f2023-05-01 08:29:30 -0700828 newest_record =
829 std::max(newest_record, f.fetcher->context().monotonic_event_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800830 f.written = false;
831 }
832
833 // TODO(james): Write tests to exercise this logic.
Austin Schuh2d612c82023-07-17 13:37:48 -0700834 CHECK_LE(f.fetcher->context().monotonic_event_time, t);
835
836 // At startup, we can end up grabbing a message at the current time.
837 // Ignore it.
838 if (f.fetcher->context().monotonic_event_time == t) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800839 break;
840 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800841
Brian Smartt03c00da2022-02-24 10:25:00 -0800842 WriteFetchedRecord(f);
Austin Schuh08dba8f2023-05-01 08:29:30 -0700843 wrote_messages = true;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800844
845 f.written = true;
846 }
847 }
848 last_synchronized_time_ = t;
Brian Smartt03c00da2022-02-24 10:25:00 -0800849
Austin Schuh08dba8f2023-05-01 08:29:30 -0700850 return std::make_pair(wrote_messages, newest_record);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800851}
852
Austin Schuh30586902021-03-30 22:54:08 -0700853void Logger::DoLogData(const monotonic_clock::time_point end_time,
854 bool run_on_logged) {
Austin Schuh2d612c82023-07-17 13:37:48 -0700855 if (end_time < last_synchronized_time_) return;
856
857 DCHECK(is_started());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800858 // We want to guarantee that messages aren't out of order by more than
859 // max_out_of_order_duration. To do this, we need sync points. Every write
860 // cycle should be a sync point.
861
862 do {
863 // Move the sync point up by at most polling_period. This forces one sync
864 // per iteration, even if it is small.
865 LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time));
866
Austin Schuh30586902021-03-30 22:54:08 -0700867 if (run_on_logged) {
Austin Schuh2f864452023-07-17 14:53:08 -0700868 on_logged_period_(last_synchronized_time_);
Austin Schuh30586902021-03-30 22:54:08 -0700869 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800870
871 // If we missed cycles, we could be pretty far behind. Spin until we are
872 // caught up.
873 } while (last_synchronized_time_ + polling_period_ < end_time);
874}
875
876void Logger::RecordFetchResult(aos::monotonic_clock::time_point start,
877 aos::monotonic_clock::time_point end,
878 bool got_new, FetcherStruct *fetcher) {
879 const auto duration = end - start;
880 if (!got_new) {
881 ++total_nop_fetch_count_;
882 total_nop_fetch_time_ += duration;
883 return;
884 }
885 ++total_message_fetch_count_;
886 total_message_fetch_bytes_ += fetcher->fetcher->context().size;
887 total_message_fetch_time_ += duration;
888 if (duration > max_message_fetch_time_) {
889 max_message_fetch_time_ = duration;
890 max_message_fetch_time_channel_ = fetcher->channel_index;
891 max_message_fetch_time_size_ = fetcher->fetcher->context().size;
892 }
893}
894
895void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start,
896 aos::monotonic_clock::time_point end,
Brian Smartt03c00da2022-02-24 10:25:00 -0800897 const FetcherStruct &fetcher) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800898 const auto duration = end - start;
899 total_copy_time_ += duration;
900 ++total_copy_count_;
Brian Smartt03c00da2022-02-24 10:25:00 -0800901 total_copy_bytes_ += fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800902 if (duration > max_copy_time_) {
903 max_copy_time_ = duration;
Brian Smartt03c00da2022-02-24 10:25:00 -0800904 max_copy_time_channel_ = fetcher.channel_index;
905 max_copy_time_size_ = fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800906 }
907}
908
909} // namespace logger
910} // namespace aos