blob: 1740b1f5dc2995d8645d28ebe88999cf6475e037 [file] [log] [blame]
Austin Schuhe309d2a2019-11-29 13:25:21 -08001#ifndef AOS_EVENTS_LOGGER_H_
2#define AOS_EVENTS_LOGGER_H_
3
4#include <deque>
5#include <vector>
Austin Schuh05b70472020-01-01 17:11:17 -08006#include <string_view>
Austin Schuhe309d2a2019-11-29 13:25:21 -08007
Austin Schuhe309d2a2019-11-29 13:25:21 -08008#include "absl/types/span.h"
9#include "aos/events/event_loop.h"
Austin Schuha36c8902019-12-30 18:07:15 -080010#include "aos/events/logging/logfile_utils.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080011#include "aos/events/logging/logger_generated.h"
Austin Schuh92547522019-12-28 14:33:43 -080012#include "aos/events/simulated_event_loop.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080013#include "aos/time/time.h"
14#include "flatbuffers/flatbuffers.h"
15
16namespace aos {
17namespace logger {
18
Austin Schuhe309d2a2019-11-29 13:25:21 -080019// Logs all channels available in the event loop to disk every 100 ms.
20// Start by logging one message per channel to capture any state and
21// configuration that is sent rately on a channel and would affect execution.
22class Logger {
23 public:
24 Logger(DetachedBufferWriter *writer, EventLoop *event_loop,
25 std::chrono::milliseconds polling_period =
26 std::chrono::milliseconds(100));
27
28 private:
29 void DoLogData();
30
31 EventLoop *event_loop_;
32 DetachedBufferWriter *writer_;
33
34 // Structure to track both a fetcher, and if the data fetched has been
35 // written. We may want to delay writing data to disk so that we don't let
36 // data get too far out of order when written to disk so we can avoid making
37 // it too hard to sort when reading.
38 struct FetcherStruct {
39 std::unique_ptr<RawFetcher> fetcher;
40 bool written = false;
Austin Schuh15649d62019-12-28 16:36:38 -080041
42 LogType log_type;
Austin Schuhe309d2a2019-11-29 13:25:21 -080043 };
44
45 std::vector<FetcherStruct> fetchers_;
46 TimerHandler *timer_handler_;
47
48 // Period to poll the channels.
49 const std::chrono::milliseconds polling_period_;
50
51 // Last time that data was written for all channels to disk.
52 monotonic_clock::time_point last_synchronized_time_;
53
54 // Max size that the header has consumed. This much extra data will be
55 // reserved in the builder to avoid reallocating.
56 size_t max_header_size_ = 0;
57};
58
59// Replays all the channels in the logfile to the event loop.
60class LogReader {
61 public:
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080062 // If you want to supply a new configuration that will be used for replay
63 // (e.g., to change message rates, or to populate an updated schema), then
64 // pass it in here. It must provide all the channels that the original logged
65 // config did.
66 LogReader(std::string_view filename,
67 const Configuration *replay_configuration = nullptr);
James Kuszmaul7daef362019-12-31 18:28:17 -080068 ~LogReader();
Austin Schuhe309d2a2019-11-29 13:25:21 -080069
James Kuszmaul84ff3e52020-01-03 19:48:53 -080070 // Registers everything, but also updates the real time time in sync. Runs
71 // until the log file starts.
72 // Note that if you use any call other than the Register() call with no
73 // arguments, the user is responsible for making sure that the config of the
74 // supplied event loop (factory) provides any necessary remapped configs.
75 void Register();
76 // Does the same as Register(), except it uses a pre-provided event loop
77 // factory.
78 void Register(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuhe309d2a2019-11-29 13:25:21 -080079 // Registers the timer and senders used to resend the messages from the log
80 // file.
81 void Register(EventLoop *event_loop);
James Kuszmaul84ff3e52020-01-03 19:48:53 -080082 // Unregisters the senders. You only need to call this if you separately
83 // supplied an event loop or event loop factory and the lifetimes are such
84 // that they need to be explicitly destroyed before the LogReader destructor
85 // gets called.
Austin Schuhe309d2a2019-11-29 13:25:21 -080086 void Deregister();
87
Austin Schuhe309d2a2019-11-29 13:25:21 -080088 // Returns the configuration from the log file.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080089 const Configuration *logged_configuration() const;
90 // Returns the configuration being used for replay.
Austin Schuh15649d62019-12-28 16:36:38 -080091 const Configuration *configuration() const;
92
93 // Returns the node that this log file was created on.
94 const Node *node() const;
Austin Schuhe309d2a2019-11-29 13:25:21 -080095
96 // Returns the starting timestamp for the log file.
97 monotonic_clock::time_point monotonic_start_time();
98 realtime_clock::time_point realtime_start_time();
99
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800100 // Causes the logger to publish the provided channel on a different name so
101 // that replayed applications can publish on the proper channel name without
102 // interference. This operates on raw channel names, without any node or
103 // application specific mappings.
104 void RemapLoggedChannel(std::string_view name, std::string_view type,
105 std::string_view add_prefix = "/original");
106 template <typename T>
107 void RemapLoggedChannel(std::string_view name,
108 std::string_view add_prefix = "/original") {
109 RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix);
110 }
111
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800112 SimulatedEventLoopFactory *event_loop_factory() {
113 return event_loop_factory_;
114 }
115
Austin Schuhe309d2a2019-11-29 13:25:21 -0800116 // TODO(austin): Add the ability to re-publish the fetched messages. Add 2
117 // options, one which publishes them *now*, and another which publishes them
118 // to the simulated event loop factory back in time where they actually
119 // happened.
120
121 private:
Austin Schuhe309d2a2019-11-29 13:25:21 -0800122 // Queues at least max_out_of_order_duration_ messages into channels_.
123 void QueueMessages();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800124 // Handle constructing a configuration with all the additional remapped
125 // channels from calls to RemapLoggedChannel.
126 void MakeRemappedConfig();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800127
Austin Schuh05b70472020-01-01 17:11:17 -0800128 // Log chunk reader.
129 SortedMessageReader sorted_message_reader_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800130
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800131 std::unique_ptr<FlatbufferDetachedBuffer<Configuration>>
132 remapped_configuration_buffer_;
133
Austin Schuh05b70472020-01-01 17:11:17 -0800134 std::vector<std::unique_ptr<RawSender>> channels_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800135
Austin Schuh92547522019-12-28 14:33:43 -0800136 std::unique_ptr<EventLoop> event_loop_unique_ptr_;
137 EventLoop *event_loop_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800138 TimerHandler *timer_handler_;
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800139
140 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_;
141 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800142
143 // Map of channel indices to new name. The channel index will be an index into
144 // logged_configuration(), and the string key will be the name of the channel
145 // to send on instead of the logged channel name.
146 std::map<size_t, std::string> remapped_channels_;
147
148 const Configuration *remapped_configuration_ = nullptr;
149 const Configuration *replay_configuration_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800150};
151
152} // namespace logger
153} // namespace aos
154
155#endif // AOS_EVENTS_LOGGER_H_