Skip to content

Commit

Permalink
zstd: Implement core detection (#2083)
Browse files Browse the repository at this point in the history
The bsdtar manual page claims that setting zstd:threads to 0 tells zstd
to use as many threads as there are cores in the system, but it actually
disables multi-threading.  Replace 0 with the number of configured
processors.

While here, add a previously missing overflow check.

Co-authored-by: Martin Matuska <martin@matuska.de>
  • Loading branch information
dag-erling and mmatuska committed Apr 23, 2024
1 parent a69a453 commit 3efcadf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,7 @@ CHECK_FUNCTION_EXISTS_GLIBC(strncpy_s HAVE_STRNCPY_S)
CHECK_FUNCTION_EXISTS_GLIBC(strnlen HAVE_STRNLEN)
CHECK_FUNCTION_EXISTS_GLIBC(strrchr HAVE_STRRCHR)
CHECK_FUNCTION_EXISTS_GLIBC(symlink HAVE_SYMLINK)
CHECK_FUNCTION_EXISTS_GLIBC(sysconf HAVE_SYSCONF)
CHECK_FUNCTION_EXISTS_GLIBC(timegm HAVE_TIMEGM)
CHECK_FUNCTION_EXISTS_GLIBC(tzset HAVE_TZSET)
CHECK_FUNCTION_EXISTS_GLIBC(unlinkat HAVE_UNLINKAT)
Expand Down
3 changes: 3 additions & 0 deletions build/cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@ typedef uint64_t uintmax_t;
/* Define to 1 if you have the `symlink' function. */
#cmakedefine HAVE_SYMLINK 1

/* Define to 1 if you have the `sysconf' function. */
#cmakedefine HAVE_SYSCONF 1

/* Define to 1 if you have the <sys/acl.h> header file. */
#cmakedefine HAVE_SYS_ACL_H 1

Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ AC_CHECK_FUNCS([nl_langinfo openat pipe poll posix_spawnp readlink readlinkat])
AC_CHECK_FUNCS([readpassphrase])
AC_CHECK_FUNCS([select setenv setlocale sigaction statfs statvfs])
AC_CHECK_FUNCS([strchr strdup strerror strncpy_s strnlen strrchr symlink])
AC_CHECK_FUNCS([sysconf])
AC_CHECK_FUNCS([timegm tzset unlinkat unsetenv utime utimensat utimes vfork])
AC_CHECK_FUNCS([wcrtomb wcscmp wcscpy wcslen wctomb wmemcmp wmemcpy wmemmove])
AC_CHECK_FUNCS([_fseeki64 _get_timezone])
Expand Down
14 changes: 13 additions & 1 deletion libarchive/archive_write_add_filter_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
Expand All @@ -38,6 +41,9 @@
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ZSTD_H
#include <zstd.h>
#endif
Expand Down Expand Up @@ -266,7 +272,13 @@ archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
if (string_to_number(value, &threads) != ARCHIVE_OK) {
return (ARCHIVE_WARN);
}
if (threads < 0) {

#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
if (threads == 0) {
threads = sysconf(_SC_NPROCESSORS_ONLN);
}
#endif
if (threads < 0 || threads > INT_MAX) {
return (ARCHIVE_WARN);
}
data->threads = (int)threads;
Expand Down
15 changes: 7 additions & 8 deletions tar/bsdtar.1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd March 1, 2024
.Dd April 23, 2024
.Dt TAR 1
.Os
.Sh NAME
Expand Down Expand Up @@ -644,14 +644,13 @@ A decimal integer from 4 to 7 specifying the lz4 compression block size
.It Cm lz4:block-dependence
Use the previous block of the block being compressed for
a compression dictionary to improve compression ratio.
.It Cm zstd:compression-level
A decimal integer specifying the zstd compression level. Supported values depend
.It Cm zstd:compression-level Ns = Ns Ar N
A decimal integer specifying the zstd compression level.
Supported values depend
on the library version, common values are from 1 to 22.
.It Cm zstd:threads
Specify the number of worker threads to use.
Setting threads to a special value 0 makes
.Xr zstd 1
use as many threads as there are CPU cores on the system.
.It Cm zstd:threads Ns = Ns Ar N
Specify the number of worker threads to use, or 0 to use as many
threads as there are CPU cores in the system.
.It Cm zstd:frame-per-file
Start a new compression frame at the beginning of each file in the
archive.
Expand Down

0 comments on commit 3efcadf

Please sign in to comment.