Aleksandr Kazakov 3c9fa8
From a5803faa083690526b96484c7e6a4cc915ca3921 Mon Nov 28 16:35:09 2022
Aleksandr Kazakov 3c9fa8
From: Aleksandr Kazakov <alexkazakov@meta.com>
Aleksandr Kazakov 3c9fa8
Date: Mon, 28 Nov 2022 16:35:09 +0000
Aleksandr Kazakov 3c9fa8
Subject: [PATCH] Backport multi-threaded zstd to 4.14.x to support
Aleksandr Kazakov 3c9fa8
 multi-threaded zstd compression on centos 8
Aleksandr Kazakov 3c9fa8
Aleksandr Kazakov 3c9fa8
Signed-off-by: Aleksandr Kazakov <alexkazakov@meta.com>
Aleksandr Kazakov 3c9fa8
---
Aleksandr Kazakov 3c9fa8
 configure.ac  |  2 +-
Aleksandr Kazakov 3c9fa8
 macros.in     |  1 +
Aleksandr Kazakov 3c9fa8
 rpmio/rpmio.c | 82 +++++++++++++++++++++++++++++++++++----------------
Aleksandr Kazakov 3c9fa8
 3 files changed, 58 insertions(+), 27 deletions(-)
Aleksandr Kazakov 3c9fa8
Aleksandr Kazakov 3c9fa8
diff --git a/configure.ac b/configure.ac
Aleksandr Kazakov 3c9fa8
index 47327bd..b1213ae 100644
Aleksandr Kazakov 3c9fa8
--- a/configure.ac
Aleksandr Kazakov 3c9fa8
+++ b/configure.ac
Aleksandr Kazakov 3c9fa8
@@ -214,7 +214,7 @@ AC_ARG_ENABLE([zstd],
Aleksandr Kazakov 3c9fa8
               [enable_zstd=auto])
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
 AS_IF([test "x$enable_zstd" != "xno"], [
Aleksandr Kazakov 3c9fa8
-  PKG_CHECK_MODULES([ZSTD], [libzstd], [have_zstd=yes], [have_zstd=no])
Aleksandr Kazakov 3c9fa8
+  PKG_CHECK_MODULES([ZSTD], [libzstd >= 1.3.8], [have_zstd=yes], [have_zstd=no])
Aleksandr Kazakov 3c9fa8
   AS_IF([test "$enable_zstd" = "yes"], [
Aleksandr Kazakov 3c9fa8
     if test "$have_zstd" = "no"; then
Aleksandr Kazakov 3c9fa8
       AC_MSG_ERROR([--enable-zstd specified, but not available])
Aleksandr Kazakov 3c9fa8
diff --git a/macros.in b/macros.in
Aleksandr Kazakov 3c9fa8
index 9b9fe23..832b60a 100644
Aleksandr Kazakov 3c9fa8
--- a/macros.in
Aleksandr Kazakov 3c9fa8
+++ b/macros.in
Aleksandr Kazakov 3c9fa8
@@ -394,6 +394,7 @@ package or when debugging this package.\
Aleksandr Kazakov 3c9fa8
 #		"w9.bzdio"	bzip2 level 9.
Aleksandr Kazakov 3c9fa8
 #		"w6.xzdio"	xz level 6, xz's default.
Aleksandr Kazakov 3c9fa8
 #		"w7T16.xzdio"	xz level 7 using 16 thread (xz only)
Aleksandr Kazakov 3c9fa8
+#		"w19T8.zstdio"	zstd level 19 using 8 threads
Aleksandr Kazakov 3c9fa8
 #		"w6.lzdio"	lzma-alone level 6, lzma's default
Aleksandr Kazakov 3c9fa8
 #
Aleksandr Kazakov 3c9fa8
 #%_source_payload	w9.gzdio
Aleksandr Kazakov 3c9fa8
diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
Aleksandr Kazakov 3c9fa8
index 09b5d02..d030a9c 100644
Aleksandr Kazakov 3c9fa8
--- a/rpmio/rpmio.c
Aleksandr Kazakov 3c9fa8
+++ b/rpmio/rpmio.c
Aleksandr Kazakov 3c9fa8
@@ -1070,6 +1070,7 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode)
Aleksandr Kazakov 3c9fa8
     char *t = stdio;
Aleksandr Kazakov 3c9fa8
     char *te = t + sizeof(stdio) - 2;
Aleksandr Kazakov 3c9fa8
     int c;
Aleksandr Kazakov 3c9fa8
+    int threads = 0;
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
     switch ((c = *s++)) {
Aleksandr Kazakov 3c9fa8
     case 'a':
Aleksandr Kazakov 3c9fa8
@@ -1098,7 +1099,14 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode)
Aleksandr Kazakov 3c9fa8
 	    flags &= ~O_ACCMODE;
Aleksandr Kazakov 3c9fa8
 	    flags |= O_RDWR;
Aleksandr Kazakov 3c9fa8
 	    continue;
Aleksandr Kazakov 3c9fa8
-	    break;
Aleksandr Kazakov 3c9fa8
+	case 'T':
Aleksandr Kazakov 3c9fa8
+	    if (*s >= '0' && *s <= '9') {
Aleksandr Kazakov 3c9fa8
+		threads = strtol(s, (char **)&s, 10);
Aleksandr Kazakov 3c9fa8
+		/* T0 means automatic detection */
Aleksandr Kazakov 3c9fa8
+		if (threads == 0)
Aleksandr Kazakov 3c9fa8
+		    threads = sysconf(_SC_NPROCESSORS_ONLN);
Aleksandr Kazakov 3c9fa8
+	    }
Aleksandr Kazakov 3c9fa8
+	    continue;
Aleksandr Kazakov 3c9fa8
 	default:
Aleksandr Kazakov 3c9fa8
 	    if (c >= (int)'0' && c <= (int)'9') {
Aleksandr Kazakov 3c9fa8
 		level = strtol(s-1, (char **)&s, 10);
Aleksandr Kazakov 3c9fa8
@@ -1132,10 +1140,16 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode)
Aleksandr Kazakov 3c9fa8
 	}
Aleksandr Kazakov 3c9fa8
 	nb = ZSTD_DStreamInSize();
Aleksandr Kazakov 3c9fa8
     } else {					/* compressing */
Aleksandr Kazakov 3c9fa8
-	if ((_stream = (void *) ZSTD_createCStream()) == NULL
Aleksandr Kazakov 3c9fa8
-	 || ZSTD_isError(ZSTD_initCStream(_stream, level))) {
Aleksandr Kazakov 3c9fa8
+	if ((_stream = (void *) ZSTD_createCCtx()) == NULL
Aleksandr Kazakov 3c9fa8
+	 || ZSTD_isError(ZSTD_CCtx_setParameter(_stream, ZSTD_c_compressionLevel, level))) {
Aleksandr Kazakov 3c9fa8
 	    goto err;
Aleksandr Kazakov 3c9fa8
 	}
Aleksandr Kazakov 3c9fa8
+
Aleksandr Kazakov 3c9fa8
+	rpmlog(RPMLOG_DEBUG, "using %i threads in zstd compression\n", threads);
Aleksandr Kazakov 3c9fa8
+	if (threads > 0) {
Aleksandr Kazakov 3c9fa8
+	    if (ZSTD_isError (ZSTD_CCtx_setParameter(_stream, ZSTD_c_nbWorkers, threads)))
Aleksandr Kazakov 3c9fa8
+		rpmlog(RPMLOG_WARNING, "zstd library does not support multi-threading\n");
Aleksandr Kazakov 3c9fa8
+	}
Aleksandr Kazakov 3c9fa8
 	nb = ZSTD_CStreamOutSize();
Aleksandr Kazakov 3c9fa8
     }
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
@@ -1155,7 +1169,7 @@ err:
Aleksandr Kazakov 3c9fa8
     if ((flags & O_ACCMODE) == O_RDONLY)
Aleksandr Kazakov 3c9fa8
 	ZSTD_freeDStream(_stream);
Aleksandr Kazakov 3c9fa8
     else
Aleksandr Kazakov 3c9fa8
-	ZSTD_freeCStream(_stream);
Aleksandr Kazakov 3c9fa8
+	ZSTD_freeCCtx(_stream);
Aleksandr Kazakov 3c9fa8
     return NULL;
Aleksandr Kazakov 3c9fa8
 }
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
@@ -1181,16 +1195,24 @@ assert(zstd);
Aleksandr Kazakov 3c9fa8
 	rc = 0;
Aleksandr Kazakov 3c9fa8
     } else {					/* compressing */
Aleksandr Kazakov 3c9fa8
 	/* close frame */
Aleksandr Kazakov 3c9fa8
-	zstd->zob.dst  = zstd->b;
Aleksandr Kazakov 3c9fa8
-	zstd->zob.size = zstd->nb;
Aleksandr Kazakov 3c9fa8
-	zstd->zob.pos  = 0;
Aleksandr Kazakov 3c9fa8
-	int xx = ZSTD_flushStream(zstd->_stream, &zstd->zob);
Aleksandr Kazakov 3c9fa8
-	if (ZSTD_isError(xx))
Aleksandr Kazakov 3c9fa8
-	    fps->errcookie = ZSTD_getErrorName(xx);
Aleksandr Kazakov 3c9fa8
-	else if (zstd->zob.pos != fwrite(zstd->b, 1, zstd->zob.pos, zstd->fp))
Aleksandr Kazakov 3c9fa8
-	    fps->errcookie = "zstdFlush fwrite failed.";
Aleksandr Kazakov 3c9fa8
-	else
Aleksandr Kazakov 3c9fa8
-	    rc = 0;
Aleksandr Kazakov 3c9fa8
+	int xx;
Aleksandr Kazakov 3c9fa8
+	do {
Aleksandr Kazakov 3c9fa8
+	  ZSTD_inBuffer zib = { NULL, 0, 0 };
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.dst  = zstd->b;
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.size = zstd->nb;
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.pos  = 0;
Aleksandr Kazakov 3c9fa8
+	  xx = ZSTD_compressStream2(zstd->_stream, &zstd->zob, &zib, ZSTD_e_flush);
Aleksandr Kazakov 3c9fa8
+	  if (ZSTD_isError(xx)) {
Aleksandr Kazakov 3c9fa8
+	      fps->errcookie = ZSTD_getErrorName(xx);
Aleksandr Kazakov 3c9fa8
+	      break;
Aleksandr Kazakov 3c9fa8
+	  }
Aleksandr Kazakov 3c9fa8
+	  else if (zstd->zob.pos != fwrite(zstd->b, 1, zstd->zob.pos, zstd->fp)) {
Aleksandr Kazakov 3c9fa8
+	      fps->errcookie = "zstdClose fwrite failed.";
Aleksandr Kazakov 3c9fa8
+	      break;
Aleksandr Kazakov 3c9fa8
+	  }
Aleksandr Kazakov 3c9fa8
+	  else
Aleksandr Kazakov 3c9fa8
+	      rc = 0;
Aleksandr Kazakov 3c9fa8
+	} while (xx != 0);
Aleksandr Kazakov 3c9fa8
     }
