Skip to content

Commit

Permalink
deps: update zlib to 1.3.0.1-motley-c2469fd
Browse files Browse the repository at this point in the history
PR-URL: #53464
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
nodejs-github-bot authored and targos committed Aug 14, 2024
1 parent e7db639 commit d0c23f3
Show file tree
Hide file tree
Showing 21 changed files with 1,259 additions and 1 deletion.
26 changes: 26 additions & 0 deletions deps/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ check_include_file(stddef.h HAVE_STDDEF_H)
option(ENABLE_SIMD_OPTIMIZATIONS "Enable all SIMD optimizations" OFF)
option(ENABLE_SIMD_AVX512 "Enable SIMD AXV512 optimizations" OFF)
option(USE_ZLIB_RABIN_KARP_HASH "Enable bitstream compatibility with canonical zlib" OFF)
option(ENABLE_INTEL_QAT_COMPRESSION "Enable Intel Quick Assist Technology use for compression" OFF)
option(BUILD_UNITTESTS "Enable standalone unit tests build" OFF)
option(BUILD_MINIZIP_BIN "Enable building minzip_bin tool" OFF)
option(BUILD_ZPIPE "Enable building zpipe tool" OFF)
Expand Down Expand Up @@ -228,6 +229,22 @@ if (ENABLE_SIMD_OPTIMIZATIONS)
endif()
endif()

if (ENABLE_INTEL_QAT_COMPRESSION)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/deflate_qat.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/io_buffers.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/memory.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/qat_buffer_list.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/qat.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/qat_instance.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/session.cpp)
list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp/qat_task.cpp)

# TODO(gustavoa): Find a way to include the qatzpp headers without having the
# presubmit check throw errors.
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/contrib/qat/qatzpp)
add_compile_definitions(QAT_COMPRESSION_ENABLED)
endif()

# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
Expand All @@ -254,6 +271,15 @@ add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HD
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)

if (ENABLE_INTEL_QAT_COMPRESSION)
target_include_directories(zlib PUBLIC ${QATZPP_INCLUDE_DIRS})
target_link_libraries(zlib ${QATZPP_LIBRARY})
target_link_libraries(zlib qat)
target_include_directories(zlibstatic PUBLIC ${QATZPP_INCLUDE_DIRS})
target_link_libraries(zlibstatic ${QATZPP_LIBRARY})
target_link_libraries(zlibstatic qat)
endif()

