Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOG_WRITER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOG_WRITER_H_ |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <string_view> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/events/logging/log_namer.h" |
| 10 | #include "aos/events/logging/logfile_utils.h" |
| 11 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 12 | #include "aos/events/simulated_event_loop.h" |
| 13 | #include "aos/network/message_bridge_server_generated.h" |
| 14 | #include "aos/network/remote_message_generated.h" |
| 15 | #include "aos/time/time.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 16 | #include "aos/uuid.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 17 | #include "flatbuffers/flatbuffers.h" |
| 18 | |
| 19 | namespace aos { |
| 20 | namespace logger { |
| 21 | |
| 22 | // Logs all channels available in the event loop to disk every 100 ms. |
| 23 | // Start by logging one message per channel to capture any state and |
| 24 | // configuration that is sent rately on a channel and would affect execution. |
| 25 | class Logger { |
| 26 | public: |
| 27 | // Constructs a logger. |
| 28 | // event_loop: The event loop used to read the messages. |
| 29 | // configuration: When provided, this is the configuration to log, and the |
| 30 | // configuration to use for the channel list to log. If not provided, |
| 31 | // this becomes the configuration from the event loop. |
| 32 | // should_log: When provided, a filter for channels to log. If not provided, |
| 33 | // all available channels are logged. |
| 34 | Logger(EventLoop *event_loop) |
| 35 | : Logger(event_loop, event_loop->configuration()) {} |
| 36 | Logger(EventLoop *event_loop, const Configuration *configuration) |
| 37 | : Logger(event_loop, configuration, |
| 38 | [](const Channel *) { return true; }) {} |
| 39 | Logger(EventLoop *event_loop, const Configuration *configuration, |
| 40 | std::function<bool(const Channel *)> should_log); |
| 41 | ~Logger(); |
| 42 | |
| 43 | // Overrides the name in the log file header. |
| 44 | void set_name(std::string_view name) { name_ = name; } |
| 45 | |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 46 | void set_logger_sha1(std::string_view sha1) { logger_sha1_ = sha1; } |
| 47 | void set_logger_version(std::string_view version) { |
| 48 | logger_version_ = version; |
| 49 | } |
| 50 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 51 | // Sets the callback to run after each period of data is logged. Defaults to |
| 52 | // doing nothing. |
| 53 | // |
| 54 | // This callback may safely do things like call Rotate(). |
| 55 | void set_on_logged_period(std::function<void()> on_logged_period) { |
| 56 | on_logged_period_ = std::move(on_logged_period); |
| 57 | } |
| 58 | |
| 59 | void set_separate_config(bool separate_config) { |
| 60 | separate_config_ = separate_config; |
| 61 | } |
| 62 | |
| 63 | // Sets the period between polling the data. Defaults to 100ms. |
| 64 | // |
| 65 | // Changing this while a set of files is being written may result in |
| 66 | // unreadable files. |
| 67 | void set_polling_period(std::chrono::nanoseconds polling_period) { |
| 68 | polling_period_ = polling_period; |
| 69 | } |
Austin Schuh | 29cf4e5 | 2021-03-31 22:51:35 -0700 | [diff] [blame] | 70 | std::chrono::nanoseconds polling_period() const { return polling_period_; } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 71 | |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 72 | std::optional<UUID> log_start_uuid() const { return log_start_uuid_; } |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 73 | UUID logger_instance_uuid() const { return logger_instance_uuid_; } |
| 74 | |
| 75 | // The maximum time for a single fetch which returned a message, or 0 if none |
| 76 | // of those have happened. |
| 77 | std::chrono::nanoseconds max_message_fetch_time() const { |
| 78 | return max_message_fetch_time_; |
| 79 | } |
| 80 | // The channel for that longest fetch which returned a message, or -1 if none |
| 81 | // of those have happened. |
| 82 | int max_message_fetch_time_channel() const { |
| 83 | return max_message_fetch_time_channel_; |
| 84 | } |
| 85 | // The size of the message returned by that longest fetch, or -1 if none of |
| 86 | // those have happened. |
| 87 | int max_message_fetch_time_size() const { |
| 88 | return max_message_fetch_time_size_; |
| 89 | } |
| 90 | // The total time spent fetching messages. |
| 91 | std::chrono::nanoseconds total_message_fetch_time() const { |
| 92 | return total_message_fetch_time_; |
| 93 | } |
| 94 | // The total number of fetch calls which returned messages. |
| 95 | int total_message_fetch_count() const { return total_message_fetch_count_; } |
| 96 | // The total number of bytes fetched. |
| 97 | int64_t total_message_fetch_bytes() const { |
| 98 | return total_message_fetch_bytes_; |
| 99 | } |
| 100 | |
| 101 | // The total time spent in fetches which did not return a message. |
| 102 | std::chrono::nanoseconds total_nop_fetch_time() const { |
| 103 | return total_nop_fetch_time_; |
| 104 | } |
| 105 | // The total number of fetches which did not return a message. |
| 106 | int total_nop_fetch_count() const { return total_nop_fetch_count_; } |
| 107 | |
| 108 | // The maximum time for a single copy, or 0 if none of those have happened. |
| 109 | std::chrono::nanoseconds max_copy_time() const { return max_copy_time_; } |
| 110 | // The channel for that longest copy, or -1 if none of those have happened. |
| 111 | int max_copy_time_channel() const { return max_copy_time_channel_; } |
| 112 | // The size of the message for that longest copy, or -1 if none of those have |
| 113 | // happened. |
| 114 | int max_copy_time_size() const { return max_copy_time_size_; } |
| 115 | // The total time spent copying messages. |
| 116 | std::chrono::nanoseconds total_copy_time() const { return total_copy_time_; } |
| 117 | // The total number of messages copied. |
| 118 | int total_copy_count() const { return total_copy_count_; } |
| 119 | // The total number of bytes copied. |
| 120 | int64_t total_copy_bytes() const { return total_copy_bytes_; } |
| 121 | |
| 122 | void ResetStatisics(); |
| 123 | |
| 124 | // Rotates the log file(s), triggering new part files to be written for each |
| 125 | // log file. |
| 126 | void Rotate(); |
| 127 | |
| 128 | // Starts logging to files with the given naming scheme. |
| 129 | // |
| 130 | // log_start_uuid may be used to tie this log event to other log events across |
| 131 | // multiple nodes. The default (empty string) indicates there isn't one |
| 132 | // available. |
| 133 | void StartLogging(std::unique_ptr<LogNamer> log_namer, |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 134 | std::optional<UUID> log_start_uuid = std::nullopt); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 135 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 136 | // Restart logging using a new naming scheme. Intended for log rotation. |
| 137 | // Returns a unique_ptr to the prior log_namer instance. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 138 | std::unique_ptr<LogNamer> RestartLogging( |
| 139 | std::unique_ptr<LogNamer> log_namer, |
| 140 | std::optional<UUID> log_start_uuid = std::nullopt); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 142 | // Moves the current log location to the new name. Returns true if a change |
| 143 | // was made, false otherwise. |
| 144 | // Only renaming the folder is supported, not the file base name. |
| 145 | bool RenameLogBase(std::string new_base_name); |
| 146 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 147 | // Stops logging. Ensures any messages through end_time make it into the log. |
| 148 | // |
| 149 | // If you want to stop ASAP, pass min_time to avoid reading any more messages. |
| 150 | // |
| 151 | // Returns the LogNamer in case the caller wants to do anything else with it |
| 152 | // before destroying it. |
| 153 | std::unique_ptr<LogNamer> StopLogging( |
| 154 | aos::monotonic_clock::time_point end_time); |
| 155 | |
| 156 | // Returns whether a log is currently being written. |
| 157 | bool is_started() const { return static_cast<bool>(log_namer_); } |
| 158 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 159 | // Shortcut to call StartLogging with a MultiNodeFilesLogNamer when event |
Austin Schuh | 5b6b3bc | 2021-03-31 22:55:06 -0700 | [diff] [blame] | 160 | // processing starts. |
| 161 | void StartLoggingOnRun(std::string base_name) { |
| 162 | event_loop_->OnRun([this, base_name]() { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 163 | StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 164 | base_name, configuration_, event_loop_, node_)); |
Austin Schuh | 5b6b3bc | 2021-03-31 22:55:06 -0700 | [diff] [blame] | 165 | }); |
| 166 | } |
| 167 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 168 | private: |
| 169 | // Structure to track both a fetcher, and if the data fetched has been |
| 170 | // written. We may want to delay writing data to disk so that we don't let |
| 171 | // data get too far out of order when written to disk so we can avoid making |
| 172 | // it too hard to sort when reading. |
| 173 | struct FetcherStruct { |
| 174 | std::unique_ptr<RawFetcher> fetcher; |
| 175 | bool written = false; |
| 176 | |
| 177 | // Channel index to log to. |
| 178 | int channel_index = -1; |
| 179 | const Channel *channel = nullptr; |
| 180 | const Node *timestamp_node = nullptr; |
| 181 | |
| 182 | LogType log_type = LogType::kLogMessage; |
| 183 | |
| 184 | // We fill out the metadata at construction, but the actual writers have to |
| 185 | // be updated each time we start logging. To avoid duplicating the complex |
| 186 | // logic determining whether each writer should be initialized, we just |
| 187 | // stash the answer in separate member variables. |
| 188 | bool wants_writer = false; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 189 | NewDataWriter *writer = nullptr; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 190 | bool wants_timestamp_writer = false; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 191 | NewDataWriter *timestamp_writer = nullptr; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 192 | bool wants_contents_writer = false; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 193 | NewDataWriter *contents_writer = nullptr; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 194 | |
| 195 | // Node which this data is from, or -1 if it is unknown. |
| 196 | int data_node_index = -1; |
| 197 | // Node that this timestamp is for, or -1 if it is known. |
| 198 | int timestamp_node_index = -1; |
| 199 | // Node that the contents this contents_writer will log are from. |
| 200 | int contents_node_index = -1; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 201 | |
| 202 | // If true, this message is being sent over a reliable channel. |
| 203 | bool reliable_forwarding = false; |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 204 | |
| 205 | // One of the following will be populated. If channel_reliable_contents is |
| 206 | // non zero size, it contains a mapping from the event loop channel (not the |
| 207 | // logged channel) to a bool telling us if that particular channel is |
| 208 | // reliable. |
| 209 | // |
| 210 | // If channel_reliable_contents is empty, reliable_contents will contain the |
| 211 | // same info for all contents logged here. This is the predominant case for |
| 212 | // split timestamp channels (the prefered approach). |
| 213 | bool reliable_contents = false; |
| 214 | std::vector<bool> channel_reliable_contents; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | // Vector mapping from the channel index from the event loop to the logged |
| 218 | // channel index. |
| 219 | std::vector<int> event_loop_to_logged_channel_index_; |
| 220 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 221 | // Start/Restart write configuration into LogNamer space. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 222 | std::string WriteConfiguration(LogNamer *log_namer); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 223 | |
Austin Schuh | 41f8df9 | 2022-04-15 11:45:52 -0700 | [diff] [blame] | 224 | void WriteHeader(aos::monotonic_clock::time_point monotonic_start_time = |
| 225 | aos::monotonic_clock::min_time, |
| 226 | aos::realtime_clock::time_point realtime_start_time = |
| 227 | aos::realtime_clock::min_time); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 228 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 229 | // Makes a template header for all the follower nodes. |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 230 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 231 | std::string_view config_sha256); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 232 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 233 | bool MaybeUpdateTimestamp( |
| 234 | const Node *node, int node_index, |
| 235 | aos::monotonic_clock::time_point monotonic_start_time, |
| 236 | aos::realtime_clock::time_point realtime_start_time); |
| 237 | |
Austin Schuh | 3058690 | 2021-03-30 22:54:08 -0700 | [diff] [blame] | 238 | void DoLogData(const monotonic_clock::time_point end_time, |
| 239 | bool run_on_logged); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 240 | |
| 241 | void WriteMissingTimestamps(); |
| 242 | |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 243 | void WriteData(NewDataWriter *writer, const FetcherStruct &f); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 244 | void WriteTimestamps(NewDataWriter *timestamps_writer, |
| 245 | const FetcherStruct &f); |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 246 | void WriteContent(NewDataWriter *contents_writer, const FetcherStruct &f); |
| 247 | |
| 248 | void WriteFetchedRecord(FetcherStruct &f); |
| 249 | |
Austin Schuh | 855f893 | 2021-03-19 22:01:32 -0700 | [diff] [blame] | 250 | // Fetches from each channel until all the data is logged. This is dangerous |
| 251 | // because it lets you log for more than 1 period. All calls need to verify |
| 252 | // that t isn't greater than 1 period in the future. |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame^] | 253 | // |
| 254 | // Returns true if there is at least one message written, and also returns the |
| 255 | // timestamp of the newest record that any fetcher is pointing to, or min_time |
| 256 | // if there are no messages published on any logged channels. |
| 257 | std::pair<bool, monotonic_clock::time_point> LogUntil( |
| 258 | monotonic_clock::time_point t); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 259 | |
| 260 | void RecordFetchResult(aos::monotonic_clock::time_point start, |
| 261 | aos::monotonic_clock::time_point end, bool got_new, |
| 262 | FetcherStruct *fetcher); |
| 263 | |
| 264 | void RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 265 | aos::monotonic_clock::time_point end, |
Brian Smartt | 03c00da | 2022-02-24 10:25:00 -0800 | [diff] [blame] | 266 | const FetcherStruct &fetcher); |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 267 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 268 | EventLoop *const event_loop_; |
| 269 | // The configuration to place at the top of the log file. |
| 270 | const Configuration *const configuration_; |
| 271 | |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 272 | // The node that is writing the log. |
| 273 | // For most cases, this is the same node as the node that is reading the |
| 274 | // messages. However, in some cases, these two nodes may be different. i.e. if |
| 275 | // one node reading and modifying the messages, and another node is listening |
| 276 | // and saving those messages to another log. |
| 277 | // |
| 278 | // node_ is a pointer to the writing node, and that node is guaranteed to be |
| 279 | // in configuration_ which is the configuration being written to the top of |
| 280 | // the log file. |
| 281 | const Node *const node_; |
| 282 | // The node_index_ is the index of the node in configuration_. |
| 283 | const size_t node_index_; |
| 284 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 285 | UUID log_event_uuid_ = UUID::Zero(); |
| 286 | const UUID logger_instance_uuid_ = UUID::Random(); |
| 287 | std::unique_ptr<LogNamer> log_namer_; |
| 288 | // Empty indicates there isn't one. |
Austin Schuh | 34f9e48 | 2021-03-31 22:54:18 -0700 | [diff] [blame] | 289 | std::optional<UUID> log_start_uuid_; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 290 | |
| 291 | // Name to save in the log file. Defaults to hostname. |
| 292 | std::string name_; |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 293 | std::string logger_sha1_; |
| 294 | std::string logger_version_; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 295 | |
| 296 | std::function<void()> on_logged_period_ = []() {}; |
| 297 | |
| 298 | std::chrono::nanoseconds max_message_fetch_time_ = |
| 299 | std::chrono::nanoseconds::zero(); |
| 300 | int max_message_fetch_time_channel_ = -1; |
| 301 | int max_message_fetch_time_size_ = -1; |
| 302 | std::chrono::nanoseconds total_message_fetch_time_ = |
| 303 | std::chrono::nanoseconds::zero(); |
| 304 | int total_message_fetch_count_ = 0; |
| 305 | int64_t total_message_fetch_bytes_ = 0; |
| 306 | |
| 307 | std::chrono::nanoseconds total_nop_fetch_time_ = |
| 308 | std::chrono::nanoseconds::zero(); |
| 309 | int total_nop_fetch_count_ = 0; |
| 310 | |
| 311 | std::chrono::nanoseconds max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 312 | int max_copy_time_channel_ = -1; |
| 313 | int max_copy_time_size_ = -1; |
| 314 | std::chrono::nanoseconds total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 315 | int total_copy_count_ = 0; |
| 316 | int64_t total_copy_bytes_ = 0; |
| 317 | |
| 318 | std::vector<FetcherStruct> fetchers_; |
| 319 | TimerHandler *timer_handler_; |
| 320 | |
| 321 | // Period to poll the channels. |
| 322 | std::chrono::nanoseconds polling_period_ = std::chrono::milliseconds(100); |
| 323 | |
| 324 | // Last time that data was written for all channels to disk. |
| 325 | monotonic_clock::time_point last_synchronized_time_; |
| 326 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 327 | // If true, write the message header into a separate file. |
| 328 | bool separate_config_ = true; |
| 329 | |
| 330 | // Fetcher for all the statistics from all the nodes. |
| 331 | aos::Fetcher<message_bridge::ServerStatistics> server_statistics_fetcher_; |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | } // namespace logger |
| 335 | } // namespace aos |
| 336 | |
| 337 | #endif // AOS_EVENTS_LOGGING_LOG_WRITER_H_ |