Commit 59cb2ab0 authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Revert D6745720: [folly][compression] Log (de)compression bytes

Summary:
This reverts commit 1249d203df610cb29c16e03f7a06ea90aea80418

bypass-lint

An infra SEV is better than not reverting this diff.
If you copy this password, see you in SEV Review!
cause_a_sev_many_files

Differential Revision: D6745720

fbshipit-source-id: b357d0d8c42388d6f322cbb8f6d8958f7f02df54
parent 0c20289b
...@@ -51,13 +51,11 @@ ...@@ -51,13 +51,11 @@
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Random.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/Varint.h> #include <folly/Varint.h>
#include <folly/compression/Utils.h> #include <folly/compression/Utils.h>
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
#include <folly/lang/Bits.h> #include <folly/lang/Bits.h>
#include <folly/stop_watch.h>
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
...@@ -67,96 +65,19 @@ using folly::io::compression::detail::prefixToStringLE; ...@@ -67,96 +65,19 @@ using folly::io::compression::detail::prefixToStringLE;
namespace folly { namespace folly {
namespace io { namespace io {
Codec::Codec( Codec::Codec(CodecType type) : type_(type) { }
CodecType type,
Optional<int> level,
StringPiece name,
bool counters)
: type_(type) {
if (counters) {
bytesBeforeCompression_ = {type,
name,
level,
CompressionCounterKey::BYTES_BEFORE_COMPRESSION,
CompressionCounterType::SUM};
bytesAfterCompression_ = {type,
name,
level,
CompressionCounterKey::BYTES_AFTER_COMPRESSION,
CompressionCounterType::SUM};
bytesBeforeDecompression_ = {
type,
name,
level,
CompressionCounterKey::BYTES_BEFORE_DECOMPRESSION,
CompressionCounterType::SUM};
bytesAfterDecompression_ = {
type,
name,
level,
CompressionCounterKey::BYTES_AFTER_DECOMPRESSION,
CompressionCounterType::SUM};
compressions_ = {type,
name,
level,
CompressionCounterKey::COMPRESSIONS,
CompressionCounterType::SUM};
decompressions_ = {type,
name,
level,
CompressionCounterKey::DECOMPRESSIONS,
CompressionCounterType::SUM};
compressionMilliseconds_ = {type,
name,
level,
CompressionCounterKey::COMPRESSION_MILLISECONDS,
CompressionCounterType::SUM};
decompressionMilliseconds_ = {
type,
name,
level,
CompressionCounterKey::DECOMPRESSION_MILLISECONDS,
CompressionCounterType::SUM};
}
}
namespace {
constexpr uint32_t kLoggingRate = 50;
class Timer {
public:
explicit Timer(folly::detail::CompressionCounter& counter)
: counter_(&counter) {}
~Timer() {
*counter_ += timer_.elapsed().count();
}
private:
folly::detail::CompressionCounter* counter_;
stop_watch<std::chrono::milliseconds> timer_;
};
} // namespace
// Ensure consistent behavior in the nullptr case // Ensure consistent behavior in the nullptr case
std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) { std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) {
if (data == nullptr) { if (data == nullptr) {
throw std::invalid_argument("Codec: data must not be nullptr"); throw std::invalid_argument("Codec: data must not be nullptr");
} }
const uint64_t len = data->computeChainDataLength(); uint64_t len = data->computeChainDataLength();
if (len > maxUncompressedLength()) { if (len > maxUncompressedLength()) {
throw std::runtime_error("Codec: uncompressed length too large"); throw std::runtime_error("Codec: uncompressed length too large");
} }
bool const logging = folly::Random::oneIn(kLoggingRate);
folly::Optional<Timer> const timer = return doCompress(data);
logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>();
auto result = doCompress(data);
if (logging) {
compressions_++;
bytesBeforeCompression_ += len;
bytesAfterCompression_ += result->computeChainDataLength();
}
return result;
} }
std::string Codec::compress(const StringPiece data) { std::string Codec::compress(const StringPiece data) {
...@@ -164,16 +85,8 @@ std::string Codec::compress(const StringPiece data) { ...@@ -164,16 +85,8 @@ std::string Codec::compress(const StringPiece data) {
if (len > maxUncompressedLength()) { if (len > maxUncompressedLength()) {
throw std::runtime_error("Codec: uncompressed length too large"); throw std::runtime_error("Codec: uncompressed length too large");
} }
bool const logging = folly::Random::oneIn(kLoggingRate);
folly::Optional<Timer> const timer = return doCompressString(data);
logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>();
auto result = doCompressString(data);
if (logging) {
compressions_++;
bytesBeforeCompression_ += len;
bytesAfterCompression_ += result.size();
}
return result;
} }
std::unique_ptr<IOBuf> Codec::uncompress( std::unique_ptr<IOBuf> Codec::uncompress(
...@@ -197,16 +110,7 @@ std::unique_ptr<IOBuf> Codec::uncompress( ...@@ -197,16 +110,7 @@ std::unique_ptr<IOBuf> Codec::uncompress(
return IOBuf::create(0); return IOBuf::create(0);
} }
bool const logging = folly::Random::oneIn(kLoggingRate); return doUncompress(data, uncompressedLength);
folly::Optional<Timer> const timer =
logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>();
auto result = doUncompress(data, uncompressedLength);
if (logging) {
decompressions_++;
bytesBeforeDecompression_ += data->computeChainDataLength();
bytesAfterDecompression_ += result->computeChainDataLength();
}
return result;
} }
std::string Codec::uncompress( std::string Codec::uncompress(
...@@ -227,16 +131,7 @@ std::string Codec::uncompress( ...@@ -227,16 +131,7 @@ std::string Codec::uncompress(
return ""; return "";
} }
bool const logging = folly::Random::oneIn(kLoggingRate); return doUncompressString(data, uncompressedLength);
folly::Optional<Timer> const timer =
logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>();
auto result = doUncompressString(data, uncompressedLength);
if (logging) {
decompressions_++;
bytesBeforeDecompression_ += data.size();
bytesAfterDecompression_ += result.size();
}
return result;
} }
bool Codec::needsUncompressedLength() const { bool Codec::needsUncompressedLength() const {
...@@ -656,24 +551,23 @@ std::unique_ptr<Codec> LZ4Codec::create(int level, CodecType type) { ...@@ -656,24 +551,23 @@ std::unique_ptr<Codec> LZ4Codec::create(int level, CodecType type) {
return std::make_unique<LZ4Codec>(level, type); return std::make_unique<LZ4Codec>(level, type);
} }
static bool lz4ConvertLevel(int level) { LZ4Codec::LZ4Codec(int level, CodecType type) : Codec(type) {
DCHECK(type == CodecType::LZ4 || type == CodecType::LZ4_VARINT_SIZE);
switch (level) { switch (level) {
case 1:
case COMPRESSION_LEVEL_FASTEST: case COMPRESSION_LEVEL_FASTEST:
case COMPRESSION_LEVEL_DEFAULT: case COMPRESSION_LEVEL_DEFAULT:
return 1; level = 1;
case 2: break;
case COMPRESSION_LEVEL_BEST: case COMPRESSION_LEVEL_BEST:
return 2; level = 2;
break;
} }
throw std::invalid_argument( if (level < 1 || level > 2) {
to<std::string>("LZ4Codec: invalid level: ", level)); throw std::invalid_argument(to<std::string>(
} "LZ4Codec: invalid level: ", level));
}
LZ4Codec::LZ4Codec(int level, CodecType type) highCompression_ = (level > 1);
: Codec(type, lz4ConvertLevel(level)),
highCompression_(lz4ConvertLevel(level) > 1) {
DCHECK(type == CodecType::LZ4 || type == CodecType::LZ4_VARINT_SIZE);
} }
bool LZ4Codec::doNeedsUncompressedLength() const { bool LZ4Codec::doNeedsUncompressedLength() const {
...@@ -845,20 +739,20 @@ void LZ4FrameCodec::resetDCtx() { ...@@ -845,20 +739,20 @@ void LZ4FrameCodec::resetDCtx() {
dirty_ = false; dirty_ = false;
} }
static int lz4fConvertLevel(int level) { LZ4FrameCodec::LZ4FrameCodec(int level, CodecType type) : Codec(type) {
DCHECK(type == CodecType::LZ4_FRAME);
switch (level) { switch (level) {
case COMPRESSION_LEVEL_FASTEST: case COMPRESSION_LEVEL_FASTEST:
case COMPRESSION_LEVEL_DEFAULT: case COMPRESSION_LEVEL_DEFAULT:
return 0; level_ = 0;
break;
case COMPRESSION_LEVEL_BEST: case COMPRESSION_LEVEL_BEST:
return 16; level_ = 16;
break;
default:
level_ = level;
break;
} }
return level;
}
LZ4FrameCodec::LZ4FrameCodec(int level, CodecType type)
: Codec(type, lz4fConvertLevel(level)), level_(lz4fConvertLevel(level)) {
DCHECK(type == CodecType::LZ4_FRAME);
} }
LZ4FrameCodec::~LZ4FrameCodec() { LZ4FrameCodec::~LZ4FrameCodec() {
...@@ -1499,26 +1393,25 @@ std::unique_ptr<StreamCodec> ZSTDStreamCodec::createStream( ...@@ -1499,26 +1393,25 @@ std::unique_ptr<StreamCodec> ZSTDStreamCodec::createStream(
return make_unique<ZSTDStreamCodec>(level, type); return make_unique<ZSTDStreamCodec>(level, type);
} }
static int zstdConvertLevel(int level) { ZSTDStreamCodec::ZSTDStreamCodec(int level, CodecType type)
: StreamCodec(type) {
DCHECK(type == CodecType::ZSTD);
switch (level) { switch (level) {
case COMPRESSION_LEVEL_FASTEST: case COMPRESSION_LEVEL_FASTEST:
return 1; level = 1;
break;
case COMPRESSION_LEVEL_DEFAULT: case COMPRESSION_LEVEL_DEFAULT:
return 1; level = 1;
break;
case COMPRESSION_LEVEL_BEST: case COMPRESSION_LEVEL_BEST:
return 19; level = 19;
break;
} }
if (level < 1 || level > ZSTD_maxCLevel()) { if (level < 1 || level > ZSTD_maxCLevel()) {
throw std::invalid_argument( throw std::invalid_argument(
to<std::string>("ZSTD: invalid level: ", level)); to<std::string>("ZSTD: invalid level: ", level));
} }
return level; level_ = level;
}
ZSTDStreamCodec::ZSTDStreamCodec(int level, CodecType type)
: StreamCodec(type, zstdConvertLevel(level)),
level_(zstdConvertLevel(level)) {
DCHECK(type == CodecType::ZSTD);
} }
bool ZSTDStreamCodec::doNeedsUncompressedLength() const { bool ZSTDStreamCodec::doNeedsUncompressedLength() const {
...@@ -2017,7 +1910,7 @@ void AutomaticCodec::addCodecIfSupported(CodecType type) { ...@@ -2017,7 +1910,7 @@ void AutomaticCodec::addCodecIfSupported(CodecType type) {
AutomaticCodec::AutomaticCodec( AutomaticCodec::AutomaticCodec(
std::vector<std::unique_ptr<Codec>> customCodecs, std::vector<std::unique_ptr<Codec>> customCodecs,
std::unique_ptr<Codec> terminalCodec) std::unique_ptr<Codec> terminalCodec)
: Codec(CodecType::USER_DEFINED, folly::none, "auto"), : Codec(CodecType::USER_DEFINED),
codecs_(std::move(customCodecs)), codecs_(std::move(customCodecs)),
terminalCodec_(std::move(terminalCodec)) { terminalCodec_(std::move(terminalCodec)) {
// Fastest -> slowest // Fastest -> slowest
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#include <folly/Optional.h> #include <folly/Optional.h>
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/compression/Counters.h>
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
/** /**
...@@ -186,11 +185,7 @@ class Codec { ...@@ -186,11 +185,7 @@ class Codec {
folly::Optional<uint64_t> uncompressedLength = folly::none) const; folly::Optional<uint64_t> uncompressedLength = folly::none) const;
protected: protected:
Codec( explicit Codec(CodecType type);
CodecType type,
folly::Optional<int> level = folly::none,
folly::StringPiece name = {},
bool counters = true);
public: public:
/** /**
...@@ -236,14 +231,6 @@ class Codec { ...@@ -236,14 +231,6 @@ class Codec {
folly::Optional<uint64_t> uncompressedLength) const; folly::Optional<uint64_t> uncompressedLength) const;
CodecType type_; CodecType type_;
folly::detail::CompressionCounter bytesBeforeCompression_;
folly::detail::CompressionCounter bytesAfterCompression_;
folly::detail::CompressionCounter bytesBeforeDecompression_;
folly::detail::CompressionCounter bytesAfterDecompression_;
folly::detail::CompressionCounter compressions_;
folly::detail::CompressionCounter decompressions_;
folly::detail::CompressionCounter compressionMilliseconds_;
folly::detail::CompressionCounter decompressionMilliseconds_;
}; };
class StreamCodec : public Codec { class StreamCodec : public Codec {
...@@ -364,12 +351,7 @@ class StreamCodec : public Codec { ...@@ -364,12 +351,7 @@ class StreamCodec : public Codec {
FlushOp flushOp = StreamCodec::FlushOp::NONE); FlushOp flushOp = StreamCodec::FlushOp::NONE);
protected: protected:
StreamCodec( explicit StreamCodec(CodecType type) : Codec(type) {}
CodecType type,
folly::Optional<int> level = folly::none,
folly::StringPiece name = {},
bool counters = true)
: Codec(type, std::move(level), name, counters) {}
// Returns the uncompressed length last passed to resetStream() or none if it // Returns the uncompressed length last passed to resetStream() or none if it
// hasn't been called yet. // hasn't been called yet.
......
...@@ -32,10 +32,6 @@ enum class CompressionCounterKey { ...@@ -32,10 +32,6 @@ enum class CompressionCounterKey {
BYTES_AFTER_COMPRESSION = 1, BYTES_AFTER_COMPRESSION = 1,
BYTES_BEFORE_DECOMPRESSION = 2, BYTES_BEFORE_DECOMPRESSION = 2,
BYTES_AFTER_DECOMPRESSION = 3, BYTES_AFTER_DECOMPRESSION = 3,
COMPRESSIONS = 4,
DECOMPRESSIONS = 5,
COMPRESSION_MILLISECONDS = 6,
DECOMPRESSION_MILLISECONDS = 7,
}; };
enum class CompressionCounterType { enum class CompressionCounterType {
......
...@@ -194,32 +194,28 @@ std::unique_ptr<StreamCodec> ZlibStreamCodec::createStream( ...@@ -194,32 +194,28 @@ std::unique_ptr<StreamCodec> ZlibStreamCodec::createStream(
return std::make_unique<ZlibStreamCodec>(options, level); return std::make_unique<ZlibStreamCodec>(options, level);
} }
static bool inBounds(int value, int low, int high) { ZlibStreamCodec::ZlibStreamCodec(Options options, int level)
return (value >= low) && (value <= high); : StreamCodec(getCodecType(options)) {
}
static int zlibConvertLevel(int level) {
switch (level) { switch (level) {
case COMPRESSION_LEVEL_FASTEST: case COMPRESSION_LEVEL_FASTEST:
return 1; level = 1;
break;
case COMPRESSION_LEVEL_DEFAULT: case COMPRESSION_LEVEL_DEFAULT:
return 6; level = Z_DEFAULT_COMPRESSION;
break;
case COMPRESSION_LEVEL_BEST: case COMPRESSION_LEVEL_BEST:
return 9; level = 9;
break;
} }
if (!inBounds(level, 0, 9)) { auto inBounds = [](int value, int low, int high) {
return (value >= low) && (value <= high);
};
if (level != Z_DEFAULT_COMPRESSION && !inBounds(level, 0, 9)) {
throw std::invalid_argument( throw std::invalid_argument(
to<std::string>("ZlibStreamCodec: invalid level: ", level)); to<std::string>("ZlibStreamCodec: invalid level: ", level));
} }
return level; level_ = level;
}
ZlibStreamCodec::ZlibStreamCodec(Options options, int level)
: StreamCodec(
getCodecType(options),
zlibConvertLevel(level),
getCodecType(options) == CodecType::GZIP ? "gzip" : "zlib"),
level_(zlibConvertLevel(level)) {
options_ = options; options_ = options;
// Although zlib allows a windowSize of 8..15, a value of 8 is not // Although zlib allows a windowSize of 8..15, a value of 8 is not
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/Random.h> #include <folly/Random.h>
#include <folly/Varint.h> #include <folly/Varint.h>
...@@ -1488,3 +1489,14 @@ INSTANTIATE_TEST_CASE_P( ...@@ -1488,3 +1489,14 @@ INSTANTIATE_TEST_CASE_P(
} // namespace test } // namespace test
} // namespace io } // namespace io
} // namespace folly } // namespace folly
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
auto ret = RUN_ALL_TESTS();
if (!ret) {
folly::runBenchmarksOnFlag();
}
return ret;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment