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