blob: 6c671300d0429cd0bb660637327404b106f98e72 [file] [log] [blame]
Austin Schuhe309d2a2019-11-29 13:25:21 -08001#ifndef AOS_EVENTS_LOGGER_H_
2#define AOS_EVENTS_LOGGER_H_
3
Austin Schuh8bd96322020-02-13 21:18:22 -08004#include <chrono>
Austin Schuhe309d2a2019-11-29 13:25:21 -08005#include <deque>
Austin Schuh05b70472020-01-01 17:11:17 -08006#include <string_view>
Austin Schuh2f8fd752020-09-01 22:38:28 -07007#include <tuple>
Austin Schuh6f3babe2020-01-26 20:34:50 -08008#include <vector>
Austin Schuhe309d2a2019-11-29 13:25:21 -08009
Austin Schuh8bd96322020-02-13 21:18:22 -080010#include "Eigen/Dense"
11#include "absl/strings/str_cat.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080012#include "absl/types/span.h"
13#include "aos/events/event_loop.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070014#include "aos/events/logging/eigen_mpq.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070015#include "aos/events/logging/log_namer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080016#include "aos/events/logging/logfile_utils.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080017#include "aos/events/logging/logger_generated.h"
Austin Schuh64fab802020-09-09 22:47:47 -070018#include "aos/events/logging/uuid.h"
Austin Schuh92547522019-12-28 14:33:43 -080019#include "aos/events/simulated_event_loop.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070020#include "aos/network/message_bridge_server_generated.h"
Austin Schuh8bd96322020-02-13 21:18:22 -080021#include "aos/network/timestamp_filter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080022#include "aos/time/time.h"
23#include "flatbuffers/flatbuffers.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070024#include "third_party/gmp/gmpxx.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080025
26namespace aos {
27namespace logger {
28
Austin Schuhe309d2a2019-11-29 13:25:21 -080029// Logs all channels available in the event loop to disk every 100 ms.
30// Start by logging one message per channel to capture any state and
31// configuration that is sent rately on a channel and would affect execution.
32class Logger {
33 public:
Austin Schuh2f8fd752020-09-01 22:38:28 -070034 Logger(std::string_view base_name, EventLoop *event_loop,
Austin Schuhe309d2a2019-11-29 13:25:21 -080035 std::chrono::milliseconds polling_period =
36 std::chrono::milliseconds(100));
Austin Schuh6f3babe2020-01-26 20:34:50 -080037 Logger(std::unique_ptr<LogNamer> log_namer, EventLoop *event_loop,
38 std::chrono::milliseconds polling_period =
39 std::chrono::milliseconds(100));
Austin Schuhe309d2a2019-11-29 13:25:21 -080040
Austin Schuh2f8fd752020-09-01 22:38:28 -070041 // Rotates the log file(s), triggering new part files to be written for each
42 // log file.
43 void Rotate();
Austin Schuhfa895892020-01-07 20:07:41 -080044
Austin Schuhe309d2a2019-11-29 13:25:21 -080045 private:
Austin Schuhfa895892020-01-07 20:07:41 -080046 void WriteHeader();
Austin Schuh2f8fd752020-09-01 22:38:28 -070047 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
48 const Node *node);
49
50 bool MaybeUpdateTimestamp(
51 const Node *node, int node_index,
52 aos::monotonic_clock::time_point monotonic_start_time,
53 aos::realtime_clock::time_point realtime_start_time);
Austin Schuhfa895892020-01-07 20:07:41 -080054
Austin Schuhe309d2a2019-11-29 13:25:21 -080055 void DoLogData();
56
Austin Schuh2f8fd752020-09-01 22:38:28 -070057 void WriteMissingTimestamps();
58
59 void StartLogging();
60
61 // Fetches from each channel until all the data is logged.
62 void LogUntil(monotonic_clock::time_point t);
63
Austin Schuhe309d2a2019-11-29 13:25:21 -080064 EventLoop *event_loop_;
Austin Schuh64fab802020-09-09 22:47:47 -070065 const UUID uuid_;
Austin Schuh6f3babe2020-01-26 20:34:50 -080066 std::unique_ptr<LogNamer> log_namer_;
Austin Schuhe309d2a2019-11-29 13:25:21 -080067
68 // Structure to track both a fetcher, and if the data fetched has been
69 // written. We may want to delay writing data to disk so that we don't let
70 // data get too far out of order when written to disk so we can avoid making
71 // it too hard to sort when reading.
72 struct FetcherStruct {
73 std::unique_ptr<RawFetcher> fetcher;
74 bool written = false;
Austin Schuh15649d62019-12-28 16:36:38 -080075
Austin Schuh6f3babe2020-01-26 20:34:50 -080076 int channel_index = -1;
77
78 LogType log_type = LogType::kLogMessage;
79
80 DetachedBufferWriter *writer = nullptr;
81 DetachedBufferWriter *timestamp_writer = nullptr;
Austin Schuh2f8fd752020-09-01 22:38:28 -070082 DetachedBufferWriter *contents_writer = nullptr;
83 const Node *writer_node = nullptr;
84 const Node *timestamp_node = nullptr;
85 int node_index = 0;
Austin Schuhe309d2a2019-11-29 13:25:21 -080086 };
87
88 std::vector<FetcherStruct> fetchers_;
89 TimerHandler *timer_handler_;
90
91 // Period to poll the channels.
92 const std::chrono::milliseconds polling_period_;
93
94 // Last time that data was written for all channels to disk.
95 monotonic_clock::time_point last_synchronized_time_;
96
Austin Schuhfa895892020-01-07 20:07:41 -080097 monotonic_clock::time_point monotonic_start_time_;
98 realtime_clock::time_point realtime_start_time_;
99
Austin Schuhe309d2a2019-11-29 13:25:21 -0800100 // Max size that the header has consumed. This much extra data will be
101 // reserved in the builder to avoid reallocating.
102 size_t max_header_size_ = 0;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700103
104 // Fetcher for all the statistics from all the nodes.
105 aos::Fetcher<message_bridge::ServerStatistics> server_statistics_fetcher_;
106
107 // Sets the start time for a specific node.
108 void SetStartTime(size_t node_index,
109 aos::monotonic_clock::time_point monotonic_start_time,
110 aos::realtime_clock::time_point realtime_start_time);
111
112 struct NodeState {
113 aos::monotonic_clock::time_point monotonic_start_time =
114 aos::monotonic_clock::min_time;
115 aos::realtime_clock::time_point realtime_start_time =
116 aos::realtime_clock::min_time;
117
118 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> log_file_header =
119 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
120 };
121 std::vector<NodeState> node_state_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800122};
123
Austin Schuh6f3babe2020-01-26 20:34:50 -0800124// We end up with one of the following 3 log file types.
125//
126// Single node logged as the source node.
127// -> Replayed just on the source node.
128//
129// Forwarding timestamps only logged from the perspective of the destination
130// node.
131// -> Matched with data on source node and logged.
132//
133// Forwarding timestamps with data logged as the destination node.
134// -> Replayed just as the destination
135// -> Replayed as the source (Much harder, ordering is not defined)
136//
137// Duplicate data logged. -> CHECK that it matches and explode otherwise.
138//
139// This can be boiled down to a set of constraints and tools.
140//
141// 1) Forwarding timestamps and data need to be logged separately.
142// 2) Any forwarded data logged on the destination node needs to be logged
143// separately such that it can be sorted.
144//
145// 1) Log reader needs to be able to sort a list of log files.
146// 2) Log reader needs to be able to merge sorted lists of log files.
147// 3) Log reader needs to be able to match timestamps with messages.
148//
149// We also need to be able to generate multiple views of a log file depending on
150// the target.
151
Austin Schuhe309d2a2019-11-29 13:25:21 -0800152// Replays all the channels in the logfile to the event loop.
153class LogReader {
154 public:
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800155 // If you want to supply a new configuration that will be used for replay
156 // (e.g., to change message rates, or to populate an updated schema), then
157 // pass it in here. It must provide all the channels that the original logged
158 // config did.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800159 //
160 // Log filenames are in the following format:
161 //
162 // {
163 // {log1_part0, log1_part1, ...},
164 // {log2}
165 // }
166 // The inner vector is a list of log file chunks which form up a log file.
167 // The outer vector is a list of log files with subsets of the messages, or
168 // messages from different nodes.
169 //
170 // If the outer vector isn't provided, it is assumed to be of size 1.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800171 LogReader(std::string_view filename,
172 const Configuration *replay_configuration = nullptr);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800173 LogReader(const std::vector<std::string> &filenames,
174 const Configuration *replay_configuration = nullptr);
175 LogReader(const std::vector<std::vector<std::string>> &filenames,
Austin Schuhfa895892020-01-07 20:07:41 -0800176 const Configuration *replay_configuration = nullptr);
James Kuszmaul7daef362019-12-31 18:28:17 -0800177 ~LogReader();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800178
Austin Schuh6331ef92020-01-07 18:28:09 -0800179 // Registers all the callbacks to send the log file data out on an event loop
180 // created in event_loop_factory. This also updates time to be at the start
181 // of the log file by running until the log file starts.
182 // Note: the configuration used in the factory should be configuration()
183 // below, but can be anything as long as the locations needed to send
184 // everything are available.
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800185 void Register(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuh6331ef92020-01-07 18:28:09 -0800186 // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(),
187 // and then calls Register.
188 void Register();
189 // Registers callbacks for all the events after the log file starts. This is
190 // only useful when replaying live.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800191 void Register(EventLoop *event_loop);
Austin Schuh6331ef92020-01-07 18:28:09 -0800192
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800193 // Unregisters the senders. You only need to call this if you separately
194 // supplied an event loop or event loop factory and the lifetimes are such
195 // that they need to be explicitly destroyed before the LogReader destructor
196 // gets called.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800197 void Deregister();
198
Austin Schuhe309d2a2019-11-29 13:25:21 -0800199 // Returns the configuration from the log file.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800200 const Configuration *logged_configuration() const;
201 // Returns the configuration being used for replay.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800202 // The pointer is invalidated whenever RemapLoggedChannel is called.
Austin Schuh15649d62019-12-28 16:36:38 -0800203 const Configuration *configuration() const;
204
Austin Schuh6f3babe2020-01-26 20:34:50 -0800205 // Returns the nodes that this log file was created on. This is a list of
206 // pointers to a node in the nodes() list inside configuration(). The
207 // pointers here are invalidated whenever RemapLoggedChannel is called.
208 std::vector<const Node *> Nodes() const;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800209
210 // Returns the starting timestamp for the log file.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800211 monotonic_clock::time_point monotonic_start_time(const Node *node = nullptr);
212 realtime_clock::time_point realtime_start_time(const Node *node = nullptr);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800213
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800214 // Causes the logger to publish the provided channel on a different name so
215 // that replayed applications can publish on the proper channel name without
216 // interference. This operates on raw channel names, without any node or
217 // application specific mappings.
218 void RemapLoggedChannel(std::string_view name, std::string_view type,
219 std::string_view add_prefix = "/original");
220 template <typename T>
221 void RemapLoggedChannel(std::string_view name,
222 std::string_view add_prefix = "/original") {
223 RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix);
224 }
225
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700226 template <typename T>
227 bool HasChannel(std::string_view name) {
228 return configuration::GetChannel(log_file_header()->configuration(), name,
229 T::GetFullyQualifiedName(), "",
230 nullptr) != nullptr;
231 }
232
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800233 SimulatedEventLoopFactory *event_loop_factory() {
234 return event_loop_factory_;
235 }
236
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700237 const LogFileHeader *log_file_header() const {
238 return &log_file_header_.message();
239 }
240
Austin Schuhe309d2a2019-11-29 13:25:21 -0800241 private:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800242 const Channel *RemapChannel(const EventLoop *event_loop,
243 const Channel *channel);
244
Austin Schuhe309d2a2019-11-29 13:25:21 -0800245 // Queues at least max_out_of_order_duration_ messages into channels_.
246 void QueueMessages();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800247 // Handle constructing a configuration with all the additional remapped
248 // channels from calls to RemapLoggedChannel.
249 void MakeRemappedConfig();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800250
Austin Schuh2f8fd752020-09-01 22:38:28 -0700251 // Returns the number of nodes.
252 size_t nodes_count() const {
253 return !configuration::MultiNode(logged_configuration())
254 ? 1u
255 : logged_configuration()->nodes()->size();
256 }
257
Austin Schuh6f3babe2020-01-26 20:34:50 -0800258 const std::vector<std::vector<std::string>> filenames_;
259
260 // This is *a* log file header used to provide the logged config. The rest of
261 // the header is likely distracting.
262 FlatbufferVector<LogFileHeader> log_file_header_;
263
Austin Schuh2f8fd752020-09-01 22:38:28 -0700264 // Returns [ta; tb; ...] = tuple[0] * t + tuple[1]
265 std::tuple<Eigen::Matrix<double, Eigen::Dynamic, 1>,
266 Eigen::Matrix<double, Eigen::Dynamic, 1>>
267 SolveOffsets();
268
269 void LogFit(std::string_view prefix);
Austin Schuh8bd96322020-02-13 21:18:22 -0800270
Austin Schuh6f3babe2020-01-26 20:34:50 -0800271 // State per node.
Austin Schuh858c9f32020-08-31 16:56:12 -0700272 class State {
273 public:
274 State(std::unique_ptr<ChannelMerger> channel_merger);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800275
Austin Schuh858c9f32020-08-31 16:56:12 -0700276 // Returns the timestamps, channel_index, and message from a channel.
277 // update_time (will be) set to true when popping this message causes the
278 // filter to change the time offset estimation function.
279 std::tuple<TimestampMerger::DeliveryTimestamp, int,
280 FlatbufferVector<MessageHeader>>
281 PopOldest(bool *update_time);
282
283 // Returns the monotonic time of the oldest message.
284 monotonic_clock::time_point OldestMessageTime() const;
285
286 // Primes the queues inside State. Should be called before calling
287 // OldestMessageTime.
288 void SeedSortedMessages();
Austin Schuh8bd96322020-02-13 21:18:22 -0800289
Austin Schuh858c9f32020-08-31 16:56:12 -0700290 // Returns the starting time for this node.
291 monotonic_clock::time_point monotonic_start_time() const {
292 return channel_merger_->monotonic_start_time();
293 }
294 realtime_clock::time_point realtime_start_time() const {
295 return channel_merger_->realtime_start_time();
296 }
297
298 // Sets the node event loop factory for replaying into a
299 // SimulatedEventLoopFactory. Returns the EventLoop to use.
300 EventLoop *SetNodeEventLoopFactory(
301 NodeEventLoopFactory *node_event_loop_factory);
302
303 // Sets and gets the event loop to use.
304 void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; }
305 EventLoop *event_loop() { return event_loop_; }
306
Austin Schuh858c9f32020-08-31 16:56:12 -0700307 // Sets the current realtime offset from the monotonic clock for this node
308 // (if we are on a simulated event loop).
309 void SetRealtimeOffset(monotonic_clock::time_point monotonic_time,
310 realtime_clock::time_point realtime_time) {
311 if (node_event_loop_factory_ != nullptr) {
312 node_event_loop_factory_->SetRealtimeOffset(monotonic_time,
313 realtime_time);
314 }
315 }
316
317 // Converts a timestamp from the monotonic clock on this node to the
318 // distributed clock.
319 distributed_clock::time_point ToDistributedClock(
320 monotonic_clock::time_point time) {
321 return node_event_loop_factory_->ToDistributedClock(time);
322 }
323
Austin Schuh2f8fd752020-09-01 22:38:28 -0700324 monotonic_clock::time_point FromDistributedClock(
325 distributed_clock::time_point time) {
326 return node_event_loop_factory_->FromDistributedClock(time);
327 }
328
Austin Schuh858c9f32020-08-31 16:56:12 -0700329 // Sets the offset (and slope) from the distributed clock.
330 void SetDistributedOffset(std::chrono::nanoseconds distributed_offset,
331 double distributed_slope) {
332 node_event_loop_factory_->SetDistributedOffset(distributed_offset,
333 distributed_slope);
334 }
335
336 // Returns the current time on the remote node which sends messages on
337 // channel_index.
338 monotonic_clock::time_point monotonic_remote_now(size_t channel_index) {
339 return channel_target_event_loop_factory_[channel_index]->monotonic_now();
340 }
341
Austin Schuh2f8fd752020-09-01 22:38:28 -0700342 distributed_clock::time_point RemoteToDistributedClock(
343 size_t channel_index, monotonic_clock::time_point time) {
344 return channel_target_event_loop_factory_[channel_index]
345 ->ToDistributedClock(time);
346 }
347
348 const Node *remote_node(size_t channel_index) {
349 return channel_target_event_loop_factory_[channel_index]->node();
350 }
351
352 monotonic_clock::time_point monotonic_now() {
353 return node_event_loop_factory_->monotonic_now();
354 }
355
Austin Schuh858c9f32020-08-31 16:56:12 -0700356 // Sets the node we will be merging as, and returns true if there is any
357 // data on it.
358 bool SetNode() { return channel_merger_->SetNode(event_loop_->node()); }
359
360 // Sets the number of channels.
361 void SetChannelCount(size_t count);
362
363 // Sets the sender, filter, and target factory for a channel.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700364 void SetChannel(size_t channel, std::unique_ptr<RawSender> sender,
365 message_bridge::NoncausalOffsetEstimator *filter,
366 NodeEventLoopFactory *channel_target_event_loop_factory);
Austin Schuh858c9f32020-08-31 16:56:12 -0700367
368 // Returns if we have read all the messages from all the logs.
369 bool at_end() const { return channel_merger_->at_end(); }
370
371 // Unregisters everything so we can destory the event loop.
372 void Deregister();
373
374 // Sets the current TimerHandle for the replay callback.
375 void set_timer_handler(TimerHandler *timer_handler) {
376 timer_handler_ = timer_handler;
377 }
378
379 // Sets the next wakeup time on the replay callback.
380 void Setup(monotonic_clock::time_point next_time) {
381 timer_handler_->Setup(next_time);
382 }
383
384 // Sends a buffer on the provided channel index.
385 bool Send(size_t channel_index, const void *data, size_t size,
386 aos::monotonic_clock::time_point monotonic_remote_time,
387 aos::realtime_clock::time_point realtime_remote_time,
388 uint32_t remote_queue_index) {
389 return channels_[channel_index]->Send(data, size, monotonic_remote_time,
390 realtime_remote_time,
391 remote_queue_index);
392 }
393
394 // Returns a debug string for the channel merger.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700395 std::string DebugString() const {
396 std::stringstream messages;
397 size_t i = 0;
398 for (const auto &message : sorted_messages_) {
399 if (i < 7 || i + 7 > sorted_messages_.size()) {
400 messages << "sorted_messages[" << i
401 << "]: " << std::get<0>(message).monotonic_event_time << " "
402 << configuration::StrippedChannelToString(
403 event_loop_->configuration()->channels()->Get(
404 std::get<2>(message).message().channel_index()))
405 << "\n";
406 } else if (i == 7) {
407 messages << "...\n";
408 }
409 ++i;
410 }
411 return messages.str() + channel_merger_->DebugString();
412 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700413
414 private:
415 // Log file.
416 std::unique_ptr<ChannelMerger> channel_merger_;
417
418 std::deque<std::tuple<TimestampMerger::DeliveryTimestamp, int,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700419 FlatbufferVector<MessageHeader>,
420 message_bridge::NoncausalOffsetEstimator *>>
Austin Schuh858c9f32020-08-31 16:56:12 -0700421 sorted_messages_;
422
423 // Senders.
424 std::vector<std::unique_ptr<RawSender>> channels_;
425
426 // Factory (if we are in sim) that this loop was created on.
427 NodeEventLoopFactory *node_event_loop_factory_ = nullptr;
428 std::unique_ptr<EventLoop> event_loop_unique_ptr_;
429 // Event loop.
430 EventLoop *event_loop_ = nullptr;
431 // And timer used to send messages.
432 TimerHandler *timer_handler_;
433
Austin Schuh8bd96322020-02-13 21:18:22 -0800434 // Filters (or nullptr if it isn't a forwarded channel) for each channel.
435 // This corresponds to the object which is shared among all the channels
436 // going between 2 nodes. The second element in the tuple indicates if this
437 // is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700438 std::vector<message_bridge::NoncausalOffsetEstimator *> filters_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800439
440 // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded
441 // channel) which correspond to the originating node.
Austin Schuh858c9f32020-08-31 16:56:12 -0700442 std::vector<NodeEventLoopFactory *> channel_target_event_loop_factory_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800443 };
444
Austin Schuh8bd96322020-02-13 21:18:22 -0800445 // Node index -> State.
446 std::vector<std::unique_ptr<State>> states_;
447
448 // Creates the requested filter if it doesn't exist, regardless of whether
449 // these nodes can actually communicate directly. The second return value
450 // reports if this is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700451 message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a,
452 const Node *node_b);
Austin Schuh8bd96322020-02-13 21:18:22 -0800453
454 // FILE to write offsets to (if populated).
455 FILE *offset_fp_ = nullptr;
456 // Timestamp of the first piece of data used for the horizontal axis on the
457 // plot.
458 aos::realtime_clock::time_point first_time_;
459
460 // List of filters for a connection. The pointer to the first node will be
461 // less than the second node.
462 std::map<std::tuple<const Node *, const Node *>,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700463 std::tuple<message_bridge::NoncausalOffsetEstimator>>
Austin Schuh8bd96322020-02-13 21:18:22 -0800464 filters_;
465
466 // Returns the offset from the monotonic clock for a node to the distributed
Austin Schuh2f8fd752020-09-01 22:38:28 -0700467 // clock. monotonic = distributed * slope() + offset();
468 double slope(int node_index) const {
469 CHECK_LT(node_index, time_slope_matrix_.rows())
James Kuszmaul46d82582020-05-09 19:50:09 -0700470 << ": Got too high of a node index.";
Austin Schuh2f8fd752020-09-01 22:38:28 -0700471 return time_slope_matrix_(node_index);
472 }
473 std::chrono::nanoseconds offset(int node_index) const {
474 CHECK_LT(node_index, time_offset_matrix_.rows())
475 << ": Got too high of a node index.";
476 return std::chrono::duration_cast<std::chrono::nanoseconds>(
477 std::chrono::duration<double>(time_offset_matrix_(node_index)));
Austin Schuh8bd96322020-02-13 21:18:22 -0800478 }
479
480 // Updates the offset matrix solution and sets the per-node distributed
481 // offsets in the factory.
482 void UpdateOffsets();
483
Austin Schuh2f8fd752020-09-01 22:38:28 -0700484 // We have 2 types of equations to do a least squares regression over to fully
485 // constrain our time function.
486 //
487 // One is simple. The distributed clock is the average of all the clocks.
488 // (ta + tb + tc + td) / num_nodex = t_distributed
489 //
490 // The second is a bit more complicated. Our basic time conversion function
491 // is:
492 // tb = ta + (ta * slope + offset)
493 // We can rewrite this as follows
494 // tb - (1 + slope) * ta = offset
495 //
496 // From here, we have enough equations to solve for t{a,b,c,...} We want to
497 // take as an input the offsets and slope, and solve for the per-node times as
498 // a function of the distributed clock.
499 //
500 // We need to massage our equations to make this work. If we solve for the
501 // per-node times at two set distributed clock times, we will be able to
502 // recreate the linear function (we know it is linear). We can do a similar
503 // thing by breaking our equation up into:
504 //
505 // [1/3 1/3 1/3 ] [ta] [t_distributed]
506 // [ 1 -1-m1 0 ] [tb] = [oab]
507 // [ 1 0 -1-m2 ] [tc] [oac]
508 //
509 // This solves to:
510 //
511 // [ta] [ a00 a01 a02] [t_distributed]
512 // [tb] = [ a10 a11 a12] * [oab]
513 // [tc] [ a20 a21 a22] [oac]
514 //
515 // and can be split into:
516 //
517 // [ta] [ a00 ] [a01 a02]
518 // [tb] = [ a10 ] * t_distributed + [a11 a12] * [oab]
519 // [tc] [ a20 ] [a21 a22] [oac]
520 //
521 // (map_matrix_ + slope_matrix_) * [ta; tb; tc] = [offset_matrix_];
522 // offset_matrix_ will be in nanoseconds.
523 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> map_matrix_;
524 Eigen::Matrix<mpq_class, Eigen::Dynamic, Eigen::Dynamic> slope_matrix_;
525 Eigen::Matrix<mpq_class, Eigen::Dynamic, 1> offset_matrix_;
526 // Matrix tracking which offsets are valid.
527 Eigen::Matrix<bool, Eigen::Dynamic, 1> valid_matrix_;
528 // Matrix tracking the last valid matrix we used to determine connected nodes.
529 Eigen::Matrix<bool, Eigen::Dynamic, 1> last_valid_matrix_;
530 size_t cached_valid_node_count_ = 0;
Austin Schuh8bd96322020-02-13 21:18:22 -0800531
Austin Schuh2f8fd752020-09-01 22:38:28 -0700532 // [ta; tb; tc] = time_slope_matrix_ * t + time_offset_matrix;
533 // t is in seconds.
534 Eigen::Matrix<double, Eigen::Dynamic, 1> time_slope_matrix_;
535 Eigen::Matrix<double, Eigen::Dynamic, 1> time_offset_matrix_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800536
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800537 std::unique_ptr<FlatbufferDetachedBuffer<Configuration>>
538 remapped_configuration_buffer_;
539
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800540 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_;
541 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800542
543 // Map of channel indices to new name. The channel index will be an index into
544 // logged_configuration(), and the string key will be the name of the channel
545 // to send on instead of the logged channel name.
546 std::map<size_t, std::string> remapped_channels_;
547
Austin Schuh6f3babe2020-01-26 20:34:50 -0800548 // Number of nodes which still have data to send. This is used to figure out
549 // when to exit.
550 size_t live_nodes_ = 0;
551
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800552 const Configuration *remapped_configuration_ = nullptr;
553 const Configuration *replay_configuration_ = nullptr;
Austin Schuhcde938c2020-02-02 17:30:07 -0800554
555 // If true, the replay timer will ignore any missing data. This is used
556 // during startup when we are bootstrapping everything and trying to get to
557 // the start of all the log files.
558 bool ignore_missing_data_ = false;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800559};
560
561} // namespace logger
562} // namespace aos
563
564#endif // AOS_EVENTS_LOGGER_H_