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" |
| 12 | #include "aos/events/logging/uuid.h" |
| 13 | #include "aos/events/simulated_event_loop.h" |
| 14 | #include "aos/network/message_bridge_server_generated.h" |
| 15 | #include "aos/network/remote_message_generated.h" |
| 16 | #include "aos/time/time.h" |
| 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 | |
| 46 | // Sets the callback to run after each period of data is logged. Defaults to |
| 47 | // doing nothing. |
| 48 | // |
| 49 | // This callback may safely do things like call Rotate(). |
| 50 | void set_on_logged_period(std::function<void()> on_logged_period) { |
| 51 | on_logged_period_ = std::move(on_logged_period); |
| 52 | } |
| 53 | |
| 54 | void set_separate_config(bool separate_config) { |
| 55 | separate_config_ = separate_config; |
| 56 | } |
| 57 | |
| 58 | // Sets the period between polling the data. Defaults to 100ms. |
| 59 | // |
| 60 | // Changing this while a set of files is being written may result in |
| 61 | // unreadable files. |
| 62 | void set_polling_period(std::chrono::nanoseconds polling_period) { |
| 63 | polling_period_ = polling_period; |
| 64 | } |
| 65 | |
| 66 | std::string_view log_start_uuid() const { return log_start_uuid_; } |
| 67 | UUID logger_instance_uuid() const { return logger_instance_uuid_; } |
| 68 | |
| 69 | // The maximum time for a single fetch which returned a message, or 0 if none |
| 70 | // of those have happened. |
| 71 | std::chrono::nanoseconds max_message_fetch_time() const { |
| 72 | return max_message_fetch_time_; |
| 73 | } |
| 74 | // The channel for that longest fetch which returned a message, or -1 if none |
| 75 | // of those have happened. |
| 76 | int max_message_fetch_time_channel() const { |
| 77 | return max_message_fetch_time_channel_; |
| 78 | } |
| 79 | // The size of the message returned by that longest fetch, or -1 if none of |
| 80 | // those have happened. |
| 81 | int max_message_fetch_time_size() const { |
| 82 | return max_message_fetch_time_size_; |
| 83 | } |
| 84 | // The total time spent fetching messages. |
| 85 | std::chrono::nanoseconds total_message_fetch_time() const { |
| 86 | return total_message_fetch_time_; |
| 87 | } |
| 88 | // The total number of fetch calls which returned messages. |
| 89 | int total_message_fetch_count() const { return total_message_fetch_count_; } |
| 90 | // The total number of bytes fetched. |
| 91 | int64_t total_message_fetch_bytes() const { |
| 92 | return total_message_fetch_bytes_; |
| 93 | } |
| 94 | |
| 95 | // The total time spent in fetches which did not return a message. |
| 96 | std::chrono::nanoseconds total_nop_fetch_time() const { |
| 97 | return total_nop_fetch_time_; |
| 98 | } |
| 99 | // The total number of fetches which did not return a message. |
| 100 | int total_nop_fetch_count() const { return total_nop_fetch_count_; } |
| 101 | |
| 102 | // The maximum time for a single copy, or 0 if none of those have happened. |
| 103 | std::chrono::nanoseconds max_copy_time() const { return max_copy_time_; } |
| 104 | // The channel for that longest copy, or -1 if none of those have happened. |
| 105 | int max_copy_time_channel() const { return max_copy_time_channel_; } |
| 106 | // The size of the message for that longest copy, or -1 if none of those have |
| 107 | // happened. |
| 108 | int max_copy_time_size() const { return max_copy_time_size_; } |
| 109 | // The total time spent copying messages. |
| 110 | std::chrono::nanoseconds total_copy_time() const { return total_copy_time_; } |
| 111 | // The total number of messages copied. |
| 112 | int total_copy_count() const { return total_copy_count_; } |
| 113 | // The total number of bytes copied. |
| 114 | int64_t total_copy_bytes() const { return total_copy_bytes_; } |
| 115 | |
| 116 | void ResetStatisics(); |
| 117 | |
| 118 | // Rotates the log file(s), triggering new part files to be written for each |
| 119 | // log file. |
| 120 | void Rotate(); |
| 121 | |
| 122 | // Starts logging to files with the given naming scheme. |
| 123 | // |
| 124 | // log_start_uuid may be used to tie this log event to other log events across |
| 125 | // multiple nodes. The default (empty string) indicates there isn't one |
| 126 | // available. |
| 127 | void StartLogging(std::unique_ptr<LogNamer> log_namer, |
| 128 | std::string_view log_start_uuid = ""); |
| 129 | |
| 130 | // Stops logging. Ensures any messages through end_time make it into the log. |
| 131 | // |
| 132 | // If you want to stop ASAP, pass min_time to avoid reading any more messages. |
| 133 | // |
| 134 | // Returns the LogNamer in case the caller wants to do anything else with it |
| 135 | // before destroying it. |
| 136 | std::unique_ptr<LogNamer> StopLogging( |
| 137 | aos::monotonic_clock::time_point end_time); |
| 138 | |
| 139 | // Returns whether a log is currently being written. |
| 140 | bool is_started() const { return static_cast<bool>(log_namer_); } |
| 141 | |
| 142 | // Shortcut to call StartLogging with a LocalLogNamer when event processing |
| 143 | // starts. |
| 144 | void StartLoggingLocalNamerOnRun(std::string base_name) { |
| 145 | event_loop_->OnRun([this, base_name]() { |
| 146 | StartLogging( |
| 147 | std::make_unique<LocalLogNamer>(base_name, event_loop_->node())); |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | // Structure to track both a fetcher, and if the data fetched has been |
| 153 | // written. We may want to delay writing data to disk so that we don't let |
| 154 | // data get too far out of order when written to disk so we can avoid making |
| 155 | // it too hard to sort when reading. |
| 156 | struct FetcherStruct { |
| 157 | std::unique_ptr<RawFetcher> fetcher; |
| 158 | bool written = false; |
| 159 | |
| 160 | // Channel index to log to. |
| 161 | int channel_index = -1; |
| 162 | const Channel *channel = nullptr; |
| 163 | const Node *timestamp_node = nullptr; |
| 164 | |
| 165 | LogType log_type = LogType::kLogMessage; |
| 166 | |
| 167 | // We fill out the metadata at construction, but the actual writers have to |
| 168 | // be updated each time we start logging. To avoid duplicating the complex |
| 169 | // logic determining whether each writer should be initialized, we just |
| 170 | // stash the answer in separate member variables. |
| 171 | bool wants_writer = false; |
| 172 | DetachedBufferWriter *writer = nullptr; |
| 173 | bool wants_timestamp_writer = false; |
| 174 | DetachedBufferWriter *timestamp_writer = nullptr; |
| 175 | bool wants_contents_writer = false; |
| 176 | DetachedBufferWriter *contents_writer = nullptr; |
| 177 | |
| 178 | // Node which this data is from, or -1 if it is unknown. |
| 179 | int data_node_index = -1; |
| 180 | // Node that this timestamp is for, or -1 if it is known. |
| 181 | int timestamp_node_index = -1; |
| 182 | // Node that the contents this contents_writer will log are from. |
| 183 | int contents_node_index = -1; |
| 184 | }; |
| 185 | |
| 186 | // Vector mapping from the channel index from the event loop to the logged |
| 187 | // channel index. |
| 188 | std::vector<int> event_loop_to_logged_channel_index_; |
| 189 | |
| 190 | struct NodeState { |
| 191 | aos::monotonic_clock::time_point monotonic_start_time = |
| 192 | aos::monotonic_clock::min_time; |
| 193 | aos::realtime_clock::time_point realtime_start_time = |
| 194 | aos::realtime_clock::min_time; |
| 195 | |
| 196 | bool has_source_node_boot_uuid = false; |
| 197 | |
| 198 | // This is an initial UUID that is a valid UUID4 and is pretty obvious that |
| 199 | // it isn't valid. |
| 200 | std::string source_node_boot_uuid = "00000000-0000-4000-8000-000000000000"; |
| 201 | |
| 202 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> log_file_header = |
| 203 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty(); |
| 204 | |
| 205 | // True if a header has been written to the start of a log file. |
| 206 | bool header_written = false; |
| 207 | // True if the current written header represents the contents which will |
| 208 | // follow. This is cleared when boot_uuid is known to not match anymore. |
| 209 | bool header_valid = false; |
| 210 | |
| 211 | // Sets the source_node_boot_uuid, properly updating everything. |
| 212 | void SetBootUUID(std::string_view new_source_node_boot_uuid) { |
| 213 | source_node_boot_uuid = new_source_node_boot_uuid; |
| 214 | header_valid = false; |
| 215 | has_source_node_boot_uuid = true; |
| 216 | |
| 217 | flatbuffers::String *source_node_boot_uuid_string = |
| 218 | log_file_header.mutable_message()->mutable_source_node_boot_uuid(); |
| 219 | CHECK_EQ(source_node_boot_uuid.size(), |
| 220 | source_node_boot_uuid_string->size()); |
| 221 | memcpy(source_node_boot_uuid_string->data(), source_node_boot_uuid.data(), |
| 222 | source_node_boot_uuid.size()); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | void WriteHeader(); |
| 227 | |
| 228 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
| 229 | const Node *node, std::string_view config_sha256); |
| 230 | |
| 231 | // Writes the header for the provided node if enough information is valid. |
| 232 | void MaybeWriteHeader(int node_index); |
| 233 | // Overload for when we already know node as well. |
| 234 | void MaybeWriteHeader(int node_index, const Node *node); |
| 235 | |
| 236 | bool MaybeUpdateTimestamp( |
| 237 | const Node *node, int node_index, |
| 238 | aos::monotonic_clock::time_point monotonic_start_time, |
| 239 | aos::realtime_clock::time_point realtime_start_time); |
| 240 | |
| 241 | void DoLogData(const monotonic_clock::time_point end_time); |
| 242 | |
| 243 | void WriteMissingTimestamps(); |
| 244 | |
| 245 | // Fetches from each channel until all the data is logged. |
| 246 | void LogUntil(monotonic_clock::time_point t); |
| 247 | |
| 248 | void RecordFetchResult(aos::monotonic_clock::time_point start, |
| 249 | aos::monotonic_clock::time_point end, bool got_new, |
| 250 | FetcherStruct *fetcher); |
| 251 | |
| 252 | void RecordCreateMessageTime(aos::monotonic_clock::time_point start, |
| 253 | aos::monotonic_clock::time_point end, |
| 254 | FetcherStruct *fetcher); |
| 255 | |
| 256 | // Sets the start time for a specific node. |
| 257 | void SetStartTime( |
| 258 | size_t node_index, aos::monotonic_clock::time_point monotonic_start_time, |
| 259 | aos::realtime_clock::time_point realtime_start_time, |
| 260 | aos::monotonic_clock::time_point logger_monotonic_start_time, |
| 261 | aos::realtime_clock::time_point logger_realtime_start_time); |
| 262 | |
| 263 | EventLoop *const event_loop_; |
| 264 | // The configuration to place at the top of the log file. |
| 265 | const Configuration *const configuration_; |
| 266 | |
| 267 | UUID log_event_uuid_ = UUID::Zero(); |
| 268 | const UUID logger_instance_uuid_ = UUID::Random(); |
| 269 | std::unique_ptr<LogNamer> log_namer_; |
| 270 | // Empty indicates there isn't one. |
| 271 | std::string log_start_uuid_; |
| 272 | |
| 273 | // Name to save in the log file. Defaults to hostname. |
| 274 | std::string name_; |
| 275 | |
| 276 | std::function<void()> on_logged_period_ = []() {}; |
| 277 | |
| 278 | std::chrono::nanoseconds max_message_fetch_time_ = |
| 279 | std::chrono::nanoseconds::zero(); |
| 280 | int max_message_fetch_time_channel_ = -1; |
| 281 | int max_message_fetch_time_size_ = -1; |
| 282 | std::chrono::nanoseconds total_message_fetch_time_ = |
| 283 | std::chrono::nanoseconds::zero(); |
| 284 | int total_message_fetch_count_ = 0; |
| 285 | int64_t total_message_fetch_bytes_ = 0; |
| 286 | |
| 287 | std::chrono::nanoseconds total_nop_fetch_time_ = |
| 288 | std::chrono::nanoseconds::zero(); |
| 289 | int total_nop_fetch_count_ = 0; |
| 290 | |
| 291 | std::chrono::nanoseconds max_copy_time_ = std::chrono::nanoseconds::zero(); |
| 292 | int max_copy_time_channel_ = -1; |
| 293 | int max_copy_time_size_ = -1; |
| 294 | std::chrono::nanoseconds total_copy_time_ = std::chrono::nanoseconds::zero(); |
| 295 | int total_copy_count_ = 0; |
| 296 | int64_t total_copy_bytes_ = 0; |
| 297 | |
| 298 | std::vector<FetcherStruct> fetchers_; |
| 299 | TimerHandler *timer_handler_; |
| 300 | |
| 301 | // Period to poll the channels. |
| 302 | std::chrono::nanoseconds polling_period_ = std::chrono::milliseconds(100); |
| 303 | |
| 304 | // Last time that data was written for all channels to disk. |
| 305 | monotonic_clock::time_point last_synchronized_time_; |
| 306 | |
| 307 | // Max size that the header has consumed. This much extra data will be |
| 308 | // reserved in the builder to avoid reallocating. |
| 309 | size_t max_header_size_ = 0; |
| 310 | |
| 311 | // If true, write the message header into a separate file. |
| 312 | bool separate_config_ = true; |
| 313 | |
| 314 | // Fetcher for all the statistics from all the nodes. |
| 315 | aos::Fetcher<message_bridge::ServerStatistics> server_statistics_fetcher_; |
| 316 | |
| 317 | std::vector<NodeState> node_state_; |
| 318 | }; |
| 319 | |
| 320 | } // namespace logger |
| 321 | } // namespace aos |
| 322 | |
| 323 | #endif // AOS_EVENTS_LOGGING_LOG_WRITER_H_ |