support negative zstd compression levels

Zstd compression goes all the way down to -7. zstd compression level -3
gives around the same compression ratio as lz4 level=3. Need further
testing to see performance comparison

Test: ota_from_target_files, update_device.py
Change-Id: Ic082b31aa8af938f80be628c73667e02353835f0
This commit is contained in:
Daniel Zheng 2023-08-14 17:13:06 -07:00
parent c079a6c34f
commit 3785e7b1f7
5 changed files with 30 additions and 26 deletions

View file

@ -17,7 +17,6 @@
#pragma once
#include <memory>
#include <vector>
#include "libsnapshot/cow_format.h"
namespace android {
@ -25,27 +24,30 @@ namespace snapshot {
class ICompressor {
public:
explicit ICompressor(uint32_t compression_level, uint32_t block_size)
explicit ICompressor(const int32_t compression_level, const uint32_t block_size)
: compression_level_(compression_level), block_size_(block_size) {}
virtual ~ICompressor() {}
// Factory methods for compression methods.
static std::unique_ptr<ICompressor> Gz(uint32_t compression_level, const int32_t block_size);
static std::unique_ptr<ICompressor> Brotli(uint32_t compression_level,
const int32_t block_size);
static std::unique_ptr<ICompressor> Lz4(uint32_t compression_level, const int32_t block_size);
static std::unique_ptr<ICompressor> Zstd(uint32_t compression_level, const int32_t block_size);
static std::unique_ptr<ICompressor> Gz(const int32_t compression_level,
const uint32_t block_size);
static std::unique_ptr<ICompressor> Brotli(const int32_t compression_level,
const uint32_t block_size);
static std::unique_ptr<ICompressor> Lz4(const int32_t compression_level,
const uint32_t block_size);
static std::unique_ptr<ICompressor> Zstd(const int32_t compression_level,
const uint32_t block_size);
static std::unique_ptr<ICompressor> Create(CowCompression compression,
const int32_t block_size);
const uint32_t block_size);
uint32_t GetCompressionLevel() const { return compression_level_; }
int32_t GetCompressionLevel() const { return compression_level_; }
uint32_t GetBlockSize() const { return block_size_; }
[[nodiscard]] virtual std::vector<uint8_t> Compress(const void* data, size_t length) const = 0;
private:
uint32_t compression_level_;
uint32_t block_size_;
const int32_t compression_level_;
const uint32_t block_size_;
};
} // namespace snapshot
} // namespace android

View file

@ -293,7 +293,7 @@ enum CowCompressionAlgorithm : uint8_t {
};
struct CowCompression {
CowCompressionAlgorithm algorithm = kCowCompressNone;
uint32_t compression_level = 0;
int32_t compression_level = 0;
};
static constexpr uint8_t kCowReadAheadNotStarted = 0;

View file

@ -17,6 +17,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <cstdint>
#include <limits>
#include <memory>
#include <queue>
@ -57,7 +58,7 @@ std::optional<CowCompressionAlgorithm> CompressionAlgorithmFromString(std::strin
}
std::unique_ptr<ICompressor> ICompressor::Create(CowCompression compression,
const int32_t block_size) {
const uint32_t block_size) {
switch (compression.algorithm) {
case kCowCompressLz4:
return ICompressor::Lz4(compression.compression_level, block_size);
@ -101,7 +102,7 @@ uint32_t CompressWorker::GetDefaultCompressionLevel(CowCompressionAlgorithm comp
class GzCompressor final : public ICompressor {
public:
GzCompressor(uint32_t compression_level, const uint32_t block_size)
GzCompressor(int32_t compression_level, const uint32_t block_size)
: ICompressor(compression_level, block_size){};
std::vector<uint8_t> Compress(const void* data, size_t length) const override {
@ -122,7 +123,7 @@ class GzCompressor final : public ICompressor {
class Lz4Compressor final : public ICompressor {
public:
Lz4Compressor(uint32_t compression_level, const uint32_t block_size)
Lz4Compressor(int32_t compression_level, const uint32_t block_size)
: ICompressor(compression_level, block_size){};
std::vector<uint8_t> Compress(const void* data, size_t length) const override {
@ -154,7 +155,7 @@ class Lz4Compressor final : public ICompressor {
class BrotliCompressor final : public ICompressor {
public:
BrotliCompressor(uint32_t compression_level, const uint32_t block_size)
BrotliCompressor(int32_t compression_level, const uint32_t block_size)
: ICompressor(compression_level, block_size){};
std::vector<uint8_t> Compress(const void* data, size_t length) const override {
@ -180,7 +181,7 @@ class BrotliCompressor final : public ICompressor {
class ZstdCompressor final : public ICompressor {
public:
ZstdCompressor(uint32_t compression_level, const uint32_t block_size)
ZstdCompressor(int32_t compression_level, const uint32_t block_size)
: ICompressor(compression_level, block_size),
zstd_context_(ZSTD_createCCtx(), ZSTD_freeCCtx) {
ZSTD_CCtx_setParameter(zstd_context_.get(), ZSTD_c_compressionLevel, compression_level);
@ -318,22 +319,23 @@ bool CompressWorker::GetCompressedBuffers(std::vector<std::vector<uint8_t>>* com
}
}
std::unique_ptr<ICompressor> ICompressor::Brotli(uint32_t compression_level,
const int32_t block_size) {
std::unique_ptr<ICompressor> ICompressor::Brotli(const int32_t compression_level,
const uint32_t block_size) {
return std::make_unique<BrotliCompressor>(compression_level, block_size);
}
std::unique_ptr<ICompressor> ICompressor::Gz(uint32_t compression_level, const int32_t block_size) {
std::unique_ptr<ICompressor> ICompressor::Gz(const int32_t compression_level,
const uint32_t block_size) {
return std::make_unique<GzCompressor>(compression_level, block_size);
}
std::unique_ptr<ICompressor> ICompressor::Lz4(uint32_t compression_level,
const int32_t block_size) {
std::unique_ptr<ICompressor> ICompressor::Lz4(const int32_t compression_level,
const uint32_t block_size) {
return std::make_unique<Lz4Compressor>(compression_level, block_size);
}
std::unique_ptr<ICompressor> ICompressor::Zstd(uint32_t compression_level,
const int32_t block_size) {
std::unique_ptr<ICompressor> ICompressor::Zstd(const int32_t compression_level,
const uint32_t block_size) {
return std::make_unique<ZstdCompressor>(compression_level, block_size);
}

View file

@ -134,7 +134,7 @@ bool CowWriterV2::ParseOptions() {
return false;
}
if (parts.size() > 1) {
if (!android::base::ParseUint(parts[1], &compression_.compression_level)) {
if (!android::base::ParseInt(parts[1], &compression_.compression_level)) {
LOG(ERROR) << "failed to parse compression level invalid type: " << parts[1];
return false;
}

View file

@ -149,7 +149,7 @@ bool CowWriterV3::ParseOptions() {
}
if (parts.size() > 1) {
if (!android::base::ParseUint(parts[1], &compression_.compression_level)) {
if (!android::base::ParseInt(parts[1], &compression_.compression_level)) {
LOG(ERROR) << "failed to parse compression level invalid type: " << parts[1];
return false;
}