|
| 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 | +#include <executorch/devtools/etdump/data_sinks/stream_data_sink.h> |
| 10 | +#include <executorch/devtools/etdump/utils.h> |
| 11 | +#include <fcntl.h> // open() |
| 12 | +#include <unistd.h> // For write and close |
| 13 | +#include <cstring> |
| 14 | + |
| 15 | +using ::executorch::runtime::Error; |
| 16 | +using ::executorch::runtime::Result; |
| 17 | + |
| 18 | +namespace executorch { |
| 19 | +namespace etdump { |
| 20 | + |
| 21 | +StreamDataSink::StreamDataSink(StreamDataSink&& other) noexcept |
| 22 | + : buffer_(std::move(other.buffer_)), |
| 23 | + buffer_offset_(other.buffer_offset_), |
| 24 | + file_descriptor_(other.file_descriptor_), |
| 25 | + total_written_bytes_(other.total_written_bytes_), |
| 26 | + alignment_(other.alignment_) { |
| 27 | + other.file_descriptor_ = -1; |
| 28 | +} |
| 29 | + |
| 30 | +Result<StreamDataSink> StreamDataSink::create( |
| 31 | + void* buffer_data_ptr, |
| 32 | + size_t buffer_size, |
| 33 | + const char* file_path, |
| 34 | + size_t alignment) { |
| 35 | + // Check if alignment is a power of two |
| 36 | + if (alignment == 0 || (alignment & (alignment - 1)) != 0) { |
| 37 | + return Error::InvalidArgument; |
| 38 | + } |
| 39 | + |
| 40 | + // Open the file and get the file descriptor |
| 41 | + // It will fail if the file already exists |
| 42 | + int file_descriptor = open(file_path, O_WRONLY | O_CREAT | O_EXCL, 0644); |
| 43 | + if (file_descriptor < 0) { |
| 44 | + // Return an error if the file cannot be accessed or created |
| 45 | + ET_LOG( |
| 46 | + Error, "Failed to open %s: %s (%d)", file_path, strerror(errno), errno); |
| 47 | + return Error::AccessFailed; |
| 48 | + } |
| 49 | + |
| 50 | + // Return the successfully created StreamDataSink |
| 51 | + return StreamDataSink( |
| 52 | + buffer_data_ptr, buffer_size, file_descriptor, alignment); |
| 53 | +} |
| 54 | + |
| 55 | +StreamDataSink::~StreamDataSink() { |
| 56 | + // Flush to and close the file descriptor |
| 57 | + if (file_descriptor_ >= 0) { |
| 58 | + flush(); |
| 59 | + close(file_descriptor_); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +Result<size_t> StreamDataSink::write(const void* ptr, size_t size) { |
| 64 | + if (size == 0) { |
| 65 | + // No data to write, return current offset |
| 66 | + return buffer_offset_ + total_written_bytes_; |
| 67 | + } |
| 68 | + |
| 69 | + const uint8_t* data_ptr = static_cast<const uint8_t*>(ptr); |
| 70 | + |
| 71 | + // Align the buffer offset |
| 72 | + uint8_t* aligned_ptr = |
| 73 | + internal::align_pointer(buffer_.data() + buffer_offset_, alignment_); |
| 74 | + |
| 75 | + // Zero out the padding between data blobs |
| 76 | + size_t n_zero_pad = aligned_ptr - (buffer_.data() + buffer_offset_); |
| 77 | + memset(buffer_.data() + buffer_offset_, 0, n_zero_pad); |
| 78 | + |
| 79 | + // Calculate the new offset |
| 80 | + size_t new_offset = (aligned_ptr - buffer_.data()) + size; |
| 81 | + |
| 82 | + if (new_offset > buffer_.size()) { |
| 83 | + // If the new offset is out of range, flush the buffer and try again |
| 84 | + Result<bool> ret = flush(); |
| 85 | + if (!ret.ok()) { |
| 86 | + return ret.error(); |
| 87 | + } |
| 88 | + |
| 89 | + aligned_ptr = internal::align_pointer(buffer_.data(), alignment_); |
| 90 | + n_zero_pad = aligned_ptr - buffer_.data(); |
| 91 | + memset(buffer_.data(), 0, n_zero_pad); |
| 92 | + new_offset = (aligned_ptr - buffer_.data()) + size; |
| 93 | + |
| 94 | + if (new_offset > buffer_.size()) { |
| 95 | + return Error::OutOfResources; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Copy data to the aligned position |
| 100 | + std::memcpy(aligned_ptr, data_ptr, size); |
| 101 | + buffer_offset_ = new_offset; |
| 102 | + |
| 103 | + return aligned_ptr - buffer_.data() + total_written_bytes_; |
| 104 | +} |
| 105 | + |
| 106 | +size_t StreamDataSink::get_used_bytes() const { |
| 107 | + return total_written_bytes_ + buffer_offset_; |
| 108 | +} |
| 109 | + |
| 110 | +Result<bool> StreamDataSink::flush() { |
| 111 | + if (buffer_offset_ > 0) { |
| 112 | + ssize_t written = ::write(file_descriptor_, buffer_.data(), buffer_offset_); |
| 113 | + if (written != buffer_offset_) { |
| 114 | + return Error::Internal; |
| 115 | + } |
| 116 | + |
| 117 | + total_written_bytes_ += written; |
| 118 | + buffer_offset_ = 0; |
| 119 | + } |
| 120 | + |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace etdump |
| 125 | +} // namespace executorch |
0 commit comments