Commit 870cfabf authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Remove compression counters

Summary:
We haven't found them super useful, so lets delete them and replace them with USDTs.

The problems with the counters are:
* We only have the aggregated results. We can't look at a particular process, stack trace, etc.
* The speed reported isn't accurate.

Reviewed By: yfeldblum

Differential Revision: D18319361

fbshipit-source-id: 778213fb63eae90cc7900b075e054c021ecf561e
parent f9eaa745
...@@ -68,76 +68,8 @@ using folly::io::compression::detail::prefixToStringLE; ...@@ -68,76 +68,8 @@ using folly::io::compression::detail::prefixToStringLE;
namespace folly { namespace folly {
namespace io { namespace io {
Codec::Codec( Codec::Codec(CodecType type, Optional<int> /* level */, StringPiece /* name */)
CodecType type, : type_(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) {
...@@ -148,16 +80,7 @@ std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) { ...@@ -148,16 +80,7 @@ std::unique_ptr<IOBuf> Codec::compress(const IOBuf* 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); return doCompress(data);
folly::Optional<Timer> const timer =
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) {
...@@ -165,16 +88,7 @@ std::string Codec::compress(const StringPiece data) { ...@@ -165,16 +88,7 @@ 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); return doCompressString(data);
folly::Optional<Timer> const timer =
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(
...@@ -198,16 +112,7 @@ std::unique_ptr<IOBuf> Codec::uncompress( ...@@ -198,16 +112,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(
...@@ -228,16 +133,7 @@ std::string Codec::uncompress( ...@@ -228,16 +133,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 {
......
...@@ -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>
/** /**
...@@ -221,8 +220,7 @@ class Codec { ...@@ -221,8 +220,7 @@ class Codec {
Codec( Codec(
CodecType type, CodecType type,
folly::Optional<int> level = folly::none, folly::Optional<int> level = folly::none,
folly::StringPiece name = {}, folly::StringPiece name = {});
bool counters = true);
public: public:
/** /**
...@@ -275,14 +273,6 @@ class Codec { ...@@ -275,14 +273,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 {
...@@ -406,9 +396,8 @@ class StreamCodec : public Codec { ...@@ -406,9 +396,8 @@ class StreamCodec : public Codec {
StreamCodec( StreamCodec(
CodecType type, CodecType type,
folly::Optional<int> level = folly::none, folly::Optional<int> level = folly::none,
folly::StringPiece name = {}, folly::StringPiece name = {})
bool counters = true) : Codec(type, std::move(level), name) {}
: 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.
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/compression/Counters.h>
#include <folly/Portability.h>
namespace folly {
FOLLY_ATTR_WEAK folly::Function<void(double)> makeCompressionCounterHandler(
folly::io::CodecType,
folly::StringPiece,
folly::Optional<int>,
CompressionCounterKey,
CompressionCounterType) {
return {};
}
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <string>
#include <folly/Function.h>
#include <folly/Optional.h>
#include <folly/Range.h>
namespace folly {
namespace io {
enum class CodecType;
} // namespace io
enum class CompressionCounterKey {
BYTES_BEFORE_COMPRESSION = 0,
BYTES_AFTER_COMPRESSION = 1,
BYTES_BEFORE_DECOMPRESSION = 2,
BYTES_AFTER_DECOMPRESSION = 3,
COMPRESSIONS = 4,
DECOMPRESSIONS = 5,
COMPRESSION_MILLISECONDS = 6,
DECOMPRESSION_MILLISECONDS = 7,
};
enum class CompressionCounterType {
AVG = 0,
SUM = 1,
};
/**
* This functions is an extension point when FOLLY_HAVE_WEAK_SYMBOLS is true.
* There is a default no-op implementation provided which can be overrided by
* linking in a library which provides its own definition.
*
* @param codecType The type of the codec for this counter.
* @param codecName The name of the codec for this counter. If the codecName
* is empty it should be defaulted using the codecType.
* @param level Optionally the level used to construct the codec.
* @param key The key of the counter.
* @param counterType The type of the counter.
* @returns A function to increment the counter for the given key and
* type. It may be an empty folly::Function.
*/
folly::Function<void(double)> makeCompressionCounterHandler(
folly::io::CodecType codecType,
folly::StringPiece codecName,
folly::Optional<int> level,
CompressionCounterKey key,
CompressionCounterType counterType);
namespace detail {
/// Wrapper around the makeCompressionCounterHandler() extension point.
class CompressionCounter {
public:
CompressionCounter() : initialized_(true) {}
CompressionCounter(
folly::io::CodecType codecType,
folly::StringPiece codecName,
folly::Optional<int> level,
CompressionCounterKey key,
CompressionCounterType counterType)
: initialized_(false) {
initialize_ = [=]() {
return makeCompressionCounterHandler(
codecType, codecName, level, key, counterType);
};
assert(!initialize_.heapAllocatedMemory());
}
void operator+=(double sum) {
performLazyInit();
if (increment_) {
increment_(sum);
}
}
void operator++() {
*this += 1.0;
}
void operator++(int) {
*this += 1.0;
}
bool hasImplementation() {
performLazyInit();
return static_cast<bool>(increment_);
}
private:
void performLazyInit() {
if (!initialized_) {
initialized_ = true;
increment_ = initialize_();
initialize_ = {};
}
}
bool initialized_;
folly::Function<folly::Function<void(double)>()> initialize_;
folly::Function<void(double)> increment_;
};
} // namespace detail
} // namespace folly
...@@ -464,35 +464,6 @@ static bool codecHasFlush(CodecType type) { ...@@ -464,35 +464,6 @@ static bool codecHasFlush(CodecType type) {
return type != CodecType::BZIP2; return type != CodecType::BZIP2;
} }
namespace {
class NoCountersCodec : public Codec {
public:
NoCountersCodec()
: Codec(CodecType::NO_COMPRESSION, {}, {}, /* counters */ false) {}
private:
uint64_t doMaxCompressedLength(uint64_t uncompressedLength) const override {
return uncompressedLength;
}
std::unique_ptr<IOBuf> doCompress(const IOBuf* buf) override {
return buf->clone();
}
std::unique_ptr<IOBuf> doUncompress(const IOBuf* buf, Optional<uint64_t>)
override {
return buf->clone();
}
};
} // namespace
TEST(CodecTest, NoCounters) {
NoCountersCodec codec;
for (size_t i = 0; i < 1000; ++i) {
EXPECT_EQ("hello", codec.uncompress(codec.compress("hello")));
}
}
class StreamingUnitTest : public testing::TestWithParam<CodecType> { class StreamingUnitTest : public testing::TestWithParam<CodecType> {
protected: protected:
void SetUp() override { void SetUp() override {
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/compression/Counters.h>
#include <folly/compression/Compression.h>
#include <folly/portability/GTest.h>
using ::folly::CompressionCounterType;
using ::folly::detail::CompressionCounter;
namespace {
constexpr auto kCodecType = folly::io::CodecType::USER_DEFINED;
constexpr folly::StringPiece kCodecName = "test";
constexpr auto kKey = folly::CompressionCounterKey::BYTES_AFTER_COMPRESSION;
} // namespace
TEST(FollyCountersTest, HasImplementation) {
CompressionCounter counter(
kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::SUM);
EXPECT_FALSE(counter.hasImplementation());
}
TEST(FollyCountersTest, SumWorks) {
CompressionCounter counter(
kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::SUM);
for (int i = 0; i < 100; ++i) {
++counter;
counter++;
}
}
TEST(FollyCountersTest, AvgWorks) {
CompressionCounter counter(
kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::AVG);
for (int i = 0; i < 100; ++i) {
counter += 5;
}
}
TEST(FollyCountersTest, DefaultConstruction) {
CompressionCounter counter;
++counter;
}
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