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 | }; |
| 39 | McapLogger(EventLoop *event_loop, const std::string &output_path, |
| 40 | Serialization serialization); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 41 | ~McapLogger(); |
| 42 | |
| 43 | private: |
| 44 | enum class OpCode { |
| 45 | kHeader = 0x01, |
| 46 | kFooter = 0x02, |
| 47 | kSchema = 0x03, |
| 48 | kChannel = 0x04, |
| 49 | kMessage = 0x05, |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 50 | 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 Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 59 | kDataEnd = 0x0F, |
| 60 | }; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 61 | // 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 Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 91 | // Helpers to write each type of relevant record. |
| 92 | void WriteMagic(); |
| 93 | void WriteHeader(); |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 94 | void WriteFooter(uint64_t summary_offset, uint64_t summary_offset_offset); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 95 | 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, |
James Kuszmaul | e4aa01d | 2022-06-28 14:09:02 -0700 | [diff] [blame^] | 98 | const aos::Channel *channel, |
| 99 | std::string_view override_name = ""); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 100 | void WriteMessage(uint16_t channel_id, const Channel *channel, |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 101 | const Context &context, std::ostream *output); |
| 102 | void WriteChunk(); |
| 103 | |
| 104 | // The helpers for writing records which appear in the Summary section will |
| 105 | // return SummaryOffset's so that they can be referenced in the SummaryOffset |
| 106 | // section. |
| 107 | SummaryOffset WriteChunkIndices(); |
| 108 | SummaryOffset WriteStatistics(); |
| 109 | std::vector<SummaryOffset> WriteSchemasAndChannels( |
| 110 | RegisterHandlers register_handlers); |
| 111 | void WriteSummaryOffset(const SummaryOffset &offset); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 112 | |
| 113 | // Writes an MCAP record to the output file. |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 114 | void WriteRecord(OpCode op, std::string_view record, std::ostream *ostream); |
| 115 | void WriteRecord(OpCode op, std::string_view record) { |
| 116 | WriteRecord(op, record, &output_); |
| 117 | } |
| 118 | // Adds an MCAP-spec string/byte-array/map/array of pairs/fixed-size integer |
| 119 | // to a buffer. |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 120 | static void AppendString(FastStringBuilder *builder, std::string_view string); |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 121 | static void AppendBytes(FastStringBuilder *builder, std::string_view bytes); |
| 122 | static void AppendChannelMap(FastStringBuilder *builder, |
| 123 | const std::map<uint16_t, uint64_t> &map); |
| 124 | static void AppendMessageIndices( |
| 125 | FastStringBuilder *builder, |
| 126 | const std::vector<std::pair<uint64_t, uint64_t>> &messages); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 127 | static void AppendInt16(FastStringBuilder *builder, uint16_t val); |
| 128 | static void AppendInt32(FastStringBuilder *builder, uint32_t val); |
| 129 | static void AppendInt64(FastStringBuilder *builder, uint64_t val); |
| 130 | |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 131 | aos::EventLoop *event_loop_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 132 | std::ofstream output_; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 133 | const Serialization serialization_; |
| 134 | size_t total_message_bytes_ = 0; |
| 135 | std::map<const Channel *, size_t> total_channel_bytes_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 136 | // Buffer containing serialized message data for the currently-being-built |
| 137 | // chunk. |
| 138 | std::stringstream current_chunk_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 139 | FastStringBuilder string_builder_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 140 | |
| 141 | // Earliest message observed in this logfile. |
| 142 | std::optional<aos::monotonic_clock::time_point> earliest_message_; |
| 143 | // Earliest message observed in the current chunk. |
| 144 | std::optional<aos::monotonic_clock::time_point> earliest_chunk_message_; |
| 145 | // Latest message observed. |
| 146 | aos::monotonic_clock::time_point latest_message_ = |
| 147 | aos::monotonic_clock::min_time; |
| 148 | // Count of all messages on each channel, indexed by channel ID. |
| 149 | std::map<uint16_t, uint64_t> message_counts_; |
James Kuszmaul | c31d736 | 2022-05-27 14:20:04 -0700 | [diff] [blame] | 150 | std::map<uint16_t, std::unique_ptr<RawFetcher>> fetchers_; |
James Kuszmaul | b3fba25 | 2022-04-06 15:13:31 -0700 | [diff] [blame] | 151 | // MessageIndex's for each message. The std::map is indexed by channel ID. The |
| 152 | // vector is then a series of pairs of (timestamp, offset from start of |
| 153 | // current_chunk_). |
| 154 | std::map<uint16_t, std::vector<std::pair<uint64_t, uint64_t>>> |
| 155 | message_indices_; |
| 156 | // ChunkIndex's for all fully written Chunks. |
| 157 | std::vector<ChunkIndex> chunk_indices_; |
James Kuszmaul | e4aa01d | 2022-06-28 14:09:02 -0700 | [diff] [blame^] | 158 | |
| 159 | // Metadata associated with the fake "configuration" channel that we create in |
| 160 | // order to ensure that foxglove extensions/users have access to the full |
| 161 | // configuration. |
| 162 | uint16_t configuration_id_ = 0; |
| 163 | FlatbufferDetachedBuffer<Channel> configuration_channel_; |
| 164 | FlatbufferDetachedBuffer<Configuration> configuration_; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 165 | }; |
| 166 | } // namespace aos |
| 167 | #endif // AOS_UTIL_MCAP_LOGGER_H_ |