if(NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version
# encoded into their final filename. We disable this on Cygwin because
Expand Down
312 changes: 312 additions & 0 deletions deps/zlib/contrib/qat/deflate_qat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
/*
* Copyright (C) 2024 Intel Corporation. All rights reserved.
* Authors:
* Gustavo A Espinoza <[email protected]>
* <[email protected]>
*
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "deflate_qat.h"
#include "deflate.h"

#include "session.hpp"
#include "qat_instance.hpp"
#include "qat_buffer_list.hpp"
#include "qat.hpp"

#include <memory>

/*
* TODO(gustavoa): Make the input size adjustable from the memlevel
* attribute on deflateInit.
*/
static constexpr size_t kInputSize = 1024 * 1024;

/* QAT Instances obtained available from the library. */
static std::vector<std::shared_ptr<qat::Instance>> qat_instances;

/*
* TODO(gustavoa): Verify if the ordering of the struct fields won't create
* unnecessary holes in the structure that requires extraneous padding.
*/
struct qat_deflate {
std::unique_ptr<qat::DeflateSession> qat_session;

/* QAT requires contiguous physical pages. Cannot be allocated using
* malloc/new.
*/
uint8_t *input_buffer;
uint8_t *output_buffer;

/* Pointer to the next byte in the output buffer. */
uint8_t *pending_out;

unsigned input_buffer_size;
unsigned output_buffer_size;

unsigned pending_in_count;
unsigned pending_out_count;
};

static std::unique_ptr<qat::DeflateSession> qat_create_session(int level, int wrap)
{
CpaDcChecksum checksum = CPA_DC_NONE;

switch(wrap) {
case 1:
checksum = CPA_DC_ADLER32;
break;
case 2:
checksum = CPA_DC_CRC32;
break;
}

return std::make_unique<qat::DeflateSession>(
qat_instances[0],
(CpaDcCompLvl)level,
checksum,
0
);
}


int qat_deflate_init()
{
return (qat::Initialize()) ? Z_ERRNO : Z_OK;
}

struct qat_deflate* qat_deflate_state_init(int level, int wrap)
{
if (qat_instances.empty()) {
qat_instances = qat::Instance::Create();
}
if (qat_instances.empty()) {
return nullptr;
}

struct qat_deflate *qat_deflate = new struct qat_deflate;
if (!qat_deflate) {
return nullptr;
}

/* TODO(gustavoa): Find a way to utilize all the available instances for the same
* process.
*/
qat_instances[0]->Start();

qat_deflate->qat_session = qat_create_session(level, wrap);

qat_deflate->input_buffer_size = kInputSize;
qat_deflate->input_buffer = qat::AllocBlockArray<uint8_t>(kInputSize, 0);
qat_deflate->output_buffer_size =
qat_deflate->qat_session->GetDeflateBound(qat_deflate->input_buffer_size);
qat_deflate->pending_out = qat_deflate->output_buffer =
qat::AllocBlockArray<uint8_t>(qat_deflate->output_buffer_size, 0);

qat_deflate->pending_in_count = qat_deflate->pending_out_count = 0;

if (!qat_deflate->input_buffer || !qat_deflate->output_buffer) {
return nullptr;
}

return qat_deflate;
}

static unsigned qat_read_buf(z_streamp strm, struct qat_deflate* qat, unsigned size)
{
unsigned len = strm->avail_in;

if (len > size) {
len = size;
}
if (len == 0) return 0;

strm->avail_in -= len;
strm->total_in += len;

zmemcpy(
qat->input_buffer + qat->pending_in_count,
strm->next_in,
len
);

strm->next_in += len;
qat->pending_in_count += len;

return len;
}

void qat_flush_pending(deflate_state* s)
{
unsigned len;
z_streamp strm = s->strm;
struct qat_deflate* qat = s->qat_s;

len = qat->pending_out_count;
if (len > strm->avail_out) len = strm->avail_out;
if (len == 0) return;

zmemcpy(strm->next_out, qat->pending_out, len);

qat->pending_out += len;
qat->pending_out_count -= len;
strm->next_out += len;
strm->avail_out -= len;
strm->total_out += len;
if (qat->pending_out_count == 0) {
qat->pending_out = qat->output_buffer;
}
}

static int qat_compress_pending(deflate_state*s, int flush)
{
struct qat_deflate* qat = s->qat_s;
uint32_t metadata_size;

/* TODO(gustavoa): find a way to make qatzpp setup this number internally. */
cpaDcBufferListGetMetaSize(qat->qat_session->getInstance()->GetHandle(), 1, &metadata_size);

auto job = qat->qat_session->Deflate(
std::make_unique<qat::IOBuffers>(
std::make_unique<qat::BufferListUser>(
qat->input_buffer,
qat->pending_in_count,
metadata_size
),
std::make_unique<qat::BufferListUser>(
qat->output_buffer,
qat->output_buffer_size,
metadata_size
)
), (flush == Z_FINISH && s->strm->avail_in == 0)
);

job->WaitCompletion();

/*
* TODO(gustavoa): make QAT perform the checksum combine.
*/
if (s->wrap == 2) {
s->strm->adler = crc32_combine(
s->strm->adler,
job->GetResults()->checksum,
job->GetResults()->consumed
);
} else if (s->wrap == 1) {
s->strm->adler = adler32(
s->strm->adler,
qat->input_buffer,
job->GetResults()->consumed
);
}

qat->pending_out_count = job->GetResults()->produced;
qat->pending_in_count -= job->GetResults()->consumed;

if(qat->pending_in_count != 0) {
/* Copy any remaining bytes to the beginning of the buffer. */
zmemcpy(
qat->input_buffer,
qat->input_buffer + job->GetResults()->consumed,
qat->pending_in_count
);
}

return 0;
}

qat_block_state qat_deflate_step(deflate_state* s, int flush)
{
z_streamp strm = s->strm;
struct qat_deflate* qat_state = s->qat_s;

for (;;) {
if (qat_state->pending_in_count < qat_state->input_buffer_size) {
qat_read_buf(
strm,
qat_state,
qat_state->input_buffer_size - qat_state->pending_in_count
);
if (qat_state->pending_in_count < qat_state->input_buffer_size && flush == Z_NO_FLUSH) {
return qat_block_need_more;
} else {
qat_compress_pending(s, flush);
}
if (strm->avail_in == 0) {
break;
}
} else {
qat_compress_pending(s, flush);
}

qat_flush_pending(s);
if (strm->avail_out == 0) {
return (flush == Z_FINISH) ? qat_block_finish_started : qat_block_need_more;
}
}

if (flush == Z_FINISH) {
qat_flush_pending(s);
if (strm->avail_out == 0) {
return qat_block_finish_started;
} else {
return qat_block_finish_done;
}
}

qat_flush_pending(s);
if (strm->avail_out == 0) {
return qat_block_done;
}

return qat_block_need_more;
}

int qat_deflate_state_free(deflate_state* s)
{
struct qat_deflate* qat_state = s->qat_s;
if (qat_state->input_buffer) {
qat::Free(qat_state->input_buffer);
}
if (qat_state->output_buffer) {
qat::Free(qat_state->output_buffer);
}

qat_state->qat_session.reset();
delete qat_state;
s->qat_s = nullptr;

return Z_OK;
}

struct qat_deflate *qat_deflate_copy(deflate_state *ss)
{
struct qat_deflate *sqat = ss->qat_s;
struct qat_deflate *dqat = nullptr;

if (!sqat) {
return nullptr;
}

dqat = new struct qat_deflate;

dqat->qat_session = qat_create_session(ss->level, ss->wrap);

dqat->input_buffer_size = sqat->input_buffer_size;
dqat->input_buffer = qat::AllocBlockArray<uint8_t>(dqat->input_buffer_size, 0);

dqat->output_buffer_size = sqat->output_buffer_size;
dqat->output_buffer = qat::AllocBlockArray<uint8_t>(dqat->output_buffer_size, 0);

dqat->pending_in_count = sqat->pending_in_count;
dqat->pending_out_count = sqat->pending_out_count;

dqat->pending_out =
dqat->output_buffer + (sqat->pending_out - sqat->output_buffer);

zmemcpy(dqat->input_buffer, sqat->input_buffer, dqat->input_buffer_size);
zmemcpy(dqat->output_buffer, sqat->output_buffer, dqat->output_buffer_size);

return dqat;
}

Loading

0 comments on commit d0c23f3

Please sign in to comment.