forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib: add qatzstd compressor (envoyproxy#32166)
Signed-off-by: giantcroc <[email protected]>
- Loading branch information
GiantCroc
authored
Mar 17, 2024
1 parent
0dede36
commit 9e447bf
Showing
41 changed files
with
909 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
api/contrib/envoy/extensions/compression/qatzstd/compressor/v3alpha/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. | ||
|
||
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
api_proto_package( | ||
deps = ["@com_github_cncf_xds//udpa/annotations:pkg"], | ||
) |
69 changes: 69 additions & 0 deletions
69
api/contrib/envoy/extensions/compression/qatzstd/compressor/v3alpha/qatzstd.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.extensions.compression.qatzstd.compressor.v3alpha; | ||
|
||
import "google/protobuf/wrappers.proto"; | ||
|
||
import "udpa/annotations/status.proto"; | ||
import "validate/validate.proto"; | ||
|
||
option java_package = "io.envoyproxy.envoy.extensions.compression.qatzstd.compressor.v3alpha"; | ||
option java_outer_classname = "QatzstdProto"; | ||
option java_multiple_files = true; | ||
option go_package = "github.com/envoyproxy/go-control-plane/contrib/envoy/extensions/compression/qatzstd/compressor/v3alpha"; | ||
option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
||
// [#protodoc-title: Qatzstd Compressor] | ||
// Qatzstd :ref:`configuration overview <config_qatzstd>`. | ||
// [#extension: envoy.compression.qatzstd.compressor] | ||
|
||
// [#next-free-field: 8] | ||
message Qatzstd { | ||
// Reference to http://facebook.github.io/zstd/zstd_manual.html | ||
enum Strategy { | ||
DEFAULT = 0; | ||
FAST = 1; | ||
DFAST = 2; | ||
GREEDY = 3; | ||
LAZY = 4; | ||
LAZY2 = 5; | ||
BTLAZY2 = 6; | ||
BTOPT = 7; | ||
BTULTRA = 8; | ||
BTULTRA2 = 9; | ||
} | ||
|
||
// Set compression parameters according to pre-defined compression level table. | ||
// Note that exact compression parameters are dynamically determined, | ||
// depending on both compression level and source content size (when known). | ||
// Value 0 means default, and default level is 3. | ||
// | ||
// Setting a level does not automatically set all other compression parameters | ||
// to default. Setting this will however eventually dynamically impact the compression | ||
// parameters which have not been manually set. The manually set | ||
// ones will 'stick'. | ||
google.protobuf.UInt32Value compression_level = 1 [(validate.rules).uint32 = {lte: 22 gte: 1}]; | ||
|
||
// A 32-bits checksum of content is written at end of frame. If not set, defaults to false. | ||
bool enable_checksum = 2; | ||
|
||
// The higher the value of selected strategy, the more complex it is, | ||
// resulting in stronger and slower compression. | ||
// | ||
// Special: value 0 means "use default strategy". | ||
Strategy strategy = 3 [(validate.rules).enum = {defined_only: true}]; | ||
|
||
// Value for compressor's next output buffer. If not set, defaults to 4096. | ||
google.protobuf.UInt32Value chunk_size = 5 [(validate.rules).uint32 = {lte: 65536 gte: 4096}]; | ||
|
||
// Enable QAT to accelerate Zstd compression or not. If not set, defaults to false. | ||
// | ||
// This is useful in the case that users want to enable QAT for a period of time and disable QAT for another period of time, | ||
// they don't have to change the config too much or prepare for another config that has software zstd compressor and just changing the value of this filed. | ||
bool enable_qat_zstd = 6; | ||
|
||
// Fallback to software for Qatzstd when input size is less than this value. | ||
// Valid only ``enable_qat_zstd`` is ``true``. 0 means no fallback at all. If not set, defaults to 4000. | ||
google.protobuf.UInt32Value qat_zstd_fallback_threshold = 7 | ||
[(validate.rules).uint32 = {lte: 65536 gte: 0}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
diff --git a/src/Makefile b/src/Makefile | ||
index 1abf10d..c5fa3a6 100644 | ||
--- a/src/Makefile | ||
+++ b/src/Makefile | ||
@@ -54,7 +54,7 @@ ifneq ($(ICP_ROOT), ) | ||
endif | ||
else | ||
QATFLAGS = -DINTREE | ||
- LDFLAGS = -lqat | ||
+ LDFLAGS += -lqat | ||
ifneq ($(ENABLE_USDM_DRV), 0) | ||
QATFLAGS += -DENABLE_USDM_DRV | ||
LDFLAGS += -lusdm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
load("@rules_foreign_cc//foreign_cc:defs.bzl", "make") | ||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_contrib_extension", | ||
"envoy_cc_library", | ||
"envoy_contrib_package", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_contrib_package() | ||
|
||
make( | ||
name = "qat-zstd", | ||
build_data = ["@com_github_qat_zstd//:all"], | ||
env = select({ | ||
"//bazel:clang_build": { | ||
"CFLAGS": "-Wno-error=unused-parameter -Wno-error=unused-command-line-argument -I$$EXT_BUILD_DEPS/qatlib/include -I$$EXT_BUILD_DEPS/zstd/include", | ||
}, | ||
"//conditions:default": { | ||
"CFLAGS": "-I$$EXT_BUILD_DEPS/qatlib/include -I$$EXT_BUILD_DEPS/zstd/include", | ||
}, | ||
}), | ||
includes = [], | ||
lib_source = "@com_github_qat_zstd//:all", | ||
out_static_libs = ["libqatseqprod.a"], | ||
tags = ["skip_on_windows"], | ||
target_compatible_with = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:x86_64", | ||
], | ||
targets = [ | ||
"ENABLE_USDM_DRV=1", | ||
"install", | ||
], | ||
deps = [ | ||
"//contrib/qat:qatlib", | ||
"//external:zstd", | ||
], | ||
) | ||
|
||
envoy_cc_library( | ||
name = "compressor_lib", | ||
srcs = ["qatzstd_compressor_impl.cc"], | ||
hdrs = ["qatzstd_compressor_impl.h"], | ||
deps = [ | ||
":qat-zstd", | ||
"//envoy/compression/compressor:compressor_interface", | ||
"//envoy/server:factory_context_interface", | ||
"//source/common/buffer:buffer_lib", | ||
"//source/common/compression/zstd/common:zstd_base_lib", | ||
"//source/common/compression/zstd/compressor:compressor_base", | ||
], | ||
) | ||
|
||
envoy_cc_contrib_extension( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":compressor_lib", | ||
":qat-zstd", | ||
"//envoy/event:dispatcher_interface", | ||
"//envoy/thread_local:thread_local_interface", | ||
"//source/common/http:headers_lib", | ||
"//source/extensions/compression/common/compressor:compressor_factory_base_lib", | ||
"@envoy_api//contrib/envoy/extensions/compression/qatzstd/compressor/v3alpha:pkg_cc_proto", | ||
], | ||
) |
81 changes: 81 additions & 0 deletions
81
contrib/qat/compression/qatzstd/compressor/source/config.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "contrib/qat/compression/qatzstd/compressor/source/config.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Compression { | ||
namespace Qatzstd { | ||
namespace Compressor { | ||
|
||
QatzstdCompressorFactory::QatzstdCompressorFactory( | ||
const envoy::extensions::compression::qatzstd::compressor::v3alpha::Qatzstd& qatzstd, | ||
ThreadLocal::SlotAllocator& tls) | ||
: compression_level_( | ||
PROTOBUF_GET_WRAPPED_OR_DEFAULT(qatzstd, compression_level, ZSTD_CLEVEL_DEFAULT)), | ||
enable_checksum_(qatzstd.enable_checksum()), strategy_(qatzstd.strategy()), | ||
chunk_size_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(qatzstd, chunk_size, ZSTD_CStreamOutSize())), | ||
enable_qat_zstd_(qatzstd.enable_qat_zstd()), | ||
qat_zstd_fallback_threshold_(PROTOBUF_GET_WRAPPED_OR_DEFAULT( | ||
qatzstd, qat_zstd_fallback_threshold, DefaultQatZstdFallbackThreshold)), | ||
tls_slot_(nullptr) { | ||
if (enable_qat_zstd_) { | ||
tls_slot_ = ThreadLocal::TypedSlot<QatzstdThreadLocal>::makeUnique(tls); | ||
tls_slot_->set([](Event::Dispatcher&) { return std::make_shared<QatzstdThreadLocal>(); }); | ||
} | ||
} | ||
|
||
QatzstdCompressorFactory::QatzstdThreadLocal::QatzstdThreadLocal() | ||
: initialized_(false), sequenceProducerState_(nullptr) {} | ||
|
||
QatzstdCompressorFactory::QatzstdThreadLocal::~QatzstdThreadLocal() { | ||
if (initialized_) { | ||
/* Free sequence producer state */ | ||
QZSTD_freeSeqProdState(sequenceProducerState_); | ||
/* Stop QAT device, please call this function when | ||
you won't use QAT anymore or before the process exits */ | ||
QZSTD_stopQatDevice(); | ||
} | ||
} | ||
|
||
void* QatzstdCompressorFactory::QatzstdThreadLocal::GetQATSession() { | ||
// The session must be initialized only once in every worker thread. | ||
if (!initialized_) { | ||
|
||
int status = QZSTD_startQatDevice(); | ||
// RELEASE_ASSERT(status == QZSTD_OK, "failed to initialize hardware"); | ||
if (status != QZSTD_OK) { | ||
ENVOY_LOG(warn, "Failed to initialize qat hardware"); | ||
} else { | ||
ENVOY_LOG(debug, "Initialize qat hardware successful"); | ||
} | ||
sequenceProducerState_ = QZSTD_createSeqProdState(); | ||
initialized_ = true; | ||
} | ||
|
||
return sequenceProducerState_; | ||
} | ||
|
||
Envoy::Compression::Compressor::CompressorPtr QatzstdCompressorFactory::createCompressor() { | ||
return std::make_unique<QatzstdCompressorImpl>( | ||
compression_level_, enable_checksum_, strategy_, chunk_size_, enable_qat_zstd_, | ||
qat_zstd_fallback_threshold_, enable_qat_zstd_ ? tls_slot_->get()->GetQATSession() : nullptr); | ||
} | ||
|
||
Envoy::Compression::Compressor::CompressorFactoryPtr | ||
QatzstdCompressorLibraryFactory::createCompressorFactoryFromProtoTyped( | ||
const envoy::extensions::compression::qatzstd::compressor::v3alpha::Qatzstd& proto_config, | ||
Server::Configuration::FactoryContext& context) { | ||
return std::make_unique<QatzstdCompressorFactory>(proto_config, | ||
context.serverFactoryContext().threadLocal()); | ||
} | ||
|
||
/** | ||
* Static registration for the zstd compressor library. @see NamedCompressorLibraryConfigFactory. | ||
*/ | ||
REGISTER_FACTORY(QatzstdCompressorLibraryFactory, | ||
Envoy::Compression::Compressor::NamedCompressorLibraryConfigFactory); | ||
|
||
} // namespace Compressor | ||
} // namespace Qatzstd | ||
} // namespace Compression | ||
} // namespace Extensions | ||
} // namespace Envoy |
Oops, something went wrong.