blob: d559311ceda5d7c1cc01b9d99a6cfe3e41537dda [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 Schuhb06f03b2021-02-17 22:00:37 -080014
15namespace aos {
16namespace logger {
17namespace {
18using message_bridge::RemoteMessage;
Austin Schuhbd06ae42021-03-31 22:48:21 -070019namespace chrono = std::chrono;
Austin Schuhb06f03b2021-02-17 22:00:37 -080020} // namespace
21
22Logger::Logger(EventLoop *event_loop, const Configuration *configuration,
23 std::function<bool(const Channel *)> should_log)
24 : event_loop_(event_loop),
25 configuration_(configuration),
Austin Schuh5b728b72021-06-16 14:57:15 -070026 node_(configuration::GetNode(configuration_, event_loop->node())),
27 node_index_(configuration::GetNodeIndex(configuration_, node_)),
Austin Schuhb06f03b2021-02-17 22:00:37 -080028 name_(network::GetHostname()),
29 timer_handler_(event_loop_->AddTimer(
Austin Schuh30586902021-03-30 22:54:08 -070030 [this]() { DoLogData(event_loop_->monotonic_now(), true); })),
Austin Schuhb06f03b2021-02-17 22:00:37 -080031 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 Schuh58646e22021-08-23 23:51:46 -070036 timer_handler_->set_name("channel_poll");
Austin Schuh5b728b72021-06-16 14:57:15 -070037 VLOG(1) << "Creating logger for " << FlatbufferToJson(node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -080038
Naman Gupta41d70c22022-11-21 15:29:52 -080039 // When we are logging remote timestamps, we need to be able to translate
40 // from the channel index that the event loop uses to the channel index in
41 // the config in the log file.
Austin Schuh01f3b392022-01-25 20:03:09 -080042 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 Schuhb06f03b2021-02-17 22:00:37 -080066
Austin Schuh61e973f2021-02-21 21:43:56 -080067 message_bridge::ChannelTimestampFinder finder(event_loop_);
68 for (const Channel *channel : *event_loop_->configuration()->channels()) {
69 if (!configuration::ChannelIsSendableOnNode(channel, event_loop_->node())) {
Austin Schuhb06f03b2021-02-17 22:00:37 -080070 continue;
71 }
Austin Schuh61e973f2021-02-21 21:43:56 -080072 if (!channel->has_destination_nodes()) {
73 continue;
74 }
Austin Schuh01f3b392022-01-25 20:03:09 -080075 const size_t channel_index =
76 configuration::ChannelIndex(event_loop_->configuration(), channel);
77
Austin Schuh61e973f2021-02-21 21:43:56 -080078 for (const Connection *connection : *channel->destination_nodes()) {
79 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(
80 connection, event_loop_->node())) {
81 const Node *other_node = configuration::GetNode(
Austin Schuh5b728b72021-06-16 14:57:15 -070082 configuration_, connection->name()->string_view());
Austin Schuh61e973f2021-02-21 21:43:56 -080083
84 VLOG(1) << "Timestamps are logged from "
85 << FlatbufferToJson(other_node);
Austin Schuh01f3b392022-01-25 20:03:09 -080086 // 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 Smartt796cca02022-04-12 15:07:21 -070098 std::get<2>(it->second)[channel_index] =
99 (connection->time_to_live() == 0);
Austin Schuh01f3b392022-01-25 20:03:09 -0800100 } 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 Schuh61e973f2021-02-21 21:43:56 -0800118 }
119 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800120 }
121
Austin Schuhb06f03b2021-02-17 22:00:37 -0800122 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 Schuh5b728b72021-06-16 14:57:15 -0700135 if (!should_log(config_channel)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800136 continue;
137 }
138
139 FetcherStruct fs;
140 fs.channel_index = channel_index;
141 fs.channel = channel;
142
143 const bool is_local =
Austin Schuh5b728b72021-06-16 14:57:15 -0700144 configuration::ChannelIsSendableOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800145
146 const bool is_readable =
Austin Schuh5b728b72021-06-16 14:57:15 -0700147 configuration::ChannelIsReadableOnNode(config_channel, node_);
Austin Schuh01f3b392022-01-25 20:03:09 -0800148 const bool is_logged =
149 configuration::ChannelMessageIsLoggedOnNode(config_channel, node_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800150 const bool log_message = is_logged && is_readable;
151
152 bool log_delivery_times = false;
Austin Schuh5b728b72021-06-16 14:57:15 -0700153 if (configuration::MultiNode(configuration_)) {
Austin Schuh72211ae2021-08-05 14:02:30 -0700154 const aos::Connection *connection =
Austin Schuh5b728b72021-06-16 14:57:15 -0700155 configuration::ConnectionToNode(config_channel, node_);
Austin Schuh72211ae2021-08-05 14:02:30 -0700156
Austin Schuhb06f03b2021-02-17 22:00:37 -0800157 log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh72211ae2021-08-05 14:02:30 -0700158 connection, event_loop_->node());
159
160 CHECK_EQ(log_delivery_times,
161 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
Austin Schuh5b728b72021-06-16 14:57:15 -0700162 config_channel, node_, node_));
Austin Schuh72211ae2021-08-05 14:02:30 -0700163
164 if (connection) {
165 fs.reliable_forwarding = (connection->time_to_live() == 0);
166 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800167 }
168
Austin Schuh01f3b392022-01-25 20:03:09 -0800169 // Now, detect a RemoteMessage timestamp logger where we should just log
170 // the contents to a file directly.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800171 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 Schuh5b728b72021-06-16 14:57:15 -0700182 fs.timestamp_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800183 }
Austin Schuhe46492f2021-07-31 19:49:41 -0700184 // 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 Schuhb06f03b2021-02-17 22:00:37 -0800187 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 Schuhe46492f2021-07-31 19:49:41 -0700192 }
193 }
194 if (log_message) {
195 VLOG(1) << " Data";
196 fs.wants_writer = true;
197 if (!is_local) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800198 fs.log_type = LogType::kLogRemoteMessage;
199 } else {
Austin Schuh5b728b72021-06-16 14:57:15 -0700200 fs.data_node_index = static_cast<int>(node_index_);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800201 }
202 }
203 if (log_contents) {
204 VLOG(1) << "Timestamp logger channel "
205 << configuration::CleanedChannelToString(channel);
Austin Schuh01f3b392022-01-25 20:03:09 -0800206 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 Schuhb06f03b2021-02-17 22:00:37 -0800214 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 Schuhb06f03b2021-02-17 22:00:37 -0800221}
222
223Logger::~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 Schuh01f3b392022-01-25 20:03:09 -0800227 // poll everything as we go to destroy the class, ie, shut down the
228 // logger, and write it to disk.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800229 StopLogging(event_loop_->monotonic_now());
230 }
231}
232
Brian Smartt796cca02022-04-12 15:07:21 -0700233std::string Logger::WriteConfiguration(LogNamer *log_namer) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800234 std::string config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800235
Austin Schuhb06f03b2021-02-17 22:00:37 -0800236 if (separate_config_) {
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 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config_header(
244 fbb.Release());
245 config_sha256 = Sha256(config_header.span());
246 LOG(INFO) << "Config sha256 of " << config_sha256;
Brian Smartt03c00da2022-02-24 10:25:00 -0800247 log_namer->WriteConfiguration(&config_header, config_sha256);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800248 }
249
Brian Smartt03c00da2022-02-24 10:25:00 -0800250 return config_sha256;
251}
252
253void Logger::StartLogging(std::unique_ptr<LogNamer> log_namer,
254 std::optional<UUID> log_start_uuid) {
255 CHECK(!log_namer_) << ": Already logging";
256
257 VLOG(1) << "Starting logger for " << FlatbufferToJson(node_);
258
259 auto config_sha256 = WriteConfiguration(log_namer.get());
260
261 log_namer_ = std::move(log_namer);
262
Austin Schuhb06f03b2021-02-17 22:00:37 -0800263 log_event_uuid_ = UUID::Random();
264 log_start_uuid_ = log_start_uuid;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800265
266 // We want to do as much work as possible before the initial Fetch. Time
267 // between that and actually starting to log opens up the possibility of
268 // falling off the end of the queue during that time.
269
270 for (FetcherStruct &f : fetchers_) {
271 if (f.wants_writer) {
272 f.writer = log_namer_->MakeWriter(f.channel);
273 }
274 if (f.wants_timestamp_writer) {
275 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
276 }
277 if (f.wants_contents_writer) {
278 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
279 f.channel, CHECK_NOTNULL(f.timestamp_node));
280 }
281 }
282
Austin Schuh73340842021-07-30 22:32:06 -0700283 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
Austin Schuhb06f03b2021-02-17 22:00:37 -0800284
Austin Schuha42ee962021-03-31 22:49:30 -0700285 const aos::monotonic_clock::time_point beginning_time =
286 event_loop_->monotonic_now();
287
Austin Schuhb06f03b2021-02-17 22:00:37 -0800288 // Grab data from each channel right before we declare the log file started
289 // so we can capture the latest message on each channel. This lets us have
290 // non periodic messages with configuration that now get logged.
291 for (FetcherStruct &f : fetchers_) {
292 const auto start = event_loop_->monotonic_now();
293 const bool got_new = f.fetcher->Fetch();
294 const auto end = event_loop_->monotonic_now();
295 RecordFetchResult(start, end, got_new, &f);
296
297 // If there is a message, we want to write it.
298 f.written = f.fetcher->context().data == nullptr;
299 }
300
301 // Clear out any old timestamps in case we are re-starting logging.
Austin Schuh572924a2021-07-30 22:32:12 -0700302 for (size_t i = 0; i < configuration::NodesCount(configuration_); ++i) {
Austin Schuh58646e22021-08-23 23:51:46 -0700303 log_namer_->ClearStartTimes();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800304 }
305
Austin Schuha42ee962021-03-31 22:49:30 -0700306 const aos::monotonic_clock::time_point fetch_time =
307 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800308 WriteHeader();
Austin Schuha42ee962021-03-31 22:49:30 -0700309 const aos::monotonic_clock::time_point header_time =
310 event_loop_->monotonic_now();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800311
Brian Smartt796cca02022-04-12 15:07:21 -0700312 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " start_time "
313 << last_synchronized_time_ << ", took "
Brian Smartt03c00da2022-02-24 10:25:00 -0800314 << chrono::duration<double>(fetch_time - beginning_time).count()
315 << " to fetch, "
316 << chrono::duration<double>(header_time - fetch_time).count()
317 << " to write headers, boot uuid " << event_loop_->boot_uuid();
Austin Schuhb06f03b2021-02-17 22:00:37 -0800318
319 // Force logging up until the start of the log file now, so the messages at
320 // the start are always ordered before the rest of the messages.
321 // Note: this ship may have already sailed, but we don't have to make it
322 // worse.
323 // TODO(austin): Test...
Austin Schuh855f8932021-03-19 22:01:32 -0700324 //
Naman Gupta41d70c22022-11-21 15:29:52 -0800325 // This is safe to call here since we have set last_synchronized_time_ as
326 // the same time as in the header, and all the data before it should be
327 // logged without ordering concerns.
Austin Schuhb06f03b2021-02-17 22:00:37 -0800328 LogUntil(last_synchronized_time_);
329
330 timer_handler_->Setup(event_loop_->monotonic_now() + polling_period_,
331 polling_period_);
332}
333
Brian Smartt796cca02022-04-12 15:07:21 -0700334std::unique_ptr<LogNamer> Logger::RestartLogging(
335 std::unique_ptr<LogNamer> log_namer, std::optional<UUID> log_start_uuid) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800336 CHECK(log_namer_) << ": Unexpected restart while not logging";
337
338 VLOG(1) << "Restarting logger for " << FlatbufferToJson(node_);
339
Austin Schuh08dba8f2023-05-01 08:29:30 -0700340 // Make sure not to write past now so we don't risk out of order problems. We
341 // don't want to get into a situation where we write out up to now + 0.1 sec,
342 // and that operation takes ~0.1 seconds, so we end up writing a different
343 // amount of the early and late channels. That would then result in the next
344 // go around finding more than 0.1 sec of data on the early channels.
345 //
346 // Make sure we read up until "now" and log it. This sets us up so that we
347 // are unlikely to fetch a message far in the future and have a ton of data
348 // before the offical start time.
Alexei Strotsbc082d82023-05-03 08:43:42 -0700349 monotonic_clock::time_point newest_record = monotonic_clock::min_time;
Austin Schuh08dba8f2023-05-01 08:29:30 -0700350 while (true) {
351 aos::monotonic_clock::time_point next_time =
352 last_synchronized_time_ + polling_period_;
353 const aos::monotonic_clock::time_point monotonic_now =
354 event_loop_->monotonic_now();
355 if (next_time > monotonic_now) {
356 next_time = monotonic_now;
357 }
358
359 bool wrote_messages = false;
360 std::tie(wrote_messages, newest_record) = LogUntil(next_time);
361
362 if (next_time == monotonic_now &&
363 (!wrote_messages || newest_record < monotonic_now + polling_period_)) {
364 // If we stopped writing messages, then we probably have stopped making
365 // progress. If the newest record (unwritten or written) on a channel is
366 // very close to the current time, then there won't be much data
367 // officially after the end of the last log but before the start of the
368 // current one. We need to pick the start of the current log to be after
369 // the last message on record so we don't have holes in the log.
370 break;
371 }
372 }
373
374 // We are now synchronized up to last_synchronized_time_. Our start time can
375 // safely be "newest_record". But, we need to guarentee that the start time
376 // is after the newest message we have a record of, and that we don't skip any
377 // messages as we rotate. This means we can't call Fetch anywhere.
Brian Smartt03c00da2022-02-24 10:25:00 -0800378
379 std::unique_ptr<LogNamer> old_log_namer = std::move(log_namer_);
380 log_namer_ = std::move(log_namer);
381
Naman Gupta41d70c22022-11-21 15:29:52 -0800382 // Now grab a representative time on both the RT and monotonic clock.
383 // Average a monotonic clock before and after to reduce the error.
Brian Smartt03c00da2022-02-24 10:25:00 -0800384 const aos::monotonic_clock::time_point beginning_time =
385 event_loop_->monotonic_now();
Austin Schuh41f8df92022-04-15 11:45:52 -0700386 const aos::realtime_clock::time_point beginning_time_rt =
387 event_loop_->realtime_now();
388 const aos::monotonic_clock::time_point beginning_time2 =
389 event_loop_->monotonic_now();
390
391 if (beginning_time > last_synchronized_time_) {
392 LOG(WARNING) << "Took over " << polling_period_.count()
393 << "ns to swap log_namer";
394 }
395
Austin Schuh08dba8f2023-05-01 08:29:30 -0700396 // Our start time is now the newest message we have a record of. We will
397 // declare the old log "done", and start in on the new one, double-logging
398 // anything we have a record of so we have all the messages from before the
399 // start.
400 const aos::monotonic_clock::time_point monotonic_start_time = newest_record;
Austin Schuh41f8df92022-04-15 11:45:52 -0700401 const aos::realtime_clock::time_point realtime_start_time =
402 (beginning_time_rt + (monotonic_start_time.time_since_epoch() -
403 ((beginning_time.time_since_epoch() +
404 beginning_time2.time_since_epoch()) /
405 2)));
Brian Smartt03c00da2022-02-24 10:25:00 -0800406
407 auto config_sha256 = WriteConfiguration(log_namer_.get());
408
409 log_event_uuid_ = UUID::Random();
410 log_start_uuid_ = log_start_uuid;
411
412 log_namer_->SetHeaderTemplate(MakeHeader(config_sha256));
413
414 // Note that WriteHeader updates last_synchronized_time_ to be the
415 // current time when it is called, which is then the "start time"
416 // of the new (restarted) log. This timestamp will be after
Naman Gupta41d70c22022-11-21 15:29:52 -0800417 // the timestamp of the last message fetched on each channel, but is
418 // carefully picked per the comment above to not violate
419 // max_out_of_order_duration.
Austin Schuh41f8df92022-04-15 11:45:52 -0700420 WriteHeader(monotonic_start_time, realtime_start_time);
Brian Smartt03c00da2022-02-24 10:25:00 -0800421
422 const aos::monotonic_clock::time_point header_time =
423 event_loop_->monotonic_now();
424
Austin Schuh08dba8f2023-05-01 08:29:30 -0700425 // Close out the old writers to free up memory to be used by the new writers.
426 old_log_namer->Close();
427
Brian Smartt03c00da2022-02-24 10:25:00 -0800428 for (FetcherStruct &f : fetchers_) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800429 // Create writers from the new namer
Brian Smartt03c00da2022-02-24 10:25:00 -0800430
431 if (f.wants_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700432 f.writer = log_namer_->MakeWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800433 }
434 if (f.wants_timestamp_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700435 f.timestamp_writer = log_namer_->MakeTimestampWriter(f.channel);
Brian Smartt03c00da2022-02-24 10:25:00 -0800436 }
437 if (f.wants_contents_writer) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700438 f.contents_writer = log_namer_->MakeForwardedTimestampWriter(
Brian Smartt03c00da2022-02-24 10:25:00 -0800439 f.channel, CHECK_NOTNULL(f.timestamp_node));
440 }
441
Austin Schuh08dba8f2023-05-01 08:29:30 -0700442 // Mark each channel with data as not written. That triggers each channel
443 // to be re-logged.
444 f.written = f.fetcher->context().data == nullptr;
Brian Smartt03c00da2022-02-24 10:25:00 -0800445 }
446
Austin Schuh08dba8f2023-05-01 08:29:30 -0700447 // And now make sure to log everything up to the start time in 1 big go so we
448 // make sure we have it before we let the world start logging normally again.
449 LogUntil(monotonic_start_time);
450
Brian Smartt03c00da2022-02-24 10:25:00 -0800451 const aos::monotonic_clock::time_point channel_time =
452 event_loop_->monotonic_now();
453
Brian Smartt796cca02022-04-12 15:07:21 -0700454 VLOG(1) << "Logging node as " << FlatbufferToJson(node_) << " restart_time "
455 << last_synchronized_time_ << ", took "
Brian Smartt03c00da2022-02-24 10:25:00 -0800456 << chrono::duration<double>(header_time - beginning_time).count()
457 << " to prepare and write header, "
458 << chrono::duration<double>(channel_time - header_time).count()
Brian Smartt796cca02022-04-12 15:07:21 -0700459 << " to write initial channel messages, boot uuid "
460 << event_loop_->boot_uuid();
Brian Smartt03c00da2022-02-24 10:25:00 -0800461
462 return old_log_namer;
463}
464
Austin Schuhb06f03b2021-02-17 22:00:37 -0800465std::unique_ptr<LogNamer> Logger::StopLogging(
466 aos::monotonic_clock::time_point end_time) {
467 CHECK(log_namer_) << ": Not logging right now";
468
469 if (end_time != aos::monotonic_clock::min_time) {
Austin Schuh30586902021-03-30 22:54:08 -0700470 // Folks like to use the on_logged_period_ callback to trigger stop and
471 // start events. We can't have those then recurse and try to stop again.
472 // Rather than making everything reentrant, let's just instead block the
473 // callback here.
474 DoLogData(end_time, false);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800475 }
476 timer_handler_->Disable();
477
478 for (FetcherStruct &f : fetchers_) {
479 f.writer = nullptr;
480 f.timestamp_writer = nullptr;
481 f.contents_writer = nullptr;
482 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800483
484 log_event_uuid_ = UUID::Zero();
Austin Schuh34f9e482021-03-31 22:54:18 -0700485 log_start_uuid_ = std::nullopt;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800486
Alexei Strotsbc776d52023-04-15 23:46:45 -0700487 log_namer_->Close();
488
Austin Schuhb06f03b2021-02-17 22:00:37 -0800489 return std::move(log_namer_);
490}
491
Austin Schuh41f8df92022-04-15 11:45:52 -0700492void Logger::WriteHeader(aos::monotonic_clock::time_point monotonic_start_time,
493 aos::realtime_clock::time_point realtime_start_time) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800494 if (configuration::MultiNode(configuration_)) {
495 server_statistics_fetcher_.Fetch();
496 }
497
Austin Schuh41f8df92022-04-15 11:45:52 -0700498 if (monotonic_start_time == aos::monotonic_clock::min_time) {
499 monotonic_start_time = event_loop_->monotonic_now();
500 realtime_start_time = event_loop_->realtime_now();
501 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800502
503 // We need to pick a point in time to declare the log file "started". This
504 // starts here. It needs to be after everything is fetched so that the
505 // fetchers are all pointed at the most recent message before the start
506 // time.
507 last_synchronized_time_ = monotonic_start_time;
508
509 for (const Node *node : log_namer_->nodes()) {
510 const int node_index = configuration::GetNodeIndex(configuration_, node);
511 MaybeUpdateTimestamp(node, node_index, monotonic_start_time,
512 realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800513 }
514}
515
Austin Schuhb06f03b2021-02-17 22:00:37 -0800516void Logger::WriteMissingTimestamps() {
517 if (configuration::MultiNode(configuration_)) {
518 server_statistics_fetcher_.Fetch();
519 } else {
520 return;
521 }
522
523 if (server_statistics_fetcher_.get() == nullptr) {
524 return;
525 }
526
527 for (const Node *node : log_namer_->nodes()) {
528 const int node_index = configuration::GetNodeIndex(configuration_, node);
529 if (MaybeUpdateTimestamp(
530 node, node_index,
531 server_statistics_fetcher_.context().monotonic_event_time,
532 server_statistics_fetcher_.context().realtime_event_time)) {
Austin Schuh58646e22021-08-23 23:51:46 -0700533 VLOG(1) << "Timestamps changed on " << aos::FlatbufferToJson(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800534 }
535 }
536}
537
Austin Schuhb06f03b2021-02-17 22:00:37 -0800538bool Logger::MaybeUpdateTimestamp(
539 const Node *node, int node_index,
540 aos::monotonic_clock::time_point monotonic_start_time,
541 aos::realtime_clock::time_point realtime_start_time) {
542 // Bail early if the start times are already set.
Austin Schuh58646e22021-08-23 23:51:46 -0700543 if (node_ == node || !configuration::MultiNode(configuration_)) {
544 if (log_namer_->monotonic_start_time(node_index,
545 event_loop_->boot_uuid()) !=
546 monotonic_clock::min_time) {
547 return false;
548 }
Brian Smartt03c00da2022-02-24 10:25:00 -0800549
Austin Schuhb06f03b2021-02-17 22:00:37 -0800550 // There are no offsets to compute for ourself, so always succeed.
Austin Schuh58646e22021-08-23 23:51:46 -0700551 log_namer_->SetStartTimes(node_index, event_loop_->boot_uuid(),
552 monotonic_start_time, realtime_start_time,
553 monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800554 return true;
555 } else if (server_statistics_fetcher_.get() != nullptr) {
556 // We must be a remote node now. Look for the connection and see if it is
557 // connected.
James Kuszmaul17607fb2021-10-15 20:00:32 -0700558 CHECK(server_statistics_fetcher_->has_connections());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800559
560 for (const message_bridge::ServerConnection *connection :
561 *server_statistics_fetcher_->connections()) {
562 if (connection->node()->name()->string_view() !=
563 node->name()->string_view()) {
564 continue;
565 }
566
567 if (connection->state() != message_bridge::State::CONNECTED) {
568 VLOG(1) << node->name()->string_view()
569 << " is not connected, can't start it yet.";
570 break;
571 }
572
Austin Schuhb06f03b2021-02-17 22:00:37 -0800573 if (!connection->has_monotonic_offset()) {
574 VLOG(1) << "Missing monotonic offset for setting start time for node "
575 << aos::FlatbufferToJson(node);
576 break;
577 }
578
James Kuszmaul17607fb2021-10-15 20:00:32 -0700579 CHECK(connection->has_boot_uuid());
Austin Schuh58646e22021-08-23 23:51:46 -0700580 const UUID boot_uuid =
581 UUID::FromString(connection->boot_uuid()->string_view());
582
583 if (log_namer_->monotonic_start_time(node_index, boot_uuid) !=
584 monotonic_clock::min_time) {
585 break;
586 }
587
588 VLOG(1) << "Updating start time for "
589 << aos::FlatbufferToJson(connection);
590
Austin Schuhb06f03b2021-02-17 22:00:37 -0800591 // Found it and it is connected. Compensate and go.
Austin Schuh73340842021-07-30 22:32:06 -0700592 log_namer_->SetStartTimes(
Austin Schuh58646e22021-08-23 23:51:46 -0700593 node_index, boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700594 monotonic_start_time +
595 std::chrono::nanoseconds(connection->monotonic_offset()),
596 realtime_start_time, monotonic_start_time, realtime_start_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800597 return true;
598 }
599 }
600 return false;
601}
602
603aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> Logger::MakeHeader(
Austin Schuh73340842021-07-30 22:32:06 -0700604 std::string_view config_sha256) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800605 flatbuffers::FlatBufferBuilder fbb;
606 fbb.ForceDefaults(true);
607
608 flatbuffers::Offset<aos::Configuration> configuration_offset;
609 if (!separate_config_) {
610 configuration_offset = CopyFlatBuffer(configuration_, &fbb);
611 } else {
612 CHECK(!config_sha256.empty());
613 }
614
615 const flatbuffers::Offset<flatbuffers::String> name_offset =
616 fbb.CreateString(name_);
617
Austin Schuhfa712682022-05-11 16:43:42 -0700618 const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset =
619 logger_sha1_.empty() ? 0 : fbb.CreateString(logger_sha1_);
620 const flatbuffers::Offset<flatbuffers::String> logger_version_offset =
621 logger_version_.empty() ? 0 : fbb.CreateString(logger_version_);
622
Austin Schuhb06f03b2021-02-17 22:00:37 -0800623 CHECK(log_event_uuid_ != UUID::Zero());
624 const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800625 log_event_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800626
627 const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800628 logger_instance_uuid_.PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800629
630 flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset;
Austin Schuh34f9e482021-03-31 22:54:18 -0700631 if (log_start_uuid_) {
632 log_start_uuid_offset = fbb.CreateString(log_start_uuid_->ToString());
Austin Schuhb06f03b2021-02-17 22:00:37 -0800633 }
634
635 flatbuffers::Offset<flatbuffers::String> config_sha256_offset;
636 if (!config_sha256.empty()) {
637 config_sha256_offset = fbb.CreateString(config_sha256);
638 }
639
640 const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800641 event_loop_->boot_uuid().PackString(&fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800642
Austin Schuhb06f03b2021-02-17 22:00:37 -0800643 flatbuffers::Offset<Node> logger_node_offset;
644
645 if (configuration::MultiNode(configuration_)) {
Austin Schuh5b728b72021-06-16 14:57:15 -0700646 logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800647 }
648
649 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
650
651 log_file_header_builder.add_name(name_offset);
Austin Schuhfa712682022-05-11 16:43:42 -0700652 if (!logger_sha1_offset.IsNull()) {
653 log_file_header_builder.add_logger_sha1(logger_sha1_offset);
654 }
655 if (!logger_version_offset.IsNull()) {
656 log_file_header_builder.add_logger_version(logger_version_offset);
657 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800658
659 // Only add the node if we are running in a multinode configuration.
Austin Schuh73340842021-07-30 22:32:06 -0700660 if (configuration::MultiNode(configuration_)) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800661 log_file_header_builder.add_logger_node(logger_node_offset);
662 }
663
664 if (!configuration_offset.IsNull()) {
665 log_file_header_builder.add_configuration(configuration_offset);
666 }
667 // The worst case theoretical out of order is the polling period times 2.
668 // One message could get logged right after the boundary, but be for right
669 // before the next boundary. And the reverse could happen for another
670 // message. Report back 3x to be extra safe, and because the cost isn't
671 // huge on the read side.
672 log_file_header_builder.add_max_out_of_order_duration(
673 std::chrono::nanoseconds(3 * polling_period_).count());
674
Austin Schuhb06f03b2021-02-17 22:00:37 -0800675 log_file_header_builder.add_log_event_uuid(log_event_uuid_offset);
676 log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset);
677 if (!log_start_uuid_offset.IsNull()) {
678 log_file_header_builder.add_log_start_uuid(log_start_uuid_offset);
679 }
680 log_file_header_builder.add_logger_node_boot_uuid(
681 logger_node_boot_uuid_offset);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800682
683 if (!config_sha256_offset.IsNull()) {
684 log_file_header_builder.add_configuration_sha256(config_sha256_offset);
685 }
686
687 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
688 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result(
689 fbb.Release());
690
691 CHECK(result.Verify()) << ": Built a corrupted header.";
692
693 return result;
694}
695
696void Logger::ResetStatisics() {
697 max_message_fetch_time_ = std::chrono::nanoseconds::zero();
698 max_message_fetch_time_channel_ = -1;
699 max_message_fetch_time_size_ = -1;
700 total_message_fetch_time_ = std::chrono::nanoseconds::zero();
701 total_message_fetch_count_ = 0;
702 total_message_fetch_bytes_ = 0;
703 total_nop_fetch_time_ = std::chrono::nanoseconds::zero();
704 total_nop_fetch_count_ = 0;
705 max_copy_time_ = std::chrono::nanoseconds::zero();
706 max_copy_time_channel_ = -1;
707 max_copy_time_size_ = -1;
708 total_copy_time_ = std::chrono::nanoseconds::zero();
709 total_copy_count_ = 0;
710 total_copy_bytes_ = 0;
711}
712
713void Logger::Rotate() {
714 for (const Node *node : log_namer_->nodes()) {
Austin Schuh73340842021-07-30 22:32:06 -0700715 log_namer_->Rotate(node);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800716 }
717}
718
Brian Smartt03c00da2022-02-24 10:25:00 -0800719void Logger::WriteData(NewDataWriter *writer, const FetcherStruct &f) {
720 if (writer != nullptr) {
721 const UUID source_node_boot_uuid =
722 static_cast<int>(node_index_) != f.data_node_index
723 ? f.fetcher->context().source_boot_uuid
724 : event_loop_->boot_uuid();
725 // Write!
726 const auto start = event_loop_->monotonic_now();
Brian Smartt03c00da2022-02-24 10:25:00 -0800727
Naman Gupta41d70c22022-11-21 15:29:52 -0800728 ContextDataCopier coppier(f.fetcher->context(), f.channel_index, f.log_type,
729 event_loop_);
Brian Smartt03c00da2022-02-24 10:25:00 -0800730
Austin Schuh48d10d62022-10-16 22:19:23 -0700731 writer->CopyMessage(&coppier, source_node_boot_uuid, start);
732 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800733
Brian Smartt796cca02022-04-12 15:07:21 -0700734 VLOG(2) << "Wrote data as node " << FlatbufferToJson(node_)
735 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800736 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700737 << " to " << writer->name();
Brian Smartt03c00da2022-02-24 10:25:00 -0800738 }
739}
740
Brian Smartt796cca02022-04-12 15:07:21 -0700741void Logger::WriteTimestamps(NewDataWriter *timestamp_writer,
742 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800743 if (timestamp_writer != nullptr) {
744 // And now handle timestamps.
Brian Smartt03c00da2022-02-24 10:25:00 -0800745
746 // Tell our writer that we know something about the remote boot.
747 timestamp_writer->UpdateRemote(
748 f.data_node_index, f.fetcher->context().source_boot_uuid,
749 f.fetcher->context().monotonic_remote_time,
750 f.fetcher->context().monotonic_event_time, f.reliable_forwarding);
Austin Schuh48d10d62022-10-16 22:19:23 -0700751
752 const auto start = event_loop_->monotonic_now();
753 ContextDataCopier coppier(f.fetcher->context(), f.channel_index,
Naman Gupta41d70c22022-11-21 15:29:52 -0800754 LogType::kLogDeliveryTimeOnly, event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700755
756 timestamp_writer->CopyMessage(&coppier, event_loop_->boot_uuid(), start);
757 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800758
Brian Smartt796cca02022-04-12 15:07:21 -0700759 VLOG(2) << "Wrote timestamps as node " << FlatbufferToJson(node_)
760 << " for channel "
Brian Smartt03c00da2022-02-24 10:25:00 -0800761 << configuration::CleanedChannelToString(f.fetcher->channel())
Alexei Strotsbc082d82023-05-03 08:43:42 -0700762 << " to " << timestamp_writer->name() << " timestamp";
Brian Smartt03c00da2022-02-24 10:25:00 -0800763 }
764}
765
Brian Smartt796cca02022-04-12 15:07:21 -0700766void Logger::WriteContent(NewDataWriter *contents_writer,
767 const FetcherStruct &f) {
Brian Smartt03c00da2022-02-24 10:25:00 -0800768 if (contents_writer != nullptr) {
769 const auto start = event_loop_->monotonic_now();
770 // And now handle the special message contents channel. Copy the
771 // message into a FlatBufferBuilder and save it to disk.
Brian Smartt03c00da2022-02-24 10:25:00 -0800772 const RemoteMessage *msg =
773 flatbuffers::GetRoot<RemoteMessage>(f.fetcher->context().data);
774
775 CHECK(msg->has_boot_uuid()) << ": " << aos::FlatbufferToJson(msg);
Brian Smartt03c00da2022-02-24 10:25:00 -0800776 // Translate from the channel index that the event loop uses to the
777 // channel index in the log file.
Austin Schuhf2d0e682022-10-16 14:20:58 -0700778 const int channel_index =
779 event_loop_to_logged_channel_index_[msg->channel_index()];
Brian Smartt03c00da2022-02-24 10:25:00 -0800780
781 const aos::monotonic_clock::time_point monotonic_timestamp_time =
782 f.fetcher->context().monotonic_event_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800783
Brian Smartt03c00da2022-02-24 10:25:00 -0800784 // Timestamps tell us information about what happened too!
785 // Capture any reboots so UpdateRemote is properly recorded.
786 contents_writer->UpdateBoot(UUID::FromVector(msg->boot_uuid()));
787
788 // Start with recording info about the data flowing from our node to the
789 // remote.
790 const bool reliable =
791 f.channel_reliable_contents.size() != 0u
792 ? f.channel_reliable_contents[msg->channel_index()]
793 : f.reliable_contents;
794
Brian Smartt796cca02022-04-12 15:07:21 -0700795 contents_writer->UpdateRemote(
796 node_index_, event_loop_->boot_uuid(),
Brian Smartt03c00da2022-02-24 10:25:00 -0800797 monotonic_clock::time_point(
798 chrono::nanoseconds(msg->monotonic_remote_time())),
799 monotonic_clock::time_point(
800 chrono::nanoseconds(msg->monotonic_sent_time())),
801 reliable, monotonic_timestamp_time);
802
Austin Schuh48d10d62022-10-16 22:19:23 -0700803 RemoteMessageCopier coppier(msg, channel_index, monotonic_timestamp_time,
Naman Gupta41d70c22022-11-21 15:29:52 -0800804 event_loop_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700805
806 contents_writer->CopyMessage(&coppier, UUID::FromVector(msg->boot_uuid()),
807 start);
808
809 RecordCreateMessageTime(start, coppier.end_time(), f);
Brian Smartt03c00da2022-02-24 10:25:00 -0800810 }
811}
812
813void Logger::WriteFetchedRecord(FetcherStruct &f) {
814 WriteData(f.writer, f);
815 WriteTimestamps(f.timestamp_writer, f);
816 WriteContent(f.contents_writer, f);
817}
818
Austin Schuh08dba8f2023-05-01 08:29:30 -0700819std::pair<bool, monotonic_clock::time_point> Logger::LogUntil(
820 monotonic_clock::time_point t) {
821 bool wrote_messages = false;
822 monotonic_clock::time_point newest_record = monotonic_clock::min_time;
Brian Smartt03c00da2022-02-24 10:25:00 -0800823
Austin Schuhb06f03b2021-02-17 22:00:37 -0800824 // Grab the latest ServerStatistics message. This will always have the
825 // oppertunity to be >= to the current time, so it will always represent any
826 // reboots which may have happened.
827 WriteMissingTimestamps();
828
829 // Write each channel to disk, one at a time.
830 for (FetcherStruct &f : fetchers_) {
Austin Schuh08dba8f2023-05-01 08:29:30 -0700831 if (f.fetcher->context().data != nullptr) {
832 newest_record =
833 std::max(newest_record, f.fetcher->context().monotonic_event_time);
834 }
835
Austin Schuhb06f03b2021-02-17 22:00:37 -0800836 while (true) {
837 if (f.written) {
838 const auto start = event_loop_->monotonic_now();
839 const bool got_new = f.fetcher->FetchNext();
840 const auto end = event_loop_->monotonic_now();
841 RecordFetchResult(start, end, got_new, &f);
842 if (!got_new) {
843 VLOG(2) << "No new data on "
844 << configuration::CleanedChannelToString(
845 f.fetcher->channel());
846 break;
847 }
Austin Schuh08dba8f2023-05-01 08:29:30 -0700848 newest_record =
849 std::max(newest_record, f.fetcher->context().monotonic_event_time);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800850 f.written = false;
851 }
852
853 // TODO(james): Write tests to exercise this logic.
854 if (f.fetcher->context().monotonic_event_time >= t) {
855 break;
856 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800857
Brian Smartt03c00da2022-02-24 10:25:00 -0800858 WriteFetchedRecord(f);
Austin Schuh08dba8f2023-05-01 08:29:30 -0700859 wrote_messages = true;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800860
861 f.written = true;
862 }
863 }
864 last_synchronized_time_ = t;
Brian Smartt03c00da2022-02-24 10:25:00 -0800865
Austin Schuh08dba8f2023-05-01 08:29:30 -0700866 return std::make_pair(wrote_messages, newest_record);
Austin Schuhb06f03b2021-02-17 22:00:37 -0800867}
868
Austin Schuh30586902021-03-30 22:54:08 -0700869void Logger::DoLogData(const monotonic_clock::time_point end_time,
870 bool run_on_logged) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800871 // We want to guarantee that messages aren't out of order by more than
872 // max_out_of_order_duration. To do this, we need sync points. Every write
873 // cycle should be a sync point.
874
875 do {
876 // Move the sync point up by at most polling_period. This forces one sync
877 // per iteration, even if it is small.
878 LogUntil(std::min(last_synchronized_time_ + polling_period_, end_time));
879
Austin Schuh30586902021-03-30 22:54:08 -0700880 if (run_on_logged) {
881 on_logged_period_();
882 }
Austin Schuhb06f03b2021-02-17 22:00:37 -0800883
884 // If we missed cycles, we could be pretty far behind. Spin until we are
885 // caught up.
886 } while (last_synchronized_time_ + polling_period_ < end_time);
887}
888
889void Logger::RecordFetchResult(aos::monotonic_clock::time_point start,
890 aos::monotonic_clock::time_point end,
891 bool got_new, FetcherStruct *fetcher) {
892 const auto duration = end - start;
893 if (!got_new) {
894 ++total_nop_fetch_count_;
895 total_nop_fetch_time_ += duration;
896 return;
897 }
898 ++total_message_fetch_count_;
899 total_message_fetch_bytes_ += fetcher->fetcher->context().size;
900 total_message_fetch_time_ += duration;
901 if (duration > max_message_fetch_time_) {
902 max_message_fetch_time_ = duration;
903 max_message_fetch_time_channel_ = fetcher->channel_index;
904 max_message_fetch_time_size_ = fetcher->fetcher->context().size;
905 }
906}
907
908void Logger::RecordCreateMessageTime(aos::monotonic_clock::time_point start,
909 aos::monotonic_clock::time_point end,
Brian Smartt03c00da2022-02-24 10:25:00 -0800910 const FetcherStruct &fetcher) {
Austin Schuhb06f03b2021-02-17 22:00:37 -0800911 const auto duration = end - start;
912 total_copy_time_ += duration;
913 ++total_copy_count_;
Brian Smartt03c00da2022-02-24 10:25:00 -0800914 total_copy_bytes_ += fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800915 if (duration > max_copy_time_) {
916 max_copy_time_ = duration;
Brian Smartt03c00da2022-02-24 10:25:00 -0800917 max_copy_time_channel_ = fetcher.channel_index;
918 max_copy_time_size_ = fetcher.fetcher->context().size;
Austin Schuhb06f03b2021-02-17 22:00:37 -0800919 }
920}
921
922} // namespace logger
923} // namespace aos