James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 1 | #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 | |
| 10 | namespace 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. |
| 19 | enum class JsonSchemaRecursion { |
| 20 | kTopLevel, |
| 21 | kNested, |
| 22 | }; |
| 23 | nlohmann::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 Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 29 | // This currently generates an uncompressed logfile with full message indexing |
| 30 | // available, to be able to support Foxglove fully. |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 31 | class McapLogger { |
| 32 | public: |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 33 | // Whether to serialize the messages into the MCAP file as JSON or |
| 34 | // flatbuffers. |
| 35 | enum class Serialization { |
| 36 | kJson, |
| 37 | kFlatbuffer, |
| 38 | }; |
James Kuszmaul | 9f607c6 | 2022-10-27 17:01:55 -0700 | [diff] [blame^] | 39 | // Whether to attempt to shorten channel names. |
| 40 | enum class CanonicalChannelNames { |
| 41 | // Just use the full, unambiguous, channel names. |
| 42 | kCanonical, |
| 43 | // Use GetChannelAliases() to determine the shortest possible name for the |
| 44 | // channel for the current node, and use that in the MCAP file. This makes |
| 45 | // it so that the channels in the resulting file are more likely to match |
| 46 | // the channel names that are used in "real" applications. |
| 47 | kShortened, |
| 48 | }; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 49 | McapLogger(EventLoop *event_loop, const std::string &output_path, |
James Kuszmaul | 9f607c6 | 2022-10-27 17:01:55 -0700 | [diff] [blame^] | 50 | Serialization serialization, CanonicalChannelNames canonical_channels); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 51 | ~McapLogger(); |
| 52 | |
| 53 | private: |
| 54 | enum class OpCode { |
| 55 | kHeader = 0x01, |
| 56 | kFooter = 0x02, |
| 57 | kSchema = 0x03, |
| 58 | kChannel = 0x04, |
| 59 | kMessage = 0x05, |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 60 | kChunk = 0x06, |
| 61 | kMessageIndex = 0x07, |
| 62 | kChunkIndex = 0x08, |
| 63 | kAttachment = 0x09, |
| 64 | kAttachmentIndex = 0x0A, |
| 65 | kStatistics = 0x0B, |
| 66 | kMetadata = 0x0C, |
| 67 | kMetadataIndex = 0x0D, |
| 68 | kSummaryOffset = 0x0E, |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 69 | kDataEnd = 0x0F, |
| 70 | }; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 71 | // Stores information associated with a SummaryOffset entry (an offset to the |
| 72 | // start of a section within Summary section, which allows readers to quickly |
| 73 | // find all the indices/channel definitions/etc. for a given log). |
| 74 | struct SummaryOffset { |
| 75 | OpCode op_code; |
| 76 | // Offset from the start of the file. |
| 77 | uint64_t offset; |
| 78 | // Total length of the section, in bytes. |
| 79 | uint64_t size; |
| 80 | }; |
| 81 | // Information needed to build a ChunkIndex entry. |
| 82 | struct ChunkIndex { |
| 83 | // Earliest and latest message times within the Chunk being referenced. |
| 84 | aos::monotonic_clock::time_point start_time; |
| 85 | aos::monotonic_clock::time_point end_time; |
| 86 | // Offset from the start of the file to the start of the relevant Chunk. |
| 87 | uint64_t offset; |
| 88 | // Total size of the Chunk, in bytes. |
| 89 | uint64_t chunk_size; |
| 90 | // Total size of the records portion of the Chunk, in bytes. |
| 91 | uint64_t records_size; |
| 92 | // Mapping of channel IDs to the MessageIndex entry for that channel within |
| 93 | // the referenced Chunk. The MessageIndex is referenced by an offset from |
| 94 | // the start of the file. |
| 95 | std::map<uint16_t, uint64_t> message_index_offsets; |
| 96 | // Total size, in bytes, of all the MessageIndex entries for this Chunk |
| 97 | // together (note that they are required to be contiguous). |
| 98 | uint64_t message_index_size; |
| 99 | }; |
| 100 | enum class RegisterHandlers { kYes, kNo }; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 101 | // Helpers to write each type of relevant record. |
| 102 | void WriteMagic(); |
| 103 | void WriteHeader(); |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 104 | void WriteFooter(uint64_t summary_offset, uint64_t summary_offset_offset); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 105 | void WriteDataEnd(); |
| 106 | void WriteSchema(const uint16_t id, const aos::Channel *channel); |
| 107 | void WriteChannel(const uint16_t id, const uint16_t schema_id, |
James Kuszmaul | e4aa01d | 2022-06-28 14:09:02 -0700 | [diff] [blame] | 108 | const aos::Channel *channel, |
| 109 | std::string_view override_name = ""); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 110 | void WriteMessage(uint16_t channel_id, const Channel *channel, |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 111 | const Context &context, std::ostream *output); |
| 112 | void WriteChunk(); |
| 113 | |
| 114 | // The helpers for writing records which appear in the Summary section will |
| 115 | // return SummaryOffset's so that they can be referenced in the SummaryOffset |
| 116 | // section. |
| 117 | SummaryOffset WriteChunkIndices(); |
| 118 | SummaryOffset WriteStatistics(); |
| 119 | std::vector<SummaryOffset> WriteSchemasAndChannels( |
| 120 | RegisterHandlers register_handlers); |
| 121 | void WriteSummaryOffset(const SummaryOffset &offset); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 122 | |
| 123 | // Writes an MCAP record to the output file. |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 124 | void WriteRecord(OpCode op, std::string_view record, std::ostream *ostream); |
| 125 | void WriteRecord(OpCode op, std::string_view record) { |
| 126 | WriteRecord(op, record, &output_); |
| 127 | } |
| 128 | // Adds an MCAP-spec string/byte-array/map/array of pairs/fixed-size integer |
| 129 | // to a buffer. |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 130 | static void AppendString(FastStringBuilder *builder, std::string_view string); |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 131 | static void AppendBytes(FastStringBuilder *builder, std::string_view bytes); |
| 132 | static void AppendChannelMap(FastStringBuilder *builder, |
| 133 | const std::map<uint16_t, uint64_t> &map); |
| 134 | static void AppendMessageIndices( |
| 135 | FastStringBuilder *builder, |
| 136 | const std::vector<std::pair<uint64_t, uint64_t>> &messages); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 137 | static void AppendInt16(FastStringBuilder *builder, uint16_t val); |
| 138 | static void AppendInt32(FastStringBuilder *builder, uint32_t val); |
| 139 | static void AppendInt64(FastStringBuilder *builder, uint64_t val); |
| 140 | |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 141 | aos::EventLoop *event_loop_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 142 | std::ofstream output_; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 143 | const Serialization serialization_; |
James Kuszmaul | 9f607c6 | 2022-10-27 17:01:55 -0700 | [diff] [blame^] | 144 | const CanonicalChannelNames canonical_channels_; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 145 | size_t total_message_bytes_ = 0; |
| 146 | std::map<const Channel *, size_t> total_channel_bytes_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 147 | // Buffer containing serialized message data for the currently-being-built |
| 148 | // chunk. |
| 149 | std::stringstream current_chunk_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 150 | FastStringBuilder string_builder_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 151 | |
| 152 | // Earliest message observed in this logfile. |
| 153 | std::optional<aos::monotonic_clock::time_point> earliest_message_; |
| 154 | // Earliest message observed in the current chunk. |
| 155 | std::optional<aos::monotonic_clock::time_point> earliest_chunk_message_; |
| 156 | // Latest message observed. |
| 157 | aos::monotonic_clock::time_point latest_message_ = |
| 158 | aos::monotonic_clock::min_time; |
| 159 | // Count of all messages on each channel, indexed by channel ID. |
| 160 | std::map<uint16_t, uint64_t> message_counts_; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 161 | std::map<uint16_t, std::unique_ptr<RawFetcher>> fetchers_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 162 | // MessageIndex's for each message. The std::map is indexed by channel ID. The |
| 163 | // vector is then a series of pairs of (timestamp, offset from start of |
| 164 | // current_chunk_). |
| 165 | std::map<uint16_t, std::vector<std::pair<uint64_t, uint64_t>>> |
| 166 | message_indices_; |
| 167 | // ChunkIndex's for all fully written Chunks. |
| 168 | std::vector<ChunkIndex> chunk_indices_; |
James Kuszmaul | e4aa01d | 2022-06-28 14:09:02 -0700 | [diff] [blame] | 169 | |
| 170 | // Metadata associated with the fake "configuration" channel that we create in |
| 171 | // order to ensure that foxglove extensions/users have access to the full |
| 172 | // configuration. |
| 173 | uint16_t configuration_id_ = 0; |
| 174 | FlatbufferDetachedBuffer<Channel> configuration_channel_; |
| 175 | FlatbufferDetachedBuffer<Configuration> configuration_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 176 | }; |
| 177 | } // namespace aos |
| 178 | #endif // AOS_UTIL_MCAP_LOGGER_H_ |