Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGER_H_ |
| 2 | #define AOS_EVENTS_LOGGER_H_ |
| 3 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 4 | #include <chrono> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include <deque> |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 6 | #include <string_view> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 7 | #include <tuple> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 8 | #include <vector> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 9 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 10 | #include "Eigen/Dense" |
| 11 | #include "absl/strings/str_cat.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 12 | #include "absl/types/span.h" |
| 13 | #include "aos/events/event_loop.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 14 | #include "aos/events/logging/eigen_mpq.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame^] | 15 | #include "aos/events/logging/log_namer.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 16 | #include "aos/events/logging/logfile_utils.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 17 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 18 | #include "aos/events/logging/uuid.h" |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 19 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 20 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 21 | #include "aos/network/timestamp_filter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 22 | #include "aos/time/time.h" |
| 23 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 24 | #include "third_party/gmp/gmpxx.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | |
| 26 | namespace aos { |
| 27 | namespace logger { |
| 28 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 29 | // 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. |
| 32 | class Logger { |
| 33 | public: |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 34 | Logger(std::string_view base_name, EventLoop *event_loop, |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 35 | std::chrono::milliseconds polling_period = |
| 36 | std::chrono::milliseconds(100)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 37 | Logger(std::unique_ptr<LogNamer> log_namer, EventLoop *event_loop, |
| 38 | std::chrono::milliseconds polling_period = |
| 39 | std::chrono::milliseconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 41 | // Rotates the log file(s), triggering new part files to be written for each |
| 42 | // log file. |
| 43 | void Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 44 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 45 | private: |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 46 | void WriteHeader(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 47 | 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 54 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 55 | void DoLogData(); |
| 56 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 57 | 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 64 | EventLoop *event_loop_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 65 | const UUID uuid_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 66 | std::unique_ptr<LogNamer> log_namer_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 67 | |
| 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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 76 | int channel_index = -1; |
| 77 | |
| 78 | LogType log_type = LogType::kLogMessage; |
| 79 | |
| 80 | DetachedBufferWriter *writer = nullptr; |
| 81 | DetachedBufferWriter *timestamp_writer = nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 82 | DetachedBufferWriter *contents_writer = nullptr; |
| 83 | const Node *writer_node = nullptr; |
| 84 | const Node *timestamp_node = nullptr; |
| 85 | int node_index = 0; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 86 | }; |
| 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 97 | monotonic_clock::time_point monotonic_start_time_; |
| 98 | realtime_clock::time_point realtime_start_time_; |
| 99 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 100 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 103 | |
| 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 124 | // 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 152 | // Replays all the channels in the logfile to the event loop. |
| 153 | class LogReader { |
| 154 | public: |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 155 | // 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 159 | // |
| 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 Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 171 | LogReader(std::string_view filename, |
| 172 | const Configuration *replay_configuration = nullptr); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 173 | LogReader(const std::vector<std::string> &filenames, |
| 174 | const Configuration *replay_configuration = nullptr); |
| 175 | LogReader(const std::vector<std::vector<std::string>> &filenames, |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 176 | const Configuration *replay_configuration = nullptr); |
James Kuszmaul | 7daef36 | 2019-12-31 18:28:17 -0800 | [diff] [blame] | 177 | ~LogReader(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 178 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 179 | // 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 Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 185 | void Register(SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 186 | // 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 191 | void Register(EventLoop *event_loop); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 192 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 193 | // 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 197 | void Deregister(); |
| 198 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 199 | // Returns the configuration from the log file. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 200 | const Configuration *logged_configuration() const; |
| 201 | // Returns the configuration being used for replay. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 202 | // The pointer is invalidated whenever RemapLoggedChannel is called. |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 203 | const Configuration *configuration() const; |
| 204 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 205 | // 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 209 | |
| 210 | // Returns the starting timestamp for the log file. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 211 | monotonic_clock::time_point monotonic_start_time(const Node *node = nullptr); |
| 212 | realtime_clock::time_point realtime_start_time(const Node *node = nullptr); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 213 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 214 | // 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 Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 226 | 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 Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 233 | SimulatedEventLoopFactory *event_loop_factory() { |
| 234 | return event_loop_factory_; |
| 235 | } |
| 236 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 237 | const LogFileHeader *log_file_header() const { |
| 238 | return &log_file_header_.message(); |
| 239 | } |
| 240 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 241 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 242 | const Channel *RemapChannel(const EventLoop *event_loop, |
| 243 | const Channel *channel); |
| 244 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 245 | // Queues at least max_out_of_order_duration_ messages into channels_. |
| 246 | void QueueMessages(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 247 | // Handle constructing a configuration with all the additional remapped |
| 248 | // channels from calls to RemapLoggedChannel. |
| 249 | void MakeRemappedConfig(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 250 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 251 | // 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 258 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 264 | // 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 270 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 271 | // State per node. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 272 | class State { |
| 273 | public: |
| 274 | State(std::unique_ptr<ChannelMerger> channel_merger); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 275 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 276 | // 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 289 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 290 | // 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 Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 307 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 324 | monotonic_clock::time_point FromDistributedClock( |
| 325 | distributed_clock::time_point time) { |
| 326 | return node_event_loop_factory_->FromDistributedClock(time); |
| 327 | } |
| 328 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 329 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 342 | 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 Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 356 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 364 | void SetChannel(size_t channel, std::unique_ptr<RawSender> sender, |
| 365 | message_bridge::NoncausalOffsetEstimator *filter, |
| 366 | NodeEventLoopFactory *channel_target_event_loop_factory); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 367 | |
| 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 395 | 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 Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 413 | |
| 414 | private: |
| 415 | // Log file. |
| 416 | std::unique_ptr<ChannelMerger> channel_merger_; |
| 417 | |
| 418 | std::deque<std::tuple<TimestampMerger::DeliveryTimestamp, int, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 419 | FlatbufferVector<MessageHeader>, |
| 420 | message_bridge::NoncausalOffsetEstimator *>> |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 421 | 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 434 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 438 | std::vector<message_bridge::NoncausalOffsetEstimator *> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 439 | |
| 440 | // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded |
| 441 | // channel) which correspond to the originating node. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 442 | std::vector<NodeEventLoopFactory *> channel_target_event_loop_factory_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 443 | }; |
| 444 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 445 | // 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 451 | message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a, |
| 452 | const Node *node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 453 | |
| 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 463 | std::tuple<message_bridge::NoncausalOffsetEstimator>> |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 464 | filters_; |
| 465 | |
| 466 | // Returns the offset from the monotonic clock for a node to the distributed |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 467 | // clock. monotonic = distributed * slope() + offset(); |
| 468 | double slope(int node_index) const { |
| 469 | CHECK_LT(node_index, time_slope_matrix_.rows()) |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 470 | << ": Got too high of a node index."; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 471 | 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // Updates the offset matrix solution and sets the per-node distributed |
| 481 | // offsets in the factory. |
| 482 | void UpdateOffsets(); |
| 483 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 484 | // 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 531 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 532 | // [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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 536 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 537 | std::unique_ptr<FlatbufferDetachedBuffer<Configuration>> |
| 538 | remapped_configuration_buffer_; |
| 539 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 540 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_; |
| 541 | SimulatedEventLoopFactory *event_loop_factory_ = nullptr; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 542 | |
| 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 548 | // 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 Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 552 | const Configuration *remapped_configuration_ = nullptr; |
| 553 | const Configuration *replay_configuration_ = nullptr; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 554 | |
| 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | } // namespace logger |
| 562 | } // namespace aos |
| 563 | |
| 564 | #endif // AOS_EVENTS_LOGGER_H_ |