blob: 5ae64138209919cbc372cec8e7eb3400e4e18f0a [file] [log] [blame]
James Kuszmaul4ed5fb12022-03-22 15:20:04 -07001#ifndef AOS_UTIL_MCAP_LOGGER_H_
2#define AOS_UTIL_MCAP_LOGGER_H_
3
4#include "aos/configuration_generated.h"
5#include "aos/events/event_loop.h"
6#include "aos/fast_string_builder.h"
7#include "aos/flatbuffer_utils.h"
8#include "single_include/nlohmann/json.hpp"
9
10namespace aos {
11
12// Produces a JSON Schema (https://json-schema.org/) for a given flatbuffer
13// type. If recursion_level is set, will include a $schema attribute indicating
14// the schema definition being used (this is used to allow for recursion).
15//
16// Note that this is pretty bare-bones, so, e.g., we don't distinguish between
17// structs and tables when generating the JSON schema, so we don't bother to
18// mark struct fields as required.
19enum class JsonSchemaRecursion {
20 kTopLevel,
21 kNested,
22};
23nlohmann::json JsonSchemaForFlatbuffer(
24 const FlatbufferType &type,
25 JsonSchemaRecursion recursion_level = JsonSchemaRecursion::kTopLevel);
26
27// Generates an MCAP file, per the specification at
28// https://github.com/foxglove/mcap/tree/main/docs/specification
James Kuszmaulb3fba252022-04-06 15:13:31 -070029// This currently generates an uncompressed logfile with full message indexing
30// available, to be able to support Foxglove fully.
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070031class McapLogger {
32 public:
James Kuszmaulc31d7362022-05-27 14:20:04 -070033 // Whether to serialize the messages into the MCAP file as JSON or
34 // flatbuffers.
35 enum class Serialization {
36 kJson,
37 kFlatbuffer,
38 };
39 McapLogger(EventLoop *event_loop, const std::string &output_path,
40 Serialization serialization);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070041 ~McapLogger();
42
43 private:
44 enum class OpCode {
45 kHeader = 0x01,
46 kFooter = 0x02,
47 kSchema = 0x03,
48 kChannel = 0x04,
49 kMessage = 0x05,
James Kuszmaulb3fba252022-04-06 15:13:31 -070050 kChunk = 0x06,
51 kMessageIndex = 0x07,
52 kChunkIndex = 0x08,
53 kAttachment = 0x09,
54 kAttachmentIndex = 0x0A,
55 kStatistics = 0x0B,
56 kMetadata = 0x0C,
57 kMetadataIndex = 0x0D,
58 kSummaryOffset = 0x0E,
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070059 kDataEnd = 0x0F,
60 };
James Kuszmaulb3fba252022-04-06 15:13:31 -070061 // Stores information associated with a SummaryOffset entry (an offset to the
62 // start of a section within Summary section, which allows readers to quickly
63 // find all the indices/channel definitions/etc. for a given log).
64 struct SummaryOffset {
65 OpCode op_code;
66 // Offset from the start of the file.
67 uint64_t offset;
68 // Total length of the section, in bytes.
69 uint64_t size;
70 };
71 // Information needed to build a ChunkIndex entry.
72 struct ChunkIndex {
73 // Earliest and latest message times within the Chunk being referenced.
74 aos::monotonic_clock::time_point start_time;
75 aos::monotonic_clock::time_point end_time;
76 // Offset from the start of the file to the start of the relevant Chunk.
77 uint64_t offset;
78 // Total size of the Chunk, in bytes.
79 uint64_t chunk_size;
80 // Total size of the records portion of the Chunk, in bytes.
81 uint64_t records_size;
82 // Mapping of channel IDs to the MessageIndex entry for that channel within
83 // the referenced Chunk. The MessageIndex is referenced by an offset from
84 // the start of the file.
85 std::map<uint16_t, uint64_t> message_index_offsets;
86 // Total size, in bytes, of all the MessageIndex entries for this Chunk
87 // together (note that they are required to be contiguous).
88 uint64_t message_index_size;
89 };
90 enum class RegisterHandlers { kYes, kNo };
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070091 // Helpers to write each type of relevant record.
92 void WriteMagic();
93 void WriteHeader();
James Kuszmaulb3fba252022-04-06 15:13:31 -070094 void WriteFooter(uint64_t summary_offset, uint64_t summary_offset_offset);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070095 void WriteDataEnd();
96 void WriteSchema(const uint16_t id, const aos::Channel *channel);
97 void WriteChannel(const uint16_t id, const uint16_t schema_id,
98 const aos::Channel *channel);
99 void WriteMessage(uint16_t channel_id, const Channel *channel,
James Kuszmaulb3fba252022-04-06 15:13:31 -0700100 const Context &context, std::ostream *output);
101 void WriteChunk();
102
103 // The helpers for writing records which appear in the Summary section will
104 // return SummaryOffset's so that they can be referenced in the SummaryOffset
105 // section.
106 SummaryOffset WriteChunkIndices();
107 SummaryOffset WriteStatistics();
108 std::vector<SummaryOffset> WriteSchemasAndChannels(
109 RegisterHandlers register_handlers);
110 void WriteSummaryOffset(const SummaryOffset &offset);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700111
112 // Writes an MCAP record to the output file.
James Kuszmaulb3fba252022-04-06 15:13:31 -0700113 void WriteRecord(OpCode op, std::string_view record, std::ostream *ostream);
114 void WriteRecord(OpCode op, std::string_view record) {
115 WriteRecord(op, record, &output_);
116 }
117 // Adds an MCAP-spec string/byte-array/map/array of pairs/fixed-size integer
118 // to a buffer.
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700119 static void AppendString(FastStringBuilder *builder, std::string_view string);
James Kuszmaulb3fba252022-04-06 15:13:31 -0700120 static void AppendBytes(FastStringBuilder *builder, std::string_view bytes);
121 static void AppendChannelMap(FastStringBuilder *builder,
122 const std::map<uint16_t, uint64_t> &map);
123 static void AppendMessageIndices(
124 FastStringBuilder *builder,
125 const std::vector<std::pair<uint64_t, uint64_t>> &messages);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700126 static void AppendInt16(FastStringBuilder *builder, uint16_t val);
127 static void AppendInt32(FastStringBuilder *builder, uint32_t val);
128 static void AppendInt64(FastStringBuilder *builder, uint64_t val);
129
James Kuszmaulb3fba252022-04-06 15:13:31 -0700130 aos::EventLoop *event_loop_;
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700131 std::ofstream output_;
James Kuszmaulc31d7362022-05-27 14:20:04 -0700132 const Serialization serialization_;
133 size_t total_message_bytes_ = 0;
134 std::map<const Channel *, size_t> total_channel_bytes_;
James Kuszmaulb3fba252022-04-06 15:13:31 -0700135 // Buffer containing serialized message data for the currently-being-built
136 // chunk.
137 std::stringstream current_chunk_;
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700138 FastStringBuilder string_builder_;
James Kuszmaulb3fba252022-04-06 15:13:31 -0700139
140 // Earliest message observed in this logfile.
141 std::optional<aos::monotonic_clock::time_point> earliest_message_;
142 // Earliest message observed in the current chunk.
143 std::optional<aos::monotonic_clock::time_point> earliest_chunk_message_;
144 // Latest message observed.
145 aos::monotonic_clock::time_point latest_message_ =
146 aos::monotonic_clock::min_time;
147 // Count of all messages on each channel, indexed by channel ID.
148 std::map<uint16_t, uint64_t> message_counts_;
James Kuszmaulc31d7362022-05-27 14:20:04 -0700149 std::map<uint16_t, std::unique_ptr<RawFetcher>> fetchers_;
James Kuszmaulb3fba252022-04-06 15:13:31 -0700150 // MessageIndex's for each message. The std::map is indexed by channel ID. The
151 // vector is then a series of pairs of (timestamp, offset from start of
152 // current_chunk_).
153 std::map<uint16_t, std::vector<std::pair<uint64_t, uint64_t>>>
154 message_indices_;
155 // ChunkIndex's for all fully written Chunks.
156 std::vector<ChunkIndex> chunk_indices_;
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700157};
158} // namespace aos
159#endif // AOS_UTIL_MCAP_LOGGER_H_