blob: 17c8da2bd51e960b55075d22afdc533613832424 [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()),
30 timer_handler_(event_loop_->AddTimer(
Austin Schuh30586902021-03-30 22:54:08 -070031 [this]() { DoLogData(event_loop_->monotonic_now(), true); })),
Austin Schuhb06f03b2021-02-17 22:00:37 -080032 server_statistics_fetcher_(
33 configuration::MultiNode(event_loop_->configuration())
34 ? event_loop_->MakeFetcher<message_bridge::ServerStatistics>(
35 "/aos")
36 : aos::Fetcher<message_bridge::ServerStatistics>()) {
Austin Schuh58646e22021-08-23 23:51:46 -070037 timer_handler_->set_name("channel_poll");
Austin Schuh5b728b72021-06-16 14:57:15 -070038 VLOG(1) << "Creating logger for " << FlatbufferToJson(node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -080039
Naman Gupta41d70c22022-11-21 15:29:52 -080040 // When we are logging remote timestamps, we need to be able to translate
41 // from the channel index that the event loop uses to the channel index in
42 // the config in the log file.
Austin Schuh01f3b392022-01-25 20:03:09 -080043 event_loop_to_logged_channel_index_.resize(
44 event_loop->configuration()->channels()->size(), -1);
45 for (size_t event_loop_channel_index = 0;
46 event_loop_channel_index <
47 event_loop->configuration()->channels()->size();
48 ++event_loop_channel_index) {
49 const Channel *event_loop_channel =
50 event_loop->configuration()->channels()->Get(event_loop_channel_index);
51
52 const Channel *logged_channel = aos::configuration::GetChannel(
53 configuration_, event_loop_channel->name()->string_view(),
54 event_loop_channel->type()->string_view(), "", node_);
55
56 if (logged_channel != nullptr) {
57 event_loop_to_logged_channel_index_[event_loop_channel_index] =
58 configuration::ChannelIndex(configuration_, logged_channel);
59 }
60 }
61
62 // Map to match source channels with the timestamp logger, if the contents
63 // should be reliable, and a list of all channels logged on it to be treated
64 // as reliable.
65 std::map<const Channel *, std::tuple<const Node *, bool, std::vector<bool>>>
66 timestamp_logger_channels;
Austin Schuhb06f03b2021-02-17 22:00:37 -080067
Austin Schuh61e973f2021-02-21 21:43:56 -080068 message_bridge::ChannelTimestampFinder finder(event_loop_);
69 for (const Channel *channel : *event_loop_->configuration()->channels()) {
70 if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node())) {
Austin Schuhb06f03b2021-02-17 22:00:37 -080071 continue;
72 }
Austin Schuh61e973f2021-02-21 21:43:56 -080073 if (!channel->has_destination_nodes()) {
74 continue;
75 }
Austin Schuh01f3b392022-01-25 20:03:09 -080076 const size_t channel_index =
77 configuration::ChannelIndex(event_loop_->configuration(), channel);
78
Austin Schuh61e973f2021-02-21 21:43:56 -080079 for (const Connection *connection : *channel->destination_nodes()) {
80 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(
81 connection, event_loop_->node())) {
82 const Node *other_node = configuration::GetNode(
Austin Schuh5b728b72021-06-16 14:57:15 -070083 configuration_, connection->name()->string_view());
Austin Schuh61e973f2021-02-21 21:43:56 -080084
85 VLOG(1) << "Timestamps are logged from "
86 << FlatbufferToJson(other_node);
Austin Schuh01f3b392022-01-25 20:03:09 -080087 // True if each channel's remote timestamps are split into a separate
88 // RemoteMessage channel.
89 const bool is_split =
90 finder.SplitChannelForChannel(channel, connection) != nullptr;
91
92 const Channel *const timestamp_logger_channel =
93 finder.ForChannel(channel, connection);
94
95 auto it = timestamp_logger_channels.find(timestamp_logger_channel);
96 if (it != timestamp_logger_channels.end()) {
97 CHECK(!is_split);
98 CHECK_LT(channel_index, std::get<2>(it->second).size());
Brian Smartt796cca02022-04-12 15:07:21 -070099 std::get<2>(it->second)[channel_index] =
100 (connection->time_to_live() == 0);
Austin Schuh01f3b392022-01-25 20:03:09 -0800101 } else {
102 if (is_split) {
103 timestamp_logger_channels.insert(std::make_pair(
104 timestamp_logger_channel,
105 std::make_tuple(other_node, (connection->time_to_live() == 0),
106 std::vector<bool>())));
107 } else {
108 std::vector<bool> channel_reliable_contents(
109 event_loop->configuration()->channels()->size(), false);
110 channel_reliable_contents[channel_index] =
111 (connection->time_to_live() == 0);
112
113 timestamp_logger_channels.insert(std::make_pair(
114 timestamp_logger_channel,
115 std::make_tuple(other_node, false,
116 std::move(channel_reliable_contents))));
117 }
118 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800119 }
120 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800121 }
122
Austin Schuhb06f03b2021-02-17 22:00:37 -0800123 for (size_t channel_index = 0;
124 channel_index < configuration_->channels()->size(); ++channel_index) {
125 const Channel *const config_channel =
126 configuration_->channels()->Get(channel_index);
127 // The MakeRawFetcher method needs a channel which is in the event loop
128 // configuration() object, not the configuration_ object. Go look that up
129 // from the config.
130 const Channel *channel = aos::configuration::GetChannel(
131 event_loop_->configuration(), config_channel->name()->string_view(),
132 config_channel->type()->string_view(), "", event_loop_->node());
133 CHECK(channel != nullptr)
134 << ": Failed to look up channel "
135 << aos::configuration::CleanedChannelToString(config_channel);
Austin Schuh5b728b72021-06-16 14:57:15 -0700136 if (!should_log(config_channel)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800137 continue;
138 }
139
140 FetcherStruct fs;
141 fs.channel_index = channel_index;
142 fs.channel = channel;
143
144 const bool is_local =
Austin Schuh5b728b72021-06-16 14:57:15 -0700145 configuration::ChannelIsSendableOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800146
147 const bool is_readable =
Austin Schuh5b728b72021-06-16 14:57:15 -0700148 configuration::ChannelIsReadableOnNode(config_channel, node_);
Austin Schuh01f3b392022-01-25 20:03:09 -0800149 const bool is_logged =
150 configuration::ChannelMessageIsLoggedOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800151 const bool log_message = is_logged && is_readable;
152
153 bool log_delivery_times = false;
Austin Schuh5b728b72021-06-16 14:57:15 -0700154 if (configuration::MultiNode(configuration_)) {
Austin Schuh72211ae2021-08-05 14:02:30 -0700155 const aos::Connection *connection =
Austin Schuh5b728b72021-06-16 14:57:15 -0700156 configuration::ConnectionToNode(config_channel, node_);
Austin Schuh72211ae2021-08-05 14:02:30 -0700157
Austin Schuhb06f03b2021-02-17 22:00:37 -0800158 log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh72211ae2021-08-05 14:02:30 -0700159 connection, event_loop_->node());
160
161 CHECK_EQ(log_delivery_times,
162 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh5b728b72021-06-16 14:57:15 -0700163 config_channel, node_, node_));
Austin Schuh72211ae2021-08-05 14:02:30 -0700164
165 if (connection) {
166 fs.reliable_forwarding = (connection->time_to_live() == 0);
167 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800168 }
169
Austin Schuh01f3b392022-01-25 20:03:09 -0800170 // Now, detect a RemoteMessage timestamp logger where we should just log
171 // the contents to a file directly.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800172 const bool log_contents = timestamp_logger_channels.find(channel) !=
173 timestamp_logger_channels.end();
174
175 if (log_message || log_delivery_times || log_contents) {
176 fs.fetcher = event_loop->MakeRawFetcher(channel);
177 VLOG(1) << "Logging channel "
178 << configuration::CleanedChannelToString(channel);
179
180 if (log_delivery_times) {
181 VLOG(1) << " Delivery times";
182 fs.wants_timestamp_writer = true;
Austin Schuh5b728b72021-06-16 14:57:15 -0700183 fs.timestamp_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800184 }
Austin Schuhe46492f2021-07-31 19:49:41 -0700185 // Both the timestamp and data writers want data_node_index so it knows
186 // what the source node is.
187 if (log_message || log_delivery_times) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800188 if (!is_local) {
189 const Node *source_node = configuration::GetNode(
190 configuration_, channel->source_node()->string_view());
191 fs.data_node_index =
192 configuration::GetNodeIndex(configuration_, source_node);
Austin Schuhe46492f2021-07-31 19:49:41 -0700193 }
194 }
195 if (log_message) {
196 VLOG(1) << " Data";
197 fs.wants_writer = true;
198 if (!is_local) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800199 fs.log_type = LogType::kLogRemoteMessage;
200 } else {
Austin Schuh5b728b72021-06-16 14:57:15 -0700201 fs.data_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800202 }
203 }
204 if (log_contents) {
205 VLOG(1) << "Timestamp logger channel "
206 << configuration::CleanedChannelToString(channel);
Austin Schuh01f3b392022-01-25 20:03:09 -0800207 auto timestamp_logger_channel_info =
208 timestamp_logger_channels.find(channel);
209 CHECK(timestamp_logger_channel_info != timestamp_logger_channels.end());
210 fs.timestamp_node = std::get<0>(timestamp_logger_channel_info->second);
211 fs.reliable_contents =
212 std::get<1>(timestamp_logger_channel_info->second);
213 fs.channel_reliable_contents =
214 std::get<2>(timestamp_logger_channel_info->second);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800215 fs.wants_contents_writer = true;
216 fs.contents_node_index =
217 configuration::GetNodeIndex(configuration_, fs.timestamp_node);
218 }
219 fetchers_.emplace_back(std::move(fs));
220 }
221 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800222}
223
224Logger::~Logger() {
225 if (log_namer_) {
226 // If we are replaying a log file, or in simulation, we want to force the
227 // last bit of data to be logged. The easiest way to deal with this is to
Austin Schuh01f3b392022-01-25 20:03:09 -0800228 // poll everything as we go to destroy the class, ie, shut down the
229 // logger, and write it to disk.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800230 StopLogging(event_loop_->monotonic_now());
231 }
232}
233
Brian Smartt796cca02022-04-12 15:07:21 -0700234std::string Logger::WriteConfiguration(LogNamer *log_namer) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800235 std::string config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800236
Austin Schuhb06f03b2021-02-17 22:00:37 -0800237 if (separate_config_) {
238 flatbuffers::FlatBufferBuilder fbb;
239 flatbuffers::Offset<aos::Configuration> configuration_offset =
240 CopyFlatBuffer(configuration_, &fbb);
241 LogFileHeader::Builder log_file_header_builder(fbb);
242 log_file_header_builder.add_configuration(configuration_offset);
243 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
244 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header(
245 fbb.Release());
246 config_sha256 = Sha256(config_header.span());
247 LOG(INFO) << "Config sha256 of " << config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800248 log_namer->WriteConfiguration(&config_header, config_sha256);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800249 }
250
Brian Smartt03c00da2022-02-24 10:25:00 -0800251 return config_sha256;
252}
253
254void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer,
255 std::optional<UUID> log_start_uuid) {
256 CHECK(!log_namer_) << ": Already logging";
257
258 VLOG(1) << "Starting logger for " << FlatbufferToJson(node_);
259
260 auto config_sha256 = WriteConfiguration(log_namer.get());
261
262 log_namer_ = std::move(log_namer);
263
Austin Schuhb06f03b2021-02-17 22:00:37 -0800264 log_event_uuid_ = UUID::Random();
265 log_start_uuid_ = log_start_uuid;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800266
267 // We want to do as much work as possible before the initial Fetch. Time
268 // between that and actually starting to log opens up the possibility of
269 // falling off the end of the queue during that time.
270
271 for (FetcherStruct &f : fetchers_) {
272 if (f.wants_writer) {
273 f.writer = log_namer_->MakeWriter(f.channel);
274 }
275 if (f.wants_timestamp_writer) {
276 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
277 }
278 if (f.wants_contents_writer) {
279 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
280 f.channel, CHECK_NOTNULL(f.timestamp_node));
281 }
282 }
283
Austin Schuh73340842021-07-30 22:32:06 -0700284 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
Austin Schuhb06f03b2021-02-17 22:00:37 -0800285
Austin Schuha42ee962021-03-31 22:49:30 -0700286 const aos::monotonic_clock::time_point beginning_time =
287 event_loop_->monotonic_now();
288
Austin Schuhb06f03b2021-02-17 22:00:37 -0800289 // Grab data from each channel right before we declare the log file started
290 // so we can capture the latest message on each channel. This lets us have
291 // non periodic messages with configuration that now get logged.
292 for (FetcherStruct &f : fetchers_) {
293 const auto start = event_loop_->monotonic_now();
294 const bool got_new = f.fetcher->Fetch();
295 const auto end = event_loop_->monotonic_now();
296 RecordFetchResult(start, end, got_new, &f);
297
298 // If there is a message, we want to write it.
299 f.written = f.fetcher->context().data == nullptr;
300 }
301
302 // Clear out any old timestamps in case we are re-starting logging.
Austin Schuh572924a2021-07-30 22:32:12 -0700303 for (size_t i = 0; i < configuration::NodesCount(configuration_); ++i) {
Austin Schuh58646e22021-08-23 23:51:46 -0700304 log_namer_->ClearStartTimes();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800305 }
306
Austin Schuha42ee962021-03-31 22:49:30 -0700307 const aos::monotonic_clock::time_point fetch_time =
308 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800309 WriteHeader();
Austin Schuha42ee962021-03-31 22:49:30 -0700310 const aos::monotonic_clock::time_point header_time =
311 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800312
Brian Smartt796cca02022-04-12 15:07:21 -0700313 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " start_time "
314 << last_synchronized_time_ << ", took "
Brian Smartt03c00da2022-02-24 10:25:00 -0800315 << chrono::duration<double>(fetch_time - beginning_time).count()
316 << " to fetch, "
317 << chrono::duration<double>(header_time - fetch_time).count()
318 << " to write headers, boot uuid " << event_loop_->boot_uuid();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800319
320 // Force logging up until the start of the log file now, so the messages at
321 // the start are always ordered before the rest of the messages.
322 // Note: this ship may have already sailed, but we don't have to make it
323 // worse.
324 // TODO(austin): Test...
Austin Schuh855f8932021-03-19 22:01:32 -0700325 //
Naman Gupta41d70c22022-11-21 15:29:52 -0800326 // This is safe to call here since we have set last_synchronized_time_ as
327 // the same time as in the header, and all the data before it should be
328 // logged without ordering concerns.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800329 LogUntil(last_synchronized_time_);
330
331 timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_,
332 polling_period_);
333}
334
Brian Smartt796cca02022-04-12 15:07:21 -0700335std::unique_ptr<LogNamer> Logger::RestartLogging(
336 std::unique_ptr<LogNamer> log_namer, std::optional<UUID> log_start_uuid) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800337 CHECK(log_namer_) << ": Unexpected restart while not logging";
338
339 VLOG(1) << "Restarting logger for " << FlatbufferToJson(node_);
340
Austin Schuh08dba8f2023-05-01 08:29:30 -0700341 // Make sure not to write past now so we don't risk out of order problems. We
342 // don't want to get into a situation where we write out up to now + 0.1 sec,
343 // and that operation takes ~0.1 seconds, so we end up writing a different
344 // amount of the early and late channels. That would then result in the next
345 // go around finding more than 0.1 sec of data on the early channels.
346 //
347 // Make sure we read up until "now" and log it. This sets us up so that we
348 // are unlikely to fetch a message far in the future and have a ton of data
349 // before the offical start time.
Alexei Strotsbc082d82023-05-03 08:43:42 -0700350 monotonic_clock::time_point newest_record = monotonic_clock::min_time;
Austin Schuh08dba8f2023-05-01 08:29:30 -0700351 while (true) {
352 aos::monotonic_clock::time_point next_time =
353 last_synchronized_time_ + polling_period_;
354 const aos::monotonic_clock::time_point monotonic_now =
355 event_loop_->monotonic_now();
356 if (next_time > monotonic_now) {
357 next_time = monotonic_now;
358 }
359
360 bool wrote_messages = false;
361 std::tie(wrote_messages, newest_record) = LogUntil(next_time);
362
363 if (next_time == monotonic_now &&
364 (!wrote_messages || newest_record < monotonic_now + polling_period_)) {
365 // If we stopped writing messages, then we probably have stopped making
366 // progress. If the newest record (unwritten or written) on a channel is
367 // very close to the current time, then there won't be much data
368 // officially after the end of the last log but before the start of the
369 // current one. We need to pick the start of the current log to be after
370 // the last message on record so we don't have holes in the log.
371 break;
372 }
373 }
374
375 // We are now synchronized up to last_synchronized_time_. Our start time can
376 // safely be "newest_record". But, we need to guarentee that the start time
377 // is after the newest message we have a record of, and that we don't skip any
378 // messages as we rotate. This means we can't call Fetch anywhere.
Brian Smartt03c00da2022-02-24 10:25:00 -0800379
380 std::unique_ptr<LogNamer> old_log_namer = std::move(log_namer_);
381 log_namer_ = std::move(log_namer);
382
Naman Gupta41d70c22022-11-21 15:29:52 -0800383 // Now grab a representative time on both the RT and monotonic clock.
384 // Average a monotonic clock before and after to reduce the error.
Brian Smartt03c00da2022-02-24 10:25:00 -0800385 const aos::monotonic_clock::time_point beginning_time =
386 event_loop_->monotonic_now();
Austin Schuh41f8df92022-04-15 11:45:52 -0700387 const aos::realtime_clock::time_point beginning_time_rt =
388 event_loop_->realtime_now();
389 const aos::monotonic_clock::time_point beginning_time2 =
390 event_loop_->monotonic_now();
391
392 if (beginning_time > last_synchronized_time_) {
393 LOG(WARNING) << "Took over " << polling_period_.count()
394 << "ns to swap log_namer";
395 }
396
Austin Schuh08dba8f2023-05-01 08:29:30 -0700397 // Our start time is now the newest message we have a record of. We will
398 // declare the old log "done", and start in on the new one, double-logging
399 // anything we have a record of so we have all the messages from before the
400 // start.
401 const aos::monotonic_clock::time_point monotonic_start_time = newest_record;
Austin Schuh41f8df92022-04-15 11:45:52 -0700402 const aos::realtime_clock::time_point realtime_start_time =
403 (beginning_time_rt + (monotonic_start_time.time_since_epoch() -
404 ((beginning_time.time_since_epoch() +
405 beginning_time2.time_since_epoch()) /
406 2)));
Brian Smartt03c00da2022-02-24 10:25:00 -0800407
408 auto config_sha256 = WriteConfiguration(log_namer_.get());
409
410 log_event_uuid_ = UUID::Random();
411 log_start_uuid_ = log_start_uuid;
412
413 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
414
415 // Note that WriteHeader updates last_synchronized_time_ to be the
416 // current time when it is called, which is then the "start time"
417 // of the new (restarted) log. This timestamp will be after
Naman Gupta41d70c22022-11-21 15:29:52 -0800418 // the timestamp of the last message fetched on each channel, but is
419 // carefully picked per the comment above to not violate
420 // max_out_of_order_duration.
Austin Schuh41f8df92022-04-15 11:45:52 -0700421 WriteHeader(monotonic_start_time, realtime_start_time);
Brian Smartt03c00da2022-02-24 10:25:00 -0800422
423 const aos::monotonic_clock::time_point header_time =
424 event_loop_->monotonic_now();
425
Austin Schuh08dba8f2023-05-01 08:29:30 -0700426 // Close out the old writers to free up memory to be used by the new writers.
427 old_log_namer->Close();
428
Brian Smartt03c00da2022-02-24 10:25:00 -0800429 for (FetcherStruct &f : fetchers_) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800430 // Create writers from the new namer
Brian Smartt03c00da2022-02-24 10:25:00 -0800431
432 if (f.wants_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700433 f.writer = log_namer_->MakeWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800434 }
435 if (f.wants_timestamp_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700436 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800437 }
438 if (f.wants_contents_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700439 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
Brian Smartt03c00da2022-02-24 10:25:00 -0800440 f.channel, CHECK_NOTNULL(f.timestamp_node));
441 }
442
Austin Schuh08dba8f2023-05-01 08:29:30 -0700443 // Mark each channel with data as not written. That triggers each channel
444 // to be re-logged.
445 f.written = f.fetcher->context().data == nullptr;
Brian Smartt03c00da2022-02-24 10:25:00 -0800446 }
447
Austin Schuh08dba8f2023-05-01 08:29:30 -0700448 // And now make sure to log everything up to the start time in 1 big go so we
449 // make sure we have it before we let the world start logging normally again.
450 LogUntil(monotonic_start_time);
451
Brian Smartt03c00da2022-02-24 10:25:00 -0800452 const aos::monotonic_clock::time_point channel_time =
453 event_loop_->monotonic_now();
454
Brian Smartt796cca02022-04-12 15:07:21 -0700455 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " restart_time "
456 << last_synchronized_time_ << ", took "
Brian Smartt03c00da2022-02-24 10:25:00 -0800457 << chrono::duration<double>(header_time - beginning_time).count()
458 << " to prepare and write header, "
459 << chrono::duration<double>(channel_time - header_time).count()
Brian Smartt796cca02022-04-12 15:07:21 -0700460 << " to write initial channel messages, boot uuid "
461 << event_loop_->boot_uuid();
Brian Smartt03c00da2022-02-24 10:25:00 -0800462
463 return old_log_namer;
464}
465
Austin Schuhb06f03b2021-02-17 22:00:37 -0800466std::unique_ptr<LogNamer> Logger::StopLogging(
467 aos::monotonic_clock::time_point end_time) {
468 CHECK(log_namer_) << ": Not logging right now";
469
470 if (end_time != aos::monotonic_clock::min_time) {
Austin Schuh30586902021-03-30 22:54:08 -0700471 // Folks like to use the on_logged_period_ callback to trigger stop and
472 // start events. We can't have those then recurse and try to stop again.
473 // Rather than making everything reentrant, let's just instead block the
474 // callback here.
475 DoLogData(end_time, false);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800476 }
477 timer_handler_->Disable();
478
479 for (FetcherStruct &f : fetchers_) {
480 f.writer = nullptr;
481 f.timestamp_writer = nullptr;
482 f.contents_writer = nullptr;
483 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800484
485 log_event_uuid_ = UUID::Zero();
Austin Schuh34f9e482021-03-31 22:54:18 -0700486 log_start_uuid_ = std::nullopt;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800487
Alexei Strotsbc776d52023-04-15 23:46:45 -0700488 log_namer_->Close();
489
Austin Schuhb06f03b2021-02-17 22:00:37 -0800490 return std::move(log_namer_);
491}
492
Austin Schuh41f8df92022-04-15 11:45:52 -0700493void Logger::WriteHeader(aos::monotonic_clock::time_point monotonic_start_time,
494 aos::realtime_clock::time_point realtime_start_time) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800495 if (configuration::MultiNode(configuration_)) {
496 server_statistics_fetcher_.Fetch();
497 }
498
Austin Schuh41f8df92022-04-15 11:45:52 -0700499 if (monotonic_start_time == aos::monotonic_clock::min_time) {
500 monotonic_start_time = event_loop_->monotonic_now();
501 realtime_start_time = event_loop_->realtime_now();
502 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800503
504 // We need to pick a point in time to declare the log file "started". This
505 // starts here. It needs to be after everything is fetched so that the
506 // fetchers are all pointed at the most recent message before the start
507 // time.
508 last_synchronized_time_ = monotonic_start_time;
509
510 for (const Node *node : log_namer_->nodes()) {
511 const int node_index = configuration::GetNodeIndex(configuration_, node);
512 MaybeUpdateTimestamp(node, node_index, monotonic_start_time,
513 realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800514 }
515}
516
Austin Schuhb06f03b2021-02-17 22:00:37 -0800517void Logger::WriteMissingTimestamps() {
518 if (configuration::MultiNode(configuration_)) {
519 server_statistics_fetcher_.Fetch();
520 } else {
521 return;
522 }
523
524 if (server_statistics_fetcher_.get() == nullptr) {
525 return;
526 }
527
528 for (const Node *node : log_namer_->nodes()) {
529 const int node_index = configuration::GetNodeIndex(configuration_, node);
530 if (MaybeUpdateTimestamp(
531 node, node_index,
532 server_statistics_fetcher_.context().monotonic_event_time,
533 server_statistics_fetcher_.context().realtime_event_time)) {
Austin Schuh58646e22021-08-23 23:51:46 -0700534 VLOG(1) << "Timestamps changed on " << aos::FlatbufferToJson(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800535 }
536 }
537}
538
Austin Schuhb06f03b2021-02-17 22:00:37 -0800539bool Logger::MaybeUpdateTimestamp(
540 const Node *node, int node_index,
541 aos::monotonic_clock::time_point monotonic_start_time,
542 aos::realtime_clock::time_point realtime_start_time) {
543 // Bail early if the start times are already set.
Austin Schuh58646e22021-08-23 23:51:46 -0700544 if (node_ == node || !configuration::MultiNode(configuration_)) {
545 if (log_namer_->monotonic_start_time(node_index,
546 event_loop_->boot_uuid()) !=
547 monotonic_clock::min_time) {
548 return false;
549 }
Brian Smartt03c00da2022-02-24 10:25:00 -0800550
Austin Schuhb06f03b2021-02-17 22:00:37 -0800551 // There are no offsets to compute for ourself, so always succeed.
Austin Schuh58646e22021-08-23 23:51:46 -0700552 log_namer_->SetStartTimes(node_index, event_loop_->boot_uuid(),
553 monotonic_start_time, realtime_start_time,
554 monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800555 return true;
556 } else if (server_statistics_fetcher_.get() != nullptr) {
557 // We must be a remote node now. Look for the connection and see if it is
558 // connected.
James Kuszmaul17607fb2021-10-15 20:00:32 -0700559 CHECK(server_statistics_fetcher_->has_connections());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800560
561 for (const message_bridge::ServerConnection *connection :
562 *server_statistics_fetcher_->connections()) {
563 if (connection->node()->name()->string_view() !=
564 node->name()->string_view()) {
565 continue;
566 }
567
568 if (connection->state() != message_bridge::State::CONNECTED) {
569 VLOG(1) << node->name()->string_view()
570 << " is not connected, can't start it yet.";
571 break;
572 }
573
Austin Schuhb06f03b2021-02-17 22:00:37 -0800574 if (!connection->has_monotonic_offset()) {
575 VLOG(1) << "Missing monotonic offset for setting start time for node "
576 << aos::FlatbufferToJson(node);
577 break;
578 }
579
James Kuszmaul17607fb2021-10-15 20:00:32 -0700580 CHECK(connection->has_boot_uuid());
Austin Schuh58646e22021-08-23 23:51:46 -0700581 const UUID boot_uuid =
582 UUID::FromString(connection->boot_uuid()->string_view());
583
584 if (log_namer_->monotonic_start_time(node_index, boot_uuid) !=
585 monotonic_clock::min_time) {
586 break;
587 }
588
589 VLOG(1) << "Updating start time for "
590 << aos::FlatbufferToJson(connection);
591
Austin Schuhb06f03b2021-02-17 22:00:37 -0800592 // Found it and it is connected. Compensate and go.
Austin Schuh73340842021-07-30 22:32:06 -0700593 log_namer_->SetStartTimes(
Austin Schuh58646e22021-08-23 23:51:46 -0700594 node_index, boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700595 monotonic_start_time +
596 std::chrono::nanoseconds(connection->monotonic_offset()),
597 realtime_start_time, monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800598 return true;
599 }
600 }
601 return false;
602}
603
604aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader(
Austin Schuh73340842021-07-30 22:32:06 -0700605 std::string_view config_sha256) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800606 flatbuffers::FlatBufferBuilder fbb;
607 fbb.ForceDefaults(true);
608
609 flatbuffers::Offset<aos::Configuration> configuration_offset;
610 if (!separate_config_) {
611 configuration_offset = CopyFlatBuffer(configuration_, &fbb);
612 } else {
613 CHECK(!config_sha256.empty());
614 }
615
616 const flatbuffers::Offset<flatbuffers::String> name_offset =
617 fbb.CreateString(name_);
618
Austin Schuhfa712682022-05-11 16:43:42 -0700619 const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset =
620 logger_sha1_.empty() ? 0 : fbb.CreateString(logger_sha1_);
621 const flatbuffers::Offset<flatbuffers::String> logger_version_offset =
622 logger_version_.empty() ? 0 : fbb.CreateString(logger_version_);
623
Austin Schuhb06f03b2021-02-17 22:00:37 -0800624 CHECK(log_event_uuid_ != UUID::Zero());
625 const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800626 log_event_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800627
628 const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800629 logger_instance_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800630
631 flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset;
Austin Schuh34f9e482021-03-31 22:54:18 -0700632 if (log_start_uuid_) {
633 log_start_uuid_offset = fbb.CreateString(log_start_uuid_->ToString());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800634 }
635
636 flatbuffers::Offset<flatbuffers::String> config_sha256_offset;
637 if (!config_sha256.empty()) {
638 config_sha256_offset = fbb.CreateString(config_sha256);
639 }
640
641 const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800642 event_loop_->boot_uuid().PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800643
Austin Schuhb06f03b2021-02-17 22:00:37 -0800644 flatbuffers::Offset<Node> logger_node_offset;
645
646 if (configuration::MultiNode(configuration_)) {
Austin Schuh5b728b72021-06-16 14:57:15 -0700647 logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800648 }
649
650 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
651
652 log_file_header_builder.add_name(name_offset);
Austin Schuhfa712682022-05-11 16:43:42 -0700653 if (!logger_sha1_offset.IsNull()) {
654 log_file_header_builder.add_logger_sha1(logger_sha1_offset);
655 }
656 if (!logger_version_offset.IsNull()) {
657 log_file_header_builder.add_logger_version(logger_version_offset);
658 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800659
660 // Only add the node if we are running in a multinode configuration.
Austin Schuh73340842021-07-30 22:32:06 -0700661 if (configuration::MultiNode(configuration_)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800662 log_file_header_builder.add_logger_node(logger_node_offset);
663 }
664
665 if (!configuration_offset.IsNull()) {
666 log_file_header_builder.add_configuration(configuration_offset);
667 }
668 // The worst case theoretical out of order is the polling period times 2.
669 // One message could get logged right after the boundary, but be for right
670 // before the next boundary. And the reverse could happen for another
671 // message. Report back 3x to be extra safe, and because the cost isn't
672 // huge on the read side.
673 log_file_header_builder.add_max_out_of_order_duration(
674 std::chrono::nanoseconds(3 * polling_period_).count());
675
Austin Schuhb06f03b2021-02-17 22:00:37 -0800676 log_file_header_builder.add_log_event_uuid(log_event_uuid_offset);
677 log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset);
678 if (!log_start_uuid_offset.IsNull()) {
679 log_file_header_builder.add_log_start_uuid(log_start_uuid_offset);
680 }
681 log_file_header_builder.add_logger_node_boot_uuid(
682 logger_node_boot_uuid_offset);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800683
684 if (!config_sha256_offset.IsNull()) {
685 log_file_header_builder.add_configuration_sha256(config_sha256_offset);
686 }
687
688 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
689 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result(
690 fbb.Release());
691
692 CHECK(result.Verify()) << ": Built a corrupted header.";
693
694 return result;
695}
696
697void Logger::ResetStatisics() {
698 max_message_fetch_time_ = std::chrono::nanoseconds::zero();
699 max_message_fetch_time_channel_ = -1;
700 max_message_fetch_time_size_ = -1;
701 total_message_fetch_time_ = std::chrono::nanoseconds::zero();
702 total_message_fetch_count_ = 0;
703 total_message_fetch_bytes_ = 0;
704 total_nop_fetch_time_ = std::chrono::nanoseconds::zero();
705 total_nop_fetch_count_ = 0;
706 max_copy_time_ = std::chrono::nanoseconds::zero();
707 max_copy_time_channel_ = -1;
708 max_copy_time_size_ = -1;
709 total_copy_time_ = std::chrono::nanoseconds::zero();
710 total_copy_count_ = 0;
711 total_copy_bytes_ = 0;
712}
713
714void Logger::Rotate() {
715 for (const Node *node : log_namer_->nodes()) {
Austin Schuh73340842021-07-30 22:32:06 -0700716 log_namer_->Rotate(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800717 }
718}
719
Brian Smartt03c00da2022-02-24 10:25:00 -0800720void Logger::WriteData(NewDataWriter *writer, const FetcherStruct &f) {
721 if (writer != nullptr) {
722 const UUID source_node_boot_uuid =
723 static_cast<int>(node_index_) != f.data_node_index
724 ? f.fetcher->context().source_boot_uuid
725 : event_loop_->boot_uuid();
726 // Write!
727 const auto start = event_loop_->monotonic_now();
Brian Smartt03c00da2022-02-24 10:25:00 -0800728
Naman Gupta41d70c22022-11-21 15:29:52 -0800729 ContextDataCopier coppier(f.fetcher->context(), f.channel_index, f.log_type,
730 event_loop_);
Brian Smartt03c00da2022-02-24 10:25:00 -0800731
Austin Schuh48d10d62022-10-16 22:19:23 -0700732 writer->CopyMessage(&coppier, source_node_boot_uuid, start);
733 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800734
Brian Smartt796cca02022-04-12 15:07:21 -0700735 VLOG(2) << "Wrote data as node " << FlatbufferToJson(node_)
736 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800737 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700738 << " to " << writer->name();
Brian Smartt03c00da2022-02-24 10:25:00 -0800739 }
740}
741
Brian Smartt796cca02022-04-12 15:07:21 -0700742void Logger::WriteTimestamps(NewDataWriter *timestamp_writer,
743 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800744 if (timestamp_writer != nullptr) {
745 // And now handle timestamps.
Brian Smartt03c00da2022-02-24 10:25:00 -0800746
747 // Tell our writer that we know something about the remote boot.
748 timestamp_writer->UpdateRemote(
749 f.data_node_index, f.fetcher->context().source_boot_uuid,
750 f.fetcher->context().monotonic_remote_time,
751 f.fetcher->context().monotonic_event_time, f.reliable_forwarding);
Austin Schuh48d10d62022-10-16 22:19:23 -0700752
753 const auto start = event_loop_->monotonic_now();
754 ContextDataCopier coppier(f.fetcher->context(), f.channel_index,
Naman Gupta41d70c22022-11-21 15:29:52 -0800755 LogType::kLogDeliveryTimeOnly, event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700756
757 timestamp_writer->CopyMessage(&coppier, event_loop_->boot_uuid(), start);
758 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800759
Brian Smartt796cca02022-04-12 15:07:21 -0700760 VLOG(2) << "Wrote timestamps as node " << FlatbufferToJson(node_)
761 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800762 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700763 << " to " << timestamp_writer->name() << " timestamp";
Brian Smartt03c00da2022-02-24 10:25:00 -0800764 }
765}
766
Brian Smartt796cca02022-04-12 15:07:21 -0700767void Logger::WriteContent(NewDataWriter *contents_writer,
768 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800769 if (contents_writer != nullptr) {
770 const auto start = event_loop_->monotonic_now();
771 // And now handle the special message contents channel. Copy the
772 // message into a FlatBufferBuilder and save it to disk.
Brian Smartt03c00da2022-02-24 10:25:00 -0800773 const RemoteMessage *msg =
774 flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data);
775
776 CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg);
Brian Smartt03c00da2022-02-24 10:25:00 -0800777 // Translate from the channel index that the event loop uses to the
778 // channel index in the log file.
Austin Schuhf2d0e682022-10-16 14:20:58 -0700779 const int channel_index =
780 event_loop_to_logged_channel_index_[msg->channel_index()];
Brian Smartt03c00da2022-02-24 10:25:00 -0800781
782 const aos::monotonic_clock::time_point monotonic_timestamp_time =
783 f.fetcher->context().monotonic_event_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800784
Brian Smartt03c00da2022-02-24 10:25:00 -0800785 // Timestamps tell us information about what happened too!
786 // Capture any reboots so UpdateRemote is properly recorded.
787 contents_writer->UpdateBoot(UUID::FromVector(msg->boot_uuid()));
788
789 // Start with recording info about the data flowing from our node to the
790 // remote.
791 const bool reliable =
792 f.channel_reliable_contents.size() != 0u
793 ? f.channel_reliable_contents[msg->channel_index()]
794 : f.reliable_contents;
795
Brian Smartt796cca02022-04-12 15:07:21 -0700796 contents_writer->UpdateRemote(
797 node_index_, event_loop_->boot_uuid(),
Brian Smartt03c00da2022-02-24 10:25:00 -0800798 monotonic_clock::time_point(
799 chrono::nanoseconds(msg->monotonic_remote_time())),
800 monotonic_clock::time_point(
801 chrono::nanoseconds(msg->monotonic_sent_time())),
802 reliable, monotonic_timestamp_time);
803
Austin Schuh48d10d62022-10-16 22:19:23 -0700804 RemoteMessageCopier coppier(msg, channel_index, monotonic_timestamp_time,
Naman Gupta41d70c22022-11-21 15:29:52 -0800805 event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700806
807 contents_writer->CopyMessage(&coppier, UUID::FromVector(msg->boot_uuid()),
808 start);
809
810 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800811 }
812}
813
814void Logger::WriteFetchedRecord(FetcherStruct &f) {
815 WriteData(f.writer, f);
816 WriteTimestamps(f.timestamp_writer, f);
817 WriteContent(f.contents_writer, f);
818}
819
Austin Schuh08dba8f2023-05-01 08:29:30 -0700820std::pair<bool, monotonic_clock::time_point> Logger::LogUntil(
821 monotonic_clock::time_point t) {
822 bool wrote_messages = false;
823 monotonic_clock::time_point newest_record = monotonic_clock::min_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800824
Austin Schuhb06f03b2021-02-17 22:00:37 -0800825 // Grab the latest ServerStatistics message. This will always have the
826 // oppertunity to be >= to the current time, so it will always represent any
827 // reboots which may have happened.
828 WriteMissingTimestamps();
829
830 // Write each channel to disk, one at a time.
831 for (FetcherStruct &f : fetchers_) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700832 if (f.fetcher->context().data != nullptr) {
833 newest_record =
834 std::max(newest_record, f.fetcher->context().monotonic_event_time);
835 }
836
Austin Schuhb06f03b2021-02-17 22:00:37 -0800837 while (true) {
838 if (f.written) {
839 const auto start = event_loop_->monotonic_now();
840 const bool got_new = f.fetcher->FetchNext();
841 const auto end = event_loop_->monotonic_now();
842 RecordFetchResult(start, end, got_new, &f);
843 if (!got_new) {
844 VLOG(2) << "No new data on "
845 << configuration::CleanedChannelToString(
846 f.fetcher->channel());
847 break;
848 }
Austin Schuh08dba8f2023-05-01 08:29:30 -0700849 newest_record =
850 std::max(newest_record, f.fetcher->context().monotonic_event_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800851 f.written = false;
852 }
853
854 // TODO(james): Write tests to exercise this logic.
855 if (f.fetcher->context().monotonic_event_time >= t) {
856 break;
857 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800858
Brian Smartt03c00da2022-02-24 10:25:00 -0800859 WriteFetchedRecord(f);
Austin Schuh08dba8f2023-05-01 08:29:30 -0700860 wrote_messages = true;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800861
862 f.written = true;
863 }
864 }
865 last_synchronized_time_ = t;
Brian Smartt03c00da2022-02-24 10:25:00 -0800866
Austin Schuh08dba8f2023-05-01 08:29:30 -0700867 return std::make_pair(wrote_messages, newest_record);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800868}
869
Austin Schuh30586902021-03-30 22:54:08 -0700870void Logger::DoLogData(const monotonic_clock::time_point end_time,
871 bool run_on_logged) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800872 // We want to guarantee that messages aren't out of order by more than
873 // max_out_of_order_duration. To do this, we need sync points. Every write
874 // cycle should be a sync point.
875
876 do {
877 // Move the sync point up by at most polling_period. This forces one sync
878 // per iteration, even if it is small.
879 LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time));
880
Austin Schuh30586902021-03-30 22:54:08 -0700881 if (run_on_logged) {
882 on_logged_period_();
883 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800884
885 // If we missed cycles, we could be pretty far behind. Spin until we are
886 // caught up.
887 } while (last_synchronized_time_ + polling_period_ < end_time);
888}
889
890void Logger::RecordFetchResult(aos::monotonic_clock::time_point start,
891 aos::monotonic_clock::time_point end,
892 bool got_new, FetcherStruct *fetcher) {
893 const auto duration = end - start;
894 if (!got_new) {
895 ++total_nop_fetch_count_;
896 total_nop_fetch_time_ += duration;
897 return;
898 }
899 ++total_message_fetch_count_;
900 total_message_fetch_bytes_ += fetcher->fetcher->context().size;
901 total_message_fetch_time_ += duration;
902 if (duration > max_message_fetch_time_) {
903 max_message_fetch_time_ = duration;
904 max_message_fetch_time_channel_ = fetcher->channel_index;
905 max_message_fetch_time_size_ = fetcher->fetcher->context().size;
906 }
907}
908
909void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start,
910 aos::monotonic_clock::time_point end,
Brian Smartt03c00da2022-02-24 10:25:00 -0800911 const FetcherStruct &fetcher) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800912 const auto duration = end - start;
913 total_copy_time_ += duration;
914 ++total_copy_count_;
Brian Smartt03c00da2022-02-24 10:25:00 -0800915 total_copy_bytes_ += fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800916 if (duration > max_copy_time_) {
917 max_copy_time_ = duration;
Brian Smartt03c00da2022-02-24 10:25:00 -0800918 max_copy_time_channel_ = fetcher.channel_index;
919 max_copy_time_size_ = fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800920 }
921}
922
923} // namespace logger
924} // namespace aos