From 6911b3e7e65f1134755450bacfbc5ecb91d6c358 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 22 Dec 2020 12:47:17 +0100 Subject: [PATCH] zstd: use thread pool --- build/Makefile.am | 1 + build/pack.c | 19 +++++++++++++++++++ rpmio/Makefile.am | 2 +- rpmio/rpmio.c | 11 +++++++++++ rpmio/rpmio.h | 2 ++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/build/Makefile.am b/build/Makefile.am index 005bb418f1..0c48346231 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -7,6 +7,7 @@ AM_CFLAGS += @OPENMP_CFLAGS@ AM_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) -I$(top_builddir)/include/ AM_CPPFLAGS += @WITH_MAGIC_INCLUDE@ AM_CPPFLAGS += @WITH_POPT_INCLUDE@ +AM_CPPFLAGS += $(ZSTD_CFLAGS) -DZSTD_STATIC_LINKING_ONLY=1 AM_CPPFLAGS += -I$(top_srcdir)/misc usrlibdir = $(libdir) diff --git a/build/pack.c b/build/pack.c index 8d6f74935e..3fda1cffaf 100644 --- a/build/pack.c +++ b/build/pack.c @@ -20,6 +20,14 @@ #include "build/rpmbuild_internal.h" #include "build/rpmbuild_misc.h" +#ifdef ENABLE_OPENMP +#include +#endif + +#ifdef HAVE_ZSTD +#include +#endif + #include "debug.h" static int rpmPackageFilesArchive(rpmfiles fi, int isSrc, @@ -762,6 +770,12 @@ rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating) } qsort(tasks, npkgs, sizeof(Package), compareBinaries); +#if defined(HAVE_ZSTD) && ZSTD_VERSION_NUMBER >= 10407 + zstd_thread_pool = ZSTD_createThreadPool (omp_get_max_threads ()); + if (zstd_thread_pool == NULL) + rpmlog(RPMLOG_DEBUG, "zstd thread pool cannot be created\n"); +#endif + #pragma omp parallel #pragma omp single for (int i = 0; i < npkgs; i++) { @@ -781,6 +795,11 @@ rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating) break; } +#if ZSTD_VERSION_NUMBER >= 10407 + ZSTD_freeThreadPool (zstd_thread_pool); + zstd_thread_pool = NULL; +#endif + /* Now check the package set if enabled */ if (rc == RPMRC_OK) rc = checkPackageSet(spec->packages); diff --git a/rpmio/Makefile.am b/rpmio/Makefile.am index 7a68caa29e..0dd252c699 100644 --- a/rpmio/Makefile.am +++ b/rpmio/Makefile.am @@ -7,7 +7,7 @@ AM_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) -I$(top_builddir)/include/ AM_CPPFLAGS += @WITH_OPENSSL_INCLUDE@ AM_CPPFLAGS += @WITH_LIBGCRYPT_INCLUDE@ AM_CPPFLAGS += @WITH_POPT_INCLUDE@ -AM_CPPFLAGS += $(ZSTD_CFLAGS) +AM_CPPFLAGS += $(ZSTD_CFLAGS) -DZSTD_STATIC_LINKING_ONLY=1 AM_CPPFLAGS += -I$(top_srcdir)/misc AM_CPPFLAGS += -DRPMCONFIGDIR="\"@RPMCONFIGDIR@\"" AM_CPPFLAGS += -DLOCALSTATEDIR="\"$(localstatedir)\"" diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index 9f4a60aa15..d111e68dee 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -24,6 +24,9 @@ #include "debug.h" +/* Thread pool shared by all ZSTD contexts. */ +void *zstd_thread_pool; + typedef struct FDSTACK_s * FDSTACK_t; struct FDSTACK_s { @@ -1135,6 +1138,14 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) if (threads > 0) { if (ZSTD_isError (ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, threads))) rpmlog(RPMLOG_DEBUG, "zstd library does not support multi-threading\n"); + else { +#if ZSTD_VERSION_NUMBER >= 10407 + if (zstd_thread_pool != NULL) { + if (ZSTD_CCtx_refThreadPool(_stream, zstd_thread_pool) != 0) + rpmlog(RPMLOG_DEBUG, "zstd thread pool cannot be used\n"); + } +#endif + } } nb = ZSTD_CStreamOutSize(); diff --git a/rpmio/rpmio.h b/rpmio/rpmio.h index 4239ef474a..c38148cac0 100644 --- a/rpmio/rpmio.h +++ b/rpmio/rpmio.h @@ -136,6 +136,8 @@ typedef enum fdOpX_e { */ rpmop fdOp(FD_t fd, fdOpX opx); +extern void *zstd_thread_pool; + #ifdef __cplusplus } #endif