|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/devtools/etdump/data_sink_base.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 13 | +#include <executorch/runtime/core/span.h> |
| 14 | + |
| 15 | +namespace executorch { |
| 16 | +namespace etdump { |
| 17 | + |
| 18 | +/** |
| 19 | + * BufferDataSink is a concrete implementation of the DataSinkBase class, |
| 20 | + * designed to store debug data in a pre-allocated, user-owned buffer. This |
| 21 | + * class provides methods to write raw data and tensor data into the buffer, |
| 22 | + * ensuring proper alignment and managing padding as needed. It is the standard |
| 23 | + * DataSink used by ETDumpGen. |
| 24 | + */ |
| 25 | +class BufferDataSink : public DataSinkBase { |
| 26 | + public: |
| 27 | + /** |
| 28 | + * Constructs a BufferDataSink with a given buffer. |
| 29 | + * |
| 30 | + * @param[in] buffer A Span object representing the buffer where data will be |
| 31 | + * stored. |
| 32 | + */ |
| 33 | + explicit BufferDataSink(::executorch::runtime::Span<uint8_t> buffer) |
| 34 | + : debug_buffer_(buffer), offset_(0), alighment_(64) {} |
| 35 | + |
| 36 | + // Uncopiable and unassignable to avoid double assignment and free of the |
| 37 | + // internal buffer. |
| 38 | + BufferDataSink(const BufferDataSink&) = delete; |
| 39 | + BufferDataSink& operator=(const BufferDataSink&) = delete; |
| 40 | + |
| 41 | + // Movable to be compatible with Result. |
| 42 | + BufferDataSink(BufferDataSink&&) = default; |
| 43 | + BufferDataSink& operator=(BufferDataSink&&) = default; |
| 44 | + |
| 45 | + ~BufferDataSink() override = default; |
| 46 | + |
| 47 | + /** |
| 48 | + * Write data into the debug buffer and return the offset of the starting |
| 49 | + * location of the data within the buffer. |
| 50 | + * |
| 51 | + * @param[in] ptr A pointer to the data to be written into the storage. |
| 52 | + * @param[in] size The size of the data in bytes. |
| 53 | + * @return A Result object containing either: |
| 54 | + * - The offset of the starting location of the data within the |
| 55 | + * debug buffer, or |
| 56 | + * - An error code indicating the failure reason, if any issue |
| 57 | + * occurs during the write process. |
| 58 | + */ |
| 59 | + ::executorch::runtime::Result<size_t> write(const void* ptr, size_t size) |
| 60 | + override; |
| 61 | + |
| 62 | + /** |
| 63 | + * Retrieves the total size of the buffer. |
| 64 | + * |
| 65 | + * @return A Result object containing the total size of the buffer in bytes. |
| 66 | + */ |
| 67 | + ::executorch::runtime::Result<size_t> get_storage_size() const override; |
| 68 | + |
| 69 | + /** |
| 70 | + * Retrieves the number of bytes currently used in the buffer. |
| 71 | + * |
| 72 | + * @return The amount of data currently stored in the buffer in bytes. |
| 73 | + */ |
| 74 | + size_t get_used_bytes() const override; |
| 75 | + |
| 76 | + private: |
| 77 | + // A Span object representing the buffer used for storing debug data. |
| 78 | + ::executorch::runtime::Span<uint8_t> debug_buffer_; |
| 79 | + |
| 80 | + // The offset of the next available location in the buffer. |
| 81 | + size_t offset_; |
| 82 | + |
| 83 | + // The alignment of the buffer. |
| 84 | + size_t alighment_; |
| 85 | +}; |
| 86 | + |
| 87 | +} // namespace etdump |
| 88 | +} // namespace executorch |
0 commit comments