Aleksandr Kazakov 3c9fa8
     return rc;
Aleksandr Kazakov 3c9fa8
 }
Aleksandr Kazakov 3c9fa8
@@ -1235,7 +1257,7 @@ assert(zstd);
Aleksandr Kazakov 3c9fa8
 	zstd->zob.pos  = 0;
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
 	/* Compress next chunk. */
Aleksandr Kazakov 3c9fa8
-        int xx = ZSTD_compressStream(zstd->_stream, &zstd->zob, &zib;;
Aleksandr Kazakov 3c9fa8
+        int xx = ZSTD_compressStream2(zstd->_stream, &zstd->zob, &zib, ZSTD_e_continue);
Aleksandr Kazakov 3c9fa8
         if (ZSTD_isError(xx)) {
Aleksandr Kazakov 3c9fa8
 	    fps->errcookie = ZSTD_getErrorName(xx);
Aleksandr Kazakov 3c9fa8
 	    return -1;
Aleksandr Kazakov 3c9fa8
@@ -1264,17 +1286,25 @@ assert(zstd);
Aleksandr Kazakov 3c9fa8
 	ZSTD_freeDStream(zstd->_stream);
Aleksandr Kazakov 3c9fa8
     } else {					/* compressing */
Aleksandr Kazakov 3c9fa8
 	/* close frame */
Aleksandr Kazakov 3c9fa8
-	zstd->zob.dst  = zstd->b;
Aleksandr Kazakov 3c9fa8
-	zstd->zob.size = zstd->nb;
Aleksandr Kazakov 3c9fa8
-	zstd->zob.pos  = 0;
Aleksandr Kazakov 3c9fa8
-	int xx = ZSTD_endStream(zstd->_stream, &zstd->zob);
Aleksandr Kazakov 3c9fa8
-	if (ZSTD_isError(xx))
Aleksandr Kazakov 3c9fa8
-	    fps->errcookie = ZSTD_getErrorName(xx);
Aleksandr Kazakov 3c9fa8
-	else if (zstd->zob.pos != fwrite(zstd->b, 1, zstd->zob.pos, zstd->fp))
Aleksandr Kazakov 3c9fa8
-	    fps->errcookie = "zstdClose fwrite failed.";
Aleksandr Kazakov 3c9fa8
-	else
Aleksandr Kazakov 3c9fa8
-	    rc = 0;
Aleksandr Kazakov 3c9fa8
-	ZSTD_freeCStream(zstd->_stream);
Aleksandr Kazakov 3c9fa8
+	int xx;
Aleksandr Kazakov 3c9fa8
+	do {
Aleksandr Kazakov 3c9fa8
+	  ZSTD_inBuffer zib = { NULL, 0, 0 };
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.dst  = zstd->b;
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.size = zstd->nb;
Aleksandr Kazakov 3c9fa8
+	  zstd->zob.pos  = 0;
Aleksandr Kazakov 3c9fa8
+	  xx = ZSTD_compressStream2(zstd->_stream, &zstd->zob, &zib, ZSTD_e_end);
Aleksandr Kazakov 3c9fa8
+	  if (ZSTD_isError(xx)) {
Aleksandr Kazakov 3c9fa8
+	      fps->errcookie = ZSTD_getErrorName(xx);
Aleksandr Kazakov 3c9fa8
+	      break;
Aleksandr Kazakov 3c9fa8
+	  }
Aleksandr Kazakov 3c9fa8
+	  else if (zstd->zob.pos != fwrite(zstd->b, 1, zstd->zob.pos, zstd->fp)) {
Aleksandr Kazakov 3c9fa8
+	      fps->errcookie = "zstdClose fwrite failed.";
Aleksandr Kazakov 3c9fa8
+	      break;
Aleksandr Kazakov 3c9fa8
+	  }
Aleksandr Kazakov 3c9fa8
+	  else
Aleksandr Kazakov 3c9fa8
+	      rc = 0;
Aleksandr Kazakov 3c9fa8
+	} while (xx != 0);
Aleksandr Kazakov 3c9fa8
+	ZSTD_freeCCtx(zstd->_stream);
Aleksandr Kazakov 3c9fa8
     }
Aleksandr Kazakov 3c9fa8
 
Aleksandr Kazakov 3c9fa8
     if (zstd->fp && fileno(zstd->fp) > 2)
Aleksandr Kazakov 3c9fa8
-- 
Aleksandr Kazakov 3c9fa8
2.38.1
Aleksandr Kazakov 3c9fa8