Loading fs_mgr/libsnapshot/include/libsnapshot/cow_compress.h +2 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #pragma once #include <memory> #include <vector> #include "libsnapshot/cow_format.h" namespace android { Loading @@ -40,8 +41,7 @@ class ICompressor { uint32_t GetCompressionLevel() const { return compression_level_; } uint32_t GetBlockSize() const { return block_size_; } [[nodiscard]] virtual std::basic_string<uint8_t> Compress(const void* data, size_t length) const = 0; [[nodiscard]] virtual std::vector<uint8_t> Compress(const void* data, size_t length) const = 0; private: uint32_t compression_level_; Loading fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h +4 −4 Original line number Diff line number Diff line Loading @@ -122,13 +122,13 @@ class CompressWorker { CompressWorker(std::unique_ptr<ICompressor>&& compressor); bool RunThread(); void EnqueueCompressBlocks(const void* buffer, size_t block_size, size_t num_blocks); bool GetCompressedBuffers(std::vector<std::basic_string<uint8_t>>* compressed_buf); bool GetCompressedBuffers(std::vector<std::vector<uint8_t>>* compressed_buf); void Finalize(); static uint32_t GetDefaultCompressionLevel(CowCompressionAlgorithm compression); static bool CompressBlocks(ICompressor* compressor, size_t block_size, const void* buffer, size_t num_blocks, std::vector<std::basic_string<uint8_t>>* compressed_data); std::vector<std::vector<uint8_t>>* compressed_data); private: struct CompressWork { Loading @@ -136,7 +136,7 @@ class CompressWorker { size_t num_blocks; size_t block_size; bool compression_status = false; std::vector<std::basic_string<uint8_t>> compressed_data; std::vector<std::vector<uint8_t>> compressed_data; }; std::unique_ptr<ICompressor> compressor_; Loading @@ -150,7 +150,7 @@ class CompressWorker { size_t total_processed_ = 0; bool CompressBlocks(const void* buffer, size_t num_blocks, size_t block_size, std::vector<std::basic_string<uint8_t>>* compressed_data); std::vector<std::vector<uint8_t>>* compressed_data); }; // Create an ICowWriter not backed by any file. This is useful for estimating Loading fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp +12 −11 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <limits> #include <memory> #include <queue> #include <vector> #include <android-base/file.h> #include <android-base/logging.h> Loading Loading @@ -103,9 +104,9 @@ class GzCompressor final : public ICompressor { GzCompressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = compressBound(length); std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); uLongf dest_len = bound; auto rv = compress2(buffer.data(), &dest_len, reinterpret_cast<const Bytef*>(data), length, Loading @@ -124,13 +125,13 @@ class Lz4Compressor final : public ICompressor { Lz4Compressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = LZ4_compressBound(length); if (!bound) { LOG(ERROR) << "LZ4_compressBound returned 0"; return {}; } std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); const auto compressed_size = LZ4_compress_default(static_cast<const char*>(data), Loading @@ -156,13 +157,13 @@ class BrotliCompressor final : public ICompressor { BrotliCompressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = BrotliEncoderMaxCompressedSize(length); if (!bound) { LOG(ERROR) << "BrotliEncoderMaxCompressedSize returned 0"; return {}; } std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); size_t encoded_size = bound; auto rv = BrotliEncoderCompress( Loading @@ -186,8 +187,8 @@ class ZstdCompressor final : public ICompressor { ZSTD_CCtx_setParameter(zstd_context_.get(), ZSTD_c_windowLog, log2(GetBlockSize())); }; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::basic_string<uint8_t> buffer(ZSTD_compressBound(length), '\0'); std::vector<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> buffer(ZSTD_compressBound(length), '\0'); const auto compressed_size = ZSTD_compress2(zstd_context_.get(), buffer.data(), buffer.size(), data, length); if (compressed_size <= 0) { Loading @@ -209,13 +210,13 @@ class ZstdCompressor final : public ICompressor { }; bool CompressWorker::CompressBlocks(const void* buffer, size_t num_blocks, size_t block_size, std::vector<std::basic_string<uint8_t>>* compressed_data) { std::vector<std::vector<uint8_t>>* compressed_data) { return CompressBlocks(compressor_.get(), block_size, buffer, num_blocks, compressed_data); } bool CompressWorker::CompressBlocks(ICompressor* compressor, size_t block_size, const void* buffer, size_t num_blocks, std::vector<std::basic_string<uint8_t>>* compressed_data) { std::vector<std::vector<uint8_t>>* compressed_data) { const uint8_t* iter = reinterpret_cast<const uint8_t*>(buffer); while (num_blocks) { auto data = compressor->Compress(iter, block_size); Loading Loading @@ -289,7 +290,7 @@ void CompressWorker::EnqueueCompressBlocks(const void* buffer, size_t block_size cv_.notify_all(); } bool CompressWorker::GetCompressedBuffers(std::vector<std::basic_string<uint8_t>>* compressed_buf) { bool CompressWorker::GetCompressedBuffers(std::vector<std::vector<uint8_t>>* compressed_buf) { while (true) { std::unique_lock<std::mutex> lock(lock_); while ((total_submitted_ != total_processed_) && compressed_queue_.empty() && !stopped_) { Loading fs_mgr/libsnapshot/libsnapshot_cow/test_v2.cpp +6 −4 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <iostream> #include <memory> #include <string_view> #include <vector> #include <android-base/file.h> #include <android-base/logging.h> Loading Loading @@ -429,7 +430,7 @@ TEST_P(CompressionTest, NoBatchWrites) { template <typename T> class HorribleStream : public IByteStream { public: HorribleStream(const std::basic_string<T>& input) : input_(input) {} HorribleStream(const std::vector<T>& input) : input_(input) {} ssize_t Read(void* buffer, size_t length) override { if (pos_ >= input_.size()) { Loading @@ -444,16 +445,17 @@ class HorribleStream : public IByteStream { size_t Size() const override { return input_.size(); } private: std::basic_string<T> input_; std::vector<T> input_; size_t pos_ = 0; }; TEST(HorribleStream, ReadFully) { std::string expected = "this is some data"; std::string expected_str = "this is some data"; std::vector<char> expected{expected_str.begin(), expected_str.end()}; HorribleStream<char> stream(expected); std::string buffer(expected.size(), '\0'); std::vector<char> buffer(expected.size(), '\0'); ASSERT_TRUE(stream.ReadFully(buffer.data(), buffer.size())); ASSERT_EQ(buffer, expected); } Loading fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.h +2 −2 Original line number Diff line number Diff line Loading @@ -81,8 +81,8 @@ class CowWriterV2 : public CowWriterBase { int num_compress_threads_ = 1; std::vector<std::unique_ptr<CompressWorker>> compress_threads_; std::vector<std::future<bool>> threads_; std::vector<std::basic_string<uint8_t>> compressed_buf_; std::vector<std::basic_string<uint8_t>>::iterator buf_iter_; std::vector<std::vector<uint8_t>> compressed_buf_; std::vector<std::vector<uint8_t>>::iterator buf_iter_; std::vector<std::unique_ptr<CowOperationV2>> opbuffer_vec_; std::vector<std::unique_ptr<uint8_t[]>> databuffer_vec_; Loading Loading
fs_mgr/libsnapshot/include/libsnapshot/cow_compress.h +2 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #pragma once #include <memory> #include <vector> #include "libsnapshot/cow_format.h" namespace android { Loading @@ -40,8 +41,7 @@ class ICompressor { uint32_t GetCompressionLevel() const { return compression_level_; } uint32_t GetBlockSize() const { return block_size_; } [[nodiscard]] virtual std::basic_string<uint8_t> Compress(const void* data, size_t length) const = 0; [[nodiscard]] virtual std::vector<uint8_t> Compress(const void* data, size_t length) const = 0; private: uint32_t compression_level_; Loading
fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h +4 −4 Original line number Diff line number Diff line Loading @@ -122,13 +122,13 @@ class CompressWorker { CompressWorker(std::unique_ptr<ICompressor>&& compressor); bool RunThread(); void EnqueueCompressBlocks(const void* buffer, size_t block_size, size_t num_blocks); bool GetCompressedBuffers(std::vector<std::basic_string<uint8_t>>* compressed_buf); bool GetCompressedBuffers(std::vector<std::vector<uint8_t>>* compressed_buf); void Finalize(); static uint32_t GetDefaultCompressionLevel(CowCompressionAlgorithm compression); static bool CompressBlocks(ICompressor* compressor, size_t block_size, const void* buffer, size_t num_blocks, std::vector<std::basic_string<uint8_t>>* compressed_data); std::vector<std::vector<uint8_t>>* compressed_data); private: struct CompressWork { Loading @@ -136,7 +136,7 @@ class CompressWorker { size_t num_blocks; size_t block_size; bool compression_status = false; std::vector<std::basic_string<uint8_t>> compressed_data; std::vector<std::vector<uint8_t>> compressed_data; }; std::unique_ptr<ICompressor> compressor_; Loading @@ -150,7 +150,7 @@ class CompressWorker { size_t total_processed_ = 0; bool CompressBlocks(const void* buffer, size_t num_blocks, size_t block_size, std::vector<std::basic_string<uint8_t>>* compressed_data); std::vector<std::vector<uint8_t>>* compressed_data); }; // Create an ICowWriter not backed by any file. This is useful for estimating Loading
fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp +12 −11 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <limits> #include <memory> #include <queue> #include <vector> #include <android-base/file.h> #include <android-base/logging.h> Loading Loading @@ -103,9 +104,9 @@ class GzCompressor final : public ICompressor { GzCompressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = compressBound(length); std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); uLongf dest_len = bound; auto rv = compress2(buffer.data(), &dest_len, reinterpret_cast<const Bytef*>(data), length, Loading @@ -124,13 +125,13 @@ class Lz4Compressor final : public ICompressor { Lz4Compressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = LZ4_compressBound(length); if (!bound) { LOG(ERROR) << "LZ4_compressBound returned 0"; return {}; } std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); const auto compressed_size = LZ4_compress_default(static_cast<const char*>(data), Loading @@ -156,13 +157,13 @@ class BrotliCompressor final : public ICompressor { BrotliCompressor(uint32_t compression_level, const uint32_t block_size) : ICompressor(compression_level, block_size){}; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> Compress(const void* data, size_t length) const override { const auto bound = BrotliEncoderMaxCompressedSize(length); if (!bound) { LOG(ERROR) << "BrotliEncoderMaxCompressedSize returned 0"; return {}; } std::basic_string<uint8_t> buffer(bound, '\0'); std::vector<uint8_t> buffer(bound, '\0'); size_t encoded_size = bound; auto rv = BrotliEncoderCompress( Loading @@ -186,8 +187,8 @@ class ZstdCompressor final : public ICompressor { ZSTD_CCtx_setParameter(zstd_context_.get(), ZSTD_c_windowLog, log2(GetBlockSize())); }; std::basic_string<uint8_t> Compress(const void* data, size_t length) const override { std::basic_string<uint8_t> buffer(ZSTD_compressBound(length), '\0'); std::vector<uint8_t> Compress(const void* data, size_t length) const override { std::vector<uint8_t> buffer(ZSTD_compressBound(length), '\0'); const auto compressed_size = ZSTD_compress2(zstd_context_.get(), buffer.data(), buffer.size(), data, length); if (compressed_size <= 0) { Loading @@ -209,13 +210,13 @@ class ZstdCompressor final : public ICompressor { }; bool CompressWorker::CompressBlocks(const void* buffer, size_t num_blocks, size_t block_size, std::vector<std::basic_string<uint8_t>>* compressed_data) { std::vector<std::vector<uint8_t>>* compressed_data) { return CompressBlocks(compressor_.get(), block_size, buffer, num_blocks, compressed_data); } bool CompressWorker::CompressBlocks(ICompressor* compressor, size_t block_size, const void* buffer, size_t num_blocks, std::vector<std::basic_string<uint8_t>>* compressed_data) { std::vector<std::vector<uint8_t>>* compressed_data) { const uint8_t* iter = reinterpret_cast<const uint8_t*>(buffer); while (num_blocks) { auto data = compressor->Compress(iter, block_size); Loading Loading @@ -289,7 +290,7 @@ void CompressWorker::EnqueueCompressBlocks(const void* buffer, size_t block_size cv_.notify_all(); } bool CompressWorker::GetCompressedBuffers(std::vector<std::basic_string<uint8_t>>* compressed_buf) { bool CompressWorker::GetCompressedBuffers(std::vector<std::vector<uint8_t>>* compressed_buf) { while (true) { std::unique_lock<std::mutex> lock(lock_); while ((total_submitted_ != total_processed_) && compressed_queue_.empty() && !stopped_) { Loading
fs_mgr/libsnapshot/libsnapshot_cow/test_v2.cpp +6 −4 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <iostream> #include <memory> #include <string_view> #include <vector> #include <android-base/file.h> #include <android-base/logging.h> Loading Loading @@ -429,7 +430,7 @@ TEST_P(CompressionTest, NoBatchWrites) { template <typename T> class HorribleStream : public IByteStream { public: HorribleStream(const std::basic_string<T>& input) : input_(input) {} HorribleStream(const std::vector<T>& input) : input_(input) {} ssize_t Read(void* buffer, size_t length) override { if (pos_ >= input_.size()) { Loading @@ -444,16 +445,17 @@ class HorribleStream : public IByteStream { size_t Size() const override { return input_.size(); } private: std::basic_string<T> input_; std::vector<T> input_; size_t pos_ = 0; }; TEST(HorribleStream, ReadFully) { std::string expected = "this is some data"; std::string expected_str = "this is some data"; std::vector<char> expected{expected_str.begin(), expected_str.end()}; HorribleStream<char> stream(expected); std::string buffer(expected.size(), '\0'); std::vector<char> buffer(expected.size(), '\0'); ASSERT_TRUE(stream.ReadFully(buffer.data(), buffer.size())); ASSERT_EQ(buffer, expected); } Loading
fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.h +2 −2 Original line number Diff line number Diff line Loading @@ -81,8 +81,8 @@ class CowWriterV2 : public CowWriterBase { int num_compress_threads_ = 1; std::vector<std::unique_ptr<CompressWorker>> compress_threads_; std::vector<std::future<bool>> threads_; std::vector<std::basic_string<uint8_t>> compressed_buf_; std::vector<std::basic_string<uint8_t>>::iterator buf_iter_; std::vector<std::vector<uint8_t>> compressed_buf_; std::vector<std::vector<uint8_t>>::iterator buf_iter_; std::vector<std::unique_ptr<CowOperationV2>> opbuffer_vec_; std::vector<std::unique_ptr<uint8_t[]>> databuffer_vec_; Loading