blob: 0892290ea23a3c48eec1ede08b125e5dab3bba6f [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#ifndef AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
2#define AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
3
4#include <sys/uio.h>
5
6#include <string_view>
7#include <vector>
8
9#include "aos/events/event_loop.h"
10#include "aos/events/logging/logger_generated.h"
11#include "flatbuffers/flatbuffers.h"
12
13namespace aos {
14namespace logger {
15
16enum class LogType : uint8_t {
17 // The message originated on this node and should be logged here.
18 kLogMessage,
19 // The message originated on another node, but only the delivery times are
20 // logged here.
21 kLogDeliveryTimeOnly,
22 // The message originated on another node. Log it and the delivery times
23 // together. The message_gateway is responsible for logging any messages
24 // which didn't get delivered.
25 kLogMessageAndDeliveryTime
26};
27
28
29// This class manages efficiently writing a sequence of detached buffers to a
30// file. It queues them up and batches the write operation.
31class DetachedBufferWriter {
32 public:
33 DetachedBufferWriter(std::string_view filename);
34 ~DetachedBufferWriter();
35
36 // TODO(austin): Snappy compress the log file if it ends with .snappy!
37
38 // Queues up a finished FlatBufferBuilder to be written. Steals the detached
39 // buffer from it.
40 void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb);
41 // Queues up a detached buffer directly.
42 void QueueSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer);
43
44 // Triggers data to be provided to the kernel and written.
45 void Flush();
46
47 private:
48 int fd_ = -1;
49
50 // Size of all the data in the queue.
51 size_t queued_size_ = 0;
52
53 // List of buffers to flush.
54 std::vector<flatbuffers::DetachedBuffer> queue_;
55 // List of iovecs to use with writev. This is a member variable to avoid
56 // churn.
57 std::vector<struct iovec> iovec_;
58};
59
60// Packes a message pointed to by the context into a MessageHeader.
61flatbuffers::Offset<MessageHeader> PackMessage(
62 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
63 int channel_index, LogType log_type);
64
65// TODO(austin): 3 objects:
66// 1) log chunk reader. Returns span.
67// 2) Sorted message header reader. Returns sorted messages.
68// 3) LogReader, which does all the registration.
69//
70// Then, we can do a multi-node sim which forwards data nicely, try logging it, and then try replaying it.
71
72// Optimization:
73// Allocate the 256k blocks like we do today. But, refcount them with shared_ptr pointed to by the messageheader that is returned. This avoids the copy.
74
75} // namespace logger
76} // namespace aos
77
78#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_