blob: e5b91ac3460fca0c2eba5300e2db22df4a3d93bb [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:
Austin Schuh05b70472020-01-01 17:11:17 -080062 LogReader(std::string_view filename);
James Kuszmaul7daef362019-12-31 18:28:17 -080063 ~LogReader();
Austin Schuhe309d2a2019-11-29 13:25:21 -080064
James Kuszmaul84ff3e52020-01-03 19:48:53 -080065 // Registers everything, but also updates the real time time in sync. Runs
66 // until the log file starts.
67 // Note that if you use any call other than the Register() call with no
68 // arguments, the user is responsible for making sure that the config of the
69 // supplied event loop (factory) provides any necessary remapped configs.
70 void Register();
71 // Does the same as Register(), except it uses a pre-provided event loop
72 // factory.
73 void Register(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuhe309d2a2019-11-29 13:25:21 -080074 // Registers the timer and senders used to resend the messages from the log
75 // file.
76 void Register(EventLoop *event_loop);
James Kuszmaul84ff3e52020-01-03 19:48:53 -080077 // Unregisters the senders. You only need to call this if you separately
78 // supplied an event loop or event loop factory and the lifetimes are such
79 // that they need to be explicitly destroyed before the LogReader destructor
80 // gets called.
Austin Schuhe309d2a2019-11-29 13:25:21 -080081 void Deregister();
82
Austin Schuhe309d2a2019-11-29 13:25:21 -080083 // Returns the configuration from the log file.
Austin Schuh15649d62019-12-28 16:36:38 -080084 const Configuration *configuration() const;
85
86 // Returns the node that this log file was created on.
87 const Node *node() const;
Austin Schuhe309d2a2019-11-29 13:25:21 -080088
89 // Returns the starting timestamp for the log file.
90 monotonic_clock::time_point monotonic_start_time();
91 realtime_clock::time_point realtime_start_time();
92
James Kuszmaul84ff3e52020-01-03 19:48:53 -080093 SimulatedEventLoopFactory *event_loop_factory() {
94 return event_loop_factory_;
95 }
96
Austin Schuhe309d2a2019-11-29 13:25:21 -080097 // TODO(austin): Add the ability to re-publish the fetched messages. Add 2
98 // options, one which publishes them *now*, and another which publishes them
99 // to the simulated event loop factory back in time where they actually
100 // happened.
101
102 private:
Austin Schuhe309d2a2019-11-29 13:25:21 -0800103 // Queues at least max_out_of_order_duration_ messages into channels_.
104 void QueueMessages();
105
Austin Schuh05b70472020-01-01 17:11:17 -0800106 // Log chunk reader.
107 SortedMessageReader sorted_message_reader_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800108
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800109 std::unique_ptr<FlatbufferDetachedBuffer<Configuration>>
110 remapped_configuration_buffer_;
111
Austin Schuh05b70472020-01-01 17:11:17 -0800112 std::vector<std::unique_ptr<RawSender>> channels_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800113
Austin Schuh92547522019-12-28 14:33:43 -0800114 std::unique_ptr<EventLoop> event_loop_unique_ptr_;
115 EventLoop *event_loop_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800116 TimerHandler *timer_handler_;
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800117
118 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_;
119 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800120};
121
122} // namespace logger
123} // namespace aos
124
125#endif // AOS_EVENTS_LOGGER_H_